Hi,

On Tue, Nov24, 2026 at 12:25 PM "Hayato Kuroda (Fujitsu)" wrote:
Hi,

> Here is the V2 patch.

Quick (and possibly stupid) question. Is there a reason why
IsXLogLogicalInfoEnabled() is done before GetRunningTransactionLocks()
and LogAccessExclusiveLocks()? You said to put it right before
GetRunningTransactionData(), but these functions are done in-between.
It's harmless but it's clearer for me to put them before.
Thanks for looking at the patch.

I don't see a functional difference in the order here and didn't intend to change the original placement, since IsLogicalDecodingEnabled() was already called at the beginning of LogStandbySnapshot() on HEAD, and 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-between). The comment was only meant to remind future readers not to defer the check until ProcArrayLock is held.

I slightly adjusted the comments in this version to make the points clear.

--
Best Regards,
Zhijie Hou
From 0a986e2830018a42c81f72ef1ed9ac860e6b665b Mon Sep 17 00:00:00 2001
From: Zhijie Hou <[email protected]>
Date: Wed, 25 Nov 2026 10:47:41 +0800
Subject: [PATCH v3] Fix LogStandbySnapshot() race when enabling logical
 decoding.

When logical decoding is active, LogStandbySnapshot() must hold
ProcArrayLock until LogCurrentRunningXacts() inserts the
xl_running_xacts record into WAL. Otherwise, a committing transaction
listed in that record can finish ProcArrayEndTransaction() first,
allowing a concurrent LogStandbySnapshot() to log a newer
oldestRunningXid ahead of the stale one in WAL. Replaying those
out-of-order xl_running_xacts records during logical decoding moves
SnapBuild's xmin backwards after older committed catalog transactions
have already been purged, corrupting historic snapshots.

Commit 67c20979ce7 made LogStandbySnapshot() check
IsLogicalDecodingEnabled() to decide whether to keep ProcArrayLock.
However, EnableLogicalDecoding() first sets xlog_logical_info = true
and waits for all processes to absorb a ProcSignalBarrier, and only
afterwards sets logical_decoding_enabled = true. If a process absorbs
that barrier and then enters LogStandbySnapshot() before
logical_decoding_enabled is set to true, it sees
logical_decoding_enabled = false and releases ProcArrayLock early,
while the activating backend can finish enabling logical decoding and
reserve the new slot's restart_lsn before LogCurrentRunningXacts()
writes the WAL record.

Fix this by checking IsXLogLogicalInfoEnabled() upfront rather than
while holding ProcArrayLock (which would risk a three-way deadlock with
DisableLogicalDecoding() and CreateInitDecodingContext()). Checking
shared xlog_logical_info rather than logical_decoding_enabled or the
process-local XLogLogicalInfo cache covers both in-progress activations
and callers in XID-assigned transactions whose local cache update is
deferred; if it is still false at that point, no ProcSignalBarrier is
absorbed before the WAL record is inserted, so no concurrent activation
can finish its barrier wait in the meantime.

Author: Zhijie Hou <[email protected]>
Author: Masahiko Sawada <[email protected]>
Reviewed-by: Masahiko Sawada <[email protected]>
Discussion: https://postgr.es/m/[email protected]
Backpatch-through: 19
---
 src/backend/storage/ipc/standby.c             | 43 +++++++++++++-
 .../recovery/t/051_effective_wal_level.pl     | 57 +++++++++++++++++++
 2 files changed, 97 insertions(+), 3 deletions(-)

diff --git a/src/backend/storage/ipc/standby.c 
b/src/backend/storage/ipc/standby.c
index 7f011e04990..3152efeafec 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,36 @@ 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.
+        *
+        * 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.
+        *
+        * 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().
+        */
+       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);
+
        /*
         * Get details of any AccessExclusiveLocks being held at the moment.
         */
@@ -1328,13 +1358,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 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 (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.54.0 (Apple Git-157)

Reply via email to