From 8cca190adc9fec651b54465a23095957a553beff Mon Sep 17 00:00:00 2001
From: Masahiko Sawada <msawada@postgresql.org>
Date: Wed, 25 Nov 2026 11:54:10 -0800
Subject: [PATCH] fixup! Fix LogStandbySnapshot() race when enabling logical
 decoding.

---
 src/backend/storage/ipc/standby.c | 43 ++++++++++++++-----------------
 1 file changed, 20 insertions(+), 23 deletions(-)

diff --git a/src/backend/storage/ipc/standby.c b/src/backend/storage/ipc/standby.c
index 3152efeafec..32493b9a9eb 100644
--- a/src/backend/storage/ipc/standby.c
+++ b/src/backend/storage/ipc/standby.c
@@ -1303,32 +1303,29 @@ LogStandbySnapshot(void)
 #endif
 
 	/*
-	 * Check whether logical WAL logging is enabled to decide if ProcArrayLock
-	 * must be held until the xl_running_xacts record is inserted.
+	 * Decide whether to hold ProcArrayLock until the xl_running_xacts record
+	 * is inserted (see below). We check the shared xlog_logical_info flag
+	 * rather than whether logical decoding is enabled: the activation sets
+	 * the flag and waits for all processes to absorb the barrier before it
+	 * marks logical decoding as enabled, so a process that has already
+	 * absorbed the barrier would otherwise not hold the lock while the
+	 * activation completes. The process-local cache doesn't work either, as
+	 * its update is deferred until the end of the transaction if we have an
+	 * XID.
 	 *
-	 * During logical decoding activation, xlog_logical_info is enabled and
-	 * all processes must absorb the barrier before logical_decoding_enabled
-	 * is set (see EnableLogicalDecoding()). Checking
-	 * IsLogicalDecodingEnabled() here is therefore insufficient, as a process
-	 * that has already absorbed the barrier could still observe logical
-	 * decoding as disabled and release ProcArrayLock early while activation
-	 * completes. Moreover, the local XLogLogicalInfo cache cannot be used
-	 * because its update is delayed until transaction end when an XID is
-	 * assigned.
+	 * If the flag is off here, a concurrent activation cannot complete until
+	 * we absorb the barrier, which we don't do before inserting the record
+	 * since nothing below processes interrupts.
 	 *
-	 * Checking this upfront is safe because none of the calls before
-	 * inserting the xl_running_xacts record process interrupts, so this
-	 * backend cannot absorb the barrier and let a concurrent activation
-	 * complete in the meantime. We also must not defer this check until
-	 * ProcArrayLock is held, since taking LogicalDecodingControlLock while
-	 * holding ProcArrayLock could cause a deadlock with
-	 * DisableLogicalDecoding() and CreateInitDecodingContext().
+	 * This must be checked before acquiring ProcArrayLock, because taking
+	 * LogicalDecodingControlLock while holding ProcArrayLock could deadlock
+	 * with DisableLogicalDecoding() and CreateInitDecodingContext().
 	 */
 	xlog_logical_info = IsXLogLogicalInfoEnabled();
 
 	/*
-	 * Tests wait here to let a concurrent activation complete. Unlike the
-	 * normal case, a waiting process can process interrupts.
+	 * Tests stop here to let a concurrent activation complete. Note that a
+	 * process waiting here can absorb the barrier, unlike in the code below.
 	 */
 	INJECTION_POINT("log-standby-snapshot", NULL);
 
@@ -1364,9 +1361,9 @@ LogStandbySnapshot(void)
 	recptr = LogCurrentRunningXacts(running);
 
 	/*
-	 * If xlog_logical_info was false when we checked it above, logical
-	 * decoding must not have been enabled since then, as we cannot absorb the
-	 * barrier until releasing XidGenLock.
+	 * If xlog_logical_info was false, logical decoding cannot have been
+	 * enabled since we checked it, as we haven't processed interrupts since
+	 * then.
 	 */
 	Assert(xlog_logical_info || !IsLogicalDecodingEnabled());
 
-- 
2.56.0

