On Sun, Nov 22, 2026 at 9:46 AM Zhijie Hou <[email protected]> wrote:
>
> Hi,
>
> While testing the dynamic logical decoding status control, I noticed
> another race condition in LogStandbySnapshot(), when logical decoding is
> concurrently enabled, ProcArrayLock can be released before logging the
> xl_running_xacts WAL record, causing historic snapshot corruption. The
> process is as follows:
>
> 1. S1 calls GetRunningTransactionData() while T1 (XID 800, e.g., CREATE
> TABLE tab_snap) is still in ProcArray, so S1 computes oldestRunningXid =
> 800 in memory.
> 2. S1 releases ProcArrayLock early, before writing its xl_running_xacts
> record to WAL.
> 3. T1 commits: it writes COMMIT 800 at LSN 100 and finishes
> ProcArrayEndTransaction(), removing 800 from ProcArray. (Had S1 kept
> ProcArrayLock, T1 would have blocked here.)
> 4. S2 runs LogStandbySnapshot(): since 800 is already gone from
> ProcArray, S2 computes oldestRunningXid = 801 (nextXid) and writes its
> xl_running_xacts record at LSN 200.
> 5. S1 writes its delayed xl_running_xacts record at LSN 300 with the
> stale oldestRunningXid = 800.
>
> Replaying that out-of-order WAL stream corrupts SnapBuild's state:
> - LSN 100 (COMMIT 800): SnapBuild adds 800 to builder->committed.xip and
> sets builder->xmax = 801.
> - LSN 200 (S2's RUNNING_XACTS, oldestRunningXid = 801): SnapBuild sets
> builder->xmin = 801 and purges 800 from builder->committed.xip.
> - LSN 300 (S1's RUNNING_XACTS, oldestRunningXid = 800): SnapBuild
> overwrites builder->xmin = 800. Now 800 is back inside [xmin, xmax)
> after already being purged from committed.xip, so subsequent historic
> snapshots built by SnapBuild mark committed transaction 800 as still in
> progress (e.g., failing with "ERROR: pg_attribute catalog is missing 1
> attribute(s) for relation OID ..." or "ERROR: could not map filenumber
> ... to relation OID").
>
> Currently, LogStandbySnapshot() checks IsLogicalDecodingEnabled()
> (logical_decoding_enabled) to decide whether to keep ProcArrayLock.
> However, enabling logical decoding happens in two phases:
>
> 1. EnableLogicalDecoding() sets xlog_logical_info = true and waits for
>     all processes to absorb a ProcSignalBarrier.
> 2. Only after that barrier wait finishes does it set
>     logical_decoding_enabled = true, after which the caller reserves the
>     new slot's restart_lsn.
>
> If S1 absorbs the barrier in phase 1 and then calls LogStandbySnapshot()
> before phase 2 sets logical_decoding_enabled = true,
> IsLogicalDecodingEnabled() still returns false, so S1 releases
> ProcArrayLock early. Meanwhile, since S1 has already absorbed the
> barrier, the activating backend does not wait for S1: it can finish
> phase 2 and reserve the new slot's restart_lsn before S1 writes its
> xl_running_xacts record, exposing that out-of-order record to logical
> decoding.

Thank you for the report. I agree with your analysis.

> Attached is a patch set to fix this:
>
> 0001 checks IsXLogLogicalInfoEnabled() right before
> GetRunningTransactionData() (not while holding ProcArrayLock, to respect
> the lock ordering with DisableLogicalDecoding()). If xlog_logical_info
> is still false at that point, neither GetRunningTransactionData() nor
> LogCurrentRunningXacts() processes ProcSignalBarriers, so no concurrent
> activation can finish its barrier wait until after the WAL record is
> inserted.

I think the fix is correct. Checking the shared xlog_logical_info
instead of the local cache makes sense given that the cache update can
be delayed in a transaction with an XID. A few comments:

+    */
+   xlog_logical_info = IsXLogLogicalInfoEnabled();

Is there a reason to move the check here? The comment explains why we
cannot do it while holding ProcArrayLock, but the beginning of the
function, where we called IsLogicalDecodingEnabled() before, is also
outside of the lock.

---
+   /*
+    * Check whether logical WAL logging is enabled before
+    * GetRunningTransactionData() acquires ProcArrayLock.
+    * IsXLogLogicalInfoEnabled() acquires LogicalDecodingControlLock, so
+    * calling it while holding ProcArrayLock could cause a three-way deadlock
+    * with DisableLogicalDecoding() (which takes LogicalDecodingControlLock
+    * before ReplicationSlotControlLock) and e.g. CreateInitDecodingContext()
+    * (which takes ReplicationSlotControlLock before ProcArrayLock).
+    */

I think it would be good to have an explanation for why we cannot use
IsLogicalDecodingEnabled() here.

>
> 0002 adds injection point to reproduce/test the above case. I kept this
> in a separate patch because it looks complicate and might not be worth
> pushing.

I agree that it's a bit too much for this fix. I think it can be
simplified by having an injection point right after
IsXLogLogicalInfoEnabled() call and by having the assertion
"xlog_logical_info || !IsLogicalDecodingEnabled()" after inserting the
record. That is, if xlog_logical_info was false when we checked,
logical decoding must not have been enabled by the time the record is
inserted. It cannot fail with a concurrent activation since the
activating process has to wait for us to absorb the barrier, which we
cannot do while holding XidGenLock. I've attached a draft patch, which
can be applied on top of the 0001 patch.

Regards,

-- 
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
diff --git a/src/backend/storage/ipc/standby.c b/src/backend/storage/ipc/standby.c
index b4bfa205262..0b7ced5bb0c 100644
--- a/src/backend/storage/ipc/standby.c
+++ b/src/backend/storage/ipc/standby.c
@@ -1321,6 +1321,12 @@ LogStandbySnapshot(void)
 	 */
 	xlog_logical_info = IsXLogLogicalInfoEnabled();
 
+	/*
+	 * Tests wait here to let a concurrent activation complete. Unlike the
+	 * normal case, a waiting process can process interrupts.
+	 */
+	INJECTION_POINT("log-standby-snapshot", NULL);
+
 	/*
 	 * Log details of all in-progress transactions. This should be the last
 	 * record we write, because standby will open up when it sees this.
@@ -1352,6 +1358,13 @@ 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.
+	 */
+	Assert(xlog_logical_info || !IsLogicalDecodingEnabled());
+
 	/* Release lock if we kept it longer ... */
 	if (xlog_logical_info)
 		LWLockRelease(ProcArrayLock);
diff --git a/src/test/recovery/t/051_effective_wal_level.pl b/src/test/recovery/t/051_effective_wal_level.pl
index eff6b04e65d..e1306c835fd 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

Reply via email to