Fix uninitialized access to InitialRunningXacts during decoding after ERROR.

The transactions and subtransactions array that was allocated under
snapshot builder memory context and recorded during decoding was not
cleared in case of errors. This can result in an assertion failure if we
attempt to retry logical decoding within the same session. To address this
issue, we register a callback function under the snapshot builder memory
context to clear the recorded transactions and subtransactions array along
with the context.

This problem doesn't exist in PG16 and HEAD as instead of using
InitialRunningXacts, we added the list of transaction IDs and
sub-transaction IDs, that have modified catalogs and are running during
snapshot serialization, to the serialized snapshot (see commit 7f13ac8123).

Author: Hou Zhijie
Reviewed-by: Amit Kapila
Backpatch-through: 11
Discussion: http://postgr.es/m/18055-ab3beed9f4b7b7d6@postgresql.org
This commit is contained in:
Amit Kapila 2023-09-12 10:12:51 +05:30
parent 2f13e8d9ec
commit f7d25117ba
1 changed files with 16 additions and 4 deletions

View File

@ -300,6 +300,17 @@ static void SnapBuildWaitSnapshot(xl_running_xacts *running, TransactionId cutof
static void SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn);
static bool SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn);
/*
* Memory context reset callback for clearing the array of running transactions
* and subtransactions.
*/
static void
SnapBuildResetRunningXactsCallback(void *arg)
{
NInitialRunningXacts = 0;
InitialRunningXacts = NULL;
}
/*
* Allocate a new snapshot builder.
*
@ -316,6 +327,7 @@ AllocateSnapshotBuilder(ReorderBuffer *reorder,
MemoryContext context;
MemoryContext oldcontext;
SnapBuild *builder;
MemoryContextCallback *mcallback;
/* allocate memory in own context, to have better accountability */
context = AllocSetContextCreate(CurrentMemoryContext,
@ -341,6 +353,10 @@ AllocateSnapshotBuilder(ReorderBuffer *reorder,
builder->building_full_snapshot = need_full_snapshot;
builder->initial_consistent_point = initial_consistent_point;
mcallback = palloc0(sizeof(MemoryContextCallback));
mcallback->func = SnapBuildResetRunningXactsCallback;
MemoryContextRegisterResetCallback(CurrentMemoryContext, mcallback);
MemoryContextSwitchTo(oldcontext);
/* The initial running transactions array must be empty. */
@ -366,10 +382,6 @@ FreeSnapshotBuilder(SnapBuild *builder)
/* other resources are deallocated via memory context reset */
MemoryContextDelete(context);
/* InitialRunningXacts is freed along with the context */
NInitialRunningXacts = 0;
InitialRunningXacts = NULL;
}
/*