From 5432d013cd90c343b0b9fd1ade7b6e0433fd2fd3 Mon Sep 17 00:00:00 2001 From: Denis Efimov <71937078+doefimov@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:33:22 +0300 Subject: [PATCH] Preserve subtransaction state in shared cursor snapshots Copy subxip, subxcnt, and suboverflowed from the restored snapshot into the cursor reader's snapshot. Missing subtransaction state can leave catalog tuples invisible after the writer commits. Add an isolation2 case that scans the catalogs through cursors while a partition is created in a subtransaction, then checks visibility after commit. --- src/backend/utils/time/sharedsnapshot.c | 11 ++ .../isolation2/expected/sharedsnapshot.out | 122 ++++++++++++++++++ src/test/isolation2/sql/sharedsnapshot.sql | 60 +++++++++ 3 files changed, 193 insertions(+) diff --git a/src/backend/utils/time/sharedsnapshot.c b/src/backend/utils/time/sharedsnapshot.c index 09caa10110e..216bcec4cde 100644 --- a/src/backend/utils/time/sharedsnapshot.c +++ b/src/backend/utils/time/sharedsnapshot.c @@ -766,6 +766,17 @@ readSharedLocalSnapshot_forCursor(Snapshot snapshot, DtxContext distributedTrans /* zero out the slack in the xip-array */ memset(snapshot->xip + snapshot->xcnt, 0, (xipEntryCount - snapshot->xcnt)*sizeof(TransactionId)); + /* Preserve the writer's view of in-progress subtransactions. */ + if (dumpsnapshot->subxcnt < 0 || + dumpsnapshot->subxcnt > GetMaxSnapshotSubxidCount()) + elog(ERROR, "shared cursor snapshot has invalid subxid count %d", + dumpsnapshot->subxcnt); + snapshot->subxcnt = dumpsnapshot->subxcnt; + snapshot->suboverflowed = dumpsnapshot->suboverflowed; + if (snapshot->subxcnt > 0) + memcpy(snapshot->subxip, dumpsnapshot->subxip, + snapshot->subxcnt * sizeof(TransactionId)); + snapshot->curcid = dumpsnapshot->curcid; SetSharedTransactionId_reader( diff --git a/src/test/isolation2/expected/sharedsnapshot.out b/src/test/isolation2/expected/sharedsnapshot.out index 4342368ff7f..1f4b1a8f785 100644 --- a/src/test/isolation2/expected/sharedsnapshot.out +++ b/src/test/isolation2/expected/sharedsnapshot.out @@ -32,3 +32,125 @@ CALL (1 row) 2: drop table dummy2, dummy1; DROP + +-- Cursor readers must retain the writer's in-progress subtransaction IDs. +8: CREATE SCHEMA sharedsnapshot_cursor; +CREATE +8: CREATE TABLE sharedsnapshot_cursor.anchor(v oid) DISTRIBUTED BY (v); +CREATE +8: INSERT INTO sharedsnapshot_cursor.anchor VALUES ('pg_class'::regclass::oid); +INSERT 1 +8: CREATE TABLE sharedsnapshot_cursor.p(a int) USING ao_column DISTRIBUTED BY (a) PARTITION BY LIST (a); +CREATE +8: CREATE TABLE sharedsnapshot_cursor.p0 PARTITION OF sharedsnapshot_cursor.p FOR VALUES IN (0); +CREATE +8: CREATE FUNCTION sharedsnapshot_cursor.add_part(child text, partvalue int) RETURNS void LANGUAGE plpgsql AS $$ BEGIN /*in func*/ EXECUTE format('CREATE TABLE sharedsnapshot_cursor.%I PARTITION OF sharedsnapshot_cursor.p FOR VALUES IN (%s)', child, partvalue); /*in func*/ EXCEPTION WHEN OTHERS THEN /*in func*/ RAISE; /*in func*/ END; /*in func*/ $$; +CREATE + +4: SET optimizer = off; +SET +4: SET enable_indexscan = off; +SET +4: SET enable_indexonlyscan = off; +SET +4: SET enable_bitmapscan = off; +SET + +-- Keep the snapshot horizon below the DDL subtransaction. +7: BEGIN; +BEGIN +7: DO $$ BEGIN PERFORM txid_current(); END $$; +DO + +-- Reuse the entry DB reader with an older local snapshot. +4: BEGIN; +BEGIN +4: DECLARE warm_class CURSOR FOR SELECT c.oid FROM pg_class c JOIN sharedsnapshot_cursor.anchor a ON c.oid = a.v; +DECLARE +4: FETCH ALL FROM warm_class; + oid +------ + 1259 +(1 row) +4: COMMIT; +COMMIT +4: BEGIN; +BEGIN +4: DECLARE warm_type CURSOR FOR SELECT c.oid FROM pg_type c JOIN sharedsnapshot_cursor.anchor a ON c.oid = a.v; +DECLARE +4: FETCH ALL FROM warm_type; + oid +----- +(0 rows) +4: COMMIT; +COMMIT + +5: BEGIN; +BEGIN +5: DO $$ BEGIN PERFORM txid_current(); END $$; +DO +5: SELECT sharedsnapshot_cursor.add_part('p1', 1); + add_part +---------- + +(1 row) + +-- A newer completed XID makes the subtransaction fall below snapshot xmax. +6: DO $$ BEGIN PERFORM txid_current(); END $$; +DO + +-- Scan the uncommitted catalog tuples without marking their xmin invalid. +4: BEGIN; +BEGIN +4: DECLARE probe_class CURSOR FOR SELECT c.oid FROM pg_class c JOIN sharedsnapshot_cursor.anchor a ON c.oid = a.v; +DECLARE +4: FETCH ALL FROM probe_class; + oid +------ + 1259 +(1 row) +4: COMMIT; +COMMIT +4: BEGIN; +BEGIN +4: DECLARE probe_type CURSOR FOR SELECT c.oid FROM pg_type c JOIN sharedsnapshot_cursor.anchor a ON c.oid = a.v; +DECLARE +4: FETCH ALL FROM probe_type; + oid +----- +(0 rows) +4: COMMIT; +COMMIT +5: COMMIT; +COMMIT + +-- A fresh session must see the committed catalogs and build the partition tree. +9: SELECT count(*) FROM pg_class WHERE relnamespace = 'sharedsnapshot_cursor'::regnamespace AND relname = 'p1'; + count +------- + 1 +(1 row) +9: SELECT count(*) FROM pg_type WHERE typnamespace = 'sharedsnapshot_cursor'::regnamespace AND typname IN ('p1', '_p1'); + count +------- + 2 +(1 row) +9: SELECT count(*) FROM sharedsnapshot_cursor.p; + count +------- + 0 +(1 row) +9: SELECT sharedsnapshot_cursor.add_part('p2', 2); + add_part +---------- + +(1 row) +9: SELECT count(*) FROM pg_inherits WHERE inhparent = 'sharedsnapshot_cursor.p'::regclass; + count +------- + 3 +(1 row) +7: ROLLBACK; +ROLLBACK +9: DROP SCHEMA sharedsnapshot_cursor CASCADE; +DROP diff --git a/src/test/isolation2/sql/sharedsnapshot.sql b/src/test/isolation2/sql/sharedsnapshot.sql index 74198767f1a..692be1e7ad1 100644 --- a/src/test/isolation2/sql/sharedsnapshot.sql +++ b/src/test/isolation2/sql/sharedsnapshot.sql @@ -27,3 +27,63 @@ END (date '2017-02-01') EXCLUSIVE EVERY (INTERVAL '1 day') ); 2: call test('2020-01-02', '2020-01-03'); 2: select count(1) from pg_inherits where inhparent = 'dummy2'::regclass; 2: drop table dummy2, dummy1; + +-- Cursor readers must retain the writer's in-progress subtransaction IDs. +8: CREATE SCHEMA sharedsnapshot_cursor; +8: CREATE TABLE sharedsnapshot_cursor.anchor(v oid) DISTRIBUTED BY (v); +8: INSERT INTO sharedsnapshot_cursor.anchor VALUES ('pg_class'::regclass::oid); +8: CREATE TABLE sharedsnapshot_cursor.p(a int) USING ao_column DISTRIBUTED BY (a) PARTITION BY LIST (a); +8: CREATE TABLE sharedsnapshot_cursor.p0 PARTITION OF sharedsnapshot_cursor.p FOR VALUES IN (0); +8: CREATE FUNCTION sharedsnapshot_cursor.add_part(child text, partvalue int) RETURNS void LANGUAGE plpgsql AS $$ + BEGIN /*in func*/ + EXECUTE format('CREATE TABLE sharedsnapshot_cursor.%I PARTITION OF sharedsnapshot_cursor.p FOR VALUES IN (%s)', child, partvalue); /*in func*/ + EXCEPTION WHEN OTHERS THEN /*in func*/ + RAISE; /*in func*/ + END; /*in func*/ + $$; + +4: SET optimizer = off; +4: SET enable_indexscan = off; +4: SET enable_indexonlyscan = off; +4: SET enable_bitmapscan = off; + +-- Keep the snapshot horizon below the DDL subtransaction. +7: BEGIN; +7: DO $$ BEGIN PERFORM txid_current(); END $$; + +-- Reuse the entry DB reader with an older local snapshot. +4: BEGIN; +4: DECLARE warm_class CURSOR FOR SELECT c.oid FROM pg_class c JOIN sharedsnapshot_cursor.anchor a ON c.oid = a.v; +4: FETCH ALL FROM warm_class; +4: COMMIT; +4: BEGIN; +4: DECLARE warm_type CURSOR FOR SELECT c.oid FROM pg_type c JOIN sharedsnapshot_cursor.anchor a ON c.oid = a.v; +4: FETCH ALL FROM warm_type; +4: COMMIT; + +5: BEGIN; +5: DO $$ BEGIN PERFORM txid_current(); END $$; +5: SELECT sharedsnapshot_cursor.add_part('p1', 1); + +-- A newer completed XID makes the subtransaction fall below snapshot xmax. +6: DO $$ BEGIN PERFORM txid_current(); END $$; + +-- Scan the uncommitted catalog tuples without marking their xmin invalid. +4: BEGIN; +4: DECLARE probe_class CURSOR FOR SELECT c.oid FROM pg_class c JOIN sharedsnapshot_cursor.anchor a ON c.oid = a.v; +4: FETCH ALL FROM probe_class; +4: COMMIT; +4: BEGIN; +4: DECLARE probe_type CURSOR FOR SELECT c.oid FROM pg_type c JOIN sharedsnapshot_cursor.anchor a ON c.oid = a.v; +4: FETCH ALL FROM probe_type; +4: COMMIT; +5: COMMIT; + +-- A fresh session must see the committed catalogs and build the partition tree. +9: SELECT count(*) FROM pg_class WHERE relnamespace = 'sharedsnapshot_cursor'::regnamespace AND relname = 'p1'; +9: SELECT count(*) FROM pg_type WHERE typnamespace = 'sharedsnapshot_cursor'::regnamespace AND typname IN ('p1', '_p1'); +9: SELECT count(*) FROM sharedsnapshot_cursor.p; +9: SELECT sharedsnapshot_cursor.add_part('p2', 2); +9: SELECT count(*) FROM pg_inherits WHERE inhparent = 'sharedsnapshot_cursor.p'::regclass; +7: ROLLBACK; +9: DROP SCHEMA sharedsnapshot_cursor CASCADE;