pg.ddx.io pgsql-committers@postgresql.org mailing list archivehelp / color / mirror / Atom feed
pgsql: Restore the portal-level snapshot after procedure COMMIT/ROLLBAC 8+ messages / 1 participants [nested] [flat]
* pgsql: Restore the portal-level snapshot after procedure COMMIT/ROLLBAC @ 2021-05-21 18:04 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 8+ messages in thread From: Tom Lane @ 2021-05-21 18:04 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Restore the portal-level snapshot after procedure COMMIT/ROLLBACK. COMMIT/ROLLBACK necessarily destroys all snapshots within the session. The original implementation of intra-procedure transactions just cavalierly did that, ignoring the fact that this left us executing in a rather different environment than normal. In particular, it turns out that handling of toasted datums depends rather critically on there being an outer ActiveSnapshot: otherwise, when SPI or the core executor pop whatever snapshot they used and return, it's unsafe to dereference any toasted datums that may appear in the query result. It's possible to demonstrate "no known snapshots" and "missing chunk number N for toast value" errors as a result of this oversight. Historically this outer snapshot has been held by the Portal code, and that seems like a good plan to preserve. So add infrastructure to pquery.c to allow re-establishing the Portal-owned snapshot if it's not there anymore, and add enough bookkeeping support that we can tell whether it is or not. We can't, however, just re-establish the Portal snapshot as part of COMMIT/ROLLBACK. As in normal transaction start, acquiring the first snapshot should wait until after SET and LOCK commands. Hence, teach spi.c about doing this at the right time. (Note that this patch doesn't fix the problem for any PLs that try to run intra-procedure transactions without using SPI to execute SQL commands.) This makes SPI's no_snapshots parameter rather a misnomer, so in HEAD, rename that to allow_nonatomic. replication/logical/worker.c also needs some fixes, because it wasn't careful to hold a snapshot open around AFTER trigger execution. That code doesn't use a Portal, which I suspect someday we're gonna have to fix. But for now, just rearrange the order of operations. This includes back-patching the recent addition of finish_estate() to centralize the cleanup logic there. This also back-patches commit 2ecfeda3e into v13, to improve the test coverage for worker.c (it was that test that exposed that worker.c's snapshot management is wrong). Per bug #15990 from Andreas Wicht. Back-patch to v11 where intra-procedure COMMIT was added. Discussion: https://postgr.es/m/15990-eee2ac466b11293d@postgresql.org Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/84f5c2908dad81e8622b0406beea580e40bb03ac Modified Files -------------- doc/src/sgml/spi.sgml | 12 +-- src/backend/commands/functioncmds.c | 15 +++ src/backend/executor/spi.c | 82 ++++++++++------ src/backend/replication/logical/worker.c | 23 ++--- src/backend/tcop/pquery.c | 135 ++++++++++++++++++++------ src/backend/utils/mmgr/portalmem.c | 56 +++++++++++ src/include/executor/spi.h | 2 +- src/include/tcop/pquery.h | 6 ++ src/include/utils/portal.h | 9 ++ src/pl/plpgsql/src/pl_exec.c | 31 +----- src/test/isolation/expected/plpgsql-toast.out | 27 ++++++ src/test/isolation/specs/plpgsql-toast.spec | 21 ++++ 12 files changed, 312 insertions(+), 107 deletions(-) ^ permalink raw reply [nested|flat] 8+ messages in thread
* pgsql: Restore the portal-level snapshot after procedure COMMIT/ROLLBAC @ 2021-05-21 18:04 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 8+ messages in thread From: Tom Lane @ 2021-05-21 18:04 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Restore the portal-level snapshot after procedure COMMIT/ROLLBACK. COMMIT/ROLLBACK necessarily destroys all snapshots within the session. The original implementation of intra-procedure transactions just cavalierly did that, ignoring the fact that this left us executing in a rather different environment than normal. In particular, it turns out that handling of toasted datums depends rather critically on there being an outer ActiveSnapshot: otherwise, when SPI or the core executor pop whatever snapshot they used and return, it's unsafe to dereference any toasted datums that may appear in the query result. It's possible to demonstrate "no known snapshots" and "missing chunk number N for toast value" errors as a result of this oversight. Historically this outer snapshot has been held by the Portal code, and that seems like a good plan to preserve. So add infrastructure to pquery.c to allow re-establishing the Portal-owned snapshot if it's not there anymore, and add enough bookkeeping support that we can tell whether it is or not. We can't, however, just re-establish the Portal snapshot as part of COMMIT/ROLLBACK. As in normal transaction start, acquiring the first snapshot should wait until after SET and LOCK commands. Hence, teach spi.c about doing this at the right time. (Note that this patch doesn't fix the problem for any PLs that try to run intra-procedure transactions without using SPI to execute SQL commands.) This makes SPI's no_snapshots parameter rather a misnomer, so in HEAD, rename that to allow_nonatomic. replication/logical/worker.c also needs some fixes, because it wasn't careful to hold a snapshot open around AFTER trigger execution. That code doesn't use a Portal, which I suspect someday we're gonna have to fix. But for now, just rearrange the order of operations. This includes back-patching the recent addition of finish_estate() to centralize the cleanup logic there. This also back-patches commit 2ecfeda3e into v13, to improve the test coverage for worker.c (it was that test that exposed that worker.c's snapshot management is wrong). Per bug #15990 from Andreas Wicht. Back-patch to v11 where intra-procedure COMMIT was added. Discussion: https://postgr.es/m/15990-eee2ac466b11293d@postgresql.org Branch ------ REL_13_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/d18ee6f92d9a22b4fae57f515797b2196bf385c7 Modified Files -------------- src/backend/commands/functioncmds.c | 15 +++ src/backend/executor/spi.c | 73 +++++++++----- src/backend/replication/logical/worker.c | 56 ++++++----- src/backend/tcop/pquery.c | 135 ++++++++++++++++++++------ src/backend/utils/mmgr/portalmem.c | 56 +++++++++++ src/include/executor/spi_priv.h | 2 +- src/include/tcop/pquery.h | 6 ++ src/include/utils/portal.h | 9 ++ src/pl/plpgsql/src/pl_exec.c | 32 +----- src/test/isolation/expected/plpgsql-toast.out | 27 ++++++ src/test/isolation/specs/plpgsql-toast.spec | 21 ++++ src/test/subscription/t/013_partition.pl | 91 ++++++++++++++++- 12 files changed, 409 insertions(+), 114 deletions(-) ^ permalink raw reply [nested|flat] 8+ messages in thread
* pgsql: Restore the portal-level snapshot after procedure COMMIT/ROLLBAC @ 2021-05-21 18:04 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 8+ messages in thread From: Tom Lane @ 2021-05-21 18:04 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Restore the portal-level snapshot after procedure COMMIT/ROLLBACK. COMMIT/ROLLBACK necessarily destroys all snapshots within the session. The original implementation of intra-procedure transactions just cavalierly did that, ignoring the fact that this left us executing in a rather different environment than normal. In particular, it turns out that handling of toasted datums depends rather critically on there being an outer ActiveSnapshot: otherwise, when SPI or the core executor pop whatever snapshot they used and return, it's unsafe to dereference any toasted datums that may appear in the query result. It's possible to demonstrate "no known snapshots" and "missing chunk number N for toast value" errors as a result of this oversight. Historically this outer snapshot has been held by the Portal code, and that seems like a good plan to preserve. So add infrastructure to pquery.c to allow re-establishing the Portal-owned snapshot if it's not there anymore, and add enough bookkeeping support that we can tell whether it is or not. We can't, however, just re-establish the Portal snapshot as part of COMMIT/ROLLBACK. As in normal transaction start, acquiring the first snapshot should wait until after SET and LOCK commands. Hence, teach spi.c about doing this at the right time. (Note that this patch doesn't fix the problem for any PLs that try to run intra-procedure transactions without using SPI to execute SQL commands.) This makes SPI's no_snapshots parameter rather a misnomer, so in HEAD, rename that to allow_nonatomic. replication/logical/worker.c also needs some fixes, because it wasn't careful to hold a snapshot open around AFTER trigger execution. That code doesn't use a Portal, which I suspect someday we're gonna have to fix. But for now, just rearrange the order of operations. This includes back-patching the recent addition of finish_estate() to centralize the cleanup logic there. This also back-patches commit 2ecfeda3e into v13, to improve the test coverage for worker.c (it was that test that exposed that worker.c's snapshot management is wrong). Per bug #15990 from Andreas Wicht. Back-patch to v11 where intra-procedure COMMIT was added. Discussion: https://postgr.es/m/15990-eee2ac466b11293d@postgresql.org Branch ------ REL_12_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/41c6a5bec25e720d98bd60d77dd5c2939189ed3c Modified Files -------------- src/backend/commands/functioncmds.c | 15 +++ src/backend/executor/spi.c | 73 +++++++++----- src/backend/replication/logical/worker.c | 55 ++++++----- src/backend/tcop/pquery.c | 135 ++++++++++++++++++++------ src/backend/utils/mmgr/portalmem.c | 56 +++++++++++ src/include/executor/spi_priv.h | 2 +- src/include/tcop/pquery.h | 6 ++ src/include/utils/portal.h | 9 ++ src/pl/plpgsql/src/pl_exec.c | 32 +----- src/test/isolation/expected/plpgsql-toast.out | 27 ++++++ src/test/isolation/specs/plpgsql-toast.spec | 21 ++++ 11 files changed, 321 insertions(+), 110 deletions(-) ^ permalink raw reply [nested|flat] 8+ messages in thread
* pgsql: Restore the portal-level snapshot after procedure COMMIT/ROLLBAC @ 2021-05-21 18:04 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 8+ messages in thread From: Tom Lane @ 2021-05-21 18:04 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Restore the portal-level snapshot after procedure COMMIT/ROLLBACK. COMMIT/ROLLBACK necessarily destroys all snapshots within the session. The original implementation of intra-procedure transactions just cavalierly did that, ignoring the fact that this left us executing in a rather different environment than normal. In particular, it turns out that handling of toasted datums depends rather critically on there being an outer ActiveSnapshot: otherwise, when SPI or the core executor pop whatever snapshot they used and return, it's unsafe to dereference any toasted datums that may appear in the query result. It's possible to demonstrate "no known snapshots" and "missing chunk number N for toast value" errors as a result of this oversight. Historically this outer snapshot has been held by the Portal code, and that seems like a good plan to preserve. So add infrastructure to pquery.c to allow re-establishing the Portal-owned snapshot if it's not there anymore, and add enough bookkeeping support that we can tell whether it is or not. We can't, however, just re-establish the Portal snapshot as part of COMMIT/ROLLBACK. As in normal transaction start, acquiring the first snapshot should wait until after SET and LOCK commands. Hence, teach spi.c about doing this at the right time. (Note that this patch doesn't fix the problem for any PLs that try to run intra-procedure transactions without using SPI to execute SQL commands.) This makes SPI's no_snapshots parameter rather a misnomer, so in HEAD, rename that to allow_nonatomic. replication/logical/worker.c also needs some fixes, because it wasn't careful to hold a snapshot open around AFTER trigger execution. That code doesn't use a Portal, which I suspect someday we're gonna have to fix. But for now, just rearrange the order of operations. This includes back-patching the recent addition of finish_estate() to centralize the cleanup logic there. This also back-patches commit 2ecfeda3e into v13, to improve the test coverage for worker.c (it was that test that exposed that worker.c's snapshot management is wrong). Per bug #15990 from Andreas Wicht. Back-patch to v11 where intra-procedure COMMIT was added. Discussion: https://postgr.es/m/15990-eee2ac466b11293d@postgresql.org Branch ------ REL_11_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/ef94805096229ee3573624465a76ca11d2bd8529 Modified Files -------------- src/backend/commands/functioncmds.c | 15 +++ src/backend/executor/spi.c | 73 +++++++++----- src/backend/replication/logical/worker.c | 55 ++++++----- src/backend/tcop/pquery.c | 135 ++++++++++++++++++++------ src/backend/utils/mmgr/portalmem.c | 56 +++++++++++ src/include/executor/spi_priv.h | 2 +- src/include/tcop/pquery.h | 6 ++ src/include/utils/portal.h | 9 ++ src/pl/plpgsql/src/pl_exec.c | 32 +----- src/test/isolation/expected/plpgsql-toast.out | 27 ++++++ src/test/isolation/specs/plpgsql-toast.spec | 21 ++++ 11 files changed, 321 insertions(+), 110 deletions(-) ^ permalink raw reply [nested|flat] 8+ messages in thread
* pgsql: Restore the portal-level snapshot for simple expressions, too. @ 2021-06-22 21:48 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 8+ messages in thread From: Tom Lane @ 2021-06-22 21:48 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Restore the portal-level snapshot for simple expressions, too. Commits 84f5c2908 et al missed the need to cover plpgsql's "simple expression" code path. If the first thing we execute after a COMMIT/ROLLBACK is one of those, rather than a full-fledged SPI command, we must explicitly do EnsurePortalSnapshotExists() to make sure we have an outer snapshot. Note that it wouldn't be good enough to just push a snapshot for the duration of the expression execution: what comes back might be toasted, so we'd better have a snapshot protecting it. The test case demonstrating this fact cheats a bit by marking a SQL function immutable even though it fetches from a table. That's nothing that users haven't been seen to do, though. Per report from Jim Nasby. Back-patch to v11, like the previous fix. Discussion: https://postgr.es/m/378885e4-f85f-fc28-6c91-c4d1c080bf26@amazon.com Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/d102aafb6259a6a412803d4b1d8c4f00aa17f67e Modified Files -------------- src/pl/plpgsql/src/expected/plpgsql_transaction.out | 18 ++++++++++++++++++ src/pl/plpgsql/src/pl_exec.c | 10 ++++++++++ src/pl/plpgsql/src/sql/plpgsql_transaction.sql | 21 +++++++++++++++++++++ 3 files changed, 49 insertions(+) ^ permalink raw reply [nested|flat] 8+ messages in thread
* pgsql: Restore the portal-level snapshot for simple expressions, too. @ 2021-06-22 21:48 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 8+ messages in thread From: Tom Lane @ 2021-06-22 21:48 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Restore the portal-level snapshot for simple expressions, too. Commits 84f5c2908 et al missed the need to cover plpgsql's "simple expression" code path. If the first thing we execute after a COMMIT/ROLLBACK is one of those, rather than a full-fledged SPI command, we must explicitly do EnsurePortalSnapshotExists() to make sure we have an outer snapshot. Note that it wouldn't be good enough to just push a snapshot for the duration of the expression execution: what comes back might be toasted, so we'd better have a snapshot protecting it. The test case demonstrating this fact cheats a bit by marking a SQL function immutable even though it fetches from a table. That's nothing that users haven't been seen to do, though. Per report from Jim Nasby. Back-patch to v11, like the previous fix. Discussion: https://postgr.es/m/378885e4-f85f-fc28-6c91-c4d1c080bf26@amazon.com Branch ------ REL_13_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/6f1321d5ae8e0db1500def46f12eadf94521f5ee Modified Files -------------- src/pl/plpgsql/src/expected/plpgsql_transaction.out | 18 ++++++++++++++++++ src/pl/plpgsql/src/pl_exec.c | 10 ++++++++++ src/pl/plpgsql/src/sql/plpgsql_transaction.sql | 21 +++++++++++++++++++++ 3 files changed, 49 insertions(+) ^ permalink raw reply [nested|flat] 8+ messages in thread
* pgsql: Restore the portal-level snapshot for simple expressions, too. @ 2021-06-22 21:48 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 8+ messages in thread From: Tom Lane @ 2021-06-22 21:48 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Restore the portal-level snapshot for simple expressions, too. Commits 84f5c2908 et al missed the need to cover plpgsql's "simple expression" code path. If the first thing we execute after a COMMIT/ROLLBACK is one of those, rather than a full-fledged SPI command, we must explicitly do EnsurePortalSnapshotExists() to make sure we have an outer snapshot. Note that it wouldn't be good enough to just push a snapshot for the duration of the expression execution: what comes back might be toasted, so we'd better have a snapshot protecting it. The test case demonstrating this fact cheats a bit by marking a SQL function immutable even though it fetches from a table. That's nothing that users haven't been seen to do, though. Per report from Jim Nasby. Back-patch to v11, like the previous fix. Discussion: https://postgr.es/m/378885e4-f85f-fc28-6c91-c4d1c080bf26@amazon.com Branch ------ REL_12_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/29d5d5761aa8695e1226f1f76b7f76a2a4b195a0 Modified Files -------------- src/pl/plpgsql/src/expected/plpgsql_transaction.out | 18 ++++++++++++++++++ src/pl/plpgsql/src/pl_exec.c | 10 ++++++++++ src/pl/plpgsql/src/sql/plpgsql_transaction.sql | 21 +++++++++++++++++++++ 3 files changed, 49 insertions(+) ^ permalink raw reply [nested|flat] 8+ messages in thread
* pgsql: Restore the portal-level snapshot for simple expressions, too. @ 2021-06-22 21:48 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 8+ messages in thread From: Tom Lane @ 2021-06-22 21:48 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Restore the portal-level snapshot for simple expressions, too. Commits 84f5c2908 et al missed the need to cover plpgsql's "simple expression" code path. If the first thing we execute after a COMMIT/ROLLBACK is one of those, rather than a full-fledged SPI command, we must explicitly do EnsurePortalSnapshotExists() to make sure we have an outer snapshot. Note that it wouldn't be good enough to just push a snapshot for the duration of the expression execution: what comes back might be toasted, so we'd better have a snapshot protecting it. The test case demonstrating this fact cheats a bit by marking a SQL function immutable even though it fetches from a table. That's nothing that users haven't been seen to do, though. Per report from Jim Nasby. Back-patch to v11, like the previous fix. Discussion: https://postgr.es/m/378885e4-f85f-fc28-6c91-c4d1c080bf26@amazon.com Branch ------ REL_11_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/77200c569238dd88b4134b09cfa65e3598136f19 Modified Files -------------- src/pl/plpgsql/src/expected/plpgsql_transaction.out | 18 ++++++++++++++++++ src/pl/plpgsql/src/pl_exec.c | 10 ++++++++++ src/pl/plpgsql/src/sql/plpgsql_transaction.sql | 21 +++++++++++++++++++++ 3 files changed, 49 insertions(+) ^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2021-06-22 21:48 UTC | newest] Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2021-05-21 18:04 pgsql: Restore the portal-level snapshot after procedure COMMIT/ROLLBAC Tom Lane <tgl@sss.pgh.pa.us> 2021-05-21 18:04 pgsql: Restore the portal-level snapshot after procedure COMMIT/ROLLBAC Tom Lane <tgl@sss.pgh.pa.us> 2021-05-21 18:04 pgsql: Restore the portal-level snapshot after procedure COMMIT/ROLLBAC Tom Lane <tgl@sss.pgh.pa.us> 2021-05-21 18:04 pgsql: Restore the portal-level snapshot after procedure COMMIT/ROLLBAC Tom Lane <tgl@sss.pgh.pa.us> 2021-06-22 21:48 pgsql: Restore the portal-level snapshot for simple expressions, too. Tom Lane <tgl@sss.pgh.pa.us> 2021-06-22 21:48 pgsql: Restore the portal-level snapshot for simple expressions, too. Tom Lane <tgl@sss.pgh.pa.us> 2021-06-22 21:48 pgsql: Restore the portal-level snapshot for simple expressions, too. Tom Lane <tgl@sss.pgh.pa.us> 2021-06-22 21:48 pgsql: Restore the portal-level snapshot for simple expressions, too. Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions for how to clone and mirror all data and code used for this inbox