From 7b5d4c29ed0262e537026cb3a85161d6cf98abcc Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 1 Oct 2021 11:10:12 -0400 Subject: Fix Portal snapshot tracking to handle subtransactions properly. Commit 84f5c2908 forgot to consider the possibility that EnsurePortalSnapshotExists could run inside a subtransaction with lifespan shorter than the Portal's. In that case, the new active snapshot would be popped at the end of the subtransaction, leaving a dangling pointer in the Portal, with mayhem ensuing. To fix, make sure the ActiveSnapshot stack entry is marked with the same subtransaction nesting level as the associated Portal. It's certainly safe to do so since we won't be here at all unless the stack is empty; hence we can't create an out-of-order stack. Let's also apply this logic in the case where PortalRunUtility sets portalSnapshot, just to be sure that path can't cause similar problems. It's slightly less clear that that path can't create an out-of-order stack, so add an assertion guarding it. Report and patch by Bertrand Drouvot (with kibitzing by me). Back-patch to v11, like the previous commit. Discussion: https://postgr.es/m/ff82b8c5-77f4-3fe7-6028-fcf3303e82dd@amazon.com --- .../plpgsql/src/expected/plpgsql_transaction.out | 28 ++++++++++++++++++++++ src/pl/plpgsql/src/sql/plpgsql_transaction.sql | 21 ++++++++++++++++ 2 files changed, 49 insertions(+) (limited to 'src/pl') diff --git a/src/pl/plpgsql/src/expected/plpgsql_transaction.out b/src/pl/plpgsql/src/expected/plpgsql_transaction.out index f79f847321c..254e5b7a706 100644 --- a/src/pl/plpgsql/src/expected/plpgsql_transaction.out +++ b/src/pl/plpgsql/src/expected/plpgsql_transaction.out @@ -430,6 +430,34 @@ SELECT * FROM test1; ---+--- (0 rows) +-- test commit/rollback inside exception handler, too +TRUNCATE test1; +DO LANGUAGE plpgsql $$ +BEGIN + FOR i IN 1..10 LOOP + BEGIN + INSERT INTO test1 VALUES (i, 'good'); + INSERT INTO test1 VALUES (i/0, 'bad'); + EXCEPTION + WHEN division_by_zero THEN + INSERT INTO test1 VALUES (i, 'exception'); + IF (i % 3) > 0 THEN COMMIT; ELSE ROLLBACK; END IF; + END; + END LOOP; +END; +$$; +SELECT * FROM test1; + a | b +----+----------- + 1 | exception + 2 | exception + 4 | exception + 5 | exception + 7 | exception + 8 | exception + 10 | exception +(7 rows) + -- detoast result of simple expression after commit CREATE TEMP TABLE test4(f1 text); ALTER TABLE test4 ALTER COLUMN f1 SET STORAGE EXTERNAL; -- disable compression diff --git a/src/pl/plpgsql/src/sql/plpgsql_transaction.sql b/src/pl/plpgsql/src/sql/plpgsql_transaction.sql index 888ddccaceb..8d76d00daaa 100644 --- a/src/pl/plpgsql/src/sql/plpgsql_transaction.sql +++ b/src/pl/plpgsql/src/sql/plpgsql_transaction.sql @@ -354,6 +354,27 @@ $$; SELECT * FROM test1; +-- test commit/rollback inside exception handler, too +TRUNCATE test1; + +DO LANGUAGE plpgsql $$ +BEGIN + FOR i IN 1..10 LOOP + BEGIN + INSERT INTO test1 VALUES (i, 'good'); + INSERT INTO test1 VALUES (i/0, 'bad'); + EXCEPTION + WHEN division_by_zero THEN + INSERT INTO test1 VALUES (i, 'exception'); + IF (i % 3) > 0 THEN COMMIT; ELSE ROLLBACK; END IF; + END; + END LOOP; +END; +$$; + +SELECT * FROM test1; + + -- detoast result of simple expression after commit CREATE TEMP TABLE test4(f1 text); ALTER TABLE test4 ALTER COLUMN f1 SET STORAGE EXTERNAL; -- disable compression -- cgit v1.2.3