From 99745f3b537ff1bfbb78364d867f1d5f8048f96c Mon Sep 17 00:00:00 2001 From: Adesh Thakare Date: Wed, 18 Nov 2026 01:13:24 +0000 Subject: [PATCH v4 1/2] Don't wait for LogicalDecodingControlLock in the postmaster On a standby with sync_replication_slots = on, the postmaster checks whether logical decoding is enabled before starting the slot sync worker. That check takes LogicalDecodingControlLock in shared mode. The postmaster has no PGPROC, so if the startup process holds the lock exclusively while replaying a logical decoding status change, the postmaster PANICs with "cannot wait without a PGPROC structure" and exits, taking the whole server down. Add IsLogicalDecodingEnabledNoLock(), which reads the flag without the lock, and use it in ValidateSlotSyncParams() when called from the postmaster. A stale answer either delays starting the worker, which is harmless because the startup process wakes the postmaster after enabling logical decoding, or starts it while logical decoding is off, which could already happen since decoding can be disabled right after the worker starts. Have the worker check the parameters again, under the lock, when it starts and error out if slot synchronization cannot proceed. Backpatch to 19, where logical decoding became dynamically enabled. --- src/backend/replication/logical/logicalctl.c | 17 ++++++++++++++++- src/backend/replication/logical/slotsync.c | 19 ++++++++++++++++++- src/include/replication/logicalctl.h | 1 + 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/backend/replication/logical/logicalctl.c b/src/backend/replication/logical/logicalctl.c index 182d15e..0e63e5f 100644 --- a/src/backend/replication/logical/logicalctl.c +++ b/src/backend/replication/logical/logicalctl.c @@ -79,7 +79,9 @@ /* * Struct for controlling the logical decoding status. * - * This struct is protected by LogicalDecodingControlLock. + * This struct is protected by LogicalDecodingControlLock. The postmaster, + * which cannot acquire locks, reads logical_decoding_enabled without it; see + * IsLogicalDecodingEnabledNoLock(). */ typedef struct LogicalDecodingCtlData { @@ -238,6 +240,19 @@ IsLogicalDecodingEnabled(void) return enabled; } +/* + * Like IsLogicalDecodingEnabled(), but read the flag without taking + * LogicalDecodingControlLock. + * + * This is for the postmaster, which has no PGPROC and so must not wait for + * an LWLock. The result may be stale; callers must tolerate that. + */ +bool +IsLogicalDecodingEnabledNoLock(void) +{ + return LogicalDecodingCtl->logical_decoding_enabled; +} + /* * Return true if logical decoding has been enabled continuously from the given * LSN up to the current replay position, that is, if the WAL in that range diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c index 27debe8..df0bd2b 100644 --- a/src/backend/replication/logical/slotsync.c +++ b/src/backend/replication/logical/slotsync.c @@ -1272,10 +1272,21 @@ CheckAndGetDbnameFromConninfo(void) bool ValidateSlotSyncParams(int elevel) { + bool logical_decoding_enabled; + /* * Logical slot sync/creation requires logical decoding to be enabled. + * + * The postmaster cannot acquire locks, so read the flag without the lock. + * The answer can go stale right after the read anyway, and the slot sync + * worker confirms it again at startup. */ - if (!IsLogicalDecodingEnabled()) + if (IsUnderPostmaster) + logical_decoding_enabled = IsLogicalDecodingEnabled(); + else + logical_decoding_enabled = IsLogicalDecodingEnabledNoLock(); + + if (!logical_decoding_enabled) { ereport(elevel, errcode(ERRCODE_INVALID_PARAMETER_VALUE), @@ -1709,6 +1720,12 @@ ReplSlotSyncWorkerMain(const void *startup_data, size_t startup_data_len) /* We can now handle ereport(ERROR) */ PG_exception_stack = &local_sigjmp_buf; + /* + * The postmaster checked the logical decoding status without the lock + * (see IsLogicalDecodingEnabledNoLock()), so confirm it is still enabled. + */ + ValidateSlotSyncParams(ERROR); + /* Setup signal handling */ pqsignal(SIGHUP, SignalHandlerForConfigReload); pqsignal(SIGINT, StatementCancelHandler); diff --git a/src/include/replication/logicalctl.h b/src/include/replication/logicalctl.h index 97cacb0..f638716 100644 --- a/src/include/replication/logicalctl.h +++ b/src/include/replication/logicalctl.h @@ -20,6 +20,7 @@ extern void StartupLogicalDecodingStatus(bool last_status); extern void InitializeProcessXLogLogicalInfo(void); extern bool ProcessBarrierUpdateXLogLogicalInfo(void); extern bool IsLogicalDecodingEnabled(void); +extern bool IsLogicalDecodingEnabledNoLock(void); extern bool StandbyLogicalDecodingEnabledSince(XLogRecPtr lsn); extern bool IsXLogLogicalInfoEnabled(void); extern void AtEOXact_LogicalCtl(void); -- 2.47.3