Hi, On Tue, Nov24, 2026 at 06:25 AM Masahiko Sawada wrote:
On Sun, Nov 22, 2026 at 9:46 AM Zhijie Hou <[email protected]> wrote: > 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:
Thanks for the 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.
I rechecked and confirmed that check in the beginning is also safe. So, I moved this back in the new version patch.
--- + /* + * 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.
Added.
> > 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.
The attached tests look good to me, I have merged it into the patch. Here is the V2 patch. -- Best Regards, Zhijie Hou
From 64d708297d10b5204f2c8d3e40c6d5c76e1c3d53 Mon Sep 17 00:00:00 2001 From: Zhijie Hou <[email protected]> Date: Tue, 24 Nov 2026 10:47:59 +0800 Subject: [PATCH v2] 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() before acquiring ProcArrayLock (not while holding ProcArrayLock, to avoid 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 | 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..a912a940f46 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 + /* + * 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. + * + * Note that this must be called before GetRunningTransactionData() + * acquires ProcArrayLock, 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 +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 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 5129e6eb638..dbc63151d99 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)
