Re: Postmaster PANIC on a standby with slot synchronization enabled

From: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>
To: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Cc: "T, Adesh" <adeshdb(at)amazon(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Postmaster PANIC on a standby with slot synchronization enabled
Date: 2026-11-23 14:22:27
Message-ID: CALj2ACVP-Sq3DCPcRxJnbdzFMxbTbeix=Nwat2+HBdBaVjydgA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Fri, Nov 20, 2026 at 3:41 PM Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com> wrote:
>
> On Fri, Nov 20, 2026 at 2:46 PM T, Adesh <adeshdb(at)amazon(dot)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

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Virender Singla 2026-11-23 13:50:11 Re: [PATCH] TRUNCATE's reset of n_live_tup/n_dead_tup is lost