On Tue, Nov 24, 2026 at 9:07 PM Hayato Kuroda (Fujitsu) <[email protected]> wrote: > > Hi, > > > > 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. > > Thanks for the detailed descriptions. I understood. v3 LGTM. >
Thank you for updating the patch! I've reviewed the v3 patch and felt the new comments were a bit long and redundant to me. I've attached the v4 patch that rewrote the comments and a commit message update. The attached nocfbot patch is the diff between v3 and v4. Regards, -- Masahiko Sawada Amazon Web Services: https://aws.amazon.com
From 8cca190adc9fec651b54465a23095957a553beff Mon Sep 17 00:00:00 2001 From: Masahiko Sawada <[email protected]> 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
From 09c3dc593cf0df90b1c46d1cf86fa8dc29cdba4b Mon Sep 17 00:00:00 2001 From: Zhijie Hou <[email protected]> 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 <[email protected]> Reviewed-by: Masahiko Sawada <[email protected]> Reviewed-by: Hayato Kuroda <[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..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
