From e8d3509ad2c4dff5159ba1dc2b64a21820aec7e7 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Wed, 1 Apr 2026 20:07:30 +0300 Subject: [PATCH v9 12/16] 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/twophase.c | 2 +- src/backend/access/transam/varsup.c | 36 ++--- src/backend/storage/ipc/dsm.c | 65 +++++---- src/backend/storage/ipc/dsm_registry.c | 39 +++--- src/backend/storage/ipc/ipci.c | 28 ---- src/backend/storage/ipc/pmsignal.c | 57 ++++---- src/backend/storage/ipc/procarray.c | 119 ++++++++-------- src/backend/storage/ipc/procsignal.c | 66 ++++----- src/backend/storage/ipc/sinvaladt.c | 40 +++--- src/backend/storage/lmgr/proc.c | 183 +++++++++++++------------ src/include/access/transam.h | 2 - src/include/storage/dsm.h | 3 - src/include/storage/dsm_registry.h | 2 - src/include/storage/pmsignal.h | 2 - src/include/storage/proc.h | 2 - src/include/storage/procarray.h | 2 - src/include/storage/procsignal.h | 3 - src/include/storage/sinvaladt.h | 2 - src/include/storage/subsystemlist.h | 18 +++ 19 files changed, 333 insertions(+), 338 deletions(-) diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c index d468c9774b3..ab1cbd67bac 100644 --- a/src/backend/access/transam/twophase.c +++ b/src/backend/access/transam/twophase.c @@ -282,7 +282,7 @@ TwoPhaseShmemInit(void) gxacts[i].next = TwoPhaseState->freeGXacts; TwoPhaseState->freeGXacts = &gxacts[i]; - /* associate it with a PGPROC assigned by InitProcGlobal */ + /* associate it with a PGPROC assigned by ProcGlobalShmemInit */ gxacts[i].pgprocno = GetNumberFromPGProc(&PreparedXactProcs[i]); } } diff --git a/src/backend/access/transam/varsup.c b/src/backend/access/transam/varsup.c index 1441a051773..2ea8d088c0e 100644 --- a/src/backend/access/transam/varsup.c +++ b/src/backend/access/transam/varsup.c @@ -23,6 +23,7 @@ #include "postmaster/autovacuum.h" #include "storage/pmsignal.h" #include "storage/proc.h" +#include "storage/subsystems.h" #include "utils/lsyscache.h" #include "utils/syscache.h" @@ -30,35 +31,28 @@ /* Number of OIDs to prefetch (preallocate) per XLOG write */ #define VAR_OID_PREFETCH 8192 +static void VarsupShmemRequest(void *arg); + /* pointer to variables struct in shared memory */ TransamVariablesData *TransamVariables = NULL; +const ShmemCallbacks VarsupShmemCallbacks = { + .request_fn = VarsupShmemRequest, +}; /* - * Initialization of shared memory for TransamVariables. + * Request shared memory for TransamVariables. */ -Size -VarsupShmemSize(void) -{ - return sizeof(TransamVariablesData); -} - -void -VarsupShmemInit(void) +static void +VarsupShmemRequest(void *arg) { - bool found; + static ShmemStructDesc TransamVariablesShmemDesc; - /* Initialize our shared state struct */ - TransamVariables = ShmemInitStruct("TransamVariables", - sizeof(TransamVariablesData), - &found); - if (!IsUnderPostmaster) - { - Assert(!found); - memset(TransamVariables, 0, sizeof(TransamVariablesData)); - } - else - Assert(found); + ShmemRequestStruct(&TransamVariablesShmemDesc, + .name = "TransamVariables", + .size = sizeof(TransamVariablesData), + .ptr = (void **) &TransamVariables, + ); } /* diff --git a/src/backend/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c index 6a5b16392f7..923593d140d 100644 --- a/src/backend/storage/ipc/dsm.c +++ b/src/backend/storage/ipc/dsm.c @@ -43,6 +43,7 @@ #include "storage/lwlock.h" #include "storage/pg_shmem.h" #include "storage/shmem.h" +#include "storage/subsystems.h" #include "utils/freepage.h" #include "utils/memutils.h" #include "utils/resowner.h" @@ -110,6 +111,14 @@ 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_request(void *arg); +static void dsm_main_space_init(void *arg); + +const ShmemCallbacks dsm_shmem_callbacks = { + .request_fn = dsm_main_space_request, + .init_fn = dsm_main_space_init, +}; + /* * List of dynamic shared memory segments used by this backend. * @@ -463,43 +472,45 @@ dsm_set_control_handle(dsm_handle h) } #endif +static ShmemStructDesc dsm_main_space_shmem_desc; + /* - * Reserve some space in the main shared memory segment for DSM segments. + * Reserve space in the main shared memory segment for DSM segments. */ -size_t -dsm_estimate_size(void) +static void +dsm_main_space_request(void *arg) { - return 1024 * 1024 * (size_t) min_dynamic_shared_memory; + size_t size = 1024 * 1024 * (size_t) min_dynamic_shared_memory; + + if (size == 0) + return; + + ShmemRequestStruct(&dsm_main_space_shmem_desc, + .name = "Preallocated DSM", + .size = size, + .ptr = &dsm_main_space_begin, + ); } -/* - * Initialize space in the main shared memory segment for DSM segments. - */ -void -dsm_shmem_init(void) +static void +dsm_main_space_init(void *arg) { - size_t size = dsm_estimate_size(); - bool found; + size_t size = dsm_main_space_shmem_desc.size; + FreePageManager *fpm = (FreePageManager *) dsm_main_space_begin; + size_t first_page = 0; + size_t pages; 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); - } + /* 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 9bfcd616827..3e9c0ba2947 100644 --- a/src/backend/storage/ipc/dsm_registry.c +++ b/src/backend/storage/ipc/dsm_registry.c @@ -45,6 +45,7 @@ #include "storage/dsm_registry.h" #include "storage/lwlock.h" #include "storage/shmem.h" +#include "storage/subsystems.h" #include "utils/builtins.h" #include "utils/memutils.h" #include "utils/tuplestore.h" @@ -57,6 +58,14 @@ typedef struct DSMRegistryCtxStruct static DSMRegistryCtxStruct *DSMRegistryCtx; +static void DSMRegistryShmemRequest(void *arg); +static void DSMRegistryShmemInit(void *arg); + +const ShmemCallbacks DSMRegistryShmemCallbacks = { + .request_fn = DSMRegistryShmemRequest, + .init_fn = DSMRegistryShmemInit, +}; + typedef struct NamedDSMState { dsm_handle handle; @@ -114,27 +123,23 @@ static const dshash_parameters dsh_params = { static dsa_area *dsm_registry_dsa; static dshash_table *dsm_registry_table; -Size -DSMRegistryShmemSize(void) +static void +DSMRegistryShmemRequest(void *arg) { - return MAXALIGN(sizeof(DSMRegistryCtxStruct)); + static ShmemStructDesc DSMRegistryCtxShmemDesc; + + ShmemRequestStruct(&DSMRegistryCtxShmemDesc, + .name = "DSM Registry Data", + .size = sizeof(DSMRegistryCtxStruct), + .ptr = (void **) &DSMRegistryCtx, + ); } -void -DSMRegistryShmemInit(void) +static void +DSMRegistryShmemInit(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 b90717c1f58..0dc3a2146ec 100644 --- a/src/backend/storage/ipc/ipci.c +++ b/src/backend/storage/ipc/ipci.c @@ -20,7 +20,6 @@ #include "access/nbtree.h" #include "access/subtrans.h" #include "access/syncscan.h" -#include "access/transam.h" #include "access/twophase.h" #include "access/xlogprefetcher.h" #include "access/xlogrecovery.h" @@ -41,18 +40,13 @@ #include "storage/aio_subsys.h" #include "storage/bufmgr.h" #include "storage/dsm.h" -#include "storage/dsm_registry.h" #include "storage/ipc.h" #include "storage/pg_shmem.h" #include "storage/pmsignal.h" #include "storage/predicate.h" #include "storage/proc.h" -#include "storage/procarray.h" -#include "storage/procsignal.h" -#include "storage/sinvaladt.h" #include "storage/subsystems.h" #include "utils/guc.h" -#include "utils/wait_event.h" /* GUCs */ int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE; @@ -102,14 +96,10 @@ CalculateShmemSize(void) size = add_size(size, ShmemGetRequestedSize()); /* legacy subsystems */ - size = add_size(size, dsm_estimate_size()); - size = add_size(size, DSMRegistryShmemSize()); 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()); @@ -119,11 +109,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()); @@ -285,13 +271,9 @@ RegisterBuiltinShmemCallbacks(void) static void CreateOrAttachShmemStructs(void) { - dsm_shmem_init(); - DSMRegistryShmemInit(); - /* * Set up xlog, clog, and buffers */ - VarsupShmemInit(); XLOGShmemInit(); XLogPrefetchShmemInit(); XLogRecoveryShmemInit(); @@ -314,23 +296,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(); diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c index 4618820b337..00588664885 100644 --- a/src/backend/storage/ipc/pmsignal.c +++ b/src/backend/storage/ipc/pmsignal.c @@ -27,6 +27,7 @@ #include "storage/ipc.h" #include "storage/pmsignal.h" #include "storage/shmem.h" +#include "storage/subsystems.h" #include "utils/memutils.h" @@ -83,6 +84,14 @@ struct PMSignalData /* PMSignalState pointer is valid in both postmaster and child processes */ NON_EXEC_STATIC volatile PMSignalData *PMSignalState = NULL; +static void PMSignalShmemRequest(void *); +static void PMSignalShmemInit(void *); + +const ShmemCallbacks PMSignalShmemCallbacks = { + .request_fn = PMSignalShmemRequest, + .init_fn = PMSignalShmemInit, +}; + /* * 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,31 @@ postmaster_death_handler(SIGNAL_ARGS) static void MarkPostmasterChildInactive(int code, Datum arg); /* - * PMSignalShmemSize - * Compute space needed for pmsignal.c's shared memory + * PMSignalShmemRequest - Register pmsignal.c's shared memory needs */ -Size -PMSignalShmemSize(void) +static void +PMSignalShmemRequest(void *arg) { - Size size; - - size = offsetof(PMSignalData, PMChildFlags); - size = add_size(size, mul_size(MaxLivePostmasterChildren(), - sizeof(sig_atomic_t))); - - return size; + static ShmemStructDesc PMSignalShmemDesc; + size_t size; + + num_child_flags = MaxLivePostmasterChildren(); + + size = add_size(offsetof(PMSignalData, PMChildFlags), + mul_size(num_child_flags, sizeof(sig_atomic_t))); + ShmemRequestStruct(&PMSignalShmemDesc, + .name = "PMSignalState", + .size = size, + .ptr = (void **) &PMSignalState, + ); } -/* - * 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; - } + Assert(PMSignalState); + 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 cc207cb56e3..c8e09f4f0b6 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -61,6 +61,7 @@ #include "storage/proc.h" #include "storage/procarray.h" #include "storage/procsignal.h" +#include "storage/subsystems.h" #include "utils/acl.h" #include "utils/builtins.h" #include "utils/injection_point.h" @@ -103,6 +104,20 @@ typedef struct ProcArrayStruct int pgprocnos[FLEXIBLE_ARRAY_MEMBER]; } ProcArrayStruct; +static void ProcArrayShmemRequest(void *arg); +static void ProcArrayShmemInit(void *arg); +static void ProcArrayShmemAttach(void *arg); + +static ProcArrayStruct *procArray; + +const struct ShmemCallbacks ProcArrayShmemCallbacks = { + .request_fn = ProcArrayShmemRequest, + .init_fn = ProcArrayShmemInit, + .attach_fn = ProcArrayShmemAttach, +}; + +static ShmemStructDesc ProcArrayShmemDesc; + /* * 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 +284,6 @@ typedef enum KAXCompressReason KAX_STARTUP_PROCESS_IDLE, /* startup process is about to sleep */ } KAXCompressReason; - -static ProcArrayStruct *procArray; - static PGPROC *allProcs; /* @@ -282,8 +294,15 @@ static TransactionId cachedXidIsNotInProgress = InvalidTransactionId; /* * Bookkeeping for tracking emulated transactions in recovery */ + static TransactionId *KnownAssignedXids; + +static ShmemStructDesc KnownAssignedXidsShmemDesc; + static bool *KnownAssignedXidsValid; + +static ShmemStructDesc KnownAssignedXidsValidShmemDesc; + static TransactionId latestObservedXid = InvalidTransactionId; /* @@ -374,19 +393,13 @@ 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) +static void +ProcArrayShmemRequest(void *arg) { - 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)); - /* * During Hot Standby processing we have a data structure called * KnownAssignedXids, created in shared memory. Local data structures are @@ -405,64 +418,52 @@ 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)); + ShmemRequestStruct(&KnownAssignedXidsShmemDesc, + .name = "KnownAssignedXids", + .size = mul_size(sizeof(TransactionId), TOTAL_MAX_CACHED_SUBXIDS), + .ptr = (void **) &KnownAssignedXids, + ); + + ShmemRequestStruct(&KnownAssignedXidsValidShmemDesc, + .name = "KnownAssignedXidsValid", + .size = mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS), + .ptr = (void **) &KnownAssignedXidsValid, + ); } - return size; + /* Register the ProcArray shared structure */ + ShmemRequestStruct(&ProcArrayShmemDesc, + .name = "Proc Array", + .size = add_size(offsetof(ProcArrayStruct, pgprocnos), + mul_size(sizeof(int), PROCARRAY_MAXPROCS)), + .ptr = (void **) &procArray, + ); } /* * 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..7c79727e308 100644 --- a/src/backend/storage/ipc/procsignal.c +++ b/src/backend/storage/ipc/procsignal.c @@ -32,6 +32,7 @@ #include "storage/shmem.h" #include "storage/sinval.h" #include "storage/smgr.h" +#include "storage/subsystems.h" #include "tcop/tcopprot.h" #include "utils/memutils.h" #include "utils/wait_event.h" @@ -105,7 +106,16 @@ struct ProcSignalHeader #define BARRIER_CLEAR_BIT(flags, type) \ ((flags) &= ~(((uint32) 1) << (uint32) (type))) +static void ProcSignalShmemRequest(void *arg); +static void ProcSignalShmemInit(void *arg); + +const ShmemCallbacks ProcSignalShmemCallbacks = { + .request_fn = ProcSignalShmemRequest, + .init_fn = ProcSignalShmemInit, +}; + NON_EXEC_STATIC ProcSignalHeader *ProcSignal = NULL; + static ProcSignalSlot *MyProcSignalSlot = NULL; static bool CheckProcSignal(ProcSignalReason reason); @@ -113,51 +123,41 @@ static void CleanupProcSignalState(int status, Datum arg); static void ResetProcSignalBarrierBits(uint32 flags); /* - * ProcSignalShmemSize - * Compute space needed for ProcSignal's shared memory + * ProcSignalShmemRequest + * Register ProcSignal's shared memory needs at postmaster startup */ -Size -ProcSignalShmemSize(void) +static void +ProcSignalShmemRequest(void *arg) { + static ShmemStructDesc ProcSignalShmemDesc; Size size; size = mul_size(NumProcSignalSlots, sizeof(ProcSignalSlot)); size = add_size(size, offsetof(ProcSignalHeader, psh_slot)); - return size; + + ShmemRequestStruct(&ProcSignalShmemDesc, + .name = "ProcSignal", + .size = size, + .ptr = (void **) &ProcSignal, + ); } -/* - * 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..34860d474bc 100644 --- a/src/backend/storage/ipc/sinvaladt.c +++ b/src/backend/storage/ipc/sinvaladt.c @@ -25,6 +25,7 @@ #include "storage/shmem.h" #include "storage/sinvaladt.h" #include "storage/spin.h" +#include "storage/subsystems.h" /* * Conceptually, the shared cache invalidation messages are stored in an @@ -205,6 +206,14 @@ typedef struct SISeg static SISeg *shmInvalBuffer; /* pointer to the shared inval buffer */ +static void SharedInvalShmemRequest(void *arg); +static void SharedInvalShmemInit(void *arg); + +const ShmemCallbacks SharedInvalShmemCallbacks = { + .request_fn = SharedInvalShmemRequest, + .init_fn = SharedInvalShmemInit, +}; + static LocalTransactionId nextLocalTransactionId; @@ -212,37 +221,32 @@ static void CleanupInvalidationState(int status, Datum arg); /* - * SharedInvalShmemSize --- return shared-memory space needed + * SharedInvalShmemRequest + * Register shared memory needs for the SI message buffer */ -Size -SharedInvalShmemSize(void) +static void +SharedInvalShmemRequest(void *arg) { + static ShmemStructDesc SharedInvalShmemDesc; Size size; size = offsetof(SISeg, procState); size = add_size(size, mul_size(sizeof(ProcState), NumProcStateSlots)); /* procState */ size = add_size(size, mul_size(sizeof(int), NumProcStateSlots)); /* pgprocnos */ - return size; + ShmemRequestStruct(&SharedInvalShmemDesc, + .name = "shmInvalBuffer", + .size = size, + .ptr = (void **) &shmInvalBuffer, + ); } -/* - * 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 9b880a6af65..b5532208364 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -52,6 +52,7 @@ #include "storage/procsignal.h" #include "storage/spin.h" #include "storage/standby.h" +#include "storage/subsystems.h" #include "utils/timeout.h" #include "utils/timestamp.h" #include "utils/wait_event.h" @@ -70,9 +71,25 @@ 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 ProcGlobalShmemRequest(void *arg); +static void ProcGlobalShmemInit(void *arg); + +const ShmemCallbacks ProcGlobalShmemCallbacks = { + .request_fn = ProcGlobalShmemRequest, + .init_fn = ProcGlobalShmemInit, +}; + +static ShmemStructDesc ProcGlobalShmemDesc; +static ShmemStructDesc ProcGlobalAllProcsShmemDesc; +static ShmemStructDesc FastPathLockArrayShmemDesc; + +static uint32 TotalProcs; + /* Is a deadlock check pending? */ static volatile sig_atomic_t got_deadlock_timeout; @@ -82,24 +99,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. */ @@ -107,8 +106,6 @@ static Size FastPathLockShmemSize(void) { Size size = 0; - Size TotalProcs = - add_size(MaxBackends, add_size(NUM_AUXILIARY_PROCS, max_prepared_xacts)); Size fpLockBitsSize, fpRelIdSize; @@ -128,26 +125,7 @@ FastPathLockShmemSize(void) } /* - * 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. + * Report number of semaphores needed by ProcGlobalShmemInit. */ int ProcGlobalSemas(void) @@ -160,7 +138,63 @@ ProcGlobalSemas(void) } /* - * InitProcGlobal - + * ProcGlobalShmemRequest + * Register shared memory needs. + * + * This is called during postmaster or standalone backend startup, and also + * during backend startup in EXEC_BACKEND mode. + */ +static void +ProcGlobalShmemRequest(void *arg) +{ + Size size; + + /* + * 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 = 0; + 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))); + ShmemRequestStruct(&ProcGlobalAllProcsShmemDesc, + .name = "PGPROC structures", + .size = size, + .ptr = (void **) &tmpAllProcs, + ); + + ShmemRequestStruct(&FastPathLockArrayShmemDesc, + .name = "Fast-Path Lock Array", + .size = IsUnderPostmaster ? SHMEM_ATTACH_UNKNOWN_SIZE : FastPathLockShmemSize(), + .ptr = (void **) &tmpFastPathLockArray, + ); + + ShmemRequestStruct(&ProcGlobalShmemDesc, + .name = "Proc Header", + .size = sizeof(PROC_HDR), + + /* + * 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 ShmemAttachRequested() has + * been called. + */ + .ptr = (void **) &ProcGlobal, + ); +} + + +/* + * ProcGlobalShmemInit - * Initialize the global process table during postmaster or standalone * backend startup. * @@ -179,36 +213,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); @@ -221,23 +242,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); @@ -246,7 +256,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 @@ -261,24 +271,21 @@ 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; @@ -405,7 +412,7 @@ InitProcess(void) /* * Decide which list should supply our PGPROC. This logic must match the - * way the freelists were constructed in InitProcGlobal(). + * way the freelists were constructed in ProcGlobalShmemInit(). */ if (AmAutoVacuumWorkerProcess() || AmSpecialWorkerProcess()) procgloballist = &ProcGlobal->autovacFreeProcs; @@ -460,7 +467,7 @@ InitProcess(void) /* * Initialize all fields of MyProc, except for those previously - * initialized by InitProcGlobal. + * initialized by ProcGlobalShmemInit. */ dlist_node_init(&MyProc->freeProcsLink); MyProc->waitStatus = PROC_WAIT_STATUS_OK; @@ -593,7 +600,7 @@ InitProcessPhase2(void) * This is called by bgwriter and similar processes so that they will have a * MyProc value that's real enough to let them wait for LWLocks. The PGPROC * and sema that are assigned are one of the extra ones created during - * InitProcGlobal. + * ProcGlobalShmemInit. * * Auxiliary processes are presently not expected to wait for real (lockmgr) * locks, so we need not set up the deadlock checker. They are never added @@ -662,7 +669,7 @@ InitAuxiliaryProcess(void) /* * Initialize all fields of MyProc, except for those previously - * initialized by InitProcGlobal. + * initialized by ProcGlobalShmemInit. */ dlist_node_init(&MyProc->freeProcsLink); MyProc->waitStatus = PROC_WAIT_STATUS_OK; diff --git a/src/include/access/transam.h b/src/include/access/transam.h index 6fa91bfcdc0..55a4ab26b34 100644 --- a/src/include/access/transam.h +++ b/src/include/access/transam.h @@ -345,8 +345,6 @@ extern TransactionId TransactionIdLatest(TransactionId mainxid, extern XLogRecPtr TransactionIdGetCommitLSN(TransactionId xid); /* in transam/varsup.c */ -extern Size VarsupShmemSize(void); -extern void VarsupShmemInit(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..1bde71b4406 100644 --- a/src/include/storage/dsm.h +++ b/src/include/storage/dsm.h @@ -26,9 +26,6 @@ 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); - #ifdef EXEC_BACKEND extern void dsm_set_control_handle(dsm_handle h); #endif diff --git a/src/include/storage/dsm_registry.h b/src/include/storage/dsm_registry.h index 506fae2c9ca..a2269c89f01 100644 --- a/src/include/storage/dsm_registry.h +++ b/src/include/storage/dsm_registry.h @@ -22,7 +22,5 @@ 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); #endif /* DSM_REGISTRY_H */ diff --git a/src/include/storage/pmsignal.h b/src/include/storage/pmsignal.h index 206fb78f8a5..001e6eea61c 100644 --- a/src/include/storage/pmsignal.h +++ b/src/include/storage/pmsignal.h @@ -66,8 +66,6 @@ extern PGDLLIMPORT volatile PMSignalData *PMSignalState; /* * prototypes for functions in pmsignal.c */ -extern Size PMSignalShmemSize(void); -extern void PMSignalShmemInit(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 1dad125706e..60732ccb33a 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -551,8 +551,6 @@ extern PGDLLIMPORT PGPROC *AuxiliaryProcs; * Function Prototypes */ extern int ProcGlobalSemas(void); -extern Size ProcGlobalShmemSize(void); -extern void InitProcGlobal(void); extern void InitProcess(void); extern void InitProcessPhase2(void); extern void InitAuxiliaryProcess(void); diff --git a/src/include/storage/procarray.h b/src/include/storage/procarray.h index abdf021e66e..d718a5b542f 100644 --- a/src/include/storage/procarray.h +++ b/src/include/storage/procarray.h @@ -19,8 +19,6 @@ #include "utils/snapshot.h" -extern Size ProcArrayShmemSize(void); -extern void ProcArrayShmemInit(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..031897015f4 100644 --- a/src/include/storage/procsignal.h +++ b/src/include/storage/procsignal.h @@ -63,9 +63,6 @@ typedef enum /* * prototypes for functions in procsignal.c */ -extern Size ProcSignalShmemSize(void); -extern void ProcSignalShmemInit(void); - extern void ProcSignalInit(const uint8 *cancel_key, int cancel_key_len); extern int SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber); diff --git a/src/include/storage/sinvaladt.h b/src/include/storage/sinvaladt.h index 122dbcdf19f..208ea9d051e 100644 --- a/src/include/storage/sinvaladt.h +++ b/src/include/storage/sinvaladt.h @@ -27,8 +27,6 @@ /* * prototypes for functions in sinvaladt.c */ -extern Size SharedInvalShmemSize(void); -extern void SharedInvalShmemInit(void); extern void SharedInvalBackendInit(bool sendOnly); extern void SIInsertDataEntries(const SharedInvalidationMessage *data, int n); diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h index 7dfbd03d6e5..5c11b2b3499 100644 --- a/src/include/storage/subsystemlist.h +++ b/src/include/storage/subsystemlist.h @@ -20,5 +20,23 @@ * of these matter. */ +PG_SHMEM_SUBSYSTEM(dsm_shmem_callbacks) +PG_SHMEM_SUBSYSTEM(DSMRegistryShmemCallbacks) + +/* xlog, clog, and buffers */ +PG_SHMEM_SUBSYSTEM(VarsupShmemCallbacks) + +/* process table */ +PG_SHMEM_SUBSYSTEM(ProcGlobalShmemCallbacks) +PG_SHMEM_SUBSYSTEM(ProcArrayShmemCallbacks) + +/* shared-inval messaging */ +PG_SHMEM_SUBSYSTEM(SharedInvalShmemCallbacks) + +/* interprocess signaling mechanisms */ +PG_SHMEM_SUBSYSTEM(PMSignalShmemCallbacks) +PG_SHMEM_SUBSYSTEM(ProcSignalShmemCallbacks) + +/* other modules that need some shared memory space */ PG_SHMEM_SUBSYSTEM(WaitEventCustomShmemCallbacks) PG_SHMEM_SUBSYSTEM(InjectionPointShmemCallbacks) -- 2.47.3