From d0d72ce506e02de2370ae5b3c6ea23461bccfb1f Mon Sep 17 00:00:00 2001 From: Narayanan Venkateswaran Date: Tue, 1 Dec 2026 16:08:47 +0000 Subject: [PATCH v1] Fix lock ordering in LogStandbySnapshot() Assert. Commit ce10389d637 added Assert(xlog_logical_info || !IsLogicalDecodingEnabled()) in LogStandbySnapshot() before LWLockRelease(XidGenLock). When xlog_logical_info is false, IsLogicalDecodingEnabled() acquires LogicalDecodingControlLock while XidGenLock is still held, inverting the lock order used by DisableLogicalDecoding(), reset_conflict_slot_xmin_to_safe_horizon(), and ProcArrayAdd() / ProcArrayRemove() and risking a 4-way LWLock deadlock on assert builds. Move the Assert after LWLockRelease(XidGenLock). LWLockRelease() does not process interrupts or ProcSignalBarriers, so the checked invariant still holds without holding any LWLocks. --- src/backend/storage/ipc/standby.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/backend/storage/ipc/standby.c b/src/backend/storage/ipc/standby.c index 5923bdc60c1..15f2cc9aeec 100644 --- a/src/backend/storage/ipc/standby.c +++ b/src/backend/storage/ipc/standby.c @@ -1386,13 +1386,6 @@ LogStandbySnapshot(void) 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 (xlog_logical_info) LWLockRelease(ProcArrayLock); @@ -1400,6 +1393,15 @@ LogStandbySnapshot(void) /* GetRunningTransactionData() acquired XidGenLock, we must release it */ LWLockRelease(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. This must be checked after releasing both ProcArrayLock and + * XidGenLock to respect the lock ordering with + * LogicalDecodingControlLock. + */ + Assert(xlog_logical_info || !IsLogicalDecodingEnabled()); + return recptr; } -- 2.56.1.911.g883d8bcb20-goog