Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/backend/utils/time/sharedsnapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
122 changes: 122 additions & 0 deletions src/test/isolation2/expected/sharedsnapshot.out
Original file line number Diff line number Diff line change
Expand Up @@ -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
60 changes: 60 additions & 0 deletions src/test/isolation2/sql/sharedsnapshot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;