0001-PG14-Reset-InitialRunningXacts-at-memory-context-res.patch

application/octet-stream

Filename: 0001-PG14-Reset-InitialRunningXacts-at-memory-context-res.patch
Type: application/octet-stream
Part: 0
Message: RE: BUG #18055: logical decoding core on AllocateSnapshotBuilder()

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch 0001
Subject: Reset InitialRunningXacts at memory context reset callback
File+
src/backend/replication/logical/snapbuild.c 16 4
From e71c5f57675d3945090980319c3c71af7e831563 Mon Sep 17 00:00:00 2001
From: Hou Zhijie <houzj.fnst@cn.fujitsu.com>
Date: Thu, 7 Sep 2023 19:51:09 +0800
Subject: [PATCH] Reset InitialRunningXacts at memory context reset
 callback

The transactions and subtransactions array that were recorded during decoding
were not cleared in case of errors. This can result in an assert 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.
---
 src/backend/replication/logical/snapbuild.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c
index b5b018e697..0425c9fae8 100644
--- a/src/backend/replication/logical/snapbuild.c
+++ b/src/backend/replication/logical/snapbuild.c
@@ -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;
 }
 
 /*
-- 
2.30.0.windows.2