From 61715b45ca2d0206b8eda692e757ea2b32ddd96b Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Wed, 1 Apr 2026 18:21:02 +0300
Subject: [PATCH 05/14] 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/launch_backend.c |  2 ++
 src/backend/postmaster/postmaster.c     |  5 +++++
 src/backend/storage/ipc/ipci.c          | 21 +++++++++++++++++
 src/backend/tcop/postgres.c             |  3 +++
 src/include/storage/ipc.h               |  1 +
 src/include/storage/subsystemlist.h     | 23 +++++++++++++++++++
 src/include/storage/subsystems.h        | 30 +++++++++++++++++++++++++
 src/tools/pginclude/headerscheck        |  1 +
 9 files changed, 88 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 26d3717c2cb..22672ffec1a 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -362,6 +362,8 @@ BootstrapModeMain(int argc, char *argv[], bool check_only)
 	SetProcessingMode(BootstrapProcessing);
 	IgnoreSystemIndexes = true;
 
+	RegisterBuiltinShmemCallbacks();
+
 	InitializeMaxBackends();
 
 	/*
diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index 75423104be8..7b81200d3c2 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -663,6 +663,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
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 01b064d62ea..3fd6a5b9a9b 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -921,6 +921,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 5333e528e1f..67ab4a78192 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -51,6 +51,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"
@@ -251,6 +252,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.
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index af7cc86d80a..6c6c2243d9e 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -4137,6 +4137,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 */
diff --git a/src/tools/pginclude/headerscheck b/src/tools/pginclude/headerscheck
index 14c466cc237..24f7416185e 100755
--- a/src/tools/pginclude/headerscheck
+++ b/src/tools/pginclude/headerscheck
@@ -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
-- 
2.47.3

