From ef82201561caaf92cb8f9fe2eb08705298f8dd90 Mon Sep 17 00:00:00 2001 From: Adesh Thakare Date: Wed, 18 Nov 2026 01:13:24 +0000 Subject: [PATCH v2 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. Read the flag without the lock when there is no PGPROC. The answer can go stale before the worker starts either way, so have the slot sync worker check the parameters again, under the lock, when it starts and exit if slot synchronization cannot proceed. At worst this costs one extra worker start. --- src/backend/replication/logical/logicalctl.c | 12 ++++++++++++ src/backend/replication/logical/slotsync.c | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/src/backend/replication/logical/logicalctl.c b/src/backend/replication/logical/logicalctl.c index 182d15e..86b9175 100644 --- a/src/backend/replication/logical/logicalctl.c +++ b/src/backend/replication/logical/logicalctl.c @@ -231,6 +231,18 @@ IsLogicalDecodingEnabled(void) { bool enabled; + /* + * The postmaster calls this through ValidateSlotSyncParams() to decide + * whether to start the slot sync worker. It has no PGPROC, so it must not + * wait for an LWLock: LWLockQueueSelf() would PANIC if, for example, the + * startup process held the lock while replaying a logical decoding status + * change. Read the flag without the lock there. Taking the lock would not + * make the answer any fresher by the time the worker starts, and the + * worker checks the status again under the lock when it starts. + */ + if (MyProc == NULL) + return LogicalDecodingCtl->logical_decoding_enabled; + LWLockAcquire(LogicalDecodingControlLock, LW_SHARED); enabled = LogicalDecodingCtl->logical_decoding_enabled; LWLockRelease(LogicalDecodingControlLock); diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c index 27debe8..bc3940f 100644 --- a/src/backend/replication/logical/slotsync.c +++ b/src/backend/replication/logical/slotsync.c @@ -1678,6 +1678,15 @@ ReplSlotSyncWorkerMain(const void *startup_data, size_t startup_data_len) Assert(SlotSyncCtx != NULL); + /* + * The postmaster reads the logical decoding status without the lock + * before starting us, so it may have acted on a stale value. Check again + * now that we can take the lock, and exit if slot synchronization cannot + * proceed. The postmaster will start us again later. + */ + if (!ValidateSlotSyncParams(LOG)) + proc_exit(0); + /* * If an exception is encountered, processing resumes here. * -- 2.47.3