Parallel autovacuum: leader crashes when no DSM segment can be created

Started by Bharath Rupireddyabout 2 months ago7 messageshackers
Jump to latest
#1Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com

Hi,

AI review found a bug in parallel autovacuum (1ff3180ca01). I checked
it myself and it reproduces on HEAD and PG19. Patch and reproducer
attached.

When no DSM segment can be created, InitializeParallelDSM() does not
fail. It sets up the parallel context in the leader's own memory with
zero workers and leaves pcxt->seg NULL. Parallel query checks that
pointer before using it, see ExecInitParallelPlan(), and VACUUM
(PARALLEL) never looks at it and vacuums all the indexes in the
leader. Parallel autovacuum registers a DSM detach callback on it, so
the autovacuum worker segfaults and the database goes through crash
recovery. It also leaves pv_shared_cost_params pointing into the
leader's private memory with nothing left to reset it.

It is not easy to hit. Right after the fallback, vacuum creates the
DSA area for its dead items, and with no segments left that fails with
a plain "too many dynamic shared memory segments" error before the
callback is reached. Another backend has to free a segment in the
window between the two.

The fix is to set up the cost parameters and the callback only when
the context has workers.

The attached script fills the segments with parallel queries and keeps
autovacuum busy on tables needing parallel index vacuuming. Most
attempts fail with that error, then one lands in the window and
crashes the worker, usually within a few seconds.

[1]: 2026-09-26 21:31:55.790 UTC [7908] LOG: autovacuum worker (PID 8904) was terminated by signal 11: Segmentation fault 2026-09-26 21:31:55.790 UTC [7908] DETAIL: Failed process was running: autovacuum: VACUUM ANALYZE public.t3
2026-09-26 21:31:55.790 UTC [7908] LOG: autovacuum worker (PID 8904)
was terminated by signal 11: Segmentation fault
2026-09-26 21:31:55.790 UTC [7908] DETAIL: Failed process was
running: autovacuum: VACUUM ANALYZE public.t3

#0 slist_push_head (head=0x38, node=0x10468ff8) at
../../../../src/include/lib/ilist.h:1008
#1 on_dsm_detach (seg=0x0, function=0x775bce
<parallel_vacuum_dsm_detach>, arg=0) at dsm.c:1148
#2 parallel_vacuum_init (rel=0x7f39f5bf0cf0, indrels=0x104be9a0,
nindexes=3, nrequested_workers=2, vac_work_mem=65536, elevel=13,
bstrategy=0x104ec410) at vacuumparallel.c:470
#3 dead_items_alloc (vacrel=0x104be050, nworkers=2) at vacuumlazy.c:3471
#4 heap_vacuum_rel (rel=0x7f39f5bf0cf0, params=0x7fffcadde380,
bstrategy=0x104ec410) at vacuumlazy.c:863
#5 table_relation_vacuum (rel=0x7f39f5bf0cf0, params=0x7fffcadde380,
bstrategy=0x104ec410) at ../../../src/include/access/tableam.h:1802
#6 vacuum_rel (relid=16399, relation=0x10501360, params=...,
bstrategy=0x104ec410, isTopLevel=true) at vacuum.c:2353
#7 vacuum (relations=0x10501480, params=0x104fd5e0,
bstrategy=0x104ec410, vac_context=0x10501260, isTopLevel=true) at
vacuum.c:635
#8 autovacuum_do_vac_analyze (tab=0x104fd5d8, bstrategy=0x104ec410)
at autovacuum.c:3351
#9 do_autovacuum () at autovacuum.c:2535
#10 AutoVacWorkerMain (startup_data=0x0, startup_data_len=0) at
autovacuum.c:1634
#11 postmaster_child_launch (child_type=B_AUTOVAC_WORKER,
child_slot=10, startup_data=0x0, startup_data_len=0, client_sock=0x0)
at launch_backend.c:268
#12 StartChildProcess (type=B_AUTOVAC_WORKER) at postmaster.c:4043
#13 StartAutovacuumWorker () at postmaster.c:4107
#14 process_pm_pmsignal () at postmaster.c:3864
#15 ServerLoop () at postmaster.c:1720
#16 PostmasterMain (argc=3, argv=0x1043c510) at postmaster.c:1414
#17 main (argc=3, argv=0x1043c510) at main.c:227

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

Attachments:

t253960_1
v1-0001-Fix-crash-in-parallel-autovacuum-when-no-DSM-segm.patchapplication/x-patch; name=v1-0001-Fix-crash-in-parallel-autovacuum-when-no-DSM-segm.patchDownload+6-3
repro.shapplication/x-sh; name=repro.shDownload
#2Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Bharath Rupireddy (#1)
Re: Parallel autovacuum: leader crashes when no DSM segment can be created

Hi,

On Sun, Sep 27, 2026 at 5:06 PM Bharath Rupireddy
<bharath.rupireddyforpostgres@gmail.com> wrote:

Hi,

AI review found a bug in parallel autovacuum (1ff3180ca01). I checked
it myself and it reproduces on HEAD and PG19. Patch and reproducer
attached.

When no DSM segment can be created, InitializeParallelDSM() does not
fail. It sets up the parallel context in the leader's own memory with
zero workers and leaves pcxt->seg NULL. Parallel query checks that
pointer before using it, see ExecInitParallelPlan(), and VACUUM
(PARALLEL) never looks at it and vacuums all the indexes in the
leader. Parallel autovacuum registers a DSM detach callback on it, so
the autovacuum worker segfaults and the database goes through crash
recovery. It also leaves pv_shared_cost_params pointing into the
leader's private memory with nothing left to reset it.

Thank you for the report and the patch!

I agree with your analysis and it should be fixed. The proposed patch
is simple and looks good to me, so I'm going to push it barring any
objections.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#3Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Masahiko Sawada (#2)
Re: Parallel autovacuum: leader crashes when no DSM segment can be created

On Mon, Sep 28, 2026 at 9:58 AM Masahiko Sawada <sawada.mshk@gmail.com> wrote:

Hi,

On Sun, Sep 27, 2026 at 5:06 PM Bharath Rupireddy
<bharath.rupireddyforpostgres@gmail.com> wrote:

Hi,

AI review found a bug in parallel autovacuum (1ff3180ca01). I checked
it myself and it reproduces on HEAD and PG19. Patch and reproducer
attached.

When no DSM segment can be created, InitializeParallelDSM() does not
fail. It sets up the parallel context in the leader's own memory with
zero workers and leaves pcxt->seg NULL. Parallel query checks that
pointer before using it, see ExecInitParallelPlan(), and VACUUM
(PARALLEL) never looks at it and vacuums all the indexes in the
leader. Parallel autovacuum registers a DSM detach callback on it, so
the autovacuum worker segfaults and the database goes through crash
recovery. It also leaves pv_shared_cost_params pointing into the
leader's private memory with nothing left to reset it.

Thank you for the report and the patch!

I agree with your analysis and it should be fixed. The proposed patch
is simple and looks good to me, so I'm going to push it barring any
objections.

Pushed.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#4Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Masahiko Sawada (#3)
Re: Parallel autovacuum: leader crashes when no DSM segment can be created

Hi,

I wonder if we should also back out of parallel_vacuum_init() when
pcxt->seg is NULL, like the parallel index builds do.

AFAICS, we otherwise go on to TidStoreCreateShared(), which needs
another DSM segment. That seems to defeat the leader-only fallback
when DSM slots stay exhausted. On master, with another session holding
every DSM slot, VACUUM (PARALLEL 2) fails with "too many dynamic shared
memory segments", while VACUUM (PARALLEL 0) succeeds.

The attached patch returns NULL there, letting the caller use a local
TidStore, and drops the nworkers check from cc053b6e127, which I think
becomes redundant?

Am I missing a reason to keep the shared TidStore in the no-worker case?

Regards,
Ayush

Attachments:

t253960_4
v1-0001-Fall-back-to-serial-vacuum-when-out-of-DSM-segmen.patchapplication/octet-stream; name=v1-0001-Fall-back-to-serial-vacuum-when-out-of-DSM-segmen.patchDownload+12-7
#5Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Ayush Tiwari (#4)
Re: Parallel autovacuum: leader crashes when no DSM segment can be created

Hi,

On Wed, 30 Sept 2026 at 15:38, Ayush Tiwari <ayushtiwari.slg01@gmail.com>
wrote:

I wonder if we should also back out of parallel_vacuum_init() when
pcxt->seg is NULL, like the parallel index builds do.

AFAICS, we otherwise go on to TidStoreCreateShared(), which needs
another DSM segment. That seems to defeat the leader-only fallback
when DSM slots stay exhausted. On master, with another session holding
every DSM slot, VACUUM (PARALLEL 2) fails with "too many dynamic shared
memory segments", while VACUUM (PARALLEL 0) succeeds.

The attached patch returns NULL there, letting the caller use a local
TidStore, and drops the nworkers check from cc053b6e127, which I think
becomes redundant?

Am I missing a reason to keep the shared TidStore in the no-worker case?

Registered this in commitfest:https://commitfest.postgresql.org/patch/7380/

Regards,
Ayush

#6Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Ayush Tiwari (#4)
Re: Parallel autovacuum: leader crashes when no DSM segment can be created

Hi,

On Wed, Sep 30, 2026 at 3:08 AM Ayush Tiwari
<ayushtiwari.slg01@gmail.com> wrote:

I wonder if we should also back out of parallel_vacuum_init() when
pcxt->seg is NULL, like the parallel index builds do.

I don't think falling back to non-parallel vacuum only when the
parallel context DSM creation fails is the right approach. For
instance, the parallel context could be created successfully, and then
later either the first or a subsequent dead-TID DSM allocation could
fail and still hit the too many DSM segments error. I don't see a
clean way to handle such cases. Since it can error out anyway, I think
failing the vacuum in these cases is fine.

The segv fixed in cc053b6e127 seems fine to me, because
InitializeParallelDSM() expects callers to work with no workers when
it detects that no DSM could be allocated.

That said, others may have different thoughts, so +1 to continuing to
pursue this for HEAD.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

#7shihao zhong
zhong950419@gmail.com
In reply to: Bharath Rupireddy (#6)
Re: Parallel autovacuum: leader crashes when no DSM segment can be created

Hi,

Bharath is right that it still fails when only one slot is left. But
I think v1 still fixes the case where all slots are taken.

That said, running out of DSM slots seems very rare. I have never seen
it happen, and this thread is the first time I learned the limit
exists.

If we think it is a real problem, how about reserving some DSM slots
just for vacuum? That would be similar to how autovacuum_worker_slots
gives autovacuum workers their own pool.

Thanks,
Shihao