Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 41c6a5b

Browse files
committed
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 2ecfeda 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
1 parent 18c6242 commit 41c6a5b

File tree

11 files changed

+321
-110
lines changed

11 files changed

+321
-110
lines changed

src/backend/commands/functioncmds.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "parser/parse_func.h"
6262
#include "parser/parse_type.h"
6363
#include "pgstat.h"
64+
#include "tcop/pquery.h"
6465
#include "utils/acl.h"
6566
#include "utils/builtins.h"
6667
#include "utils/fmgroids.h"
@@ -2431,6 +2432,20 @@ ExecuteCallStmt(CallStmt *stmt, ParamListInfo params, bool atomic, DestReceiver
24312432
if (fcinfo->isnull)
24322433
elog(ERROR, "procedure returned null record");
24332434

2435+
/*
2436+
* Ensure there's an active snapshot whilst we execute whatever's
2437+
* involved here. Note that this is *not* sufficient to make the
2438+
* world safe for TOAST pointers to be included in the returned data:
2439+
* the referenced data could have gone away while we didn't hold a
2440+
* snapshot. Hence, it's incumbent on PLs that can do COMMIT/ROLLBACK
2441+
* to not return TOAST pointers, unless those pointers were fetched
2442+
* after the last COMMIT/ROLLBACK in the procedure.
2443+
*
2444+
* XXX that is a really nasty, hard-to-test requirement. Is there a
2445+
* way to remove it?
2446+
*/
2447+
EnsurePortalSnapshotExists();
2448+
24342449
td = DatumGetHeapTupleHeader(retval);
24352450
tupType = HeapTupleHeaderGetTypeId(td);
24362451
tupTypmod = HeapTupleHeaderGetTypMod(td);

src/backend/executor/spi.c

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,8 @@ _SPI_commit(bool chain)
251251
/* Start the actual commit */
252252
_SPI_current->internal_xact = true;
253253

254-
/*
255-
* Before committing, pop all active snapshots to avoid error about
256-
* "snapshot %p still active".
257-
*/
258-
while (ActiveSnapshotSet())
259-
PopActiveSnapshot();
254+
/* Release snapshots associated with portals */
255+
ForgetPortalSnapshots();
260256

261257
if (chain)
262258
SaveTransactionCharacteristics();
@@ -313,6 +309,9 @@ _SPI_rollback(bool chain)
313309
/* Start the actual rollback */
314310
_SPI_current->internal_xact = true;
315311

312+
/* Release snapshots associated with portals */
313+
ForgetPortalSnapshots();
314+
316315
if (chain)
317316
SaveTransactionCharacteristics();
318317

@@ -2105,6 +2104,7 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
21052104
uint64 my_processed = 0;
21062105
SPITupleTable *my_tuptable = NULL;
21072106
int res = 0;
2107+
bool allow_nonatomic = plan->no_snapshots; /* legacy API name */
21082108
bool pushed_active_snap = false;
21092109
ErrorContextCallback spierrcontext;
21102110
CachedPlan *cplan = NULL;
@@ -2137,11 +2137,12 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
21372137
* In the first two cases, we can just push the snap onto the stack once
21382138
* for the whole plan list.
21392139
*
2140-
* But if the plan has no_snapshots set to true, then don't manage
2141-
* snapshots at all. The caller should then take care of that.
2140+
* Note that snapshot != InvalidSnapshot implies an atomic execution
2141+
* context.
21422142
*/
2143-
if (snapshot != InvalidSnapshot && !plan->no_snapshots)
2143+
if (snapshot != InvalidSnapshot)
21442144
{
2145+
Assert(!allow_nonatomic);
21452146
if (read_only)
21462147
{
21472148
PushActiveSnapshot(snapshot);
@@ -2216,15 +2217,39 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
22162217
stmt_list = cplan->stmt_list;
22172218

22182219
/*
2219-
* In the default non-read-only case, get a new snapshot, replacing
2220-
* any that we pushed in a previous cycle.
2220+
* If we weren't given a specific snapshot to use, and the statement
2221+
* list requires a snapshot, set that up.
22212222
*/
2222-
if (snapshot == InvalidSnapshot && !read_only && !plan->no_snapshots)
2223+
if (snapshot == InvalidSnapshot &&
2224+
(list_length(stmt_list) > 1 ||
2225+
(list_length(stmt_list) == 1 &&
2226+
PlannedStmtRequiresSnapshot(linitial_node(PlannedStmt,
2227+
stmt_list)))))
22232228
{
2224-
if (pushed_active_snap)
2225-
PopActiveSnapshot();
2226-
PushActiveSnapshot(GetTransactionSnapshot());
2227-
pushed_active_snap = true;
2229+
/*
2230+
* First, ensure there's a Portal-level snapshot. This back-fills
2231+
* the snapshot stack in case the previous operation was a COMMIT
2232+
* or ROLLBACK inside a procedure or DO block. (We can't put back
2233+
* the Portal snapshot any sooner, or we'd break cases like doing
2234+
* SET or LOCK just after COMMIT.) It's enough to check once per
2235+
* statement list, since COMMIT/ROLLBACK/CALL/DO can't appear
2236+
* within a multi-statement list.
2237+
*/
2238+
EnsurePortalSnapshotExists();
2239+
2240+
/*
2241+
* In the default non-read-only case, get a new per-statement-list
2242+
* snapshot, replacing any that we pushed in a previous cycle.
2243+
* Skip it when doing non-atomic execution, though (we rely
2244+
* entirely on the Portal snapshot in that case).
2245+
*/
2246+
if (!read_only && !allow_nonatomic)
2247+
{
2248+
if (pushed_active_snap)
2249+
PopActiveSnapshot();
2250+
PushActiveSnapshot(GetTransactionSnapshot());
2251+
pushed_active_snap = true;
2252+
}
22282253
}
22292254

22302255
foreach(lc2, stmt_list)
@@ -2236,6 +2261,7 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
22362261
_SPI_current->processed = 0;
22372262
_SPI_current->tuptable = NULL;
22382263

2264+
/* Check for unsupported cases. */
22392265
if (stmt->utilityStmt)
22402266
{
22412267
if (IsA(stmt->utilityStmt, CopyStmt))
@@ -2267,9 +2293,10 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
22672293

22682294
/*
22692295
* If not read-only mode, advance the command counter before each
2270-
* command and update the snapshot.
2296+
* command and update the snapshot. (But skip it if the snapshot
2297+
* isn't under our control.)
22712298
*/
2272-
if (!read_only && !plan->no_snapshots)
2299+
if (!read_only && pushed_active_snap)
22732300
{
22742301
CommandCounterIncrement();
22752302
UpdateActiveSnapshotCommandId();
@@ -2303,13 +2330,11 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
23032330
ProcessUtilityContext context;
23042331

23052332
/*
2306-
* If the SPI context is atomic, or we are asked to manage
2307-
* snapshots, then we are in an atomic execution context.
2308-
* Conversely, to propagate a nonatomic execution context, the
2309-
* caller must be in a nonatomic SPI context and manage
2310-
* snapshots itself.
2333+
* If the SPI context is atomic, or we were not told to allow
2334+
* nonatomic operations, tell ProcessUtility this is an atomic
2335+
* execution context.
23112336
*/
2312-
if (_SPI_current->atomic || !plan->no_snapshots)
2337+
if (_SPI_current->atomic || !allow_nonatomic)
23132338
context = PROCESS_UTILITY_QUERY;
23142339
else
23152340
context = PROCESS_UTILITY_QUERY_NONATOMIC;

src/backend/replication/logical/worker.c

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ create_estate_for_relation(LogicalRepRelMapEntry *rel)
178178
ResultRelInfo *resultRelInfo;
179179
RangeTblEntry *rte;
180180

181+
/*
182+
* Input functions may need an active snapshot, as may AFTER triggers
183+
* invoked during finish_estate. For safety, ensure an active snapshot
184+
* exists throughout all our usage of the executor.
185+
*/
186+
PushActiveSnapshot(GetTransactionSnapshot());
187+
181188
estate = CreateExecutorState();
182189

183190
rte = makeNode(RangeTblEntry);
@@ -202,6 +209,22 @@ create_estate_for_relation(LogicalRepRelMapEntry *rel)
202209
return estate;
203210
}
204211

212+
/*
213+
* Finish any operations related to the executor state created by
214+
* create_estate_for_relation().
215+
*/
216+
static void
217+
finish_estate(EState *estate)
218+
{
219+
/* Handle any queued AFTER triggers. */
220+
AfterTriggerEndQuery(estate);
221+
222+
/* Cleanup. */
223+
ExecResetTupleTable(estate->es_tupleTable, false);
224+
FreeExecutorState(estate);
225+
PopActiveSnapshot();
226+
}
227+
205228
/*
206229
* Executes default values for columns for which we can't map to remote
207230
* relation columns.
@@ -609,9 +632,6 @@ apply_handle_insert(StringInfo s)
609632
RelationGetDescr(rel->localrel),
610633
&TTSOpsVirtual);
611634

612-
/* Input functions may need an active snapshot, so get one */
613-
PushActiveSnapshot(GetTransactionSnapshot());
614-
615635
/* Process and store remote tuple in the slot */
616636
oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
617637
slot_store_cstrings(remoteslot, rel, newtup.values);
@@ -625,13 +645,8 @@ apply_handle_insert(StringInfo s)
625645

626646
/* Cleanup. */
627647
ExecCloseIndices(estate->es_result_relation_info);
628-
PopActiveSnapshot();
629648

630-
/* Handle queued AFTER triggers. */
631-
AfterTriggerEndQuery(estate);
632-
633-
ExecResetTupleTable(estate->es_tupleTable, false);
634-
FreeExecutorState(estate);
649+
finish_estate(estate);
635650

636651
logicalrep_rel_close(rel, NoLock);
637652

@@ -745,7 +760,6 @@ apply_handle_update(StringInfo s)
745760
/* Also populate extraUpdatedCols, in case we have generated columns */
746761
fill_extraUpdatedCols(target_rte, rel->localrel);
747762

748-
PushActiveSnapshot(GetTransactionSnapshot());
749763
ExecOpenIndices(estate->es_result_relation_info, false);
750764

751765
/* Build the search tuple. */
@@ -804,15 +818,10 @@ apply_handle_update(StringInfo s)
804818
}
805819

806820
/* Cleanup. */
821+
EvalPlanQualEnd(&epqstate);
807822
ExecCloseIndices(estate->es_result_relation_info);
808-
PopActiveSnapshot();
809823

810-
/* Handle queued AFTER triggers. */
811-
AfterTriggerEndQuery(estate);
812-
813-
EvalPlanQualEnd(&epqstate);
814-
ExecResetTupleTable(estate->es_tupleTable, false);
815-
FreeExecutorState(estate);
824+
finish_estate(estate);
816825

817826
logicalrep_rel_close(rel, NoLock);
818827

@@ -864,7 +873,6 @@ apply_handle_delete(StringInfo s)
864873
&estate->es_tupleTable);
865874
EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1);
866875

867-
PushActiveSnapshot(GetTransactionSnapshot());
868876
ExecOpenIndices(estate->es_result_relation_info, false);
869877

870878
/* Find the tuple using the replica identity index. */
@@ -905,15 +913,10 @@ apply_handle_delete(StringInfo s)
905913
}
906914

907915
/* Cleanup. */
916+
EvalPlanQualEnd(&epqstate);
908917
ExecCloseIndices(estate->es_result_relation_info);
909-
PopActiveSnapshot();
910918

911-
/* Handle queued AFTER triggers. */
912-
AfterTriggerEndQuery(estate);
913-
914-
EvalPlanQualEnd(&epqstate);
915-
ExecResetTupleTable(estate->es_tupleTable, false);
916-
FreeExecutorState(estate);
919+
finish_estate(estate);
917920

918921
logicalrep_rel_close(rel, NoLock);
919922

@@ -936,7 +939,7 @@ apply_handle_truncate(StringInfo s)
936939
List *relids = NIL;
937940
List *relids_logged = NIL;
938941
ListCell *lc;
939-
LOCKMODE lockmode = AccessExclusiveLock;
942+
LOCKMODE lockmode = AccessExclusiveLock;
940943

941944
ensure_transaction();
942945

0 commit comments

Comments
 (0)