From 09c3dc593cf0df90b1c46d1cf86fa8dc29cdba4b Mon Sep 17 00:00:00 2001
From: Zhijie Hou <houzhijie22@gmail.com>
Date: Wed, 25 Nov 2026 10:47:41 +0800
Subject: [PATCH v4] Fix LogStandbySnapshot() race when enabling logical
 decoding.

While logical decoding is enabled, LogStandbySnapshot() must hold
ProcArrayLock until the xl_running_xacts record is inserted, so that no
transaction listed as running in the record commits before the record
in WAL. Commit 67c20979ce7 made it decide whether to hold the lock by
checking only whether logical decoding is enabled. However, the
activation first enables logical information WAL logging and waits for
all processes to absorb the barrier, and only then marks logical
decoding as enabled. So if a process logging a standby snapshot has
absorbed the barrier but logical decoding is not marked as enabled
yet, it doesn't hold the lock until the xl_running_xacts record is
inserted. Logical decoding reading such a record could then use a
wrong historic snapshot.

Fix by checking the shared flag for logical information WAL logging
instead. If the flag is still off, the activation cannot complete until
this process absorbs the barrier. Since the process doesn't process
interrupts until it inserts the record, the activation completes only
after the record is inserted.

Author: Zhijie Hou <houzhijie22@gmail.com>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Discussion: https://postgr.es/m/bd622b46-c734-4557-8ae1-6c113cd434a2@gmail.com
Backpatch-through: 19
---
 src/backend/storage/ipc/standby.c             | 40 ++++++++++++-
 .../recovery/t/051_effective_wal_level.pl     | 57 +++++++++++++++++++
 2 files changed, 94 insertions(+), 3 deletions(-)

diff --git a/src/backend/storage/ipc/standby.c b/src/backend/storage/ipc/standby.c
index 7f011e04990..32493b9a9eb 100644
--- a/src/backend/storage/ipc/standby.c
+++ b/src/backend/storage/ipc/standby.c
@@ -1287,7 +1287,7 @@ LogStandbySnapshot(void)
 	RunningTransactions running;
 	xl_standby_lock *locks;
 	int			nlocks;
-	bool		logical_decoding_enabled = IsLogicalDecodingEnabled();
+	bool		xlog_logical_info;
 
 	Assert(XLogStandbyInfoActive());
 
@@ -1302,6 +1302,33 @@ LogStandbySnapshot(void)
 	}
 #endif
 
+	/*
+	 * 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.
+	 *
+	 * 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.
+	 *
+	 * This must be checked before acquiring ProcArrayLock, because taking
+	 * LogicalDecodingControlLock while holding ProcArrayLock could deadlock
+	 * with DisableLogicalDecoding() and CreateInitDecodingContext().
+	 */
+	xlog_logical_info = IsXLogLogicalInfoEnabled();
+
+	/*
+	 * 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);
+
 	/*
 	 * Get details of any AccessExclusiveLocks being held at the moment.
 	 */
@@ -1328,13 +1355,20 @@ LogStandbySnapshot(void)
 	 * record. Fortunately this routine isn't executed frequently, and it's
 	 * only a shared lock.
 	 */
-	if (!logical_decoding_enabled)
+	if (!xlog_logical_info)
 		LWLockRelease(ProcArrayLock);
 
 	recptr = LogCurrentRunningXacts(running);
 
+	/*
+	 * 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());
+
 	/* Release lock if we kept it longer ... */
-	if (logical_decoding_enabled)
+	if (xlog_logical_info)
 		LWLockRelease(ProcArrayLock);
 
 	/* GetRunningTransactionData() acquired XidGenLock, we must release it */
diff --git a/src/test/recovery/t/051_effective_wal_level.pl b/src/test/recovery/t/051_effective_wal_level.pl
index 3482fe1171b..80e2a7bb315 100644
--- a/src/test/recovery/t/051_effective_wal_level.pl
+++ b/src/test/recovery/t/051_effective_wal_level.pl
@@ -479,6 +479,63 @@ select pg_drop_replication_slot('slot_2');
 ]);
 	wait_for_logical_decoding_disabled($primary);
 
+	# Test logging a standby snapshot during the activation. An assertion in
+	# LogStandbySnapshot() fails if ProcArrayLock is released too early.
+
+	# Use a transaction with an XID to delay its XLogLogicalInfo cache update.
+	my $psql_snapshot = $primary->background_psql('postgres');
+	$psql_snapshot->query_safe(
+		q[
+select injection_points_set_local();
+select injection_points_attach('log-standby-snapshot', 'wait');
+begin;
+select pg_current_xact_id();
+]);
+
+	# Stop the activation right before enabling logical decoding.
+	$psql_create_slot = $primary->background_psql('postgres');
+	$psql_create_slot_pid =
+	  $psql_create_slot->query_safe('select pg_backend_pid()');
+	$psql_create_slot->query_until(
+		qr/create_slot_snapshot/,
+		q(\echo create_slot_snapshot
+select injection_points_set_local();
+select injection_points_attach('logical-decoding-activation', 'wait');
+select pg_create_logical_replication_slot('slot_snapshot', 'test_decoding');
+));
+	$primary->wait_for_event('client backend', 'logical-decoding-activation');
+
+	# Stop logging a standby snapshot before collecting running transactions.
+	$psql_snapshot->query_until(
+		qr/log_snapshot/,
+		q(\echo log_snapshot
+select pg_log_standby_snapshot();
+));
+	$primary->wait_for_event('client backend', 'log-standby-snapshot');
+
+	# Let the activation complete, then resume logging the snapshot.
+	my $snapshot_log_offset = -s $primary->logfile;
+	$primary->safe_psql('postgres',
+		qq[select injection_points_wakeup('logical-decoding-activation')]);
+	$primary->wait_for_log(
+		"logical decoding is enabled upon creating a new logical replication slot",
+		$snapshot_log_offset);
+	$primary->safe_psql('postgres',
+		qq[select injection_points_wakeup('log-standby-snapshot')]);
+
+	# The slot creation waits for the transaction to finish.
+	$psql_snapshot->query_safe('commit');
+	$psql_snapshot->quit;
+	quit_and_wait($primary, $psql_create_slot, $psql_create_slot_pid);
+	test_wal_level($primary, "replica|logical",
+		"standby snapshot is logged during the activation of logical decoding"
+	);
+
+	# Restore the disabled state for the tests that follow.
+	$primary->safe_psql('postgres',
+		qq[select pg_drop_replication_slot('slot_snapshot')]);
+	wait_for_logical_decoding_disabled($primary);
+
 	# Test a race when logical decoding activation is concurrently interrupted.
 
 	# Start a psql session to test the case where the activation process is
-- 
2.56.0

