]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Introduce a registry of built-in shmem subsystems
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Sun, 5 Apr 2026 23:12:55 +0000 (02:12 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Sun, 5 Apr 2026 23:12:55 +0000 (02:12 +0300)
To add a new built-in subsystem, add it to subsystemslist.h. That
hooks up its shmem 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.

Reviewed-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Reviewed-by: Matthias van de Meent <boekewurm+postgres@gmail.com>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Discussion: https://www.postgresql.org/message-id/CAExHW5vM1bneLYfg0wGeAa=52UiJ3z4vKd3AJ72X8Fw6k3KKrg@mail.gmail.com

src/backend/bootstrap/bootstrap.c
src/backend/postmaster/launch_backend.c
src/backend/postmaster/postmaster.c
src/backend/storage/ipc/ipci.c
src/backend/storage/ipc/shmem.c
src/backend/tcop/postgres.c
src/include/storage/ipc.h
src/include/storage/subsystemlist.h [new file with mode: 0644]
src/include/storage/subsystems.h [new file with mode: 0644]
src/tools/pginclude/headerscheck

index 3766d8231acefdbebf90a17d2e21eef9f34d19e5..63378ab3d8c2c47589dfaa19af6492da0999e2a2 100644 (file)
@@ -363,6 +363,8 @@ BootstrapModeMain(int argc, char *argv[], bool check_only)
        SetProcessingMode(BootstrapProcessing);
        IgnoreSystemIndexes = true;
 
+       RegisterBuiltinShmemCallbacks();
+
        InitializeMaxBackends();
 
        /*
index 0973010b7dc333a9c9e22d5b821c3f8c71e95b71..ed0f4f2d2343678ad8d10f3389c485a40a173d90 100644 (file)
@@ -664,6 +664,8 @@ SubPostmasterMain(int argc, char *argv[])
         */
        LocalProcessControlFile(false);
 
+       RegisterBuiltinShmemCallbacks();
+
        /*
         * Reload any libraries that were preloaded by the postmaster.  Since we
         * exec'd this process, those libraries didn't come along with us; but we
index 7a8ee19bdafd5e7d6a9c53896dd12b4ddf6a1d4b..a2de96a9a8eae97ed5f5bf597cde2d90c651149b 100644 (file)
@@ -922,6 +922,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
         */
index 24422a80ab3df43101e2eb3a2a83feb1771ff1e8..e4a6a52f12db7d096e035862812cdd952b50d972 100644 (file)
@@ -52,6 +52,7 @@
 #include "storage/procsignal.h"
 #include "storage/shmem_internal.h"
 #include "storage/sinvaladt.h"
+#include "storage/subsystems.h"
 #include "utils/guc.h"
 #include "utils/injection_point.h"
 #include "utils/wait_event.h"
@@ -252,6 +253,26 @@ 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)
+{
+       /*
+        * Call RegisterShmemCallbacks(...) on each subsystem listed in
+        * subsystemslist.h
+        */
+#define PG_SHMEM_SUBSYSTEM(subsystem_callbacks) \
+       RegisterShmemCallbacks(&(subsystem_callbacks));
+
+#include "storage/subsystemlist.h"
+
+#undef PG_SHMEM_SUBSYSTEM
+}
+
 /*
  * Initialize various subsystems, setting up their data structures in
  * shared memory.
index e1724b0358d11686b5f368c7b9da974808b783bc..51d974523d41d3474240eb467a648334e253725a 100644 (file)
  *                     );
  *     }
  *
- * Register the callbacks by calling RegisterShmemCallbacks(&MyShmemCallbacks)
- * in the extension's _PG_init() function.
+ * In builtin PostgreSQL code, add the callbacks to the list in
+ * src/include/storage/subsystemlist.h.  In an add-in module, you can register
+ * the callbacks by calling RegisterShmemCallbacks(&MyShmemCallbacks) in the
+ * extension's _PG_init() function.
  *
  * Lifecycle
  * ---------
index 93851269e436c0dfcf695bbb91d0a5cfc64283f0..6a9ff3ad225dcbd477ea814780434decdaf69452 100644 (file)
@@ -4138,6 +4138,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
         */
index da32787ab51875fef865ac73e3ef8a9381f766fd..b205b00e7a1ab954407f780390878b198b048cb0 100644 (file)
@@ -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 (file)
index 0000000..ed43c90
--- /dev/null
@@ -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 (file)
index 0000000..38b735b
--- /dev/null
@@ -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 */
index 14c466cc237a55fcd700d416227439397e60ac00..24f7416185e30c83c17a3ef093f3d7cb630bcbae 100755 (executable)
@@ -131,6 +131,7 @@ do
        test "$f" = src/include/postmaster/proctypelist.h && continue
        test "$f" = src/include/regex/regerrs.h && continue
        test "$f" = src/include/storage/lwlocklist.h && continue
+       test "$f" = src/include/storage/subsystemlist.h && continue
        test "$f" = src/include/tcop/cmdtaglist.h && continue
        test "$f" = src/interfaces/ecpg/preproc/c_kwlist.h && continue
        test "$f" = src/interfaces/ecpg/preproc/ecpg_kwlist.h && continue