Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2bdf1b2

Browse files
committed
Reserve a PGPROC slot and semaphore for the slotsync worker process.
The need for this was missed in commit 93db6cb, with the result being that if we launch a slotsync worker it would consume one of the PGPROCs in the max_connections pool. That could lead to inability to launch the worker, or to subsequent failures of connection requests that should have succeeded according to the configured settings. Rather than create some one-off infrastructure to support this, let's group the slotsync worker with the existing autovac launcher in a new category of "special worker" processes. These are kind of like auxiliary processes, but they cannot use that infrastructure because they need to be able to run transactions. For the moment, make these processes share the PGPROC freelist used for autovac workers (which previously supplied the autovac launcher too). This is partly to avoid an ABI change in v17, and partly because it seems silly to have a freelist with at most two members. This might be worth revisiting if we grow enough workers in this category. Tom Lane and Hou Zhijie. Back-patch to v17. Discussion: https://postgr.es/m/1808397.1735156190@sss.pgh.pa.us
1 parent ff90ee6 commit 2bdf1b2

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

src/backend/storage/lmgr/proc.c

+21-16
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,12 @@ InitProcGlobal(void)
197197

198198
/*
199199
* Create and initialize all the PGPROC structures we'll need. There are
200-
* five separate consumers: (1) normal backends, (2) autovacuum workers
201-
* and the autovacuum launcher, (3) background workers, (4) auxiliary
202-
* processes, and (5) prepared transactions. Each PGPROC structure is
203-
* dedicated to exactly one of these purposes, and they do not move
204-
* between groups.
200+
* six separate consumers: (1) normal backends, (2) autovacuum workers and
201+
* special workers, (3) background workers, (4) walsenders, (5) auxiliary
202+
* processes, and (6) prepared transactions. (For largely-historical
203+
* reasons, we combine autovacuum and special workers into one category
204+
* with a single freelist.) Each PGPROC structure is dedicated to exactly
205+
* one of these purposes, and they do not move between groups.
205206
*/
206207
procs = (PGPROC *) ShmemAlloc(TotalProcs * sizeof(PGPROC));
207208
MemSet(procs, 0, TotalProcs * sizeof(PGPROC));
@@ -269,26 +270,27 @@ InitProcGlobal(void)
269270
}
270271

271272
/*
272-
* Newly created PGPROCs for normal backends, autovacuum and bgworkers
273-
* must be queued up on the appropriate free list. Because there can
274-
* only ever be a small, fixed number of auxiliary processes, no free
275-
* list is used in that case; InitAuxiliaryProcess() instead uses a
276-
* linear search. PGPROCs for prepared transactions are added to a
277-
* free list by TwoPhaseShmemInit().
273+
* Newly created PGPROCs for normal backends, autovacuum workers,
274+
* special workers, bgworkers, and walsenders must be queued up on the
275+
* appropriate free list. Because there can only ever be a small,
276+
* fixed number of auxiliary processes, no free list is used in that
277+
* case; InitAuxiliaryProcess() instead uses a linear search. PGPROCs
278+
* for prepared transactions are added to a free list by
279+
* TwoPhaseShmemInit().
278280
*/
279281
if (i < MaxConnections)
280282
{
281283
/* PGPROC for normal backend, add to freeProcs list */
282284
dlist_push_tail(&ProcGlobal->freeProcs, &proc->links);
283285
proc->procgloballist = &ProcGlobal->freeProcs;
284286
}
285-
else if (i < MaxConnections + autovacuum_max_workers + 1)
287+
else if (i < MaxConnections + autovacuum_max_workers + NUM_SPECIAL_WORKER_PROCS)
286288
{
287-
/* PGPROC for AV launcher/worker, add to autovacFreeProcs list */
289+
/* PGPROC for AV or special worker, add to autovacFreeProcs list */
288290
dlist_push_tail(&ProcGlobal->autovacFreeProcs, &proc->links);
289291
proc->procgloballist = &ProcGlobal->autovacFreeProcs;
290292
}
291-
else if (i < MaxConnections + autovacuum_max_workers + 1 + max_worker_processes)
293+
else if (i < MaxConnections + autovacuum_max_workers + NUM_SPECIAL_WORKER_PROCS + max_worker_processes)
292294
{
293295
/* PGPROC for bgworker, add to bgworkerFreeProcs list */
294296
dlist_push_tail(&ProcGlobal->bgworkerFreeProcs, &proc->links);
@@ -358,8 +360,11 @@ InitProcess(void)
358360
if (IsUnderPostmaster)
359361
RegisterPostmasterChildActive();
360362

361-
/* Decide which list should supply our PGPROC. */
362-
if (AmAutoVacuumLauncherProcess() || AmAutoVacuumWorkerProcess())
363+
/*
364+
* Decide which list should supply our PGPROC. This logic must match the
365+
* way the freelists were constructed in InitProcGlobal().
366+
*/
367+
if (AmAutoVacuumWorkerProcess() || AmSpecialWorkerProcess())
363368
procgloballist = &ProcGlobal->autovacFreeProcs;
364369
else if (AmBackgroundWorkerProcess())
365370
procgloballist = &ProcGlobal->bgworkerFreeProcs;

src/backend/utils/init/postinit.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,9 @@ InitializeMaxBackends(void)
544544
{
545545
Assert(MaxBackends == 0);
546546

547-
/* the extra unit accounts for the autovacuum launcher */
548-
MaxBackends = MaxConnections + autovacuum_max_workers + 1 +
549-
max_worker_processes + max_wal_senders;
547+
/* Note that this does not include "auxiliary" processes */
548+
MaxBackends = MaxConnections + autovacuum_max_workers +
549+
max_worker_processes + max_wal_senders + NUM_SPECIAL_WORKER_PROCS;
550550

551551
if (MaxBackends > MAX_BACKENDS)
552552
ereport(ERROR,
@@ -555,7 +555,7 @@ InitializeMaxBackends(void)
555555
errdetail("\"max_connections\" (%d) plus \"autovacuum_max_workers\" (%d) plus \"max_worker_processes\" (%d) plus \"max_wal_senders\" (%d) must be less than %d.",
556556
MaxConnections, autovacuum_max_workers,
557557
max_worker_processes, max_wal_senders,
558-
MAX_BACKENDS)));
558+
MAX_BACKENDS - (NUM_SPECIAL_WORKER_PROCS - 1))));
559559
}
560560

561561
/*

src/include/miscadmin.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,9 @@ typedef enum BackendType
350350

351351
/*
352352
* Auxiliary processes. These have PGPROC entries, but they are not
353-
* attached to any particular database. There can be only one of each of
354-
* these running at a time.
353+
* attached to any particular database, and cannot run transactions or
354+
* even take heavyweight locks. There can be only one of each of these
355+
* running at a time.
355356
*
356357
* If you modify these, make sure to update NUM_AUXILIARY_PROCS and the
357358
* glossary in the docs.
@@ -388,6 +389,10 @@ extern PGDLLIMPORT BackendType MyBackendType;
388389
#define AmWalSummarizerProcess() (MyBackendType == B_WAL_SUMMARIZER)
389390
#define AmWalWriterProcess() (MyBackendType == B_WAL_WRITER)
390391

392+
#define AmSpecialWorkerProcess() \
393+
(AmAutoVacuumLauncherProcess() || \
394+
AmLogicalSlotSyncWorkerProcess())
395+
391396
extern const char *GetBackendTypeDesc(BackendType backendType);
392397

393398
extern void SetDatabasePath(const char *path);

src/include/storage/proc.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ typedef struct PROC_HDR
408408
uint32 allProcCount;
409409
/* Head of list of free PGPROC structures */
410410
dlist_head freeProcs;
411-
/* Head of list of autovacuum's free PGPROC structures */
411+
/* Head of list of autovacuum & special worker free PGPROC structures */
412412
dlist_head autovacFreeProcs;
413413
/* Head of list of bgworker free PGPROC structures */
414414
dlist_head bgworkerFreeProcs;
@@ -442,9 +442,19 @@ extern PGDLLIMPORT PGPROC *PreparedXactProcs;
442442
#define GetPGProcByNumber(n) (&ProcGlobal->allProcs[(n)])
443443
#define GetNumberFromPGProc(proc) ((proc) - &ProcGlobal->allProcs[0])
444444

445+
/*
446+
* We set aside some extra PGPROC structures for "special worker" processes,
447+
* which are full-fledged backends (they can run transactions)
448+
* but are unique animals that there's never more than one of.
449+
* Currently there are two such processes: the autovacuum launcher
450+
* and the slotsync worker.
451+
*/
452+
#define NUM_SPECIAL_WORKER_PROCS 2
453+
445454
/*
446455
* We set aside some extra PGPROC structures for auxiliary processes,
447-
* ie things that aren't full-fledged backends but need shmem access.
456+
* ie things that aren't full-fledged backends (they cannot run transactions
457+
* or take heavyweight locks) but need shmem access.
448458
*
449459
* Background writer, checkpointer, WAL writer, WAL summarizer, and archiver
450460
* run during normal operation. Startup process and WAL receiver also consume

0 commit comments

Comments
 (0)