1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-09 06:21:09 +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

@@ -2072,7 +2072,7 @@ BTreeShmemSize(void)
Size size;
size = offsetof(BTVacInfo, vacuums);
size = add_size(size, mul_size(MaxBackends, sizeof(BTOneVacInfo)));
size = add_size(size, mul_size(GetMaxBackends(), sizeof(BTOneVacInfo)));
return size;
}
@@ -2101,7 +2101,7 @@ BTreeShmemInit(void)
btvacinfo->cycle_ctr = (BTCycleId) time(NULL);
btvacinfo->num_vacuums = 0;
btvacinfo->max_vacuums = MaxBackends;
btvacinfo->max_vacuums = GetMaxBackends();
}
else
Assert(found);

View File

@@ -282,12 +282,11 @@ typedef struct MultiXactStateData
} MultiXactStateData;
/*
* Last element of OldestMemberMXactId and OldestVisibleMXactId arrays.
* Valid elements are (1..MaxOldestSlot); element 0 is never used.
* Pointers to the state data in shared memory
*
* The index of the last element of the OldestMemberMXactId and
* OldestVisibleMXacId arrays can be obtained with GetMaxOldestSlot().
*/
#define MaxOldestSlot (MaxBackends + max_prepared_xacts)
/* Pointers to the state data in shared memory */
static MultiXactStateData *MultiXactState;
static MultiXactId *OldestMemberMXactId;
static MultiXactId *OldestVisibleMXactId;
@@ -342,6 +341,7 @@ static void MultiXactIdSetOldestVisible(void);
static void RecordNewMultiXact(MultiXactId multi, MultiXactOffset offset,
int nmembers, MultiXactMember *members);
static MultiXactId GetNewMultiXactId(int nmembers, MultiXactOffset *offset);
static inline int GetMaxOldestSlot(void);
/* MultiXact cache management */
static int mxactMemberComparator(const void *arg1, const void *arg2);
@@ -662,6 +662,17 @@ MultiXactIdSetOldestMember(void)
}
}
/*
* Retrieve the index of the last element of the OldestMemberMXactId and
* OldestVisibleMXactId arrays. Valid elements are (1..MaxOldestSlot); element
* 0 is never used.
*/
static inline int
GetMaxOldestSlot(void)
{
return GetMaxBackends() + max_prepared_xacts;
}
/*
* MultiXactIdSetOldestVisible
* Save the oldest MultiXactId this transaction considers possibly live.
@@ -684,6 +695,7 @@ MultiXactIdSetOldestVisible(void)
if (!MultiXactIdIsValid(OldestVisibleMXactId[MyBackendId]))
{
MultiXactId oldestMXact;
int maxOldestSlot = GetMaxOldestSlot();
int i;
LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
@@ -697,7 +709,7 @@ MultiXactIdSetOldestVisible(void)
if (oldestMXact < FirstMultiXactId)
oldestMXact = FirstMultiXactId;
for (i = 1; i <= MaxOldestSlot; i++)
for (i = 1; i <= maxOldestSlot; i++)
{
MultiXactId thisoldest = OldestMemberMXactId[i];
@@ -1831,7 +1843,7 @@ MultiXactShmemSize(void)
/* We need 2*MaxOldestSlot + 1 perBackendXactIds[] entries */
#define SHARED_MULTIXACT_STATE_SIZE \
add_size(offsetof(MultiXactStateData, perBackendXactIds) + sizeof(MultiXactId), \
mul_size(sizeof(MultiXactId) * 2, MaxOldestSlot))
mul_size(sizeof(MultiXactId) * 2, GetMaxOldestSlot()))
size = SHARED_MULTIXACT_STATE_SIZE;
size = add_size(size, SimpleLruShmemSize(NUM_MULTIXACTOFFSET_BUFFERS, 0));
@@ -1882,7 +1894,7 @@ MultiXactShmemInit(void)
* since we only use indexes 1..MaxOldestSlot in each array.
*/
OldestMemberMXactId = MultiXactState->perBackendXactIds;
OldestVisibleMXactId = OldestMemberMXactId + MaxOldestSlot;
OldestVisibleMXactId = OldestMemberMXactId + GetMaxOldestSlot();
}
/*
@@ -2507,6 +2519,7 @@ GetOldestMultiXactId(void)
{
MultiXactId oldestMXact;
MultiXactId nextMXact;
int maxOldestSlot = GetMaxOldestSlot();
int i;
/*
@@ -2525,7 +2538,7 @@ GetOldestMultiXactId(void)
nextMXact = FirstMultiXactId;
oldestMXact = nextMXact;
for (i = 1; i <= MaxOldestSlot; i++)
for (i = 1; i <= maxOldestSlot; i++)
{
MultiXactId thisoldest;

View File

@@ -260,6 +260,7 @@ TwoPhaseShmemInit(void)
{
GlobalTransaction gxacts;
int i;
int max_backends = GetMaxBackends();
Assert(!found);
TwoPhaseState->freeGXacts = NULL;
@@ -293,7 +294,7 @@ TwoPhaseShmemInit(void)
* prepared transaction. Currently multixact.c uses that
* technique.
*/
gxacts[i].dummyBackendId = MaxBackends + 1 + i;
gxacts[i].dummyBackendId = max_backends + 1 + i;
}
}
else