diff options
author | Robert Haas <rhaas@postgresql.org> | 2022-02-08 15:52:40 -0500 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2022-02-08 15:53:19 -0500 |
commit | aa64f23b02924724eafbd9eadbf26d85df30a12b (patch) | |
tree | 54f72bfd7e36c2e879dc87149eb94db220d1cae3 /src/backend/storage/lmgr/proc.c | |
parent | 2da896182ce11240774af6c4d769777f90a09536 (diff) | |
download | postgresql-aa64f23b02924724eafbd9eadbf26d85df30a12b.tar.gz postgresql-aa64f23b02924724eafbd9eadbf26d85df30a12b.zip |
Remove MaxBackends variable in favor of GetMaxBackends() function.
Previously, it was really easy to write code that accessed MaxBackends
before we'd actually initialized it, especially when coding up an
extension. To make this less error-prune, introduce a new function
GetMaxBackends() which should be used to obtain the correct value.
This will ERROR if called too early. Demote the global variable to
a file-level static, so that nobody can peak at it directly.
Nathan Bossart. Idea by Andres Freund. Review by Greg Sabino Mullane,
by Michael Paquier (who had doubts about the approach), and by me.
Discussion: http://postgr.es/m/20210802224204.bckcikl45uezv5e4@alap3.anarazel.de
Diffstat (limited to 'src/backend/storage/lmgr/proc.c')
-rw-r--r-- | src/backend/storage/lmgr/proc.c | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index e306b04738d..37f032e7b95 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -103,7 +103,7 @@ ProcGlobalShmemSize(void) { Size size = 0; Size TotalProcs = - add_size(MaxBackends, add_size(NUM_AUXILIARY_PROCS, max_prepared_xacts)); + add_size(GetMaxBackends(), add_size(NUM_AUXILIARY_PROCS, max_prepared_xacts)); /* ProcGlobal */ size = add_size(size, sizeof(PROC_HDR)); @@ -127,7 +127,7 @@ ProcGlobalSemas(void) * We need a sema per backend (including autovacuum), plus one for each * auxiliary process. */ - return MaxBackends + NUM_AUXILIARY_PROCS; + return GetMaxBackends() + NUM_AUXILIARY_PROCS; } /* @@ -162,7 +162,8 @@ InitProcGlobal(void) int i, j; bool found; - uint32 TotalProcs = MaxBackends + NUM_AUXILIARY_PROCS + max_prepared_xacts; + int max_backends = GetMaxBackends(); + uint32 TotalProcs = max_backends + NUM_AUXILIARY_PROCS + max_prepared_xacts; /* Create the ProcGlobal shared structure */ ProcGlobal = (PROC_HDR *) @@ -195,7 +196,7 @@ InitProcGlobal(void) MemSet(procs, 0, TotalProcs * sizeof(PGPROC)); ProcGlobal->allProcs = procs; /* XXX allProcCount isn't really all of them; it excludes prepared xacts */ - ProcGlobal->allProcCount = MaxBackends + NUM_AUXILIARY_PROCS; + ProcGlobal->allProcCount = max_backends + NUM_AUXILIARY_PROCS; /* * Allocate arrays mirroring PGPROC fields in a dense manner. See @@ -221,7 +222,7 @@ InitProcGlobal(void) * dummy PGPROCs don't need these though - they're never associated * with a real process */ - if (i < MaxBackends + NUM_AUXILIARY_PROCS) + if (i < max_backends + NUM_AUXILIARY_PROCS) { procs[i].sem = PGSemaphoreCreate(); InitSharedLatch(&(procs[i].procLatch)); @@ -258,7 +259,7 @@ InitProcGlobal(void) ProcGlobal->bgworkerFreeProcs = &procs[i]; procs[i].procgloballist = &ProcGlobal->bgworkerFreeProcs; } - else if (i < MaxBackends) + else if (i < max_backends) { /* PGPROC for walsender, add to walsenderFreeProcs list */ procs[i].links.next = (SHM_QUEUE *) ProcGlobal->walsenderFreeProcs; @@ -286,8 +287,8 @@ InitProcGlobal(void) * Save pointers to the blocks of PGPROC structures reserved for auxiliary * processes and prepared transactions. */ - AuxiliaryProcs = &procs[MaxBackends]; - PreparedXactProcs = &procs[MaxBackends + NUM_AUXILIARY_PROCS]; + AuxiliaryProcs = &procs[max_backends]; + PreparedXactProcs = &procs[max_backends + NUM_AUXILIARY_PROCS]; /* Create ProcStructLock spinlock, too */ ProcStructLock = (slock_t *) ShmemAlloc(sizeof(slock_t)); |