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

Поиск
Список
Период
Сортировка
Искать

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

От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
Hi,

On Sun, Sep 27, 2026 at 5:06 PM Bharath Rupireddy
 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


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

От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
On Mon, Sep 28, 2026 at 9:58 AM Masahiko Sawada  wrote:
>
> Hi,
>
> On Sun, Sep 27, 2026 at 5:06 PM Bharath Rupireddy
>  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


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

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
Вложения:
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

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

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
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

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

От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi,

On Wed, Sep 30, 2026 at 3:08 AM Ayush Tiwari
 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


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

От:
shihao zhong <zhong950419@gmail.com>
Дата:
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
FAQ