From 77dbf3df8c56cc760fa2d4661f35413b39b8ddb8 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Sat, 21 Mar 2026 13:42:11 +0200 Subject: [PATCH v8 09/16] Introduce registry of built-in subsystems To add a new built-in subsystem, add it to subsystemslist.h. That hooks up its callbacks so that they get called at the right times during postmaster startup. For now this is unused, but will replace the current SubsystemShmemSize() and SubsystemShmemInit() calls in the next commits. --- src/backend/bootstrap/bootstrap.c | 2 ++ src/backend/postmaster/postmaster.c | 5 +++++ src/backend/storage/ipc/ipci.c | 19 ++++++++++++++++++ src/backend/tcop/postgres.c | 3 +++ src/include/storage/ipc.h | 1 + src/include/storage/subsystemlist.h | 23 ++++++++++++++++++++++ src/include/storage/subsystems.h | 30 +++++++++++++++++++++++++++++ 7 files changed, 83 insertions(+) create mode 100644 src/include/storage/subsystemlist.h create mode 100644 src/include/storage/subsystems.h diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c index 86fe86354f5..69e90fef4f9 100644 --- a/src/backend/bootstrap/bootstrap.c +++ b/src/backend/bootstrap/bootstrap.c @@ -359,6 +359,8 @@ BootstrapModeMain(int argc, char *argv[], bool check_only) SetProcessingMode(BootstrapProcessing); IgnoreSystemIndexes = true; + RegisterBuiltinShmemCallbacks(); + InitializeMaxBackends(); /* diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index e81ef248bf1..ae9e1a94b03 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -929,6 +929,11 @@ PostmasterMain(int argc, char *argv[]) */ ApplyLauncherRegister(); + /* + * Register the shared memory needs of all core subsystems. + */ + RegisterBuiltinShmemCallbacks(); + /* * process any libraries that should be preloaded at postmaster start */ diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c index 493ddd7f12f..a2ef290b5b3 100644 --- a/src/backend/storage/ipc/ipci.c +++ b/src/backend/storage/ipc/ipci.c @@ -50,6 +50,7 @@ #include "storage/procarray.h" #include "storage/procsignal.h" #include "storage/sinvaladt.h" +#include "storage/subsystems.h" #include "utils/guc.h" #include "utils/injection_point.h" #include "utils/wait_event.h" @@ -253,6 +254,24 @@ CreateSharedMemoryAndSemaphores(void) shmem_startup_hook(); } +/* + * Early initialization of various subsystems, giving them a chance to + * register their shared memory needs before the shared memory segment is + * allocated. + */ +void +RegisterBuiltinShmemCallbacks(void) +{ + const ShmemCallbacks *builtin_subsystems[] = { +#define PG_SHMEM_SUBSYSTEM(subsystem_callbacks) &subsystem_callbacks, +#include "storage/subsystemlist.h" +#undef PG_SHMEM_SUBSYSTEM + }; + + for (int i = 0; i < lengthof(builtin_subsystems); i++) + RegisterShmemCallbacks(builtin_subsystems[i]); +} + /* * Initialize various subsystems, setting up their data structures in * shared memory. diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 278d2f20376..8320e478d08 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -4146,6 +4146,9 @@ PostgresSingleUserMain(int argc, char *argv[], /* read control file (error checking and contains config ) */ LocalProcessControlFile(false); + /* Register the shared memory needs of all core subsystems. */ + RegisterBuiltinShmemCallbacks(); + /* * process any libraries that should be preloaded at postmaster start */ diff --git a/src/include/storage/ipc.h b/src/include/storage/ipc.h index da32787ab51..b205b00e7a1 100644 --- a/src/include/storage/ipc.h +++ b/src/include/storage/ipc.h @@ -77,6 +77,7 @@ extern void check_on_shmem_exit_lists_are_empty(void); /* ipci.c */ extern PGDLLIMPORT shmem_startup_hook_type shmem_startup_hook; +extern void RegisterBuiltinShmemCallbacks(void); extern Size CalculateShmemSize(void); extern void CreateSharedMemoryAndSemaphores(void); #ifdef EXEC_BACKEND diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h new file mode 100644 index 00000000000..ed43c90bcc3 --- /dev/null +++ b/src/include/storage/subsystemlist.h @@ -0,0 +1,23 @@ +/*--------------------------------------------------------------------------- + * subsystemlist.h + * + * List of initialization callbacks of built-in subsystems. This is kept in + * its own source file for possible use by automatic tools. + * PG_SHMEM_SUBSYSTEM is defined in the callers depending on how the list is + * used. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/storage/subsystemlist.h + *--------------------------------------------------------------------------- + */ + +/* there is deliberately not an #ifndef SUBSYSTEMLIST_H here */ + +/* + * Note: there are some inter-dependencies between these, so the order of some + * of these matter. + */ + +/* TODO: empty for now */ diff --git a/src/include/storage/subsystems.h b/src/include/storage/subsystems.h new file mode 100644 index 00000000000..38b735bec67 --- /dev/null +++ b/src/include/storage/subsystems.h @@ -0,0 +1,30 @@ +/*------------------------------------------------------------------------- + * + * subsystems.h + * Provide extern declarations for all the built-in subsystem callbacks + * + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/storage/subsystems.h + * + *------------------------------------------------------------------------- + */ +#ifndef SUBSYSTEMS_H +#define SUBSYSTEMS_H + +#include "storage/shmem.h" + +/* + * Extern declarations of all the built-in subsystem callbacks + * + * The actual list is in subsystemlist.h, so that the same list can be used + * for other purposes. + */ +#define PG_SHMEM_SUBSYSTEM(callbacks) \ + extern const ShmemCallbacks callbacks; +#include "storage/subsystemlist.h" +#undef PG_SHMEM_SUBSYSTEM + +#endif /* SUBSYSTEMS_H */ -- 2.47.3