# Copyright (c) 2026, PostgreSQL Global Development Group use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; # Test only temporary, isolated instances; never migrate user clusters. my $node = PostgreSQL::Test::Cluster->new('temp_catalog_rbtree'); $node->init; $node->start; my $a = $node->background_psql('postgres'); my $b = $node->background_psql('postgres'); is($a->query_safe('SHOW enable_temp_memory_catalog'), 'off', 'default GUC remains off'); $a->query_safe(q{ SET enable_temp_memory_catalog=on; DO $body$ BEGIN FOR i IN 1..64 LOOP EXECUTE format('CREATE TEMP TABLE tc_rb_%s(id integer, v integer DEFAULT 7)', i); END LOOP; END $body$; }); is($a->query_safe(q{ SELECT count(*) FROM generate_series(1,64) g WHERE to_regclass(format('pg_temp.tc_rb_%s',g)) IS NOT NULL }), '64', '64 memory catalog objects resolve after indexed lookups'); is($a->query_safe(q{ SELECT EXISTS (SELECT FROM pg_backend_memory_contexts WHERE name='TempCatalogIndexContext') }), 't', 'derived RBTree index was created, not only a list scan'); is($b->query_safe(q{ SELECT to_regclass('pg_temp.tc_rb_1') IS NULL }), 't', 'other session does not see temporary catalog objects'); $b->query_safe(q{ SET enable_temp_memory_catalog=on; CREATE TEMP TABLE tc_rb_1(id integer,v integer); INSERT INTO tc_rb_1 VALUES(42,84); }); is($b->query_safe('SELECT v FROM tc_rb_1'), '84', 'second session has independent temporary relation'); is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_1') IS NOT NULL}), 't', 'first session preserved same-named temporary relation'); $a->query_safe(q{ DROP TABLE tc_rb_32; DROP TABLE tc_rb_16; DROP TABLE tc_rb_48; }); is($a->query_safe(q{ SELECT (to_regclass('pg_temp.tc_rb_32') IS NULL AND to_regclass('pg_temp.tc_rb_31') IS NOT NULL AND to_regclass('pg_temp.tc_rb_33') IS NOT NULL) }), 't', 'deleted middle key does not corrupt its RBTree neighbors'); is($a->query_safe(q{ SELECT (to_regclass('pg_temp.tc_rb_16') IS NULL AND to_regclass('pg_temp.tc_rb_48') IS NULL) }), 't', 'independent deletions remove only matching keys'); $a->query_safe('ALTER TABLE tc_rb_31 RENAME TO tc_rb_31_renamed'); is($a->query_safe(q{ SELECT (to_regclass('pg_temp.tc_rb_31') IS NULL AND to_regclass('pg_temp.tc_rb_31_renamed') IS NOT NULL) }), 't', 'catalog rename invalidates old indexed key'); $a->query_safe(q{ BEGIN; SAVEPOINT rb_created; CREATE TEMP TABLE tc_rb_transient(x integer); }); is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_transient') IS NOT NULL}), 't', 'created tuple visible before subtransaction rollback'); $a->query_safe('ROLLBACK TO SAVEPOINT rb_created; RELEASE SAVEPOINT rb_created; COMMIT'); is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_transient') IS NULL}), 't', 'rollback removed tuple and its indexed key'); $a->query_safe(q{ BEGIN; SAVEPOINT rb_deleted; DROP TABLE tc_rb_33; }); is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_33') IS NULL}), 't', 'deleted tuple invisible in current subtransaction'); $a->query_safe('ROLLBACK TO SAVEPOINT rb_deleted; RELEASE SAVEPOINT rb_deleted; COMMIT'); is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_33') IS NOT NULL}), 't', 'undo restores deleted tuple and indexed key'); $a->query_safe('INSERT INTO tc_rb_34(id,v) VALUES (1,700)'); $a->query_safe('PREPARE rb_reader AS SELECT v FROM tc_rb_34 WHERE id=1'); is($a->query_safe('EXECUTE rb_reader'), '700', 'prepared reader accesses live temporary relation'); $a->query_safe('ALTER TABLE tc_rb_30 RENAME TO tc_rb_30_renamed'); is($a->query_safe('EXECUTE rb_reader'), '700', 'prepared reader survives an unrelated catalog rename'); $a->query_safe(q{ SET enable_temp_memory_catalog=off; ALTER TABLE tc_rb_34 ALTER COLUMN v TYPE bigint USING v::bigint; SET enable_temp_memory_catalog=on; }); is($a->query_safe('SELECT pg_typeof(v)::text FROM tc_rb_34 LIMIT 1'), 'bigint', 'column type update is visible through catalog lookup'); is($a->query_safe('SELECT v FROM tc_rb_34 WHERE id=1'), '700', 'column type update preserves existing data'); $a->query_safe(q{ BEGIN; CREATE TEMP TABLE tc_rb_top_abort(x integer); }); is($a->query_safe(q{ SELECT to_regclass('pg_temp.tc_rb_top_abort') IS NOT NULL }), 't', 'top-level transaction sees its new temporary table'); $a->query_safe('ROLLBACK'); is($a->query_safe(q{ SELECT to_regclass('pg_temp.tc_rb_top_abort') IS NULL }), 't', 'top-level abort removes all newly created catalog tuples'); $a->query_safe('BEGIN; DROP TABLE tc_rb_35; COMMIT'); is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_35') IS NULL}), 't', 'committed deletion does not leave stale RBTree entry'); $a->query_safe('CREATE TEMP TABLE tc_rb_32(id integer,v integer)'); is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_32') IS NOT NULL}), 't', 'previously deleted name may be reused'); $a->query_safe('DROP TABLE tc_rb_32'); is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_32') IS NULL}), 't', 'reused key removed without touching other buckets'); $b->query_safe('DROP TABLE tc_rb_1'); is($b->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_1') IS NULL}), 't', 'second session removes only its own tuple'); is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_1') IS NOT NULL}), 't', 'first session remains unaffected by second session cleanup'); # Clean up inside the test connection, not by deleting a foreign PGDATA. $a->query_safe(q{ DO $body$ BEGIN FOR i IN 1..64 LOOP IF to_regclass(format('pg_temp.tc_rb_%s', i)) IS NOT NULL THEN EXECUTE format('DROP TABLE pg_temp.tc_rb_%s', i); END IF; END LOOP; END $body$; DROP TABLE tc_rb_31_renamed; DROP TABLE tc_rb_30_renamed; }); is($a->query_safe(q{ SELECT (to_regclass('pg_temp.tc_rb_1') IS NULL AND to_regclass('pg_temp.tc_rb_31_renamed') IS NULL AND to_regclass('pg_temp.tc_rb_30_renamed') IS NULL) }), 't', 'all task-owned temporary table names are removed'); $a->quit; $b->quit; $node->stop; done_testing();