1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-19 13:42:17 +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

@@ -80,13 +80,6 @@ typedef struct
ProcSignalSlot psh_slot[FLEXIBLE_ARRAY_MEMBER];
} ProcSignalHeader;
/*
* We reserve a slot for each possible BackendId, plus one for each
* possible auxiliary process type. (This scheme assumes there is not
* more than one of any auxiliary process type at a time.)
*/
#define NumProcSignalSlots (MaxBackends + NUM_AUXPROCTYPES)
/* Check whether the relevant type bit is set in the flags. */
#define BARRIER_SHOULD_CHECK(flags, type) \
(((flags) & (((uint32) 1) << (uint32) (type))) != 0)
@@ -102,6 +95,20 @@ static bool CheckProcSignal(ProcSignalReason reason);
static void CleanupProcSignalState(int status, Datum arg);
static void ResetProcSignalBarrierBits(uint32 flags);
static bool ProcessBarrierPlaceholder(void);
static inline int GetNumProcSignalSlots(void);
/*
* GetNumProcSignalSlots
*
* We reserve a slot for each possible BackendId, plus one for each possible
* auxiliary process type. (This scheme assume there is not more than one of
* any auxiliary process type at a time.)
*/
static inline int
GetNumProcSignalSlots(void)
{
return GetMaxBackends() + NUM_AUXPROCTYPES;
}
/*
* ProcSignalShmemSize
@@ -112,7 +119,7 @@ ProcSignalShmemSize(void)
{
Size size;
size = mul_size(NumProcSignalSlots, sizeof(ProcSignalSlot));
size = mul_size(GetNumProcSignalSlots(), sizeof(ProcSignalSlot));
size = add_size(size, offsetof(ProcSignalHeader, psh_slot));
return size;
}
@@ -126,6 +133,7 @@ ProcSignalShmemInit(void)
{
Size size = ProcSignalShmemSize();
bool found;
int numProcSignalSlots = GetNumProcSignalSlots();
ProcSignal = (ProcSignalHeader *)
ShmemInitStruct("ProcSignal", size, &found);
@@ -137,7 +145,7 @@ ProcSignalShmemInit(void)
pg_atomic_init_u64(&ProcSignal->psh_barrierGeneration, 0);
for (i = 0; i < NumProcSignalSlots; ++i)
for (i = 0; i < numProcSignalSlots; ++i)
{
ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
@@ -163,7 +171,7 @@ ProcSignalInit(int pss_idx)
ProcSignalSlot *slot;
uint64 barrier_generation;
Assert(pss_idx >= 1 && pss_idx <= NumProcSignalSlots);
Assert(pss_idx >= 1 && pss_idx <= GetNumProcSignalSlots());
slot = &ProcSignal->psh_slot[pss_idx - 1];
@@ -292,7 +300,7 @@ SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId)
*/
int i;
for (i = NumProcSignalSlots - 1; i >= 0; i--)
for (i = GetNumProcSignalSlots() - 1; i >= 0; i--)
{
slot = &ProcSignal->psh_slot[i];
@@ -333,6 +341,7 @@ EmitProcSignalBarrier(ProcSignalBarrierType type)
{
uint32 flagbit = 1 << (uint32) type;
uint64 generation;
int numProcSignalSlots = GetNumProcSignalSlots();
/*
* Set all the flags.
@@ -342,7 +351,7 @@ EmitProcSignalBarrier(ProcSignalBarrierType type)
* anything that we do afterwards. (This is also true of the later call to
* pg_atomic_add_fetch_u64.)
*/
for (int i = 0; i < NumProcSignalSlots; i++)
for (int i = 0; i < numProcSignalSlots; i++)
{
volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
@@ -368,7 +377,7 @@ EmitProcSignalBarrier(ProcSignalBarrierType type)
* backends that need to update state - but they won't actually need to
* change any state.
*/
for (int i = NumProcSignalSlots - 1; i >= 0; i--)
for (int i = numProcSignalSlots - 1; i >= 0; i--)
{
volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
pid_t pid = slot->pss_pid;
@@ -393,7 +402,7 @@ WaitForProcSignalBarrier(uint64 generation)
{
Assert(generation <= pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration));
for (int i = NumProcSignalSlots - 1; i >= 0; i--)
for (int i = GetNumProcSignalSlots() - 1; i >= 0; i--)
{
ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
uint64 oldval;