1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-02 04:21:28 +03:00

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
This commit is contained in:
Robert Haas
2022-02-08 15:52:40 -05:00
parent 2da896182c
commit aa64f23b02
19 changed files with 218 additions and 133 deletions

View File

@@ -97,7 +97,7 @@ typedef struct ProcArrayStruct
/* oldest catalog xmin of any replication slot */
TransactionId replication_slot_catalog_xmin;
/* indexes into allProcs[], has PROCARRAY_MAXPROCS entries */
/* indexes into allProcs[], has ProcArrayMaxProcs entries */
int pgprocnos[FLEXIBLE_ARRAY_MEMBER];
} ProcArrayStruct;
@@ -355,6 +355,17 @@ static void MaintainLatestCompletedXidRecovery(TransactionId latestXid);
static inline FullTransactionId FullXidRelativeTo(FullTransactionId rel,
TransactionId xid);
static void GlobalVisUpdateApply(ComputeXidHorizonsResult *horizons);
static inline int GetProcArrayMaxProcs(void);
/*
* Retrieve the number of slots in the ProcArray structure.
*/
static inline int
GetProcArrayMaxProcs(void)
{
return GetMaxBackends() + max_prepared_xacts;
}
/*
* Report shared-memory space needed by CreateSharedProcArray.
@@ -365,10 +376,8 @@ ProcArrayShmemSize(void)
Size size;
/* Size of the ProcArray structure itself */
#define PROCARRAY_MAXPROCS (MaxBackends + max_prepared_xacts)
size = offsetof(ProcArrayStruct, pgprocnos);
size = add_size(size, mul_size(sizeof(int), PROCARRAY_MAXPROCS));
size = add_size(size, mul_size(sizeof(int), GetProcArrayMaxProcs()));
/*
* During Hot Standby processing we have a data structure called
@@ -384,7 +393,7 @@ ProcArrayShmemSize(void)
* shared memory is being set up.
*/
#define TOTAL_MAX_CACHED_SUBXIDS \
((PGPROC_MAX_CACHED_SUBXIDS + 1) * PROCARRAY_MAXPROCS)
((PGPROC_MAX_CACHED_SUBXIDS + 1) * GetProcArrayMaxProcs())
if (EnableHotStandby)
{
@@ -411,7 +420,7 @@ CreateSharedProcArray(void)
ShmemInitStruct("Proc Array",
add_size(offsetof(ProcArrayStruct, pgprocnos),
mul_size(sizeof(int),
PROCARRAY_MAXPROCS)),
GetProcArrayMaxProcs())),
&found);
if (!found)
@@ -420,7 +429,7 @@ CreateSharedProcArray(void)
* We're the first - initialize.
*/
procArray->numProcs = 0;
procArray->maxProcs = PROCARRAY_MAXPROCS;
procArray->maxProcs = GetProcArrayMaxProcs();
procArray->maxKnownAssignedXids = TOTAL_MAX_CACHED_SUBXIDS;
procArray->numKnownAssignedXids = 0;
procArray->tailKnownAssignedXids = 0;
@@ -4623,7 +4632,7 @@ KnownAssignedXidsCompress(bool force)
*/
int nelements = head - tail;
if (nelements < 4 * PROCARRAY_MAXPROCS ||
if (nelements < 4 * GetProcArrayMaxProcs() ||
nelements < 2 * pArray->numKnownAssignedXids)
return;
}