postgresql/src/include/executor
Tom Lane d18ee6f92d 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
2021-05-21 14:03:53 -04:00
..
execdebug.h Implement Incremental Sort 2020-04-06 21:35:10 +02:00
execdesc.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
execExpr.h Redesign the caching done by get_cached_rowtype(). 2021-04-13 13:37:07 -04:00
execParallel.h Add infrastructure to track WAL usage. 2020-04-04 10:02:08 +05:30
execPartition.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
executor.h Fix mishandling of resjunk columns in ON CONFLICT ... UPDATE tlists. 2021-05-10 11:02:29 -04:00
functions.h Fix list-munging bug that broke SQL function result coercions. 2020-10-19 14:33:10 -04:00
hashjoin.h Revert "Fix race in Parallel Hash Join batch cleanup." 2021-03-18 01:00:56 +13:00
instrument.h Change the display of WAL usage statistics in Explain. 2020-05-05 08:00:53 +05:30
nodeAgg.h HashAgg: use better cardinality estimate for recursive spilling. 2020-07-28 23:17:23 -07:00
nodeAppend.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeBitmapAnd.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeBitmapHeapscan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeBitmapIndexscan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeBitmapOr.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeCtescan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeCustom.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeForeignscan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeFunctionscan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeGather.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeGatherMerge.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeGroup.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeHash.h Add hash_mem_multiplier GUC. 2020-07-29 14:14:57 -07:00
nodeHashjoin.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeIncrementalSort.h Implement Incremental Sort 2020-04-06 21:35:10 +02:00
nodeIndexonlyscan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeIndexscan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeLimit.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeLockRows.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeMaterial.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeMergeAppend.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeMergejoin.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeModifyTable.h Optimize update of tables with generated columns 2020-02-17 15:20:58 +01:00
nodeNamedtuplestorescan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeNestloop.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeProjectSet.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeRecursiveunion.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeResult.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeSamplescan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeSeqscan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeSetOp.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeSort.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeSubplan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeSubqueryscan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeTableFuncscan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeTidscan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeUnique.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeValuesscan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeWindowAgg.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nodeWorktablescan.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
spi.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
spi_priv.h Restore the portal-level snapshot after procedure COMMIT/ROLLBACK. 2021-05-21 14:03:53 -04:00
tablefunc.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
tqueue.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
tstoreReceiver.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
tuptable.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00