Re: REPACK (CONCURRENTLY) backend waits indefinitely when decoding worker fails to start
Re: REPACK (CONCURRENTLY) backend waits indefinitely when decoding worker fails to start
От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Вложения:
Hi, On Thu, Aug 27, 2026 at 9:47 AM Nathan Bossart wrote: > > On Thu, Aug 27, 2026 at 09:41:49AM -0700, Bharath Rupireddy wrote: > > REPACK (CONCURRENTLY) starts a decoding bgworker and then waits in > > start_repack_decoding_worker() for the worker to set a shared-memory > > flag. The wait has no liveness check on the worker itself. If the > > worker never reaches that point (e.g., fork failure under memory > > pressure, or when BecomeLockGroupMember() returns false, or early exit > > before the shm_mq error redirect is set up), the backend waits > > indefinitely with no way out other than cancellation. I reproduced > > this with an induced fork failure, so I think we need to tighten this > > for both PG19 and HEAD branches. > > Oops, I just concurrently reported this [0]. Note that teardown can > deadlock, too. > > [0] https://postgr.es/m/apBpOVZOyqrakEr_%40nathan Thanks. Here's my first attempt at fixing both the fork failure hang and the teardown deadlock. I tried to use the parallel query approach as much as possible. The fork failure hang can occur because the backend running concurrent repack sleeps on a CV and ignores the SIGUSR1 sent via bgw_notify_pid by the postmaster upon fork failure. The teardown deadlock can occur because the backend waits for the worker to exit before detaching the error queue, while the worker is blocked writing to a full queue, so both end up waiting on each other. Although these issues seem rare to hit, I think it's good to tighten the repack code because users can see them via SQL. Therefore, I think we need to backpatch these to PG19. Please have a look at the attached patch. While here, I noticed that the same wait event is used for both the worker startup wait and the file export wait. Ideally these would have separate wait events, but given that the startup wait is typically very short, reusing the same one seems fine. -- Bharath Rupireddy Amazon Web Services: https://aws.amazon.com
Re: REPACK (CONCURRENTLY) backend waits indefinitely when decoding worker fails to start
Hi, On Mon, Aug 31, 2026 at 3:31 AM Antonin Houska wrote: > > Thanks for the fix(es). One thing I'm not sure I understand is: Thanks for reviewing. > @@ -3851,9 +3900,11 @@ ProcessRepackMessages(void) > > /* > * Nothing to do if we haven't launched the worker yet or have already > - * terminated it. > + * terminated it. stop_repack_decoding_worker() detaches the error queue > + * before clearing decoding_worker, so also bail out once error_mqh is > + * gone. > */ > - if (decoding_worker == NULL) > + if (decoding_worker == NULL || decoding_worker->error_mqh == NULL) > return; > > /* > > I don't think that stop_repack_decoding_worker() can clear ->error_mqh w/o > also clearing decoding_worker. With the patch, there's a window where stop_repack_decoding_worker() sets the error queue pointer to NULL before setting the worker's shared memory pointer to NULL. I would like to keep this check.| > Other than that, I'm not sure you need to mention the condition variable in > the comments. And maybe even the mentions of parallel workers are not > necessary. Reworded the comments. > > While here, I noticed that the same wait event is used for both the > > worker startup wait and the file export wait. Ideally these would have > > separate wait events, but given that the startup wait is typically > > very short, reusing the same one seems fine. > > I think that initially I also considered this situation not worth a new wait > event, but I probably had missed an existing one: > WAIT_EVENT_BGWORKER_STARTUP. It's already used for multiple workers, so we > could perhaps use it here. I think using WAIT_EVENT_BGWORKER_STARTUP is fine, because if a worker is ever stuck here, one can look at pg_stat_activity to tell whether this is the REPACK decoding worker. I've done that in the 0002 patch. Please find the attached v2 patches. -- Bharath Rupireddy Amazon Web Services: https://aws.amazon.com
Re: REPACK (CONCURRENTLY) backend waits indefinitely when decoding worker fails to start
On Mon, Sep 7, 2026 at 12:15 PM Bharath Rupireddy
wrote:
> After thinking more about this, I simplified the handling. There are two
> things the backend needs to wait for. First, for the worker to come up and
> attach to the error message queue. Second, for the worker to set up the
> logical decoding machinery before it waits for snapshot export. The first
> wait catches fork failures and worker startup issues. I used similar logic
> to what parallel.c uses for this. The second wait catches failures that
> happen after the worker starts up and attaches to the error queue but
> before it finishes setting up the logical decoding machinery. I kept the
> shared memory initialized flag with the CV wait as-is for this.
Álvaro pointed me here from my thread:
https://postgr.es/m/CAM527d9A7FBXJn7A3w2VAPq0Gojm7tnUDFMh1uwJitGfi2kNeQ@mail.gmail.com
My AI harness tested v4. Silent worker exits still hang three
post-attachment waits. Mihail raised snapshot/replay liveness in
December 2025.
Snapshot repro on a test build configured with --enable-injection-points,
with v4 plus only the attached patch's test support:
create extension injection_points;
create table t (i int primary key);
insert into t select generate_series(1, 100);
set statement_timeout = '2s';
select injection_points_attach(
'repack-worker-before-snapshot-export',
'injection_points', 'injection_exit', null);
repack (concurrently) t;
This times out after worker exit. The attached patch applies atop v4.
As far as I can see, the current uncommitted patches cover overall 5 cases:
- startup before queue attach: Bharath v4
- full error queue teardown: Bharath v4
- silent exit after attach, before initialization: attached patch
- silent exit before snapshot export: attached patch
- silent exit before replay export: attached patch
Nik
On Sun, Sep 13, 2026 at 11:09 AM Bharath Rupireddy
wrote:
>
> Hi,
>
> On Tue, Sep 8, 2026 at 12:17 AM Antonin Houska wrote:
> >
> > Bharath Rupireddy wrote:
> >
> > > On Fri, Sep 4, 2026 at 5:11 PM Masahiko Sawada wrote:
> > >
> > > After thinking more about this, I simplified the handling. There are two things the backend needs to wait for. First, for the worker to come up
> > > and attach to the error message queue. Second, for the worker to set up the logical decoding machinery before it waits for snapshot export.
> > > The first wait catches fork failures and worker startup issues. I used similar logic to what parallel.c uses for this. The second wait catches
> > > failures that happen after the worker starts up and attaches to the error queue but before it finishes setting up the logical decoding
> > > machinery. I kept the shared memory initialized flag with the CV wait as-is for this.
> > >
> > > Although the initialized flag wait may seem redundant with the snapshot export wait in get_initial_snapshot(), I would still keep it because it
> > > ensures the worker has fully set up the decoding before the backend proceeds.
> > >
> > > Dividing this into two separate waits (waiting for the worker to come up and attach to the error message queue, and then waiting for it to
> > > finish setup) makes the logic simpler, lets us reuse most of parallel.c's code, is easier to reason about, and fixes the hang issue without letting
> > > the backend reach the snapshot export wait with the worker not fully ready.
> >
> > Another reason for two separate waits is that two separate event types make
> > sense: WAIT_EVENT_BGWORKER_STARTUP and WAIT_EVENT_REPACK_WORKER_EXPORT.
>
> That's correct.
>
> > > Please find the attached v3 patch.
> >
> > Just two comments:
>
> Thanks for reviewing it.
>
> > * wait_for_repack_worker_to_attach() - as the decoding_worker variable is
> > declared static, this function does not necessarily need the argument.
>
> Removed the function parameter.
>
> > * Regarding comment: when the following is reached, the error message queue
> > has already been detached, so no implicit detaching should happen. Also,
> > there are no "other shared memory queues".
> >
> > + /*
> > + * If we have allocated a shared memory segment, detach it. This will
> > + * implicitly detach the error message queue, and any other shared memory
> > + * queues, stored there.
> > + */
> > + if (decoding_worker->seg != NULL)
> > + {
> > + dsm_detach(decoding_worker->seg);
> > + decoding_worker->seg = NULL;
> > + }
>
> I borrowed it from parallel.c. I agree it can be simplified, and I
> have done that.
>
> Please find the attached v4 patch.
>
> --
> Bharath Rupireddy
> Amazon Web Services: https://aws.amazon.com
Re: REPACK (CONCURRENTLY) backend waits indefinitely when decoding worker fails to start
Hi Bharath,
I reviewed v5 patch by patch. v6 is attached
No false alarms in 100 REPACK (CONCURRENTLY) runs with concurrent updates
and the owner's client_min_messages at debug1 and debug5, so the worker
often blocks on a full queue. pg_terminate_backend() on the worker gives
the backend the worker's own error at once.
One problem. The REPACK calls have no timeout, so a regression hangs
the test instead of failing it. Under meson it prints nothing until the
test is killed, and under make check nothing stops it. v6-0003 passes
timeout_default to those calls, so it now fails with "psql timed out"
at the right line. The rest of the test already waits with
timeout_default in poll_query_until(), pump_until() and
background_psql(), so this adds no new risk on slow machines.
0004
Looks fine. It matches what parallel.c does.
I reviewed v5 patch by patch. v6 is attached
0001
It works. I made fork() really fail with a low RLIMIT_NPROC. On master,
REPACK (CONCURRENTLY) waits until statement_timeout. With 0001 it fails
at once with "REPACK decoding worker failed to start".
One problem. stop_repack_decoding_worker() now calls dsm_detach() before
it waits for the worker to exit. If the worker has mapped the segment
but not yet called SharedFileSetAttach(), the detach destroys the file
set, and the worker fails with
ERROR: could not attach to a SharedFileSet that is already destroyed
I hit this 12 times in 1200 cancelled REPACKs. Only shm_mq_detach() has
to come before the wait to fix the deadlock, so v6-0001 moves
dsm_detach() back after the wait, as on master. That gives 0 in 1200.
It works. I made fork() really fail with a low RLIMIT_NPROC. On master,
REPACK (CONCURRENTLY) waits until statement_timeout. With 0001 it fails
at once with "REPACK decoding worker failed to start".
One problem. stop_repack_decoding_worker() now calls dsm_detach() before
it waits for the worker to exit. If the worker has mapped the segment
but not yet called SharedFileSetAttach(), the detach destroys the file
set, and the worker fails with
ERROR: could not attach to a SharedFileSet that is already destroyed
I hit this 12 times in 1200 cancelled REPACKs. Only shm_mq_detach() has
to come before the wait to fix the deadlock, so v6-0001 moves
dsm_detach() back after the wait, as on master. That gives 0 in 1200.
0002
Looks good. It follows parallel.c, and detaching before signalling is
needed, since with the old order the backend can read the queue too
early and never look again. With 0002 in, polling the worker status in
the wait loops, as Nikolay's patch does, is not needed.
Looks good. It follows parallel.c, and detaching before signalling is
needed, since with the old order the backend can read the queue too
early and never look again. With 0002 in, polling the worker status in
the wait loops, as Nikolay's patch does, is not needed.
No false alarms in 100 REPACK (CONCURRENTLY) runs with concurrent updates
and the owner's client_min_messages at debug1 and debug5, so the worker
often blocks on a full queue. pg_terminate_backend() on the worker gives
the backend the worker's own error at once.
0003
Please keep it. I checked that it catches each fix. Without the attach
wait, without the lost connection error, or with the queue detached
after the wait again, the test no longer passes.
Please keep it. I checked that it catches each fix. Without the attach
wait, without the lost connection error, or with the queue detached
after the wait again, the test no longer passes.
One problem. The REPACK calls have no timeout, so a regression hangs
the test instead of failing it. Under meson it prints nothing until the
test is killed, and under make check nothing stops it. v6-0003 passes
timeout_default to those calls, so it now fails with "psql timed out"
at the right line. The rest of the test already waits with
timeout_default in poll_query_until(), pump_until() and
background_psql(), so this adds no new risk on slow machines.
0004
Looks fine. It matches what parallel.c does.
Thanks,
Shihao
Re: REPACK (CONCURRENTLY) backend waits indefinitely when decoding worker fails to start
От:
Alvaro Herrera <alvherre@kurilemu.de>
Дата:
Hello, I have pushed these fixes, with some editorialization of my own. I decided not to push the test case, because it has some nontrivial additions to the injection point infrastructure that I'm not so sure about. The part of the test that doesn't require those changes could perhaps be considered for 19, and we can discuss adding the rest in 20. Thanks! -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
Re: REPACK (CONCURRENTLY) backend waits indefinitely when decoding worker fails to start
От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi, On Thu, Sep 17, 2026 at 2:53 AM Alvaro Herrera wrote: > > Hello, > > I have pushed these fixes, with some editorialization of my own. > > I decided not to push the test case, because it has some nontrivial > additions to the injection point infrastructure that I'm not so sure > about. The part of the test that doesn't require those changes could > perhaps be considered for 19, and we can discuss adding the rest in 20. Thanks Alvaro for taking care of this. I think it is fine to not have the tests in core and to have them here in the thread for reference. My thinking is that the issues fixed here look rare in practice and making them run reliably and deterministically would need more developer cycles. Moreover, we haven't had such tests for parallel query for a while, and testing resources are not free. Others may have a different opinion. And thanks Shihao for reviewing and identifying one of the problems and proposing the fix for it. -- Bharath Rupireddy Amazon Web Services: https://aws.amazon.com