From 7ca50643ceb6ae5c86cc38a1d62a3f7f6f5053c5 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Fri, 13 Mar 2026 23:01:44 +0200 Subject: [PATCH v5 5/5] Use the new mechanism in a few core subsystems I chose these subsystems specifically because they have some complicating properties, making them slightly harder to convert than most: - The initialization callbacks of some of these subsystems have dependencies, i.e. they need to be initialized in the right order. - The ProgGlobal pointer still needs to be inherited by the BackendParameters mechanism on EXEC_BACKEND builds, because ProcGlobal is required by InitProcess() to get a PGPROC entry, and the PGPROC entry is required to use LWLocks, and usually attaching to shared memory areas requires the use of LWLocks. - Similarly, ProcSignal pointer still needs to be handled by BackendParameters, because query cancellation connections access it without calling InitProcess I'm believe converting all the rest of the subsystems after this will be pretty mechanic. Reviewed-by: Ashutosh Bapat Reviewed-by: Zsolt Parragi Discussion: https://www.postgresql.org/message-id/CAExHW5vM1bneLYfg0wGeAa=52UiJ3z4vKd3AJ72X8Fw6k3KKrg@mail.gmail.com --- src/backend/access/transam/varsup.c | 33 ++--- src/backend/storage/ipc/dsm.c | 61 ++++---- src/backend/storage/ipc/dsm_registry.c | 34 ++--- src/backend/storage/ipc/ipci.c | 50 +++---- src/backend/storage/ipc/pmsignal.c | 49 ++++--- src/backend/storage/ipc/procarray.c | 122 ++++++++-------- src/backend/storage/ipc/procsignal.c | 63 ++++----- src/backend/storage/ipc/sinvaladt.c | 36 ++--- src/backend/storage/lmgr/proc.c | 178 ++++++++++++------------ src/backend/utils/activity/wait_event.c | 94 ++++++------- src/include/access/transam.h | 3 +- src/include/storage/dsm.h | 3 +- src/include/storage/dsm_registry.h | 3 +- src/include/storage/pmsignal.h | 3 +- src/include/storage/proc.h | 2 +- src/include/storage/procarray.h | 3 +- src/include/storage/procsignal.h | 3 +- src/include/storage/sinvaladt.h | 3 +- src/include/utils/wait_event.h | 3 +- 19 files changed, 366 insertions(+), 380 deletions(-) diff --git a/src/backend/access/transam/varsup.c b/src/backend/access/transam/varsup.c index 3e95d4cfd16..84e6c90f4fa 100644 --- a/src/backend/access/transam/varsup.c +++ b/src/backend/access/transam/varsup.c @@ -30,35 +30,32 @@ /* Number of OIDs to prefetch (preallocate) per XLOG write */ #define VAR_OID_PREFETCH 8192 +static void VarsupShmemInit(void *arg); + /* pointer to variables struct in shared memory */ TransamVariablesData *TransamVariables = NULL; +static ShmemStructDesc TransamVariablesShmemDesc = { + .name = "TransamVariables", + .size = sizeof(TransamVariablesData), + .init_fn = VarsupShmemInit, + .ptr = (void **) &TransamVariables, +}; /* * Initialization of shared memory for TransamVariables. */ -Size -VarsupShmemSize(void) +void +VarsupShmemRegister(void) { - return sizeof(TransamVariablesData); + ShmemRegisterStruct(&TransamVariablesShmemDesc); } -void -VarsupShmemInit(void) -{ - bool found; +static void +VarsupShmemInit(void *arg) - /* Initialize our shared state struct */ - TransamVariables = ShmemInitStruct("TransamVariables", - sizeof(TransamVariablesData), - &found); - if (!IsUnderPostmaster) - { - Assert(!found); - memset(TransamVariables, 0, sizeof(TransamVariablesData)); - } - else - Assert(found); +{ + memset(TransamVariables, 0, sizeof(TransamVariablesData)); } /* diff --git a/src/backend/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c index 6a5b16392f7..52b4a1e017f 100644 --- a/src/backend/storage/ipc/dsm.c +++ b/src/backend/storage/ipc/dsm.c @@ -110,6 +110,15 @@ static bool dsm_init_done = false; /* Preallocated DSM space in the main shared memory region. */ static void *dsm_main_space_begin = NULL; +static void dsm_main_space_init(void *); + +static ShmemStructDesc dsm_main_space_shmem_desc = { + .name = "Preallocated DSM", + .size = 0, /* calculated later */ + .init_fn = dsm_main_space_init, + .ptr = &dsm_main_space_begin, +}; + /* * List of dynamic shared memory segments used by this backend. * @@ -464,42 +473,36 @@ dsm_set_control_handle(dsm_handle h) #endif /* - * Reserve some space in the main shared memory segment for DSM segments. - */ -size_t -dsm_estimate_size(void) -{ - return 1024 * 1024 * (size_t) min_dynamic_shared_memory; -} - -/* - * Initialize space in the main shared memory segment for DSM segments. + * Reserve space in the main shared memory segment for DSM segments. */ void -dsm_shmem_init(void) +dsm_shmem_register(void) { - size_t size = dsm_estimate_size(); - bool found; + size_t size = 1024 * 1024 * (size_t) min_dynamic_shared_memory; if (size == 0) return; - dsm_main_space_begin = ShmemInitStruct("Preallocated DSM", size, &found); - if (!found) - { - FreePageManager *fpm = (FreePageManager *) dsm_main_space_begin; - size_t first_page = 0; - size_t pages; - - /* Reserve space for the FreePageManager. */ - while (first_page * FPM_PAGE_SIZE < sizeof(FreePageManager)) - ++first_page; - - /* Initialize it and give it all the rest of the space. */ - FreePageManagerInitialize(fpm, dsm_main_space_begin); - pages = (size / FPM_PAGE_SIZE) - first_page; - FreePageManagerPut(fpm, first_page, pages); - } + dsm_main_space_shmem_desc.size = size; + ShmemRegisterStruct(&dsm_main_space_shmem_desc); +} + +static void +dsm_main_space_init(void *arg) +{ + size_t size = dsm_main_space_shmem_desc.size; + FreePageManager *fpm = (FreePageManager *) dsm_main_space_begin; + size_t first_page = 0; + size_t pages; + + /* Reserve space for the FreePageManager. */ + while (first_page * FPM_PAGE_SIZE < sizeof(FreePageManager)) + ++first_page; + + /* Initialize it and give it all the rest of the space. */ + FreePageManagerInitialize(fpm, dsm_main_space_begin); + pages = (size / FPM_PAGE_SIZE) - first_page; + FreePageManagerPut(fpm, first_page, pages); } /* diff --git a/src/backend/storage/ipc/dsm_registry.c b/src/backend/storage/ipc/dsm_registry.c index 068c1577b12..60c84471221 100644 --- a/src/backend/storage/ipc/dsm_registry.c +++ b/src/backend/storage/ipc/dsm_registry.c @@ -56,6 +56,16 @@ typedef struct DSMRegistryCtxStruct static DSMRegistryCtxStruct *DSMRegistryCtx; +static void DSMRegistryCtxShmemInit(void *arg); + +static ShmemStructDesc DSMRegistryCtxShmemDesc = { + .name = "DSM Registry Data", + .size = sizeof(DSMRegistryCtxStruct), + .init_fn = DSMRegistryCtxShmemInit, + .ptr = (void **) &DSMRegistryCtx, +}; + + typedef struct NamedDSMState { dsm_handle handle; @@ -113,27 +123,17 @@ static const dshash_parameters dsh_params = { static dsa_area *dsm_registry_dsa; static dshash_table *dsm_registry_table; -Size -DSMRegistryShmemSize(void) +void +DSMRegistryShmemRegister(void) { - return MAXALIGN(sizeof(DSMRegistryCtxStruct)); + ShmemRegisterStruct(&DSMRegistryCtxShmemDesc); } -void -DSMRegistryShmemInit(void) +static void +DSMRegistryCtxShmemInit(void *arg) { - bool found; - - DSMRegistryCtx = (DSMRegistryCtxStruct *) - ShmemInitStruct("DSM Registry Data", - DSMRegistryShmemSize(), - &found); - - if (!found) - { - DSMRegistryCtx->dsah = DSA_HANDLE_INVALID; - DSMRegistryCtx->dshh = DSHASH_HANDLE_INVALID; - } + DSMRegistryCtx->dsah = DSA_HANDLE_INVALID; + DSMRegistryCtx->dshh = DSHASH_HANDLE_INVALID; } /* diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c index 405c69655f0..40ceeddae58 100644 --- a/src/backend/storage/ipc/ipci.c +++ b/src/backend/storage/ipc/ipci.c @@ -101,16 +101,11 @@ CalculateShmemSize(void) size = 100000; size = add_size(size, ShmemRegisteredSize()); - size = add_size(size, dsm_estimate_size()); - size = add_size(size, DSMRegistryShmemSize()); - /* legacy subsystems */ size = add_size(size, BufferManagerShmemSize()); size = add_size(size, LockManagerShmemSize()); size = add_size(size, PredicateLockShmemSize()); - size = add_size(size, ProcGlobalShmemSize()); size = add_size(size, XLogPrefetchShmemSize()); - size = add_size(size, VarsupShmemSize()); size = add_size(size, XLOGShmemSize()); size = add_size(size, XLogRecoveryShmemSize()); size = add_size(size, CLOGShmemSize()); @@ -120,11 +115,7 @@ CalculateShmemSize(void) size = add_size(size, BackgroundWorkerShmemSize()); size = add_size(size, MultiXactShmemSize()); size = add_size(size, LWLockShmemSize()); - size = add_size(size, ProcArrayShmemSize()); size = add_size(size, BackendStatusShmemSize()); - size = add_size(size, SharedInvalShmemSize()); - size = add_size(size, PMSignalShmemSize()); - size = add_size(size, ProcSignalShmemSize()); size = add_size(size, CheckpointerShmemSize()); size = add_size(size, AutoVacuumShmemSize()); size = add_size(size, ReplicationSlotsShmemSize()); @@ -138,7 +129,6 @@ CalculateShmemSize(void) size = add_size(size, SyncScanShmemSize()); size = add_size(size, AsyncShmemSize()); size = add_size(size, StatsShmemSize()); - size = add_size(size, WaitEventCustomShmemSize()); size = add_size(size, InjectionPointShmemSize()); size = add_size(size, SlotSyncShmemSize()); size = add_size(size, AioShmemSize()); @@ -246,11 +236,28 @@ void RegisterShmemStructs(void) { /* - * TODO: Not used in any built-in subsystems yet. In the future, most of - * the calls *ShmemInit() calls in CreateOrAttachShmemStructs(), and - * *ShmemSize() calls in CalculateShmemSize() will be replaced by calls - * into the subsystems from here. + * TODO: In the future, all the calls *ShmemInit() calls in + * CreateOrAttachShmemStructs(), and all the *ShmemSize() calls in + * CalculateShmemSize() will be replaced by calls into the subsystems from + * here. + */ + + /* + * Note: there are some inter-dependencies between these, so the order of + * some of these matter. */ + + DSMRegistryShmemRegister(); + dsm_shmem_register(); + + ProcGlobalShmemRegister(); + VarsupShmemRegister(); + ProcArrayShmemRegister(); + SharedInvalShmemRegister(); + PMSignalShmemRegister(); + ProcSignalShmemRegister(); + + WaitEventCustomShmemRegister(); } /* @@ -292,13 +299,9 @@ CreateOrAttachShmemStructs(void) ShmemInitRegistered(); } - dsm_shmem_init(); - DSMRegistryShmemInit(); - /* * Set up xlog, clog, and buffers */ - VarsupShmemInit(); XLOGShmemInit(); XLogPrefetchShmemInit(); XLogRecoveryShmemInit(); @@ -321,23 +324,13 @@ CreateOrAttachShmemStructs(void) /* * Set up process table */ - if (!IsUnderPostmaster) - InitProcGlobal(); - ProcArrayShmemInit(); BackendStatusShmemInit(); TwoPhaseShmemInit(); BackgroundWorkerShmemInit(); - /* - * Set up shared-inval messaging - */ - SharedInvalShmemInit(); - /* * Set up interprocess signaling mechanisms */ - PMSignalShmemInit(); - ProcSignalShmemInit(); CheckpointerShmemInit(); AutoVacuumShmemInit(); ReplicationSlotsShmemInit(); @@ -356,7 +349,6 @@ CreateOrAttachShmemStructs(void) SyncScanShmemInit(); AsyncShmemInit(); StatsShmemInit(); - WaitEventCustomShmemInit(); InjectionPointShmemInit(); AioShmemInit(); WaitLSNShmemInit(); diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c index 4618820b337..d7463df73ab 100644 --- a/src/backend/storage/ipc/pmsignal.c +++ b/src/backend/storage/ipc/pmsignal.c @@ -83,6 +83,15 @@ struct PMSignalData /* PMSignalState pointer is valid in both postmaster and child processes */ NON_EXEC_STATIC volatile PMSignalData *PMSignalState = NULL; +static void PMSignalShmemInit(void *); + +static ShmemStructDesc PMSignalShmemDesc = { + .name = "PMSignalState", + .size = 0, /* calculated later */ + .init_fn = PMSignalShmemInit, + .ptr = (void **) &PMSignalState, +}; + /* * Local copy of PMSignalState->num_child_flags, only valid in the * postmaster. Postmaster keeps a local copy so that it doesn't need to @@ -123,39 +132,29 @@ postmaster_death_handler(SIGNAL_ARGS) static void MarkPostmasterChildInactive(int code, Datum arg); /* - * PMSignalShmemSize - * Compute space needed for pmsignal.c's shared memory + * PMSignalShmemRegister - Register pmsignal.c's shared memory needs */ -Size -PMSignalShmemSize(void) +void +PMSignalShmemRegister(void) { Size size; - size = offsetof(PMSignalData, PMChildFlags); - size = add_size(size, mul_size(MaxLivePostmasterChildren(), - sizeof(sig_atomic_t))); + num_child_flags = MaxLivePostmasterChildren(); - return size; + size = offsetof(PMSignalData, PMChildFlags); + size = add_size(size, mul_size(num_child_flags, sizeof(sig_atomic_t))); + PMSignalShmemDesc.size = size; + ShmemRegisterStruct(&PMSignalShmemDesc); } -/* - * PMSignalShmemInit - initialize during shared-memory creation - */ -void -PMSignalShmemInit(void) +static void +PMSignalShmemInit(void *arg) { - bool found; - - PMSignalState = (PMSignalData *) - ShmemInitStruct("PMSignalState", PMSignalShmemSize(), &found); - - if (!found) - { - /* initialize all flags to zeroes */ - MemSet(unvolatize(PMSignalData *, PMSignalState), 0, PMSignalShmemSize()); - num_child_flags = MaxLivePostmasterChildren(); - PMSignalState->num_child_flags = num_child_flags; - } + /* initialize all flags to zeroes */ + Assert(PMSignalState); + MemSet(unvolatize(PMSignalData *, PMSignalState), 0, PMSignalShmemDesc.size); + Assert(num_child_flags > 0); + PMSignalState->num_child_flags = num_child_flags; } /* diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index 0f913897acc..5daf33d5323 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -103,6 +103,19 @@ typedef struct ProcArrayStruct int pgprocnos[FLEXIBLE_ARRAY_MEMBER]; } ProcArrayStruct; +static void ProcArrayShmemInit(void *arg); +static void ProcArrayShmemAttach(void *arg); + +static ProcArrayStruct *procArray; + +static ShmemStructDesc ProcArrayShmemDesc = { + .name = "Proc Array", + .size = 0, /* calculated later */ + .init_fn = ProcArrayShmemInit, + .attach_fn = ProcArrayShmemAttach, + .ptr = (void **) &procArray, +}; + /* * State for the GlobalVisTest* family of functions. Those functions can * e.g. be used to decide if a deleted row can be removed without violating @@ -269,9 +282,6 @@ typedef enum KAXCompressReason KAX_STARTUP_PROCESS_IDLE, /* startup process is about to sleep */ } KAXCompressReason; - -static ProcArrayStruct *procArray; - static PGPROC *allProcs; /* @@ -282,8 +292,25 @@ static TransactionId cachedXidIsNotInProgress = InvalidTransactionId; /* * Bookkeeping for tracking emulated transactions in recovery */ + static TransactionId *KnownAssignedXids; + +static ShmemStructDesc KnownAssignedXidsShmemDesc = { + .name = "KnownAssignedXids", + .size = 0, /* calculated later */ + .init_fn = NULL, /* no initialization needed */ + .ptr = (void **) &KnownAssignedXids, +}; + static bool *KnownAssignedXidsValid; + +static ShmemStructDesc KnownAssignedXidsValidShmemDesc = { + .name = "KnownAssignedXidsValid", + .size = 0, /* calculated later */ + .init_fn = NULL, /* no initialization needed */ + .ptr = (void **) &KnownAssignedXidsValid, +}; + static TransactionId latestObservedXid = InvalidTransactionId; /* @@ -374,18 +401,18 @@ static inline FullTransactionId FullXidRelativeTo(FullTransactionId rel, static void GlobalVisUpdateApply(ComputeXidHorizonsResult *horizons); /* - * Report shared-memory space needed by ProcArrayShmemInit + * Register the shared PGPROC array during postmaster startup. */ -Size -ProcArrayShmemSize(void) +void +ProcArrayShmemRegister(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)); + /* Register the ProcArray shared structure */ + ProcArrayShmemDesc.size = + add_size(offsetof(ProcArrayStruct, pgprocnos), + mul_size(sizeof(int), PROCARRAY_MAXPROCS)); + ShmemRegisterStruct(&ProcArrayShmemDesc); /* * During Hot Standby processing we have a data structure called @@ -405,64 +432,41 @@ ProcArrayShmemSize(void) if (EnableHotStandby) { - size = add_size(size, - mul_size(sizeof(TransactionId), - TOTAL_MAX_CACHED_SUBXIDS)); - size = add_size(size, - mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS)); + KnownAssignedXidsShmemDesc.size = + mul_size(sizeof(TransactionId), + TOTAL_MAX_CACHED_SUBXIDS); + ShmemRegisterStruct(&KnownAssignedXidsShmemDesc); + + KnownAssignedXidsValidShmemDesc.size = + mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS); + ShmemRegisterStruct(&KnownAssignedXidsValidShmemDesc); } - - return size; } /* * Initialize the shared PGPROC array during postmaster startup. */ -void -ProcArrayShmemInit(void) +static void +ProcArrayShmemInit(void *arg) { - bool found; - - /* Create or attach to the ProcArray shared structure */ - procArray = (ProcArrayStruct *) - ShmemInitStruct("Proc Array", - add_size(offsetof(ProcArrayStruct, pgprocnos), - mul_size(sizeof(int), - PROCARRAY_MAXPROCS)), - &found); - - if (!found) - { - /* - * We're the first - initialize. - */ - procArray->numProcs = 0; - procArray->maxProcs = PROCARRAY_MAXPROCS; - procArray->maxKnownAssignedXids = TOTAL_MAX_CACHED_SUBXIDS; - procArray->numKnownAssignedXids = 0; - procArray->tailKnownAssignedXids = 0; - procArray->headKnownAssignedXids = 0; - procArray->lastOverflowedXid = InvalidTransactionId; - procArray->replication_slot_xmin = InvalidTransactionId; - procArray->replication_slot_catalog_xmin = InvalidTransactionId; - TransamVariables->xactCompletionCount = 1; - } + procArray->numProcs = 0; + procArray->maxProcs = PROCARRAY_MAXPROCS; + procArray->maxKnownAssignedXids = TOTAL_MAX_CACHED_SUBXIDS; + procArray->numKnownAssignedXids = 0; + procArray->tailKnownAssignedXids = 0; + procArray->headKnownAssignedXids = 0; + procArray->lastOverflowedXid = InvalidTransactionId; + procArray->replication_slot_xmin = InvalidTransactionId; + procArray->replication_slot_catalog_xmin = InvalidTransactionId; + TransamVariables->xactCompletionCount = 1; allProcs = ProcGlobal->allProcs; +} - /* Create or attach to the KnownAssignedXids arrays too, if needed */ - if (EnableHotStandby) - { - KnownAssignedXids = (TransactionId *) - ShmemInitStruct("KnownAssignedXids", - mul_size(sizeof(TransactionId), - TOTAL_MAX_CACHED_SUBXIDS), - &found); - KnownAssignedXidsValid = (bool *) - ShmemInitStruct("KnownAssignedXidsValid", - mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS), - &found); - } +static void +ProcArrayShmemAttach(void *arg) +{ + allProcs = ProcGlobal->allProcs; } /* diff --git a/src/backend/storage/ipc/procsignal.c b/src/backend/storage/ipc/procsignal.c index 7e017c8d53b..a0c94ddd77c 100644 --- a/src/backend/storage/ipc/procsignal.c +++ b/src/backend/storage/ipc/procsignal.c @@ -105,7 +105,18 @@ struct ProcSignalHeader #define BARRIER_CLEAR_BIT(flags, type) \ ((flags) &= ~(((uint32) 1) << (uint32) (type))) +static void ProcSignalShmemInit(void *arg); + NON_EXEC_STATIC ProcSignalHeader *ProcSignal = NULL; + +static ShmemStructDesc ProcSignalShmemDesc = { + .name = "ProcSignal", + .size = 0, /* calculated later */ + .init_fn = ProcSignalShmemInit, + .ptr = (void **) &ProcSignal, +}; + + static ProcSignalSlot *MyProcSignalSlot = NULL; static bool CheckProcSignal(ProcSignalReason reason); @@ -113,51 +124,37 @@ static void CleanupProcSignalState(int status, Datum arg); static void ResetProcSignalBarrierBits(uint32 flags); /* - * ProcSignalShmemSize - * Compute space needed for ProcSignal's shared memory + * ProcSignalShmemRegister + * Register ProcSignal's shared memory needs at postmaster startup */ -Size -ProcSignalShmemSize(void) +void +ProcSignalShmemRegister(void) { Size size; size = mul_size(NumProcSignalSlots, sizeof(ProcSignalSlot)); size = add_size(size, offsetof(ProcSignalHeader, psh_slot)); - return size; + + ProcSignalShmemDesc.size = size; + ShmemRegisterStruct(&ProcSignalShmemDesc); } -/* - * ProcSignalShmemInit - * Allocate and initialize ProcSignal's shared memory - */ -void -ProcSignalShmemInit(void) +static void +ProcSignalShmemInit(void *arg) { - Size size = ProcSignalShmemSize(); - bool found; + pg_atomic_init_u64(&ProcSignal->psh_barrierGeneration, 0); - ProcSignal = (ProcSignalHeader *) - ShmemInitStruct("ProcSignal", size, &found); - - /* If we're first, initialize. */ - if (!found) + for (int i = 0; i < NumProcSignalSlots; ++i) { - int i; - - pg_atomic_init_u64(&ProcSignal->psh_barrierGeneration, 0); + ProcSignalSlot *slot = &ProcSignal->psh_slot[i]; - for (i = 0; i < NumProcSignalSlots; ++i) - { - ProcSignalSlot *slot = &ProcSignal->psh_slot[i]; - - SpinLockInit(&slot->pss_mutex); - pg_atomic_init_u32(&slot->pss_pid, 0); - slot->pss_cancel_key_len = 0; - MemSet(slot->pss_signalFlags, 0, sizeof(slot->pss_signalFlags)); - pg_atomic_init_u64(&slot->pss_barrierGeneration, PG_UINT64_MAX); - pg_atomic_init_u32(&slot->pss_barrierCheckMask, 0); - ConditionVariableInit(&slot->pss_barrierCV); - } + SpinLockInit(&slot->pss_mutex); + pg_atomic_init_u32(&slot->pss_pid, 0); + slot->pss_cancel_key_len = 0; + MemSet(slot->pss_signalFlags, 0, sizeof(slot->pss_signalFlags)); + pg_atomic_init_u64(&slot->pss_barrierGeneration, PG_UINT64_MAX); + pg_atomic_init_u32(&slot->pss_barrierCheckMask, 0); + ConditionVariableInit(&slot->pss_barrierCV); } } diff --git a/src/backend/storage/ipc/sinvaladt.c b/src/backend/storage/ipc/sinvaladt.c index a7a7cc4f0a9..b9df3b84de9 100644 --- a/src/backend/storage/ipc/sinvaladt.c +++ b/src/backend/storage/ipc/sinvaladt.c @@ -203,8 +203,17 @@ typedef struct SISeg */ #define NumProcStateSlots (MaxBackends + NUM_AUXILIARY_PROCS) +static void SharedInvalShmemInit(void *arg); + static SISeg *shmInvalBuffer; /* pointer to the shared inval buffer */ +static ShmemStructDesc SharedInvalShmemDesc = { + .name = "shmInvalBuffer", + .size = 0, /* calculated later */ + .init_fn = SharedInvalShmemInit, + .ptr = (void **) &shmInvalBuffer, +}; + static LocalTransactionId nextLocalTransactionId; @@ -212,10 +221,11 @@ static void CleanupInvalidationState(int status, Datum arg); /* - * SharedInvalShmemSize --- return shared-memory space needed + * SharedInvalShmemRegister + * Register shared memory needs for the SI message buffer */ -Size -SharedInvalShmemSize(void) +void +SharedInvalShmemRegister(void) { Size size; @@ -223,26 +233,16 @@ SharedInvalShmemSize(void) size = add_size(size, mul_size(sizeof(ProcState), NumProcStateSlots)); /* procState */ size = add_size(size, mul_size(sizeof(int), NumProcStateSlots)); /* pgprocnos */ - return size; + SharedInvalShmemDesc.size = size; + ShmemRegisterStruct(&SharedInvalShmemDesc); } -/* - * SharedInvalShmemInit - * Create and initialize the SI message buffer - */ -void -SharedInvalShmemInit(void) +static void +SharedInvalShmemInit(void *arg) { int i; - bool found; - - /* Allocate space in shared memory */ - shmInvalBuffer = (SISeg *) - ShmemInitStruct("shmInvalBuffer", SharedInvalShmemSize(), &found); - if (found) - return; - /* Clear message counters, save size of procState array, init spinlock */ + /* Clear message counters, init spinlock */ shmInvalBuffer->minMsgNum = 0; shmInvalBuffer->maxMsgNum = 0; shmInvalBuffer->nextThreshold = CLEANUP_MIN; diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index 8f5ce0e2a8a..f880aba9be9 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -69,9 +69,41 @@ PGPROC *MyProc = NULL; /* Pointers to shared-memory structures */ PROC_HDR *ProcGlobal = NULL; +static void *tmpAllProcs; +static void *tmpFastPathLockArray; NON_EXEC_STATIC PGPROC *AuxiliaryProcs = NULL; PGPROC *PreparedXactProcs = NULL; +static void ProcGlobalShmemInit(void *arg); + +static ShmemStructDesc ProcGlobalShmemDesc = { + .name = "Proc Header", + .size = sizeof(PROC_HDR), + .init_fn = ProcGlobalShmemInit, + + /* + * ProcGlobal is registered here in .ptr as usual, but it needs to be + * propagated specially in EXEC_BACKEND mode, because ProcGlobal needs to + * be accessed early at backend startup, before ShmemAttachRegistered() + * has been called. + */ + .ptr = (void **) &ProcGlobal, +}; + +static ShmemStructDesc ProcGlobalAllProcsShmemDesc = { + .name = "PGPROC structures", + .size = 0, /* calculated later */ + .ptr = (void **) &tmpAllProcs, +}; + +static ShmemStructDesc FastPathLockArrayShmemDesc = { + .name = "Fast-Path Lock Array", + .size = 0, /* calculated later */ + .ptr = (void **) &tmpFastPathLockArray, +}; + +static uint32 TotalProcs; + /* Is a deadlock check pending? */ static volatile sig_atomic_t got_deadlock_timeout; @@ -81,24 +113,6 @@ static void AuxiliaryProcKill(int code, Datum arg); static DeadLockState CheckDeadLock(void); -/* - * Report shared-memory space needed by PGPROC. - */ -static Size -PGProcShmemSize(void) -{ - Size size = 0; - Size TotalProcs = - add_size(MaxBackends, add_size(NUM_AUXILIARY_PROCS, max_prepared_xacts)); - - size = add_size(size, mul_size(TotalProcs, sizeof(PGPROC))); - size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->xids))); - size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->subxidStates))); - size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->statusFlags))); - - return size; -} - /* * Report shared-memory space needed by Fast-Path locks. */ @@ -106,8 +120,6 @@ static Size FastPathLockShmemSize(void) { Size size = 0; - Size TotalProcs = - add_size(MaxBackends, add_size(NUM_AUXILIARY_PROCS, max_prepared_xacts)); Size fpLockBitsSize, fpRelIdSize; @@ -123,25 +135,6 @@ FastPathLockShmemSize(void) return size; } -/* - * Report shared-memory space needed by InitProcGlobal. - */ -Size -ProcGlobalShmemSize(void) -{ - Size size = 0; - - /* ProcGlobal */ - size = add_size(size, sizeof(PROC_HDR)); - size = add_size(size, sizeof(slock_t)); - - size = add_size(size, PGSemaphoreShmemSize(ProcGlobalSemas())); - size = add_size(size, PGProcShmemSize()); - size = add_size(size, FastPathLockShmemSize()); - - return size; -} - /* * Report number of semaphores needed by InitProcGlobal. */ @@ -156,7 +149,50 @@ ProcGlobalSemas(void) } /* - * InitProcGlobal - + * ProcGlobalShmemRegister - + * Register shared memory needs. + * + * This is called during postmaster or standalone backend startup, and also + * during backend startup in EXEC_BACKEND mode. + */ +void +ProcGlobalShmemRegister(void) +{ + Size size = 0; + + /* + * Reserve all the PGPROC structures we'll need. There are six separate + * consumers: (1) normal backends, (2) autovacuum workers and special + * workers, (3) background workers, (4) walsenders, (5) auxiliary + * processes, and (6) prepared transactions. (For largely-historical + * reasons, we combine autovacuum and special workers into one category + * with a single freelist.) Each PGPROC structure is dedicated to exactly + * one of these purposes, and they do not move between groups. + */ + TotalProcs = + add_size(MaxBackends, add_size(NUM_AUXILIARY_PROCS, max_prepared_xacts)); + + size = add_size(size, mul_size(TotalProcs, sizeof(PGPROC))); + size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->xids))); + size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->subxidStates))); + size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->statusFlags))); + + ProcGlobalAllProcsShmemDesc.size = size; + ShmemRegisterStruct(&ProcGlobalAllProcsShmemDesc); + + FastPathLockArrayShmemDesc.size = FastPathLockShmemSize(); + ShmemRegisterStruct(&FastPathLockArrayShmemDesc); + + /* + * Register the ProcGlobal shared structure last. Its init callback + * initializes the others too. + */ + ShmemRegisterStruct(&ProcGlobalShmemDesc); +} + + +/* + * ProcGlobalShmemInit - * Initialize the global process table during postmaster or standalone * backend startup. * @@ -175,36 +211,23 @@ ProcGlobalSemas(void) * Another reason for creating semaphores here is that the semaphore * implementation typically requires us to create semaphores in the * postmaster, not in backends. - * - * Note: this is NOT called by individual backends under a postmaster, - * not even in the EXEC_BACKEND case. The ProcGlobal and AuxiliaryProcs - * pointers must be propagated specially for EXEC_BACKEND operation. */ -void -InitProcGlobal(void) +static void +ProcGlobalShmemInit(void *arg) { + char *ptr; + size_t requestSize; PGPROC *procs; int i, j; - bool found; - uint32 TotalProcs = MaxBackends + NUM_AUXILIARY_PROCS + max_prepared_xacts; /* Used for setup of per-backend fast-path slots. */ char *fpPtr, *fpEndPtr PG_USED_FOR_ASSERTS_ONLY; Size fpLockBitsSize, fpRelIdSize; - Size requestSize; - char *ptr; - - /* Create the ProcGlobal shared structure */ - ProcGlobal = (PROC_HDR *) - ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found); - Assert(!found); - /* - * Initialize the data structures. - */ + Assert(ProcGlobal); ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY; SpinLockInit(&ProcGlobal->freeProcsLock); dlist_init(&ProcGlobal->freeProcs); @@ -217,23 +240,12 @@ InitProcGlobal(void) pg_atomic_init_u32(&ProcGlobal->procArrayGroupFirst, INVALID_PROC_NUMBER); pg_atomic_init_u32(&ProcGlobal->clogGroupFirst, INVALID_PROC_NUMBER); - /* - * Create and initialize all the PGPROC structures we'll need. There are - * six separate consumers: (1) normal backends, (2) autovacuum workers and - * special workers, (3) background workers, (4) walsenders, (5) auxiliary - * processes, and (6) prepared transactions. (For largely-historical - * reasons, we combine autovacuum and special workers into one category - * with a single freelist.) Each PGPROC structure is dedicated to exactly - * one of these purposes, and they do not move between groups. - */ - requestSize = PGProcShmemSize(); - - ptr = ShmemInitStruct("PGPROC structures", - requestSize, - &found); - + Assert(tmpAllProcs); + ptr = tmpAllProcs; + requestSize = ProcGlobalAllProcsShmemDesc.size; MemSet(ptr, 0, requestSize); + /* Carve out the allProcs array from the shared memory area */ procs = (PGPROC *) ptr; ptr = ptr + TotalProcs * sizeof(PGPROC); @@ -242,7 +254,7 @@ InitProcGlobal(void) ProcGlobal->allProcCount = MaxBackends + NUM_AUXILIARY_PROCS; /* - * Allocate arrays mirroring PGPROC fields in a dense manner. See + * Carve out arrays mirroring PGPROC fields in a dense manner. See * PROC_HDR. * * XXX: It might make sense to increase padding for these arrays, given @@ -257,31 +269,25 @@ InitProcGlobal(void) ProcGlobal->statusFlags = (uint8 *) ptr; ptr = ptr + (TotalProcs * sizeof(*ProcGlobal->statusFlags)); - /* make sure wer didn't overflow */ + /* make sure we didn't overflow */ Assert((ptr > (char *) procs) && (ptr <= (char *) procs + requestSize)); /* - * Allocate arrays for fast-path locks. Those are variable-length, so + * Initialize arrays for fast-path locks. Those are variable-length, so * can't be included in PGPROC directly. We allocate a separate piece of * shared memory and then divide that between backends. */ fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64)); fpRelIdSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(Oid)); - requestSize = FastPathLockShmemSize(); - - fpPtr = ShmemInitStruct("Fast-Path Lock Array", - requestSize, - &found); - - MemSet(fpPtr, 0, requestSize); + Assert(tmpFastPathLockArray); + fpPtr = tmpFastPathLockArray; + requestSize = FastPathLockArrayShmemDesc.size; + memset(fpPtr, 0, requestSize); /* For asserts checking we did not overflow. */ fpEndPtr = fpPtr + requestSize; - /* Reserve space for semaphores. */ - PGReserveSemaphores(ProcGlobalSemas()); - for (i = 0; i < TotalProcs; i++) { PGPROC *proc = &procs[i]; diff --git a/src/backend/utils/activity/wait_event.c b/src/backend/utils/activity/wait_event.c index aca2c8fc742..93e87a68fdf 100644 --- a/src/backend/utils/activity/wait_event.c +++ b/src/backend/utils/activity/wait_event.c @@ -79,6 +79,30 @@ typedef struct WaitEventCustomEntryByName uint32 wait_event_info; } WaitEventCustomEntryByName; +static ShmemHashDesc WaitEventCustomHashByInfoDesc = +{ + .name = "WaitEventCustom hash by wait event information", + .ptr = &WaitEventCustomHashByInfo, + + .init_size = WAIT_EVENT_CUSTOM_HASH_INIT_SIZE, + .max_size = WAIT_EVENT_CUSTOM_HASH_MAX_SIZE, + .hash_info.keysize = sizeof(uint32), + .hash_info.entrysize = sizeof(WaitEventCustomEntryByInfo), + .hash_flags = HASH_ELEM | HASH_BLOBS, +}; + +static ShmemHashDesc WaitEventCustomHashByNameDesc = +{ + .name = "WaitEventCustom hash by name", + .ptr = &WaitEventCustomHashByName, + + .init_size = WAIT_EVENT_CUSTOM_HASH_INIT_SIZE, + .max_size = WAIT_EVENT_CUSTOM_HASH_MAX_SIZE, + /* key is a NULL-terminated string */ + .hash_info.keysize = sizeof(char[NAMEDATALEN]), + .hash_info.entrysize = sizeof(WaitEventCustomEntryByName), + .hash_flags = HASH_ELEM | HASH_STRINGS, +}; /* dynamic allocation counter for custom wait events */ typedef struct WaitEventCustomCounterData @@ -90,6 +114,16 @@ typedef struct WaitEventCustomCounterData /* pointer to the shared memory */ static WaitEventCustomCounterData *WaitEventCustomCounter; +static void WaitEventCustomCounterDataShmemInit(void *arg); + +static ShmemStructDesc WaitEventCustomCounterShmemDesc = +{ + .name = "WaitEventCustomCounterData", + .size = sizeof(WaitEventCustomCounterData), + .init_fn = WaitEventCustomCounterDataShmemInit, + .ptr = (void **) &WaitEventCustomCounter, +}; + /* first event ID of custom wait events */ #define WAIT_EVENT_CUSTOM_INITIAL_ID 1 @@ -97,60 +131,22 @@ static uint32 WaitEventCustomNew(uint32 classId, const char *wait_event_name); static const char *GetWaitEventCustomIdentifier(uint32 wait_event_info); /* - * Return the space for dynamic shared hash tables and dynamic allocation counter. + * Register shmem space for dynamic shared hash and dynamic allocation counter. */ -Size -WaitEventCustomShmemSize(void) +void +WaitEventCustomShmemRegister(void) { - Size sz; - - sz = MAXALIGN(sizeof(WaitEventCustomCounterData)); - sz = add_size(sz, hash_estimate_size(WAIT_EVENT_CUSTOM_HASH_MAX_SIZE, - sizeof(WaitEventCustomEntryByInfo))); - sz = add_size(sz, hash_estimate_size(WAIT_EVENT_CUSTOM_HASH_MAX_SIZE, - sizeof(WaitEventCustomEntryByName))); - return sz; + ShmemRegisterStruct(&WaitEventCustomCounterShmemDesc); + ShmemRegisterHash(&WaitEventCustomHashByInfoDesc); + ShmemRegisterHash(&WaitEventCustomHashByNameDesc); } -/* - * Allocate shmem space for dynamic shared hash and dynamic allocation counter. - */ -void -WaitEventCustomShmemInit(void) +static void +WaitEventCustomCounterDataShmemInit(void *arg) { - bool found; - HASHCTL info; - - WaitEventCustomCounter = (WaitEventCustomCounterData *) - ShmemInitStruct("WaitEventCustomCounterData", - sizeof(WaitEventCustomCounterData), &found); - - if (!found) - { - /* initialize the allocation counter and its spinlock. */ - WaitEventCustomCounter->nextId = WAIT_EVENT_CUSTOM_INITIAL_ID; - SpinLockInit(&WaitEventCustomCounter->mutex); - } - - /* initialize or attach the hash tables to store custom wait events */ - info.keysize = sizeof(uint32); - info.entrysize = sizeof(WaitEventCustomEntryByInfo); - WaitEventCustomHashByInfo = - ShmemInitHash("WaitEventCustom hash by wait event information", - WAIT_EVENT_CUSTOM_HASH_INIT_SIZE, - WAIT_EVENT_CUSTOM_HASH_MAX_SIZE, - &info, - HASH_ELEM | HASH_BLOBS); - - /* key is a NULL-terminated string */ - info.keysize = sizeof(char[NAMEDATALEN]); - info.entrysize = sizeof(WaitEventCustomEntryByName); - WaitEventCustomHashByName = - ShmemInitHash("WaitEventCustom hash by name", - WAIT_EVENT_CUSTOM_HASH_INIT_SIZE, - WAIT_EVENT_CUSTOM_HASH_MAX_SIZE, - &info, - HASH_ELEM | HASH_STRINGS); + /* initialize the allocation counter and its spinlock. */ + WaitEventCustomCounter->nextId = WAIT_EVENT_CUSTOM_INITIAL_ID; + SpinLockInit(&WaitEventCustomCounter->mutex); } /* diff --git a/src/include/access/transam.h b/src/include/access/transam.h index 6fa91bfcdc0..2dfc8b0f85f 100644 --- a/src/include/access/transam.h +++ b/src/include/access/transam.h @@ -345,8 +345,7 @@ extern TransactionId TransactionIdLatest(TransactionId mainxid, extern XLogRecPtr TransactionIdGetCommitLSN(TransactionId xid); /* in transam/varsup.c */ -extern Size VarsupShmemSize(void); -extern void VarsupShmemInit(void); +extern void VarsupShmemRegister(void); extern FullTransactionId GetNewTransactionId(bool isSubXact); extern void AdvanceNextFullTransactionIdPastXid(TransactionId xid); extern FullTransactionId ReadNextFullTransactionId(void); diff --git a/src/include/storage/dsm.h b/src/include/storage/dsm.h index 407657df3ff..061bf725f88 100644 --- a/src/include/storage/dsm.h +++ b/src/include/storage/dsm.h @@ -26,8 +26,7 @@ extern void dsm_postmaster_startup(PGShmemHeader *); extern void dsm_backend_shutdown(void); extern void dsm_detach_all(void); -extern size_t dsm_estimate_size(void); -extern void dsm_shmem_init(void); +extern void dsm_shmem_register(void); #ifdef EXEC_BACKEND extern void dsm_set_control_handle(dsm_handle h); diff --git a/src/include/storage/dsm_registry.h b/src/include/storage/dsm_registry.h index 506fae2c9ca..9a1b4d982af 100644 --- a/src/include/storage/dsm_registry.h +++ b/src/include/storage/dsm_registry.h @@ -22,7 +22,6 @@ extern dsa_area *GetNamedDSA(const char *name, bool *found); extern dshash_table *GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found); -extern Size DSMRegistryShmemSize(void); -extern void DSMRegistryShmemInit(void); +extern void DSMRegistryShmemRegister(void); #endif /* DSM_REGISTRY_H */ diff --git a/src/include/storage/pmsignal.h b/src/include/storage/pmsignal.h index 206fb78f8a5..7cdc4852334 100644 --- a/src/include/storage/pmsignal.h +++ b/src/include/storage/pmsignal.h @@ -66,8 +66,7 @@ extern PGDLLIMPORT volatile PMSignalData *PMSignalState; /* * prototypes for functions in pmsignal.c */ -extern Size PMSignalShmemSize(void); -extern void PMSignalShmemInit(void); +extern void PMSignalShmemRegister(void); extern void SendPostmasterSignal(PMSignalReason reason); extern bool CheckPostmasterSignal(PMSignalReason reason); extern void SetQuitSignalReason(QuitSignalReason reason); diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h index 3f89450c216..1d1e0881af2 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -552,7 +552,7 @@ extern PGDLLIMPORT PGPROC *AuxiliaryProcs; * Function Prototypes */ extern int ProcGlobalSemas(void); -extern Size ProcGlobalShmemSize(void); +extern void ProcGlobalShmemRegister(void); extern void InitProcGlobal(void); extern void InitProcess(void); extern void InitProcessPhase2(void); diff --git a/src/include/storage/procarray.h b/src/include/storage/procarray.h index c5ab1574fe3..572516c4e21 100644 --- a/src/include/storage/procarray.h +++ b/src/include/storage/procarray.h @@ -20,8 +20,7 @@ #include "utils/snapshot.h" -extern Size ProcArrayShmemSize(void); -extern void ProcArrayShmemInit(void); +extern void ProcArrayShmemRegister(void); extern void ProcArrayAdd(PGPROC *proc); extern void ProcArrayRemove(PGPROC *proc, TransactionId latestXid); diff --git a/src/include/storage/procsignal.h b/src/include/storage/procsignal.h index 348fba53a93..d2344b1cbb3 100644 --- a/src/include/storage/procsignal.h +++ b/src/include/storage/procsignal.h @@ -63,8 +63,7 @@ typedef enum /* * prototypes for functions in procsignal.c */ -extern Size ProcSignalShmemSize(void); -extern void ProcSignalShmemInit(void); +extern void ProcSignalShmemRegister(void); extern void ProcSignalInit(const uint8 *cancel_key, int cancel_key_len); extern int SendProcSignal(pid_t pid, ProcSignalReason reason, diff --git a/src/include/storage/sinvaladt.h b/src/include/storage/sinvaladt.h index a1694500a85..4edba2936e6 100644 --- a/src/include/storage/sinvaladt.h +++ b/src/include/storage/sinvaladt.h @@ -28,8 +28,7 @@ /* * prototypes for functions in sinvaladt.c */ -extern Size SharedInvalShmemSize(void); -extern void SharedInvalShmemInit(void); +extern void SharedInvalShmemRegister(void); extern void SharedInvalBackendInit(bool sendOnly); extern void SIInsertDataEntries(const SharedInvalidationMessage *data, int n); diff --git a/src/include/utils/wait_event.h b/src/include/utils/wait_event.h index 34c27cc3dc3..86fc8637f5e 100644 --- a/src/include/utils/wait_event.h +++ b/src/include/utils/wait_event.h @@ -42,8 +42,7 @@ extern PGDLLIMPORT uint32 *my_wait_event_info; extern uint32 WaitEventExtensionNew(const char *wait_event_name); extern uint32 WaitEventInjectionPointNew(const char *wait_event_name); -extern void WaitEventCustomShmemInit(void); -extern Size WaitEventCustomShmemSize(void); +extern void WaitEventCustomShmemRegister(void); extern char **GetWaitEventCustomNames(uint32 classId, int *nwaitevents); /* ---------- -- 2.47.3