Postmaster PANIC on a standby with slot synchronization enabled
Hi,
On a standby with slot synchronization enabled, the postmaster can PANIC
with "cannot wait without a PGPROC structure" and exit, taking the whole
server down. While no slot sync worker is running, the postmaster checks
on every wakeup whether logical decoding is enabled. Since 67c20979ce7,
which turns logical decoding on when the first logical slot is created,
that check takes an LWLock. If the startup process holds that lock
exclusively while it replays a logical decoding status change, the
postmaster has to wait, and it can't wait without a PGPROC. The window is
narrow, but it is easy to hit with a debugger or an injection point, and
it reproduces on current master.
The attached 0001 has the postmaster read the flag without the lock. It is
a single bool, and a locked read would not stay valid until the worker
starts anyway. To cover a stale read, the slot sync worker checks again
under the lock when it starts and exits if logical decoding is off, which
costs at most one extra worker start. Other options would be to make the
flag an atomic in shared memory, or to drop the check from the postmaster
and let the worker decide. 0002 adds an injection point and a TAP test
that reproduces the PANIC; it is not for commit. The fix needs to be
backpatched to v19.
Thanks,
Adesh Thakare
Attachments:
t259611_1v2-0001-Don-t-wait-for-LogicalDecodingControlLock-in-the-.patchapplication/octet-stream; name=v2-0001-Don-t-wait-for-LogicalDecodingControlLock-in-the-.patchDownload+21-1
v2-0002-Add-a-test-for-the-postmaster-waiting-on-LogicalD.patchapplication/octet-stream; name=v2-0002-Add-a-test-for-the-postmaster-waiting-on-LogicalD.patchDownload+120-1
Hi,
Thank you for the report.
On Fri, Nov 20, 2026 at 2:46 PM T, Adesh <adeshdb@amazon.com> wrote:
Your attachments are now available for secure download. Amazon has replaced the attachments in this email with download links.
Important: These links may contain content that is confidential. Please exercise appropriate caution when sharing.
2 Attachments Download all Passcode required VESgWX
v2-0001-Don-t-wait-for-LogicalDecod...the-.patch v2-0002-Add-a-test-for-the-postmast...calD.patch
Attachments Available Until: December 20, 2026, 22:46 (UTC+00:00)
Help us improve: Share Your Feedback Visit Here for FAQs and support resources.
Hi,On a standby with slot synchronization enabled, the postmaster can PANIC
with "cannot wait without a PGPROC structure" and exit, taking the whole
server down. While no slot sync worker is running, the postmaster checks
on every wakeup whether logical decoding is enabled. Since 67c20979ce7,
which turns logical decoding on when the first logical slot is created,
that check takes an LWLock. If the startup process holds that lock
exclusively while it replays a logical decoding status change, the
postmaster has to wait, and it can't wait without a PGPROC. The window is
narrow, but it is easy to hit with a debugger or an injection point, and
it reproduces on current master.
Good find. It should be fixed.
The attached 0001 has the postmaster read the flag without the lock. It is
a single bool, and a locked read would not stay valid until the worker
starts anyway. To cover a stale read, the slot sync worker checks again
under the lock when it starts and exits if logical decoding is off, which
costs at most one extra worker start. Other options would be to make the
flag an atomic in shared memory, or to drop the check from the postmaster
and let the worker decide. 0002 adds an injection point and a TAP test
that reproduces the PANIC; it is not for commit. The fix needs to be
backpatched to v19.
Regarding the proposed change, while I agree the basic idea of the
fix, I have a comment in the following change:
+ if (MyProc == NULL)
+ return LogicalDecodingCtl->logical_decoding_enabled;
I'm not sure we need to make the flag an atomic in shmem as it's a boolean flag.
I think it's better to introduce a function, say
IsLogicalDecodingEnabledNoLock() that reads the flag without lwlock so
that the caller explicitly specifies whether to read the flag without
lock. If the postmaster reads a stale value of the flag, we would end
up delaying launching the slotsync worker or launching it even if the
logical decoding is disabled. I think the former is not a problem in
practice since the startup process wakes up the postmaster after
enabling logical decoding, and the postmaster checks it again within
60 sec in the worst case. The latter case could happen even today and
even reading the flag with LogicalDecodingControlLock doesn't help
since the logical decoding can be disabled after the postmaster
launches the slotsync worker. So I think the proposed approach would
work.
Another possible fix would be to let the postmaster start the slotsync
worker without logical decoding status check, and have the slotsync
worker check it with a lock and exit. But it would require more
changes and change the existing behavior, which is not appropriate for
v19.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
Import Notes
Reply to msg id not found: 010101a1fb238525-b2862f92-98ea-4a60-85d5-63026f7c4973-000000@us-west-2.amazonses.comReference msg id not found: 010101a1fb238525-b2862f92-98ea-4a60-85d5-63026f7c4973-000000@us-west-2.amazonses.com
Hi,
On Fri, Nov 20, 2026 at 3:41 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
On Fri, Nov 20, 2026 at 2:46 PM T, Adesh <adeshdb@amazon.com> wrote:
On a standby with slot synchronization enabled, the postmaster can PANIC
with "cannot wait without a PGPROC structure" and exit, taking the whole
server down. While no slot sync worker is running, the postmaster checks
on every wakeup whether logical decoding is enabled. Since 67c20979ce7,
which turns logical decoding on when the first logical slot is created,
that check takes an LWLock. If the startup process holds that lock
exclusively while it replays a logical decoding status change, the
postmaster has to wait, and it can't wait without a PGPROC. The window is
narrow, but it is easy to hit with a debugger or an injection point, and
it reproduces on current master.Good find. It should be fixed.
+1. Do you mind registering it as a bug in the open CF -
https://commitfest.postgresql.org?
The attached 0001 has the postmaster read the flag without the lock. It is
a single bool, and a locked read would not stay valid until the worker
starts anyway. To cover a stale read, the slot sync worker checks again
under the lock when it starts and exits if logical decoding is off, which
costs at most one extra worker start. Other options would be to make the
flag an atomic in shared memory, or to drop the check from the postmaster
and let the worker decide. 0002 adds an injection point and a TAP test
that reproduces the PANIC; it is not for commit. The fix needs to be
backpatched to v19.Regarding the proposed change, while I agree the basic idea of the
fix, I have a comment in the following change:
AFAICS, this is the only lock that the postmaster accesses, violating
its "cannot acquire locks" principle and hitting this PANIC when
another process is trying to enable/disable logical decoding.
+ if (MyProc == NULL) + return LogicalDecodingCtl->logical_decoding_enabled;I'm not sure we need to make the flag an atomic in shmem as it's a boolean flag.
Agreed, I don't think a torn read can happen for the boolean (1 byte)
on most platforms. Even if it does, there's just one wasted fork, and
the sync worker has the check anyway to quickly exit, so that's fine
IMO.
I think it's better to introduce a function, say
IsLogicalDecodingEnabledNoLock() that reads the flag without lwlock so
that the caller explicitly specifies whether to read the flag without
lock.
+1. A function here makes the code more maintainable.
If the postmaster reads a stale value of the flag, we would end
up delaying launching the slotsync worker or launching it even if the
logical decoding is disabled. I think the former is not a problem in
practice since the startup process wakes up the postmaster after
enabling logical decoding, and the postmaster checks it again within
60 sec in the worst case.
Yes, this doesn't seem like a big problem. When another process is
enabling the decoding, if the postmaster sees it as false at that
moment, that's fine. The postmaster restarts the sync worker in the
next cycle. When disabling it, the sync worker remains alive anyway
today, and the postmaster doesn't need to do anything specific as far
as this fix is concerned.
The latter case could happen even today and
even reading the flag with LogicalDecodingControlLock doesn't help
since the logical decoding can be disabled after the postmaster
launches the slotsync worker. So I think the proposed approach would
work.
Also, once the sync worker is started, if the status changes to
disabled, the worker stays alive (I see a comment about a future
improvement to stop the worker in this case).
Another possible fix would be to let the postmaster start the slotsync
worker without logical decoding status check, and have the slotsync
worker check it with a lock and exit. But it would require more
changes and change the existing behavior, which is not appropriate for
v19.
Agreed. Let's keep things simple.
A few comments on the patch:
1/ Move this check after the sigsetjmp block and just call
ValidateSlotSyncParams with ERROR (to be safe, just in case the
validation grows an ERROR path in future).
+ if (!ValidateSlotSyncParams(LOG))
+ proc_exit(0);
2/ Also, I would simplify the comments.
For ReplSlotSyncWorkerMain():
/*
* Postmaster checked the logical decoding status without any lock (see
* IsLogicalDecodingEnabledNoLock()), confirm again here if it is still
* true.
*/
For IsLogicalDecodingEnabledNoLock() caller: no need to mention all
the function call chain in the comment.
/*
* 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.
*/
3/ Comment atop LogicalDecodingCtlData struct needs adjustment for the
status flag about it protected by the lock.
4/ TAP test is nice here and it is just for the records, not for commit.
--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com