ProcArrayStruct->pgprocnos vs ->maxProcs vs PGPROC ordering

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-hackers(at)postgresql(dot)org
Subject: ProcArrayStruct->pgprocnos vs ->maxProcs vs PGPROC ordering
Date: 2021-06-12 01:44:34
Message-ID: 20210612014434.lmeghlt3yx4ue2br@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I just tried to add a, seemingly innocuous, assertion to
ProcArrayAdd()/Remove() that proc->pgprocno is < arrayP->maxProcs. That
quickly fails.

The reason for that is that PGPROC are created in the following order
1) MaxBackends normal (*[1]) backends
2) NUM_AUXILIARY_PROCS auxiliary processes
3) max_prepared_xacts prepared transactions.

and pgprocnos are assigned sequentially - they are needed to index into
ProcGlobal->allProcs.

In contrast to that procarray.c initializes maxProcs to
#define PROCARRAY_MAXPROCS (MaxBackends + max_prepared_xacts)
i.e. without the aux processes.

Which means that some of the prepared transactions have pgprocnos that are
bigger than ProcArrayStruct->maxProcs. Hence my assertion failure.

This is obviously not a bug, but is quite hard to understand / confusing. I
think I made a similar mistake before.

I'd at least like to add a comment with a warning somewhere in ProcArrayStruct
or such.

An alternative approach would be to change the PGPROC order to instead be 1)
aux, b) normal backends, 3) prepared xacts and give aux processes a negative
or invalid pgprocno.

One small advantage of that would be that we'd not need to "skip" over the
"aux process hole" between normal and prepared xact PGPROCs in various
procarray.c routines that iterate over procs.

Greetings,

Andres Freund

[1] well, kinda. It's user backends followed by autovacuum worker, launcher,
worker processes and wal senders.

Browse pgsql-hackers by date

  From Date Subject
Next Message Justin Pryzby 2021-06-12 01:54:08 Re: A new function to wait for the backend exit after termination
Previous Message Bruce Momjian 2021-06-12 01:42:41 Re: array_cat anycompatible change is breaking xversion upgrade tests (v14 release notes)