From c90220167e0033b53b8a55a1eeecfc71aadea16e Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Mon, 23 Nov 2026 01:44:05 +0800 Subject: [PATCH v1 1/2] 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() right before GetRunningTransactionData() (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, neither GetRunningTransactionData() nor LogCurrentRunningXacts() processes ProcSignalBarriers, so no concurrent activation can finish its barrier wait until after the WAL record is inserted. Author: Zhijie Hou Backpatch-through: 19 --- src/backend/storage/ipc/standby.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/backend/storage/ipc/standby.c b/src/backend/storage/ipc/standby.c index 7f011e04990..b4bfa205262 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()); @@ -1310,6 +1310,17 @@ LogStandbySnapshot(void) LogAccessExclusiveLocks(nlocks, locks); pfree(locks); + /* + * 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). + */ + xlog_logical_info = IsXLogLogicalInfoEnabled(); + /* * Log details of all in-progress transactions. This should be the last * record we write, because standby will open up when it sees this. @@ -1327,14 +1338,22 @@ LogStandbySnapshot(void) * which, according to the WAL, has committed before the xl_running_xacts * record. Fortunately this routine isn't executed frequently, and it's * only a shared lock. + * + * Check shared xlog_logical_info (sampled above) so we also keep the lock + * while logical decoding is being enabled, or when this backend's local + * XLogLogicalInfo update is delayed until transaction end. If + * xlog_logical_info was false, no concurrent activation can finish its + * ProcSignalBarrier wait until LogCurrentRunningXacts() inserts the WAL + * record, since neither GetRunningTransactionData() nor + * LogCurrentRunningXacts() processes barriers. */ - if (!logical_decoding_enabled) + if (!xlog_logical_info) LWLockRelease(ProcArrayLock); recptr = LogCurrentRunningXacts(running); /* Release lock if we kept it longer ... */ - if (logical_decoding_enabled) + if (xlog_logical_info) LWLockRelease(ProcArrayLock); /* GetRunningTransactionData() acquired XidGenLock, we must release it */ -- 2.54.0 (Apple Git-157)