public inbox for [email protected]  
help / color / mirror / Atom feed
From: Heikki Linnakangas <[email protected]>
To: Robert Haas <[email protected]>
Cc: Ashutosh Bapat <[email protected]>
Cc: Andres Freund <[email protected]>
Cc: pgsql-hackers <[email protected]>
Cc: [email protected]
Subject: Re: Better shared data structure management and resizable shared data structures
Date: Fri, 27 Mar 2026 02:51:22 +0200
Message-ID: <[email protected]> (raw)
In-Reply-To: <CA+TgmoYqx1+CeQHFf2d=VXecx9xz2xTM0_cP8oeXzakS89C6MQ@mail.gmail.com>
References: <CAExHW5vM1bneLYfg0wGeAa=52UiJ3z4vKd3AJ72X8Fw6k3KKrg@mail.gmail.com>
	<CAExHW5so6VSxBC-1V=35229Z1+dw5vhw8HxHg9ry7UzceKcXzA@mail.gmail.com>
	<[email protected]>
	<CA+TgmoaoSha9KnSdrA7DDMYD9+Uor4OPEaQ88bbEC3QuVE5Mcg@mail.gmail.com>
	<[email protected]>
	<CA+TgmoboxAaS6MCRf__LRhf8Ji3B0O9_=d7KzRS12QhOuocnTQ@mail.gmail.com>
	<[email protected]>
	<CA+Tgmob7E3CGDoEBB3mwfk+v1CgcOG8yL0VHje2gy9Ts=e1Zxw@mail.gmail.com>
	<[email protected]>
	<CA+TgmobqwCHp1w6xkTJmZiq4C8Pr0O3tNUvSFZcAgYLkvF45bg@mail.gmail.com>
	<[email protected]>
	<CA+Tgmob8ot7qam7a650nx_4jyEoFLs8s_kPMc1+EB7-dr56=WA@mail.gmail.com>
	<[email protected]>
	<CA+TgmoauqYXm8iA3FGRAVKxYShUxWBiS_MSLmQpTrmO7wNHamw@mail.gmail.com>
	<[email protected]>
	<CA+TgmoYqx1+CeQHFf2d=VXecx9xz2xTM0_cP8oeXzakS89C6MQ@mail.gmail.com>

On 25/03/2026 20:37, Robert Haas wrote:
> On Sat, Mar 21, 2026 at 8:14 PM Heikki Linnakangas <[email protected]> wrote:>> Shmem callbacks
>> ---------------
>>
>> I separated the request/init/fn callbacks from the structs. There's now
>> a concept of "shmem callbacks", which you register in _PG_init(). For
>> example:
>>
>> static void pgss_shmem_request(void *arg);
>> static void pgss_shmem_init(void *arg);
>>
>> static const ShmemCallbacks pgss_shmem_callbacks = {
>>       .request_fn = pgss_shmem_request,
>>       .init_fn = pgss_shmem_init,
>>       .attach_fn = NULL, /* no special attach actions needed */
>> };
> 
> What's the advantage of coupling the functions together this way, vs.
> just registering each callback individually?

One reason is to support allocations after postmaster startup. The 
RegisterShmemCallbacks() call ties together all the resources requested 
by the request_fn callback, with the the init_fn or attach_fn callbacks 
that will later initialize/attach them. The init_fn/attach_fn callbacks 
are called only after *all* the resources requested by the request_fn 
callback have been initialized, and it holds a lock while doing all that.

If the callbacks were registered separately, shmem.c wouldn't know when 
to call the init_fn/attach_fn. There's no problem during postmaster or 
backend startup, because we run all init_fn or attach_fn callbacks in 
the whole system, after requesting all the resources, but after startup, 
you must only call the callbacks related to the newly-requested resources.

Aside from that after-startup allocation issue, though, IMO the 
ShmemCallbacks struct makes it more clear that the callbacks are meant 
to work together on the same resources.

One way to think of this is that all the resources requested by the 
request_fn callback are implicitly part of the same "subsystem", and 
need to be initialized/attached to together. We discussed that before, 
and I still wonder if we should make that concept of a subsystem more 
explicit. If we just renamed ShmemCallbacks to ShmemSubsystem, and give 
each subsystem a name, it'd look like this:

static void pgss_shmem_request(void *arg);
static void pgss_shmem_init(void *arg);

static const ShmemSubsystem pgss_shmem_subsystem = {
     .name = "pg_stat_statements"
     .request_fn = pgss_shmem_request,
     .init_fn = pgss_shmem_init,
     .attach_fn = NULL, /* no special attach actions needed */
};

static void
pgss_shmem_request(void *arg)
{
     ShmemRequestStruct(&pgssSharedStateDesc, &(ShmemRequestStructOpts) {
          /*
           * name is optional in this design, subsystem's name is used if
           * not given
           */
         .name = "pg_stat_statements",
         .size = sizeof(pgssSharedState),
         .ptr = (void **) &pgss,
     });
}

static void
pgss_shmem_init(void *arg)
{
     /* initialize contents of pgss */
     ...
}

void
_PG_init(void)
{
     RegisterShmemSubsystem(&pgss_shmem_subsystem);
}



Thinking how this might work without such a struct, registering the 
callbacks separately, here's an alternative design:

static void pgss_shmem_request(void *arg);
static void pgss_shmem_init(void *arg);

static void
pgss_shmem_request(void *arg)
{
     ShmemRequestStruct(&pgssSharedStateDesc, &(ShmemRequestStructOpts) {
         .name = "pg_stat_statements",
         .size = sizeof(pgssSharedState),
         .ptr = (void **) &pgss,
     });

     ShmemRegisterInitCallback(&pgss_shmem_init);
     /* no attach callback needed, but for illustration: */
     ShmemRegisterInitCallback(&pgss_shmem_attach);
}

static void
pgss_shmem_init(void *arg)
{
     /* initialize contents of pgss */
     ...
}

void
_PG_init(void)
{
     ShmemRegisterRequestCallback(&pgss_shmem_request);
}

In this design, the ShmemRegisterRequestCallback() call still ties 
together all the related resources. All the resources requested in the 
request-callback are initialized together, and the fact that the 
init/attach callbacks are registered within the request callback 
associates them with the resources. This feels a little Rube 
Goldbergian, with one callback registering more callbacks, but would 
also work.

> Also, I don't understand what "arg" is supposed to be doing. It
> doesn't seem to be getting used for anything.

It's an opaque pointer that's passed through to the callbacks. Some 
callers might want to pass extra data to the callback. None of the 
current callers use it, so maybe it's not needed, but it seemed like 
good future-proofing.

>> Shmem requests
>> --------------
>>
>> To register a shmem area, you call ShmemRequestStruct() or
>> ShmmeRequestHash() from the request callback function. For example:
>>
>> static void
>> pgss_shmem_request(void *arg)
>> {
>>       static ShmemHashDesc pgssSharedHashDesc = {
>>          .name = "pg_stat_statements hash",
>>           .ptr = &pgss_hash,
>>           .hash_info.keysize = sizeof(pgssHashKey),
>>           .hash_info.entrysize = sizeof(pgssEntry),
>>           .hash_flags = HASH_ELEM | HASH_BLOBS,
>>       };
>>       static ShmemStructDesc pgssSharedStateDesc = {
>>           .name = "pg_stat_statements",
>>           .size = sizeof(pgssSharedState),
>>           .ptr = (void **) &pgss,
>>       };
>>
>>       pgssSharedHashDesc.init_size = pgss_max;
>>       pgssSharedHashDesc.max_size = pgss_max;
>>       ShmemRequestHash(&pgssSharedHashDesc);
>>       ShmemRequestStruct(&pgssSharedStateDesc);
>> }
>>
>> Initialization happens in the init callback, which is called after all
>> the pointers (pgss, pgss_hash) have been set.
> 
> I think this is not bad. I suppose it lets you get a complete list of
> all of the descriptors someplace. It seems to avoid double-computing
> the request size, or any possibility of drift between the requested
> size and the allocated size. Having to create the struct and then set
> the size afterward in some cases is a tiny bit awkward-looking, but
> it's not awful. I do wonder if some coding pattern might creep in
> where people create the struct and register it and then try to change
> the size, or other fields, afterward. I'm tempted to propose making
> the struct non-static and having the registration functions copy it,
> so that there can be absolutely no question of getting away with such
> antisocial behavior.

Here's another version, where that now looks like this:

static void
pgss_shmem_request(void *arg)
{
     static ShmemHashDesc pgssSharedHashDesc;
     static ShmemStructDesc pgssSharedStateDesc;

     ShmemRequestHash(&pgssSharedHashDesc, &(ShmemRequestHashOpts) {
         .name = "pg_stat_statements hash",
         .ptr = &pgss_hash,
         .init_size = pgss_max,
         .max_size = pgss_max,
         .hash_info.keysize = sizeof(pgssHashKey),
         .hash_info.entrysize = sizeof(pgssEntry),
         .hash_flags = HASH_ELEM | HASH_BLOBS,
     });
     ShmemRequestStruct(&pgssSharedStateDesc, &(ShmemRequestStructOpts) {
         .name = "pg_stat_statements",
         .size = sizeof(pgssSharedState),
         .ptr = (void **) &pgss,
     });
}

Notable differences to that since last version:

I separated the backend-private "handle" and the options structs. So 
ShmemRequestStruct/Hash now takes two arguments:

The first argument is a pointer to the "descriptor", which is a 
backend-private handle for the shared memory area. It's currently not 
very interesting for plain structs because you can't really do anything 
with it, but in the future the handle could be used to resize the area 
for example. Also, one of the later patches in this patch set refactors 
SLRUs to be requested in this fashion too. For SLRUs the handle is 
already useful; SLRUs have always had a backend-private "SlruCtl" handle 
like that.

The second argument is an "options" struct, which is really just 
syntactic sugar to pass multiple arguments, some of which can be left 
empty. The options are now copied, so the struct can be short-lived. 
Alternatively, the arguments could be passed as normal function 
arguments like you suggested earlier. But I quite like this style, 
especially with hash tables and SLRUs which take more options and 
already used structs to pass the options.

There's one annoying problem with that syntax though: pgindent doesn't 
like it and gives errors like this:

Failure in contrib/pg_stat_statements/pg_stat_statements.c: Error@508: 
Unbalanced parens
Warning@516: Extra )
Error@517: Unbalanced parens
Warning@521: Extra )

That would be nice to fix in pgindent in any case but I haven't looked 
into it yet.

Another idea is to use a macro to hide that from pgindent, which would 
make the calls little less verbose anyway:

#define ShmemRequestStruct(desc, ...) ShmemRequestStructWithOpts(desc, 
&(ShmemRequestStructOpts) { __VA_ARGS__ })

Then the call would be simply:

     ShmemRequestStruct(&pgssSharedStateDesc,
         .name = "pg_stat_statements",
         .size = sizeof(pgssSharedState),
         .ptr = (void **) &pgss,
     );


New version attached. I also pushed this to 
https://github.com/hlinnaka/postgres/tree/shmem-init-refactor-8.

- Heikki


Attachments:

  [text/x-patch] v8-0001-Test-pg_stat_statements-across-crash-restart.patch (2.6K, 2-v8-0001-Test-pg_stat_statements-across-crash-restart.patch)
  download | inline diff:
From 4b906f22bcef3165086eec04e38459dd6a549353 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Fri, 27 Mar 2026 01:45:16 +0200
Subject: [PATCH v8 01/16] Test pg_stat_statements across crash restart

Add 'pg_stat_statements' to the crash restart test, to test that
shared memory and LWLock initialization works across crash restart in
a library listed in shared_preload_libraries.

Reviewed-by: Ashutosh Bapat <[email protected]>
Discussion: https://www.postgresql.org/message-id/CAExHW5vM1bneLYfg0wGeAa=52UiJ3z4vKd3AJ72X8Fw6k3KKrg@mail.gmail.com
---
 src/test/recovery/t/013_crash_restart.pl | 33 +++++++++++++++++++++---
 1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/src/test/recovery/t/013_crash_restart.pl b/src/test/recovery/t/013_crash_restart.pl
index 20d648ad6af..56afb1aa6eb 100644
--- a/src/test/recovery/t/013_crash_restart.pl
+++ b/src/test/recovery/t/013_crash_restart.pl
@@ -21,14 +21,32 @@ my $psql_timeout = IPC::Run::timer($PostgreSQL::Test::Utils::timeout_default);
 
 my $node = PostgreSQL::Test::Cluster->new('primary');
 $node->init(allows_streaming => 1);
+
+# Enable pg_stat_statements to test restart of shared_preload_libraries.
+$node->append_conf(
+	'postgresql.conf',
+	qq{shared_preload_libraries = 'pg_stat_statements'
+pg_stat_statements.max = 50000
+compute_query_id = 'regress'
+});
+
 $node->start();
 
 # by default PostgreSQL::Test::Cluster doesn't restart after a crash
 $node->safe_psql(
-	'postgres',
-	q[ALTER SYSTEM SET restart_after_crash = 1;
-				   ALTER SYSTEM SET log_connections = receipt;
-				   SELECT pg_reload_conf();]);
+	'postgres', q[
+		ALTER SYSTEM SET restart_after_crash = 1;
+		ALTER SYSTEM SET log_connections = receipt;
+		SELECT pg_reload_conf();
+	]);
+
+# Remember the time that pg_stat_statements was reset. We'll use it later to
+# verify that it gets re-initialized after crash.
+my $stats_reset = $node->safe_psql(
+	'postgres', q[
+		CREATE EXTENSION pg_stat_statements;
+		SELECT stats_reset FROM pg_stat_statements_info;
+	]);
 
 # Run psql, keeping session alive, so we have an alive backend to kill.
 my ($killme_stdin, $killme_stdout, $killme_stderr) = ('', '', '');
@@ -141,6 +159,13 @@ $killme->run();
 ($monitor_stdin, $monitor_stdout, $monitor_stderr) = ('', '', '');
 $monitor->run();
 
+# Verify that pg_stat_statements, loaded via shared_preload_libraries,
+# was re-initialized at the crash.
+my $stats_reset_after = $node->safe_psql('postgres',
+	q[SELECT stats_reset FROM pg_stat_statements_info]);
+cmp_ok($stats_reset, 'ne', $stats_reset_after,
+	"pg_stat_statements was reset by restart");
+
 
 # Acquire pid of new backend
 $killme_stdin .= q[
-- 
2.47.3



  [text/x-patch] v8-0002-refactor-Move-ShmemInitHash-to-separate-file.patch (7.3K, 3-v8-0002-refactor-Move-ShmemInitHash-to-separate-file.patch)
  download | inline diff:
From 5bdc4db83f5fec8a65f101d0c0aa0d8f4ce1a32d Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Sat, 21 Mar 2026 13:07:28 +0200
Subject: [PATCH v8 02/16] refactor: Move ShmemInitHash to separate file

In preparation for next commits
---
 src/backend/storage/ipc/Makefile     |  1 +
 src/backend/storage/ipc/meson.build  |  1 +
 src/backend/storage/ipc/shmem.c      | 66 ---------------------
 src/backend/storage/ipc/shmem_hash.c | 85 ++++++++++++++++++++++++++++
 4 files changed, 87 insertions(+), 66 deletions(-)
 create mode 100644 src/backend/storage/ipc/shmem_hash.c

diff --git a/src/backend/storage/ipc/Makefile b/src/backend/storage/ipc/Makefile
index 9a07f6e1d92..f71653bbe48 100644
--- a/src/backend/storage/ipc/Makefile
+++ b/src/backend/storage/ipc/Makefile
@@ -22,6 +22,7 @@ OBJS = \
 	shm_mq.o \
 	shm_toc.o \
 	shmem.o \
+	shmem_hash.o \
 	signalfuncs.o \
 	sinval.o \
 	sinvaladt.o \
diff --git a/src/backend/storage/ipc/meson.build b/src/backend/storage/ipc/meson.build
index 9c1ca954d9d..b8c31e29967 100644
--- a/src/backend/storage/ipc/meson.build
+++ b/src/backend/storage/ipc/meson.build
@@ -14,6 +14,7 @@ backend_sources += files(
   'shm_mq.c',
   'shm_toc.c',
   'shmem.c',
+  'shmem_hash.c',
   'signalfuncs.c',
   'sinval.c',
   'sinvaladt.c',
diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index 3cc38450949..c86d691dcfb 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -294,72 +294,6 @@ ShmemAddrIsValid(const void *addr)
 	return (addr >= ShmemBase) && (addr < ShmemEnd);
 }
 
-/*
- * ShmemInitHash -- Create and initialize, or attach to, a
- *		shared memory hash table.
- *
- * We assume caller is doing some kind of synchronization
- * so that two processes don't try to create/initialize the same
- * table at once.  (In practice, all creations are done in the postmaster
- * process; child processes should always be attaching to existing tables.)
- *
- * max_size is the estimated maximum number of hashtable entries.  This is
- * not a hard limit, but the access efficiency will degrade if it is
- * exceeded substantially (since it's used to compute directory size and
- * the hash table buckets will get overfull).
- *
- * init_size is the number of hashtable entries to preallocate.  For a table
- * whose maximum size is certain, this should be equal to max_size; that
- * ensures that no run-time out-of-shared-memory failures can occur.
- *
- * *infoP and hash_flags must specify at least the entry sizes and key
- * comparison semantics (see hash_create()).  Flag bits and values specific
- * to shared-memory hash tables are added here, except that callers may
- * choose to specify HASH_PARTITION and/or HASH_FIXED_SIZE.
- *
- * Note: before Postgres 9.0, this function returned NULL for some failure
- * cases.  Now, it always throws error instead, so callers need not check
- * for NULL.
- */
-HTAB *
-ShmemInitHash(const char *name,		/* table string name for shmem index */
-			  int64 init_size,	/* initial table size */
-			  int64 max_size,	/* max size of the table */
-			  HASHCTL *infoP,	/* info about key and bucket size */
-			  int hash_flags)	/* info about infoP */
-{
-	bool		found;
-	void	   *location;
-
-	/*
-	 * Hash tables allocated in shared memory have a fixed directory; it can't
-	 * grow or other backends wouldn't be able to find it. So, make sure we
-	 * make it big enough to start with.
-	 *
-	 * The shared memory allocator must be specified too.
-	 */
-	infoP->dsize = infoP->max_dsize = hash_select_dirsize(max_size);
-	infoP->alloc = ShmemAllocNoError;
-	hash_flags |= HASH_SHARED_MEM | HASH_ALLOC | HASH_DIRSIZE;
-
-	/* look it up in the shmem index */
-	location = ShmemInitStruct(name,
-							   hash_get_shared_size(infoP, hash_flags),
-							   &found);
-
-	/*
-	 * if it already exists, attach to it rather than allocate and initialize
-	 * new space
-	 */
-	if (found)
-		hash_flags |= HASH_ATTACH;
-
-	/* Pass location of hashtable header to hash_create */
-	infoP->hctl = (HASHHDR *) location;
-
-	return hash_create(name, init_size, infoP, hash_flags);
-}
-
 /*
  * ShmemInitStruct -- Create/attach to a structure in shared memory.
  *
diff --git a/src/backend/storage/ipc/shmem_hash.c b/src/backend/storage/ipc/shmem_hash.c
new file mode 100644
index 00000000000..b0c8d5939a0
--- /dev/null
+++ b/src/backend/storage/ipc/shmem_hash.c
@@ -0,0 +1,85 @@
+/*-------------------------------------------------------------------------
+ *
+ * shmem_hash.c
+ *	  XXX
+ *
+ * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ *	  src/backend/storage/ipc/shmem_hash.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "storage/shmem.h"
+
+
+/*
+ * ShmemInitHash -- Create and initialize, or attach to, a
+ *		shared memory hash table.
+ *
+ * We assume caller is doing some kind of synchronization
+ * so that two processes don't try to create/initialize the same
+ * table at once.  (In practice, all creations are done in the postmaster
+ * process; child processes should always be attaching to existing tables.)
+ *
+ * max_size is the estimated maximum number of hashtable entries.  This is
+ * not a hard limit, but the access efficiency will degrade if it is
+ * exceeded substantially (since it's used to compute directory size and
+ * the hash table buckets will get overfull).
+ *
+ * init_size is the number of hashtable entries to preallocate.  For a table
+ * whose maximum size is certain, this should be equal to max_size; that
+ * ensures that no run-time out-of-shared-memory failures can occur.
+ *
+ * *infoP and hash_flags must specify at least the entry sizes and key
+ * comparison semantics (see hash_create()).  Flag bits and values specific
+ * to shared-memory hash tables are added here, except that callers may
+ * choose to specify HASH_PARTITION and/or HASH_FIXED_SIZE.
+ *
+ * Note: before Postgres 9.0, this function returned NULL for some failure
+ * cases.  Now, it always throws error instead, so callers need not check
+ * for NULL.
+ */
+HTAB *
+ShmemInitHash(const char *name,		/* table string name for shmem index */
+			  int64 init_size,	/* initial table size */
+			  int64 max_size,	/* max size of the table */
+			  HASHCTL *infoP,	/* info about key and bucket size */
+			  int hash_flags)	/* info about infoP */
+{
+	bool		found;
+	void	   *location;
+
+	/*
+	 * Hash tables allocated in shared memory have a fixed directory; it can't
+	 * grow or other backends wouldn't be able to find it. So, make sure we
+	 * make it big enough to start with.
+	 *
+	 * The shared memory allocator must be specified too.
+	 */
+	infoP->dsize = infoP->max_dsize = hash_select_dirsize(max_size);
+	infoP->alloc = ShmemAllocNoError;
+	hash_flags |= HASH_SHARED_MEM | HASH_ALLOC | HASH_DIRSIZE;
+
+	/* look it up in the shmem index */
+	location = ShmemInitStruct(name,
+							   hash_get_shared_size(infoP, hash_flags),
+							   &found);
+
+	/*
+	 * if it already exists, attach to it rather than allocate and initialize
+	 * new space
+	 */
+	if (found)
+		hash_flags |= HASH_ATTACH;
+
+	/* Pass location of hashtable header to hash_create */
+	infoP->hctl = (HASHHDR *) location;
+
+	return hash_create(name, init_size, infoP, hash_flags);
+}
-- 
2.47.3



  [text/x-patch] v8-0003-refactor-predicate.c-inline-SerialInit-to-the-cal.patch (3.6K, 4-v8-0003-refactor-predicate.c-inline-SerialInit-to-the-cal.patch)
  download | inline diff:
From 110197f7e862ee7fd88573c968d4881e1fa3f111 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Thu, 19 Mar 2026 17:21:30 +0200
Subject: [PATCH v8 03/16] refactor predicate.c: inline SerialInit to the
 caller

The ShmemInit function is very complicated currently. These
refactorings move it in a direction that is more natural with the new
shmem callbacks.
---
 src/backend/storage/lmgr/predicate.c | 73 +++++++++++-----------------
 1 file changed, 29 insertions(+), 44 deletions(-)

diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c
index edabbf4ca31..27682f416e7 100644
--- a/src/backend/storage/lmgr/predicate.c
+++ b/src/backend/storage/lmgr/predicate.c
@@ -444,7 +444,6 @@ static void FlagSxactUnsafe(SERIALIZABLEXACT *sxact);
 
 static bool SerialPagePrecedesLogically(int64 page1, int64 page2);
 static int	serial_errdetail_for_io_error(const void *opaque_data);
-static void SerialInit(void);
 static void SerialAdd(TransactionId xid, SerCommitSeqNo minConflictCommitSeqNo);
 static SerCommitSeqNo SerialGetMinConflictCommitSeqNo(TransactionId xid);
 static void SerialSetActiveSerXmin(TransactionId xid);
@@ -809,48 +808,6 @@ SerialPagePrecedesLogicallyUnitTests(void)
 }
 #endif
 
-/*
- * Initialize for the tracking of old serializable committed xids.
- */
-static void
-SerialInit(void)
-{
-	bool		found;
-
-	/*
-	 * Set up SLRU management of the pg_serial data.
-	 */
-	SerialSlruCtl->PagePrecedes = SerialPagePrecedesLogically;
-	SerialSlruCtl->errdetail_for_io_error = serial_errdetail_for_io_error;
-	SimpleLruInit(SerialSlruCtl, "serializable",
-				  serializable_buffers, 0, "pg_serial",
-				  LWTRANCHE_SERIAL_BUFFER, LWTRANCHE_SERIAL_SLRU,
-				  SYNC_HANDLER_NONE, false);
-#ifdef USE_ASSERT_CHECKING
-	SerialPagePrecedesLogicallyUnitTests();
-#endif
-	SlruPagePrecedesUnitTests(SerialSlruCtl, SERIAL_ENTRIESPERPAGE);
-
-	/*
-	 * Create or attach to the SerialControl structure.
-	 */
-	serialControl = (SerialControl)
-		ShmemInitStruct("SerialControlData", sizeof(SerialControlData), &found);
-
-	Assert(found == IsUnderPostmaster);
-	if (!found)
-	{
-		/*
-		 * Set control information to reflect empty SLRU.
-		 */
-		LWLockAcquire(SerialControlLock, LW_EXCLUSIVE);
-		serialControl->headPage = -1;
-		serialControl->headXid = InvalidTransactionId;
-		serialControl->tailXid = InvalidTransactionId;
-		LWLockRelease(SerialControlLock);
-	}
-}
-
 /*
  * GUC check_hook for serializable_buffers
  */
@@ -1358,7 +1315,35 @@ PredicateLockShmemInit(void)
 	 * Initialize the SLRU storage for old committed serializable
 	 * transactions.
 	 */
-	SerialInit();
+	SerialSlruCtl->PagePrecedes = SerialPagePrecedesLogically;
+	SerialSlruCtl->errdetail_for_io_error = serial_errdetail_for_io_error;
+	SimpleLruInit(SerialSlruCtl, "serializable",
+				  serializable_buffers, 0, "pg_serial",
+				  LWTRANCHE_SERIAL_BUFFER, LWTRANCHE_SERIAL_SLRU,
+				  SYNC_HANDLER_NONE, false);
+#ifdef USE_ASSERT_CHECKING
+	SerialPagePrecedesLogicallyUnitTests();
+#endif
+	SlruPagePrecedesUnitTests(SerialSlruCtl, SERIAL_ENTRIESPERPAGE);
+
+	/*
+	 * Create or attach to the SerialControl structure.
+	 */
+	serialControl = (SerialControl)
+		ShmemInitStruct("SerialControlData", sizeof(SerialControlData), &found);
+
+	Assert(found == IsUnderPostmaster);
+	if (!found)
+	{
+		/*
+		 * Set control information to reflect empty SLRU.
+		 */
+		LWLockAcquire(SerialControlLock, LW_EXCLUSIVE);
+		serialControl->headPage = -1;
+		serialControl->headXid = InvalidTransactionId;
+		serialControl->tailXid = InvalidTransactionId;
+		LWLockRelease(SerialControlLock);
+	}
 }
 
 /*
-- 
2.47.3



  [text/x-patch] v8-0004-refactor-predicate.c-Use-separate-variables-for-d.patch (6.7K, 5-v8-0004-refactor-predicate.c-Use-separate-variables-for-d.patch)
  download | inline diff:
From bf42b34a31fd1b3d79ce7a6e6390c284fa2789d8 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Fri, 20 Mar 2026 15:29:53 +0200
Subject: [PATCH v8 04/16] refactor predicate.c: Use separate variables for
 different sizes for clarity

The ShmemInit function is very complicated currently. These
refactorings move it in a direction that is more natural with the new
shmem callbacks.
---
 src/backend/storage/lmgr/predicate.c | 68 ++++++++++++++--------------
 1 file changed, 35 insertions(+), 33 deletions(-)

diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c
index 27682f416e7..40950ee3a4f 100644
--- a/src/backend/storage/lmgr/predicate.c
+++ b/src/backend/storage/lmgr/predicate.c
@@ -1113,7 +1113,10 @@ void
 PredicateLockShmemInit(void)
 {
 	HASHCTL		info;
-	int64		max_table_size;
+	int64		max_predicate_lock_targets;
+	int64		max_predicate_locks;
+	int64		max_serializable_xacts;
+	int64		max_rw_conflicts;
 	Size		requestSize;
 	bool		found;
 
@@ -1125,7 +1128,7 @@ PredicateLockShmemInit(void)
 	 * Compute size of predicate lock target hashtable. Note these
 	 * calculations must agree with PredicateLockShmemSize!
 	 */
-	max_table_size = NPREDICATELOCKTARGETENTS();
+	max_predicate_lock_targets = NPREDICATELOCKTARGETENTS();
 
 	/*
 	 * Allocate hash table for PREDICATELOCKTARGET structs.  This stores
@@ -1136,8 +1139,8 @@ PredicateLockShmemInit(void)
 	info.num_partitions = NUM_PREDICATELOCK_PARTITIONS;
 
 	PredicateLockTargetHash = ShmemInitHash("PREDICATELOCKTARGET hash",
-											max_table_size,
-											max_table_size,
+											max_predicate_lock_targets,
+											max_predicate_lock_targets,
 											&info,
 											HASH_ELEM | HASH_BLOBS |
 											HASH_PARTITION | HASH_FIXED_SIZE);
@@ -1169,11 +1172,11 @@ PredicateLockShmemInit(void)
 	info.num_partitions = NUM_PREDICATELOCK_PARTITIONS;
 
 	/* Assume an average of 2 xacts per target */
-	max_table_size *= 2;
+	max_predicate_locks = max_predicate_lock_targets * 2;
 
 	PredicateLockHash = ShmemInitHash("PREDICATELOCK hash",
-									  max_table_size,
-									  max_table_size,
+									  max_predicate_locks,
+									  max_predicate_locks,
 									  &info,
 									  HASH_ELEM | HASH_FUNCTION |
 									  HASH_PARTITION | HASH_FIXED_SIZE);
@@ -1181,23 +1184,20 @@ PredicateLockShmemInit(void)
 	/*
 	 * Compute size for serializable transaction hashtable. Note these
 	 * calculations must agree with PredicateLockShmemSize!
-	 */
-	max_table_size = (MaxBackends + max_prepared_xacts);
-
-	/*
-	 * Allocate a list to hold information on transactions participating in
-	 * predicate locking.
 	 *
 	 * Assume an average of 10 predicate locking transactions per backend.
 	 * This allows aggressive cleanup while detail is present before data must
 	 * be summarized for storage in SLRU and the "dummy" transaction.
 	 */
-	max_table_size *= 10;
+	max_serializable_xacts = (MaxBackends + max_prepared_xacts) * 10;
 
+	/*
+	 * Allocate a list to hold information on transactions participating in
+	 * predicate locking.
+	 */
 	requestSize = add_size(PredXactListDataSize,
-						   (mul_size((Size) max_table_size,
+						   (mul_size((Size) max_serializable_xacts,
 									 sizeof(SERIALIZABLEXACT))));
-
 	PredXact = ShmemInitStruct("PredXactList",
 							   requestSize,
 							   &found);
@@ -1220,7 +1220,7 @@ PredicateLockShmemInit(void)
 		PredXact->element
 			= (SERIALIZABLEXACT *) ((char *) PredXact + PredXactListDataSize);
 		/* Add all elements to available list, clean. */
-		for (i = 0; i < max_table_size; i++)
+		for (i = 0; i < max_serializable_xacts; i++)
 		{
 			LWLockInitialize(&PredXact->element[i].perXactPredicateListLock,
 							 LWTRANCHE_PER_XACT_PREDICATE_LIST);
@@ -1254,8 +1254,8 @@ PredicateLockShmemInit(void)
 	info.entrysize = sizeof(SERIALIZABLEXID);
 
 	SerializableXidHash = ShmemInitHash("SERIALIZABLEXID hash",
-										max_table_size,
-										max_table_size,
+										max_serializable_xacts,
+										max_serializable_xacts,
 										&info,
 										HASH_ELEM | HASH_BLOBS |
 										HASH_FIXED_SIZE);
@@ -1271,10 +1271,10 @@ PredicateLockShmemInit(void)
 	 * occasional transactions canceled when trying to flag conflicts. That's
 	 * probably OK.
 	 */
-	max_table_size *= 5;
+	max_rw_conflicts = max_serializable_xacts * 5;
 
 	requestSize = RWConflictPoolHeaderDataSize +
-		mul_size((Size) max_table_size,
+		mul_size((Size) max_rw_conflicts,
 				 RWConflictDataSize);
 
 	RWConflictPool = ShmemInitStruct("RWConflictPool",
@@ -1292,7 +1292,7 @@ PredicateLockShmemInit(void)
 		RWConflictPool->element = (RWConflict) ((char *) RWConflictPool +
 												RWConflictPoolHeaderDataSize);
 		/* Add all elements to available list, clean. */
-		for (i = 0; i < max_table_size; i++)
+		for (i = 0; i < max_rw_conflicts; i++)
 		{
 			dlist_push_tail(&RWConflictPool->availableList,
 							&RWConflictPool->element[i].outLink);
@@ -1353,16 +1353,19 @@ Size
 PredicateLockShmemSize(void)
 {
 	Size		size = 0;
-	long		max_table_size;
+	int64		max_predicate_lock_targets;
+	int64		max_predicate_locks;
+	int64		max_serializable_xacts;
+	int64		max_rw_conflicts;
 
 	/* predicate lock target hash table */
-	max_table_size = NPREDICATELOCKTARGETENTS();
-	size = add_size(size, hash_estimate_size(max_table_size,
+	max_predicate_lock_targets = NPREDICATELOCKTARGETENTS();
+	size = add_size(size, hash_estimate_size(max_predicate_lock_targets,
 											 sizeof(PREDICATELOCKTARGET)));
 
 	/* predicate lock hash table */
-	max_table_size *= 2;
-	size = add_size(size, hash_estimate_size(max_table_size,
+	max_predicate_locks = max_predicate_lock_targets * 2;
+	size = add_size(size, hash_estimate_size(max_predicate_locks,
 											 sizeof(PREDICATELOCK)));
 
 	/*
@@ -1372,20 +1375,19 @@ PredicateLockShmemSize(void)
 	size = add_size(size, size / 10);
 
 	/* transaction list */
-	max_table_size = MaxBackends + max_prepared_xacts;
-	max_table_size *= 10;
+	max_serializable_xacts = (MaxBackends + max_prepared_xacts) * 10;
 	size = add_size(size, PredXactListDataSize);
-	size = add_size(size, mul_size((Size) max_table_size,
+	size = add_size(size, mul_size((Size) max_serializable_xacts,
 								   sizeof(SERIALIZABLEXACT)));
 
 	/* transaction xid table */
-	size = add_size(size, hash_estimate_size(max_table_size,
+	size = add_size(size, hash_estimate_size(max_serializable_xacts,
 											 sizeof(SERIALIZABLEXID)));
 
 	/* rw-conflict pool */
-	max_table_size *= 5;
+	max_rw_conflicts = max_serializable_xacts * 5;
 	size = add_size(size, RWConflictPoolHeaderDataSize);
-	size = add_size(size, mul_size((Size) max_table_size,
+	size = add_size(size, mul_size((Size) max_rw_conflicts,
 								   RWConflictDataSize));
 
 	/* Head for list of finished serializable transactions. */
-- 
2.47.3



  [text/x-patch] v8-0005-refactor-predicate.c-Move-all-the-initialization-.patch (8.3K, 6-v8-0005-refactor-predicate.c-Move-all-the-initialization-.patch)
  download | inline diff:
From 98c04053eacc1a0e0695efaf51d03264619e7dea Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Fri, 20 Mar 2026 20:27:50 +0200
Subject: [PATCH v8 05/16] refactor predicate.c: Move all the initialization
 together

The ShmemInit function is very complicated currently. These
refactorings move it in a direction that is more natural with the new
shmem callbacks.
---
 src/backend/storage/lmgr/predicate.c | 164 +++++++++++++--------------
 1 file changed, 79 insertions(+), 85 deletions(-)

diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c
index 40950ee3a4f..4f80fc73639 100644
--- a/src/backend/storage/lmgr/predicate.c
+++ b/src/backend/storage/lmgr/predicate.c
@@ -1145,19 +1145,6 @@ PredicateLockShmemInit(void)
 											HASH_ELEM | HASH_BLOBS |
 											HASH_PARTITION | HASH_FIXED_SIZE);
 
-	/*
-	 * Reserve a dummy entry in the hash table; we use it to make sure there's
-	 * always one entry available when we need to split or combine a page,
-	 * because running out of space there could mean aborting a
-	 * non-serializable transaction.
-	 */
-	if (!IsUnderPostmaster)
-	{
-		(void) hash_search(PredicateLockTargetHash, &ScratchTargetTag,
-						   HASH_ENTER, &found);
-		Assert(!found);
-	}
-
 	/* Pre-calculate the hash and partition lock of the scratch entry */
 	ScratchTargetTagHash = PredicateLockTargetTagHashCode(&ScratchTargetTag);
 	ScratchPartitionLock = PredicateLockHashPartitionLock(ScratchTargetTagHash);
@@ -1202,49 +1189,6 @@ PredicateLockShmemInit(void)
 							   requestSize,
 							   &found);
 	Assert(found == IsUnderPostmaster);
-	if (!found)
-	{
-		int			i;
-
-		/* clean everything, both the header and the element */
-		memset(PredXact, 0, requestSize);
-
-		dlist_init(&PredXact->availableList);
-		dlist_init(&PredXact->activeList);
-		PredXact->SxactGlobalXmin = InvalidTransactionId;
-		PredXact->SxactGlobalXminCount = 0;
-		PredXact->WritableSxactCount = 0;
-		PredXact->LastSxactCommitSeqNo = FirstNormalSerCommitSeqNo - 1;
-		PredXact->CanPartialClearThrough = 0;
-		PredXact->HavePartialClearedThrough = 0;
-		PredXact->element
-			= (SERIALIZABLEXACT *) ((char *) PredXact + PredXactListDataSize);
-		/* Add all elements to available list, clean. */
-		for (i = 0; i < max_serializable_xacts; i++)
-		{
-			LWLockInitialize(&PredXact->element[i].perXactPredicateListLock,
-							 LWTRANCHE_PER_XACT_PREDICATE_LIST);
-			dlist_push_tail(&PredXact->availableList, &PredXact->element[i].xactLink);
-		}
-		PredXact->OldCommittedSxact = CreatePredXact();
-		SetInvalidVirtualTransactionId(PredXact->OldCommittedSxact->vxid);
-		PredXact->OldCommittedSxact->prepareSeqNo = 0;
-		PredXact->OldCommittedSxact->commitSeqNo = 0;
-		PredXact->OldCommittedSxact->SeqNo.lastCommitBeforeSnapshot = 0;
-		dlist_init(&PredXact->OldCommittedSxact->outConflicts);
-		dlist_init(&PredXact->OldCommittedSxact->inConflicts);
-		dlist_init(&PredXact->OldCommittedSxact->predicateLocks);
-		dlist_node_init(&PredXact->OldCommittedSxact->finishedLink);
-		dlist_init(&PredXact->OldCommittedSxact->possibleUnsafeConflicts);
-		PredXact->OldCommittedSxact->topXid = InvalidTransactionId;
-		PredXact->OldCommittedSxact->finishedBefore = InvalidTransactionId;
-		PredXact->OldCommittedSxact->xmin = InvalidTransactionId;
-		PredXact->OldCommittedSxact->flags = SXACT_FLAG_COMMITTED;
-		PredXact->OldCommittedSxact->pid = 0;
-		PredXact->OldCommittedSxact->pgprocno = INVALID_PROC_NUMBER;
-	}
-	/* This never changes, so let's keep a local copy. */
-	OldCommittedSxact = PredXact->OldCommittedSxact;
 
 	/*
 	 * Allocate hash table for SERIALIZABLEXID structs.  This stores per-xid
@@ -1281,23 +1225,6 @@ PredicateLockShmemInit(void)
 									 requestSize,
 									 &found);
 	Assert(found == IsUnderPostmaster);
-	if (!found)
-	{
-		int			i;
-
-		/* clean everything, including the elements */
-		memset(RWConflictPool, 0, requestSize);
-
-		dlist_init(&RWConflictPool->availableList);
-		RWConflictPool->element = (RWConflict) ((char *) RWConflictPool +
-												RWConflictPoolHeaderDataSize);
-		/* Add all elements to available list, clean. */
-		for (i = 0; i < max_rw_conflicts; i++)
-		{
-			dlist_push_tail(&RWConflictPool->availableList,
-							&RWConflictPool->element[i].outLink);
-		}
-	}
 
 	/*
 	 * Create or attach to the header for the list of finished serializable
@@ -1308,8 +1235,6 @@ PredicateLockShmemInit(void)
 						sizeof(dlist_head),
 						&found);
 	Assert(found == IsUnderPostmaster);
-	if (!found)
-		dlist_init(FinishedSerializableTransactions);
 
 	/*
 	 * Initialize the SLRU storage for old committed serializable
@@ -1331,19 +1256,88 @@ PredicateLockShmemInit(void)
 	 */
 	serialControl = (SerialControl)
 		ShmemInitStruct("SerialControlData", sizeof(SerialControlData), &found);
-
 	Assert(found == IsUnderPostmaster);
-	if (!found)
+
+	/*
+	 * If we just attached to existing shared memory (EXEC_BACKEND), we're all
+	 * done.  Otherwise, during postmaster startup proceed to initialize the
+	 * shared memory.
+	 */
+	if (IsUnderPostmaster)
 	{
-		/*
-		 * Set control information to reflect empty SLRU.
-		 */
-		LWLockAcquire(SerialControlLock, LW_EXCLUSIVE);
-		serialControl->headPage = -1;
-		serialControl->headXid = InvalidTransactionId;
-		serialControl->tailXid = InvalidTransactionId;
-		LWLockRelease(SerialControlLock);
+		/* This never changes, so let's keep a local copy. */
+		OldCommittedSxact = PredXact->OldCommittedSxact;
+		return;
+	}
+
+	/*
+	 * Reserve a dummy entry in the hash table; we use it to make sure there's
+	 * always one entry available when we need to split or combine a page,
+	 * because running out of space there could mean aborting a
+	 * non-serializable transaction.
+	 */
+	(void) hash_search(PredicateLockTargetHash, &ScratchTargetTag,
+					   HASH_ENTER, &found);
+	Assert(!found);
+
+	/* Initialize PredXact list */
+	dlist_init(&PredXact->availableList);
+	dlist_init(&PredXact->activeList);
+	PredXact->SxactGlobalXmin = InvalidTransactionId;
+	PredXact->SxactGlobalXminCount = 0;
+	PredXact->WritableSxactCount = 0;
+	PredXact->LastSxactCommitSeqNo = FirstNormalSerCommitSeqNo - 1;
+	PredXact->CanPartialClearThrough = 0;
+	PredXact->HavePartialClearedThrough = 0;
+	PredXact->element
+		= (SERIALIZABLEXACT *) ((char *) PredXact + PredXactListDataSize);
+	/* Add all elements to available list, clean. */
+	for (int i = 0; i < max_serializable_xacts; i++)
+	{
+		LWLockInitialize(&PredXact->element[i].perXactPredicateListLock,
+						 LWTRANCHE_PER_XACT_PREDICATE_LIST);
+		dlist_push_tail(&PredXact->availableList, &PredXact->element[i].xactLink);
 	}
+	PredXact->OldCommittedSxact = CreatePredXact();
+	SetInvalidVirtualTransactionId(PredXact->OldCommittedSxact->vxid);
+	PredXact->OldCommittedSxact->prepareSeqNo = 0;
+	PredXact->OldCommittedSxact->commitSeqNo = 0;
+	PredXact->OldCommittedSxact->SeqNo.lastCommitBeforeSnapshot = 0;
+	dlist_init(&PredXact->OldCommittedSxact->outConflicts);
+	dlist_init(&PredXact->OldCommittedSxact->inConflicts);
+	dlist_init(&PredXact->OldCommittedSxact->predicateLocks);
+	dlist_node_init(&PredXact->OldCommittedSxact->finishedLink);
+	dlist_init(&PredXact->OldCommittedSxact->possibleUnsafeConflicts);
+	PredXact->OldCommittedSxact->topXid = InvalidTransactionId;
+	PredXact->OldCommittedSxact->finishedBefore = InvalidTransactionId;
+	PredXact->OldCommittedSxact->xmin = InvalidTransactionId;
+	PredXact->OldCommittedSxact->flags = SXACT_FLAG_COMMITTED;
+	PredXact->OldCommittedSxact->pid = 0;
+	PredXact->OldCommittedSxact->pgprocno = INVALID_PROC_NUMBER;
+
+	/* This never changes, so let's keep a local copy. */
+	OldCommittedSxact = PredXact->OldCommittedSxact;
+
+	/* Initialize the rw-conflict pool */
+	dlist_init(&RWConflictPool->availableList);
+	RWConflictPool->element = (RWConflict) ((char *) RWConflictPool +
+											RWConflictPoolHeaderDataSize);
+	/* Add all elements to available list, clean. */
+	for (int i = 0; i < max_rw_conflicts; i++)
+	{
+		dlist_push_tail(&RWConflictPool->availableList,
+						&RWConflictPool->element[i].outLink);
+	}
+
+	/* Initialize the list of finished serializable transactions */
+	dlist_init(FinishedSerializableTransactions);
+
+	/* Initialize SerialControl to reflect empty SLRU. */
+	LWLockAcquire(SerialControlLock, LW_EXCLUSIVE);
+	serialControl->headPage = -1;
+	serialControl->headXid = InvalidTransactionId;
+	serialControl->tailXid = InvalidTransactionId;
+	LWLockRelease(SerialControlLock);
 }
 
 /*
-- 
2.47.3



  [text/x-patch] v8-0006-Introduce-a-new-mechanism-for-registering-shared-.patch (59.3K, 7-v8-0006-Introduce-a-new-mechanism-for-registering-shared-.patch)
  download | inline diff:
From 3701db9df2d08a3995974d7447edb1ca59bff37f Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Fri, 27 Mar 2026 02:32:36 +0200
Subject: [PATCH v8 06/16] Introduce a new mechanism for registering shared
 memory areas

Each shared memory area is registered with a "descriptor struct" that
contains parameters like name and size of the area. The descriptor
struct makes it easier to add optional fields in the future; the
additional fields can just be left as zeros.

This merges the separate [Subsystem]ShmemSize() and
[Subsystem]ShmemInit() phases at postmaster startup. Each subsystem is
now called into just once, before the shared memory segment has been
allocated, to register the subsystem's shared memory needs. The
registration includes the size, which replaces the
[Subsystem]ShmemSize() calls, and a pointer to an initialization
callback function, which replaces the [Subsystem]ShmemInit()
calls. This is more ergonomic, as you only need to calculate the size
once, when you register the struct.

This replaces ShmemInitStruct() and ShmemInitHash(), which become just
backwards-compatibility wrappers around the new functions. In future
commits, I plan to replace all ShmemInitStruct() and ShmemInitHash()
calls with the new functions, although we'll still need to keep them
around for extensions.

Reviewed-by: Ashutosh Bapat <[email protected]>
Reviewed-by: Zsolt Parragi <[email protected]>
Reviewed-by: Robert Haas <[email protected]>
Reviewed-by: Daniel Gustafsson <[email protected]>
Discussion: https://www.postgresql.org/message-id/CAExHW5vM1bneLYfg0wGeAa=52UiJ3z4vKd3AJ72X8Fw6k3KKrg@mail.gmail.com
---
 doc/src/sgml/system-views.sgml          |   4 +-
 doc/src/sgml/xfunc.sgml                 | 161 +++--
 src/backend/bootstrap/bootstrap.c       |   1 +
 src/backend/postmaster/launch_backend.c |   4 +
 src/backend/postmaster/postmaster.c     |  18 +-
 src/backend/storage/ipc/ipci.c          |  33 +-
 src/backend/storage/ipc/shmem.c         | 770 ++++++++++++++++++++----
 src/backend/storage/ipc/shmem_hash.c    |  92 ++-
 src/backend/storage/lmgr/proc.c         |   3 +
 src/backend/tcop/postgres.c             |   9 +-
 src/backend/utils/hash/dynahash.c       |   2 +
 src/include/storage/shmem.h             | 210 ++++++-
 src/test/modules/test_aio/test_aio.c    |   1 -
 src/tools/pgindent/typedefs.list        |   9 +-
 14 files changed, 1120 insertions(+), 197 deletions(-)

diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml
index 9ee1a2bfc6a..2ebec6928d5 100644
--- a/doc/src/sgml/system-views.sgml
+++ b/doc/src/sgml/system-views.sgml
@@ -4254,8 +4254,8 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
   <para>
    Anonymous allocations are allocations that have been made
    with <literal>ShmemAlloc()</literal> directly, rather than via
-   <literal>ShmemInitStruct()</literal> or
-   <literal>ShmemInitHash()</literal>.
+   <literal>ShmemRequestStruct()</literal> or
+   <literal>ShmemRequestHash()</literal>.
   </para>
 
   <para>
diff --git a/doc/src/sgml/xfunc.sgml b/doc/src/sgml/xfunc.sgml
index 70e815b8a2c..1e9ff801a7c 100644
--- a/doc/src/sgml/xfunc.sgml
+++ b/doc/src/sgml/xfunc.sgml
@@ -3628,71 +3628,131 @@ CREATE FUNCTION make_array(anyelement) RETURNS anyarray
       Add-ins can reserve shared memory on server startup.  To do so, the
       add-in's shared library must be preloaded by specifying it in
       <xref linkend="guc-shared-preload-libraries"/><indexterm><primary>shared_preload_libraries</primary></indexterm>.
-      The shared library should also register a
-      <literal>shmem_request_hook</literal> in its
-      <function>_PG_init</function> function.  This
-      <literal>shmem_request_hook</literal> can reserve shared memory by
-      calling:
+      The shared library should register callbacks in its
+      its <function>_PG_init</function> function, which then get called at the
+      right stages of the system startup to initialize the shared memory.
+      Here is an example:
 <programlisting>
-void RequestAddinShmemSpace(Size size)
-</programlisting>
-      Each backend should obtain a pointer to the reserved shared memory by
-      calling:
-<programlisting>
-void *ShmemInitStruct(const char *name, Size size, bool *foundPtr)
-</programlisting>
-      If this function sets <literal>foundPtr</literal> to
-      <literal>false</literal>, the caller should proceed to initialize the
-      contents of the reserved shared memory.  If <literal>foundPtr</literal>
-      is set to <literal>true</literal>, the shared memory was already
-      initialized by another backend, and the caller need not initialize
-      further.
-     </para>
+typedef struct MyShmemData {
+    LWLock      lock;    /* protects the fields below */
 
-     <para>
-      To avoid race conditions, each backend should use the LWLock
-      <function>AddinShmemInitLock</function> when initializing its allocation
-      of shared memory, as shown here:
-<programlisting>
-static mystruct *ptr = NULL;
-bool        found;
+    ... shared memory contents ...
+} MyShmemData;
+
+static MyShmemData *MyShmem;    /* pointer to the struct in shared memory */
+
+static void my_shmem_request(void *arg);
+static void my_shmem_init(void *arg);
+
+const ShmemCallbacks my_shmem_callbacks = {
+    .shmem_request_fn = my_shmem_request,
+    .shmem_init_fn = my_shmem_init,
+};
+
+/*
+ * Module load callback
+ */
+void
+_PG_init(void)
+{
+    /*
+     * In order to create our shared memory area, we have to be loaded via
+     * shared_preload_libraries.
+     */
+    if (!process_shared_preload_libraries_in_progress)
+        return;
+
+    /* Register our shared memory needs */
+    RegisterShmemCallbacks(&amp;my_shmem_callbacks);
+}
+
+/* callback to request  */
+static void
+my_shmem_request(void *arg)
+{
+    /* A persistent handle to the shared memory area in this backend */
+    static ShmemStructDesc MyShmemDesc;
+
+    ShmemRequestStruct(&amp;MyShmemDesc, &amp;(ShmemRequestStructOpts) {
+        .name = "My shmem area",
+        .size = sizeof(MyShmemData),
+        .ptr = (void **) &amp;MyShmem,
+    });
+}
 
-LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
-ptr = ShmemInitStruct("my struct name", size, &amp;found);
-if (!found)
+/* callback to initialize the contents of the MyShmem area at startup */
+static void
+my_shmem_init(void *arg)
 {
-    ... initialize contents of shared memory ...
-    ptr->locks = GetNamedLWLockTranche("my tranche name");
+    int         tranche_id;
+
+    /* Initialize the lock */
+    tranche_id = LWLockNewTrancheId("my tranche name");
+    LWLockInitialize(&amp;MyShmem->lock, tranche_id);
+
+    ... initialize the rest of MyShmem fields ...
 }
-LWLockRelease(AddinShmemInitLock);
+
 </programlisting>
-      <literal>shmem_startup_hook</literal> provides a convenient place for the
-      initialization code, but it is not strictly required that all such code
-      be placed in this hook.  On Windows (and anywhere else where
-      <literal>EXEC_BACKEND</literal> is defined), each backend executes the
-      registered <literal>shmem_startup_hook</literal> shortly after it
-      attaches to shared memory, so add-ins should still acquire
-      <function>AddinShmemInitLock</function> within this hook, as shown in the
-      example above.  On other platforms, only the postmaster process executes
-      the <literal>shmem_startup_hook</literal>, and each backend automatically
-      inherits the pointers to shared memory.
+      The <function>request_fn</function> callback is called during system
+      startup, before the shared memory has been allocated. It should call
+      <function>ShmemRequestStruct()</function> to register the add-in's
+      shared memory needs. Note that <function>ShmemRequestStruct()</function>
+      doesn't immediately allocate or initialize the memory, it merely
+      registers the space to be allocated later in the startup sequence.  When
+      the memory is allocated, it is initialized to zero.  For any more
+      complex initialization, set the <function>init_fn()</function> callback,
+      which will be called after the memory has been allocated and initialized
+      to zero, but before any other processes are running, and thus no locking
+      is required.
      </para>
-
      <para>
-      An example of a <literal>shmem_request_hook</literal> and
-      <literal>shmem_startup_hook</literal> can be found in
+      On Windows, the <function>attach_fn</function> callback, if any, is
+      additionally called at every backend startup.  It can be used to
+      initialize additional per-backend state related to the shared memory
+      area that is inherited via <function>fork()</function> on other systems.
+     </para>
+     <para>
+      An example of allocating shared memory can be found in
       <filename>contrib/pg_stat_statements/pg_stat_statements.c</filename> in
       the <productname>PostgreSQL</productname> source tree.
      </para>
     </sect3>
 
     <sect3 id="xfunc-shared-addin-after-startup">
-     <title>Requesting Shared Memory After Startup</title>
+     <title>Requesting Shared Memory After Startup with <function>ShmemRequestStruct</function></title>
+
+     <para>
+      The <function>ShmemRequestStruct()</function> can also be called after
+      system startup, which is useful to allow small allocations in add-in
+      libraries that are not specified in
+      <xref linkend="guc-shared-preload-libraries"/><indexterm><primary>shared_preload_libraries</primary></indexterm>.
+      However, after startup the allocation can fail if there is not enough
+      shared memory available. The system reserves some memory for allocations
+      after startup, but that reservation is small.
+     </para>
+     <para>
+      By default, <function>RegisterShmemCallbacks()</function> fails with an
+      error if called after system startup. To use it after startup, you must
+      set the <literal>SHMEM_ALLOW_AFTER_STARTUP</literal> flag in the
+      descriptor to acknowledge the risk.
+     </para>
+     <para>
+      When <function>RegisterShmemCallbacks()</function> is called after
+      startup, it will immediately call the appropriate callbacks, depending
+      on whether the requested memory areas were already initialized by
+      another backend. The callbacks will be called while holding an internal
+      lock, which prevents concurrent two backends from initializating the
+      memory area concurrently.
+     </para>
+    </sect3>
+
+    <sect3 id="xfunc-shared-addin-dynamic">
+     <title>Allocating Dynamic Shared Memory After Startup</title>
 
      <para>
       There is another, more flexible method of reserving shared memory that
-      can be done after server startup and outside a
-      <literal>shmem_request_hook</literal>.  To do so, each backend that will
+      can be done after server startup.  To do so, each backend that will
       use the shared memory should obtain a pointer to it by calling:
 <programlisting>
 void *GetNamedDSMSegment(const char *name, size_t size,
@@ -3711,10 +3771,7 @@ void *GetNamedDSMSegment(const char *name, size_t size,
      </para>
 
      <para>
-      Unlike shared memory reserved at server startup, there is no need to
-      acquire <function>AddinShmemInitLock</function> or otherwise take action
-      to avoid race conditions when reserving shared memory with
-      <function>GetNamedDSMSegment</function>.  This function ensures that only
+      <function>GetNamedDSMSegment</function> ensures that only
       one backend allocates and initializes the segment and that all other
       backends receive a pointer to the fully allocated and initialized
       segment.
diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index 68a42de0889..86fe86354f5 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -370,6 +370,7 @@ BootstrapModeMain(int argc, char *argv[], bool check_only)
 
 	InitializeFastPathLocks();
 
+	ShmemCallRequestCallbacks();
 	CreateSharedMemoryAndSemaphores();
 
 	/*
diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index 434e0643022..75423104be8 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -49,6 +49,7 @@
 #include "replication/walreceiver.h"
 #include "storage/dsm.h"
 #include "storage/io_worker.h"
+#include "storage/ipc.h"
 #include "storage/pg_shmem.h"
 #include "tcop/backend_startup.h"
 #include "utils/memutils.h"
@@ -672,7 +673,10 @@ SubPostmasterMain(int argc, char *argv[])
 
 	/* Restore basic shared memory pointers */
 	if (UsedShmemSegAddr != NULL)
+	{
 		InitShmemAllocator(UsedShmemSegAddr);
+		ShmemCallRequestCallbacks();
+	}
 
 	/*
 	 * Run the appropriate Main function
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 3fac46c402b..e81ef248bf1 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -959,7 +959,14 @@ PostmasterMain(int argc, char *argv[])
 	InitializeFastPathLocks();
 
 	/*
-	 * Give preloaded libraries a chance to request additional shared memory.
+	 * Ask all subsystems, including preloaded libraries, to register their
+	 * shared memory needs.
+	 */
+	ShmemCallRequestCallbacks();
+
+	/*
+	 * Also call any legacy shmem request hooks that might'be been installed
+	 * by preloaded libraries.
 	 */
 	process_shmem_requests();
 
@@ -3235,7 +3242,14 @@ PostmasterStateMachine(void)
 		/* re-read control file into local memory */
 		LocalProcessControlFile(true);
 
-		/* re-create shared memory and semaphores */
+		/*
+		 * Re-initialize shared memory and semaphores.  Note: We don't call
+		 * RegisterShmemStructs() here, we keep the old registrations.  In
+		 * order to re-register structs in extensions, we'd need to reload
+		 * shared preload libraries, and we don't want to do that.
+		 */
+		ResetShmemAllocator();
+		ShmemCallRequestCallbacks();
 		CreateSharedMemoryAndSemaphores();
 
 		UpdatePMState(PM_STARTUP);
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index d692d419846..493ddd7f12f 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -99,8 +99,9 @@ CalculateShmemSize(void)
 	 * during the actual allocation phase.
 	 */
 	size = 100000;
-	size = add_size(size, hash_estimate_size(SHMEM_INDEX_SIZE,
-											 sizeof(ShmemIndexEnt)));
+	size = add_size(size, ShmemGetRequestedSize());
+
+	/* legacy subsystems */
 	size = add_size(size, dsm_estimate_size());
 	size = add_size(size, DSMRegistryShmemSize());
 	size = add_size(size, BufferManagerShmemSize());
@@ -174,6 +175,13 @@ AttachSharedMemoryStructs(void)
 	 */
 	InitializeFastPathLocks();
 
+	/*
+	 * Attach to LWLocks first. They are needed by most other subsystems.
+	 */
+	LWLockShmemInit();
+
+	/* Establish pointers to all shared memory areas in this backend */
+	ShmemAttachRequested();
 	CreateOrAttachShmemStructs();
 
 	/*
@@ -218,7 +226,21 @@ CreateSharedMemoryAndSemaphores(void)
 	 */
 	InitShmemAllocator(seghdr);
 
-	/* Initialize subsystems */
+	/* Reserve space for semaphores. */
+	if (!IsUnderPostmaster)
+		PGReserveSemaphores(ProcGlobalSemas());
+
+	/*
+	 * Initialize LWLocks first, in case any of the shmem init function use
+	 * LWLocks.  (Nothing else can be running during startup, so they don't
+	 * need to do any locking yet, but we nevertheless allow it.)
+	 */
+	LWLockShmemInit();
+
+	/* Initialize all shmem areas */
+	ShmemInitRequested();
+
+	/* Initialize legacy subsystems */
 	CreateOrAttachShmemStructs();
 
 	/* Initialize dynamic shared memory facilities. */
@@ -249,11 +271,6 @@ CreateSharedMemoryAndSemaphores(void)
 static void
 CreateOrAttachShmemStructs(void)
 {
-	/*
-	 * Set up LWLocks.  They are needed by most other subsystems.
-	 */
-	LWLockShmemInit();
-
 	dsm_shmem_init();
 	DSMRegistryShmemInit();
 
diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index c86d691dcfb..84099ce78fe 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -19,48 +19,116 @@
  * methods).  The routines in this file are used for allocating and
  * binding to shared memory data structures.
  *
- * NOTES:
- *		(a) There are three kinds of shared memory data structures
- *	available to POSTGRES: fixed-size structures, queues and hash
- *	tables.  Fixed-size structures contain things like global variables
- *	for a module and should never be allocated after the shared memory
- *	initialization phase.  Hash tables have a fixed maximum size, but
- *	their actual size can vary dynamically.  When entries are added
- *	to the table, more space is allocated.  Queues link data structures
- *	that have been allocated either within fixed-size structures or as hash
- *	buckets.  Each shared data structure has a string name to identify
- *	it (assigned in the module that declares it).
- *
- *		(b) During initialization, each module looks for its
- *	shared data structures in a hash table called the "Shmem Index".
- *	If the data structure is not present, the caller can allocate
- *	a new one and initialize it.  If the data structure is present,
- *	the caller "attaches" to the structure by initializing a pointer
- *	in the local address space.
- *		The shmem index has two purposes: first, it gives us
- *	a simple model of how the world looks when a backend process
- *	initializes.  If something is present in the shmem index,
- *	it is initialized.  If it is not, it is uninitialized.  Second,
- *	the shmem index allows us to allocate shared memory on demand
- *	instead of trying to preallocate structures and hard-wire the
- *	sizes and locations in header files.  If you are using a lot
- *	of shared memory in a lot of different places (and changing
- *	things during development), this is important.
- *
- *		(c) In standard Unix-ish environments, individual backends do not
- *	need to re-establish their local pointers into shared memory, because
- *	they inherit correct values of those variables via fork() from the
- *	postmaster.  However, this does not work in the EXEC_BACKEND case.
- *	In ports using EXEC_BACKEND, new backends have to set up their local
- *	pointers using the method described in (b) above.
- *
- *		(d) memory allocation model: shared memory can never be
- *	freed, once allocated.   Each hash table has its own free list,
- *	so hash buckets can be reused when an item is deleted.  However,
- *	if one hash table grows very large and then shrinks, its space
- *	cannot be redistributed to other tables.  We could build a simple
- *	hash bucket garbage collector if need be.  Right now, it seems
- *	unnecessary.
+ * This module provides facilities to allocate fixed-size structures in shared
+ * memory, for things like variables shared between all backend processes.
+ * Each such structure has a string name to identify it, specified in the
+ * descriptor when it is requested.  shmem_hash.c provides a shared hash table
+ * implementation on top of that.
+ *
+ * Shared memory areas should usually not be allocated after postmaster
+ * startup, although we do allow small allocations later for the benefit of
+ * extension modules that loaded after startup.  Despite that allowance,
+ * extensions that need shared memory should be added in
+ * shared_preload_libraries, because the allowance is quite small and there is
+ * no guarantee that any memory is available after startup.
+ *
+ * Nowadays, there is also a third way to allocate shared memory called
+ * Dynamic Shared Memory.  See dsm.c for that facility.  One big difference
+ * between traditional shared memory handled by shmem.c and dynamic shared
+ * memory is that traditional shared memory areas are mapped to the same
+ * address in all processes, so you can use normal pointers in shared memory
+ * structs.  With Dynamic Shared Memory, you must use offsets or DSA pointers
+ * instead.
+ *
+ * Shared memory managed by shmem.c can never be freed, once allocated.  Each
+ * hash table has its own free list, so hash buckets can be reused when an
+ * item is deleted.  However, if one hash table grows very large and then
+ * shrinks, its space cannot be redistributed to other tables.  We could build
+ * a simple hash bucket garbage collector if need be.  Right now, it seems
+ * unnecessary.
+ *
+ * Usage
+ * -----
+ *
+ * To allocate shared memory, you need to register a set of callback functions
+ * which handle the lifecycle of the allocation.  In the register_fn
+ * callback, fill in a ShmemStructDesc descriptor with the name, size, and any
+ * other options, and call ShmemRequestStruct().  Leave any unused fields as
+ * zeros.
+ *
+ *	typedef struct MyShmemData {
+ *		...
+ *	} MyShmemData;
+ *
+ *	static MyShmemData *MyShmem;
+ *
+ *	static void my_shmem_request(void *arg);
+ *	static void my_shmem_init(void *arg);
+ *
+ *  const ShmemCallbacks MyShmemCallbacks = {
+ *		.request_fn = my_shmem_request,
+ *		.init_fn = my_shmem_init,
+ *	};
+ *
+ *	static void
+ *	my_shmem_request(void *arg)
+ *	{
+ *		static ShmemStructDesc MyShmemDesc;
+ *
+ *		ShmemRequestStruct(&MyShmemDesc, &(ShmemRequestStructOpts) {
+ *			.name = "My shmem area",
+ *			.size = sizeof(MyShmemData),
+ *			.ptr = (void **) &MyShmem,
+ *		});
+ *	}
+ *
+ * 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
+ * ---------
+ *
+ * Initializing shared memory happens in multiple phases. In the first phase,
+ * during postmaster startup, all the shmem_request callbacks are called.
+ * Only after all the request callbacks have been called and all the shmem
+ * areas have been requested by the ShmemRequestStruct() calls we know how
+ * much shared memory we need in total. After that, postmaster allocates
+ * global shared memory segment, and calls all the init_fn callbacks to
+ * initialize all the requested shmem areas.
+ *
+ * In standard Unix-ish environments, individual backends do not need to
+ * re-establish their local pointers into shared memory, because they inherit
+ * correct values of those variables via fork() from the postmaster.  However,
+ * this does not work in the EXEC_BACKEND case.  In ports using EXEC_BACKEND,
+ * backend startup also calls the shmem_request callbacks to re-establish the
+ * knowledge about each shared memory area, sets the pointer variables
+ * (*ShmemStructDesc->ptr), and calls the attach_fn callback, if any, for
+ * additional per-backend setup.
+ *
+ * Legacy ShmemInitStruct()/ShmemInitHash() functions
+ * --------------------------------------------------
+ *
+ * ShmemInitStruct()/ShmemInitHash() is another way of registering shmem
+ * areas.  It pre-dates the ShmemRequestStruct()/ShmemRequestHash() functions,
+ * and should not be used in new code, but as of this writing it is still
+ * widely used in extensions.
+ *
+ * To allocate a shmem area with ShmemInitStruct(), you need to separately
+ * register the size needed for the area by calling RequestAddinShmemSpace()
+ * from the extension's shmem_request_hook, and allocate the area by calling
+ * ShmemInitStruct() from the extension's shmem_startup_hook.  There are no
+ * init/attach callbacks.  Instead, the caller of ShmemInitStruct() must check
+ * the return status of ShmemInitStruct() and initialize the struct if it was
+ * not previously initialized.
+ *
+ * Calling ShmemAlloc() directly
+ * -----------------------------
+ *
+ * There's a more low-level way of allocating shared memory too: you can call
+ * ShmemAlloc() directly.  It's used to implement the higher level mechanisms,
+ * and should generally not be called directly.
  */
 
 #include "postgres.h"
@@ -79,6 +147,74 @@
 #include "utils/builtins.h"
 #include "utils/tuplestore.h"
 
+/*
+ * Registered callbacks.
+ *
+ * During postmaster startup, we accumulate the callbacks from all subsystems
+ * in this list.
+ *
+ * This is in process private memory, although on Unix-like systems, we expect
+ * all the registrations to happen at postmaster startup time and be inherited
+ * by all the child processes via fork().
+ */
+static List *registered_shmem_callbacks;
+
+/*
+ * In the shmem request phase, all the shmem areas requested with
+ * ShmemRequestInternal() are accumulated here.
+ */
+typedef struct
+{
+	ShmemStructDesc *desc;
+	ShmemRequestStructOpts *options;
+	ShmemAreaKind kind;
+} ShmemRequest;
+
+static List *requested_shmem_areas;
+
+/*
+ * Per-process state machine, for sanity checking that we do things in the
+ * right order.
+ *
+ * Postmaster:
+ *   INITIAL -> REQUESTING -> INITIALIZING -> DONE
+ *
+ * Backends in EXEC_BACKEND mode:
+ *   INITIAL -> REQUESTING -> ATTACHING -> DONE
+ *
+ * Late request:
+ *   DONE -> REQUESTING -> LATE_ATTACH_OR_INIT -> DONE
+ */
+static enum
+{
+	/* Initial state */
+	SB_INITIAL,
+
+	/*
+	 * When we call the shmem_request callbacks, we enter the SB_REQUESTING
+	 * phase. All ShmemRequestStruct calls happen in this state.
+	 */
+	SB_REQUESTING,
+
+	/*
+	 * Postmaster has finished all shmem requests, and is now initializing the
+	 * shared memory segment. We are now calling the init_fn callbacks.
+	 */
+	SB_INITIALIZING,
+
+	/*
+	 * A postmaster child process is starting up. The attach_fn callbacks are
+	 * called in this state.
+	 */
+	SB_ATTACHING,
+
+	/* An after-startup allocation or attachment is in progress. */
+	SB_LATE_ATTACH_OR_INIT,
+
+	/* Normal state after shmem initialization / attachment */
+	SB_DONE,
+}			shmem_startup_state;
+
 /*
  * This is the first data structure stored in the shared memory segment, at
  * the offset that PGShmemHeader->content_offset points to.  Allocations by
@@ -109,25 +245,373 @@ static void *ShmemBase;			/* start address of shared memory */
 static void *ShmemEnd;			/* end+1 address of shared memory */
 
 static ShmemAllocatorData *ShmemAllocator;
-static HTAB *ShmemIndex = NULL; /* primary index hashtable for shmem */
+
+/*
+ * ShmemIndex is a global directory of shmem areas, itself also stored in the
+ * shared memory.
+ */
+static HTAB *ShmemIndex;
+
+ /* max size of data structure string name */
+#define SHMEM_INDEX_KEYSIZE		 (48)
+
+/*
+ * # of additional entries to reserve in the shmem index table, for
+ * allocations after postmaster startup.  (This is not a hard limit, the hash
+ * table can grow larger than that if there is shared memory available)
+ */
+#define SHMEM_INDEX_ADDITIONAL_SIZE		 (64)
+
+/* this is a hash bucket in the shmem index table */
+typedef struct
+{
+	char		key[SHMEM_INDEX_KEYSIZE];	/* string name */
+	void	   *location;		/* location in shared mem */
+	Size		size;			/* # bytes requested for the structure */
+	Size		allocated_size; /* # bytes actually allocated */
+} ShmemIndexEnt;
 
 /* To get reliable results for NUMA inquiry we need to "touch pages" once */
 static bool firstNumaTouch = true;
 
+static bool AttachOrInit(ShmemRequest *request, bool init_allowed, bool attach_allowed);
+
 Datum		pg_numa_available(PG_FUNCTION_ARGS);
 
+/*
+ *	ShmemRequestStruct() --- request a named shared memory area
+ *
+ * Subsystems call this to register their shared memory needs.  This is
+ * usually done early in postmaster startup, before the shared memory segment
+ * has been created, so that the size can be included in the estimate for
+ * total amount of shared memory needed.  We set aside a small amount of
+ * memory for allocations that happen later, for the benefit of non-preloaded
+ * extensions, but that should not be relied upon.
+ *
+ * This does not yet allocate the memory, but merely register the need for it.
+ * The actual allocation happens later in the postmaster startup sequence.
+ *
+ * This must be called from a shmem_request callback function, registered with
+ * RegisterShmemCallbacks().  This enforces a coding pattern that works the
+ * same in normal Unix systems and with EXEC_BACKEND.  In postmaster, the the
+ * shmem_request callback is called during startup, but in EXEC_BACKEND mode,
+ * it is also called in each backend at backend startup.  By calling the same
+ * function in both cases, we ensure that all the shmem areas are registered
+ * the same way in all processes.
+ *
+ * 'desc' is a backend-private handle for the shared memory area.
+ *
+ * 'options' defines the name and size of the area, and any other optional
+ * features. Leave unused options as zeros.  The options are copied to
+ * longer-lived memory, so it doesn't need to live after the
+ * ShmemRequestStruct() call and can point to a local variable in the calling
+ * function. The 'name' must point to a long-lived string though, only the
+ * pointer to it is copied.
+ */
+void
+ShmemRequestStruct(ShmemStructDesc *desc, const ShmemRequestStructOpts *options)
+{
+	ShmemRequestStructOpts *options_copy;
+
+	options_copy = MemoryContextAlloc(TopMemoryContext,
+									  sizeof(ShmemRequestStructOpts));
+	memcpy(options_copy, options, sizeof(ShmemRequestStructOpts));
+
+	ShmemRequestInternal(desc, options_copy, SHMEM_KIND_STRUCT);
+}
+
+/*
+ * Internal workhorse of ShmemRequestStruct() and ShmemRequestHash().
+ *
+ * Note: 'desc' and 'options' must live until the init/attach callbacks have
+ * been called.  Unlike in the public ShmemRequestStruct() and
+ * ShmemRequestHash() functions, 'options' is *not* copied.  This allows
+ * ShmemRequestHash() to pass a pointer to the extended ShmemRequestHashOpts
+ * struct instead.
+ */
+void
+ShmemRequestInternal(ShmemStructDesc *desc, ShmemRequestStructOpts *options,
+					 ShmemAreaKind kind)
+{
+	ListCell   *lc;
+	ShmemRequest *request;
+
+	if (options->name == NULL)
+		elog(ERROR, "shared memory request is missing 'name' option");
+
+	if (IsUnderPostmaster)
+	{
+		if (options->size <= 0 && options->size != SHMEM_REQUEST_UNKNOWN_SIZE)
+			elog(ERROR, "invalid size %zd for shared memory request for \"%s\"",
+				 options->size, options->name);
+	}
+	else
+	{
+		if (options->size == SHMEM_REQUEST_UNKNOWN_SIZE)
+			elog(ERROR, "SHMEM_REQUEST_UNKNOWN_SIZE cannot be used during startup");
+		if (options->size <= 0)
+			elog(ERROR, "invalid size %zd for shared memory request for \"%s\"",
+				 options->size, options->name);
+	}
+
+	if (shmem_startup_state != SB_REQUESTING)
+		elog(ERROR, "ShmemRequestStruct can only be called from a shmem_request callback");
+
+	/* Check that it's not already registered in this process */
+	foreach(lc, requested_shmem_areas)
+	{
+		ShmemStructDesc *existing = (ShmemStructDesc *) lfirst(lc);
+
+		if (strcmp(existing->name, options->name) == 0)
+			ereport(ERROR,
+					(errmsg("shared memory struct \"%s\" is already registered",
+							options->name)));
+	}
+
+	request = palloc(sizeof(ShmemRequest));
+	request->options = options;
+	request->desc = desc;
+	request->kind = kind;
+	requested_shmem_areas = lappend(requested_shmem_areas, request);
+}
+
+/*
+ *	ShmemGetRequestedSize() --- estimate the total size of all registered shared
+ *                              memory structures.
+ *
+ * This is called once at postmaster startup, before the shared memory segment
+ * has been created.
+ */
+size_t
+ShmemGetRequestedSize(void)
+{
+	ListCell   *lc;
+	size_t		size;
+
+	/* memory needed for the ShmemIndex */
+	size = hash_estimate_size(list_length(requested_shmem_areas) + SHMEM_INDEX_ADDITIONAL_SIZE,
+							  sizeof(ShmemIndexEnt));
+
+	/* memory needed for all the requested areas */
+	foreach(lc, requested_shmem_areas)
+	{
+		ShmemRequest *request = (ShmemRequest *) lfirst(lc);
+
+		size = add_size(size, request->options->size);
+		size = add_size(size, request->options->extra_size);
+	}
+
+	return size;
+}
+
+/*
+ *	ShmemInitRequested() --- allocate and initialize requested shared memory
+ *                            structures.
+ *
+ * This is called once at postmaster startup, after the shared memory segment
+ * has been created.
+ */
+void
+ShmemInitRequested(void)
+{
+	ListCell   *lc;
+
+	/* Should be called only by the postmaster or a standalone backend. */
+	Assert(!IsUnderPostmaster);
+	Assert(shmem_startup_state == SB_INITIALIZING);
+
+	/*
+	 * Initialize all the requested memory areas.  There are no concurrent
+	 * processes yet, so no need for locking.
+	 */
+	foreach(lc, requested_shmem_areas)
+	{
+		ShmemRequest *request = (ShmemRequest *) lfirst(lc);
+
+		AttachOrInit(request, true, false);
+	}
+	list_free_deep(requested_shmem_areas);
+	requested_shmem_areas = NIL;
+
+	/* Call init callbacks */
+	foreach(lc, registered_shmem_callbacks)
+	{
+		const ShmemCallbacks *callbacks = (const ShmemCallbacks *) lfirst(lc);
+
+		if (callbacks->init_fn)
+			callbacks->init_fn(callbacks->init_fn_arg);
+	}
+
+	shmem_startup_state = SB_DONE;
+}
+
+/*
+ * Re-establish process private state related to shmem areas.
+ *
+ * This is called at backend startup in EXEC_BACKEND mode, in every backend.
+ */
+#ifdef EXEC_BACKEND
+void
+ShmemAttachRequested(void)
+{
+	ListCell   *lc;
+
+	/* Must be initializing a (non-standalone) backend */
+	Assert(IsUnderPostmaster);
+	Assert(ShmemAllocator->index != NULL);
+	Assert(shmem_startup_state == SB_REQUESTING);
+	shmem_startup_state = SB_ATTACHING;
+
+	LWLockAcquire(ShmemIndexLock, LW_SHARED);
+
+	/*
+	 * Attach to all the requested memory areas.
+	 */
+	foreach(lc, requested_shmem_areas)
+	{
+		ShmemRequest *request = (ShmemRequest *) lfirst(lc);
+
+		AttachOrInit(request, false, true);
+	}
+	list_free(requested_shmem_areas);
+	requested_shmem_areas = NIL;
+
+	/* Call attach callbacks */
+	foreach(lc, registered_shmem_callbacks)
+	{
+		const ShmemCallbacks *callbacks = (const ShmemCallbacks *) lfirst(lc);
+
+		if (callbacks->attach_fn)
+			callbacks->attach_fn(callbacks->attach_fn_arg);
+	}
+
+	LWLockRelease(ShmemIndexLock);
+
+	shmem_startup_state = SB_DONE;
+}
+#endif
+
+/*
+ * Workhorse to insert or look up a named shmem area in the shared memory
+ * index, and initialize or attach to it.
+ *
+ * If !init_allowed and the entry is not found, throws an error.  If
+ * !attach_allowed and the entry is found, throws an error.
+ */
+static bool
+AttachOrInit(ShmemRequest *request, bool init_allowed, bool attach_allowed)
+{
+	/*
+	 * If called after postmaster startup, we need to immediately also
+	 * initialize or attach to the area.
+	 */
+	ShmemStructDesc *desc = request->desc;
+	ShmemIndexEnt *index_entry;
+	bool		found;
+
+	desc->name = request->options->name;
+	desc->ptr = NULL;
+
+	/* look it up in the shmem index */
+	index_entry = (ShmemIndexEnt *)
+		hash_search(ShmemIndex, request->options->name,
+					init_allowed ? HASH_ENTER_NULL : HASH_FIND, &found);
+	if (found)
+	{
+		/* Already present, just attach to it */
+		if (!attach_allowed)
+			elog(ERROR, "shared memory struct \"%s\" is already initialized", desc->name);
+
+		if (index_entry->size != request->options->size &&
+			request->options->size != SHMEM_REQUEST_UNKNOWN_SIZE)
+		{
+			elog(ERROR, "shared memory struct \"%s\" is already registered with different size",
+				 desc->name);
+		}
+		desc->ptr = index_entry->location;
+		desc->size = index_entry->size;
+		switch (request->kind)
+		{
+			case SHMEM_KIND_STRUCT:
+				if (request->options->ptr)
+					*(request->options->ptr) = index_entry->location;
+				break;
+			case SHMEM_KIND_HASH:
+				shmem_hash_attach(desc, request->options);
+				break;
+		}
+	}
+	else if (!init_allowed)
+	{
+		/* attach was requested, but it was not found */
+		ereport(ERROR,
+				(errcode(ERRCODE_OUT_OF_MEMORY),
+				 errmsg("could not find ShmemIndex entry for data structure \"%s\"",
+						desc->name)));
+	}
+	else if (!index_entry)
+	{
+		/* tried to add it to the hash table, but there was no space */
+		ereport(ERROR,
+				(errcode(ERRCODE_OUT_OF_MEMORY),
+				 errmsg("could not create ShmemIndex entry for data structure \"%s\"",
+						desc->name)));
+	}
+	else
+	{
+		/*
+		 * We inserted the entry to the shared memory index. Allocate
+		 * requested amount of shared memory for it, and do basic
+		 * initializion.
+		 */
+		size_t		allocated_size;
+		void	   *structPtr;
+
+		structPtr = ShmemAllocRaw(request->options->size, &allocated_size);
+		if (structPtr == NULL)
+		{
+			/* out of memory; remove the failed ShmemIndex entry */
+			hash_search(ShmemIndex, desc->name, HASH_REMOVE, NULL);
+			ereport(ERROR,
+					(errcode(ERRCODE_OUT_OF_MEMORY),
+					 errmsg("not enough shared memory for data structure"
+							" \"%s\" (%zu bytes requested)",
+							desc->name, request->options->size)));
+		}
+		index_entry->size = request->options->size;
+		index_entry->allocated_size = allocated_size;
+		index_entry->location = structPtr;
+
+		desc->ptr = index_entry->location;
+		desc->size = index_entry->size;
+		switch (request->kind)
+		{
+			case SHMEM_KIND_STRUCT:
+				if (request->options->ptr)
+					*(request->options->ptr) = index_entry->location;
+				break;
+			case SHMEM_KIND_HASH:
+				shmem_hash_init(desc, request->options);
+				break;
+		}
+	}
+
+	return found;
+}
+
 /*
  *	InitShmemAllocator() --- set up basic pointers to shared memory.
  *
  * Called at postmaster or stand-alone backend startup, to initialize the
  * allocator's data structure in the shared memory segment.  In EXEC_BACKEND,
- * this is also called at backend startup, to set up pointers to the shared
- * memory areas.
+ * this is also called at backend startup, to set up pointers to the
+ * already-initialized data structure.
  */
 void
 InitShmemAllocator(PGShmemHeader *seghdr)
 {
 	Size		offset;
+	int64		hash_size;
 	HASHCTL		info;
 	int			hash_flags;
 	size_t		size;
@@ -137,6 +621,16 @@ InitShmemAllocator(PGShmemHeader *seghdr)
 #endif
 	Assert(seghdr != NULL);
 
+	if (IsUnderPostmaster)
+	{
+		Assert(shmem_startup_state == SB_INITIAL);
+	}
+	else
+	{
+		Assert(shmem_startup_state == SB_REQUESTING);
+		shmem_startup_state = SB_INITIALIZING;
+	}
+
 	/*
 	 * We assume the pointer and offset are MAXALIGN.  Not a hard requirement,
 	 * but it's true today and keeps the math below simpler.
@@ -181,9 +675,11 @@ InitShmemAllocator(PGShmemHeader *seghdr)
 	 * use ShmemInitHash() here because it relies on ShmemIndex being already
 	 * initialized.
 	 */
+	hash_size = list_length(requested_shmem_areas) + SHMEM_INDEX_ADDITIONAL_SIZE;
+
 	info.keysize = SHMEM_INDEX_KEYSIZE;
 	info.entrysize = sizeof(ShmemIndexEnt);
-	info.dsize = info.max_dsize = hash_select_dirsize(SHMEM_INDEX_SIZE);
+	info.dsize = info.max_dsize = hash_select_dirsize(hash_size);
 	info.alloc = ShmemAllocNoError;
 	hash_flags = HASH_ELEM | HASH_STRINGS | HASH_SHARED_MEM | HASH_ALLOC | HASH_DIRSIZE;
 	if (!IsUnderPostmaster)
@@ -194,10 +690,26 @@ InitShmemAllocator(PGShmemHeader *seghdr)
 	else
 		hash_flags |= HASH_ATTACH;
 	info.hctl = ShmemAllocator->index;
-	ShmemIndex = hash_create("ShmemIndex", SHMEM_INDEX_SIZE, &info, hash_flags);
+	ShmemIndex = hash_create("ShmemIndex", hash_size, &info, hash_flags);
 	Assert(ShmemIndex != NULL);
 }
 
+/*
+ * Reset state on postmaster crash restart.
+ */
+void
+ResetShmemAllocator(void)
+{
+	Assert(!IsUnderPostmaster);
+	shmem_startup_state = SB_INITIAL;
+	requested_shmem_areas = NIL;
+
+	/*
+	 * Note that we don't clear the registered callbacks.  We will need to
+	 * call them again as we restart
+	 */
+}
+
 /*
  * ShmemAlloc -- allocate max-aligned chunk from shared memory
  *
@@ -295,92 +807,128 @@ ShmemAddrIsValid(const void *addr)
 }
 
 /*
- * ShmemInitStruct -- Create/attach to a structure in shared memory.
- *
- *		This is called during initialization to find or allocate
- *		a data structure in shared memory.  If no other process
- *		has created the structure, this routine allocates space
- *		for it.  If it exists already, a pointer to the existing
- *		structure is returned.
+ * Register callbacks that define a shared memory area (or multiple areas).
  *
- *	Returns: pointer to the object.  *foundPtr is set true if the object was
- *		already in the shmem index (hence, already initialized).
+ * The system will call the callbacks at different stages of postmaster or
+ * backend startup, to allocate and initialize the area.
  *
- *	Note: before Postgres 9.0, this function returned NULL for some failure
- *	cases.  Now, it always throws error instead, so callers need not check
- *	for NULL.
+ * This is normally called early during postmaster startup, but if the
+ * SHMEM_ALLOW_AFTER_STARTUP is set, this can also be used after startup,
+ * although after startup there's no guarantee that there's enough shared
+ * memory available.  When called after startup, this immediately calls the
+ * right callbacks depending on whether another backend had already
+ * initialized the area.
  */
-void *
-ShmemInitStruct(const char *name, Size size, bool *foundPtr)
+void
+RegisterShmemCallbacks(const ShmemCallbacks *callbacks)
 {
-	ShmemIndexEnt *result;
-	void	   *structPtr;
+	if (shmem_startup_state == SB_DONE && IsUnderPostmaster)
+	{
+		/* After-startup initialization */
+		ListCell   *lc;
+		bool		found = false;
 
-	Assert(ShmemIndex != NULL);
+		if ((callbacks->flags & SHMEM_ALLOW_AFTER_STARTUP) == 0)
+			elog(ERROR, "cannot request shared memory at this time");
 
-	LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE);
+		Assert(requested_shmem_areas == NIL);
+		Assert(shmem_startup_state == SB_DONE);
+		shmem_startup_state = SB_REQUESTING;
+		if (callbacks->request_fn)
+			callbacks->request_fn(callbacks->request_fn_arg);
+		shmem_startup_state = SB_LATE_ATTACH_OR_INIT;
 
-	/* look it up in the shmem index */
-	result = (ShmemIndexEnt *)
-		hash_search(ShmemIndex, name, HASH_ENTER_NULL, foundPtr);
+		LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE);
 
-	if (!result)
-	{
-		LWLockRelease(ShmemIndexLock);
-		ereport(ERROR,
-				(errcode(ERRCODE_OUT_OF_MEMORY),
-				 errmsg("could not create ShmemIndex entry for data structure \"%s\"",
-						name)));
-	}
+		foreach(lc, requested_shmem_areas)
+		{
+			ShmemRequest *request = (ShmemRequest *) lfirst(lc);
+
+			found = AttachOrInit(request, true, true);
+		}
 
-	if (*foundPtr)
-	{
 		/*
-		 * Structure is in the shmem index so someone else has allocated it
-		 * already.  The size better be the same as the size we are trying to
-		 * initialize to, or there is a name conflict (or worse).
+		 * FIXME: What to do if multiple shmem areas were requested, and some
+		 * of them are already initialized but not all?  We expect all shmem
+		 * areas requested by a single callback to form a coherent unit.
 		 */
-		if (result->size != size)
+		if (found)
 		{
-			LWLockRelease(ShmemIndexLock);
-			ereport(ERROR,
-					(errmsg("ShmemIndex entry size is wrong for data structure"
-							" \"%s\": expected %zu, actual %zu",
-							name, size, result->size)));
+			if (callbacks->attach_fn)
+				callbacks->attach_fn(callbacks->attach_fn_arg);
 		}
-		structPtr = result->location;
-	}
-	else
-	{
-		Size		allocated_size;
-
-		/* It isn't in the table yet. allocate and initialize it */
-		structPtr = ShmemAllocRaw(size, &allocated_size);
-		if (structPtr == NULL)
+		else
 		{
-			/* out of memory; remove the failed ShmemIndex entry */
-			hash_search(ShmemIndex, name, HASH_REMOVE, NULL);
-			LWLockRelease(ShmemIndexLock);
-			ereport(ERROR,
-					(errcode(ERRCODE_OUT_OF_MEMORY),
-					 errmsg("not enough shared memory for data structure"
-							" \"%s\" (%zu bytes requested)",
-							name, size)));
+			if (callbacks->init_fn)
+				callbacks->init_fn(callbacks->init_fn_arg);
 		}
-		result->size = size;
-		result->allocated_size = allocated_size;
-		result->location = structPtr;
+
+		LWLockRelease(ShmemIndexLock);
+		shmem_startup_state = SB_DONE;
+		return;
 	}
 
-	LWLockRelease(ShmemIndexLock);
+	registered_shmem_callbacks = lappend(registered_shmem_callbacks,
+										 (void *) callbacks);
+}
+
+/*
+ * Call all shmem request callbacks.
+ */
+void
+ShmemCallRequestCallbacks(void)
+{
+	ListCell   *lc;
 
-	Assert(ShmemAddrIsValid(structPtr));
+	Assert(shmem_startup_state == SB_INITIAL);
+	shmem_startup_state = SB_REQUESTING;
 
-	Assert(structPtr == (void *) CACHELINEALIGN(structPtr));
+	foreach(lc, registered_shmem_callbacks)
+	{
+		const ShmemCallbacks *callbacks = (const ShmemCallbacks *) lfirst(lc);
 
-	return structPtr;
+		if (callbacks->request_fn)
+			callbacks->request_fn(callbacks->request_fn_arg);
+	}
 }
 
+/*
+ * ShmemInitStruct -- Create/attach to a structure in shared memory.
+ *
+ *		This is called during initialization to find or allocate
+ *		a data structure in shared memory.  If no other process
+ *		has created the structure, this routine allocates space
+ *		for it.  If it exists already, a pointer to the existing
+ *		structure is returned.
+ *
+ *	Returns: pointer to the object.  *foundPtr is set true if the object was
+ *		already in the shmem index (hence, already initialized).
+ *
+ * Note: This is a legacy interface, kept for backwards compatibility with
+ * extensions.  Use ShmemRequestStruct() in new code!
+ */
+void *
+ShmemInitStruct(const char *name, Size size, bool *foundPtr)
+{
+	ShmemStructDesc desc;
+	ShmemRequestStructOpts options = {
+		.name = name,
+		.size = size,
+	};
+	ShmemRequest request = {&desc, &options, SHMEM_KIND_STRUCT};
+
+	Assert(shmem_startup_state == SB_DONE ||
+		   shmem_startup_state == SB_INITIALIZING ||
+		   shmem_startup_state == SB_REQUESTING);
+
+	/* look it up immediately */
+	LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE);
+	*foundPtr = AttachOrInit(&request, true, true);
+	LWLockRelease(ShmemIndexLock);
+
+	Assert(desc.ptr != NULL);
+	return desc.ptr;
+}
 
 /*
  * Add two Size values, checking for overflow
diff --git a/src/backend/storage/ipc/shmem_hash.c b/src/backend/storage/ipc/shmem_hash.c
index b0c8d5939a0..48bb5d97c1a 100644
--- a/src/backend/storage/ipc/shmem_hash.c
+++ b/src/backend/storage/ipc/shmem_hash.c
@@ -1,11 +1,17 @@
 /*-------------------------------------------------------------------------
  *
  * shmem_hash.c
- *	  XXX
+ *	  hash table implementation in shared memory
  *
  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
+ * A shared memory hash table implementation on top of the named, fixed-size
+ * shared memory areas managed by shmem.c.  Hash tables have a fixed maximum
+ * size, but their actual size can vary dynamically.  When entries are added
+ * to the table, more space is allocated.  Each shared data structure and hash
+ * has a string name to identify it, specified in its descriptor when its
+ * requested.
  *
  * IDENTIFICATION
  *	  src/backend/storage/ipc/shmem_hash.c
@@ -16,6 +22,85 @@
 #include "postgres.h"
 
 #include "storage/shmem.h"
+#include "utils/memutils.h"
+
+/*
+ * ShmemRequestHash -- Request a shared memory hash table.
+ *
+ * Similar to ShmemRequestStruct(), but requests a hash table instead of an
+ * opaque area.
+ */
+void
+ShmemRequestHash(ShmemHashDesc *desc, const ShmemRequestHashOpts *options)
+{
+	ShmemRequestHashOpts *options_copy;
+	int64		dirsize;
+
+	Assert(options->name != NULL);
+
+	options_copy = MemoryContextAlloc(TopMemoryContext,
+									  sizeof(ShmemRequestHashOpts));
+	memcpy(options_copy, options, sizeof(ShmemRequestHashOpts));
+
+	/*
+	 * Hash tables allocated in shared memory have a fixed directory; it can't
+	 * grow or other backends wouldn't be able to find it. So, make sure we
+	 * make it big enough to start with.
+	 *
+	 * The shared memory allocator must be specified too.
+	 */
+	dirsize = hash_select_dirsize(options->max_size);
+	options_copy->hash_info.dsize = dirsize;
+	options_copy->hash_info.max_dsize = dirsize;
+	options_copy->hash_info.alloc = ShmemAllocNoError;
+	options_copy->hash_flags |= HASH_SHARED_MEM | HASH_ALLOC | HASH_DIRSIZE;
+
+	/*
+	 * Create a struct descriptor for the fixed-size area holding the hash
+	 * table
+	 */
+	options_copy->base.name = options->name;
+	options_copy->base.size = hash_get_shared_size(&options_copy->hash_info,
+												   options_copy->hash_flags);
+
+	/* Reserve extra space for the buckets */
+	options_copy->base.extra_size =
+		hash_estimate_size(options->max_size, options_copy->hash_info.entrysize) - options_copy->base.size;
+
+	ShmemRequestInternal(&desc->base, &options_copy->base, SHMEM_KIND_HASH);
+}
+
+void
+shmem_hash_init(ShmemStructDesc *base_desc, const ShmemRequestStructOpts *base_options)
+{
+	ShmemHashDesc *desc = (ShmemHashDesc *) base_desc;
+	ShmemRequestHashOpts *options = (ShmemRequestHashOpts *) base_options;
+	int			hash_flags = options->hash_flags;
+
+	options->hash_info.hctl = desc->base.ptr;
+	Assert(options->hash_info.hctl != NULL);
+	desc->ptr = hash_create(desc->base.name, options->init_size, &options->hash_info, hash_flags);
+
+	if (options->ptr)
+		*options->ptr = desc->ptr;
+}
+
+void
+shmem_hash_attach(ShmemStructDesc *base_desc, const ShmemRequestStructOpts *base_options)
+{
+	ShmemHashDesc *desc = (ShmemHashDesc *) base_desc;
+	ShmemRequestHashOpts *options = (ShmemRequestHashOpts *) base_options;
+	int			hash_flags = options->hash_flags;
+
+	/* attach to it rather than allocate and initialize new space */
+	hash_flags |= HASH_ATTACH;
+	options->hash_info.hctl = desc->base.ptr;
+	Assert(options->hash_info.hctl != NULL);
+	desc->ptr = hash_create(desc->base.name, options->init_size, &options->hash_info, hash_flags);
+
+	if (options->ptr)
+		*options->ptr = desc->ptr;
+}
 
 
 /*
@@ -41,9 +126,8 @@
  * to shared-memory hash tables are added here, except that callers may
  * choose to specify HASH_PARTITION and/or HASH_FIXED_SIZE.
  *
- * Note: before Postgres 9.0, this function returned NULL for some failure
- * cases.  Now, it always throws error instead, so callers need not check
- * for NULL.
+ * Note: This is a legacy interface, kept for backwards compatibility with
+ * extensions.  Use ShmemRequestHash() in new code!
  */
 HTAB *
 ShmemInitHash(const char *name,		/* table string name for shmem index */
diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index 5c47cf13473..9b880a6af65 100644
--- a/src/backend/storage/lmgr/proc.c
+++ b/src/backend/storage/lmgr/proc.c
@@ -121,6 +121,9 @@ FastPathLockShmemSize(void)
 
 	size = add_size(size, mul_size(TotalProcs, (fpLockBitsSize + fpRelIdSize)));
 
+	Assert(TotalProcs > 0);
+	Assert(size > 0);
+
 	return size;
 }
 
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index b3563113219..278d2f20376 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -4164,7 +4164,14 @@ PostgresSingleUserMain(int argc, char *argv[],
 	InitializeFastPathLocks();
 
 	/*
-	 * Give preloaded libraries a chance to request additional shared memory.
+	 * Before computing the total size needed, give all subsystems, including
+	 * add-ins, a chance to chance to adjust their requested shmem sizes.
+	 */
+	ShmemCallRequestCallbacks();
+
+	/*
+	 * Also call any legacy shmem request hooks that might'be been installed
+	 * by preloaded libraries.
 	 */
 	process_shmem_requests();
 
diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c
index 11f5778eba1..b40e14abace 100644
--- a/src/backend/utils/hash/dynahash.c
+++ b/src/backend/utils/hash/dynahash.c
@@ -396,6 +396,8 @@ hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
 	}
 
 	/* Initialize the hash header, plus a copy of the table name */
+	Assert(tabname != NULL);
+	Assert(CurrentDynaHashCxt != NULL);
 	hashp = (HTAB *) MemoryContextAlloc(CurrentDynaHashCxt,
 										sizeof(HTAB) + strlen(tabname) + 1);
 	MemSet(hashp, 0, sizeof(HTAB));
diff --git a/src/include/storage/shmem.h b/src/include/storage/shmem.h
index 2a9e9becd26..a07fccfb1ba 100644
--- a/src/include/storage/shmem.h
+++ b/src/include/storage/shmem.h
@@ -24,17 +24,212 @@
 #include "storage/spin.h"
 #include "utils/hsearch.h"
 
+/* Different kinds of shmem areas. */
+typedef enum
+{
+	SHMEM_KIND_STRUCT = 0,		/* plain, contiguous area of memory */
+	SHMEM_KIND_HASH,			/* a hash table */
+} ShmemAreaKind;
+
+/*
+ * ShmemStructDesc describes a named area or struct in shared memory.
+ *
+ * 'name' and 'size' are required.  Initialize any optional fields that you
+ * don't use to zeros.
+ *
+ * After registration, the shmem machinery reserves memory for the area, sets
+ * '*ptr' to point to the allocation, and calls the callbacks at the right
+ * moments.
+ */
+typedef struct ShmemStructDesc
+{
+	/* Name and size of the shared memory area. */
+	const char *name;
+
+	void	   *ptr;
+	size_t		size;
+} ShmemStructDesc;
+
+#define SHMEM_REQUEST_UNKNOWN_SIZE (-1)
+
+typedef struct ShmemRequestStructOpts
+{
+	const char *name;
+
+	ssize_t		size;
+
+	/*
+	 * Extra space to reserve in the shared memory segment, but it's not part
+	 * of the struct itself.  This is used for shared memory hash tables that
+	 * can grow beyond the initial size when more buckets are allocated.
+	 */
+	size_t		extra_size;
+
+	/*
+	 * When the shmem area is initialized or attached to, pointer to it is
+	 * stored in *ptr.  It usually points to a global variable, used to access
+	 * the shared memory area later.  *ptr is set before the init_fn or
+	 * attach_fn callback is called.
+	 */
+	void	  **ptr;
+} ShmemRequestStructOpts;
+
+typedef struct ShmemHashDesc
+{
+	ShmemStructDesc base;
+
+	/*
+	 * When the hash table is initialized or attached to, pointer to its
+	 * backend-private handle is stored in *ptr.  It usually points to a
+	 * global variable, used to access the hash table later.
+	 */
+	HTAB	   *ptr;
+} ShmemHashDesc;
+
+/*
+ * Descriptor for a named shared memory hash table.
+ *
+ * Similar to ShmemStructDesc, but describes a shared memory hash table.  Each
+ * hash table is backed by an allocated area, described by 'base_desc', but if
+ * 'max_size' is greater than 'init_size', it can also grow beyond the initial
+ * allocated area by allocating more hash entries from the global unreserved
+ * space.
+ */
+typedef struct ShmemRequestHashOpts
+{
+	ShmemRequestStructOpts base;
+
+	/*
+	 * Name of the shared memory area.  Required.  Must be unique across the
+	 * system.
+	 */
+	const char *name;
+
+	/*
+	 * max_size is the estimated maximum number of hashtable entries.  This is
+	 * not a hard limit, but the access efficiency will degrade if it is
+	 * exceeded substantially (since it's used to compute directory size and
+	 * the hash table buckets will get overfull).
+	 */
+	size_t		max_size;
+
+	/*
+	 * init_size is the number of hashtable entries to preallocate.  For a
+	 * table whose maximum size is certain, this should be equal to max_size;
+	 * that ensures that no run-time out-of-shared-memory failures can occur.
+	 */
+	size_t		init_size;
+
+	/*
+	 * Hash table options passed to hash_create()
+	 *
+	 * hash_info and hash_flags must specify at least the entry sizes and key
+	 * comparison semantics (see hash_create()).  Flag bits and values
+	 * specific to shared-memory hash tables are added implicitly in
+	 * ShmemRequestHash(), except that callers may choose to specify
+	 * HASH_PARTITION and/or HASH_FIXED_SIZE.
+	 */
+	HASHCTL		hash_info;
+	int			hash_flags;
+
+	/*
+	 * When the hash table is initialized or attached to, pointer to its
+	 * backend-private handle is stored in *ptr.  It usually points to a
+	 * global variable, used to access the hash table later.
+	 */
+	HTAB	  **ptr;
+} ShmemRequestHashOpts;
+
+
+typedef void (*ShmemRequestCallback) (void *arg);
+typedef void (*ShmemInitCallback) (void *arg);
+typedef void (*ShmemAttachCallback) (void *arg);
+
+/*
+ * Shared memory is reserved and allocated in stages at postmaster startup,
+ * and in EXEC_BACKEND mode, there's some extra work done to "attach" to them
+ * at backend startup.  ShmemCallbacks holds callback functions that are
+ * called at different stages.
+ */
+typedef struct ShmemCallbacks
+{
+	/* SHMEM_* flags */
+	int			flags;
+
+	/*
+	 * 'request_fn' is called during postmaster startup, before the shared
+	 * memory has been allocated.  The function should call
+	 * RequestShmemStruct() and RequestShmemHash() to register the subsystem's
+	 * shared memory needs.
+	 */
+	ShmemRequestCallback request_fn;
+	void	   *request_fn_arg;
+
+	/*
+	 * Initialization callback function.  This is called when the shared
+	 * memory area is allocated, usually at postmaster startup.
+	 */
+	ShmemInitCallback init_fn;
+	void	   *init_fn_arg;
+
+	/*
+	 * Attachment callback function.  In EXEC_BACKEND mode, this is called at
+	 * startup of each backend.  In !EXEC_BACKEND mode, this is only called if
+	 * the shared memory area is registered after postmaster startup (see
+	 * SHMEM_ALLOW_AFTER_STARTUP).
+	 */
+	ShmemAttachCallback attach_fn;
+	void	   *attach_fn_arg;
+} ShmemCallbacks;
+
+/*
+ * Allow these shared memory allocations after postmaster startup.  Normally,
+ * RegisterShmemCallbacks() errors out if it's called after postmaster startup
+ * e.g. in an add-in library loaded on-demaind in a backend.  If you set this
+ * flag, RegisterShmemCallbacks() will instead immediately call the callbacks,
+ * to initialize or attach to the requested shared memory areas.
+ *
+ * This is not used by any built-in subsystems, but extensions can find it
+ * useful.
+ */
+#define SHMEM_ALLOW_AFTER_STARTUP		0x00000001
 
 /* shmem.c */
 typedef struct PGShmemHeader PGShmemHeader; /* avoid including
 											 * storage/pg_shmem.h here */
+extern void ResetShmemAllocator(void);
 extern void InitShmemAllocator(PGShmemHeader *seghdr);
+#ifdef EXEC_BACKEND
+extern void AttachShmemAllocator(PGShmemHeader *seghdr);
+#endif
 extern void *ShmemAlloc(Size size);
 extern void *ShmemAllocNoError(Size size);
 extern bool ShmemAddrIsValid(const void *addr);
+
+extern void RegisterShmemCallbacks(const ShmemCallbacks *callbacks);
+
+extern void ShmemRequestInternal(ShmemStructDesc *desc, ShmemRequestStructOpts *options,
+								 ShmemAreaKind kind);
+
+extern void ShmemRequestStruct(ShmemStructDesc *desc, const ShmemRequestStructOpts *options);
+extern void ShmemRequestHash(ShmemHashDesc *desc, const ShmemRequestHashOpts *options);
+
+extern void ShmemCallRequestCallbacks(void);
+
+/* legacy shmem allocation functions */
 extern HTAB *ShmemInitHash(const char *name, int64 init_size, int64 max_size,
 						   HASHCTL *infoP, int hash_flags);
 extern void *ShmemInitStruct(const char *name, Size size, bool *foundPtr);
+
+extern size_t ShmemGetRequestedSize(void);
+extern void ShmemInitRequested(void);
+#ifdef EXEC_BACKEND
+extern void ShmemAttachRequested(void);
+#endif
+
+extern void shmem_hash_init(ShmemStructDesc *base_desc, const ShmemRequestStructOpts *options);
+extern void shmem_hash_attach(ShmemStructDesc *base_desc, const ShmemRequestStructOpts *options);
+
 extern Size add_size(Size s1, Size s2);
 extern Size mul_size(Size s1, Size s2);
 
@@ -43,19 +238,4 @@ extern PGDLLIMPORT Size pg_get_shmem_pagesize(void);
 /* ipci.c */
 extern void RequestAddinShmemSpace(Size size);
 
-/* size constants for the shmem index table */
- /* max size of data structure string name */
-#define SHMEM_INDEX_KEYSIZE		 (48)
- /* estimated size of the shmem index table (not a hard limit) */
-#define SHMEM_INDEX_SIZE		 (64)
-
-/* this is a hash bucket in the shmem index table */
-typedef struct
-{
-	char		key[SHMEM_INDEX_KEYSIZE];	/* string name */
-	void	   *location;		/* location in shared mem */
-	Size		size;			/* # bytes requested for the structure */
-	Size		allocated_size; /* # bytes actually allocated */
-} ShmemIndexEnt;
-
 #endif							/* SHMEM_H */
diff --git a/src/test/modules/test_aio/test_aio.c b/src/test/modules/test_aio/test_aio.c
index b1aa8af9ec0..d687408af0c 100644
--- a/src/test/modules/test_aio/test_aio.c
+++ b/src/test/modules/test_aio/test_aio.c
@@ -50,7 +50,6 @@ static InjIoErrorState *inj_io_error_state;
 static shmem_request_hook_type prev_shmem_request_hook = NULL;
 static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
 
-
 static PgAioHandle *last_handle;
 
 
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 712d84128ca..d8d2548ef2d 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2848,9 +2848,16 @@ SharedTypmodTableEntry
 Sharedsort
 ShellTypeInfo
 ShippableCacheEntry
-ShmemAllocatorData
 ShippableCacheKey
+ShmemAllocatorData
+ShmemAreaKind
+ShmemCallbacks
 ShmemIndexEnt
+ShmemHashDesc
+ShmemRequest
+ShmemRequestHashOpts
+ShmemRequestStructOpts
+ShmemStructDesc
 ShutdownForeignScan_function
 ShutdownInformation
 ShutdownMode
-- 
2.47.3



  [text/x-patch] v8-0007-Add-test-module-to-test-after-startup-shmem-alloc.patch (10.0K, 8-v8-0007-Add-test-module-to-test-after-startup-shmem-alloc.patch)
  download | inline diff:
From 351c752b3657c64cab938bf4f8a1fc3b908fcda9 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Sat, 21 Mar 2026 19:04:00 +0200
Subject: [PATCH v8 07/16] Add test module to test after-startup shmem
 allocations

None of the existing modules could make use of the lazy shmem
allocation after postmaster startup:

- pg_stat_statements needs to load and dump stats file on startup and
  shutdown, which doesn't really work if the library is not loaded into
  postmaster

- test_aio registers injection points, which reference the library
  itself, which creates a weird initialization loop if you try to do
  that directly from _PG_init() in a backend. The initialization
  really needs to happen after _PG_init()

- injection_points would be a candidate, but it already knows to use
  DSM when it's not loaded from shared_preload_libraries.
---
 src/test/modules/Makefile                     |   1 +
 src/test/modules/meson.build                  |   1 +
 src/test/modules/test_shmem/Makefile          |  24 ++++
 src/test/modules/test_shmem/meson.build       |  33 ++++++
 .../test_shmem/t/001_late_shmem_alloc.pl      |  43 +++++++
 .../modules/test_shmem/test_shmem--1.0.sql    |   9 ++
 src/test/modules/test_shmem/test_shmem.c      | 107 ++++++++++++++++++
 .../modules/test_shmem/test_shmem.control     |   3 +
 src/tools/pgindent/typedefs.list              |   1 +
 9 files changed, 222 insertions(+)
 create mode 100644 src/test/modules/test_shmem/Makefile
 create mode 100644 src/test/modules/test_shmem/meson.build
 create mode 100644 src/test/modules/test_shmem/t/001_late_shmem_alloc.pl
 create mode 100644 src/test/modules/test_shmem/test_shmem--1.0.sql
 create mode 100644 src/test/modules/test_shmem/test_shmem.c
 create mode 100644 src/test/modules/test_shmem/test_shmem.control

diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile
index 28ce3b35eda..62fab9f3c2f 100644
--- a/src/test/modules/Makefile
+++ b/src/test/modules/Makefile
@@ -47,6 +47,7 @@ SUBDIRS = \
 		  test_resowner \
 		  test_rls_hooks \
 		  test_saslprep \
+		  test_shmem \
 		  test_shm_mq \
 		  test_slru \
 		  test_tidstore \
diff --git a/src/test/modules/meson.build b/src/test/modules/meson.build
index 3ac291656c1..6799ba11e11 100644
--- a/src/test/modules/meson.build
+++ b/src/test/modules/meson.build
@@ -48,6 +48,7 @@ subdir('test_regex')
 subdir('test_resowner')
 subdir('test_rls_hooks')
 subdir('test_saslprep')
+subdir('test_shmem')
 subdir('test_shm_mq')
 subdir('test_slru')
 subdir('test_tidstore')
diff --git a/src/test/modules/test_shmem/Makefile b/src/test/modules/test_shmem/Makefile
new file mode 100644
index 00000000000..2407f7462fe
--- /dev/null
+++ b/src/test/modules/test_shmem/Makefile
@@ -0,0 +1,24 @@
+# src/test/modules/test_shmem/Makefile
+
+PGFILEDESC = "test_shmem - test code for shmem allocations"
+
+MODULE_big = test_shmem
+OBJS = \
+	$(WIN32RES) \
+	test_shmem.o
+
+EXTENSION = test_shmem
+DATA = test_shmem--1.0.sql
+
+TAP_TESTS = 1
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = src/test/modules/test_shmem
+top_builddir = ../../../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/src/test/modules/test_shmem/meson.build b/src/test/modules/test_shmem/meson.build
new file mode 100644
index 00000000000..fb4bf328b8f
--- /dev/null
+++ b/src/test/modules/test_shmem/meson.build
@@ -0,0 +1,33 @@
+# Copyright (c) 2024-2026, PostgreSQL Global Development Group
+
+test_shmem_sources = files(
+  'test_shmem.c',
+)
+
+if host_system == 'windows'
+  test_shmem_sources += rc_lib_gen.process(win32ver_rc, extra_args: [
+    '--NAME', 'test_shmem',
+    '--FILEDESC', 'test_shmem - test code for shmem allocations',])
+endif
+
+test_shmem = shared_module('test_shmem',
+  test_shmem_sources,
+  kwargs: pg_test_mod_args,
+)
+test_install_libs += test_shmem
+
+test_install_data += files(
+  'test_shmem.control',
+  'test_shmem--1.0.sql',
+)
+
+tests += {
+  'name': 'test_shmem',
+  'sd': meson.current_source_dir(),
+  'bd': meson.current_build_dir(),
+  'tap': {
+    'tests': [
+      't/001_late_shmem_alloc.pl',
+    ],
+  },
+}
diff --git a/src/test/modules/test_shmem/t/001_late_shmem_alloc.pl b/src/test/modules/test_shmem/t/001_late_shmem_alloc.pl
new file mode 100644
index 00000000000..84ec841b542
--- /dev/null
+++ b/src/test/modules/test_shmem/t/001_late_shmem_alloc.pl
@@ -0,0 +1,43 @@
+# Copyright (c) 2025-2026, PostgreSQL Global Development Group
+
+use strict;
+use warnings FATAL => 'all';
+
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+###
+# Test allocating memory after startup, i.e. when the library is not
+# in shared_preload_libraries
+###
+my $node = PostgreSQL::Test::Cluster->new('main');
+$node->init;
+$node->start;
+
+
+$node->safe_psql("postgres", "CREATE EXTENSION test_shmem;");
+
+# Check that the attach counter is incremented on a new connection
+my $attach_count1 = $node->safe_psql("postgres", "SELECT get_test_shmem_attach_count();");
+my $attach_count2 = $node->safe_psql("postgres", "SELECT get_test_shmem_attach_count();");
+cmp_ok($attach_count2, '>', $attach_count1, "attach callback is called in each backend");
+$node->stop;
+
+###
+# Test that loading via shared_preload_libraries also works
+###
+$node->append_conf('postgresql.conf', "shared_preload_libraries = 'test_shmem'");
+$node->start;
+
+# When loaded via shared_preload_libraries, the attach callback is
+# called or not, depending on whether this is an EXEC_BACKEND build.
+$attach_count1 = $node->safe_psql("postgres", "SELECT get_test_shmem_attach_count();");
+$attach_count2 = $node->safe_psql("postgres", "SELECT get_test_shmem_attach_count();");
+
+ok($attach_count1 == 0 && $attach_count2 == 0 ||
+   $attach_count2 >= $attach_count1,
+   "loaded via shared_preload_libraries");
+
+$node->stop;
+done_testing();
diff --git a/src/test/modules/test_shmem/test_shmem--1.0.sql b/src/test/modules/test_shmem/test_shmem--1.0.sql
new file mode 100644
index 00000000000..2d01fd9256c
--- /dev/null
+++ b/src/test/modules/test_shmem/test_shmem--1.0.sql
@@ -0,0 +1,9 @@
+/* src/test/modules/test_shmem/test_shmem--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION test_shmem" to load this file. \quit
+
+
+CREATE FUNCTION get_test_shmem_attach_count()
+RETURNS pg_catalog.int4 STRICT
+AS 'MODULE_PATHNAME' LANGUAGE C;
diff --git a/src/test/modules/test_shmem/test_shmem.c b/src/test/modules/test_shmem/test_shmem.c
new file mode 100644
index 00000000000..1d7f31b37c7
--- /dev/null
+++ b/src/test/modules/test_shmem/test_shmem.c
@@ -0,0 +1,107 @@
+/*-------------------------------------------------------------------------
+ *
+ * test_shmem.c
+ *		Helpers to test shmem allocation routines
+ *
+ *  XXX This module provides interface functions for C functionality to SQL, to
+ * make it possible to test AIO related behavior in a targeted way from SQL.
+ * It'd not generally be safe to export these functions to SQL, but for a test
+ * that's fine.
+ *
+ * Copyright (c) 2020-2026, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *	  src/test/modules/test_shmem/test_shmem.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "access/relation.h"
+#include "fmgr.h"
+#include "miscadmin.h"
+#include "storage/shmem.h"
+
+
+PG_MODULE_MAGIC;
+
+typedef struct TestShmemData
+{
+	int			value;
+	bool		initialized;
+	int			attach_count;
+} TestShmemData;
+
+static TestShmemData *TestShmem;
+
+static bool attached_or_initialized = false;
+
+static void test_shmem_request(void *arg);
+static void test_shmem_init(void *arg);
+static void test_shmem_attach(void *arg);
+
+static const ShmemCallbacks TestShmemCallbacks = {
+	.flags = SHMEM_ALLOW_AFTER_STARTUP,
+	.request_fn = test_shmem_request,
+	.init_fn = test_shmem_init,
+	.attach_fn = test_shmem_attach,
+};
+
+static void
+test_shmem_request(void *arg)
+{
+	static ShmemStructDesc TestShmemDesc;
+
+	elog(LOG, "test_shmem_request callback called");
+
+	ShmemRequestStruct(&TestShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "test_shmem area",
+		.size = sizeof(TestShmemData),
+		.ptr = (void **) &TestShmem,
+		});
+}
+
+static void
+test_shmem_init(void *arg)
+{
+	elog(LOG, "init callback called");
+	if (TestShmem->initialized)
+		elog(ERROR, "shmem area already initialized");
+	TestShmem->initialized = true;
+
+	if (attached_or_initialized)
+		elog(ERROR, "attach or initialize already called in this process");
+	attached_or_initialized = true;
+}
+
+static void
+test_shmem_attach(void *arg)
+{
+	elog(LOG, "test_shmem_attach callback called");
+	if (!TestShmem->initialized)
+		elog(ERROR, "shmem area not yet initialized");
+	TestShmem->attach_count++;
+
+	if (attached_or_initialized)
+		elog(ERROR, "attach or initialize already called in this process");
+	attached_or_initialized = true;
+}
+
+void
+_PG_init(void)
+{
+	elog(LOG, "test_shmem module's _PG_init called");
+	RegisterShmemCallbacks(&TestShmemCallbacks);
+}
+
+PG_FUNCTION_INFO_V1(get_test_shmem_attach_count);
+Datum
+get_test_shmem_attach_count(PG_FUNCTION_ARGS)
+{
+	if (!attached_or_initialized)
+		elog(ERROR, "shmem area not attached or initialized in this process");
+	if (!TestShmem->initialized)
+		elog(ERROR, "shmem area not yet initialized");
+	PG_RETURN_INT32(TestShmem->attach_count);
+}
diff --git a/src/test/modules/test_shmem/test_shmem.control b/src/test/modules/test_shmem/test_shmem.control
new file mode 100644
index 00000000000..f2f26f4537a
--- /dev/null
+++ b/src/test/modules/test_shmem/test_shmem.control
@@ -0,0 +1,3 @@
+comment = 'Test code for shmem allocations'
+default_version = '1.0'
+module_pathname = '$libdir/test_shmem'
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index d8d2548ef2d..139cb6f9da5 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -3129,6 +3129,7 @@ TestDSMRegistryHashEntry
 TestDSMRegistryStruct
 TestDecodingData
 TestDecodingTxnData
+TestShmemData
 TestSpec
 TestValueType
 TextFreq
-- 
2.47.3



  [text/x-patch] v8-0008-Convert-pg_stat_statements-to-use-the-new-interfa.patch (11.2K, 9-v8-0008-Convert-pg_stat_statements-to-use-the-new-interfa.patch)
  download | inline diff:
From ac38579f317287bb0f603c3de08081d6099de66c Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Fri, 13 Mar 2026 23:00:00 +0200
Subject: [PATCH v8 08/16] Convert pg_stat_statements to use the new interface

As part of this, embed the LWLock it needs in the shared memory struct
itself, so that we don't need to use RequestNamedLWLockTranche()
anymore. LWLockNewTrancheId+LWLockInitialize is more convenient to use
in extensions.

Reviewed-by: Ashutosh Bapat <[email protected]>
Reviewed-by: Zsolt Parragi <[email protected]>
Reviewed-by: Robert Haas <[email protected]>
Discussion: https://www.postgresql.org/message-id/CAExHW5vM1bneLYfg0wGeAa=52UiJ3z4vKd3AJ72X8Fw6k3KKrg@mail.gmail.com
---
 .../pg_stat_statements/pg_stat_statements.c   | 177 ++++++++----------
 1 file changed, 79 insertions(+), 98 deletions(-)

diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 6cb14824ec3..d9b1b8b4fe9 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -249,7 +249,7 @@ typedef struct pgssEntry
  */
 typedef struct pgssSharedState
 {
-	LWLock	   *lock;			/* protects hashtable search/modification */
+	LWLockPadded lock;			/* protects hashtable search/modification */
 	double		cur_median_usage;	/* current median usage in hashtable */
 	Size		mean_query_len; /* current mean entry text length */
 	slock_t		mutex;			/* protects following fields only: */
@@ -259,14 +259,24 @@ typedef struct pgssSharedState
 	pgssGlobalStats stats;		/* global statistics for pgss */
 } pgssSharedState;
 
+/* Links to shared memory state */
+static pgssSharedState *pgss;
+static HTAB *pgss_hash;
+
+static void pgss_shmem_request(void *arg);
+static void pgss_shmem_init(void *arg);
+
+static const ShmemCallbacks pgss_shmem_callbacks = {
+	.request_fn = pgss_shmem_request,
+	.init_fn = pgss_shmem_init,
+};
+
 /*---- Local variables ----*/
 
 /* Current nesting depth of planner/ExecutorRun/ProcessUtility calls */
 static int	nesting_level = 0;
 
 /* Saved hook values */
-static shmem_request_hook_type prev_shmem_request_hook = NULL;
-static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
 static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
 static planner_hook_type prev_planner_hook = NULL;
 static ExecutorStart_hook_type prev_ExecutorStart = NULL;
@@ -275,10 +285,6 @@ static ExecutorFinish_hook_type prev_ExecutorFinish = NULL;
 static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
 static ProcessUtility_hook_type prev_ProcessUtility = NULL;
 
-/* Links to shared memory state */
-static pgssSharedState *pgss = NULL;
-static HTAB *pgss_hash = NULL;
-
 /*---- GUC variables ----*/
 
 typedef enum
@@ -331,8 +337,6 @@ PG_FUNCTION_INFO_V1(pg_stat_statements_1_13);
 PG_FUNCTION_INFO_V1(pg_stat_statements);
 PG_FUNCTION_INFO_V1(pg_stat_statements_info);
 
-static void pgss_shmem_request(void);
-static void pgss_shmem_startup(void);
 static void pgss_shmem_shutdown(int code, Datum arg);
 static void pgss_post_parse_analyze(ParseState *pstate, Query *query,
 									JumbleState *jstate);
@@ -366,7 +370,6 @@ static void pgss_store(const char *query, int64 queryId,
 static void pg_stat_statements_internal(FunctionCallInfo fcinfo,
 										pgssVersion api_version,
 										bool showtext);
-static Size pgss_memsize(void);
 static pgssEntry *entry_alloc(pgssHashKey *key, Size query_offset, int query_len,
 							  int encoding, bool sticky);
 static void entry_dealloc(void);
@@ -471,13 +474,14 @@ _PG_init(void)
 
 	MarkGUCPrefixReserved("pg_stat_statements");
 
+	/*
+	 * Register our shared memory needs.
+	 */
+	RegisterShmemCallbacks(&pgss_shmem_callbacks);
+
 	/*
 	 * Install hooks.
 	 */
-	prev_shmem_request_hook = shmem_request_hook;
-	shmem_request_hook = pgss_shmem_request;
-	prev_shmem_startup_hook = shmem_startup_hook;
-	shmem_startup_hook = pgss_shmem_startup;
 	prev_post_parse_analyze_hook = post_parse_analyze_hook;
 	post_parse_analyze_hook = pgss_post_parse_analyze;
 	prev_planner_hook = planner_hook;
@@ -495,30 +499,48 @@ _PG_init(void)
 }
 
 /*
- * shmem_request hook: request additional shared resources.  We'll allocate or
- * attach to the shared resources in pgss_shmem_startup().
+ * shmem request callback: Request shared memory resources.
+ *
+ * This is called at postmaster startup.  Note that the shared memory isn't
+ * allocated here yet, this merely register our needs.
+ *
+ * In EXEC_BACKEND mode, this is also called in each backend, to re-attach to
+ * the shared memory area that was already initialized.
  */
 static void
-pgss_shmem_request(void)
+pgss_shmem_request(void *arg)
 {
-	if (prev_shmem_request_hook)
-		prev_shmem_request_hook();
-
-	RequestAddinShmemSpace(pgss_memsize());
-	RequestNamedLWLockTranche("pg_stat_statements", 1);
+	static ShmemHashDesc pgssSharedHashDesc;
+	static ShmemStructDesc pgssSharedStateDesc;
+
+	ShmemRequestHash(&pgssSharedHashDesc, &(ShmemRequestHashOpts) {
+		.name = "pg_stat_statements hash",
+		.ptr = &pgss_hash,
+		.init_size = pgss_max,
+		.max_size = pgss_max,
+		.hash_info.keysize = sizeof(pgssHashKey),
+		.hash_info.entrysize = sizeof(pgssEntry),
+		.hash_flags = HASH_ELEM | HASH_BLOBS,
+	});
+	ShmemRequestStruct(&pgssSharedStateDesc, &(ShmemRequestStructOpts) {
+		.name = "pg_stat_statements",
+		.size = sizeof(pgssSharedState),
+		.ptr = (void **) &pgss,
+	});
 }
 
 /*
- * shmem_startup hook: allocate or attach to shared memory,
- * then load any pre-existing statistics from file.
- * Also create and load the query-texts file, which is expected to exist
- * (even if empty) while the module is enabled.
+ * shmem init callback: Initialize our shared memory data structures at
+ * postmaster startup.
+ *
+ * Load any pre-existing statistics from file.  Also create and load the
+ * query-texts file, which is expected to exist (even if empty) while the
+ * module is enabled.
  */
 static void
-pgss_shmem_startup(void)
+pgss_shmem_init(void *arg)
 {
-	bool		found;
-	HASHCTL		info;
+	int			tranche_id;
 	FILE	   *file = NULL;
 	FILE	   *qfile = NULL;
 	uint32		header;
@@ -528,59 +550,32 @@ pgss_shmem_startup(void)
 	int			buffer_size;
 	char	   *buffer = NULL;
 
-	if (prev_shmem_startup_hook)
-		prev_shmem_startup_hook();
-
-	/* reset in case this is a restart within the postmaster */
-	pgss = NULL;
-	pgss_hash = NULL;
-
 	/*
-	 * Create or attach to the shared memory state, including hash table
+	 * Initialize the shmem area with no statistics.
 	 */
-	LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
-
-	pgss = ShmemInitStruct("pg_stat_statements",
-						   sizeof(pgssSharedState),
-						   &found);
-
-	if (!found)
-	{
-		/* First time through ... */
-		pgss->lock = &(GetNamedLWLockTranche("pg_stat_statements"))->lock;
-		pgss->cur_median_usage = ASSUMED_MEDIAN_INIT;
-		pgss->mean_query_len = ASSUMED_LENGTH_INIT;
-		SpinLockInit(&pgss->mutex);
-		pgss->extent = 0;
-		pgss->n_writers = 0;
-		pgss->gc_count = 0;
-		pgss->stats.dealloc = 0;
-		pgss->stats.stats_reset = GetCurrentTimestamp();
-	}
-
-	info.keysize = sizeof(pgssHashKey);
-	info.entrysize = sizeof(pgssEntry);
-	pgss_hash = ShmemInitHash("pg_stat_statements hash",
-							  pgss_max, pgss_max,
-							  &info,
-							  HASH_ELEM | HASH_BLOBS);
+	tranche_id = LWLockNewTrancheId("pg_stat_statements");
+	LWLockInitialize(&pgss->lock.lock, tranche_id);
+	pgss->cur_median_usage = ASSUMED_MEDIAN_INIT;
+	pgss->mean_query_len = ASSUMED_LENGTH_INIT;
+	SpinLockInit(&pgss->mutex);
+	pgss->extent = 0;
+	pgss->n_writers = 0;
+	pgss->gc_count = 0;
+	pgss->stats.dealloc = 0;
+	pgss->stats.stats_reset = GetCurrentTimestamp();
 
-	LWLockRelease(AddinShmemInitLock);
+	/* The hash table must be initialized already */
+	Assert(pgss_hash != NULL);
 
 	/*
-	 * If we're in the postmaster (or a standalone backend...), set up a shmem
-	 * exit hook to dump the statistics to disk.
+	 * Set up a shmem exit hook to dump the statistics to disk on postmaster
+	 * (or standalone backend) exit.
 	 */
-	if (!IsUnderPostmaster)
-		on_shmem_exit(pgss_shmem_shutdown, (Datum) 0);
-
-	/*
-	 * Done if some other process already completed our initialization.
-	 */
-	if (found)
-		return;
+	on_shmem_exit(pgss_shmem_shutdown, (Datum) 0);
 
 	/*
+	 * Load any pre-existing statistics from file.
+	 *
 	 * Note: we don't bother with locks here, because there should be no other
 	 * processes running when this code is reached.
 	 */
@@ -1338,7 +1333,7 @@ pgss_store(const char *query, int64 queryId,
 	key.toplevel = (nesting_level == 0);
 
 	/* Lookup the hash table entry with shared lock. */
-	LWLockAcquire(pgss->lock, LW_SHARED);
+	LWLockAcquire(&pgss->lock.lock, LW_SHARED);
 
 	entry = (pgssEntry *) hash_search(pgss_hash, &key, HASH_FIND, NULL);
 
@@ -1359,11 +1354,11 @@ pgss_store(const char *query, int64 queryId,
 		 */
 		if (jstate)
 		{
-			LWLockRelease(pgss->lock);
+			LWLockRelease(&pgss->lock.lock);
 			norm_query = generate_normalized_query(jstate, query,
 												   query_location,
 												   &query_len);
-			LWLockAcquire(pgss->lock, LW_SHARED);
+			LWLockAcquire(&pgss->lock.lock, LW_SHARED);
 		}
 
 		/* Append new query text to file with only shared lock held */
@@ -1378,8 +1373,8 @@ pgss_store(const char *query, int64 queryId,
 		do_gc = need_gc_qtexts();
 
 		/* Need exclusive lock to make a new hashtable entry - promote */
-		LWLockRelease(pgss->lock);
-		LWLockAcquire(pgss->lock, LW_EXCLUSIVE);
+		LWLockRelease(&pgss->lock.lock);
+		LWLockAcquire(&pgss->lock.lock, LW_EXCLUSIVE);
 
 		/*
 		 * A garbage collection may have occurred while we weren't holding the
@@ -1518,7 +1513,7 @@ pgss_store(const char *query, int64 queryId,
 	}
 
 done:
-	LWLockRelease(pgss->lock);
+	LWLockRelease(&pgss->lock.lock);
 
 	/* We postpone this clean-up until we're out of the lock */
 	if (norm_query)
@@ -1807,7 +1802,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
 	 * we need to partition the hash table to limit the time spent holding any
 	 * one lock.
 	 */
-	LWLockAcquire(pgss->lock, LW_SHARED);
+	LWLockAcquire(&pgss->lock.lock, LW_SHARED);
 
 	if (showtext)
 	{
@@ -2044,7 +2039,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
 		tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
 	}
 
-	LWLockRelease(pgss->lock);
+	LWLockRelease(&pgss->lock.lock);
 
 	free(qbuffer);
 }
@@ -2083,20 +2078,6 @@ pg_stat_statements_info(PG_FUNCTION_ARGS)
 	PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
 }
 
-/*
- * Estimate shared memory space needed.
- */
-static Size
-pgss_memsize(void)
-{
-	Size		size;
-
-	size = MAXALIGN(sizeof(pgssSharedState));
-	size = add_size(size, hash_estimate_size(pgss_max, sizeof(pgssEntry)));
-
-	return size;
-}
-
 /*
  * Allocate a new hashtable entry.
  * caller must hold an exclusive lock on pgss->lock
@@ -2726,7 +2707,7 @@ entry_reset(Oid userid, Oid dbid, int64 queryid, bool minmax_only)
 				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
 				 errmsg("pg_stat_statements must be loaded via \"shared_preload_libraries\"")));
 
-	LWLockAcquire(pgss->lock, LW_EXCLUSIVE);
+	LWLockAcquire(&pgss->lock.lock, LW_EXCLUSIVE);
 	num_entries = hash_get_num_entries(pgss_hash);
 
 	stats_reset = GetCurrentTimestamp();
@@ -2820,7 +2801,7 @@ done:
 	record_gc_qtexts();
 
 release_lock:
-	LWLockRelease(pgss->lock);
+	LWLockRelease(&pgss->lock.lock);
 
 	return stats_reset;
 }
-- 
2.47.3



  [text/x-patch] v8-0009-Introduce-registry-of-built-in-subsystems.patch (6.1K, 10-v8-0009-Introduce-registry-of-built-in-subsystems.patch)
  download | inline diff:
From 77dbf3df8c56cc760fa2d4661f35413b39b8ddb8 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
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



  [text/x-patch] v8-0010-Convert-injection-points-to-use-the-new-interface.patch (4.6K, 11-v8-0010-Convert-injection-points-to-use-the-new-interface.patch)
  download | inline diff:
From 94cdde8a4211f26bbc5be2639f49d986551aa189 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Sat, 21 Mar 2026 19:05:26 +0200
Subject: [PATCH v8 10/16] Convert injection points to use the new interface

---
 src/backend/storage/ipc/ipci.c           |  3 --
 src/backend/utils/misc/injection_point.c | 60 ++++++++++++------------
 src/include/storage/subsystemlist.h      |  2 +-
 src/include/utils/injection_point.h      |  3 --
 4 files changed, 30 insertions(+), 38 deletions(-)

diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index a2ef290b5b3..9d1b87e863e 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -52,7 +52,6 @@
 #include "storage/sinvaladt.h"
 #include "storage/subsystems.h"
 #include "utils/guc.h"
-#include "utils/injection_point.h"
 #include "utils/wait_event.h"
 
 /* GUCs */
@@ -139,7 +138,6 @@ CalculateShmemSize(void)
 	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());
 	size = add_size(size, WaitLSNShmemSize());
@@ -355,7 +353,6 @@ CreateOrAttachShmemStructs(void)
 	AsyncShmemInit();
 	StatsShmemInit();
 	WaitEventCustomShmemInit();
-	InjectionPointShmemInit();
 	AioShmemInit();
 	WaitLSNShmemInit();
 	LogicalDecodingCtlShmemInit();
diff --git a/src/backend/utils/misc/injection_point.c b/src/backend/utils/misc/injection_point.c
index c06b0e9b800..62e5cfcf4f9 100644
--- a/src/backend/utils/misc/injection_point.c
+++ b/src/backend/utils/misc/injection_point.c
@@ -17,6 +17,7 @@
  */
 #include "postgres.h"
 
+#include "storage/subsystems.h"
 #include "utils/injection_point.h"
 
 #ifdef USE_INJECTION_POINTS
@@ -109,6 +110,11 @@ typedef struct InjectionPointCacheEntry
 
 static HTAB *InjectionPointCache = NULL;
 
+#ifdef USE_INJECTION_POINTS
+static void InjectionPointShmemRequest(void *arg);
+static void InjectionPointShmemInit(void *arg);
+#endif
+
 /*
  * injection_point_cache_add
  *
@@ -226,46 +232,38 @@ injection_point_cache_get(const char *name)
 }
 #endif							/* USE_INJECTION_POINTS */
 
-/*
- * Return the space for dynamic shared hash table.
- */
-Size
-InjectionPointShmemSize(void)
-{
+const ShmemCallbacks InjectionPointShmemCallbacks = {
 #ifdef USE_INJECTION_POINTS
-	Size		sz = 0;
-
-	sz = add_size(sz, sizeof(InjectionPointsCtl));
-	return sz;
-#else
-	return 0;
+	.request_fn = InjectionPointShmemRequest,
+	.init_fn = InjectionPointShmemInit,
 #endif
-}
+};
 
 /*
- * Allocate shmem space for dynamic shared hash.
+ * Reserve space for the dynamic shared hash table
  */
-void
-InjectionPointShmemInit(void)
-{
 #ifdef USE_INJECTION_POINTS
-	bool		found;
+static void
+InjectionPointShmemRequest(void *arg)
+{
+	static ShmemStructDesc InjectionPointShmemDesc;
 
-	ActiveInjectionPoints = ShmemInitStruct("InjectionPoint hash",
-											sizeof(InjectionPointsCtl),
-											&found);
-	if (!IsUnderPostmaster)
-	{
-		Assert(!found);
-		pg_atomic_init_u32(&ActiveInjectionPoints->max_inuse, 0);
-		for (int i = 0; i < MAX_INJECTION_POINTS; i++)
-			pg_atomic_init_u64(&ActiveInjectionPoints->entries[i].generation, 0);
-	}
-	else
-		Assert(found);
-#endif
+	ShmemRequestStruct(&InjectionPointShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "InjectionPoint hash",
+		.size = sizeof(InjectionPointsCtl),
+		.ptr = (void **) &ActiveInjectionPoints,
+		});
 }
 
+static void
+InjectionPointShmemInit(void *arg)
+{
+	pg_atomic_init_u32(&ActiveInjectionPoints->max_inuse, 0);
+	for (int i = 0; i < MAX_INJECTION_POINTS; i++)
+		pg_atomic_init_u64(&ActiveInjectionPoints->entries[i].generation, 0);
+}
+#endif
+
 /*
  * Attach a new injection point.
  */
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index ed43c90bcc3..65da6f17c5d 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -20,4 +20,4 @@
  * of these matter.
  */
 
-/* TODO: empty for now */
+PG_SHMEM_SUBSYSTEM(InjectionPointShmemCallbacks)
diff --git a/src/include/utils/injection_point.h b/src/include/utils/injection_point.h
index 27a2526524f..fabd1455c3c 100644
--- a/src/include/utils/injection_point.h
+++ b/src/include/utils/injection_point.h
@@ -46,9 +46,6 @@ typedef void (*InjectionPointCallback) (const char *name,
 										const void *private_data,
 										void *arg);
 
-extern Size InjectionPointShmemSize(void);
-extern void InjectionPointShmemInit(void);
-
 extern void InjectionPointAttach(const char *name,
 								 const char *library,
 								 const char *function,
-- 
2.47.3



  [text/x-patch] v8-0011-Convert-test_aio-to-use-the-new-mechanism.patch (4.5K, 12-v8-0011-Convert-test_aio-to-use-the-new-mechanism.patch)
  download | inline diff:
From 8d306404b81126a386a7b169c60f09c4972ebf5d Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Sat, 21 Mar 2026 19:05:45 +0200
Subject: [PATCH v8 11/16] Convert test_aio to use the new mechanism

I wanted to use this to showcase SHMEM_ALLOW_ALLOC_AFTER_STARTUP, but
unfortunately that didn't work because then we'd call
InjectionPointLoad from _PG_init().
---
 src/test/modules/test_aio/test_aio.c | 106 ++++++++++++---------------
 1 file changed, 47 insertions(+), 59 deletions(-)

diff --git a/src/test/modules/test_aio/test_aio.c b/src/test/modules/test_aio/test_aio.c
index d687408af0c..2ba5f779b80 100644
--- a/src/test/modules/test_aio/test_aio.c
+++ b/src/test/modules/test_aio/test_aio.c
@@ -25,7 +25,6 @@
 #include "storage/buf_internals.h"
 #include "storage/bufmgr.h"
 #include "storage/checksum.h"
-#include "storage/ipc.h"
 #include "storage/lwlock.h"
 #include "utils/builtins.h"
 #include "utils/injection_point.h"
@@ -35,6 +34,7 @@
 PG_MODULE_MAGIC;
 
 
+/* In shared memory */
 typedef struct InjIoErrorState
 {
 	bool		enabled_short_read;
@@ -46,75 +46,66 @@ typedef struct InjIoErrorState
 
 static InjIoErrorState *inj_io_error_state;
 
-/* Shared memory init callbacks */
-static shmem_request_hook_type prev_shmem_request_hook = NULL;
-static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
-
-static PgAioHandle *last_handle;
+static void inj_io_shmem_request(void *arg);
+static void inj_io_shmem_init(void *arg);
+static void inj_io_shmem_attach(void *arg);
 
+static const ShmemCallbacks inj_io_shmem_callbacks = {
+	.request_fn = inj_io_shmem_request,
+	.init_fn = inj_io_shmem_init,
+	.attach_fn = inj_io_shmem_attach,
+};
 
+static PgAioHandle *last_handle;
 
 static void
-test_aio_shmem_request(void)
+inj_io_shmem_request(void *arg)
 {
-	if (prev_shmem_request_hook)
-		prev_shmem_request_hook();
+	static ShmemStructDesc inj_io_shmem_desc;
 
-	RequestAddinShmemSpace(sizeof(InjIoErrorState));
+	ShmemRequestStruct(&inj_io_shmem_desc, &(ShmemRequestStructOpts) {
+		.name = "test_aio",
+		.size = sizeof(InjIoErrorState),
+		.ptr = (void **) &inj_io_error_state,
+		});
 }
 
 static void
-test_aio_shmem_startup(void)
+inj_io_shmem_init(void *arg)
 {
-	bool		found;
-
-	if (prev_shmem_startup_hook)
-		prev_shmem_startup_hook();
-
-	/* Create or attach to the shared memory state */
-	LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
-
-	inj_io_error_state = ShmemInitStruct("injection_points",
-										 sizeof(InjIoErrorState),
-										 &found);
-
-	if (!found)
-	{
-		/* First time through, initialize */
-		inj_io_error_state->enabled_short_read = false;
-		inj_io_error_state->enabled_reopen = false;
+	/* First time through, initialize */
+	inj_io_error_state->enabled_short_read = false;
+	inj_io_error_state->enabled_reopen = false;
 
 #ifdef USE_INJECTION_POINTS
-		InjectionPointAttach("aio-process-completion-before-shared",
-							 "test_aio",
-							 "inj_io_short_read",
-							 NULL,
-							 0);
-		InjectionPointLoad("aio-process-completion-before-shared");
-
-		InjectionPointAttach("aio-worker-after-reopen",
-							 "test_aio",
-							 "inj_io_reopen",
-							 NULL,
-							 0);
-		InjectionPointLoad("aio-worker-after-reopen");
-
+	InjectionPointAttach("aio-process-completion-before-shared",
+						 "test_aio",
+						 "inj_io_short_read",
+						 NULL,
+						 0);
+	InjectionPointLoad("aio-process-completion-before-shared");
+
+	InjectionPointAttach("aio-worker-after-reopen",
+						 "test_aio",
+						 "inj_io_reopen",
+						 NULL,
+						 0);
+	InjectionPointLoad("aio-worker-after-reopen");
 #endif
-	}
-	else
-	{
-		/*
-		 * Pre-load the injection points now, so we can call them in a
-		 * critical section.
-		 */
+}
+
+static void
+inj_io_shmem_attach(void *arg)
+{
+	/*
+	 * Pre-load the injection points now, so we can call them in a critical
+	 * section.
+	 */
 #ifdef USE_INJECTION_POINTS
-		InjectionPointLoad("aio-process-completion-before-shared");
-		InjectionPointLoad("aio-worker-after-reopen");
-		elog(LOG, "injection point loaded");
+	InjectionPointLoad("aio-process-completion-before-shared");
+	InjectionPointLoad("aio-worker-after-reopen");
+	elog(LOG, "injection point loaded");
 #endif
-	}
-
-	LWLockRelease(AddinShmemInitLock);
 }
 
 void
@@ -123,10 +114,7 @@ _PG_init(void)
 	if (!process_shared_preload_libraries_in_progress)
 		return;
 
-	prev_shmem_request_hook = shmem_request_hook;
-	shmem_request_hook = test_aio_shmem_request;
-	prev_shmem_startup_hook = shmem_startup_hook;
-	shmem_startup_hook = test_aio_shmem_startup;
+	RegisterShmemCallbacks(&inj_io_shmem_callbacks);
 }
 
 
-- 
2.47.3



  [text/x-patch] v8-0012-Use-the-new-mechanism-in-a-few-core-subsystems.patch (44.1K, 13-v8-0012-Use-the-new-mechanism-in-a-few-core-subsystems.patch)
  download | inline diff:
From 6060c473fac7fafe592ca8683a57468dc0aef7be Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Sat, 21 Mar 2026 19:06:04 +0200
Subject: [PATCH v8 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 <[email protected]>
Reviewed-by: Zsolt Parragi <[email protected]>
Discussion: https://www.postgresql.org/message-id/CAExHW5vM1bneLYfg0wGeAa=52UiJ3z4vKd3AJ72X8Fw6k3KKrg@mail.gmail.com
---
 src/backend/access/transam/varsup.c     |  36 ++---
 src/backend/postmaster/launch_backend.c |   2 +
 src/backend/storage/ipc/dsm.c           |  65 +++++----
 src/backend/storage/ipc/dsm_registry.c  |  39 +++---
 src/backend/storage/ipc/ipci.c          |  30 ----
 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         | 176 ++++++++++++------------
 src/backend/utils/activity/wait_event.c |  96 +++++++------
 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              |   1 -
 src/include/storage/procarray.h         |   2 -
 src/include/storage/procsignal.h        |   3 -
 src/include/storage/sinvaladt.h         |   2 -
 src/include/storage/subsystemlist.h     |  19 +++
 src/include/utils/wait_event.h          |   2 -
 21 files changed, 377 insertions(+), 387 deletions(-)

diff --git a/src/backend/access/transam/varsup.c b/src/backend/access/transam/varsup.c
index 1441a051773..1eb72d44155 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, &(ShmemRequestStructOpts) {
+		.name = "TransamVariables",
+		.size = sizeof(TransamVariablesData),
+		.ptr = (void **) &TransamVariables,
+	});
 }
 
 /*
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/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c
index 6a5b16392f7..9fadb4e6cfd 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, &(ShmemRequestStructOpts) {
+		.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..92385a08727 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, &(ShmemRequestStructOpts) {
+		.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 9d1b87e863e..0a4e9ee6502 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());
@@ -137,7 +123,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, SlotSyncShmemSize());
 	size = add_size(size, AioShmemSize());
 	size = add_size(size, WaitLSNShmemSize());
@@ -288,13 +273,9 @@ RegisterBuiltinShmemCallbacks(void)
 static void
 CreateOrAttachShmemStructs(void)
 {
-	dsm_shmem_init();
-	DSMRegistryShmemInit();
-
 	/*
 	 * Set up xlog, clog, and buffers
 	 */
-	VarsupShmemInit();
 	XLOGShmemInit();
 	XLogPrefetchShmemInit();
 	XLogRecoveryShmemInit();
@@ -317,23 +298,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();
@@ -352,7 +323,6 @@ CreateOrAttachShmemStructs(void)
 	SyncScanShmemInit();
 	AsyncShmemInit();
 	StatsShmemInit();
-	WaitEventCustomShmemInit();
 	AioShmemInit();
 	WaitLSNShmemInit();
 	LogicalDecodingCtlShmemInit();
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index 4618820b337..d901f8e9947 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, &(ShmemRequestStructOpts) {
+		.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..1508c37ef62 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, &(ShmemRequestStructOpts) {
+			.name = "KnownAssignedXids",
+			.size = mul_size(sizeof(TransactionId), TOTAL_MAX_CACHED_SUBXIDS),
+			.ptr = (void **) &KnownAssignedXids,
+		});
+
+		ShmemRequestStruct(&KnownAssignedXidsValidShmemDesc, &(ShmemRequestStructOpts) {
+			.name = "KnownAssignedXidsValid",
+			.size = mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS),
+			.ptr = (void **) &KnownAssignedXidsValid,
+		});
 	}
 
-	return size;
+	/* Register the ProcArray shared structure */
+	ShmemRequestStruct(&ProcArrayShmemDesc, &(ShmemRequestStructOpts) {
+		.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..9ed24fae8d9 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, &(ShmemRequestStructOpts) {
+		.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..11138af0a23 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, &(ShmemRequestStructOpts) {
+		.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..e7501fee4c7 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;
 
@@ -127,25 +124,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.
  */
@@ -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, &(ShmemRequestStructOpts) {
+		.name = "PGPROC structures",
+		.size = size,
+		.ptr = (void **) &tmpAllProcs,
+	});
+
+	ShmemRequestStruct(&FastPathLockArrayShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Fast-Path Lock Array",
+		.size = IsUnderPostmaster ? SHMEM_REQUEST_UNKNOWN_SIZE : FastPathLockShmemSize(),
+		.ptr = (void **) &tmpFastPathLockArray,
+	});
+
+	ShmemRequestStruct(&ProcGlobalShmemDesc, &(ShmemRequestStructOpts) {
+		.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,31 +271,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 e5a2289f0b0..56b455d0023 100644
--- a/src/backend/utils/activity/wait_event.c
+++ b/src/backend/utils/activity/wait_event.c
@@ -25,6 +25,7 @@
 #include "storage/lmgr.h"
 #include "storage/lwlock.h"
 #include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "storage/spin.h"
 #include "utils/wait_event.h"
 
@@ -97,61 +98,58 @@ static WaitEventCustomCounterData *WaitEventCustomCounter;
 static uint32 WaitEventCustomNew(uint32 classId, const char *wait_event_name);
 static const char *GetWaitEventCustomIdentifier(uint32 wait_event_info);
 
+static void WaitEventCustomShmemRequest(void *arg);
+static void WaitEventCustomShmemInit(void *arg);
+
+const ShmemCallbacks WaitEventCustomShmemCallbacks = {
+	.request_fn = WaitEventCustomShmemRequest,
+	.init_fn = WaitEventCustomShmemInit,
+};
+
 /*
- *  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)
+static void
+WaitEventCustomShmemRequest(void *arg)
 {
-	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;
+	static ShmemStructDesc WaitEventCustomCounterShmemDesc;
+	static ShmemHashDesc WaitEventCustomHashByInfoDesc;
+	static ShmemHashDesc WaitEventCustomHashByNameDesc;
+
+	ShmemRequestStruct(&WaitEventCustomCounterShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "WaitEventCustomCounterData",
+		.size = sizeof(WaitEventCustomCounterData),
+		.ptr = (void **) &WaitEventCustomCounter,
+	});
+	ShmemRequestHash(&WaitEventCustomHashByInfoDesc, &(ShmemRequestHashOpts) {
+		.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,
+	});
+	ShmemRequestHash(&WaitEventCustomHashByNameDesc, &(ShmemRequestHashOpts) {
+		.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,
+	});
 }
 
-/*
- * Allocate shmem space for dynamic shared hash and dynamic allocation counter.
- */
-void
-WaitEventCustomShmemInit(void)
+static void
+WaitEventCustomShmemInit(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..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..3729f202fff 100644
--- a/src/include/storage/proc.h
+++ b/src/include/storage/proc.h
@@ -551,7 +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);
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 65da6f17c5d..5c11b2b3499 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -20,4 +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)
diff --git a/src/include/utils/wait_event.h b/src/include/utils/wait_event.h
index 34c27cc3dc3..86ee348220d 100644
--- a/src/include/utils/wait_event.h
+++ b/src/include/utils/wait_event.h
@@ -42,8 +42,6 @@ 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 char **GetWaitEventCustomNames(uint32 classId, int *nwaitevents);
 
 /* ----------
-- 
2.47.3



  [text/x-patch] v8-0013-Convert-SLRUs-to-use-the-new-interface.patch (85.5K, 14-v8-0013-Convert-SLRUs-to-use-the-new-interface.patch)
  download | inline diff:
From 95f4a4367d8e306f16870a10ecc53b8b6ec341b1 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Thu, 26 Mar 2026 20:39:46 +0200
Subject: [PATCH v8 13/16] Convert SLRUs to use the new interface

I replaced the old SimpleLruInit() function without a backwards
compatibility wrapper, because few extensions define their own SLRUs.
---
 src/backend/access/transam/clog.c        |  55 ++--
 src/backend/access/transam/commit_ts.c   |  92 +++---
 src/backend/access/transam/multixact.c   | 140 +++++----
 src/backend/access/transam/slru.c        | 364 ++++++++++++-----------
 src/backend/access/transam/subtrans.c    |  57 ++--
 src/backend/commands/async.c             | 117 ++++----
 src/backend/storage/ipc/ipci.c           |  16 -
 src/backend/storage/ipc/shmem.c          |   7 +
 src/backend/storage/lmgr/predicate.c     | 299 +++++++++----------
 src/backend/utils/activity/pgstat_slru.c |   1 +
 src/include/access/clog.h                |   2 -
 src/include/access/commit_ts.h           |   2 -
 src/include/access/multixact.h           |   2 -
 src/include/access/slru.h                | 104 ++++---
 src/include/access/subtrans.h            |   2 -
 src/include/commands/async.h             |   3 -
 src/include/storage/predicate.h          |   5 -
 src/include/storage/shmem.h              |   1 +
 src/include/storage/subsystemlist.h      |   8 +
 src/test/modules/test_slru/test_slru.c   | 110 +++----
 src/tools/pgindent/typedefs.list         |   4 +-
 21 files changed, 716 insertions(+), 675 deletions(-)

diff --git a/src/backend/access/transam/clog.c b/src/backend/access/transam/clog.c
index c654e0929b3..2e9a5cfa19e 100644
--- a/src/backend/access/transam/clog.c
+++ b/src/backend/access/transam/clog.c
@@ -43,6 +43,7 @@
 #include "pg_trace.h"
 #include "pgstat.h"
 #include "storage/proc.h"
+#include "storage/subsystems.h"
 #include "storage/sync.h"
 #include "utils/guc_hooks.h"
 #include "utils/wait_event.h"
@@ -106,13 +107,21 @@ TransactionIdToPage(TransactionId xid)
 /*
  * Link to shared-memory data structures for CLOG control
  */
-static SlruCtlData XactCtlData;
+static void CLOGShmemRequest(void *arg);
+static void CLOGShmemInit(void *arg);
+static bool CLOGPagePrecedes(int64 page1, int64 page2);
+static int	clog_errdetail_for_io_error(const void *opaque_data);
 
-#define XactCtl (&XactCtlData)
+const ShmemCallbacks CLOGShmemCallbacks = {
+	.request_fn = CLOGShmemRequest,
+	.init_fn = CLOGShmemInit,
+};
+
+static SlruDesc XactSlruDesc;
+
+#define XactCtl (&XactSlruDesc)
 
 
-static bool CLOGPagePrecedes(int64 page1, int64 page2);
-static int	clog_errdetail_for_io_error(const void *opaque_data);
 static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXact,
 								 Oid oldestXactDb);
 static void TransactionIdSetPageStatus(TransactionId xid, int nsubxids,
@@ -775,16 +784,10 @@ CLOGShmemBuffers(void)
 }
 
 /*
- * Initialization of shared memory for CLOG
+ * Register shared memory for CLOG
  */
-Size
-CLOGShmemSize(void)
-{
-	return SimpleLruShmemSize(CLOGShmemBuffers(), CLOG_LSNS_PER_PAGE);
-}
-
-void
-CLOGShmemInit(void)
+static void
+CLOGShmemRequest(void *arg)
 {
 	/* If auto-tuning is requested, now is the time to do it */
 	if (transaction_buffers == 0)
@@ -806,12 +809,26 @@ CLOGShmemInit(void)
 							PGC_S_OVERRIDE);
 	}
 	Assert(transaction_buffers != 0);
+	SimpleLruRequest(&XactSlruDesc, &(SlruRequestOpts) {
+		.name = "transaction",
+		.Dir = "pg_xact",
+		.long_segment_names = false,
+
+		.nslots = CLOGShmemBuffers(),
+		.nlsns = CLOG_LSNS_PER_PAGE,
+
+		.sync_handler = SYNC_HANDLER_CLOG,
+		.PagePrecedes = CLOGPagePrecedes,
+		.errdetail_for_io_error = clog_errdetail_for_io_error,
 
-	XactCtl->PagePrecedes = CLOGPagePrecedes;
-	XactCtl->errdetail_for_io_error = clog_errdetail_for_io_error;
-	SimpleLruInit(XactCtl, "transaction", CLOGShmemBuffers(), CLOG_LSNS_PER_PAGE,
-				  "pg_xact", LWTRANCHE_XACT_BUFFER,
-				  LWTRANCHE_XACT_SLRU, SYNC_HANDLER_CLOG, false);
+		.buffer_tranche_id = LWTRANCHE_XACT_BUFFER,
+		.bank_tranche_id = LWTRANCHE_XACT_SLRU,
+		});
+}
+
+static void
+CLOGShmemInit(void *arg)
+{
 	SlruPagePrecedesUnitTests(XactCtl, CLOG_XACTS_PER_PAGE);
 }
 
@@ -827,7 +844,7 @@ check_transaction_buffers(int *newval, void **extra, GucSource source)
 /*
  * This func must be called ONCE on system install.  It creates
  * the initial CLOG segment.  (The CLOG directory is assumed to
- * have been created by initdb, and CLOGShmemInit must have been
+ * have been created by initdb, and CLOGShmemInit must have been XXX
  * called already.)
  */
 void
diff --git a/src/backend/access/transam/commit_ts.c b/src/backend/access/transam/commit_ts.c
index 36219dd13cc..d6e67cc805d 100644
--- a/src/backend/access/transam/commit_ts.c
+++ b/src/backend/access/transam/commit_ts.c
@@ -30,6 +30,7 @@
 #include "funcapi.h"
 #include "miscadmin.h"
 #include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "utils/fmgrprotos.h"
 #include "utils/guc_hooks.h"
 #include "utils/timestamp.h"
@@ -80,9 +81,19 @@ TransactionIdToCTsPage(TransactionId xid)
 /*
  * Link to shared-memory data structures for CommitTs control
  */
-static SlruCtlData CommitTsCtlData;
+static void CommitTsShmemRequest(void *arg);
+static void CommitTsShmemInit(void *arg);
+static bool CommitTsPagePrecedes(int64 page1, int64 page2);
+static int	commit_ts_errdetail_for_io_error(const void *opaque_data);
+
+const ShmemCallbacks CommitTsShmemCallbacks = {
+	.request_fn = CommitTsShmemRequest,
+	.init_fn = CommitTsShmemInit,
+};
+
+static SlruDesc CommitTsSlruDesc;
 
-#define CommitTsCtl (&CommitTsCtlData)
+#define CommitTsCtl (&CommitTsSlruDesc)
 
 /*
  * We keep a cache of the last value set in shared memory.
@@ -104,6 +115,9 @@ typedef struct CommitTimestampShared
 
 static CommitTimestampShared *commitTsShared;
 
+static void CommitTsShmemInit(void *arg);
+
+static ShmemStructDesc CommitTsShmemDesc;
 
 /* GUC variable */
 bool		track_commit_timestamp;
@@ -114,8 +128,6 @@ static void SetXidCommitTsInPage(TransactionId xid, int nsubxids,
 static void TransactionIdSetCommitTs(TransactionId xid, TimestampTz ts,
 									 ReplOriginId nodeid, int slotno);
 static void error_commit_ts_disabled(void);
-static bool CommitTsPagePrecedes(int64 page1, int64 page2);
-static int	commit_ts_errdetail_for_io_error(const void *opaque_data);
 static void ActivateCommitTs(void);
 static void DeactivateCommitTs(void);
 static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXid);
@@ -512,24 +524,12 @@ CommitTsShmemBuffers(void)
 }
 
 /*
- * Shared memory sizing for CommitTs
+ * Register CommitTs shared memory needs at system startup (postmaster start
+ * or standalone backend)
  */
-Size
-CommitTsShmemSize(void)
-{
-	return SimpleLruShmemSize(CommitTsShmemBuffers(), 0) +
-		sizeof(CommitTimestampShared);
-}
-
-/*
- * Initialize CommitTs at system startup (postmaster start or standalone
- * backend)
- */
-void
-CommitTsShmemInit(void)
+static void
+CommitTsShmemRequest(void *arg)
 {
-	bool		found;
-
 	/* If auto-tuning is requested, now is the time to do it */
 	if (commit_timestamp_buffers == 0)
 	{
@@ -550,31 +550,37 @@ CommitTsShmemInit(void)
 							PGC_S_OVERRIDE);
 	}
 	Assert(commit_timestamp_buffers != 0);
+	SimpleLruRequest(&CommitTsSlruDesc, &(SlruRequestOpts) {
+		.name = "commit_timestamp",
+		.Dir = "pg_commit_ts",
+		.long_segment_names = false,
+
+		.nslots = CommitTsShmemBuffers(),
+
+		.PagePrecedes = CommitTsPagePrecedes,
+		.errdetail_for_io_error = commit_ts_errdetail_for_io_error,
+
+		.sync_handler = SYNC_HANDLER_COMMIT_TS,
+		.buffer_tranche_id = LWTRANCHE_COMMITTS_BUFFER,
+		.bank_tranche_id = LWTRANCHE_COMMITTS_SLRU,
+		});
+
+	ShmemRequestStruct(&CommitTsShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "CommitTs shared",
+		.size = sizeof(CommitTimestampShared),
+		.ptr = (void **) &commitTsShared,
+		});
+}
 
-	CommitTsCtl->PagePrecedes = CommitTsPagePrecedes;
-	CommitTsCtl->errdetail_for_io_error = commit_ts_errdetail_for_io_error;
-	SimpleLruInit(CommitTsCtl, "commit_timestamp", CommitTsShmemBuffers(), 0,
-				  "pg_commit_ts", LWTRANCHE_COMMITTS_BUFFER,
-				  LWTRANCHE_COMMITTS_SLRU,
-				  SYNC_HANDLER_COMMIT_TS,
-				  false);
-	SlruPagePrecedesUnitTests(CommitTsCtl, COMMIT_TS_XACTS_PER_PAGE);
-
-	commitTsShared = ShmemInitStruct("CommitTs shared",
-									 sizeof(CommitTimestampShared),
-									 &found);
-
-	if (!IsUnderPostmaster)
-	{
-		Assert(!found);
+static void
+CommitTsShmemInit(void *arg)
+{
+	commitTsShared->xidLastCommit = InvalidTransactionId;
+	TIMESTAMP_NOBEGIN(commitTsShared->dataLastCommit.time);
+	commitTsShared->dataLastCommit.nodeid = InvalidReplOriginId;
+	commitTsShared->commitTsActive = false;
 
-		commitTsShared->xidLastCommit = InvalidTransactionId;
-		TIMESTAMP_NOBEGIN(commitTsShared->dataLastCommit.time);
-		commitTsShared->dataLastCommit.nodeid = InvalidReplOriginId;
-		commitTsShared->commitTsActive = false;
-	}
-	else
-		Assert(found);
+	SlruPagePrecedesUnitTests(CommitTsCtl, COMMIT_TS_XACTS_PER_PAGE);
 }
 
 /*
diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.c
index 9f8d542c098..f9cb0ec8a3d 100644
--- a/src/backend/access/transam/multixact.c
+++ b/src/backend/access/transam/multixact.c
@@ -83,6 +83,7 @@
 #include "storage/pmsignal.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
+#include "storage/subsystems.h"
 #include "utils/guc_hooks.h"
 #include "utils/injection_point.h"
 #include "utils/lsyscache.h"
@@ -113,11 +114,16 @@ PreviousMultiXactId(MultiXactId multi)
 /*
  * Links to shared-memory data structures for MultiXact control
  */
-static SlruCtlData MultiXactOffsetCtlData;
-static SlruCtlData MultiXactMemberCtlData;
+static bool MultiXactOffsetPagePrecedes(int64 page1, int64 page2);
+static int	MultiXactOffsetIoErrorDetail(const void *opaque_data);
+static bool MultiXactMemberPagePrecedes(int64 page1, int64 page2);
+static int	MultiXactMemberIoErrorDetail(const void *opaque_data);
+
+static SlruDesc MultiXactOffsetSlruDesc;
+static SlruDesc MultiXactMemberSlruDesc;
 
-#define MultiXactOffsetCtl	(&MultiXactOffsetCtlData)
-#define MultiXactMemberCtl	(&MultiXactMemberCtlData)
+#define MultiXactOffsetCtl	(&MultiXactOffsetSlruDesc)
+#define MultiXactMemberCtl	(&MultiXactMemberSlruDesc)
 
 /*
  * MultiXact state shared across all backends.  All this state is protected
@@ -220,6 +226,15 @@ static MultiXactStateData *MultiXactState;
 static MultiXactId *OldestMemberMXactId;
 static MultiXactId *OldestVisibleMXactId;
 
+static void MultiXactShmemRequest(void *arg);
+static void MultiXactShmemInit(void *arg);
+static void MultiXactShmemAttach(void *arg);
+
+const ShmemCallbacks MultiXactShmemCallbacks = {
+	.request_fn = MultiXactShmemRequest,
+	.init_fn = MultiXactShmemInit,
+	.attach_fn = MultiXactShmemAttach,
+};
 
 static inline MultiXactId *
 MyOldestMemberMXactIdSlot(void)
@@ -321,10 +336,6 @@ typedef struct MultiXactMemberSlruReadContext
 	MultiXactOffset offset;
 } MultiXactMemberSlruReadContext;
 
-static bool MultiXactOffsetPagePrecedes(int64 page1, int64 page2);
-static bool MultiXactMemberPagePrecedes(int64 page1, int64 page2);
-static int	MultiXactOffsetIoErrorDetail(const void *opaque_data);
-static int	MultiXactMemberIoErrorDetail(const void *opaque_data);
 static void ExtendMultiXactOffset(MultiXactId multi);
 static void ExtendMultiXactMember(MultiXactOffset offset, int nmembers);
 static void SetOldestOffset(void);
@@ -1747,80 +1758,83 @@ multixact_twophase_postabort(FullTransactionId fxid, uint16 info,
 	multixact_twophase_postcommit(fxid, info, recdata, len);
 }
 
+
 /*
- * Initialization of shared memory for MultiXact.
- *
- * MultiXactSharedStateShmemSize() calculates the size of the MultiXactState
- * struct, and the two per-backend MultiXactId arrays.  They are carved out of
- * the same allocation.  MultiXactShmemSize() additionally includes the memory
- * needed for the two SLRU areas.
+ * Register shared memory needs for MultiXact.
  */
-static Size
-MultiXactSharedStateShmemSize(void)
+static void
+MultiXactShmemRequest(void *arg)
 {
+	static ShmemStructDesc MultiXactShmemDesc;
 	Size		size;
 
+	/*
+	 * Calculate the size of the MultiXactState struct, and the two
+	 * per-backend MultiXactId arrays.  They are carved out of the same
+	 * allocation.
+	 */
 	size = offsetof(MultiXactStateData, perBackendXactIds);
 	size = add_size(size,
 					mul_size(sizeof(MultiXactId), NumMemberSlots));
 	size = add_size(size,
 					mul_size(sizeof(MultiXactId), NumVisibleSlots));
-	return size;
-}
+	ShmemRequestStruct(&MultiXactShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Shared MultiXact State",
+		.size = size,
+		.ptr = (void **) &MultiXactState,
+		});
 
-Size
-MultiXactShmemSize(void)
-{
-	Size		size;
+	SimpleLruRequest(&MultiXactOffsetSlruDesc, &(SlruRequestOpts) {
+		.name = "multixact_offset",
+		.Dir = "pg_multixact/offsets",
+		.long_segment_names = false,
 
-	size = MultiXactSharedStateShmemSize();
-	size = add_size(size, SimpleLruShmemSize(multixact_offset_buffers, 0));
-	size = add_size(size, SimpleLruShmemSize(multixact_member_buffers, 0));
+		.nslots = multixact_offset_buffers,
 
-	return size;
-}
+		.sync_handler = SYNC_HANDLER_MULTIXACT_OFFSET,
+		.PagePrecedes = MultiXactOffsetPagePrecedes,
+		.errdetail_for_io_error = MultiXactOffsetIoErrorDetail,
 
-void
-MultiXactShmemInit(void)
-{
-	bool		found;
+		.buffer_tranche_id = LWTRANCHE_MULTIXACTOFFSET_BUFFER,
+		.bank_tranche_id = LWTRANCHE_MULTIXACTOFFSET_SLRU,
+		});
 
-	debug_elog2(DEBUG2, "Shared Memory Init for MultiXact");
+	SimpleLruRequest(&MultiXactMemberSlruDesc, &(SlruRequestOpts) {
+		.name = "multixact_member",
+		.Dir = "pg_multixact/members",
+		.long_segment_names = true,
 
-	MultiXactOffsetCtl->PagePrecedes = MultiXactOffsetPagePrecedes;
-	MultiXactMemberCtl->PagePrecedes = MultiXactMemberPagePrecedes;
-	MultiXactOffsetCtl->errdetail_for_io_error = MultiXactOffsetIoErrorDetail;
-	MultiXactMemberCtl->errdetail_for_io_error = MultiXactMemberIoErrorDetail;
+		.nslots = multixact_member_buffers,
 
-	SimpleLruInit(MultiXactOffsetCtl,
-				  "multixact_offset", multixact_offset_buffers, 0,
-				  "pg_multixact/offsets", LWTRANCHE_MULTIXACTOFFSET_BUFFER,
-				  LWTRANCHE_MULTIXACTOFFSET_SLRU,
-				  SYNC_HANDLER_MULTIXACT_OFFSET,
-				  false);
-	SlruPagePrecedesUnitTests(MultiXactOffsetCtl, MULTIXACT_OFFSETS_PER_PAGE);
-	SimpleLruInit(MultiXactMemberCtl,
-				  "multixact_member", multixact_member_buffers, 0,
-				  "pg_multixact/members", LWTRANCHE_MULTIXACTMEMBER_BUFFER,
-				  LWTRANCHE_MULTIXACTMEMBER_SLRU,
-				  SYNC_HANDLER_MULTIXACT_MEMBER,
-				  true);
-	/* doesn't call SimpleLruTruncate() or meet criteria for unit tests */
-
-	/* Initialize our shared state struct */
-	MultiXactState = ShmemInitStruct("Shared MultiXact State",
-									 MultiXactSharedStateShmemSize(),
-									 &found);
-	if (!IsUnderPostmaster)
-	{
-		Assert(!found);
+		.sync_handler = SYNC_HANDLER_MULTIXACT_MEMBER,
+		.PagePrecedes = MultiXactMemberPagePrecedes,
+		.errdetail_for_io_error = MultiXactMemberIoErrorDetail,
 
-		/* Make sure we zero out the per-backend state */
-		MemSet(MultiXactState, 0, MultiXactSharedStateShmemSize());
-	}
-	else
-		Assert(found);
+		.buffer_tranche_id = LWTRANCHE_MULTIXACTMEMBER_BUFFER,
+		.bank_tranche_id = LWTRANCHE_MULTIXACTMEMBER_SLRU,
+		});
 
+	/*
+	 * members SLRU doesn't call SimpleLruTruncate() or meet criteria for unit
+	 * tests
+	 */
+}
+
+static void
+MultiXactShmemInit(void *arg)
+{
+	/*
+	 * Set up array pointers.
+	 */
+	OldestMemberMXactId = MultiXactState->perBackendXactIds;
+	OldestVisibleMXactId = OldestMemberMXactId + NumMemberSlots;
+
+	SlruPagePrecedesUnitTests(MultiXactOffsetCtl, MULTIXACT_OFFSETS_PER_PAGE);
+}
+
+static void
+MultiXactShmemAttach(void *arg)
+{
 	/*
 	 * Set up array pointers.
 	 */
diff --git a/src/backend/access/transam/slru.c b/src/backend/access/transam/slru.c
index a2bb8fa8033..c3fe00d4039 100644
--- a/src/backend/access/transam/slru.c
+++ b/src/backend/access/transam/slru.c
@@ -71,6 +71,7 @@
 #include "storage/fd.h"
 #include "storage/shmem.h"
 #include "utils/guc.h"
+#include "utils/memutils.h"
 #include "utils/wait_event.h"
 
 /*
@@ -89,9 +90,9 @@
  *  dir/123456 for [2^20, 2^24-1]
  */
 static inline int
-SlruFileName(SlruCtl ctl, char *path, int64 segno)
+SlruFileName(SlruDesc *ctl, char *path, int64 segno)
 {
-	if (ctl->long_segment_names)
+	if (ctl->options.long_segment_names)
 	{
 		/*
 		 * We could use 16 characters here but the disadvantage would be that
@@ -101,7 +102,7 @@ SlruFileName(SlruCtl ctl, char *path, int64 segno)
 		 * that in the future we can't decrease SLRU_PAGES_PER_SEGMENT easily.
 		 */
 		Assert(segno >= 0 && segno <= INT64CONST(0xFFFFFFFFFFFFFFF));
-		return snprintf(path, MAXPGPATH, "%s/%015" PRIX64, ctl->Dir, segno);
+		return snprintf(path, MAXPGPATH, "%s/%015" PRIX64, ctl->options.Dir, segno);
 	}
 	else
 	{
@@ -110,7 +111,7 @@ SlruFileName(SlruCtl ctl, char *path, int64 segno)
 		 * integers are allowed. See SlruCorrectSegmentFilenameLength()
 		 */
 		Assert(segno >= 0 && segno <= INT64CONST(0xFFFFFF));
-		return snprintf(path, MAXPGPATH, "%s/%04X", (ctl)->Dir,
+		return snprintf(path, MAXPGPATH, "%s/%04X", (ctl)->options.Dir,
 						(unsigned int) segno);
 	}
 }
@@ -176,19 +177,19 @@ static SlruErrorCause slru_errcause;
 static int	slru_errno;
 
 
-static void SimpleLruZeroLSNs(SlruCtl ctl, int slotno);
-static void SimpleLruWaitIO(SlruCtl ctl, int slotno);
-static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata);
-static bool SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno);
-static bool SlruPhysicalWritePage(SlruCtl ctl, int64 pageno, int slotno,
+static void SimpleLruZeroLSNs(SlruDesc *ctl, int slotno);
+static void SimpleLruWaitIO(SlruDesc *ctl, int slotno);
+static void SlruInternalWritePage(SlruDesc *ctl, int slotno, SlruWriteAll fdata);
+static bool SlruPhysicalReadPage(SlruDesc *ctl, int64 pageno, int slotno);
+static bool SlruPhysicalWritePage(SlruDesc *ctl, int64 pageno, int slotno,
 								  SlruWriteAll fdata);
-static void SlruReportIOError(SlruCtl ctl, int64 pageno,
+static void SlruReportIOError(SlruDesc *ctl, int64 pageno,
 							  const void *opaque_data);
-static int	SlruSelectLRUPage(SlruCtl ctl, int64 pageno);
+static int	SlruSelectLRUPage(SlruDesc *ctl, int64 pageno);
 
-static bool SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename,
+static bool SlruScanDirCbDeleteCutoff(SlruDesc *ctl, char *filename,
 									  int64 segpage, void *data);
-static void SlruInternalDeleteSegment(SlruCtl ctl, int64 segno);
+static void SlruInternalDeleteSegment(SlruDesc *ctl, int64 segno);
 static inline void SlruRecentlyUsed(SlruShared shared, int slotno);
 
 
@@ -196,7 +197,7 @@ static inline void SlruRecentlyUsed(SlruShared shared, int slotno);
  * Initialization of shared memory
  */
 
-Size
+static Size
 SimpleLruShmemSize(int nslots, int nlsns)
 {
 	int			nbanks = nslots / SLRU_BANK_SIZE;
@@ -238,120 +239,134 @@ SimpleLruAutotuneBuffers(int divisor, int max)
 }
 
 /*
- * Initialize, or attach to, a simple LRU cache in shared memory.
- *
- * ctl: address of local (unshared) control structure.
- * name: name of SLRU.  (This is user-visible, pick with care!)
- * nslots: number of page slots to use.
- * nlsns: number of LSN groups per page (set to zero if not relevant).
- * subdir: PGDATA-relative subdirectory that will contain the files.
- * buffer_tranche_id: tranche ID to use for the SLRU's per-buffer LWLocks.
- * bank_tranche_id: tranche ID to use for the bank LWLocks.
- * sync_handler: which set of functions to use to handle sync requests
- * long_segment_names: use short or long segment names
+ * Register a simple LRU cache in shared memory.
  */
 void
-SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
-			  const char *subdir, int buffer_tranche_id, int bank_tranche_id,
-			  SyncRequestHandler sync_handler, bool long_segment_names)
+SimpleLruRequest(SlruDesc *desc, const SlruRequestOpts *options)
 {
+	SlruRequestOpts *options_copy;
+
+	Assert(options->name != NULL);
+	Assert(options->nslots > 0);
+	Assert(options->PagePrecedes != NULL);
+	Assert(options->errdetail_for_io_error != NULL);
+
+	options_copy = MemoryContextAlloc(TopMemoryContext,
+									  sizeof(SlruRequestOpts));
+	memcpy(options_copy, options, sizeof(SlruRequestOpts));
+
+	options_copy->base.name = options->name;
+	options_copy->base.size = SimpleLruShmemSize(options_copy->nslots, options_copy->nlsns);
+
+	ShmemRequestInternal(&desc->base, &options_copy->base, SHMEM_KIND_SLRU);
+}
+
+/* Initialize locks and shared memory area */
+void
+shmem_slru_init(ShmemStructDesc *base_desc, const ShmemRequestStructOpts *base_options)
+{
+	const SlruRequestOpts *options = (SlruRequestOpts *) base_options;
+	SlruDesc   *desc = (SlruDesc *) base_desc;
+	char		namebuf[NAMEDATALEN];
 	SlruShared	shared;
-	bool		found;
+	int			nslots = options->nslots;
 	int			nbanks = nslots / SLRU_BANK_SIZE;
+	int			nlsns = options->nlsns;
+	char	   *ptr;
+	Size		offset;
+
+	shared = desc->shared = (SlruShared) desc->base.ptr;
+	desc->nbanks = nbanks;
+	memcpy(&desc->options, options, sizeof(SlruRequestOpts));
+
+	/* assign new tranche IDs, if not given */
+	if (desc->options.buffer_tranche_id == 0)
+	{
+		snprintf(namebuf, sizeof(namebuf), "%s buffer", desc->options.name);
+		desc->options.buffer_tranche_id = LWLockNewTrancheId(namebuf);
+	}
+	if (desc->options.bank_tranche_id == 0)
+	{
+		snprintf(namebuf, sizeof(namebuf), "%s bank", desc->options.name);
+		desc->options.bank_tranche_id = LWLockNewTrancheId(namebuf);
+	}
 
 	Assert(nslots <= SLRU_MAX_ALLOWED_BUFFERS);
 
-	Assert(ctl->PagePrecedes != NULL);
-	Assert(ctl->errdetail_for_io_error != NULL);
+	memset(shared, 0, sizeof(SlruSharedData));
 
-	shared = (SlruShared) ShmemInitStruct(name,
-										  SimpleLruShmemSize(nslots, nlsns),
-										  &found);
+	shared->num_slots = nslots;
+	shared->lsn_groups_per_page = nlsns;
 
-	if (!IsUnderPostmaster)
-	{
-		/* Initialize locks and shared memory area */
-		char	   *ptr;
-		Size		offset;
-
-		Assert(!found);
-
-		memset(shared, 0, sizeof(SlruSharedData));
-
-		shared->num_slots = nslots;
-		shared->lsn_groups_per_page = nlsns;
-
-		pg_atomic_init_u64(&shared->latest_page_number, 0);
-
-		shared->slru_stats_idx = pgstat_get_slru_index(name);
-
-		ptr = (char *) shared;
-		offset = MAXALIGN(sizeof(SlruSharedData));
-		shared->page_buffer = (char **) (ptr + offset);
-		offset += MAXALIGN(nslots * sizeof(char *));
-		shared->page_status = (SlruPageStatus *) (ptr + offset);
-		offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
-		shared->page_dirty = (bool *) (ptr + offset);
-		offset += MAXALIGN(nslots * sizeof(bool));
-		shared->page_number = (int64 *) (ptr + offset);
-		offset += MAXALIGN(nslots * sizeof(int64));
-		shared->page_lru_count = (int *) (ptr + offset);
-		offset += MAXALIGN(nslots * sizeof(int));
-
-		/* Initialize LWLocks */
-		shared->buffer_locks = (LWLockPadded *) (ptr + offset);
-		offset += MAXALIGN(nslots * sizeof(LWLockPadded));
-		shared->bank_locks = (LWLockPadded *) (ptr + offset);
-		offset += MAXALIGN(nbanks * sizeof(LWLockPadded));
-		shared->bank_cur_lru_count = (int *) (ptr + offset);
-		offset += MAXALIGN(nbanks * sizeof(int));
-
-		if (nlsns > 0)
-		{
-			shared->group_lsn = (XLogRecPtr *) (ptr + offset);
-			offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
-		}
+	pg_atomic_init_u64(&shared->latest_page_number, 0);
 
-		ptr += BUFFERALIGN(offset);
-		for (int slotno = 0; slotno < nslots; slotno++)
-		{
-			LWLockInitialize(&shared->buffer_locks[slotno].lock,
-							 buffer_tranche_id);
+	shared->slru_stats_idx = pgstat_get_slru_index(desc->options.name);
 
-			shared->page_buffer[slotno] = ptr;
-			shared->page_status[slotno] = SLRU_PAGE_EMPTY;
-			shared->page_dirty[slotno] = false;
-			shared->page_lru_count[slotno] = 0;
-			ptr += BLCKSZ;
-		}
+	ptr = (char *) shared;
+	offset = MAXALIGN(sizeof(SlruSharedData));
+	shared->page_buffer = (char **) (ptr + offset);
+	offset += MAXALIGN(nslots * sizeof(char *));
+	shared->page_status = (SlruPageStatus *) (ptr + offset);
+	offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
+	shared->page_dirty = (bool *) (ptr + offset);
+	offset += MAXALIGN(nslots * sizeof(bool));
+	shared->page_number = (int64 *) (ptr + offset);
+	offset += MAXALIGN(nslots * sizeof(int64));
+	shared->page_lru_count = (int *) (ptr + offset);
+	offset += MAXALIGN(nslots * sizeof(int));
 
-		/* Initialize the slot banks. */
-		for (int bankno = 0; bankno < nbanks; bankno++)
-		{
-			LWLockInitialize(&shared->bank_locks[bankno].lock, bank_tranche_id);
-			shared->bank_cur_lru_count[bankno] = 0;
-		}
+	/* Initialize LWLocks */
+	shared->buffer_locks = (LWLockPadded *) (ptr + offset);
+	offset += MAXALIGN(nslots * sizeof(LWLockPadded));
+	shared->bank_locks = (LWLockPadded *) (ptr + offset);
+	offset += MAXALIGN(nbanks * sizeof(LWLockPadded));
+	shared->bank_cur_lru_count = (int *) (ptr + offset);
+	offset += MAXALIGN(nbanks * sizeof(int));
 
-		/* Should fit to estimated shmem size */
-		Assert(ptr - (char *) shared <= SimpleLruShmemSize(nslots, nlsns));
+	if (nlsns > 0)
+	{
+		shared->group_lsn = (XLogRecPtr *) (ptr + offset);
+		offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
 	}
-	else
+
+	ptr += BUFFERALIGN(offset);
+	for (int slotno = 0; slotno < nslots; slotno++)
 	{
-		Assert(found);
-		Assert(shared->num_slots == nslots);
+		LWLockInitialize(&shared->buffer_locks[slotno].lock,
+						 desc->options.buffer_tranche_id);
+
+		shared->page_buffer[slotno] = ptr;
+		shared->page_status[slotno] = SLRU_PAGE_EMPTY;
+		shared->page_dirty[slotno] = false;
+		shared->page_lru_count[slotno] = 0;
+		ptr += BLCKSZ;
 	}
 
-	/*
-	 * Initialize the unshared control struct, including directory path. We
-	 * assume caller set PagePrecedes.
-	 */
-	ctl->shared = shared;
-	ctl->sync_handler = sync_handler;
-	ctl->long_segment_names = long_segment_names;
-	ctl->nbanks = nbanks;
-	strlcpy(ctl->Dir, subdir, sizeof(ctl->Dir));
+	/* Initialize the slot banks. */
+	for (int bankno = 0; bankno < nbanks; bankno++)
+	{
+		LWLockInitialize(&shared->bank_locks[bankno].lock, desc->options.bank_tranche_id);
+		shared->bank_cur_lru_count[bankno] = 0;
+	}
+
+	/* Should fit to estimated shmem size */
+	Assert(ptr - (char *) shared <= SimpleLruShmemSize(nslots, nlsns));
+}
+
+void
+shmem_slru_attach(ShmemStructDesc *base_desc, const ShmemRequestStructOpts *base_options)
+{
+	const SlruRequestOpts *options = (SlruRequestOpts *) base_options;
+	SlruDesc   *desc = (SlruDesc *) base_desc;
+	int			nslots = options->nslots;
+	int			nbanks = nslots / SLRU_BANK_SIZE;
+
+	desc->shared = (SlruShared) desc->base.ptr;
+	desc->nbanks = nbanks;
+	memcpy(&desc->options, options, sizeof(SlruRequestOpts));
 }
 
+
 /*
  * Helper function for GUC check_hook to check whether slru buffers are in
  * multiples of SLRU_BANK_SIZE.
@@ -377,7 +392,7 @@ check_slru_buffers(const char *name, int *newval)
  * Bank lock must be held at entry, and will be held at exit.
  */
 int
-SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
+SimpleLruZeroPage(SlruDesc *ctl, int64 pageno)
 {
 	SlruShared	shared = ctl->shared;
 	int			slotno;
@@ -430,7 +445,7 @@ SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
  * This assumes that InvalidXLogRecPtr is bitwise-all-0.
  */
 static void
-SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
+SimpleLruZeroLSNs(SlruDesc *ctl, int slotno)
 {
 	SlruShared	shared = ctl->shared;
 
@@ -446,7 +461,7 @@ SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
  * SLRU bank lock is acquired and released here.
  */
 void
-SimpleLruZeroAndWritePage(SlruCtl ctl, int64 pageno)
+SimpleLruZeroAndWritePage(SlruDesc *ctl, int64 pageno)
 {
 	int			slotno;
 	LWLock	   *lock;
@@ -472,7 +487,7 @@ SimpleLruZeroAndWritePage(SlruCtl ctl, int64 pageno)
  * Bank lock must be held at entry, and will be held at exit.
  */
 static void
-SimpleLruWaitIO(SlruCtl ctl, int slotno)
+SimpleLruWaitIO(SlruDesc *ctl, int slotno)
 {
 	SlruShared	shared = ctl->shared;
 	int			bankno = SlotGetBankNumber(slotno);
@@ -530,7 +545,7 @@ SimpleLruWaitIO(SlruCtl ctl, int slotno)
  * The correct bank lock must be held at entry, and will be held at exit.
  */
 int
-SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok,
+SimpleLruReadPage(SlruDesc *ctl, int64 pageno, bool write_ok,
 				  const void *opaque_data)
 {
 	SlruShared	shared = ctl->shared;
@@ -634,7 +649,7 @@ SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok,
  * It is unspecified whether the lock will be shared or exclusive.
  */
 int
-SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, const void *opaque_data)
+SimpleLruReadPage_ReadOnly(SlruDesc *ctl, int64 pageno, const void *opaque_data)
 {
 	SlruShared	shared = ctl->shared;
 	LWLock	   *banklock = SimpleLruGetBankLock(ctl, pageno);
@@ -681,7 +696,7 @@ SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, const void *opaque_data)
  * Bank lock must be held at entry, and will be held at exit.
  */
 static void
-SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata)
+SlruInternalWritePage(SlruDesc *ctl, int slotno, SlruWriteAll fdata)
 {
 	SlruShared	shared = ctl->shared;
 	int64		pageno = shared->page_number[slotno];
@@ -761,7 +776,7 @@ SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata)
  * fdata is always passed a NULL here.
  */
 void
-SimpleLruWritePage(SlruCtl ctl, int slotno)
+SimpleLruWritePage(SlruDesc *ctl, int slotno)
 {
 	Assert(ctl->shared->page_status[slotno] != SLRU_PAGE_EMPTY);
 
@@ -775,7 +790,7 @@ SimpleLruWritePage(SlruCtl ctl, int slotno)
  * large enough to contain the given page.
  */
 bool
-SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
+SimpleLruDoesPhysicalPageExist(SlruDesc *ctl, int64 pageno)
 {
 	int64		segno = pageno / SLRU_PAGES_PER_SEGMENT;
 	int			rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
@@ -833,7 +848,7 @@ SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
  * read/write operations.  We could cache one virtual file pointer ...
  */
 static bool
-SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno)
+SlruPhysicalReadPage(SlruDesc *ctl, int64 pageno, int slotno)
 {
 	SlruShared	shared = ctl->shared;
 	int64		segno = pageno / SLRU_PAGES_PER_SEGMENT;
@@ -905,7 +920,7 @@ SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno)
  * SimpleLruWriteAll.
  */
 static bool
-SlruPhysicalWritePage(SlruCtl ctl, int64 pageno, int slotno, SlruWriteAll fdata)
+SlruPhysicalWritePage(SlruDesc *ctl, int64 pageno, int slotno, SlruWriteAll fdata)
 {
 	SlruShared	shared = ctl->shared;
 	int64		segno = pageno / SLRU_PAGES_PER_SEGMENT;
@@ -1037,11 +1052,11 @@ SlruPhysicalWritePage(SlruCtl ctl, int64 pageno, int slotno, SlruWriteAll fdata)
 	pgstat_report_wait_end();
 
 	/* Queue up a sync request for the checkpointer. */
-	if (ctl->sync_handler != SYNC_HANDLER_NONE)
+	if (ctl->options.sync_handler != SYNC_HANDLER_NONE)
 	{
 		FileTag		tag;
 
-		INIT_SLRUFILETAG(tag, ctl->sync_handler, segno);
+		INIT_SLRUFILETAG(tag, ctl->options.sync_handler, segno);
 		if (!RegisterSyncRequest(&tag, SYNC_REQUEST, false))
 		{
 			/* No space to enqueue sync request.  Do it synchronously. */
@@ -1077,7 +1092,7 @@ SlruPhysicalWritePage(SlruCtl ctl, int64 pageno, int slotno, SlruWriteAll fdata)
  * SlruPhysicalWritePage.  Call this after cleaning up shared-memory state.
  */
 static void
-SlruReportIOError(SlruCtl ctl, int64 pageno, const void *opaque_data)
+SlruReportIOError(SlruDesc *ctl, int64 pageno, const void *opaque_data)
 {
 	int64		segno = pageno / SLRU_PAGES_PER_SEGMENT;
 	int			rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
@@ -1092,14 +1107,14 @@ SlruReportIOError(SlruCtl ctl, int64 pageno, const void *opaque_data)
 			ereport(ERROR,
 					(errcode_for_file_access(),
 					 errmsg("could not open file \"%s\": %m", path),
-					 opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
+					 opaque_data ? ctl->options.errdetail_for_io_error(opaque_data) : 0));
 			break;
 		case SLRU_SEEK_FAILED:
 			ereport(ERROR,
 					(errcode_for_file_access(),
 					 errmsg("could not seek in file \"%s\" to offset %d: %m",
 							path, offset),
-					 opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
+					 opaque_data ? ctl->options.errdetail_for_io_error(opaque_data) : 0));
 			break;
 		case SLRU_READ_FAILED:
 			if (errno)
@@ -1107,12 +1122,12 @@ SlruReportIOError(SlruCtl ctl, int64 pageno, const void *opaque_data)
 						(errcode_for_file_access(),
 						 errmsg("could not read from file \"%s\" at offset %d: %m",
 								path, offset),
-						 opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
+						 opaque_data ? ctl->options.errdetail_for_io_error(opaque_data) : 0));
 			else
 				ereport(ERROR,
 						(errmsg("could not read from file \"%s\" at offset %d: read too few bytes",
 								path, offset),
-						 opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
+						 opaque_data ? ctl->options.errdetail_for_io_error(opaque_data) : 0));
 			break;
 		case SLRU_WRITE_FAILED:
 			if (errno)
@@ -1120,26 +1135,26 @@ SlruReportIOError(SlruCtl ctl, int64 pageno, const void *opaque_data)
 						(errcode_for_file_access(),
 						 errmsg("Could not write to file \"%s\" at offset %d: %m",
 								path, offset),
-						 opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
+						 opaque_data ? ctl->options.errdetail_for_io_error(opaque_data) : 0));
 			else
 				ereport(ERROR,
 						(errmsg("Could not write to file \"%s\" at offset %d: wrote too few bytes.",
 								path, offset),
-						 opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
+						 opaque_data ? ctl->options.errdetail_for_io_error(opaque_data) : 0));
 			break;
 		case SLRU_FSYNC_FAILED:
 			ereport(data_sync_elevel(ERROR),
 					(errcode_for_file_access(),
 					 errmsg("could not fsync file \"%s\": %m",
 							path),
-					 opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
+					 opaque_data ? ctl->options.errdetail_for_io_error(opaque_data) : 0));
 			break;
 		case SLRU_CLOSE_FAILED:
 			ereport(ERROR,
 					(errcode_for_file_access(),
 					 errmsg("could not close file \"%s\": %m",
 							path),
-					 opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
+					 opaque_data ? ctl->options.errdetail_for_io_error(opaque_data) : 0));
 			break;
 		default:
 			/* can't get here, we trust */
@@ -1199,7 +1214,7 @@ SlruRecentlyUsed(SlruShared shared, int slotno)
  * The correct bank lock must be held at entry, and will be held at exit.
  */
 static int
-SlruSelectLRUPage(SlruCtl ctl, int64 pageno)
+SlruSelectLRUPage(SlruDesc *ctl, int64 pageno)
 {
 	SlruShared	shared = ctl->shared;
 
@@ -1291,8 +1306,8 @@ SlruSelectLRUPage(SlruCtl ctl, int64 pageno)
 			{
 				if (this_delta > best_valid_delta ||
 					(this_delta == best_valid_delta &&
-					 ctl->PagePrecedes(this_page_number,
-									   best_valid_page_number)))
+					 ctl->options.PagePrecedes(this_page_number,
+											   best_valid_page_number)))
 				{
 					bestvalidslot = slotno;
 					best_valid_delta = this_delta;
@@ -1303,8 +1318,8 @@ SlruSelectLRUPage(SlruCtl ctl, int64 pageno)
 			{
 				if (this_delta > best_invalid_delta ||
 					(this_delta == best_invalid_delta &&
-					 ctl->PagePrecedes(this_page_number,
-									   best_invalid_page_number)))
+					 ctl->options.PagePrecedes(this_page_number,
+											   best_invalid_page_number)))
 				{
 					bestinvalidslot = slotno;
 					best_invalid_delta = this_delta;
@@ -1352,7 +1367,7 @@ SlruSelectLRUPage(SlruCtl ctl, int64 pageno)
  * entries are on disk.
  */
 void
-SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
+SimpleLruWriteAll(SlruDesc *ctl, bool allow_redirtied)
 {
 	SlruShared	shared = ctl->shared;
 	SlruWriteAllData fdata;
@@ -1422,8 +1437,8 @@ SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
 		SlruReportIOError(ctl, pageno, NULL);
 
 	/* Ensure that directory entries for new files are on disk. */
-	if (ctl->sync_handler != SYNC_HANDLER_NONE)
-		fsync_fname(ctl->Dir, true);
+	if (ctl->options.sync_handler != SYNC_HANDLER_NONE)
+		fsync_fname(ctl->options.Dir, true);
 }
 
 /*
@@ -1438,7 +1453,7 @@ SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
  * after it has accrued freshly-written data.
  */
 void
-SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
+SimpleLruTruncate(SlruDesc *ctl, int64 cutoffPage)
 {
 	SlruShared	shared = ctl->shared;
 	int			prevbank;
@@ -1460,12 +1475,12 @@ restart:
 	 * bugs elsewhere in SLRU handling, so we don't care if we read a slightly
 	 * outdated value; therefore we don't add a memory barrier.
 	 */
-	if (ctl->PagePrecedes(pg_atomic_read_u64(&shared->latest_page_number),
-						  cutoffPage))
+	if (ctl->options.PagePrecedes(pg_atomic_read_u64(&shared->latest_page_number),
+								  cutoffPage))
 	{
 		ereport(LOG,
 				(errmsg("could not truncate directory \"%s\": apparent wraparound",
-						ctl->Dir)));
+						ctl->options.Dir)));
 		return;
 	}
 
@@ -1488,7 +1503,7 @@ restart:
 
 		if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
 			continue;
-		if (!ctl->PagePrecedes(shared->page_number[slotno], cutoffPage))
+		if (!ctl->options.PagePrecedes(shared->page_number[slotno], cutoffPage))
 			continue;
 
 		/*
@@ -1533,16 +1548,16 @@ restart:
  * they either can't yet contain anything, or have already been cleaned out.
  */
 static void
-SlruInternalDeleteSegment(SlruCtl ctl, int64 segno)
+SlruInternalDeleteSegment(SlruDesc *ctl, int64 segno)
 {
 	char		path[MAXPGPATH];
 
 	/* Forget any fsync requests queued for this segment. */
-	if (ctl->sync_handler != SYNC_HANDLER_NONE)
+	if (ctl->options.sync_handler != SYNC_HANDLER_NONE)
 	{
 		FileTag		tag;
 
-		INIT_SLRUFILETAG(tag, ctl->sync_handler, segno);
+		INIT_SLRUFILETAG(tag, ctl->options.sync_handler, segno);
 		RegisterSyncRequest(&tag, SYNC_FORGET_REQUEST, true);
 	}
 
@@ -1556,7 +1571,7 @@ SlruInternalDeleteSegment(SlruCtl ctl, int64 segno)
  * Delete an individual SLRU segment, identified by the segment number.
  */
 void
-SlruDeleteSegment(SlruCtl ctl, int64 segno)
+SlruDeleteSegment(SlruDesc *ctl, int64 segno)
 {
 	SlruShared	shared = ctl->shared;
 	int			prevbank = SlotGetBankNumber(0);
@@ -1633,19 +1648,19 @@ restart:
  * first>=cutoff && last>=cutoff: no; every page of this segment is too young
  */
 static bool
-SlruMayDeleteSegment(SlruCtl ctl, int64 segpage, int64 cutoffPage)
+SlruMayDeleteSegment(SlruDesc *ctl, int64 segpage, int64 cutoffPage)
 {
 	int64		seg_last_page = segpage + SLRU_PAGES_PER_SEGMENT - 1;
 
 	Assert(segpage % SLRU_PAGES_PER_SEGMENT == 0);
 
-	return (ctl->PagePrecedes(segpage, cutoffPage) &&
-			ctl->PagePrecedes(seg_last_page, cutoffPage));
+	return (ctl->options.PagePrecedes(segpage, cutoffPage) &&
+			ctl->options.PagePrecedes(seg_last_page, cutoffPage));
 }
 
 #ifdef USE_ASSERT_CHECKING
 static void
-SlruPagePrecedesTestOffset(SlruCtl ctl, int per_page, uint32 offset)
+SlruPagePrecedesTestOffset(SlruDesc *ctl, int per_page, uint32 offset)
 {
 	TransactionId lhs,
 				rhs;
@@ -1654,6 +1669,9 @@ SlruPagePrecedesTestOffset(SlruCtl ctl, int per_page, uint32 offset)
 	TransactionId newestXact,
 				oldestXact;
 
+	/* This must be called after the Slru has been initialized */
+	Assert(ctl->options.PagePrecedes);
+
 	/*
 	 * Compare an XID pair having undefined order (see RFC 1982), a pair at
 	 * "opposite ends" of the XID space.  TransactionIdPrecedes() treats each
@@ -1670,19 +1688,19 @@ SlruPagePrecedesTestOffset(SlruCtl ctl, int per_page, uint32 offset)
 	Assert(!TransactionIdPrecedes(rhs, lhs + 1));
 	Assert(!TransactionIdFollowsOrEquals(lhs, rhs));
 	Assert(!TransactionIdFollowsOrEquals(rhs, lhs));
-	Assert(!ctl->PagePrecedes(lhs / per_page, lhs / per_page));
-	Assert(!ctl->PagePrecedes(lhs / per_page, rhs / per_page));
-	Assert(!ctl->PagePrecedes(rhs / per_page, lhs / per_page));
-	Assert(!ctl->PagePrecedes((lhs - per_page) / per_page, rhs / per_page));
-	Assert(ctl->PagePrecedes(rhs / per_page, (lhs - 3 * per_page) / per_page));
-	Assert(ctl->PagePrecedes(rhs / per_page, (lhs - 2 * per_page) / per_page));
-	Assert(ctl->PagePrecedes(rhs / per_page, (lhs - 1 * per_page) / per_page)
+	Assert(!ctl->options.PagePrecedes(lhs / per_page, lhs / per_page));
+	Assert(!ctl->options.PagePrecedes(lhs / per_page, rhs / per_page));
+	Assert(!ctl->options.PagePrecedes(rhs / per_page, lhs / per_page));
+	Assert(!ctl->options.PagePrecedes((lhs - per_page) / per_page, rhs / per_page));
+	Assert(ctl->options.PagePrecedes(rhs / per_page, (lhs - 3 * per_page) / per_page));
+	Assert(ctl->options.PagePrecedes(rhs / per_page, (lhs - 2 * per_page) / per_page));
+	Assert(ctl->options.PagePrecedes(rhs / per_page, (lhs - 1 * per_page) / per_page)
 		   || (1U << 31) % per_page != 0);	/* See CommitTsPagePrecedes() */
-	Assert(ctl->PagePrecedes((lhs + 1 * per_page) / per_page, rhs / per_page)
+	Assert(ctl->options.PagePrecedes((lhs + 1 * per_page) / per_page, rhs / per_page)
 		   || (1U << 31) % per_page != 0);
-	Assert(ctl->PagePrecedes((lhs + 2 * per_page) / per_page, rhs / per_page));
-	Assert(ctl->PagePrecedes((lhs + 3 * per_page) / per_page, rhs / per_page));
-	Assert(!ctl->PagePrecedes(rhs / per_page, (lhs + per_page) / per_page));
+	Assert(ctl->options.PagePrecedes((lhs + 2 * per_page) / per_page, rhs / per_page));
+	Assert(ctl->options.PagePrecedes((lhs + 3 * per_page) / per_page, rhs / per_page));
+	Assert(!ctl->options.PagePrecedes(rhs / per_page, (lhs + per_page) / per_page));
 
 	/*
 	 * GetNewTransactionId() has assigned the last XID it can safely use, and
@@ -1727,7 +1745,7 @@ SlruPagePrecedesTestOffset(SlruCtl ctl, int per_page, uint32 offset)
  * do not apply to them.)
  */
 void
-SlruPagePrecedesUnitTests(SlruCtl ctl, int per_page)
+SlruPagePrecedesUnitTests(SlruDesc *ctl, int per_page)
 {
 	/* Test first, middle and last entries of a page. */
 	SlruPagePrecedesTestOffset(ctl, per_page, 0);
@@ -1742,7 +1760,7 @@ SlruPagePrecedesUnitTests(SlruCtl ctl, int per_page)
  *		one containing the page passed as "data".
  */
 bool
-SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int64 segpage,
+SlruScanDirCbReportPresence(SlruDesc *ctl, char *filename, int64 segpage,
 							void *data)
 {
 	int64		cutoffPage = *(int64 *) data;
@@ -1758,7 +1776,7 @@ SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int64 segpage,
  *		This callback deletes segments prior to the one passed in as "data".
  */
 static bool
-SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename, int64 segpage,
+SlruScanDirCbDeleteCutoff(SlruDesc *ctl, char *filename, int64 segpage,
 						  void *data)
 {
 	int64		cutoffPage = *(int64 *) data;
@@ -1774,7 +1792,7 @@ SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename, int64 segpage,
  *		This callback deletes all segments.
  */
 bool
-SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int64 segpage, void *data)
+SlruScanDirCbDeleteAll(SlruDesc *ctl, char *filename, int64 segpage, void *data)
 {
 	SlruInternalDeleteSegment(ctl, segpage / SLRU_PAGES_PER_SEGMENT);
 
@@ -1788,9 +1806,9 @@ SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int64 segpage, void *data)
  * SLRU segment.
  */
 static inline bool
-SlruCorrectSegmentFilenameLength(SlruCtl ctl, size_t len)
+SlruCorrectSegmentFilenameLength(SlruDesc *ctl, size_t len)
 {
-	if (ctl->long_segment_names)
+	if (ctl->options.long_segment_names)
 		return (len == 15);		/* see SlruFileName() */
 	else
 
@@ -1821,7 +1839,7 @@ SlruCorrectSegmentFilenameLength(SlruCtl ctl, size_t len)
  * Note that no locking is applied.
  */
 bool
-SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
+SlruScanDirectory(SlruDesc *ctl, SlruScanCallback callback, void *data)
 {
 	bool		retval = false;
 	DIR		   *cldir;
@@ -1829,8 +1847,8 @@ SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
 	int64		segno;
 	int64		segpage;
 
-	cldir = AllocateDir(ctl->Dir);
-	while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
+	cldir = AllocateDir(ctl->options.Dir);
+	while ((clde = ReadDir(cldir, ctl->options.Dir)) != NULL)
 	{
 		size_t		len;
 
@@ -1843,7 +1861,7 @@ SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
 			segpage = segno * SLRU_PAGES_PER_SEGMENT;
 
 			elog(DEBUG2, "SlruScanDirectory invoking callback on %s/%s",
-				 ctl->Dir, clde->d_name);
+				 ctl->options.Dir, clde->d_name);
 			retval = callback(ctl, clde->d_name, segpage, data);
 			if (retval)
 				break;
@@ -1861,7 +1879,7 @@ SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
  * performs the fsync.
  */
 int
-SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
+SlruSyncFileTag(SlruDesc *ctl, const FileTag *ftag, char *path)
 {
 	int			fd;
 	int			save_errno;
diff --git a/src/backend/access/transam/subtrans.c b/src/backend/access/transam/subtrans.c
index c6ce71fc703..469ac02a1a3 100644
--- a/src/backend/access/transam/subtrans.c
+++ b/src/backend/access/transam/subtrans.c
@@ -33,6 +33,7 @@
 #include "access/transam.h"
 #include "miscadmin.h"
 #include "pg_trace.h"
+#include "storage/subsystems.h"
 #include "utils/guc_hooks.h"
 #include "utils/snapmgr.h"
 
@@ -66,16 +67,22 @@ TransactionIdToPage(TransactionId xid)
 #define TransactionIdToEntry(xid) ((xid) % (TransactionId) SUBTRANS_XACTS_PER_PAGE)
 
 
+static void SUBTRANSShmemRequest(void *arg);
+static void SUBTRANSShmemInit(void *arg);
+static bool SubTransPagePrecedes(int64 page1, int64 page2);
+static int	subtrans_errdetail_for_io_error(const void *opaque_data);
+
+const ShmemCallbacks SUBTRANSShmemCallbacks = {
+	.request_fn = SUBTRANSShmemRequest,
+	.init_fn = SUBTRANSShmemInit,
+};
+
 /*
  * Link to shared-memory data structures for SUBTRANS control
  */
-static SlruCtlData SubTransCtlData;
-
-#define SubTransCtl  (&SubTransCtlData)
+static SlruDesc SubTransSlruDesc;
 
-
-static bool SubTransPagePrecedes(int64 page1, int64 page2);
-static int	subtrans_errdetail_for_io_error(const void *opaque_data);
+#define SubTransCtl  (&SubTransSlruDesc)
 
 
 /*
@@ -207,17 +214,13 @@ SUBTRANSShmemBuffers(void)
 	return Min(Max(16, subtransaction_buffers), SLRU_MAX_ALLOWED_BUFFERS);
 }
 
+
+
 /*
- * Initialization of shared memory for SUBTRANS
+ * Register shared memory for SUBTRANS
  */
-Size
-SUBTRANSShmemSize(void)
-{
-	return SimpleLruShmemSize(SUBTRANSShmemBuffers(), 0);
-}
-
-void
-SUBTRANSShmemInit(void)
+static void
+SUBTRANSShmemRequest(void *arg)
 {
 	/* If auto-tuning is requested, now is the time to do it */
 	if (subtransaction_buffers == 0)
@@ -240,11 +243,25 @@ SUBTRANSShmemInit(void)
 	}
 	Assert(subtransaction_buffers != 0);
 
-	SubTransCtl->PagePrecedes = SubTransPagePrecedes;
-	SubTransCtl->errdetail_for_io_error = subtrans_errdetail_for_io_error;
-	SimpleLruInit(SubTransCtl, "subtransaction", SUBTRANSShmemBuffers(), 0,
-				  "pg_subtrans", LWTRANCHE_SUBTRANS_BUFFER,
-				  LWTRANCHE_SUBTRANS_SLRU, SYNC_HANDLER_NONE, false);
+	SimpleLruRequest(&SubTransSlruDesc, &(SlruRequestOpts) {
+		.name = "subtransaction",
+		.Dir = "pg_subtrans",
+		.long_segment_names = false,
+
+		.nslots = SUBTRANSShmemBuffers(),
+
+		.sync_handler = SYNC_HANDLER_NONE,
+		.PagePrecedes = SubTransPagePrecedes,
+		.errdetail_for_io_error = subtrans_errdetail_for_io_error,
+
+		.buffer_tranche_id = LWTRANCHE_SUBTRANS_BUFFER,
+		.bank_tranche_id = LWTRANCHE_SUBTRANS_SLRU,
+	});
+}
+
+static void
+SUBTRANSShmemInit(void *arg)
+{
 	SlruPagePrecedesUnitTests(SubTransCtl, SUBTRANS_XACTS_PER_PAGE);
 }
 
diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c
index 5c9a56c3d40..6f4639efea4 100644
--- a/src/backend/commands/async.c
+++ b/src/backend/commands/async.c
@@ -179,6 +179,7 @@
 #include "storage/latch.h"
 #include "storage/lmgr.h"
 #include "storage/procsignal.h"
+#include "storage/subsystems.h"
 #include "tcop/tcopprot.h"
 #include "utils/builtins.h"
 #include "utils/dsa.h"
@@ -345,6 +346,15 @@ typedef struct AsyncQueueControl
 
 static AsyncQueueControl *asyncQueueControl;
 
+static void AsyncShmemRequest(void *arg);
+static void AsyncShmemInit(void *arg);
+
+const ShmemCallbacks AsyncShmemCallbacks = {
+	.request_fn = AsyncShmemRequest,
+	.init_fn = AsyncShmemInit,
+};
+
+
 #define QUEUE_HEAD					(asyncQueueControl->head)
 #define QUEUE_TAIL					(asyncQueueControl->tail)
 #define QUEUE_STOP_PAGE				(asyncQueueControl->stopPage)
@@ -359,9 +369,13 @@ static AsyncQueueControl *asyncQueueControl;
 /*
  * The SLRU buffer area through which we access the notification queue
  */
-static SlruCtlData NotifyCtlData;
+static inline bool asyncQueuePagePrecedes(int64 p, int64 q);
+static int	asyncQueueErrdetailForIoError(const void *opaque_data);
+
+static SlruDesc NotifySlruDesc;
 
-#define NotifyCtl					(&NotifyCtlData)
+
+#define NotifyCtl					(&NotifySlruDesc)
 #define QUEUE_PAGESIZE				BLCKSZ
 
 #define QUEUE_FULL_WARN_INTERVAL	5000	/* warn at most once every 5s */
@@ -570,9 +584,7 @@ bool		Trace_notify = false;
 int			max_notify_queue_pages = 1048576;
 
 /* local function prototypes */
-static int	asyncQueueErrdetailForIoError(const void *opaque_data);
 static inline int64 asyncQueuePageDiff(int64 p, int64 q);
-static inline bool asyncQueuePagePrecedes(int64 p, int64 q);
 static inline void GlobalChannelKeyInit(GlobalChannelKey *key, Oid dboid,
 										const char *channel);
 static dshash_hash globalChannelTableHash(const void *key, size_t size,
@@ -780,78 +792,65 @@ initPendingListenActions(void)
 }
 
 /*
- * Report space needed for our shared memory area
+ * Register our shared memory needs
  */
-Size
-AsyncShmemSize(void)
+static void
+AsyncShmemRequest(void *arg)
 {
+	static ShmemStructDesc AsyncQueueControlShmemDesc;
 	Size		size;
 
-	/* This had better match AsyncShmemInit */
 	size = mul_size(MaxBackends, sizeof(QueueBackendStatus));
 	size = add_size(size, offsetof(AsyncQueueControl, backend));
 
-	size = add_size(size, SimpleLruShmemSize(notify_buffers, 0));
+	ShmemRequestStruct(&AsyncQueueControlShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Async Queue Control",
+		.size = size,
+		.ptr = (void **) &asyncQueueControl,
+	});
 
-	return size;
-}
+	SimpleLruRequest(&NotifySlruDesc, &(SlruRequestOpts) {
+		.name = "notify",
+		.Dir = "pg_notify",
 
-/*
- * Initialize our shared memory area
- */
-void
-AsyncShmemInit(void)
-{
-	bool		found;
-	Size		size;
+		/* long segment names are used in order to avoid wraparound */
+		.long_segment_names = true,
 
-	/*
-	 * Create or attach to the AsyncQueueControl structure.
-	 */
-	size = mul_size(MaxBackends, sizeof(QueueBackendStatus));
-	size = add_size(size, offsetof(AsyncQueueControl, backend));
+		.nslots = notify_buffers,
 
-	asyncQueueControl = (AsyncQueueControl *)
-		ShmemInitStruct("Async Queue Control", size, &found);
+		.sync_handler = SYNC_HANDLER_NONE,
+		.PagePrecedes = asyncQueuePagePrecedes,
+		.errdetail_for_io_error = asyncQueueErrdetailForIoError,
 
-	if (!found)
+		.buffer_tranche_id = LWTRANCHE_NOTIFY_BUFFER,
+		.bank_tranche_id = LWTRANCHE_NOTIFY_SLRU,
+	});
+}
+
+static void
+AsyncShmemInit(void *arg)
+{
+	SET_QUEUE_POS(QUEUE_HEAD, 0, 0);
+	SET_QUEUE_POS(QUEUE_TAIL, 0, 0);
+	QUEUE_STOP_PAGE = 0;
+	QUEUE_FIRST_LISTENER = INVALID_PROC_NUMBER;
+	asyncQueueControl->lastQueueFillWarn = 0;
+	asyncQueueControl->globalChannelTableDSA = DSA_HANDLE_INVALID;
+	asyncQueueControl->globalChannelTableDSH = DSHASH_HANDLE_INVALID;
+	for (int i = 0; i < MaxBackends; i++)
 	{
-		/* First time through, so initialize it */
-		SET_QUEUE_POS(QUEUE_HEAD, 0, 0);
-		SET_QUEUE_POS(QUEUE_TAIL, 0, 0);
-		QUEUE_STOP_PAGE = 0;
-		QUEUE_FIRST_LISTENER = INVALID_PROC_NUMBER;
-		asyncQueueControl->lastQueueFillWarn = 0;
-		asyncQueueControl->globalChannelTableDSA = DSA_HANDLE_INVALID;
-		asyncQueueControl->globalChannelTableDSH = DSHASH_HANDLE_INVALID;
-		for (int i = 0; i < MaxBackends; i++)
-		{
-			QUEUE_BACKEND_PID(i) = InvalidPid;
-			QUEUE_BACKEND_DBOID(i) = InvalidOid;
-			QUEUE_NEXT_LISTENER(i) = INVALID_PROC_NUMBER;
-			SET_QUEUE_POS(QUEUE_BACKEND_POS(i), 0, 0);
-			QUEUE_BACKEND_WAKEUP_PENDING(i) = false;
-			QUEUE_BACKEND_IS_ADVANCING(i) = false;
-		}
+		QUEUE_BACKEND_PID(i) = InvalidPid;
+		QUEUE_BACKEND_DBOID(i) = InvalidOid;
+		QUEUE_NEXT_LISTENER(i) = INVALID_PROC_NUMBER;
+		SET_QUEUE_POS(QUEUE_BACKEND_POS(i), 0, 0);
+		QUEUE_BACKEND_WAKEUP_PENDING(i) = false;
+		QUEUE_BACKEND_IS_ADVANCING(i) = false;
 	}
 
 	/*
-	 * Set up SLRU management of the pg_notify data. Note that long segment
-	 * names are used in order to avoid wraparound.
+	 * During start or reboot, clean out the pg_notify directory.
 	 */
-	NotifyCtl->PagePrecedes = asyncQueuePagePrecedes;
-	NotifyCtl->errdetail_for_io_error = asyncQueueErrdetailForIoError;
-	SimpleLruInit(NotifyCtl, "notify", notify_buffers, 0,
-				  "pg_notify", LWTRANCHE_NOTIFY_BUFFER, LWTRANCHE_NOTIFY_SLRU,
-				  SYNC_HANDLER_NONE, true);
-
-	if (!found)
-	{
-		/*
-		 * During start or reboot, clean out the pg_notify directory.
-		 */
-		(void) SlruScanDirectory(NotifyCtl, SlruScanDirCbDeleteAll, NULL);
-	}
+	(void) SlruScanDirectory(NotifyCtl, SlruScanDirCbDeleteAll, NULL);
 }
 
 
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 0a4e9ee6502..32255ead2bf 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -98,16 +98,11 @@ CalculateShmemSize(void)
 	/* legacy subsystems */
 	size = add_size(size, BufferManagerShmemSize());
 	size = add_size(size, LockManagerShmemSize());
-	size = add_size(size, PredicateLockShmemSize());
 	size = add_size(size, XLogPrefetchShmemSize());
 	size = add_size(size, XLOGShmemSize());
 	size = add_size(size, XLogRecoveryShmemSize());
-	size = add_size(size, CLOGShmemSize());
-	size = add_size(size, CommitTsShmemSize());
-	size = add_size(size, SUBTRANSShmemSize());
 	size = add_size(size, TwoPhaseShmemSize());
 	size = add_size(size, BackgroundWorkerShmemSize());
-	size = add_size(size, MultiXactShmemSize());
 	size = add_size(size, LWLockShmemSize());
 	size = add_size(size, BackendStatusShmemSize());
 	size = add_size(size, CheckpointerShmemSize());
@@ -121,7 +116,6 @@ CalculateShmemSize(void)
 	size = add_size(size, ApplyLauncherShmemSize());
 	size = add_size(size, BTreeShmemSize());
 	size = add_size(size, SyncScanShmemSize());
-	size = add_size(size, AsyncShmemSize());
 	size = add_size(size, StatsShmemSize());
 	size = add_size(size, SlotSyncShmemSize());
 	size = add_size(size, AioShmemSize());
@@ -279,10 +273,6 @@ CreateOrAttachShmemStructs(void)
 	XLOGShmemInit();
 	XLogPrefetchShmemInit();
 	XLogRecoveryShmemInit();
-	CLOGShmemInit();
-	CommitTsShmemInit();
-	SUBTRANSShmemInit();
-	MultiXactShmemInit();
 	BufferManagerShmemInit();
 
 	/*
@@ -290,11 +280,6 @@ CreateOrAttachShmemStructs(void)
 	 */
 	LockManagerShmemInit();
 
-	/*
-	 * Set up predicate lock manager
-	 */
-	PredicateLockShmemInit();
-
 	/*
 	 * Set up process table
 	 */
@@ -321,7 +306,6 @@ CreateOrAttachShmemStructs(void)
 	 */
 	BTreeShmemInit();
 	SyncScanShmemInit();
-	AsyncShmemInit();
 	StatsShmemInit();
 	AioShmemInit();
 	WaitLSNShmemInit();
diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index 84099ce78fe..595f1e582d5 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -135,6 +135,7 @@
 
 #include <unistd.h>
 
+#include "access/slru.h"
 #include "common/int.h"
 #include "fmgr.h"
 #include "funcapi.h"
@@ -539,6 +540,9 @@ AttachOrInit(ShmemRequest *request, bool init_allowed, bool attach_allowed)
 			case SHMEM_KIND_HASH:
 				shmem_hash_attach(desc, request->options);
 				break;
+			case SHMEM_KIND_SLRU:
+				shmem_slru_attach(desc, request->options);
+				break;
 		}
 	}
 	else if (!init_allowed)
@@ -593,6 +597,9 @@ AttachOrInit(ShmemRequest *request, bool init_allowed, bool attach_allowed)
 			case SHMEM_KIND_HASH:
 				shmem_hash_init(desc, request->options);
 				break;
+			case SHMEM_KIND_SLRU:
+				shmem_slru_init(desc, request->options);
+				break;
 		}
 	}
 
diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c
index 4f80fc73639..6475b50a2ab 100644
--- a/src/backend/storage/lmgr/predicate.c
+++ b/src/backend/storage/lmgr/predicate.c
@@ -152,10 +152,6 @@
 /*
  * INTERFACE ROUTINES
  *
- * housekeeping for setting up shared memory predicate lock structures
- *		PredicateLockShmemInit(void)
- *		PredicateLockShmemSize(void)
- *
  * predicate lock reporting
  *		GetPredicateLockStatusData(void)
  *		PageIsPredicateLocked(Relation relation, BlockNumber blkno)
@@ -211,6 +207,8 @@
 #include "storage/predicate_internals.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
+#include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "utils/guc_hooks.h"
 #include "utils/rel.h"
 #include "utils/snapmgr.h"
@@ -322,9 +320,12 @@
 /*
  * The SLRU buffer area through which we access the old xids.
  */
-static SlruCtlData SerialSlruCtlData;
+static bool SerialPagePrecedesLogically(int64 page1, int64 page2);
+static int	serial_errdetail_for_io_error(const void *opaque_data);
 
-#define SerialSlruCtl			(&SerialSlruCtlData)
+static SlruDesc SerialSlruDesc;
+
+#define SerialSlruCtl			(&SerialSlruDesc)
 
 #define SERIAL_PAGESIZE			BLCKSZ
 #define SERIAL_ENTRYSIZE			sizeof(SerCommitSeqNo)
@@ -384,6 +385,17 @@ int			max_predicate_locks_per_page;	/* in guc_tables.c */
  */
 static PredXactList PredXact;
 
+static void PredicateLockShmemRequest(void *arg);
+static void PredicateLockShmemInit(void *arg);
+static void PredicateLockShmemAttach(void *arg);
+
+const ShmemCallbacks PredicateLockShmemCallbacks = {
+	.request_fn = PredicateLockShmemRequest,
+	.init_fn = PredicateLockShmemInit,
+	.attach_fn = PredicateLockShmemAttach,
+};
+
+
 /*
  * This provides a pool of RWConflict data elements to use in conflict lists
  * between transactions.
@@ -431,6 +443,16 @@ static bool MyXactDidWrite = false;
  */
 static SERIALIZABLEXACT *SavedSerializableXact = InvalidSerializableXact;
 
+static ShmemStructDesc PredXactListShmemDesc;
+
+static int64 max_serializable_xacts;
+
+static ShmemStructDesc RWConflictPoolShmemDesc;
+
+static ShmemStructDesc FinishedSerializableShmemDesc;
+
+static ShmemStructDesc SerialControlShmemDesc;
+
 /* local functions */
 
 static SERIALIZABLEXACT *CreatePredXact(void);
@@ -442,13 +464,18 @@ static void SetPossibleUnsafeConflict(SERIALIZABLEXACT *roXact, SERIALIZABLEXACT
 static void ReleaseRWConflict(RWConflict conflict);
 static void FlagSxactUnsafe(SERIALIZABLEXACT *sxact);
 
-static bool SerialPagePrecedesLogically(int64 page1, int64 page2);
-static int	serial_errdetail_for_io_error(const void *opaque_data);
 static void SerialAdd(TransactionId xid, SerCommitSeqNo minConflictCommitSeqNo);
 static SerCommitSeqNo SerialGetMinConflictCommitSeqNo(TransactionId xid);
 static void SerialSetActiveSerXmin(TransactionId xid);
 
 static uint32 predicatelock_hash(const void *key, Size keysize);
+
+static ShmemHashDesc SerializableXidHashDesc;
+
+static ShmemHashDesc PredicateLockTargetHashDesc;
+
+static ShmemHashDesc PredicateLockHashDesc;
+
 static void SummarizeOldestCommittedSxact(void);
 static Snapshot GetSafeSnapshot(Snapshot origSnapshot);
 static Snapshot GetSerializableTransactionSnapshotInt(Snapshot snapshot,
@@ -1100,73 +1127,61 @@ CheckPointPredicate(void)
 /*------------------------------------------------------------------------*/
 
 /*
- * PredicateLockShmemInit -- Initialize the predicate locking data structures.
- *
- * This is called from CreateSharedMemoryAndSemaphores(), which see for
- * more comments.  In the normal postmaster case, the shared hash tables
- * are created here.  Backends inherit the pointers
- * to the shared tables via fork().  In the EXEC_BACKEND case, each
- * backend re-executes this code to obtain pointers to the already existing
- * shared hash tables.
+ * PredicateLockShmemRequest -- Register the predicate locking data structures.
  */
-void
-PredicateLockShmemInit(void)
+static void
+PredicateLockShmemRequest(void *arg)
 {
-	HASHCTL		info;
 	int64		max_predicate_lock_targets;
 	int64		max_predicate_locks;
-	int64		max_serializable_xacts;
 	int64		max_rw_conflicts;
-	Size		requestSize;
-	bool		found;
-
-#ifndef EXEC_BACKEND
-	Assert(!IsUnderPostmaster);
-#endif
 
 	/*
-	 * Compute size of predicate lock target hashtable. Note these
-	 * calculations must agree with PredicateLockShmemSize!
+	 * Hash tables and other structs are set up by ShmemInitRegistered() /
+	 * ShmemAttachRegistered() via registered descriptors in
+	 * PredicateLockShmemRegister().  Here we do the remaining initialization
+	 * that can't be done in a callback.
 	 */
 	max_predicate_lock_targets = NPREDICATELOCKTARGETENTS();
 
 	/*
-	 * Allocate hash table for PREDICATELOCKTARGET structs.  This stores
+	 * Register hash table for PREDICATELOCKTARGET structs.  This stores
 	 * per-predicate-lock-target information.
 	 */
-	info.keysize = sizeof(PREDICATELOCKTARGETTAG);
-	info.entrysize = sizeof(PREDICATELOCKTARGET);
-	info.num_partitions = NUM_PREDICATELOCK_PARTITIONS;
+	ShmemRequestHash(&PredicateLockTargetHashDesc, &(ShmemRequestHashOpts) {
+		.name = "PREDICATELOCKTARGET hash",
 
-	PredicateLockTargetHash = ShmemInitHash("PREDICATELOCKTARGET hash",
-											max_predicate_lock_targets,
-											max_predicate_lock_targets,
-											&info,
-											HASH_ELEM | HASH_BLOBS |
-											HASH_PARTITION | HASH_FIXED_SIZE);
+		.init_size = max_predicate_lock_targets,
+		.max_size = max_predicate_lock_targets,
 
-	/* Pre-calculate the hash and partition lock of the scratch entry */
-	ScratchTargetTagHash = PredicateLockTargetTagHashCode(&ScratchTargetTag);
-	ScratchPartitionLock = PredicateLockHashPartitionLock(ScratchTargetTagHash);
+		.ptr = &PredicateLockTargetHash,
+		.hash_info.keysize = sizeof(PREDICATELOCKTARGETTAG),
+		.hash_info.entrysize = sizeof(PREDICATELOCKTARGET),
+		.hash_info.num_partitions = NUM_PREDICATELOCK_PARTITIONS,
+		.hash_flags = HASH_ELEM | HASH_BLOBS | HASH_PARTITION | HASH_FIXED_SIZE,
+	});
 
 	/*
 	 * Allocate hash table for PREDICATELOCK structs.  This stores per
 	 * xact-lock-of-a-target information.
 	 */
-	info.keysize = sizeof(PREDICATELOCKTAG);
-	info.entrysize = sizeof(PREDICATELOCK);
-	info.hash = predicatelock_hash;
-	info.num_partitions = NUM_PREDICATELOCK_PARTITIONS;
 
 	/* Assume an average of 2 xacts per target */
 	max_predicate_locks = max_predicate_lock_targets * 2;
 
-	PredicateLockHash = ShmemInitHash("PREDICATELOCK hash",
-									  max_predicate_locks,
-									  max_predicate_locks,
-									  &info,
-									  HASH_ELEM | HASH_FUNCTION |
-									  HASH_PARTITION | HASH_FIXED_SIZE);
+	ShmemRequestHash(&PredicateLockHashDesc, &(ShmemRequestHashOpts) {
+		.name = "PREDICATELOCK hash",
+
+		.init_size = max_predicate_locks,
+		.max_size = max_predicate_locks,
+
+		.ptr = &PredicateLockHash,
+		.hash_info.keysize = sizeof(PREDICATELOCKTAG),
+		.hash_info.entrysize = sizeof(PREDICATELOCK),
+		.hash_info.hash = predicatelock_hash,
+		.hash_info.num_partitions = NUM_PREDICATELOCK_PARTITIONS,
+		.hash_flags = HASH_ELEM | HASH_FUNCTION | HASH_PARTITION | HASH_FIXED_SIZE,
+		});
 
 	/*
 	 * Compute size for serializable transaction hashtable. Note these
@@ -1179,30 +1194,32 @@ PredicateLockShmemInit(void)
 	max_serializable_xacts = (MaxBackends + max_prepared_xacts) * 10;
 
 	/*
-	 * Allocate a list to hold information on transactions participating in
+	 * Register a list to hold information on transactions participating in
 	 * predicate locking.
 	 */
-	requestSize = add_size(PredXactListDataSize,
-						   (mul_size((Size) max_serializable_xacts,
-									 sizeof(SERIALIZABLEXACT))));
-	PredXact = ShmemInitStruct("PredXactList",
-							   requestSize,
-							   &found);
-	Assert(found == IsUnderPostmaster);
+	ShmemRequestStruct(&PredXactListShmemDesc, &(ShmemRequestStructOpts) {
+			.name = "PredXactList",
+			.size = add_size(PredXactListDataSize,
+							 (mul_size((Size) max_serializable_xacts,
+									   sizeof(SERIALIZABLEXACT)))),
+			.ptr = (void **) &PredXact,
+		});
 
 	/*
-	 * Allocate hash table for SERIALIZABLEXID structs.  This stores per-xid
+	 * Register hash table for SERIALIZABLEXID structs.  This stores per-xid
 	 * information for serializable transactions which have accessed data.
 	 */
-	info.keysize = sizeof(SERIALIZABLEXIDTAG);
-	info.entrysize = sizeof(SERIALIZABLEXID);
+	ShmemRequestHash(&SerializableXidHashDesc, &(ShmemRequestHashOpts) {
+		.name = "SERIALIZABLEXID hash",
+
+		.init_size = max_serializable_xacts,
+		.max_size = max_serializable_xacts,
 
-	SerializableXidHash = ShmemInitHash("SERIALIZABLEXID hash",
-										max_serializable_xacts,
-										max_serializable_xacts,
-										&info,
-										HASH_ELEM | HASH_BLOBS |
-										HASH_FIXED_SIZE);
+		.ptr = &SerializableXidHash,
+		.hash_info.keysize = sizeof(SERIALIZABLEXIDTAG),
+		.hash_info.entrysize = sizeof(SERIALIZABLEXID),
+		.hash_flags = HASH_ELEM | HASH_BLOBS | HASH_FIXED_SIZE,
+		});
 
 	/*
 	 * Allocate space for tracking rw-conflicts in lists attached to the
@@ -1217,58 +1234,53 @@ PredicateLockShmemInit(void)
 	 */
 	max_rw_conflicts = max_serializable_xacts * 5;
 
-	requestSize = RWConflictPoolHeaderDataSize +
-		mul_size((Size) max_rw_conflicts,
-				 RWConflictDataSize);
+	ShmemRequestStruct(&RWConflictPoolShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "RWConflictPool",
+		.size = RWConflictPoolHeaderDataSize + mul_size((Size) max_rw_conflicts,
+														RWConflictDataSize),
+		.ptr = (void **) &RWConflictPool,
+	});
 
-	RWConflictPool = ShmemInitStruct("RWConflictPool",
-									 requestSize,
-									 &found);
-	Assert(found == IsUnderPostmaster);
-
-	/*
-	 * Create or attach to the header for the list of finished serializable
-	 * transactions.
-	 */
-	FinishedSerializableTransactions = (dlist_head *)
-		ShmemInitStruct("FinishedSerializableTransactions",
-						sizeof(dlist_head),
-						&found);
-	Assert(found == IsUnderPostmaster);
+	ShmemRequestStruct(&FinishedSerializableShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "FinishedSerializableTransactions",
+		.size = sizeof(dlist_head),
+		.ptr = (void **) &FinishedSerializableTransactions,
+	});
 
 	/*
 	 * Initialize the SLRU storage for old committed serializable
 	 * transactions.
 	 */
-	SerialSlruCtl->PagePrecedes = SerialPagePrecedesLogically;
-	SerialSlruCtl->errdetail_for_io_error = serial_errdetail_for_io_error;
-	SimpleLruInit(SerialSlruCtl, "serializable",
-				  serializable_buffers, 0, "pg_serial",
-				  LWTRANCHE_SERIAL_BUFFER, LWTRANCHE_SERIAL_SLRU,
-				  SYNC_HANDLER_NONE, false);
+	SimpleLruRequest(&SerialSlruDesc, &(SlruRequestOpts) {
+		.name = "serializable",
+		.Dir = "pg_serial",
+		.long_segment_names = false,
+
+		.nslots = serializable_buffers,
+
+		.sync_handler = SYNC_HANDLER_NONE,
+		.PagePrecedes = SerialPagePrecedesLogically,
+		.errdetail_for_io_error = serial_errdetail_for_io_error,
+
+		.buffer_tranche_id = LWTRANCHE_SERIAL_BUFFER,
+		.bank_tranche_id = LWTRANCHE_SERIAL_SLRU,
+		});
 #ifdef USE_ASSERT_CHECKING
 	SerialPagePrecedesLogicallyUnitTests();
 #endif
-	SlruPagePrecedesUnitTests(SerialSlruCtl, SERIAL_ENTRIESPERPAGE);
 
-	/*
-	 * Create or attach to the SerialControl structure.
-	 */
-	serialControl = (SerialControl)
-		ShmemInitStruct("SerialControlData", sizeof(SerialControlData), &found);
-	Assert(found == IsUnderPostmaster);
+	ShmemRequestStruct(&SerialControlShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "SerialControlData",
+		.size = sizeof(SerialControlData),
+		.ptr = (void **) &serialControl,
+	});
+}
 
-	/*
-	 * If we just attached to existing shared memory (EXEC_BACKEND), we're all
-	 * done.  Otherwise, during postmaster startup proceed to initialize the
-	 * shared memory.
-	 */
-	if (IsUnderPostmaster)
-	{
-		/* This never changes, so let's keep a local copy. */
-		OldCommittedSxact = PredXact->OldCommittedSxact;
-		return;
-	}
+static void
+PredicateLockShmemInit(void *arg)
+{
+	int			max_rw_conflicts;
+	bool		found;
 
 	/*
 	 * Reserve a dummy entry in the hash table; we use it to make sure there's
@@ -1280,7 +1292,6 @@ PredicateLockShmemInit(void)
 					   HASH_ENTER, &found);
 	Assert(!found);
 
-	/* Initialize PredXact list */
 	dlist_init(&PredXact->availableList);
 	dlist_init(&PredXact->activeList);
 	PredXact->SxactGlobalXmin = InvalidTransactionId;
@@ -1322,6 +1333,9 @@ PredicateLockShmemInit(void)
 	dlist_init(&RWConflictPool->availableList);
 	RWConflictPool->element = (RWConflict) ((char *) RWConflictPool +
 											RWConflictPoolHeaderDataSize);
+
+	max_rw_conflicts = max_serializable_xacts * 5;
+
 	/* Add all elements to available list, clean. */
 	for (int i = 0; i < max_rw_conflicts; i++)
 	{
@@ -1338,63 +1352,28 @@ PredicateLockShmemInit(void)
 	serialControl->headXid = InvalidTransactionId;
 	serialControl->tailXid = InvalidTransactionId;
 	LWLockRelease(SerialControlLock);
-}
-
-/*
- * Estimate shared-memory space used for predicate lock table
- */
-Size
-PredicateLockShmemSize(void)
-{
-	Size		size = 0;
-	int64		max_predicate_lock_targets;
-	int64		max_predicate_locks;
-	int64		max_serializable_xacts;
-	int64		max_rw_conflicts;
-
-	/* predicate lock target hash table */
-	max_predicate_lock_targets = NPREDICATELOCKTARGETENTS();
-	size = add_size(size, hash_estimate_size(max_predicate_lock_targets,
-											 sizeof(PREDICATELOCKTARGET)));
-
-	/* predicate lock hash table */
-	max_predicate_locks = max_predicate_lock_targets * 2;
-	size = add_size(size, hash_estimate_size(max_predicate_locks,
-											 sizeof(PREDICATELOCK)));
 
-	/*
-	 * Since NPREDICATELOCKTARGETENTS is only an estimate, add 10% safety
-	 * margin.
-	 */
-	size = add_size(size, size / 10);
-
-	/* transaction list */
-	max_serializable_xacts = (MaxBackends + max_prepared_xacts) * 10;
-	size = add_size(size, PredXactListDataSize);
-	size = add_size(size, mul_size((Size) max_serializable_xacts,
-								   sizeof(SERIALIZABLEXACT)));
-
-	/* transaction xid table */
-	size = add_size(size, hash_estimate_size(max_serializable_xacts,
-											 sizeof(SERIALIZABLEXID)));
+	/* This never changes, so let's keep a local copy. */
+	OldCommittedSxact = PredXact->OldCommittedSxact;
 
-	/* rw-conflict pool */
-	max_rw_conflicts = max_serializable_xacts * 5;
-	size = add_size(size, RWConflictPoolHeaderDataSize);
-	size = add_size(size, mul_size((Size) max_rw_conflicts,
-								   RWConflictDataSize));
+	/* Pre-calculate the hash and partition lock of the scratch entry */
+	ScratchTargetTagHash = PredicateLockTargetTagHashCode(&ScratchTargetTag);
+	ScratchPartitionLock = PredicateLockHashPartitionLock(ScratchTargetTagHash);
 
-	/* Head for list of finished serializable transactions. */
-	size = add_size(size, sizeof(dlist_head));
+	SlruPagePrecedesUnitTests(SerialSlruCtl, SERIAL_ENTRIESPERPAGE);
+}
 
-	/* Shared memory structures for SLRU tracking of old committed xids. */
-	size = add_size(size, sizeof(SerialControlData));
-	size = add_size(size, SimpleLruShmemSize(serializable_buffers, 0));
+static void
+PredicateLockShmemAttach(void *arg)
+{
+	/* This never changes, so let's keep a local copy. */
+	OldCommittedSxact = PredXact->OldCommittedSxact;
 
-	return size;
+	/* Pre-calculate the hash and partition lock of the scratch entry */
+	ScratchTargetTagHash = PredicateLockTargetTagHashCode(&ScratchTargetTag);
+	ScratchPartitionLock = PredicateLockHashPartitionLock(ScratchTargetTagHash);
 }
 
-
 /*
  * Compute the hash code associated with a PREDICATELOCKTAG.
  *
diff --git a/src/backend/utils/activity/pgstat_slru.c b/src/backend/utils/activity/pgstat_slru.c
index 2190f388eae..f4dfe8697d7 100644
--- a/src/backend/utils/activity/pgstat_slru.c
+++ b/src/backend/utils/activity/pgstat_slru.c
@@ -119,6 +119,7 @@ pgstat_get_slru_index(const char *name)
 {
 	int			i;
 
+	Assert(name);
 	for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
 	{
 		if (strcmp(slru_names[i], name) == 0)
diff --git a/src/include/access/clog.h b/src/include/access/clog.h
index a1cfed5f43c..7894998c763 100644
--- a/src/include/access/clog.h
+++ b/src/include/access/clog.h
@@ -40,8 +40,6 @@ extern void TransactionIdSetTreeStatus(TransactionId xid, int nsubxids,
 									   TransactionId *subxids, XidStatus status, XLogRecPtr lsn);
 extern XidStatus TransactionIdGetStatus(TransactionId xid, XLogRecPtr *lsn);
 
-extern Size CLOGShmemSize(void);
-extern void CLOGShmemInit(void);
 extern void BootStrapCLOG(void);
 extern void StartupCLOG(void);
 extern void TrimCLOG(void);
diff --git a/src/include/access/commit_ts.h b/src/include/access/commit_ts.h
index 49ee21cd5d2..825ccda90ed 100644
--- a/src/include/access/commit_ts.h
+++ b/src/include/access/commit_ts.h
@@ -27,8 +27,6 @@ extern bool TransactionIdGetCommitTsData(TransactionId xid,
 extern TransactionId GetLatestCommitTsData(TimestampTz *ts,
 										   ReplOriginId *nodeid);
 
-extern Size CommitTsShmemSize(void);
-extern void CommitTsShmemInit(void);
 extern void BootStrapCommitTs(void);
 extern void StartupCommitTs(void);
 extern void CommitTsParameterChange(bool newvalue, bool oldvalue);
diff --git a/src/include/access/multixact.h b/src/include/access/multixact.h
index 2ae8b571dcc..6be5299ab68 100644
--- a/src/include/access/multixact.h
+++ b/src/include/access/multixact.h
@@ -121,8 +121,6 @@ extern void AtEOXact_MultiXact(void);
 extern void AtPrepare_MultiXact(void);
 extern void PostPrepare_MultiXact(FullTransactionId fxid);
 
-extern Size MultiXactShmemSize(void);
-extern void MultiXactShmemInit(void);
 extern void BootStrapMultiXact(void);
 extern void StartupMultiXact(void);
 extern void TrimMultiXact(void);
diff --git a/src/include/access/slru.h b/src/include/access/slru.h
index f966d0d9fe7..d4c669aa7a2 100644
--- a/src/include/access/slru.h
+++ b/src/include/access/slru.h
@@ -16,6 +16,7 @@
 #include "access/transam.h"
 #include "access/xlogdefs.h"
 #include "storage/lwlock.h"
+#include "storage/shmem.h"
 #include "storage/sync.h"
 
 /*
@@ -106,23 +107,20 @@ typedef struct SlruSharedData
 
 typedef SlruSharedData *SlruShared;
 
-/*
- * SlruCtlData is an unshared structure that points to the active information
- * in shared memory.
- */
-typedef struct SlruCtlData
+typedef struct SlruRequestOpts
 {
-	SlruShared	shared;
-
-	/* Number of banks in this SLRU. */
-	uint16		nbanks;
+	ShmemRequestStructOpts base;
 
 	/*
-	 * If true, use long segment file names.  Otherwise, use short file names.
-	 *
-	 * For details about the file name format, see SlruFileName().
+	 * name of SLRU.  (This is user-visible, pick with care!)
 	 */
-	bool		long_segment_names;
+	const char *name;
+
+	/* number of page slots to use. */
+	int			nslots;
+
+	/* number of LSN groups per page (set to zero if not relevant). */
+	int			nlsns;
 
 	/*
 	 * Which sync handler function to use when handing sync requests over to
@@ -130,6 +128,19 @@ typedef struct SlruCtlData
 	 */
 	SyncRequestHandler sync_handler;
 
+	/*
+	 * PGDATA-relative subdirectory that will contain the files.
+	 */
+	const char *Dir;
+
+	/*
+	 * If true, use long segment file names.  Otherwise, use short file names.
+	 *
+	 * For details about the file name format, see SlruFileName().
+	 */
+	bool		long_segment_names;
+
+
 	/*
 	 * Decide whether a page is "older" for truncation and as a hint for
 	 * evicting pages in LRU order.  Return true if every entry of the first
@@ -153,13 +164,28 @@ typedef struct SlruCtlData
 	int			(*errdetail_for_io_error) (const void *opaque_data);
 
 	/*
-	 * Dir is set during SimpleLruInit and does not change thereafter. Since
-	 * it's always the same, it doesn't need to be in shared memory.
+	 * Tranche IDs to use for the SLRU's per-buffer and per-bank LWLocks.  If
+	 * these are left as zeros, new tranches will be assigned dynamically.
 	 */
-	char		Dir[64];
-} SlruCtlData;
+	int			buffer_tranche_id;
+	int			bank_tranche_id;
+} SlruRequestOpts;
 
-typedef SlruCtlData *SlruCtl;
+/*
+ * SlruDesc is an unshared structure that points to the active information
+ * in shared memory.
+ */
+typedef struct SlruDesc
+{
+	ShmemStructDesc base;
+
+	SlruRequestOpts options;
+
+	SlruShared	shared;
+
+	/* Number of banks in this SLRU. */
+	uint16		nbanks;
+} SlruDesc;
 
 /*
  * Get the SLRU bank lock for given SlruCtl and the pageno.
@@ -168,48 +194,48 @@ typedef SlruCtlData *SlruCtl;
  * respective bank.
  */
 static inline LWLock *
-SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
+SimpleLruGetBankLock(SlruDesc *ctl, int64 pageno)
 {
 	int			bankno;
 
+	Assert(ctl->nbanks != 0);
 	bankno = pageno % ctl->nbanks;
 	return &(ctl->shared->bank_locks[bankno].lock);
 }
 
-extern Size SimpleLruShmemSize(int nslots, int nlsns);
+extern void SimpleLruRequest(SlruDesc *desc, const SlruRequestOpts *options);
 extern int	SimpleLruAutotuneBuffers(int divisor, int max);
-extern void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
-						  const char *subdir, int buffer_tranche_id,
-						  int bank_tranche_id, SyncRequestHandler sync_handler,
-						  bool long_segment_names);
-extern int	SimpleLruZeroPage(SlruCtl ctl, int64 pageno);
-extern void SimpleLruZeroAndWritePage(SlruCtl ctl, int64 pageno);
-extern int	SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok,
+extern int	SimpleLruZeroPage(SlruDesc *ctl, int64 pageno);
+extern void SimpleLruZeroAndWritePage(SlruDesc *ctl, int64 pageno);
+extern int	SimpleLruReadPage(SlruDesc *ctl, int64 pageno, bool write_ok,
 							  const void *opaque_data);
-extern int	SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno,
+extern int	SimpleLruReadPage_ReadOnly(SlruDesc *ctl, int64 pageno,
 									   const void *opaque_data);
-extern void SimpleLruWritePage(SlruCtl ctl, int slotno);
-extern void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied);
+extern void SimpleLruWritePage(SlruDesc *ctl, int slotno);
+extern void SimpleLruWriteAll(SlruDesc *ctl, bool allow_redirtied);
 #ifdef USE_ASSERT_CHECKING
-extern void SlruPagePrecedesUnitTests(SlruCtl ctl, int per_page);
+extern void SlruPagePrecedesUnitTests(SlruDesc *ctl, int per_page);
 #else
 #define SlruPagePrecedesUnitTests(ctl, per_page) do {} while (0)
 #endif
-extern void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage);
-extern bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno);
+extern void SimpleLruTruncate(SlruDesc *ctl, int64 cutoffPage);
+extern bool SimpleLruDoesPhysicalPageExist(SlruDesc *ctl, int64 pageno);
 
-typedef bool (*SlruScanCallback) (SlruCtl ctl, char *filename, int64 segpage,
+typedef bool (*SlruScanCallback) (SlruDesc *ctl, char *filename, int64 segpage,
 								  void *data);
-extern bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data);
-extern void SlruDeleteSegment(SlruCtl ctl, int64 segno);
+extern bool SlruScanDirectory(SlruDesc *ctl, SlruScanCallback callback, void *data);
+extern void SlruDeleteSegment(SlruDesc *ctl, int64 segno);
 
-extern int	SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path);
+extern int	SlruSyncFileTag(SlruDesc *ctl, const FileTag *ftag, char *path);
 
 /* SlruScanDirectory public callbacks */
-extern bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename,
+extern bool SlruScanDirCbReportPresence(SlruDesc *ctl, char *filename,
 										int64 segpage, void *data);
-extern bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int64 segpage,
+extern bool SlruScanDirCbDeleteAll(SlruDesc *ctl, char *filename, int64 segpage,
 								   void *data);
 extern bool check_slru_buffers(const char *name, int *newval);
 
+extern void shmem_slru_init(ShmemStructDesc *base_desc, const ShmemRequestStructOpts *options);
+extern void shmem_slru_attach(ShmemStructDesc *base_desc, const ShmemRequestStructOpts *options);
+
 #endif							/* SLRU_H */
diff --git a/src/include/access/subtrans.h b/src/include/access/subtrans.h
index 11b7355dbdf..d986cd9e802 100644
--- a/src/include/access/subtrans.h
+++ b/src/include/access/subtrans.h
@@ -15,8 +15,6 @@ extern void SubTransSetParent(TransactionId xid, TransactionId parent);
 extern TransactionId SubTransGetParent(TransactionId xid);
 extern TransactionId SubTransGetTopmostTransaction(TransactionId xid);
 
-extern Size SUBTRANSShmemSize(void);
-extern void SUBTRANSShmemInit(void);
 extern void BootStrapSUBTRANS(void);
 extern void StartupSUBTRANS(TransactionId oldestActiveXID);
 extern void CheckPointSUBTRANS(void);
diff --git a/src/include/commands/async.h b/src/include/commands/async.h
index 3baae7cb8dc..202e4aa5e74 100644
--- a/src/include/commands/async.h
+++ b/src/include/commands/async.h
@@ -19,9 +19,6 @@ extern PGDLLIMPORT bool Trace_notify;
 extern PGDLLIMPORT int max_notify_queue_pages;
 extern PGDLLIMPORT volatile sig_atomic_t notifyInterruptPending;
 
-extern Size AsyncShmemSize(void);
-extern void AsyncShmemInit(void);
-
 extern void NotifyMyFrontEnd(const char *channel,
 							 const char *payload,
 							 int32 srcPid);
diff --git a/src/include/storage/predicate.h b/src/include/storage/predicate.h
index a5ac55b8f7e..443bffb58fd 100644
--- a/src/include/storage/predicate.h
+++ b/src/include/storage/predicate.h
@@ -41,11 +41,6 @@ typedef void *SerializableXactHandle;
 /*
  * function prototypes
  */
-
-/* housekeeping for shared memory predicate lock structures */
-extern void PredicateLockShmemInit(void);
-extern Size PredicateLockShmemSize(void);
-
 extern void CheckPointPredicate(void);
 
 /* predicate lock reporting */
diff --git a/src/include/storage/shmem.h b/src/include/storage/shmem.h
index a07fccfb1ba..f59460ae10f 100644
--- a/src/include/storage/shmem.h
+++ b/src/include/storage/shmem.h
@@ -29,6 +29,7 @@ typedef enum
 {
 	SHMEM_KIND_STRUCT = 0,		/* plain, contiguous area of memory */
 	SHMEM_KIND_HASH,			/* a hash table */
+	SHMEM_KIND_SLRU,			/* SLRU buffers and control structures */
 } ShmemAreaKind;
 
 /*
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 5c11b2b3499..63d1d60ae36 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -25,6 +25,13 @@ PG_SHMEM_SUBSYSTEM(DSMRegistryShmemCallbacks)
 
 /* xlog, clog, and buffers */
 PG_SHMEM_SUBSYSTEM(VarsupShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(CLOGShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(CommitTsShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(SUBTRANSShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(MultiXactShmemCallbacks)
+
+/* predicate lock manager */
+PG_SHMEM_SUBSYSTEM(PredicateLockShmemCallbacks)
 
 /* process table */
 PG_SHMEM_SUBSYSTEM(ProcGlobalShmemCallbacks)
@@ -38,5 +45,6 @@ PG_SHMEM_SUBSYSTEM(PMSignalShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(ProcSignalShmemCallbacks)
 
 /* other modules that need some shared memory space */
+PG_SHMEM_SUBSYSTEM(AsyncShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(WaitEventCustomShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(InjectionPointShmemCallbacks)
diff --git a/src/test/modules/test_slru/test_slru.c b/src/test/modules/test_slru/test_slru.c
index e4bd2af0bf5..c017838a694 100644
--- a/src/test/modules/test_slru/test_slru.c
+++ b/src/test/modules/test_slru/test_slru.c
@@ -40,14 +40,22 @@ PG_FUNCTION_INFO_V1(test_slru_delete_all);
 /* Number of SLRU page slots */
 #define NUM_TEST_BUFFERS		16
 
-static SlruCtlData TestSlruCtlData;
-#define TestSlruCtl			(&TestSlruCtlData)
+static void test_slru_shmem_request(void *arg);
+static bool test_slru_page_precedes_logically(int64 page1, int64 page2);
+static int	test_slru_errdetail_for_io_error(const void *opaque_data);
 
-static shmem_request_hook_type prev_shmem_request_hook = NULL;
-static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
+static const char *TestSlruDir = "pg_test_slru";
+
+static SlruDesc TestSlruDesc;
+
+static const ShmemCallbacks test_slru_shmem_callbacks = {
+	.request_fn = test_slru_shmem_request
+};
+
+#define TestSlruCtl			(&TestSlruDesc)
 
 static bool
-test_slru_scan_cb(SlruCtl ctl, char *filename, int64 segpage, void *data)
+test_slru_scan_cb(SlruDesc *ctl, char *filename, int64 segpage, void *data)
 {
 	elog(NOTICE, "Calling test_slru_scan_cb()");
 	return SlruScanDirCbDeleteAll(ctl, filename, segpage, data);
@@ -190,20 +198,6 @@ test_slru_delete_all(PG_FUNCTION_ARGS)
 	PG_RETURN_VOID();
 }
 
-/*
- * Module load callbacks and initialization.
- */
-
-static void
-test_slru_shmem_request(void)
-{
-	if (prev_shmem_request_hook)
-		prev_shmem_request_hook();
-
-	/* reserve shared memory for the test SLRU */
-	RequestAddinShmemSpace(SimpleLruShmemSize(NUM_TEST_BUFFERS, 0));
-}
-
 static bool
 test_slru_page_precedes_logically(int64 page1, int64 page2)
 {
@@ -218,48 +212,6 @@ test_slru_errdetail_for_io_error(const void *opaque_data)
 	return errdetail("Could not access test_slru entry %u.", xid);
 }
 
-static void
-test_slru_shmem_startup(void)
-{
-	/*
-	 * Short segments names are well tested elsewhere so in this test we are
-	 * focusing on long names.
-	 */
-	const bool	long_segment_names = true;
-	const char	slru_dir_name[] = "pg_test_slru";
-	int			test_tranche_id = -1;
-	int			test_buffer_tranche_id = -1;
-
-	if (prev_shmem_startup_hook)
-		prev_shmem_startup_hook();
-
-	/*
-	 * Create the SLRU directory if it does not exist yet, from the root of
-	 * the data directory.
-	 */
-	(void) MakePGDirectory(slru_dir_name);
-
-	/*
-	 * Initialize the SLRU facility.  In EXEC_BACKEND builds, the
-	 * shmem_startup_hook is called in the postmaster and in each backend, but
-	 * we only need to generate the LWLock tranches once.  Note that these
-	 * tranche ID variables are not used by SimpleLruInit() when
-	 * IsUnderPostmaster is true.
-	 */
-	if (!IsUnderPostmaster)
-	{
-		test_tranche_id = LWLockNewTrancheId("test_slru_tranche");
-		test_buffer_tranche_id = LWLockNewTrancheId("test_buffer_tranche");
-	}
-
-	TestSlruCtl->PagePrecedes = test_slru_page_precedes_logically;
-	TestSlruCtl->errdetail_for_io_error = test_slru_errdetail_for_io_error;
-	SimpleLruInit(TestSlruCtl, "TestSLRU",
-				  NUM_TEST_BUFFERS, 0, slru_dir_name,
-				  test_buffer_tranche_id, test_tranche_id, SYNC_HANDLER_NONE,
-				  long_segment_names);
-}
-
 void
 _PG_init(void)
 {
@@ -269,9 +221,37 @@ _PG_init(void)
 				 errdetail("\"%s\" must be loaded with \"shared_preload_libraries\".",
 						   "test_slru")));
 
-	prev_shmem_request_hook = shmem_request_hook;
-	shmem_request_hook = test_slru_shmem_request;
+	/*
+	 * Create the SLRU directory if it does not exist yet, from the root of
+	 * the data directory.
+	 */
+	(void) MakePGDirectory(TestSlruDir);
 
-	prev_shmem_startup_hook = shmem_startup_hook;
-	shmem_startup_hook = test_slru_shmem_startup;
+	RegisterShmemCallbacks(&test_slru_shmem_callbacks);
+}
+
+static void
+test_slru_shmem_request(void *arg)
+{
+	SimpleLruRequest(&TestSlruDesc, &(SlruRequestOpts) {
+		.name = "TestSLRU",
+		.Dir = TestSlruDir,
+
+		/*
+		 * Short segments names are well tested elsewhere so in this test we are
+		 * focusing on long names.
+		 */
+		.long_segment_names = true,
+
+		.nslots = NUM_TEST_BUFFERS,
+		.nlsns = 0,
+
+		.sync_handler = SYNC_HANDLER_NONE,
+		.PagePrecedes = test_slru_page_precedes_logically,
+		.errdetail_for_io_error = test_slru_errdetail_for_io_error,
+
+		/* let slru.c assign these */
+		.buffer_tranche_id = 0,
+		.bank_tranche_id = 0,
+	});
 }
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 139cb6f9da5..b657f8956dc 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2886,10 +2886,10 @@ SlotInvalidationCauseMap
 SlotNumber
 SlotSyncCtxStruct
 SlotSyncSkipReason
-SlruCtl
-SlruCtlData
+SlruDesc
 SlruErrorCause
 SlruPageStatus
+SlruRequestOpts
 SlruScanCallback
 SlruSegState
 SlruShared
-- 
2.47.3



  [text/x-patch] v8-0014-Convert-AIO-to-the-new-interface.patch (14.8K, 15-v8-0014-Convert-AIO-to-the-new-interface.patch)
  download | inline diff:
From 5556f2b13649378f035ac99446558485619869e9 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Sat, 21 Mar 2026 12:43:16 +0200
Subject: [PATCH v8 14/16] Convert AIO to the new interface

This replaces the "shmem_size" and "shmem_init" callbacks in the IO
methods table with the same ShmemCallback struct that we now use in
other subsystems
---
 src/backend/storage/aio/aio_init.c        | 119 ++++++++++++++--------
 src/backend/storage/aio/method_io_uring.c |  42 ++++----
 src/backend/storage/aio/method_worker.c   |  84 ++++++++-------
 src/backend/storage/ipc/ipci.c            |   2 -
 src/include/storage/aio_internal.h        |  16 +--
 src/include/storage/aio_subsys.h          |   4 -
 src/include/storage/subsystemlist.h       |   3 +
 7 files changed, 155 insertions(+), 115 deletions(-)

diff --git a/src/backend/storage/aio/aio_init.c b/src/backend/storage/aio/aio_init.c
index d3c68d8b04c..54ab1238131 100644
--- a/src/backend/storage/aio/aio_init.c
+++ b/src/backend/storage/aio/aio_init.c
@@ -23,16 +23,46 @@
 #include "storage/ipc.h"
 #include "storage/proc.h"
 #include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "utils/guc.h"
 
 
-
-static Size
-AioCtlShmemSize(void)
-{
-	/* pgaio_ctl itself */
-	return sizeof(PgAioCtl);
-}
+static void AioShmemRequest(void *arg);
+static void AioShmemInit(void *arg);
+static void AioShmemAttach(void *arg);
+
+const ShmemCallbacks AioShmemCallbacks = {
+	.request_fn = AioShmemRequest,
+	.init_fn = AioShmemInit,
+	.attach_fn = AioShmemAttach,
+};
+
+static ShmemStructDesc AioCtlShmemDesc = {
+	.name = "AioCtl",
+	.size = sizeof(PgAioCtl),
+	.ptr = (void **) &pgaio_ctl,
+};
+
+static PgAioBackend *AioBackendShmemPtr;
+static ShmemStructDesc AioBackendShmemDesc = {
+	.name = "AioBackend",
+	.ptr = (void **) &AioBackendShmemPtr,
+};
+static PgAioHandle *AioHandleShmemPtr;
+static ShmemStructDesc AioHandleShmemDesc = {
+	.name = "AioHandle",
+	.ptr = (void **) &AioHandleShmemPtr,
+};
+static struct iovec *AioHandleIOVShmemPtr;
+static ShmemStructDesc AioHandleIOVShmemDesc = {
+	.name = "AioHandleIOV",
+	.ptr = (void **) &AioHandleIOVShmemPtr,
+};
+static uint64 *AioHandleDataShmemPtr;
+static ShmemStructDesc AioHandleDataShmemDesc = {
+	.name = "AioHandleData",
+	.ptr = (void **) &AioHandleDataShmemPtr,
+};
 
 static uint32
 AioProcs(void)
@@ -109,10 +139,13 @@ AioChooseMaxConcurrency(void)
 	return Min(max_proportional_pins, 64);
 }
 
-Size
-AioShmemSize(void)
+/*
+ * Register shared memory area for AIO subsystem.
+ */
+static void
+AioShmemRequest(void *arg)
 {
-	Size		sz = 0;
+	/* Resolve io_max_concurrency if not already done. */
 
 	/*
 	 * We prefer to report this value's source as PGC_S_DYNAMIC_DEFAULT.
@@ -132,48 +165,41 @@ AioShmemSize(void)
 							PGC_S_OVERRIDE);
 	}
 
-	sz = add_size(sz, AioCtlShmemSize());
-	sz = add_size(sz, AioBackendShmemSize());
-	sz = add_size(sz, AioHandleShmemSize());
-	sz = add_size(sz, AioHandleIOVShmemSize());
-	sz = add_size(sz, AioHandleDataShmemSize());
+	ShmemRequestStruct(&AioCtlShmemDesc);
 
-	/* Reserve space for method specific resources. */
-	if (pgaio_method_ops->shmem_size)
-		sz = add_size(sz, pgaio_method_ops->shmem_size());
+	AioBackendShmemDesc.size = AioBackendShmemSize();
+	ShmemRequestStruct(&AioBackendShmemDesc);
 
-	return sz;
+	AioHandleShmemDesc.size = AioHandleShmemSize();
+	ShmemRequestStruct(&AioHandleShmemDesc);
+
+	AioHandleIOVShmemDesc.size = AioHandleIOVShmemSize();
+	ShmemRequestStruct(&AioHandleIOVShmemDesc);
+
+	AioHandleDataShmemDesc.size = AioHandleDataShmemSize();
+	ShmemRequestStruct(&AioHandleDataShmemDesc);
+
+	if (pgaio_method_ops->shmem_callbacks.request_fn)
+		pgaio_method_ops->shmem_callbacks.request_fn(pgaio_method_ops->shmem_callbacks.request_fn_arg);
 }
 
-void
-AioShmemInit(void)
+/*
+ * Initialize AIO shared memory during postmaster startup.
+ */
+static void
+AioShmemInit(void *arg)
 {
-	bool		found;
 	uint32		io_handle_off = 0;
 	uint32		iovec_off = 0;
 	uint32		per_backend_iovecs = io_max_concurrency * io_max_combine_limit;
 
-	pgaio_ctl = (PgAioCtl *)
-		ShmemInitStruct("AioCtl", AioCtlShmemSize(), &found);
-
-	if (found)
-		goto out;
-
-	memset(pgaio_ctl, 0, AioCtlShmemSize());
-
 	pgaio_ctl->io_handle_count = AioProcs() * io_max_concurrency;
 	pgaio_ctl->iovec_count = AioProcs() * per_backend_iovecs;
 
-	pgaio_ctl->backend_state = (PgAioBackend *)
-		ShmemInitStruct("AioBackend", AioBackendShmemSize(), &found);
-
-	pgaio_ctl->io_handles = (PgAioHandle *)
-		ShmemInitStruct("AioHandle", AioHandleShmemSize(), &found);
-
-	pgaio_ctl->iovecs = (struct iovec *)
-		ShmemInitStruct("AioHandleIOV", AioHandleIOVShmemSize(), &found);
-	pgaio_ctl->handle_data = (uint64 *)
-		ShmemInitStruct("AioHandleData", AioHandleDataShmemSize(), &found);
+	pgaio_ctl->backend_state = AioBackendShmemPtr;
+	pgaio_ctl->io_handles = AioHandleShmemPtr;
+	pgaio_ctl->iovecs = AioHandleIOVShmemPtr;
+	pgaio_ctl->handle_data = AioHandleDataShmemPtr;
 
 	for (int procno = 0; procno < AioProcs(); procno++)
 	{
@@ -208,10 +234,15 @@ AioShmemInit(void)
 		}
 	}
 
-out:
-	/* Initialize IO method specific resources. */
-	if (pgaio_method_ops->shmem_init)
-		pgaio_method_ops->shmem_init(!found);
+	if (pgaio_method_ops->shmem_callbacks.init_fn)
+		pgaio_method_ops->shmem_callbacks.init_fn(pgaio_method_ops->shmem_callbacks.init_fn_arg);
+}
+
+static void
+AioShmemAttach(void *arg)
+{
+	if (pgaio_method_ops->shmem_callbacks.attach_fn)
+		pgaio_method_ops->shmem_callbacks.attach_fn(pgaio_method_ops->shmem_callbacks.attach_fn_arg);
 }
 
 void
diff --git a/src/backend/storage/aio/method_io_uring.c b/src/backend/storage/aio/method_io_uring.c
index 4867ded35ea..df2d01d66fa 100644
--- a/src/backend/storage/aio/method_io_uring.c
+++ b/src/backend/storage/aio/method_io_uring.c
@@ -49,8 +49,8 @@
 
 
 /* Entry points for IoMethodOps. */
-static size_t pgaio_uring_shmem_size(void);
-static void pgaio_uring_shmem_init(bool first_time);
+static void pgaio_uring_shmem_request(void *arg);
+static void pgaio_uring_shmem_init(void *arg);
 static void pgaio_uring_init_backend(void);
 static int	pgaio_uring_submit(uint16 num_staged_ios, PgAioHandle **staged_ios);
 static void pgaio_uring_wait_one(PgAioHandle *ioh, uint64 ref_generation);
@@ -58,7 +58,6 @@ static void pgaio_uring_wait_one(PgAioHandle *ioh, uint64 ref_generation);
 /* helper functions */
 static void pgaio_uring_sq_from_io(PgAioHandle *ioh, struct io_uring_sqe *sqe);
 
-
 const IoMethodOps pgaio_uring_ops = {
 	/*
 	 * While io_uring mostly is OK with FDs getting closed while the IO is in
@@ -69,8 +68,8 @@ const IoMethodOps pgaio_uring_ops = {
 	 */
 	.wait_on_fd_before_close = true,
 
-	.shmem_size = pgaio_uring_shmem_size,
-	.shmem_init = pgaio_uring_shmem_init,
+	.shmem_callbacks.request_fn = pgaio_uring_shmem_request,
+	.shmem_callbacks.init_fn = pgaio_uring_shmem_init,
 	.init_backend = pgaio_uring_init_backend,
 
 	.submit = pgaio_uring_submit,
@@ -265,23 +264,34 @@ pgaio_uring_shmem_size(void)
 {
 	size_t		sz;
 
+	sz = pgaio_uring_context_shmem_size();
+	sz = add_size(sz, pgaio_uring_ring_shmem_size());
+
+	return sz;
+}
+
+static void
+pgaio_uring_shmem_request(void *arg)
+{
+	static ShmemStructDesc AioUringShmemDesc = {
+		.name = "AioUringContext",
+		.ptr = (void **) &pgaio_uring_contexts,
+	};
+
 	/*
 	 * Kernel and liburing support for various features influences how much
 	 * shmem we need, perform the necessary checks.
 	 */
 	pgaio_uring_check_capabilities();
 
-	sz = pgaio_uring_context_shmem_size();
-	sz = add_size(sz, pgaio_uring_ring_shmem_size());
-
-	return sz;
+	AioUringShmemDesc.size = pgaio_uring_shmem_size();
+	ShmemRequestStruct(&AioUringShmemDesc);
 }
 
 static void
-pgaio_uring_shmem_init(bool first_time)
+pgaio_uring_shmem_init(void *arg)
 {
 	int			TotalProcs = pgaio_uring_procs();
-	bool		found;
 	char	   *shmem;
 	size_t		ring_mem_remain = 0;
 	char	   *ring_mem_next = 0;
@@ -289,13 +299,11 @@ pgaio_uring_shmem_init(bool first_time)
 	/*
 	 * We allocate memory for all PgAioUringContext instances and, if
 	 * supported, the memory required for each of the io_uring instances, in
-	 * one ShmemInitStruct().
+	 * one combined allocation.
+	 *
+	 * pgaio_uring_contexts is already set to the base of the allocation.
 	 */
-	shmem = ShmemInitStruct("AioUringContext", pgaio_uring_shmem_size(), &found);
-	if (found)
-		return;
-
-	pgaio_uring_contexts = (PgAioUringContext *) shmem;
+	shmem = (char *) pgaio_uring_contexts;
 	shmem += pgaio_uring_context_shmem_size();
 
 	/* if supported, handle memory alignment / sizing for io_uring memory */
diff --git a/src/backend/storage/aio/method_worker.c b/src/backend/storage/aio/method_worker.c
index efe38e9f113..82c8b098a9e 100644
--- a/src/backend/storage/aio/method_worker.c
+++ b/src/backend/storage/aio/method_worker.c
@@ -41,6 +41,7 @@
 #include "storage/ipc.h"
 #include "storage/latch.h"
 #include "storage/proc.h"
+#include "storage/shmem.h"
 #include "tcop/tcopprot.h"
 #include "utils/injection_point.h"
 #include "utils/memdebug.h"
@@ -73,16 +74,20 @@ typedef struct PgAioWorkerControl
 } PgAioWorkerControl;
 
 
-static size_t pgaio_worker_shmem_size(void);
-static void pgaio_worker_shmem_init(bool first_time);
+static void pgaio_worker_shmem_request(void *arg);
+static void pgaio_worker_shmem_init(void *arg);
+static void pgaio_worker_shmem_attach(void *arg);
+
+static PgAioWorkerSubmissionQueue *io_worker_submission_queue;
 
 static bool pgaio_worker_needs_synchronous_execution(PgAioHandle *ioh);
 static int	pgaio_worker_submit(uint16 num_staged_ios, PgAioHandle **staged_ios);
 
 
 const IoMethodOps pgaio_worker_ops = {
-	.shmem_size = pgaio_worker_shmem_size,
-	.shmem_init = pgaio_worker_shmem_init,
+	.shmem_callbacks.request_fn = pgaio_worker_shmem_request,
+	.shmem_callbacks.init_fn = pgaio_worker_shmem_init,
+	.shmem_callbacks.attach_fn = pgaio_worker_shmem_attach,
 
 	.needs_synchronous_execution = pgaio_worker_needs_synchronous_execution,
 	.submit = pgaio_worker_submit,
@@ -95,7 +100,6 @@ int			io_workers = 3;
 
 static int	io_worker_queue_size = 64;
 static int	MyIoWorkerId;
-static PgAioWorkerSubmissionQueue *io_worker_submission_queue;
 static PgAioWorkerControl *io_worker_control;
 
 
@@ -116,50 +120,60 @@ pgaio_worker_control_shmem_size(void)
 		sizeof(PgAioWorkerSlot) * MAX_IO_WORKERS;
 }
 
-static size_t
-pgaio_worker_shmem_size(void)
+/*
+ * Set secondary AIO worker pointer from the combined allocation.
+ */
+static void
+pgaio_worker_set_secondary_ptr(void)
 {
-	size_t		sz;
 	int			queue_size;
+	Size		queue_sz = pgaio_worker_queue_shmem_size(&queue_size);
 
-	sz = pgaio_worker_queue_shmem_size(&queue_size);
-	sz = add_size(sz, pgaio_worker_control_shmem_size());
-
-	return sz;
+	io_worker_control = (PgAioWorkerControl *)
+		((char *) io_worker_submission_queue + MAXALIGN(queue_sz));
 }
 
 static void
-pgaio_worker_shmem_init(bool first_time)
+pgaio_worker_shmem_init(void *arg)
 {
-	bool		found;
 	int			queue_size;
 
-	io_worker_submission_queue =
-		ShmemInitStruct("AioWorkerSubmissionQueue",
-						pgaio_worker_queue_shmem_size(&queue_size),
-						&found);
-	if (!found)
-	{
-		io_worker_submission_queue->size = queue_size;
-		io_worker_submission_queue->head = 0;
-		io_worker_submission_queue->tail = 0;
-	}
+	pgaio_worker_queue_shmem_size(&queue_size);
+	io_worker_submission_queue->size = queue_size;
+	io_worker_submission_queue->head = 0;
+	io_worker_submission_queue->tail = 0;
+
+	pgaio_worker_set_secondary_ptr();
 
-	io_worker_control =
-		ShmemInitStruct("AioWorkerControl",
-						pgaio_worker_control_shmem_size(),
-						&found);
-	if (!found)
+	io_worker_control->idle_worker_mask = 0;
+	for (int i = 0; i < MAX_IO_WORKERS; ++i)
 	{
-		io_worker_control->idle_worker_mask = 0;
-		for (int i = 0; i < MAX_IO_WORKERS; ++i)
-		{
-			io_worker_control->workers[i].latch = NULL;
-			io_worker_control->workers[i].in_use = false;
-		}
+		io_worker_control->workers[i].latch = NULL;
+		io_worker_control->workers[i].in_use = false;
 	}
 }
 
+static void
+pgaio_worker_shmem_attach(void *arg)
+{
+	pgaio_worker_set_secondary_ptr();
+}
+
+static void
+pgaio_worker_shmem_request(void *arg)
+{
+	static ShmemStructDesc AioWorkerShmemDesc = {
+		.name = "AioWorkerSubmissionQueue",
+		.ptr = (void **) &io_worker_submission_queue,
+	};
+	int			queue_size;
+
+	AioWorkerShmemDesc.size =
+		MAXALIGN(pgaio_worker_queue_shmem_size(&queue_size)) +
+		pgaio_worker_control_shmem_size();
+	ShmemRequestStruct(&AioWorkerShmemDesc);
+}
+
 static int
 pgaio_worker_choose_idle(void)
 {
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 32255ead2bf..b945035d98a 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -118,7 +118,6 @@ CalculateShmemSize(void)
 	size = add_size(size, SyncScanShmemSize());
 	size = add_size(size, StatsShmemSize());
 	size = add_size(size, SlotSyncShmemSize());
-	size = add_size(size, AioShmemSize());
 	size = add_size(size, WaitLSNShmemSize());
 	size = add_size(size, LogicalDecodingCtlShmemSize());
 
@@ -307,7 +306,6 @@ CreateOrAttachShmemStructs(void)
 	BTreeShmemInit();
 	SyncScanShmemInit();
 	StatsShmemInit();
-	AioShmemInit();
 	WaitLSNShmemInit();
 	LogicalDecodingCtlShmemInit();
 }
diff --git a/src/include/storage/aio_internal.h b/src/include/storage/aio_internal.h
index 5feea15be9e..9dd8d63b25c 100644
--- a/src/include/storage/aio_internal.h
+++ b/src/include/storage/aio_internal.h
@@ -20,6 +20,8 @@
 #include "port/pg_iovec.h"
 #include "storage/aio.h"
 #include "storage/condition_variable.h"
+#include "storage/ipc.h"
+#include "storage/shmem.h"
 
 
 /*
@@ -267,20 +269,8 @@ typedef struct IoMethodOps
 	 */
 	bool		wait_on_fd_before_close;
 
-
 	/* global initialization */
-
-	/*
-	 * Amount of additional shared memory to reserve for the io_method. Called
-	 * just like a normal ipci.c style *Size() function. Optional.
-	 */
-	size_t		(*shmem_size) (void);
-
-	/*
-	 * Initialize shared memory. First time is true if AIO's shared memory was
-	 * just initialized, false otherwise. Optional.
-	 */
-	void		(*shmem_init) (bool first_time);
+	ShmemCallbacks shmem_callbacks;
 
 	/*
 	 * Per-backend initialization. Optional.
diff --git a/src/include/storage/aio_subsys.h b/src/include/storage/aio_subsys.h
index 276cb3e31c4..dd54869351f 100644
--- a/src/include/storage/aio_subsys.h
+++ b/src/include/storage/aio_subsys.h
@@ -20,12 +20,8 @@
 
 
 /* aio_init.c */
-extern Size AioShmemSize(void);
-extern void AioShmemInit(void);
-
 extern void pgaio_init_backend(void);
 
-
 /* aio.c */
 extern void pgaio_error_cleanup(void);
 extern void AtEOXact_Aio(bool is_commit);
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 63d1d60ae36..e8e06be30c2 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -48,3 +48,6 @@ PG_SHMEM_SUBSYSTEM(ProcSignalShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(AsyncShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(WaitEventCustomShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(InjectionPointShmemCallbacks)
+
+/* AIO subsystem. This delegates to the method-specific callbacks */
+PG_SHMEM_SUBSYSTEM(AioShmemCallbacks)
-- 
2.47.3



  [text/x-patch] v8-0015-Add-option-for-aligning-shmem-allocations.patch (3.9K, 16-v8-0015-Add-option-for-aligning-shmem-allocations.patch)
  download | inline diff:
From 9fdc8cef18e54f939dca0db203d80f1baecc86ae Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Sat, 21 Mar 2026 23:44:15 +0200
Subject: [PATCH v8 15/16] Add option for aligning shmem allocations

The buffer blocks (in the next commit) are IO-aligned. This might come
handy in other places too, so make it an explicit feature of
ShmemRequestStruct.
---
 src/backend/storage/ipc/shmem.c | 22 +++++++++++++---------
 src/include/storage/shmem.h     |  6 ++++++
 2 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index 595f1e582d5..d3808432ff1 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -237,7 +237,7 @@ typedef struct ShmemAllocatorData
 
 #define ShmemIndexLock (&ShmemAllocator->index_lock)
 
-static void *ShmemAllocRaw(Size size, Size *allocated_size);
+static void *ShmemAllocRaw(Size size, Size alignment, Size *allocated_size);
 
 /* shared memory global variables */
 
@@ -400,6 +400,7 @@ ShmemGetRequestedSize(void)
 
 		size = add_size(size, request->options->size);
 		size = add_size(size, request->options->extra_size);
+		size = add_size(size, request->options->alignment);
 	}
 
 	return size;
@@ -571,7 +572,7 @@ AttachOrInit(ShmemRequest *request, bool init_allowed, bool attach_allowed)
 		size_t		allocated_size;
 		void	   *structPtr;
 
-		structPtr = ShmemAllocRaw(request->options->size, &allocated_size);
+		structPtr = ShmemAllocRaw(request->options->size, request->options->alignment, &allocated_size);
 		if (structPtr == NULL)
 		{
 			/* out of memory; remove the failed ShmemIndex entry */
@@ -730,7 +731,7 @@ ShmemAlloc(Size size)
 	void	   *newSpace;
 	Size		allocated_size;
 
-	newSpace = ShmemAllocRaw(size, &allocated_size);
+	newSpace = ShmemAllocRaw(size, 0, &allocated_size);
 	if (!newSpace)
 		ereport(ERROR,
 				(errcode(ERRCODE_OUT_OF_MEMORY),
@@ -749,7 +750,7 @@ ShmemAllocNoError(Size size)
 {
 	Size		allocated_size;
 
-	return ShmemAllocRaw(size, &allocated_size);
+	return ShmemAllocRaw(size, 0, &allocated_size);
 }
 
 /*
@@ -759,8 +760,9 @@ ShmemAllocNoError(Size size)
  * be equal to the number requested plus any padding we choose to add.
  */
 static void *
-ShmemAllocRaw(Size size, Size *allocated_size)
+ShmemAllocRaw(Size size, Size alignment, Size *allocated_size)
 {
+	Size		rawStart;
 	Size		newStart;
 	Size		newFree;
 	void	   *newSpace;
@@ -776,14 +778,15 @@ ShmemAllocRaw(Size size, Size *allocated_size)
 	 * structures out to a power-of-two size - but without this, even that
 	 * won't be sufficient.
 	 */
-	size = CACHELINEALIGN(size);
-	*allocated_size = size;
+	if (alignment < PG_CACHE_LINE_SIZE)
+		alignment = PG_CACHE_LINE_SIZE;
 
 	Assert(ShmemSegHdr != NULL);
 
 	SpinLockAcquire(&ShmemAllocator->shmem_lock);
 
-	newStart = ShmemAllocator->free_offset;
+	rawStart = ShmemAllocator->free_offset;
+	newStart = TYPEALIGN(alignment, rawStart);
 
 	newFree = newStart + size;
 	if (newFree <= ShmemSegHdr->totalsize)
@@ -797,8 +800,9 @@ ShmemAllocRaw(Size size, Size *allocated_size)
 	SpinLockRelease(&ShmemAllocator->shmem_lock);
 
 	/* note this assert is okay with newSpace == NULL */
-	Assert(newSpace == (void *) CACHELINEALIGN(newSpace));
+	Assert(newSpace == (void *) TYPEALIGN(alignment, newSpace));
 
+	*allocated_size = newFree - rawStart;
 	return newSpace;
 }
 
diff --git a/src/include/storage/shmem.h b/src/include/storage/shmem.h
index f59460ae10f..150c86d5884 100644
--- a/src/include/storage/shmem.h
+++ b/src/include/storage/shmem.h
@@ -59,6 +59,12 @@ typedef struct ShmemRequestStructOpts
 
 	ssize_t		size;
 
+	/*
+	 * Alignment of the starting address. If not set, defaults to cacheline
+	 * boundary. Must be a power of two.
+	 */
+	size_t		alignment;
+
 	/*
 	 * Extra space to reserve in the shared memory segment, but it's not part
 	 * of the struct itself.  This is used for shared memory hash tables that
-- 
2.47.3



  [text/x-patch] v8-0016-Convert-all-remaining-subsystems-to-use-the-new-A.patch (117.5K, 17-v8-0016-Convert-all-remaining-subsystems-to-use-the-new-A.patch)
  download | inline diff:
From 85d226598d50aa78f9119f581d9ccfb931547a39 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Fri, 27 Mar 2026 02:31:06 +0200
Subject: [PATCH v8 16/16] Convert all remaining subsystems to use the new API

---
 src/backend/access/common/syncscan.c          |  79 ++++----
 src/backend/access/nbtree/nbtutils.c          |  56 +++---
 src/backend/access/transam/twophase.c         |  77 +++----
 src/backend/access/transam/xlog.c             |  86 ++++----
 src/backend/access/transam/xlogprefetcher.c   |  54 ++---
 src/backend/access/transam/xlogrecovery.c     |  36 ++--
 src/backend/access/transam/xlogwait.c         |  52 ++---
 src/backend/postmaster/autovacuum.c           |  81 ++++----
 src/backend/postmaster/bgworker.c             | 107 +++++-----
 src/backend/postmaster/checkpointer.c         |  58 +++---
 src/backend/postmaster/pgarch.c               |  46 +++--
 src/backend/postmaster/walsummarizer.c        |  64 +++---
 src/backend/replication/logical/launcher.c    |  58 +++---
 src/backend/replication/logical/logicalctl.c  |  30 +--
 src/backend/replication/logical/origin.c      |  61 +++---
 src/backend/replication/logical/slotsync.c    |  44 ++--
 src/backend/replication/slot.c                |  66 +++---
 src/backend/replication/walreceiverfuncs.c    |  52 ++---
 src/backend/replication/walsender.c           |  61 +++---
 src/backend/storage/aio/aio_init.c            |  71 +++----
 src/backend/storage/aio/method_io_uring.c     |  12 +-
 src/backend/storage/aio/method_worker.c       |  16 +-
 src/backend/storage/buffer/buf_init.c         | 164 +++++++--------
 src/backend/storage/buffer/buf_table.c        |  40 ++--
 src/backend/storage/buffer/freelist.c         |  94 ++++-----
 src/backend/storage/ipc/ipci.c                | 119 +----------
 src/backend/storage/lmgr/lock.c               | 126 ++++++------
 src/backend/utils/activity/backend_status.c   | 190 ++++++++----------
 src/backend/utils/activity/pgstat_shmem.c     | 161 ++++++++-------
 src/include/access/nbtree.h                   |   2 -
 src/include/access/syncscan.h                 |   2 -
 src/include/access/twophase.h                 |   3 -
 src/include/access/xlog.h                     |   2 -
 src/include/access/xlogprefetcher.h           |   3 -
 src/include/access/xlogrecovery.h             |   3 -
 src/include/access/xlogwait.h                 |   2 -
 src/include/pgstat.h                          |   4 -
 src/include/postmaster/autovacuum.h           |   4 -
 src/include/postmaster/bgworker_internals.h   |   2 -
 src/include/postmaster/bgwriter.h             |   3 -
 src/include/postmaster/pgarch.h               |   2 -
 src/include/postmaster/walsummarizer.h        |   2 -
 src/include/replication/logicalctl.h          |   2 -
 src/include/replication/logicallauncher.h     |   3 -
 src/include/replication/origin.h              |   4 -
 src/include/replication/slot.h                |   4 -
 src/include/replication/slotsync.h            |   2 -
 src/include/replication/walreceiver.h         |   2 -
 src/include/replication/walsender.h           |   2 -
 src/include/storage/buf_internals.h           |   6 +-
 src/include/storage/bufmgr.h                  |   4 -
 src/include/storage/lock.h                    |   2 -
 src/include/storage/subsystemlist.h           |  26 +++
 src/include/utils/backend_status.h            |   8 -
 .../injection_points/injection_points.c       |  60 ++----
 55 files changed, 1061 insertions(+), 1259 deletions(-)

diff --git a/src/backend/access/common/syncscan.c b/src/backend/access/common/syncscan.c
index 6fcfcb0e560..45db6bbc8b7 100644
--- a/src/backend/access/common/syncscan.c
+++ b/src/backend/access/common/syncscan.c
@@ -50,6 +50,7 @@
 #include "miscadmin.h"
 #include "storage/lwlock.h"
 #include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "utils/rel.h"
 
 
@@ -111,6 +112,14 @@ typedef struct ss_scan_locations_t
 #define SizeOfScanLocations(N) \
 	(offsetof(ss_scan_locations_t, items) + (N) * sizeof(ss_lru_item_t))
 
+static void SyncScanShmemRequest(void *arg);
+static void SyncScanShmemInit(void *arg);
+
+const ShmemCallbacks SyncScanShmemCallbacks = {
+	.request_fn = SyncScanShmemRequest,
+	.init_fn = SyncScanShmemInit,
+};
+
 /* Pointer to struct in shared memory */
 static ss_scan_locations_t *scan_locations;
 
@@ -120,58 +129,50 @@ static BlockNumber ss_search(RelFileLocator relfilelocator,
 
 
 /*
- * SyncScanShmemSize --- report amount of shared memory space needed
+ * SyncScanShmemRequest --- register this module's shared memory
  */
-Size
-SyncScanShmemSize(void)
+static void
+SyncScanShmemRequest(void *arg)
 {
-	return SizeOfScanLocations(SYNC_SCAN_NELEM);
+	static ShmemStructDesc SyncScanShmemDesc;
+
+	ShmemRequestStruct(&SyncScanShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Sync Scan Locations List",
+		.size = SizeOfScanLocations(SYNC_SCAN_NELEM),
+		.ptr = (void **) &scan_locations,
+	});
 }
 
 /*
  * SyncScanShmemInit --- initialize this module's shared memory
  */
-void
-SyncScanShmemInit(void)
+static void
+SyncScanShmemInit(void *arg)
 {
 	int			i;
-	bool		found;
 
-	scan_locations = (ss_scan_locations_t *)
-		ShmemInitStruct("Sync Scan Locations List",
-						SizeOfScanLocations(SYNC_SCAN_NELEM),
-						&found);
+	scan_locations->head = &scan_locations->items[0];
+	scan_locations->tail = &scan_locations->items[SYNC_SCAN_NELEM - 1];
 
-	if (!IsUnderPostmaster)
+	for (i = 0; i < SYNC_SCAN_NELEM; i++)
 	{
-		/* Initialize shared memory area */
-		Assert(!found);
-
-		scan_locations->head = &scan_locations->items[0];
-		scan_locations->tail = &scan_locations->items[SYNC_SCAN_NELEM - 1];
-
-		for (i = 0; i < SYNC_SCAN_NELEM; i++)
-		{
-			ss_lru_item_t *item = &scan_locations->items[i];
-
-			/*
-			 * Initialize all slots with invalid values. As scans are started,
-			 * these invalid entries will fall off the LRU list and get
-			 * replaced with real entries.
-			 */
-			item->location.relfilelocator.spcOid = InvalidOid;
-			item->location.relfilelocator.dbOid = InvalidOid;
-			item->location.relfilelocator.relNumber = InvalidRelFileNumber;
-			item->location.location = InvalidBlockNumber;
-
-			item->prev = (i > 0) ?
-				(&scan_locations->items[i - 1]) : NULL;
-			item->next = (i < SYNC_SCAN_NELEM - 1) ?
-				(&scan_locations->items[i + 1]) : NULL;
-		}
+		ss_lru_item_t *item = &scan_locations->items[i];
+
+		/*
+		 * Initialize all slots with invalid values. As scans are started,
+		 * these invalid entries will fall off the LRU list and get
+		 * replaced with real entries.
+		 */
+		item->location.relfilelocator.spcOid = InvalidOid;
+		item->location.relfilelocator.dbOid = InvalidOid;
+		item->location.relfilelocator.relNumber = InvalidRelFileNumber;
+		item->location.location = InvalidBlockNumber;
+
+		item->prev = (i > 0) ?
+			(&scan_locations->items[i - 1]) : NULL;
+		item->next = (i < SYNC_SCAN_NELEM - 1) ?
+			(&scan_locations->items[i + 1]) : NULL;
 	}
-	else
-		Assert(found);
 }
 
 /*
diff --git a/src/backend/access/nbtree/nbtutils.c b/src/backend/access/nbtree/nbtutils.c
index 732bc750c9e..1ac856ebdd5 100644
--- a/src/backend/access/nbtree/nbtutils.c
+++ b/src/backend/access/nbtree/nbtutils.c
@@ -25,6 +25,7 @@
 #include "lib/qunique.h"
 #include "miscadmin.h"
 #include "storage/lwlock.h"
+#include "storage/subsystems.h"
 #include "utils/datum.h"
 #include "utils/lsyscache.h"
 #include "utils/rel.h"
@@ -417,6 +418,13 @@ typedef struct BTVacInfo
 
 static BTVacInfo *btvacinfo;
 
+static void BTreeShmemRequest(void *arg);
+static void BTreeShmemInit(void *arg);
+
+const ShmemCallbacks BTreeShmemCallbacks = {
+	.request_fn = BTreeShmemRequest,
+	.init_fn = BTreeShmemInit,
+};
 
 /*
  * _bt_vacuum_cycleid --- get the active vacuum cycle ID for an index,
@@ -553,47 +561,39 @@ _bt_end_vacuum_callback(int code, Datum arg)
 }
 
 /*
- * BTreeShmemSize --- report amount of shared memory space needed
+ * BTreeShmemRequest --- register this module's shared memory
  */
-Size
-BTreeShmemSize(void)
+static void
+BTreeShmemRequest(void *arg)
 {
+	static ShmemStructDesc BTreeShmemDesc;
 	Size		size;
 
 	size = offsetof(BTVacInfo, vacuums);
 	size = add_size(size, mul_size(MaxBackends, sizeof(BTOneVacInfo)));
-	return size;
+
+	ShmemRequestStruct(&BTreeShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "BTree Vacuum State",
+		.size = size,
+		.ptr = (void **) &btvacinfo,
+		});
 }
 
 /*
  * BTreeShmemInit --- initialize this module's shared memory
  */
-void
-BTreeShmemInit(void)
+static void
+BTreeShmemInit(void *arg)
 {
-	bool		found;
-
-	btvacinfo = (BTVacInfo *) ShmemInitStruct("BTree Vacuum State",
-											  BTreeShmemSize(),
-											  &found);
-
-	if (!IsUnderPostmaster)
-	{
-		/* Initialize shared memory area */
-		Assert(!found);
-
-		/*
-		 * It doesn't really matter what the cycle counter starts at, but
-		 * having it always start the same doesn't seem good.  Seed with
-		 * low-order bits of time() instead.
-		 */
-		btvacinfo->cycle_ctr = (BTCycleId) time(NULL);
+	/*
+	 * It doesn't really matter what the cycle counter starts at, but
+	 * having it always start the same doesn't seem good.  Seed with
+	 * low-order bits of time() instead.
+	 */
+	btvacinfo->cycle_ctr = (BTCycleId) time(NULL);
 
-		btvacinfo->num_vacuums = 0;
-		btvacinfo->max_vacuums = MaxBackends;
-	}
-	else
-		Assert(found);
+	btvacinfo->num_vacuums = 0;
+	btvacinfo->max_vacuums = MaxBackends;
 }
 
 bytea *
diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c
index d468c9774b3..88a931d5028 100644
--- a/src/backend/access/transam/twophase.c
+++ b/src/backend/access/transam/twophase.c
@@ -102,6 +102,7 @@
 #include "storage/predicate.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
+#include "storage/subsystems.h"
 #include "utils/builtins.h"
 #include "utils/injection_point.h"
 #include "utils/memutils.h"
@@ -187,8 +188,16 @@ typedef struct TwoPhaseStateData
 	GlobalTransaction prepXacts[FLEXIBLE_ARRAY_MEMBER];
 } TwoPhaseStateData;
 
+static void TwoPhaseShmemRequest(void *arg);
+static void TwoPhaseShmemInit(void *arg);
+
 static TwoPhaseStateData *TwoPhaseState;
 
+const ShmemCallbacks TwoPhaseShmemCallbacks = {
+	.request_fn = TwoPhaseShmemRequest,
+	.init_fn = TwoPhaseShmemInit,
+};
+
 /*
  * Global transaction entry currently locked by us, if any.  Note that any
  * access to the entry pointed to by this variable must be protected by
@@ -234,11 +243,12 @@ static void RemoveTwoPhaseFile(FullTransactionId fxid, bool giveWarning);
 static void RecreateTwoPhaseFile(FullTransactionId fxid, void *content, int len);
 
 /*
- * Initialization of shared memory
+ * Register shared memory for two-phase state.
  */
-Size
-TwoPhaseShmemSize(void)
+static void
+TwoPhaseShmemRequest(void *arg)
 {
+	static ShmemStructDesc TwoPhaseShmemDesc;
 	Size		size;
 
 	/* Need the fixed struct, the array of pointers, and the GTD structs */
@@ -248,46 +258,41 @@ TwoPhaseShmemSize(void)
 	size = MAXALIGN(size);
 	size = add_size(size, mul_size(max_prepared_xacts,
 								   sizeof(GlobalTransactionData)));
-
-	return size;
+	ShmemRequestStruct(&TwoPhaseShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Prepared Transaction Table",
+		.size = size,
+		.ptr = (void **) &TwoPhaseState,
+	});
 }
 
-void
-TwoPhaseShmemInit(void)
+/*
+ * Initialize shared memory for two-phase state.
+ */
+static void
+TwoPhaseShmemInit(void *arg)
 {
-	bool		found;
-
-	TwoPhaseState = ShmemInitStruct("Prepared Transaction Table",
-									TwoPhaseShmemSize(),
-									&found);
-	if (!IsUnderPostmaster)
-	{
-		GlobalTransaction gxacts;
-		int			i;
+	GlobalTransaction gxacts;
+	int			i;
 
-		Assert(!found);
-		TwoPhaseState->freeGXacts = NULL;
-		TwoPhaseState->numPrepXacts = 0;
+	TwoPhaseState->freeGXacts = NULL;
+	TwoPhaseState->numPrepXacts = 0;
 
-		/*
-		 * Initialize the linked list of free GlobalTransactionData structs
-		 */
-		gxacts = (GlobalTransaction)
-			((char *) TwoPhaseState +
-			 MAXALIGN(offsetof(TwoPhaseStateData, prepXacts) +
-					  sizeof(GlobalTransaction) * max_prepared_xacts));
-		for (i = 0; i < max_prepared_xacts; i++)
-		{
-			/* insert into linked list */
-			gxacts[i].next = TwoPhaseState->freeGXacts;
-			TwoPhaseState->freeGXacts = &gxacts[i];
+	/*
+	 * Initialize the linked list of free GlobalTransactionData structs
+	 */
+	gxacts = (GlobalTransaction)
+		((char *) TwoPhaseState +
+		 MAXALIGN(offsetof(TwoPhaseStateData, prepXacts) +
+				  sizeof(GlobalTransaction) * max_prepared_xacts));
+	for (i = 0; i < max_prepared_xacts; i++)
+	{
+		/* insert into linked list */
+		gxacts[i].next = TwoPhaseState->freeGXacts;
+		TwoPhaseState->freeGXacts = &gxacts[i];
 
-			/* associate it with a PGPROC assigned by InitProcGlobal */
-			gxacts[i].pgprocno = GetNumberFromPGProc(&PreparedXactProcs[i]);
-		}
+		/* associate it with a PGPROC assigned by InitProcGlobal */
+		gxacts[i].pgprocno = GetNumberFromPGProc(&PreparedXactProcs[i]);
 	}
-	else
-		Assert(found);
 }
 
 /*
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index f5c9a34374d..660b530fe52 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -94,6 +94,7 @@
 #include "storage/procarray.h"
 #include "storage/reinit.h"
 #include "storage/spin.h"
+#include "storage/subsystems.h"
 #include "storage/sync.h"
 #include "utils/guc_hooks.h"
 #include "utils/guc_tables.h"
@@ -566,6 +567,16 @@ typedef enum
 	WALINSERT_SPECIAL_CHECKPOINT
 } WalInsertClass;
 
+static void XLOGShmemRequest(void *arg);
+static void XLOGShmemInit(void *arg);
+static void XLOGShmemAttach(void *arg);
+
+const ShmemCallbacks XLOGShmemCallbacks = {
+	.request_fn = XLOGShmemRequest,
+	.init_fn = XLOGShmemInit,
+	.attach_fn = XLOGShmemAttach,
+};
+
 static XLogCtlData *XLogCtl = NULL;
 
 /* a private copy of XLogCtl->Insert.WALInsertLocks, for convenience */
@@ -574,6 +585,7 @@ static WALInsertLockPadded *WALInsertLocks = NULL;
 /*
  * We maintain an image of pg_control in shared memory.
  */
+static ControlFileData *LocalControlFile = NULL;
 static ControlFileData *ControlFile = NULL;
 
 /*
@@ -4923,7 +4935,8 @@ void
 LocalProcessControlFile(bool reset)
 {
 	Assert(reset || ControlFile == NULL);
-	ControlFile = palloc_object(ControlFileData);
+	LocalControlFile = palloc_object(ControlFileData);
+	ControlFile = LocalControlFile;
 	ReadControlFile();
 }
 
@@ -4939,11 +4952,13 @@ GetActiveWalLevelOnStandby(void)
 }
 
 /*
- * Initialization of shared memory for XLOG
+ * Register shared memory for XLOG.
  */
-Size
-XLOGShmemSize(void)
+static void
+XLOGShmemRequest(void *arg)
 {
+	static ShmemStructDesc XLogCtlShmemDesc;
+	static ShmemStructDesc ControlFileShmemDesc;
 	Size		size;
 
 	/*
@@ -4982,23 +4997,26 @@ XLOGShmemSize(void)
 	/* and the buffers themselves */
 	size = add_size(size, mul_size(XLOG_BLCKSZ, XLOGbuffers));
 
-	/*
-	 * Note: we don't count ControlFileData, it comes out of the "slop factor"
-	 * added by CreateSharedMemoryAndSemaphores.  This lets us use this
-	 * routine again below to compute the actual allocation size.
-	 */
-
-	return size;
+	ShmemRequestStruct(&XLogCtlShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "XLOG Ctl",
+		.size = size,
+		.ptr = (void **) &XLogCtl,
+	});
+	ShmemRequestStruct(&ControlFileShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Control File",
+		.size = sizeof(ControlFileData),
+		.ptr = (void **) &ControlFile,
+	});
 }
 
-void
-XLOGShmemInit(void)
+/*
+ * XLOGShmemInit - initialize the XLogCtl shared memory area.
+ */
+static void
+XLOGShmemInit(void *arg)
 {
-	bool		foundCFile,
-				foundXLog;
 	char	   *allocptr;
 	int			i;
-	ControlFileData *localControlFile;
 
 #ifdef WAL_DEBUG
 
@@ -5016,36 +5034,17 @@ XLOGShmemInit(void)
 	}
 #endif
 
-
-	XLogCtl = (XLogCtlData *)
-		ShmemInitStruct("XLOG Ctl", XLOGShmemSize(), &foundXLog);
-
-	localControlFile = ControlFile;
-	ControlFile = (ControlFileData *)
-		ShmemInitStruct("Control File", sizeof(ControlFileData), &foundCFile);
-
-	if (foundCFile || foundXLog)
-	{
-		/* both should be present or neither */
-		Assert(foundCFile && foundXLog);
-
-		/* Initialize local copy of WALInsertLocks */
-		WALInsertLocks = XLogCtl->Insert.WALInsertLocks;
-
-		if (localControlFile)
-			pfree(localControlFile);
-		return;
-	}
 	memset(XLogCtl, 0, sizeof(XLogCtlData));
 
 	/*
 	 * Already have read control file locally, unless in bootstrap mode. Move
 	 * contents into shared memory.
 	 */
-	if (localControlFile)
+	if (LocalControlFile)
 	{
-		memcpy(ControlFile, localControlFile, sizeof(ControlFileData));
-		pfree(localControlFile);
+		memcpy(ControlFile, LocalControlFile, sizeof(ControlFileData));
+		pfree(LocalControlFile);
+		LocalControlFile = NULL;
 	}
 
 	/*
@@ -5102,6 +5101,15 @@ XLOGShmemInit(void)
 	pg_atomic_init_u64(&XLogCtl->unloggedLSN, InvalidXLogRecPtr);
 }
 
+/*
+ * XLOGShmemAttach - set up WALInsertLocks pointer after attaching.
+ */
+static void
+XLOGShmemAttach(void *arg)
+{
+	WALInsertLocks = XLogCtl->Insert.WALInsertLocks;
+}
+
 /*
  * This func must be called ONCE on system install.  It creates pg_control
  * and the initial XLOG segment.
diff --git a/src/backend/access/transam/xlogprefetcher.c b/src/backend/access/transam/xlogprefetcher.c
index c235eca7c51..006c45b817a 100644
--- a/src/backend/access/transam/xlogprefetcher.c
+++ b/src/backend/access/transam/xlogprefetcher.c
@@ -39,6 +39,7 @@
 #include "storage/fd.h"
 #include "storage/shmem.h"
 #include "storage/smgr.h"
+#include "storage/subsystems.h"
 #include "utils/fmgrprotos.h"
 #include "utils/guc_hooks.h"
 #include "utils/hsearch.h"
@@ -200,6 +201,14 @@ static LsnReadQueueNextStatus XLogPrefetcherNextBlock(uintptr_t pgsr_private,
 
 static XLogPrefetchStats *SharedStats;
 
+static void XLogPrefetchShmemRequest(void *arg);
+static void XLogPrefetchShmemInit(void *arg);
+
+const ShmemCallbacks XLogPrefetchShmemCallbacks = {
+	.request_fn = XLogPrefetchShmemRequest,
+	.init_fn = XLogPrefetchShmemInit,
+};
+
 static inline LsnReadQueue *
 lrq_alloc(uint32 max_distance,
 		  uint32 max_inflight,
@@ -292,10 +301,28 @@ lrq_complete_lsn(LsnReadQueue *lrq, XLogRecPtr lsn)
 		lrq_prefetch(lrq);
 }
 
-size_t
-XLogPrefetchShmemSize(void)
+static void
+XLogPrefetchShmemRequest(void *arg)
+{
+	static ShmemStructDesc XLogPrefetchShmemDesc;
+
+	ShmemRequestStruct(&XLogPrefetchShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "XLogPrefetchStats",
+		.size = sizeof(XLogPrefetchStats),
+		.ptr = (void **) &SharedStats,
+	});
+}
+
+static void
+XLogPrefetchShmemInit(void *arg)
 {
-	return sizeof(XLogPrefetchStats);
+	pg_atomic_init_u64(&SharedStats->reset_time, GetCurrentTimestamp());
+	pg_atomic_init_u64(&SharedStats->prefetch, 0);
+	pg_atomic_init_u64(&SharedStats->hit, 0);
+	pg_atomic_init_u64(&SharedStats->skip_init, 0);
+	pg_atomic_init_u64(&SharedStats->skip_new, 0);
+	pg_atomic_init_u64(&SharedStats->skip_fpw, 0);
+	pg_atomic_init_u64(&SharedStats->skip_rep, 0);
 }
 
 /*
@@ -313,27 +340,6 @@ XLogPrefetchResetStats(void)
 	pg_atomic_write_u64(&SharedStats->skip_rep, 0);
 }
 
-void
-XLogPrefetchShmemInit(void)
-{
-	bool		found;
-
-	SharedStats = (XLogPrefetchStats *)
-		ShmemInitStruct("XLogPrefetchStats",
-						sizeof(XLogPrefetchStats),
-						&found);
-
-	if (!found)
-	{
-		pg_atomic_init_u64(&SharedStats->reset_time, GetCurrentTimestamp());
-		pg_atomic_init_u64(&SharedStats->prefetch, 0);
-		pg_atomic_init_u64(&SharedStats->hit, 0);
-		pg_atomic_init_u64(&SharedStats->skip_init, 0);
-		pg_atomic_init_u64(&SharedStats->skip_new, 0);
-		pg_atomic_init_u64(&SharedStats->skip_fpw, 0);
-		pg_atomic_init_u64(&SharedStats->skip_rep, 0);
-	}
-}
 
 /*
  * Called when any GUC is changed that affects prefetching.
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index fd1c36d061d..d87c4059dac 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -58,6 +58,7 @@
 #include "storage/pmsignal.h"
 #include "storage/procarray.h"
 #include "storage/spin.h"
+#include "storage/subsystems.h"
 #include "utils/datetime.h"
 #include "utils/fmgrprotos.h"
 #include "utils/guc_hooks.h"
@@ -307,6 +308,14 @@ static char *primary_image_masked = NULL;
 
 XLogRecoveryCtlData *XLogRecoveryCtl = NULL;
 
+static void XLogRecoveryShmemRequest(void *arg);
+static void XLogRecoveryShmemInit(void *arg);
+
+const ShmemCallbacks XLogRecoveryShmemCallbacks = {
+	.request_fn = XLogRecoveryShmemRequest,
+	.init_fn = XLogRecoveryShmemInit,
+};
+
 /*
  * abortedRecPtr is the start pointer of a broken record at end of WAL when
  * recovery completes; missingContrecPtr is the location of the first
@@ -385,28 +394,23 @@ static void SetCurrentChunkStartTime(TimestampTz xtime);
 static void SetLatestXTime(TimestampTz xtime);
 
 /*
- * Initialization of shared memory for WAL recovery
+ * Register shared memory for WAL recovery
  */
-Size
-XLogRecoveryShmemSize(void)
+static void
+XLogRecoveryShmemRequest(void *arg)
 {
-	Size		size;
-
-	/* XLogRecoveryCtl */
-	size = sizeof(XLogRecoveryCtlData);
+	static ShmemStructDesc XLogRecoveryShmemDesc;
 
-	return size;
+	ShmemRequestStruct(&XLogRecoveryShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "XLOG Recovery Ctl",
+		.size = sizeof(XLogRecoveryCtlData),
+		.ptr = (void **) &XLogRecoveryCtl,
+	});
 }
 
-void
-XLogRecoveryShmemInit(void)
+static void
+XLogRecoveryShmemInit(void *arg)
 {
-	bool		found;
-
-	XLogRecoveryCtl = (XLogRecoveryCtlData *)
-		ShmemInitStruct("XLOG Recovery Ctl", XLogRecoveryShmemSize(), &found);
-	if (found)
-		return;
 	memset(XLogRecoveryCtl, 0, sizeof(XLogRecoveryCtlData));
 
 	SpinLockInit(&XLogRecoveryCtl->info_lck);
diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.c
index bf4630677b4..830ead44bdd 100644
--- a/src/backend/access/transam/xlogwait.c
+++ b/src/backend/access/transam/xlogwait.c
@@ -57,6 +57,7 @@
 #include "storage/latch.h"
 #include "storage/proc.h"
 #include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "utils/fmgrprotos.h"
 #include "utils/pg_lsn.h"
 #include "utils/snapmgr.h"
@@ -68,6 +69,14 @@ static int	waitlsn_cmp(const pairingheap_node *a, const pairingheap_node *b,
 
 struct WaitLSNState *waitLSNState = NULL;
 
+static void WaitLSNShmemRequest(void *arg);
+static void WaitLSNShmemInit(void *arg);
+
+const ShmemCallbacks WaitLSNShmemCallbacks = {
+	.request_fn = WaitLSNShmemRequest,
+	.init_fn = WaitLSNShmemInit,
+};
+
 /*
  * Wait event for each WaitLSNType, used with WaitLatch() to report
  * the wait in pg_stat_activity.
@@ -109,41 +118,36 @@ GetCurrentLSNForWaitType(WaitLSNType lsnType)
 	pg_unreachable();
 }
 
-/* Report the amount of shared memory space needed for WaitLSNState. */
-Size
-WaitLSNShmemSize(void)
+/* Register the shared memory space needed for WaitLSNState. */
+static void
+WaitLSNShmemRequest(void *arg)
 {
+	static ShmemStructDesc WaitLSNShmemDesc;
 	Size		size;
 
 	size = offsetof(WaitLSNState, procInfos);
 	size = add_size(size, mul_size(MaxBackends + NUM_AUXILIARY_PROCS, sizeof(WaitLSNProcInfo)));
-	return size;
+	ShmemRequestStruct(&WaitLSNShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "WaitLSNState",
+		.size = size,
+		.ptr = (void **) &waitLSNState,
+	});
 }
 
 /* Initialize the WaitLSNState in the shared memory. */
-void
-WaitLSNShmemInit(void)
+static void
+WaitLSNShmemInit(void *arg)
 {
-	bool		found;
-
-	waitLSNState = (WaitLSNState *) ShmemInitStruct("WaitLSNState",
-													WaitLSNShmemSize(),
-													&found);
-	if (!found)
+	/* Initialize heaps and tracking */
+	for (int i = 0; i < WAIT_LSN_TYPE_COUNT; i++)
 	{
-		int			i;
-
-		/* Initialize heaps and tracking */
-		for (i = 0; i < WAIT_LSN_TYPE_COUNT; i++)
-		{
-			pg_atomic_init_u64(&waitLSNState->minWaitedLSN[i], PG_UINT64_MAX);
-			pairingheap_initialize(&waitLSNState->waitersHeap[i], waitlsn_cmp, NULL);
-		}
-
-		/* Initialize process info array */
-		memset(&waitLSNState->procInfos, 0,
-			   (MaxBackends + NUM_AUXILIARY_PROCS) * sizeof(WaitLSNProcInfo));
+		pg_atomic_init_u64(&waitLSNState->minWaitedLSN[i], PG_UINT64_MAX);
+		pairingheap_initialize(&waitLSNState->waitersHeap[i], waitlsn_cmp, NULL);
 	}
+
+	/* Initialize process info array */
+	memset(&waitLSNState->procInfos, 0,
+		   (MaxBackends + NUM_AUXILIARY_PROCS) * sizeof(WaitLSNProcInfo));
 }
 
 /*
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 7ecb069c248..fe02425987a 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -97,6 +97,7 @@
 #include "storage/proc.h"
 #include "storage/procsignal.h"
 #include "storage/smgr.h"
+#include "storage/subsystems.h"
 #include "tcop/tcopprot.h"
 #include "utils/fmgroids.h"
 #include "utils/fmgrprotos.h"
@@ -304,6 +305,14 @@ typedef struct
 
 static AutoVacuumShmemStruct *AutoVacuumShmem;
 
+static void AutoVacuumShmemRequest(void *arg);
+static void AutoVacuumShmemInit(void *arg);
+
+const ShmemCallbacks AutoVacuumShmemCallbacks = {
+	.request_fn = AutoVacuumShmemRequest,
+	.init_fn = AutoVacuumShmemInit,
+};
+
 /*
  * the database list (of avl_dbase elements) in the launcher, and the context
  * that contains it
@@ -3354,12 +3363,13 @@ autovac_init(void)
 }
 
 /*
- * AutoVacuumShmemSize
- *		Compute space needed for autovacuum-related shared memory
+ * AutoVacuumShmemRequest
+ *		Register shared memory space needed for autovacuum
  */
-Size
-AutoVacuumShmemSize(void)
+static void
+AutoVacuumShmemRequest(void *arg)
 {
+	static ShmemStructDesc AutoVacuumShmemDesc;
 	Size		size;
 
 	/*
@@ -3369,53 +3379,42 @@ AutoVacuumShmemSize(void)
 	size = MAXALIGN(size);
 	size = add_size(size, mul_size(autovacuum_worker_slots,
 								   sizeof(WorkerInfoData)));
-	return size;
+
+	ShmemRequestStruct(&AutoVacuumShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "AutoVacuum Data",
+		.size = size,
+		.ptr = (void **) &AutoVacuumShmem,
+	});
 }
 
 /*
  * AutoVacuumShmemInit
- *		Allocate and initialize autovacuum-related shared memory
+ *		Initialize autovacuum-related shared memory
  */
-void
-AutoVacuumShmemInit(void)
+static void
+AutoVacuumShmemInit(void *arg)
 {
-	bool		found;
-
-	AutoVacuumShmem = (AutoVacuumShmemStruct *)
-		ShmemInitStruct("AutoVacuum Data",
-						AutoVacuumShmemSize(),
-						&found);
-
-	if (!IsUnderPostmaster)
-	{
-		WorkerInfo	worker;
-		int			i;
+	WorkerInfo	worker;
 
-		Assert(!found);
-
-		AutoVacuumShmem->av_launcherpid = 0;
-		dclist_init(&AutoVacuumShmem->av_freeWorkers);
-		dlist_init(&AutoVacuumShmem->av_runningWorkers);
-		AutoVacuumShmem->av_startingWorker = NULL;
-		memset(AutoVacuumShmem->av_workItems, 0,
-			   sizeof(AutoVacuumWorkItem) * NUM_WORKITEMS);
-
-		worker = (WorkerInfo) ((char *) AutoVacuumShmem +
-							   MAXALIGN(sizeof(AutoVacuumShmemStruct)));
-
-		/* initialize the WorkerInfo free list */
-		for (i = 0; i < autovacuum_worker_slots; i++)
-		{
-			dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
-							 &worker[i].wi_links);
-			pg_atomic_init_flag(&worker[i].wi_dobalance);
-		}
+	AutoVacuumShmem->av_launcherpid = 0;
+	dclist_init(&AutoVacuumShmem->av_freeWorkers);
+	dlist_init(&AutoVacuumShmem->av_runningWorkers);
+	AutoVacuumShmem->av_startingWorker = NULL;
+	memset(AutoVacuumShmem->av_workItems, 0,
+		   sizeof(AutoVacuumWorkItem) * NUM_WORKITEMS);
 
-		pg_atomic_init_u32(&AutoVacuumShmem->av_nworkersForBalance, 0);
+	worker = (WorkerInfo) ((char *) AutoVacuumShmem +
+						   MAXALIGN(sizeof(AutoVacuumShmemStruct)));
 
+	/* initialize the WorkerInfo free list */
+	for (int i = 0; i < autovacuum_worker_slots; i++)
+	{
+		dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
+						 &worker[i].wi_links);
+		pg_atomic_init_flag(&worker[i].wi_dobalance);
 	}
-	else
-		Assert(found);
+
+	pg_atomic_init_u32(&AutoVacuumShmem->av_nworkersForBalance, 0);
 }
 
 /*
diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c
index d1fe3cc71ce..93d05680aeb 100644
--- a/src/backend/postmaster/bgworker.c
+++ b/src/backend/postmaster/bgworker.c
@@ -29,6 +29,7 @@
 #include "storage/procarray.h"
 #include "storage/procsignal.h"
 #include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "tcop/tcopprot.h"
 #include "utils/ascii.h"
 #include "utils/memutils.h"
@@ -109,6 +110,14 @@ struct BackgroundWorkerHandle
 
 static BackgroundWorkerArray *BackgroundWorkerData;
 
+static void BackgroundWorkerShmemRequest(void *arg);
+static void BackgroundWorkerShmemInit(void *arg);
+
+const ShmemCallbacks BackgroundWorkerShmemCallbacks = {
+	.request_fn = BackgroundWorkerShmemRequest,
+	.init_fn = BackgroundWorkerShmemInit,
+};
+
 /*
  * List of internal background worker entry points.  We need this for
  * reasons explained in LookupBackgroundWorkerFunction(), below.
@@ -151,77 +160,71 @@ static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname
 
 
 /*
- * Calculate shared memory needed.
+ * Register shared memory needed for background workers.
  */
-Size
-BackgroundWorkerShmemSize(void)
+static void
+BackgroundWorkerShmemRequest(void *arg)
 {
+	static ShmemStructDesc BackgroundWorkerShmemDesc;
 	Size		size;
 
 	/* Array of workers is variably sized. */
 	size = offsetof(BackgroundWorkerArray, slot);
 	size = add_size(size, mul_size(max_worker_processes,
 								   sizeof(BackgroundWorkerSlot)));
-
-	return size;
+	ShmemRequestStruct(&BackgroundWorkerShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Background Worker Data",
+		.size = size,
+		.ptr = (void **) &BackgroundWorkerData,
+	});
 }
 
 /*
- * Initialize shared memory.
+ * Initialize shared memory for background workers.
  */
-void
-BackgroundWorkerShmemInit(void)
+static void
+BackgroundWorkerShmemInit(void *arg)
 {
-	bool		found;
-
-	BackgroundWorkerData = ShmemInitStruct("Background Worker Data",
-										   BackgroundWorkerShmemSize(),
-										   &found);
-	if (!IsUnderPostmaster)
-	{
-		dlist_iter	iter;
-		int			slotno = 0;
+	dlist_iter	iter;
+	int			slotno = 0;
 
-		BackgroundWorkerData->total_slots = max_worker_processes;
-		BackgroundWorkerData->parallel_register_count = 0;
-		BackgroundWorkerData->parallel_terminate_count = 0;
+	BackgroundWorkerData->total_slots = max_worker_processes;
+	BackgroundWorkerData->parallel_register_count = 0;
+	BackgroundWorkerData->parallel_terminate_count = 0;
 
-		/*
-		 * Copy contents of worker list into shared memory.  Record the shared
-		 * memory slot assigned to each worker.  This ensures a 1-to-1
-		 * correspondence between the postmaster's private list and the array
-		 * in shared memory.
-		 */
-		dlist_foreach(iter, &BackgroundWorkerList)
-		{
-			BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
-			RegisteredBgWorker *rw;
+	/*
+	 * Copy contents of worker list into shared memory.  Record the shared
+	 * memory slot assigned to each worker.  This ensures a 1-to-1
+	 * correspondence between the postmaster's private list and the array
+	 * in shared memory.
+	 */
+	dlist_foreach(iter, &BackgroundWorkerList)
+	{
+		BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
+		RegisteredBgWorker *rw;
 
-			rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
-			Assert(slotno < max_worker_processes);
-			slot->in_use = true;
-			slot->terminate = false;
-			slot->pid = InvalidPid;
-			slot->generation = 0;
-			rw->rw_shmem_slot = slotno;
-			rw->rw_worker.bgw_notify_pid = 0;	/* might be reinit after crash */
-			memcpy(&slot->worker, &rw->rw_worker, sizeof(BackgroundWorker));
-			++slotno;
-		}
+		rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
+		Assert(slotno < max_worker_processes);
+		slot->in_use = true;
+		slot->terminate = false;
+		slot->pid = InvalidPid;
+		slot->generation = 0;
+		rw->rw_shmem_slot = slotno;
+		rw->rw_worker.bgw_notify_pid = 0;	/* might be reinit after crash */
+		memcpy(&slot->worker, &rw->rw_worker, sizeof(BackgroundWorker));
+		++slotno;
+	}
 
-		/*
-		 * Mark any remaining slots as not in use.
-		 */
-		while (slotno < max_worker_processes)
-		{
-			BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
+	/*
+	 * Mark any remaining slots as not in use.
+	 */
+	while (slotno < max_worker_processes)
+	{
+		BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
 
-			slot->in_use = false;
-			++slotno;
-		}
+		slot->in_use = false;
+		++slotno;
 	}
-	else
-		Assert(found);
 }
 
 /*
diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c
index 3c982c6ffac..c04faf9a1fd 100644
--- a/src/backend/postmaster/checkpointer.c
+++ b/src/backend/postmaster/checkpointer.c
@@ -63,6 +63,7 @@
 #include "storage/shmem.h"
 #include "storage/smgr.h"
 #include "storage/spin.h"
+#include "storage/subsystems.h"
 #include "utils/acl.h"
 #include "utils/guc.h"
 #include "utils/memutils.h"
@@ -143,6 +144,14 @@ typedef struct
 
 static CheckpointerShmemStruct *CheckpointerShmem;
 
+static void CheckpointerShmemRequest(void *arg);
+static void CheckpointerShmemInit(void *arg);
+
+const ShmemCallbacks CheckpointerShmemCallbacks = {
+	.request_fn = CheckpointerShmemRequest,
+	.init_fn = CheckpointerShmemInit,
+};
+
 /* interval for calling AbsorbSyncRequests in CheckpointWriteDelay */
 #define WRITES_PER_ABSORB		1000
 
@@ -950,12 +959,13 @@ ReqShutdownXLOG(SIGNAL_ARGS)
  */
 
 /*
- * CheckpointerShmemSize
- *		Compute space needed for checkpointer-related shared memory
+ * CheckpointerShmemRequest
+ *		Register shared memory space needed for checkpointer
  */
-Size
-CheckpointerShmemSize(void)
+static void
+CheckpointerShmemRequest(void *arg)
 {
+	static ShmemStructDesc CheckpointerShmemDesc;
 	Size		size;
 
 	/*
@@ -967,39 +977,25 @@ CheckpointerShmemSize(void)
 	size = add_size(size, mul_size(Min(NBuffers,
 									   MAX_CHECKPOINT_REQUESTS),
 								   sizeof(CheckpointerRequest)));
-
-	return size;
+	ShmemRequestStruct(&CheckpointerShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Checkpointer Data",
+		.size = size,
+		.ptr = (void **) &CheckpointerShmem,
+	});
 }
 
 /*
  * CheckpointerShmemInit
- *		Allocate and initialize checkpointer-related shared memory
+ *		Initialize checkpointer-related shared memory
  */
-void
-CheckpointerShmemInit(void)
+static void
+CheckpointerShmemInit(void *arg)
 {
-	Size		size = CheckpointerShmemSize();
-	bool		found;
-
-	CheckpointerShmem = (CheckpointerShmemStruct *)
-		ShmemInitStruct("Checkpointer Data",
-						size,
-						&found);
-
-	if (!found)
-	{
-		/*
-		 * First time through, so initialize.  Note that we zero the whole
-		 * requests array; this is so that CompactCheckpointerRequestQueue can
-		 * assume that any pad bytes in the request structs are zeroes.
-		 */
-		MemSet(CheckpointerShmem, 0, size);
-		SpinLockInit(&CheckpointerShmem->ckpt_lck);
-		CheckpointerShmem->max_requests = Min(NBuffers, MAX_CHECKPOINT_REQUESTS);
-		CheckpointerShmem->head = CheckpointerShmem->tail = 0;
-		ConditionVariableInit(&CheckpointerShmem->start_cv);
-		ConditionVariableInit(&CheckpointerShmem->done_cv);
-	}
+	SpinLockInit(&CheckpointerShmem->ckpt_lck);
+	CheckpointerShmem->max_requests = Min(NBuffers, MAX_CHECKPOINT_REQUESTS);
+	CheckpointerShmem->head = CheckpointerShmem->tail = 0;
+	ConditionVariableInit(&CheckpointerShmem->start_cv);
+	ConditionVariableInit(&CheckpointerShmem->done_cv);
 }
 
 /*
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index fa4bdfe9ab9..cc983675373 100644
--- a/src/backend/postmaster/pgarch.c
+++ b/src/backend/postmaster/pgarch.c
@@ -48,6 +48,7 @@
 #include "storage/proc.h"
 #include "storage/procsignal.h"
 #include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "utils/guc.h"
 #include "utils/memutils.h"
 #include "utils/ps_status.h"
@@ -154,33 +155,34 @@ static int	ready_file_comparator(Datum a, Datum b, void *arg);
 static void LoadArchiveLibrary(void);
 static void pgarch_call_module_shutdown_cb(int code, Datum arg);
 
-/* Report shared memory space needed by PgArchShmemInit */
-Size
-PgArchShmemSize(void)
-{
-	Size		size = 0;
-
-	size = add_size(size, sizeof(PgArchData));
+static void PgArchShmemRequest(void *arg);
+static void PgArchShmemInit(void *arg);
 
-	return size;
-}
+const ShmemCallbacks PgArchShmemCallbacks = {
+	.request_fn = PgArchShmemRequest,
+	.init_fn = PgArchShmemInit,
+};
 
-/* Allocate and initialize archiver-related shared memory */
-void
-PgArchShmemInit(void)
+/* Register shared memory space needed by the archiver */
+static void
+PgArchShmemRequest(void *arg)
 {
-	bool		found;
+	static ShmemStructDesc PgArchShmemDesc;
 
-	PgArch = (PgArchData *)
-		ShmemInitStruct("Archiver Data", PgArchShmemSize(), &found);
+	ShmemRequestStruct(&PgArchShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Archiver Data",
+		.size = sizeof(PgArchData),
+		.ptr = (void **) &PgArch,
+	});
+}
 
-	if (!found)
-	{
-		/* First time through, so initialize */
-		MemSet(PgArch, 0, PgArchShmemSize());
-		PgArch->pgprocno = INVALID_PROC_NUMBER;
-		pg_atomic_init_u32(&PgArch->force_dir_scan, 0);
-	}
+/* Initialize archiver-related shared memory */
+static void
+PgArchShmemInit(void *arg)
+{
+	MemSet(PgArch, 0, sizeof(PgArchData));
+	PgArch->pgprocno = INVALID_PROC_NUMBER;
+	pg_atomic_init_u32(&PgArch->force_dir_scan, 0);
 }
 
 /*
diff --git a/src/backend/postmaster/walsummarizer.c b/src/backend/postmaster/walsummarizer.c
index 0c0670f7da9..99ef65f09ba 100644
--- a/src/backend/postmaster/walsummarizer.c
+++ b/src/backend/postmaster/walsummarizer.c
@@ -47,6 +47,7 @@
 #include "storage/proc.h"
 #include "storage/procsignal.h"
 #include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "utils/guc.h"
 #include "utils/memutils.h"
 #include "utils/wait_event.h"
@@ -109,6 +110,14 @@ typedef struct
 /* Pointer to shared memory state. */
 static WalSummarizerData *WalSummarizerCtl;
 
+static void WalSummarizerShmemRequest(void *arg);
+static void WalSummarizerShmemInit(void *arg);
+
+const ShmemCallbacks WalSummarizerShmemCallbacks = {
+	.request_fn = WalSummarizerShmemRequest,
+	.init_fn = WalSummarizerShmemInit,
+};
+
 /*
  * When we reach end of WAL and need to read more, we sleep for a number of
  * milliseconds that is an integer multiple of MS_PER_SLEEP_QUANTUM. This is
@@ -168,43 +177,38 @@ static void summarizer_wait_for_wal(void);
 static void MaybeRemoveOldWalSummaries(void);
 
 /*
- * Amount of shared memory required for this module.
+ * Register shared memory space needed by this module.
  */
-Size
-WalSummarizerShmemSize(void)
+static void
+WalSummarizerShmemRequest(void *arg)
 {
-	return sizeof(WalSummarizerData);
+	static ShmemStructDesc WalSummarizerShmemDesc;
+
+	ShmemRequestStruct(&WalSummarizerShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Wal Summarizer Ctl",
+		.size = sizeof(WalSummarizerData),
+		.ptr = (void **) &WalSummarizerCtl,
+	});
 }
 
 /*
- * Create or attach to shared memory segment for this module.
+ * Initialize shared memory for this module.
  */
-void
-WalSummarizerShmemInit(void)
+static void
+WalSummarizerShmemInit(void *arg)
 {
-	bool		found;
-
-	WalSummarizerCtl = (WalSummarizerData *)
-		ShmemInitStruct("Wal Summarizer Ctl", WalSummarizerShmemSize(),
-						&found);
-
-	if (!found)
-	{
-		/*
-		 * First time through, so initialize.
-		 *
-		 * We're just filling in dummy values here -- the real initialization
-		 * will happen when GetOldestUnsummarizedLSN() is called for the first
-		 * time.
-		 */
-		WalSummarizerCtl->initialized = false;
-		WalSummarizerCtl->summarized_tli = 0;
-		WalSummarizerCtl->summarized_lsn = InvalidXLogRecPtr;
-		WalSummarizerCtl->lsn_is_exact = false;
-		WalSummarizerCtl->summarizer_pgprocno = INVALID_PROC_NUMBER;
-		WalSummarizerCtl->pending_lsn = InvalidXLogRecPtr;
-		ConditionVariableInit(&WalSummarizerCtl->summary_file_cv);
-	}
+	/*
+	 * We're just filling in dummy values here -- the real initialization
+	 * will happen when GetOldestUnsummarizedLSN() is called for the first
+	 * time.
+	 */
+	WalSummarizerCtl->initialized = false;
+	WalSummarizerCtl->summarized_tli = 0;
+	WalSummarizerCtl->summarized_lsn = InvalidXLogRecPtr;
+	WalSummarizerCtl->lsn_is_exact = false;
+	WalSummarizerCtl->summarizer_pgprocno = INVALID_PROC_NUMBER;
+	WalSummarizerCtl->pending_lsn = InvalidXLogRecPtr;
+	ConditionVariableInit(&WalSummarizerCtl->summary_file_cv);
 }
 
 /*
diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c
index 09964198550..61a162773d8 100644
--- a/src/backend/replication/logical/launcher.c
+++ b/src/backend/replication/logical/launcher.c
@@ -38,6 +38,7 @@
 #include "storage/ipc.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
+#include "storage/subsystems.h"
 #include "tcop/tcopprot.h"
 #include "utils/builtins.h"
 #include "utils/memutils.h"
@@ -71,6 +72,14 @@ typedef struct LogicalRepCtxStruct
 
 static LogicalRepCtxStruct *LogicalRepCtx;
 
+static void ApplyLauncherShmemRequest(void *arg);
+static void ApplyLauncherShmemInit(void *arg);
+
+const ShmemCallbacks ApplyLauncherShmemCallbacks = {
+	.request_fn = ApplyLauncherShmemRequest,
+	.init_fn = ApplyLauncherShmemInit,
+};
+
 /* an entry in the last-start-times shared hash table */
 typedef struct LauncherLastStartTimesEntry
 {
@@ -972,12 +981,13 @@ logicalrep_pa_worker_count(Oid subid)
 }
 
 /*
- * ApplyLauncherShmemSize
- *		Compute space needed for replication launcher shared memory
+ * ApplyLauncherShmemRequest
+ *		Register shared memory space needed for replication launcher
  */
-Size
-ApplyLauncherShmemSize(void)
+static void
+ApplyLauncherShmemRequest(void *arg)
 {
+	static ShmemStructDesc ApplyLauncherShmemDesc;
 	Size		size;
 
 	/*
@@ -987,7 +997,11 @@ ApplyLauncherShmemSize(void)
 	size = MAXALIGN(size);
 	size = add_size(size, mul_size(max_logical_replication_workers,
 								   sizeof(LogicalRepWorker)));
-	return size;
+	ShmemRequestStruct(&ApplyLauncherShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Logical Replication Launcher Data",
+		.size = size,
+		.ptr = (void **) &LogicalRepCtx,
+	});
 }
 
 /*
@@ -1028,35 +1042,23 @@ ApplyLauncherRegister(void)
 
 /*
  * ApplyLauncherShmemInit
- *		Allocate and initialize replication launcher shared memory
+ *		Initialize replication launcher shared memory
  */
-void
-ApplyLauncherShmemInit(void)
+static void
+ApplyLauncherShmemInit(void *arg)
 {
-	bool		found;
+	int			slot;
 
-	LogicalRepCtx = (LogicalRepCtxStruct *)
-		ShmemInitStruct("Logical Replication Launcher Data",
-						ApplyLauncherShmemSize(),
-						&found);
+	LogicalRepCtx->last_start_dsa = DSA_HANDLE_INVALID;
+	LogicalRepCtx->last_start_dsh = DSHASH_HANDLE_INVALID;
 
-	if (!found)
+	/* Initialize memory and spin locks for each worker slot. */
+	for (slot = 0; slot < max_logical_replication_workers; slot++)
 	{
-		int			slot;
-
-		memset(LogicalRepCtx, 0, ApplyLauncherShmemSize());
-
-		LogicalRepCtx->last_start_dsa = DSA_HANDLE_INVALID;
-		LogicalRepCtx->last_start_dsh = DSHASH_HANDLE_INVALID;
+		LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
 
-		/* Initialize memory and spin locks for each worker slot. */
-		for (slot = 0; slot < max_logical_replication_workers; slot++)
-		{
-			LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
-
-			memset(worker, 0, sizeof(LogicalRepWorker));
-			SpinLockInit(&worker->relmutex);
-		}
+		memset(worker, 0, sizeof(LogicalRepWorker));
+		SpinLockInit(&worker->relmutex);
 	}
 }
 
diff --git a/src/backend/replication/logical/logicalctl.c b/src/backend/replication/logical/logicalctl.c
index 4e292951201..86b48edd8e6 100644
--- a/src/backend/replication/logical/logicalctl.c
+++ b/src/backend/replication/logical/logicalctl.c
@@ -72,6 +72,7 @@
 #include "storage/proc.h"
 #include "storage/procarray.h"
 #include "storage/procsignal.h"
+#include "storage/subsystems.h"
 #include "utils/injection_point.h"
 
 /*
@@ -98,6 +99,12 @@ typedef struct LogicalDecodingCtlData
 
 static LogicalDecodingCtlData *LogicalDecodingCtl = NULL;
 
+static void LogicalDecodingCtlShmemRequest(void *arg);
+
+const ShmemCallbacks LogicalDecodingCtlShmemCallbacks = {
+	.request_fn = LogicalDecodingCtlShmemRequest,
+};
+
 /*
  * A process-local cache of LogicalDecodingCtl->xlog_logical_info. This is
  * initialized at process startup, and updated when processing the process
@@ -120,23 +127,16 @@ static void update_xlog_logical_info(void);
 static void abort_logical_decoding_activation(int code, Datum arg);
 static void write_logical_decoding_status_update_record(bool status);
 
-Size
-LogicalDecodingCtlShmemSize(void)
-{
-	return sizeof(LogicalDecodingCtlData);
-}
-
-void
-LogicalDecodingCtlShmemInit(void)
+static void
+LogicalDecodingCtlShmemRequest(void *arg)
 {
-	bool		found;
-
-	LogicalDecodingCtl = ShmemInitStruct("Logical decoding control",
-										 LogicalDecodingCtlShmemSize(),
-										 &found);
+	static ShmemStructDesc LogicalDecodingCtlShmemDesc;
 
-	if (!found)
-		MemSet(LogicalDecodingCtl, 0, LogicalDecodingCtlShmemSize());
+	ShmemRequestStruct(&LogicalDecodingCtlShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Logical decoding control",
+		.size = sizeof(LogicalDecodingCtlData),
+		.ptr = (void **) &LogicalDecodingCtl,
+	});
 }
 
 /*
diff --git a/src/backend/replication/logical/origin.c b/src/backend/replication/logical/origin.c
index 661d68ad653..1a7316b2338 100644
--- a/src/backend/replication/logical/origin.c
+++ b/src/backend/replication/logical/origin.c
@@ -88,6 +88,7 @@
 #include "storage/fd.h"
 #include "storage/ipc.h"
 #include "storage/lmgr.h"
+#include "storage/subsystems.h"
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
 #include "utils/guc.h"
@@ -176,6 +177,16 @@ ReplOriginXactState replorigin_xact_state = {
  */
 static ReplicationState *replication_states;
 
+static void ReplicationOriginShmemRequest(void *arg);
+static void ReplicationOriginShmemInit(void *arg);
+static void ReplicationOriginShmemAttach(void *arg);
+
+const ShmemCallbacks ReplicationOriginShmemCallbacks = {
+	.request_fn = ReplicationOriginShmemRequest,
+	.init_fn = ReplicationOriginShmemInit,
+	.attach_fn = ReplicationOriginShmemAttach,
+};
+
 /*
  * Actual shared memory block (replication_states[] is now part of this).
  */
@@ -539,50 +550,50 @@ replorigin_by_oid(ReplOriginId roident, bool missing_ok, char **roname)
  * ---------------------------------------------------------------------------
  */
 
-Size
-ReplicationOriginShmemSize(void)
+static void
+ReplicationOriginShmemRequest(void *arg)
 {
+	static ShmemStructDesc ReplicationOriginShmemDesc;
 	Size		size = 0;
 
 	if (max_active_replication_origins == 0)
-		return size;
+		return;
 
 	size = add_size(size, offsetof(ReplicationStateCtl, states));
-
 	size = add_size(size,
 					mul_size(max_active_replication_origins, sizeof(ReplicationState)));
-	return size;
+	ShmemRequestStruct(&ReplicationOriginShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "ReplicationOriginState",
+		.size = size,
+		.ptr = (void **) &replication_states_ctl,
+	});
 }
 
-void
-ReplicationOriginShmemInit(void)
+static void
+ReplicationOriginShmemInit(void *arg)
 {
-	bool		found;
-
 	if (max_active_replication_origins == 0)
 		return;
 
-	replication_states_ctl = (ReplicationStateCtl *)
-		ShmemInitStruct("ReplicationOriginState",
-						ReplicationOriginShmemSize(),
-						&found);
 	replication_states = replication_states_ctl->states;
 
-	if (!found)
-	{
-		int			i;
+	replication_states_ctl->tranche_id = LWTRANCHE_REPLICATION_ORIGIN_STATE;
 
-		MemSet(replication_states_ctl, 0, ReplicationOriginShmemSize());
+	for (int i = 0; i < max_active_replication_origins; i++)
+	{
+		LWLockInitialize(&replication_states[i].lock,
+						 replication_states_ctl->tranche_id);
+		ConditionVariableInit(&replication_states[i].origin_cv);
+	}
+}
 
-		replication_states_ctl->tranche_id = LWTRANCHE_REPLICATION_ORIGIN_STATE;
+static void
+ReplicationOriginShmemAttach(void *arg)
+{
+	if (max_active_replication_origins == 0)
+		return;
 
-		for (i = 0; i < max_active_replication_origins; i++)
-		{
-			LWLockInitialize(&replication_states[i].lock,
-							 replication_states_ctl->tranche_id);
-			ConditionVariableInit(&replication_states[i].origin_cv);
-		}
-	}
+	replication_states = replication_states_ctl->states;
 }
 
 /* ---------------------------------------------------------------------------
diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c
index e75db69e3f6..22c5f8e48a2 100644
--- a/src/backend/replication/logical/slotsync.c
+++ b/src/backend/replication/logical/slotsync.c
@@ -73,6 +73,7 @@
 #include "storage/lmgr.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
+#include "storage/subsystems.h"
 #include "tcop/tcopprot.h"
 #include "utils/builtins.h"
 #include "utils/memutils.h"
@@ -118,6 +119,14 @@ typedef struct SlotSyncCtxStruct
 
 static SlotSyncCtxStruct *SlotSyncCtx = NULL;
 
+static void SlotSyncShmemRequest(void *arg);
+static void SlotSyncShmemInit(void *arg);
+
+const ShmemCallbacks SlotSyncShmemCallbacks = {
+	.request_fn = SlotSyncShmemRequest,
+	.init_fn = SlotSyncShmemInit,
+};
+
 /* GUC variable */
 bool		sync_replication_slots = false;
 
@@ -1828,32 +1837,29 @@ IsSyncingReplicationSlots(void)
 }
 
 /*
- * Amount of shared memory required for slot synchronization.
+ * Register shared memory space needed for slot synchronization.
  */
-Size
-SlotSyncShmemSize(void)
+static void
+SlotSyncShmemRequest(void *arg)
 {
-	return sizeof(SlotSyncCtxStruct);
+	static ShmemStructDesc SlotSyncShmemDesc;
+
+	ShmemRequestStruct(&SlotSyncShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Slot Sync Data",
+		.size = sizeof(SlotSyncCtxStruct),
+		.ptr = (void **) &SlotSyncCtx,
+	});
 }
 
 /*
- * Allocate and initialize the shared memory of slot synchronization.
+ * Initialize shared memory for slot synchronization.
  */
-void
-SlotSyncShmemInit(void)
+static void
+SlotSyncShmemInit(void *arg)
 {
-	Size		size = SlotSyncShmemSize();
-	bool		found;
-
-	SlotSyncCtx = (SlotSyncCtxStruct *)
-		ShmemInitStruct("Slot Sync Data", size, &found);
-
-	if (!found)
-	{
-		memset(SlotSyncCtx, 0, size);
-		SlotSyncCtx->pid = InvalidPid;
-		SpinLockInit(&SlotSyncCtx->mutex);
-	}
+	memset(SlotSyncCtx, 0, sizeof(SlotSyncCtxStruct));
+	SlotSyncCtx->pid = InvalidPid;
+	SpinLockInit(&SlotSyncCtx->mutex);
 }
 
 /*
diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c
index a9092fc2382..b68b27c356d 100644
--- a/src/backend/replication/slot.c
+++ b/src/backend/replication/slot.c
@@ -55,6 +55,7 @@
 #include "storage/ipc.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
+#include "storage/subsystems.h"
 #include "utils/builtins.h"
 #include "utils/guc_hooks.h"
 #include "utils/injection_point.h"
@@ -145,6 +146,14 @@ StaticAssertDecl(lengthof(SlotInvalidationCauses) == (RS_INVAL_MAX_CAUSES + 1),
 /* Control array for replication slot management */
 ReplicationSlotCtlData *ReplicationSlotCtl = NULL;
 
+static void ReplicationSlotsShmemRequest(void *arg);
+static void ReplicationSlotsShmemInit(void *arg);
+
+const ShmemCallbacks ReplicationSlotsShmemCallbacks = {
+	.request_fn = ReplicationSlotsShmemRequest,
+	.init_fn = ReplicationSlotsShmemInit,
+};
+
 /* My backend's replication slot in the shared memory array */
 ReplicationSlot *MyReplicationSlot = NULL;
 
@@ -183,56 +192,43 @@ static void CreateSlotOnDisk(ReplicationSlot *slot);
 static void SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel);
 
 /*
- * Report shared-memory space needed by ReplicationSlotsShmemInit.
+ * Register shared memory space needed for replication slots.
  */
-Size
-ReplicationSlotsShmemSize(void)
+static void
+ReplicationSlotsShmemRequest(void *arg)
 {
-	Size		size = 0;
+	static ShmemStructDesc ReplicationSlotsShmemDesc;
+	Size		size;
 
 	if (max_replication_slots == 0)
-		return size;
+		return;
 
 	size = offsetof(ReplicationSlotCtlData, replication_slots);
 	size = add_size(size,
 					mul_size(max_replication_slots, sizeof(ReplicationSlot)));
-
-	return size;
+	ShmemRequestStruct(&ReplicationSlotsShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "ReplicationSlot Ctl",
+		.size = size,
+		.ptr = (void **) &ReplicationSlotCtl,
+	});
 }
 
 /*
- * Allocate and initialize shared memory for replication slots.
+ * Initialize shared memory for replication slots.
  */
-void
-ReplicationSlotsShmemInit(void)
+static void
+ReplicationSlotsShmemInit(void *arg)
 {
-	bool		found;
-
-	if (max_replication_slots == 0)
-		return;
-
-	ReplicationSlotCtl = (ReplicationSlotCtlData *)
-		ShmemInitStruct("ReplicationSlot Ctl", ReplicationSlotsShmemSize(),
-						&found);
-
-	if (!found)
+	for (int i = 0; i < max_replication_slots; i++)
 	{
-		int			i;
+		ReplicationSlot *slot = &ReplicationSlotCtl->replication_slots[i];
 
-		/* First time through, so initialize */
-		MemSet(ReplicationSlotCtl, 0, ReplicationSlotsShmemSize());
-
-		for (i = 0; i < max_replication_slots; i++)
-		{
-			ReplicationSlot *slot = &ReplicationSlotCtl->replication_slots[i];
-
-			/* everything else is zeroed by the memset above */
-			slot->active_proc = INVALID_PROC_NUMBER;
-			SpinLockInit(&slot->mutex);
-			LWLockInitialize(&slot->io_in_progress_lock,
-							 LWTRANCHE_REPLICATION_SLOT_IO);
-			ConditionVariableInit(&slot->active_cv);
-		}
+		/* everything else is zeroed by the memset above */
+		slot->active_proc = INVALID_PROC_NUMBER;
+		SpinLockInit(&slot->mutex);
+		LWLockInitialize(&slot->io_in_progress_lock,
+						 LWTRANCHE_REPLICATION_SLOT_IO);
+		ConditionVariableInit(&slot->active_cv);
 	}
 }
 
diff --git a/src/backend/replication/walreceiverfuncs.c b/src/backend/replication/walreceiverfuncs.c
index 45b9d4f09f2..49c203f915e 100644
--- a/src/backend/replication/walreceiverfuncs.c
+++ b/src/backend/replication/walreceiverfuncs.c
@@ -29,47 +29,49 @@
 #include "storage/pmsignal.h"
 #include "storage/proc.h"
 #include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "utils/timestamp.h"
 #include "utils/wait_event.h"
 
 WalRcvData *WalRcv = NULL;
 
+static void WalRcvShmemRequest(void *arg);
+static void WalRcvShmemInit(void *arg);
+
+const ShmemCallbacks WalRcvShmemCallbacks = {
+	.request_fn = WalRcvShmemRequest,
+	.init_fn = WalRcvShmemInit,
+};
+
 /*
  * How long to wait for walreceiver to start up after requesting
  * postmaster to launch it. In seconds.
  */
 #define WALRCV_STARTUP_TIMEOUT 10
 
-/* Report shared memory space needed by WalRcvShmemInit */
-Size
-WalRcvShmemSize(void)
+/* Register shared memory space needed by walreceiver */
+static void
+WalRcvShmemRequest(void *arg)
 {
-	Size		size = 0;
+	static ShmemStructDesc WalRcvShmemDesc;
 
-	size = add_size(size, sizeof(WalRcvData));
-
-	return size;
+	ShmemRequestStruct(&WalRcvShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Wal Receiver Ctl",
+		.size = sizeof(WalRcvData),
+		.ptr = (void **) &WalRcv,
+	});
 }
 
-/* Allocate and initialize walreceiver-related shared memory */
-void
-WalRcvShmemInit(void)
+/* Initialize walreceiver-related shared memory */
+static void
+WalRcvShmemInit(void *arg)
 {
-	bool		found;
-
-	WalRcv = (WalRcvData *)
-		ShmemInitStruct("Wal Receiver Ctl", WalRcvShmemSize(), &found);
-
-	if (!found)
-	{
-		/* First time through, so initialize */
-		MemSet(WalRcv, 0, WalRcvShmemSize());
-		WalRcv->walRcvState = WALRCV_STOPPED;
-		ConditionVariableInit(&WalRcv->walRcvStoppedCV);
-		SpinLockInit(&WalRcv->mutex);
-		pg_atomic_init_u64(&WalRcv->writtenUpto, 0);
-		WalRcv->procno = INVALID_PROC_NUMBER;
-	}
+	MemSet(WalRcv, 0, sizeof(WalRcvData));
+	WalRcv->walRcvState = WALRCV_STOPPED;
+	ConditionVariableInit(&WalRcv->walRcvStoppedCV);
+	SpinLockInit(&WalRcv->mutex);
+	pg_atomic_init_u64(&WalRcv->writtenUpto, 0);
+	WalRcv->procno = INVALID_PROC_NUMBER;
 }
 
 /* Is walreceiver running (or starting up)? */
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index 66507e9c2dd..7255a9b5e2e 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -86,6 +86,7 @@
 #include "storage/pmsignal.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
+#include "storage/subsystems.h"
 #include "tcop/dest.h"
 #include "tcop/tcopprot.h"
 #include "utils/acl.h"
@@ -117,6 +118,14 @@
 /* Array of WalSnds in shared memory */
 WalSndCtlData *WalSndCtl = NULL;
 
+static void WalSndShmemRequest(void *arg);
+static void WalSndShmemInit(void *arg);
+
+const ShmemCallbacks WalSndShmemCallbacks = {
+	.request_fn = WalSndShmemRequest,
+	.init_fn = WalSndShmemInit,
+};
+
 /* My slot in the shared memory array */
 WalSnd	   *MyWalSnd = NULL;
 
@@ -3763,47 +3772,39 @@ WalSndSignals(void)
 	pqsignal(SIGCHLD, SIG_DFL);
 }
 
-/* Report shared-memory space needed by WalSndShmemInit */
-Size
-WalSndShmemSize(void)
+/* Register shared-memory space needed by walsender */
+static void
+WalSndShmemRequest(void *arg)
 {
-	Size		size = 0;
+	static ShmemStructDesc WalSndShmemDesc;
+	Size		size;
 
 	size = offsetof(WalSndCtlData, walsnds);
 	size = add_size(size, mul_size(max_wal_senders, sizeof(WalSnd)));
-
-	return size;
+	ShmemRequestStruct(&WalSndShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Wal Sender Ctl",
+		.size = size,
+		.ptr = (void **) &WalSndCtl,
+	});
 }
 
-/* Allocate and initialize walsender-related shared memory */
-void
-WalSndShmemInit(void)
+/* Initialize walsender-related shared memory */
+static void
+WalSndShmemInit(void *arg)
 {
-	bool		found;
-	int			i;
+	for (int i = 0; i < NUM_SYNC_REP_WAIT_MODE; i++)
+		dlist_init(&(WalSndCtl->SyncRepQueue[i]));
 
-	WalSndCtl = (WalSndCtlData *)
-		ShmemInitStruct("Wal Sender Ctl", WalSndShmemSize(), &found);
-
-	if (!found)
+	for (int i = 0; i < max_wal_senders; i++)
 	{
-		/* First time through, so initialize */
-		MemSet(WalSndCtl, 0, WalSndShmemSize());
-
-		for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; i++)
-			dlist_init(&(WalSndCtl->SyncRepQueue[i]));
-
-		for (i = 0; i < max_wal_senders; i++)
-		{
-			WalSnd	   *walsnd = &WalSndCtl->walsnds[i];
-
-			SpinLockInit(&walsnd->mutex);
-		}
+		WalSnd	   *walsnd = &WalSndCtl->walsnds[i];
 
-		ConditionVariableInit(&WalSndCtl->wal_flush_cv);
-		ConditionVariableInit(&WalSndCtl->wal_replay_cv);
-		ConditionVariableInit(&WalSndCtl->wal_confirm_rcv_cv);
+		SpinLockInit(&walsnd->mutex);
 	}
+
+	ConditionVariableInit(&WalSndCtl->wal_flush_cv);
+	ConditionVariableInit(&WalSndCtl->wal_replay_cv);
+	ConditionVariableInit(&WalSndCtl->wal_confirm_rcv_cv);
 }
 
 /*
diff --git a/src/backend/storage/aio/aio_init.c b/src/backend/storage/aio/aio_init.c
index 54ab1238131..fead2fd3776 100644
--- a/src/backend/storage/aio/aio_init.c
+++ b/src/backend/storage/aio/aio_init.c
@@ -37,32 +37,11 @@ const ShmemCallbacks AioShmemCallbacks = {
 	.attach_fn = AioShmemAttach,
 };
 
-static ShmemStructDesc AioCtlShmemDesc = {
-	.name = "AioCtl",
-	.size = sizeof(PgAioCtl),
-	.ptr = (void **) &pgaio_ctl,
-};
 
 static PgAioBackend *AioBackendShmemPtr;
-static ShmemStructDesc AioBackendShmemDesc = {
-	.name = "AioBackend",
-	.ptr = (void **) &AioBackendShmemPtr,
-};
 static PgAioHandle *AioHandleShmemPtr;
-static ShmemStructDesc AioHandleShmemDesc = {
-	.name = "AioHandle",
-	.ptr = (void **) &AioHandleShmemPtr,
-};
 static struct iovec *AioHandleIOVShmemPtr;
-static ShmemStructDesc AioHandleIOVShmemDesc = {
-	.name = "AioHandleIOV",
-	.ptr = (void **) &AioHandleIOVShmemPtr,
-};
 static uint64 *AioHandleDataShmemPtr;
-static ShmemStructDesc AioHandleDataShmemDesc = {
-	.name = "AioHandleData",
-	.ptr = (void **) &AioHandleDataShmemPtr,
-};
 
 static uint32
 AioProcs(void)
@@ -145,9 +124,15 @@ AioChooseMaxConcurrency(void)
 static void
 AioShmemRequest(void *arg)
 {
-	/* Resolve io_max_concurrency if not already done. */
+	static ShmemStructDesc AioCtlShmemDesc;
+	static ShmemStructDesc AioBackendShmemDesc;
+	static ShmemStructDesc AioHandleShmemDesc;
+	static ShmemStructDesc AioHandleIOVShmemDesc;
+	static ShmemStructDesc AioHandleDataShmemDesc;
 
 	/*
+	 * Resolve io_max_concurrency if not already done
+	 *
 	 * We prefer to report this value's source as PGC_S_DYNAMIC_DEFAULT.
 	 * However, if the DBA explicitly set io_max_concurrency = -1 in the
 	 * config file, then PGC_S_DYNAMIC_DEFAULT will fail to override that and
@@ -165,19 +150,35 @@ AioShmemRequest(void *arg)
 							PGC_S_OVERRIDE);
 	}
 
-	ShmemRequestStruct(&AioCtlShmemDesc);
-
-	AioBackendShmemDesc.size = AioBackendShmemSize();
-	ShmemRequestStruct(&AioBackendShmemDesc);
-
-	AioHandleShmemDesc.size = AioHandleShmemSize();
-	ShmemRequestStruct(&AioHandleShmemDesc);
-
-	AioHandleIOVShmemDesc.size = AioHandleIOVShmemSize();
-	ShmemRequestStruct(&AioHandleIOVShmemDesc);
-
-	AioHandleDataShmemDesc.size = AioHandleDataShmemSize();
-	ShmemRequestStruct(&AioHandleDataShmemDesc);
+	ShmemRequestStruct(&AioCtlShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "AioCtl",
+		.size = sizeof(PgAioCtl),
+		.ptr = (void **) &pgaio_ctl,
+	});
+
+	ShmemRequestStruct(&AioBackendShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "AioBackend",
+		.size = AioBackendShmemSize(),
+		.ptr = (void **) &AioBackendShmemPtr,
+	});
+
+	ShmemRequestStruct(&AioHandleShmemDesc,  &(ShmemRequestStructOpts) {
+		.name = "AioHandle",
+		.size = AioHandleShmemSize(),
+		.ptr = (void **) &AioHandleShmemPtr,
+	});
+
+	ShmemRequestStruct(&AioHandleIOVShmemDesc,  &(ShmemRequestStructOpts) {
+		.name = "AioHandleIOV",
+		.size = AioHandleIOVShmemSize(),
+		.ptr = (void **) &AioHandleIOVShmemPtr,
+	});
+
+	ShmemRequestStruct(&AioHandleDataShmemDesc,  &(ShmemRequestStructOpts) {
+		.name = "AioHandleData",
+		.size = AioHandleDataShmemSize(),
+		.ptr = (void **) &AioHandleDataShmemPtr,
+	});
 
 	if (pgaio_method_ops->shmem_callbacks.request_fn)
 		pgaio_method_ops->shmem_callbacks.request_fn(pgaio_method_ops->shmem_callbacks.request_fn_arg);
diff --git a/src/backend/storage/aio/method_io_uring.c b/src/backend/storage/aio/method_io_uring.c
index df2d01d66fa..d08be794a11 100644
--- a/src/backend/storage/aio/method_io_uring.c
+++ b/src/backend/storage/aio/method_io_uring.c
@@ -273,10 +273,7 @@ pgaio_uring_shmem_size(void)
 static void
 pgaio_uring_shmem_request(void *arg)
 {
-	static ShmemStructDesc AioUringShmemDesc = {
-		.name = "AioUringContext",
-		.ptr = (void **) &pgaio_uring_contexts,
-	};
+	static ShmemStructDesc AioUringShmemDesc;
 
 	/*
 	 * Kernel and liburing support for various features influences how much
@@ -284,8 +281,11 @@ pgaio_uring_shmem_request(void *arg)
 	 */
 	pgaio_uring_check_capabilities();
 
-	AioUringShmemDesc.size = pgaio_uring_shmem_size();
-	ShmemRequestStruct(&AioUringShmemDesc);
+	ShmemRequestStruct(&AioUringShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "AioUringContext",
+		.size  =pgaio_uring_shmem_size(),
+		.ptr = (void **) &pgaio_uring_contexts,
+	});
 }
 
 static void
diff --git a/src/backend/storage/aio/method_worker.c b/src/backend/storage/aio/method_worker.c
index 82c8b098a9e..7dc9a51f5ee 100644
--- a/src/backend/storage/aio/method_worker.c
+++ b/src/backend/storage/aio/method_worker.c
@@ -162,16 +162,18 @@ pgaio_worker_shmem_attach(void *arg)
 static void
 pgaio_worker_shmem_request(void *arg)
 {
-	static ShmemStructDesc AioWorkerShmemDesc = {
-		.name = "AioWorkerSubmissionQueue",
-		.ptr = (void **) &io_worker_submission_queue,
-	};
+	static ShmemStructDesc AioWorkerShmemDesc;
 	int			queue_size;
+	size_t		size;
 
-	AioWorkerShmemDesc.size =
-		MAXALIGN(pgaio_worker_queue_shmem_size(&queue_size)) +
+	size = MAXALIGN(pgaio_worker_queue_shmem_size(&queue_size)) +
 		pgaio_worker_control_shmem_size();
-	ShmemRequestStruct(&AioWorkerShmemDesc);
+
+	ShmemRequestStruct(&AioWorkerShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "AioWorkerSubmissionQueue",
+		.size = size,
+		.ptr = (void **) &io_worker_submission_queue,
+	});
 }
 
 static int
diff --git a/src/backend/storage/buffer/buf_init.c b/src/backend/storage/buffer/buf_init.c
index c0c223b2e32..bfee62b8208 100644
--- a/src/backend/storage/buffer/buf_init.c
+++ b/src/backend/storage/buffer/buf_init.c
@@ -18,6 +18,8 @@
 #include "storage/buf_internals.h"
 #include "storage/bufmgr.h"
 #include "storage/proclist.h"
+#include "storage/shmem.h"
+#include "storage/subsystems.h"
 
 BufferDescPadded *BufferDescriptors;
 char	   *BufferBlocks;
@@ -25,6 +27,15 @@ ConditionVariableMinimallyPadded *BufferIOCVArray;
 WritebackContext BackendWritebackContext;
 CkptSortItem *CkptBufferIds;
 
+static void BufferManagerShmemRequest(void *arg);
+static void BufferManagerShmemInit(void *arg);
+static void BufferManagerShmemAttach(void *arg);
+
+const ShmemCallbacks BufferManagerShmemCallbacks = {
+	.request_fn = BufferManagerShmemRequest,
+	.init_fn = BufferManagerShmemInit,
+	.attach_fn = BufferManagerShmemAttach,
+};
 
 /*
  * Data Structures:
@@ -60,37 +71,39 @@ CkptSortItem *CkptBufferIds;
 
 
 /*
- * Initialize shared buffer pool
- *
- * This is called once during shared-memory initialization (either in the
- * postmaster, or in a standalone backend).
+ * Register shared memory area for the buffer pool.
  */
-void
-BufferManagerShmemInit(void)
+static void
+BufferManagerShmemRequest(void *arg)
 {
-	bool		foundBufs,
-				foundDescs,
-				foundIOCV,
-				foundBufCkpt;
-
-	/* Align descriptors to a cacheline boundary. */
-	BufferDescriptors = (BufferDescPadded *)
-		ShmemInitStruct("Buffer Descriptors",
-						NBuffers * sizeof(BufferDescPadded),
-						&foundDescs);
-
-	/* Align buffer pool on IO page size boundary. */
-	BufferBlocks = (char *)
-		TYPEALIGN(PG_IO_ALIGN_SIZE,
-				  ShmemInitStruct("Buffer Blocks",
-								  NBuffers * (Size) BLCKSZ + PG_IO_ALIGN_SIZE,
-								  &foundBufs));
-
-	/* Align condition variables to cacheline boundary. */
-	BufferIOCVArray = (ConditionVariableMinimallyPadded *)
-		ShmemInitStruct("Buffer IO Condition Variables",
-						NBuffers * sizeof(ConditionVariableMinimallyPadded),
-						&foundIOCV);
+	static ShmemStructDesc BufferDescriptorsShmemDesc;
+	static ShmemStructDesc BufferBlocksShmemDesc;
+	static ShmemStructDesc BufferIOCVArrayShmemDesc;
+	static ShmemStructDesc CkptBufferIdsShmemDesc;
+
+	ShmemRequestStruct(&BufferDescriptorsShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Buffer Descriptors",
+		.size = NBuffers * sizeof(BufferDescPadded),
+		/* Align descriptors to a cacheline boundary. */
+		.alignment = PG_CACHE_LINE_SIZE,
+		.ptr = (void **) &BufferDescriptors,
+	});
+
+	ShmemRequestStruct(&BufferBlocksShmemDesc,  &(ShmemRequestStructOpts) {
+		.name = "Buffer Blocks",
+		.size = NBuffers * (Size) BLCKSZ,
+		/* Align buffer pool on IO page size boundary. */
+		.alignment = PG_IO_ALIGN_SIZE,
+		.ptr = (void **) &BufferBlocks,
+	});
+
+	ShmemRequestStruct(&BufferIOCVArrayShmemDesc,  &(ShmemRequestStructOpts) {
+		.name = "Buffer IO Condition Variables",
+		.size = NBuffers * sizeof(ConditionVariableMinimallyPadded),
+		/* Align descriptors to a cacheline boundary. */
+		.alignment = PG_CACHE_LINE_SIZE,
+		.ptr = (void **) &BufferIOCVArray,
+	});
 
 	/*
 	 * The array used to sort to-be-checkpointed buffer ids is located in
@@ -99,80 +112,51 @@ BufferManagerShmemInit(void)
 	 * the checkpointer is restarted, memory allocation failures would be
 	 * painful.
 	 */
-	CkptBufferIds = (CkptSortItem *)
-		ShmemInitStruct("Checkpoint BufferIds",
-						NBuffers * sizeof(CkptSortItem), &foundBufCkpt);
+	ShmemRequestStruct(&CkptBufferIdsShmemDesc,  &(ShmemRequestStructOpts) {
+		.name = "Checkpoint BufferIds",
+		.size = NBuffers * sizeof(CkptSortItem),
+		.ptr = (void **) &CkptBufferIds,
+	});
+}
 
-	if (foundDescs || foundBufs || foundIOCV || foundBufCkpt)
-	{
-		/* should find all of these, or none of them */
-		Assert(foundDescs && foundBufs && foundIOCV && foundBufCkpt);
-		/* note: this path is only taken in EXEC_BACKEND case */
-	}
-	else
+/*
+ * Initialize shared buffer pool
+ *
+ * This is called once during shared-memory initialization (either in the
+ * postmaster, or in a standalone backend).
+ */
+static void
+BufferManagerShmemInit(void *arg)
+{
+	/*
+	 * Initialize all the buffer headers.
+	 */
+	for (int i = 0; i < NBuffers; i++)
 	{
-		int			i;
-
-		/*
-		 * Initialize all the buffer headers.
-		 */
-		for (i = 0; i < NBuffers; i++)
-		{
-			BufferDesc *buf = GetBufferDescriptor(i);
+		BufferDesc *buf = GetBufferDescriptor(i);
 
-			ClearBufferTag(&buf->tag);
+		ClearBufferTag(&buf->tag);
 
-			pg_atomic_init_u64(&buf->state, 0);
-			buf->wait_backend_pgprocno = INVALID_PROC_NUMBER;
+		pg_atomic_init_u64(&buf->state, 0);
+		buf->wait_backend_pgprocno = INVALID_PROC_NUMBER;
 
-			buf->buf_id = i;
+		buf->buf_id = i;
 
-			pgaio_wref_clear(&buf->io_wref);
+		pgaio_wref_clear(&buf->io_wref);
 
-			proclist_init(&buf->lock_waiters);
-			ConditionVariableInit(BufferDescriptorGetIOCV(buf));
-		}
+		proclist_init(&buf->lock_waiters);
+		ConditionVariableInit(BufferDescriptorGetIOCV(buf));
 	}
 
-	/* Init other shared buffer-management stuff */
-	StrategyInitialize(!foundDescs);
-
 	/* Initialize per-backend file flush context */
 	WritebackContextInit(&BackendWritebackContext,
 						 &backend_flush_after);
 }
 
-/*
- * BufferManagerShmemSize
- *
- * compute the size of shared memory for the buffer pool including
- * data pages, buffer descriptors, hash tables, etc.
- */
-Size
-BufferManagerShmemSize(void)
+static void
+BufferManagerShmemAttach(void *arg)
 {
-	Size		size = 0;
-
-	/* size of buffer descriptors */
-	size = add_size(size, mul_size(NBuffers, sizeof(BufferDescPadded)));
-	/* to allow aligning buffer descriptors */
-	size = add_size(size, PG_CACHE_LINE_SIZE);
-
-	/* size of data pages, plus alignment padding */
-	size = add_size(size, PG_IO_ALIGN_SIZE);
-	size = add_size(size, mul_size(NBuffers, BLCKSZ));
-
-	/* size of stuff controlled by freelist.c */
-	size = add_size(size, StrategyShmemSize());
-
-	/* size of I/O condition variables */
-	size = add_size(size, mul_size(NBuffers,
-								   sizeof(ConditionVariableMinimallyPadded)));
-	/* to allow aligning the above */
-	size = add_size(size, PG_CACHE_LINE_SIZE);
-
-	/* size of checkpoint sort array in bufmgr.c */
-	size = add_size(size, mul_size(NBuffers, sizeof(CkptSortItem)));
-
-	return size;
+	/* Initialize per-backend file flush context */
+	WritebackContextInit(&BackendWritebackContext,
+						 &backend_flush_after);
 }
diff --git a/src/backend/storage/buffer/buf_table.c b/src/backend/storage/buffer/buf_table.c
index 23d85fd32e2..ebac60f8482 100644
--- a/src/backend/storage/buffer/buf_table.c
+++ b/src/backend/storage/buffer/buf_table.c
@@ -32,37 +32,25 @@ typedef struct
 
 static HTAB *SharedBufHash;
 
-
-/*
- * Estimate space needed for mapping hashtable
- *		size is the desired hash table size (possibly more than NBuffers)
- */
-Size
-BufTableShmemSize(int size)
-{
-	return hash_estimate_size(size, sizeof(BufferLookupEnt));
-}
-
 /*
- * Initialize shmem hash table for mapping buffers
+ * Register shmem hash table for mapping buffers.
  *		size is the desired hash table size (possibly more than NBuffers)
  */
 void
-InitBufTable(int size)
+BufTableShmemRequest(int size)
 {
-	HASHCTL		info;
-
-	/* assume no locking is needed yet */
-
-	/* BufferTag maps to Buffer */
-	info.keysize = sizeof(BufferTag);
-	info.entrysize = sizeof(BufferLookupEnt);
-	info.num_partitions = NUM_BUFFER_PARTITIONS;
-
-	SharedBufHash = ShmemInitHash("Shared Buffer Lookup Table",
-								  size, size,
-								  &info,
-								  HASH_ELEM | HASH_BLOBS | HASH_PARTITION | HASH_FIXED_SIZE);
+	static ShmemHashDesc SharedBufHashDesc;
+
+	ShmemRequestHash(&SharedBufHashDesc, &(ShmemRequestHashOpts) {
+		.name = "Shared Buffer Lookup Table",
+		.max_size = size,
+		.init_size = size,
+		.ptr = &SharedBufHash,
+		.hash_info.keysize = sizeof(BufferTag),
+		.hash_info.entrysize = sizeof(BufferLookupEnt),
+		.hash_info.num_partitions = NUM_BUFFER_PARTITIONS,
+		.hash_flags = HASH_ELEM | HASH_BLOBS | HASH_PARTITION | HASH_FIXED_SIZE,
+	});
 }
 
 /*
diff --git a/src/backend/storage/buffer/freelist.c b/src/backend/storage/buffer/freelist.c
index b7687836188..ef21a0175b5 100644
--- a/src/backend/storage/buffer/freelist.c
+++ b/src/backend/storage/buffer/freelist.c
@@ -20,6 +20,8 @@
 #include "storage/buf_internals.h"
 #include "storage/bufmgr.h"
 #include "storage/proc.h"
+#include "storage/shmem.h"
+#include "storage/subsystems.h"
 
 #define INT_ACCESS_ONCE(var)	((int)(*((volatile int *)&(var))))
 
@@ -56,6 +58,14 @@ typedef struct
 /* Pointers to shared state */
 static BufferStrategyControl *StrategyControl = NULL;
 
+static void StrategyCtlShmemRequest(void *arg);
+static void StrategyCtlShmemInit(void *arg);
+
+const ShmemCallbacks StrategyCtlShmemCallbacks = {
+	.request_fn = StrategyCtlShmemRequest,
+	.init_fn = StrategyCtlShmemInit,
+};
+
 /*
  * Private (non-shared) state for managing a ring of shared buffers to re-use.
  * This is currently the only kind of BufferAccessStrategy object, but someday
@@ -369,41 +379,22 @@ StrategyNotifyBgWriter(int bgwprocno)
 
 
 /*
- * StrategyShmemSize
- *
- * estimate the size of shared memory used by the freelist-related structures.
- *
- * Note: for somewhat historical reasons, the buffer lookup hashtable size
- * is also determined here.
+ * StrategyCtlShmemRequest -- register shared memory for the buffer
+ *		cache replacement strategy.
  */
-Size
-StrategyShmemSize(void)
+static void
+StrategyCtlShmemRequest(void *arg)
 {
-	Size		size = 0;
-
-	/* size of lookup hash table ... see comment in StrategyInitialize */
-	size = add_size(size, BufTableShmemSize(NBuffers + NUM_BUFFER_PARTITIONS));
-
-	/* size of the shared replacement strategy control block */
-	size = add_size(size, MAXALIGN(sizeof(BufferStrategyControl)));
+	static ShmemStructDesc StrategyCtlShmemDesc;
 
-	return size;
-}
-
-/*
- * StrategyInitialize -- initialize the buffer cache replacement
- *		strategy.
- *
- * Assumes: All of the buffers are already built into a linked list.
- *		Only called by postmaster and only during initialization.
- */
-void
-StrategyInitialize(bool init)
-{
-	bool		found;
+	ShmemRequestStruct(&StrategyCtlShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Buffer Strategy Status",
+		.size = sizeof(BufferStrategyControl),
+		.ptr = (void **) &StrategyControl
+	});
 
 	/*
-	 * Initialize the shared buffer lookup hashtable.
+	 * Request the shared buffer lookup hashtable.
 	 *
 	 * Since we can't tolerate running out of lookup table entries, we must be
 	 * sure to specify an adequate table size here.  The maximum steady-state
@@ -412,37 +403,26 @@ StrategyInitialize(bool init)
 	 * happening in each partition concurrently, so we could need as many as
 	 * NBuffers + NUM_BUFFER_PARTITIONS entries.
 	 */
-	InitBufTable(NBuffers + NUM_BUFFER_PARTITIONS);
-
-	/*
-	 * Get or create the shared strategy control block
-	 */
-	StrategyControl = (BufferStrategyControl *)
-		ShmemInitStruct("Buffer Strategy Status",
-						sizeof(BufferStrategyControl),
-						&found);
-
-	if (!found)
-	{
-		/*
-		 * Only done once, usually in postmaster
-		 */
-		Assert(init);
+	BufTableShmemRequest(NBuffers + NUM_BUFFER_PARTITIONS);
+}
 
-		SpinLockInit(&StrategyControl->buffer_strategy_lock);
+/*
+ * StrategyCtlShmemInit -- initialize the buffer cache replacement strategy.
+ */
+static void
+StrategyCtlShmemInit(void *arg)
+{
+	SpinLockInit(&StrategyControl->buffer_strategy_lock);
 
-		/* Initialize the clock-sweep pointer */
-		pg_atomic_init_u32(&StrategyControl->nextVictimBuffer, 0);
+	/* Initialize the clock-sweep pointer */
+	pg_atomic_init_u32(&StrategyControl->nextVictimBuffer, 0);
 
-		/* Clear statistics */
-		StrategyControl->completePasses = 0;
-		pg_atomic_init_u32(&StrategyControl->numBufferAllocs, 0);
+	/* Clear statistics */
+	StrategyControl->completePasses = 0;
+	pg_atomic_init_u32(&StrategyControl->numBufferAllocs, 0);
 
-		/* No pending notification */
-		StrategyControl->bgwprocno = -1;
-	}
-	else
-		Assert(!init);
+	/* No pending notification */
+	StrategyControl->bgwprocno = -1;
 }
 
 
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index b945035d98a..217f5291270 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -14,36 +14,13 @@
  */
 #include "postgres.h"
 
-#include "access/clog.h"
-#include "access/commit_ts.h"
-#include "access/multixact.h"
-#include "access/nbtree.h"
-#include "access/subtrans.h"
-#include "access/syncscan.h"
-#include "access/twophase.h"
-#include "access/xlogprefetcher.h"
-#include "access/xlogrecovery.h"
-#include "access/xlogwait.h"
-#include "commands/async.h"
 #include "miscadmin.h"
 #include "pgstat.h"
-#include "postmaster/autovacuum.h"
-#include "postmaster/bgworker_internals.h"
-#include "postmaster/bgwriter.h"
-#include "postmaster/walsummarizer.h"
-#include "replication/logicallauncher.h"
-#include "replication/origin.h"
-#include "replication/slot.h"
-#include "replication/slotsync.h"
-#include "replication/walreceiver.h"
-#include "replication/walsender.h"
-#include "storage/aio_subsys.h"
-#include "storage/bufmgr.h"
 #include "storage/dsm.h"
 #include "storage/ipc.h"
+#include "storage/lock.h"
 #include "storage/pg_shmem.h"
 #include "storage/pmsignal.h"
-#include "storage/predicate.h"
 #include "storage/proc.h"
 #include "storage/subsystems.h"
 #include "utils/guc.h"
@@ -55,8 +32,6 @@ shmem_startup_hook_type shmem_startup_hook = NULL;
 
 static Size total_addin_request = 0;
 
-static void CreateOrAttachShmemStructs(void);
-
 /*
  * RequestAddinShmemSpace
  *		Request that extra shmem space be allocated for use by
@@ -95,32 +70,6 @@ CalculateShmemSize(void)
 	size = 100000;
 	size = add_size(size, ShmemGetRequestedSize());
 
-	/* legacy subsystems */
-	size = add_size(size, BufferManagerShmemSize());
-	size = add_size(size, LockManagerShmemSize());
-	size = add_size(size, XLogPrefetchShmemSize());
-	size = add_size(size, XLOGShmemSize());
-	size = add_size(size, XLogRecoveryShmemSize());
-	size = add_size(size, TwoPhaseShmemSize());
-	size = add_size(size, BackgroundWorkerShmemSize());
-	size = add_size(size, LWLockShmemSize());
-	size = add_size(size, BackendStatusShmemSize());
-	size = add_size(size, CheckpointerShmemSize());
-	size = add_size(size, AutoVacuumShmemSize());
-	size = add_size(size, ReplicationSlotsShmemSize());
-	size = add_size(size, ReplicationOriginShmemSize());
-	size = add_size(size, WalSndShmemSize());
-	size = add_size(size, WalRcvShmemSize());
-	size = add_size(size, WalSummarizerShmemSize());
-	size = add_size(size, PgArchShmemSize());
-	size = add_size(size, ApplyLauncherShmemSize());
-	size = add_size(size, BTreeShmemSize());
-	size = add_size(size, SyncScanShmemSize());
-	size = add_size(size, StatsShmemSize());
-	size = add_size(size, SlotSyncShmemSize());
-	size = add_size(size, WaitLSNShmemSize());
-	size = add_size(size, LogicalDecodingCtlShmemSize());
-
 	/* include additional requested shmem from preload libraries */
 	size = add_size(size, total_addin_request);
 
@@ -159,7 +108,6 @@ AttachSharedMemoryStructs(void)
 
 	/* Establish pointers to all shared memory areas in this backend */
 	ShmemAttachRequested();
-	CreateOrAttachShmemStructs();
 
 	/*
 	 * Now give loadable modules a chance to set up their shmem allocations
@@ -217,9 +165,6 @@ CreateSharedMemoryAndSemaphores(void)
 	/* Initialize all shmem areas */
 	ShmemInitRequested();
 
-	/* Initialize legacy subsystems */
-	CreateOrAttachShmemStructs();
-
 	/* Initialize dynamic shared memory facilities. */
 	dsm_postmaster_startup(shim);
 
@@ -248,68 +193,6 @@ RegisterBuiltinShmemCallbacks(void)
 		RegisterShmemCallbacks(builtin_subsystems[i]);
 }
 
-/*
- * Initialize various subsystems, setting up their data structures in
- * shared memory.
- *
- * This is called by the postmaster or by a standalone backend.
- * It is also called by a backend forked from the postmaster in the
- * EXEC_BACKEND case.  In the latter case, the shared memory segment
- * already exists and has been physically attached to, but we have to
- * initialize pointers in local memory that reference the shared structures,
- * because we didn't inherit the correct pointer values from the postmaster
- * as we do in the fork() scenario.  The easiest way to do that is to run
- * through the same code as before.  (Note that the called routines mostly
- * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
- * This is a bit code-wasteful and could be cleaned up.)
- */
-static void
-CreateOrAttachShmemStructs(void)
-{
-	/*
-	 * Set up xlog, clog, and buffers
-	 */
-	XLOGShmemInit();
-	XLogPrefetchShmemInit();
-	XLogRecoveryShmemInit();
-	BufferManagerShmemInit();
-
-	/*
-	 * Set up lock manager
-	 */
-	LockManagerShmemInit();
-
-	/*
-	 * Set up process table
-	 */
-	BackendStatusShmemInit();
-	TwoPhaseShmemInit();
-	BackgroundWorkerShmemInit();
-
-	/*
-	 * Set up interprocess signaling mechanisms
-	 */
-	CheckpointerShmemInit();
-	AutoVacuumShmemInit();
-	ReplicationSlotsShmemInit();
-	ReplicationOriginShmemInit();
-	WalSndShmemInit();
-	WalRcvShmemInit();
-	WalSummarizerShmemInit();
-	PgArchShmemInit();
-	ApplyLauncherShmemInit();
-	SlotSyncShmemInit();
-
-	/*
-	 * Set up other modules that need some shared memory space
-	 */
-	BTreeShmemInit();
-	SyncScanShmemInit();
-	StatsShmemInit();
-	WaitLSNShmemInit();
-	LogicalDecodingCtlShmemInit();
-}
-
 /*
  * InitializeShmemGUCs
  *
diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index 234643e4dd7..566374b9c40 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -43,8 +43,10 @@
 #include "storage/lmgr.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
+#include "storage/shmem.h"
 #include "storage/spin.h"
 #include "storage/standby.h"
+#include "storage/subsystems.h"
 #include "utils/memutils.h"
 #include "utils/ps_status.h"
 #include "utils/resowner.h"
@@ -312,6 +314,14 @@ typedef struct
 
 static volatile FastPathStrongRelationLockData *FastPathStrongRelationLocks;
 
+static void LockManagerShmemRequest(void *arg);
+static void LockManagerShmemInit(void *arg);
+
+const ShmemCallbacks LockManagerShmemCallbacks = {
+	.request_fn = LockManagerShmemRequest,
+	.init_fn = LockManagerShmemInit,
+};
+
 
 /*
  * Pointers to hash tables containing lock state
@@ -409,6 +419,7 @@ PROCLOCK_PRINT(const char *where, const PROCLOCK *proclockP)
 
 
 static uint32 proclock_hash(const void *key, Size keysize);
+
 static void RemoveLocalLock(LOCALLOCK *locallock);
 static PROCLOCK *SetupLockInTable(LockMethod lockMethodTable, PGPROC *proc,
 								  const LOCKTAG *locktag, uint32 hashcode, LOCKMODE lockmode);
@@ -432,22 +443,19 @@ static void GetSingleProcBlockerStatusData(PGPROC *blocked_proc,
 
 
 /*
- * Initialize the lock manager's shmem data structures.
+ * Register the lock manager's shmem data structures.
  *
- * This is called from CreateSharedMemoryAndSemaphores(), which see for more
- * comments.  In the normal postmaster case, the shared hash tables are
- * created here, and backends inherit pointers to them via fork().  In the
- * EXEC_BACKEND case, each backend re-executes this code to obtain pointers to
- * the already existing shared hash tables.  In either case, each backend must
- * also call InitLockManagerAccess() to create the locallock hash table.
+ * In addition to this, each backend must also call InitLockManagerAccess() to
+ * create the locallock hash table.
  */
-void
-LockManagerShmemInit(void)
+static void
+LockManagerShmemRequest(void *arg)
 {
-	HASHCTL		info;
-	int64		init_table_size,
+	static ShmemHashDesc LockHashDesc;
+	static ShmemHashDesc ProcLockHashDesc;
+	static ShmemStructDesc FastPathShmemDesc;
+	long		init_table_size,
 				max_table_size;
-	bool		found;
 
 	/*
 	 * Compute init/max size to request for lock hashtables.  Note these
@@ -456,47 +464,51 @@ LockManagerShmemInit(void)
 	max_table_size = NLOCKENTS();
 	init_table_size = max_table_size / 2;
 
-	/*
-	 * Allocate hash table for LOCK structs.  This stores per-locked-object
-	 * information.
-	 */
-	info.keysize = sizeof(LOCKTAG);
-	info.entrysize = sizeof(LOCK);
-	info.num_partitions = NUM_LOCK_PARTITIONS;
-
-	LockMethodLockHash = ShmemInitHash("LOCK hash",
-									   init_table_size,
-									   max_table_size,
-									   &info,
-									   HASH_ELEM | HASH_BLOBS | HASH_PARTITION);
+	ShmemRequestHash(&LockHashDesc, &(ShmemRequestHashOpts) {
+		.name = "LOCK hash",
+		.init_size = init_table_size,
+		.max_size = max_table_size,
+		.ptr = &LockMethodLockHash,
+		.hash_info.keysize = sizeof(LOCKTAG),
+		.hash_info.entrysize = sizeof(LOCK),
+		.hash_info.num_partitions = NUM_LOCK_PARTITIONS,
+		.hash_flags = HASH_ELEM | HASH_BLOBS | HASH_PARTITION,
+	});
 
 	/* Assume an average of 2 holders per lock */
 	max_table_size *= 2;
 	init_table_size *= 2;
 
-	/*
-	 * Allocate hash table for PROCLOCK structs.  This stores
-	 * per-lock-per-holder information.
-	 */
-	info.keysize = sizeof(PROCLOCKTAG);
-	info.entrysize = sizeof(PROCLOCK);
-	info.hash = proclock_hash;
-	info.num_partitions = NUM_LOCK_PARTITIONS;
-
-	LockMethodProcLockHash = ShmemInitHash("PROCLOCK hash",
-										   init_table_size,
-										   max_table_size,
-										   &info,
-										   HASH_ELEM | HASH_FUNCTION | HASH_PARTITION);
+	ShmemRequestHash(&ProcLockHashDesc, &(ShmemRequestHashOpts) {
+		.name = "PROCLOCK hash",
+		.init_size = init_table_size,
+		.max_size = max_table_size,
+		.ptr = &LockMethodProcLockHash,
+		.hash_info.keysize = sizeof(PROCLOCKTAG),
+		.hash_info.entrysize = sizeof(PROCLOCK),
+		.hash_info.hash = proclock_hash,
+		.hash_info.num_partitions = NUM_LOCK_PARTITIONS,
+		.hash_flags = HASH_ELEM | HASH_FUNCTION | HASH_PARTITION,
+	});
+
+	ShmemRequestStruct(&FastPathShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Fast Path Strong Relation Lock Data",
+		.size = sizeof(FastPathStrongRelationLockData),
+		.ptr = (void **) (void *) &FastPathStrongRelationLocks,
+	});
 
 	/*
-	 * Allocate fast-path structures.
+	 * FIXME: we used to do this in the size calculation:
+	 *
+	 * // Since NLOCKENTS is only an estimate, add 10% safety margin.
+	 * size = add_size(size, size / 10);
 	 */
-	FastPathStrongRelationLocks =
-		ShmemInitStruct("Fast Path Strong Relation Lock Data",
-						sizeof(FastPathStrongRelationLockData), &found);
-	if (!found)
-		SpinLockInit(&FastPathStrongRelationLocks->mutex);
+}
+
+static void
+LockManagerShmemInit(void *arg)
+{
+	SpinLockInit(&FastPathStrongRelationLocks->mutex);
 }
 
 /*
@@ -3761,30 +3773,6 @@ PostPrepare_Locks(FullTransactionId fxid)
 }
 
 
-/*
- * Estimate shared-memory space used for lock tables
- */
-Size
-LockManagerShmemSize(void)
-{
-	Size		size = 0;
-	long		max_table_size;
-
-	/* lock hash table */
-	max_table_size = NLOCKENTS();
-	size = add_size(size, hash_estimate_size(max_table_size, sizeof(LOCK)));
-
-	/* proclock hash table */
-	max_table_size *= 2;
-	size = add_size(size, hash_estimate_size(max_table_size, sizeof(PROCLOCK)));
-
-	/*
-	 * Since NLOCKENTS is only an estimate, add 10% safety margin.
-	 */
-	size = add_size(size, size / 10);
-
-	return size;
-}
 
 /*
  * GetLockStatusData - Return a summary of the lock manager's internal
diff --git a/src/backend/utils/activity/backend_status.c b/src/backend/utils/activity/backend_status.c
index cd087129469..958d4b04ef7 100644
--- a/src/backend/utils/activity/backend_status.c
+++ b/src/backend/utils/activity/backend_status.c
@@ -18,7 +18,9 @@
 #include "pgstat.h"
 #include "storage/ipc.h"
 #include "storage/proc.h"		/* for MyProc */
+#include "storage/shmem.h"
 #include "storage/procarray.h"
+#include "storage/subsystems.h"
 #include "utils/ascii.h"
 #include "utils/guc.h"			/* for application_name */
 #include "utils/memutils.h"
@@ -73,133 +75,114 @@ static void pgstat_beshutdown_hook(int code, Datum arg);
 static void pgstat_read_current_status(void);
 static void pgstat_setup_backend_status_context(void);
 
+static void BackendStatusShmemRequest(void *arg);
+static void BackendStatusShmemInit(void *arg);
+static void BackendStatusShmemAttach(void *arg);
+
+const ShmemCallbacks BackendStatusShmemCallbacks = {
+	.request_fn = BackendStatusShmemRequest,
+	.init_fn = BackendStatusShmemInit,
+	.attach_fn = BackendStatusShmemAttach,
+};
 
 /*
- * Report shared-memory space needed by BackendStatusShmemInit.
+ * Register shared memory needs for backend status reporting.
  */
-Size
-BackendStatusShmemSize(void)
+static void
+BackendStatusShmemRequest(void *arg)
 {
-	Size		size;
-
-	/* BackendStatusArray: */
-	size = mul_size(sizeof(PgBackendStatus), NumBackendStatSlots);
-	/* BackendAppnameBuffer: */
-	size = add_size(size,
-					mul_size(NAMEDATALEN, NumBackendStatSlots));
-	/* BackendClientHostnameBuffer: */
-	size = add_size(size,
-					mul_size(NAMEDATALEN, NumBackendStatSlots));
-	/* BackendActivityBuffer: */
-	size = add_size(size,
-					mul_size(pgstat_track_activity_query_size, NumBackendStatSlots));
+	static ShmemStructDesc BackendStatusArrayShmemDesc;
+	static ShmemStructDesc BackendAppnameBufferShmemDesc;
+	static ShmemStructDesc BackendClientHostnameBufferShmemDesc;
+	static ShmemStructDesc BackendActivityBufferSizeShmemDesc;
 #ifdef USE_SSL
-	/* BackendSslStatusBuffer: */
-	size = add_size(size,
-					mul_size(sizeof(PgBackendSSLStatus), NumBackendStatSlots));
+	static ShmemStructDesc BackendSslStatusBufferShmemDesc;
 #endif
 #ifdef ENABLE_GSS
-	/* BackendGssStatusBuffer: */
-	size = add_size(size,
-					mul_size(sizeof(PgBackendGSSStatus), NumBackendStatSlots));
+	static ShmemStructDesc BackendGssStatusBufferShmemDesc;
+#endif
+
+	ShmemRequestStruct(&BackendStatusArrayShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Backend Status Array",
+		.size = mul_size(sizeof(PgBackendStatus), NumBackendStatSlots),
+		.ptr = (void **) &BackendStatusArray,
+	});
+
+	ShmemRequestStruct(&BackendAppnameBufferShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Backend Application Name Buffer",
+		.size = mul_size(NAMEDATALEN, NumBackendStatSlots),
+		.ptr = (void **) &BackendAppnameBuffer,
+	});
+
+	ShmemRequestStruct(&BackendClientHostnameBufferShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Backend Client Host Name Buffer",
+		.size =	mul_size(NAMEDATALEN, NumBackendStatSlots),
+		.ptr = (void **) &BackendClientHostnameBuffer,
+	});
+
+	BackendActivityBufferSize = mul_size(pgstat_track_activity_query_size,
+										 NumBackendStatSlots);
+	ShmemRequestStruct(&BackendActivityBufferSizeShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Backend Activity Buffer",
+		.size = BackendActivityBufferSize,
+		.ptr = (void **) &BackendActivityBuffer
+	});
+
+#ifdef USE_SSL
+	ShmemRequestStruct(&BackendSslStatusBufferShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Backend SSL Status Buffer",
+		.size = mul_size(sizeof(PgBackendSSLStatus), NumBackendStatSlots),
+		.ptr = (void **) &BackendSslStatusBuffer,
+	});
+#endif
+
+#ifdef ENABLE_GSS
+	ShmemRequestStruct(&BackendGssStatusBufferShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Backend GSS Status Buffer",
+		.size = mul_size(sizeof(PgBackendGSSStatus), NumBackendStatSlots),
+		.ptr = (void **) &BackendGssStatusBuffer,
+	});
 #endif
-	return size;
 }
 
 /*
  * Initialize the shared status array and several string buffers
  * during postmaster startup.
  */
-void
-BackendStatusShmemInit(void)
+static void
+BackendStatusShmemInit(void *arg)
 {
-	Size		size;
-	bool		found;
 	int			i;
 	char	   *buffer;
 
-	/* Create or attach to the shared array */
-	size = mul_size(sizeof(PgBackendStatus), NumBackendStatSlots);
-	BackendStatusArray = (PgBackendStatus *)
-		ShmemInitStruct("Backend Status Array", size, &found);
-
-	if (!found)
+	/* Initialize st_appname pointers. */
+	buffer = BackendAppnameBuffer;
+	for (i = 0; i < NumBackendStatSlots; i++)
 	{
-		/*
-		 * We're the first - initialize.
-		 */
-		MemSet(BackendStatusArray, 0, size);
+		BackendStatusArray[i].st_appname = buffer;
+		buffer += NAMEDATALEN;
 	}
 
-	/* Create or attach to the shared appname buffer */
-	size = mul_size(NAMEDATALEN, NumBackendStatSlots);
-	BackendAppnameBuffer = (char *)
-		ShmemInitStruct("Backend Application Name Buffer", size, &found);
-
-	if (!found)
+	/* Initialize st_clienthostname pointers. */
+	buffer = BackendClientHostnameBuffer;
+	for (i = 0; i < NumBackendStatSlots; i++)
 	{
-		MemSet(BackendAppnameBuffer, 0, size);
-
-		/* Initialize st_appname pointers. */
-		buffer = BackendAppnameBuffer;
-		for (i = 0; i < NumBackendStatSlots; i++)
-		{
-			BackendStatusArray[i].st_appname = buffer;
-			buffer += NAMEDATALEN;
-		}
+		BackendStatusArray[i].st_clienthostname = buffer;
+		buffer += NAMEDATALEN;
 	}
 
-	/* Create or attach to the shared client hostname buffer */
-	size = mul_size(NAMEDATALEN, NumBackendStatSlots);
-	BackendClientHostnameBuffer = (char *)
-		ShmemInitStruct("Backend Client Host Name Buffer", size, &found);
-
-	if (!found)
+	/* Initialize st_activity pointers. */
+	buffer = BackendActivityBuffer;
+	for (i = 0; i < NumBackendStatSlots; i++)
 	{
-		MemSet(BackendClientHostnameBuffer, 0, size);
-
-		/* Initialize st_clienthostname pointers. */
-		buffer = BackendClientHostnameBuffer;
-		for (i = 0; i < NumBackendStatSlots; i++)
-		{
-			BackendStatusArray[i].st_clienthostname = buffer;
-			buffer += NAMEDATALEN;
-		}
-	}
-
-	/* Create or attach to the shared activity buffer */
-	BackendActivityBufferSize = mul_size(pgstat_track_activity_query_size,
-										 NumBackendStatSlots);
-	BackendActivityBuffer = (char *)
-		ShmemInitStruct("Backend Activity Buffer",
-						BackendActivityBufferSize,
-						&found);
-
-	if (!found)
-	{
-		MemSet(BackendActivityBuffer, 0, BackendActivityBufferSize);
-
-		/* Initialize st_activity pointers. */
-		buffer = BackendActivityBuffer;
-		for (i = 0; i < NumBackendStatSlots; i++)
-		{
-			BackendStatusArray[i].st_activity_raw = buffer;
-			buffer += pgstat_track_activity_query_size;
-		}
+		BackendStatusArray[i].st_activity_raw = buffer;
+		buffer += pgstat_track_activity_query_size;
 	}
 
 #ifdef USE_SSL
-	/* Create or attach to the shared SSL status buffer */
-	size = mul_size(sizeof(PgBackendSSLStatus), NumBackendStatSlots);
-	BackendSslStatusBuffer = (PgBackendSSLStatus *)
-		ShmemInitStruct("Backend SSL Status Buffer", size, &found);
-
-	if (!found)
 	{
 		PgBackendSSLStatus *ptr;
 
-		MemSet(BackendSslStatusBuffer, 0, size);
-
 		/* Initialize st_sslstatus pointers. */
 		ptr = BackendSslStatusBuffer;
 		for (i = 0; i < NumBackendStatSlots; i++)
@@ -211,17 +194,9 @@ BackendStatusShmemInit(void)
 #endif
 
 #ifdef ENABLE_GSS
-	/* Create or attach to the shared GSSAPI status buffer */
-	size = mul_size(sizeof(PgBackendGSSStatus), NumBackendStatSlots);
-	BackendGssStatusBuffer = (PgBackendGSSStatus *)
-		ShmemInitStruct("Backend GSS Status Buffer", size, &found);
-
-	if (!found)
 	{
 		PgBackendGSSStatus *ptr;
 
-		MemSet(BackendGssStatusBuffer, 0, size);
-
 		/* Initialize st_gssstatus pointers. */
 		ptr = BackendGssStatusBuffer;
 		for (i = 0; i < NumBackendStatSlots; i++)
@@ -233,6 +208,13 @@ BackendStatusShmemInit(void)
 #endif
 }
 
+static void
+BackendStatusShmemAttach(void *arg)
+{
+	BackendActivityBufferSize = mul_size(pgstat_track_activity_query_size,
+										 NumBackendStatSlots);
+}
+
 /*
  * Initialize pgstats backend activity state, and set up our on-proc-exit
  * hook.  Called from InitPostgres and AuxiliaryProcessMain.  MyProcNumber must
diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/activity/pgstat_shmem.c
index 33fbdca9609..ac96baf65c9 100644
--- a/src/backend/utils/activity/pgstat_shmem.c
+++ b/src/backend/utils/activity/pgstat_shmem.c
@@ -14,6 +14,7 @@
 
 #include "pgstat.h"
 #include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "utils/memutils.h"
 #include "utils/pgstat_internal.h"
 
@@ -57,6 +58,13 @@ static void pgstat_release_matching_entry_refs(bool discard_pending, ReleaseMatc
 
 static void pgstat_setup_memcxt(void);
 
+static void StatsShmemRequest(void *arg);
+static void StatsShmemInit(void *arg);
+
+const ShmemCallbacks StatsShmemCallbacks = {
+	.request_fn = StatsShmemRequest,
+	.init_fn = StatsShmemInit,
+};
 
 /* parameter for the shared hash */
 static const dshash_parameters dsh_params = {
@@ -123,7 +131,7 @@ pgstat_dsa_init_size(void)
 /*
  * Compute shared memory space needed for cumulative statistics
  */
-Size
+static Size
 StatsShmemSize(void)
 {
 	Size		sz;
@@ -150,101 +158,100 @@ StatsShmemSize(void)
 }
 
 /*
- * Initialize cumulative statistics system during startup
+ * Register shared memory area for cumulative statistics
  */
-void
-StatsShmemInit(void)
+static void
+StatsShmemRequest(void *arg)
 {
-	bool		found;
-	Size		sz;
-
-	sz = StatsShmemSize();
-	pgStatLocal.shmem = (PgStat_ShmemControl *)
-		ShmemInitStruct("Shared Memory Stats", sz, &found);
+	static ShmemStructDesc StatsShmemDesc;
 
-	if (!IsUnderPostmaster)
-	{
-		dsa_area   *dsa;
-		dshash_table *dsh;
-		PgStat_ShmemControl *ctl = pgStatLocal.shmem;
-		char	   *p = (char *) ctl;
+	ShmemRequestStruct(&StatsShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "Shared Memory Stats",
+		.size = StatsShmemSize(),
+		.ptr = (void **) &pgStatLocal.shmem,
+	});
+}
 
-		Assert(!found);
+/*
+ * Initialize cumulative statistics system during startup
+ */
+static void
+StatsShmemInit(void *arg)
+{
+	dsa_area   *dsa;
+	dshash_table *dsh;
+	PgStat_ShmemControl *ctl = pgStatLocal.shmem;
+	char	   *p = (char *) ctl;
 
-		/* the allocation of pgStatLocal.shmem itself */
-		p += MAXALIGN(sizeof(PgStat_ShmemControl));
+	/* the allocation of pgStatLocal.shmem itself */
+	p += MAXALIGN(sizeof(PgStat_ShmemControl));
 
-		/*
-		 * Create a small dsa allocation in plain shared memory. This is
-		 * required because postmaster cannot use dsm segments. It also
-		 * provides a small efficiency win.
-		 */
-		ctl->raw_dsa_area = p;
-		dsa = dsa_create_in_place(ctl->raw_dsa_area,
-								  pgstat_dsa_init_size(),
-								  LWTRANCHE_PGSTATS_DSA, NULL);
-		dsa_pin(dsa);
+	/*
+	 * Create a small dsa allocation in plain shared memory. This is
+	 * required because postmaster cannot use dsm segments. It also
+	 * provides a small efficiency win.
+	 */
+	ctl->raw_dsa_area = p;
+	dsa = dsa_create_in_place(ctl->raw_dsa_area,
+							  pgstat_dsa_init_size(),
+							  LWTRANCHE_PGSTATS_DSA, NULL);
+	dsa_pin(dsa);
 
-		/*
-		 * To ensure dshash is created in "plain" shared memory, temporarily
-		 * limit size of dsa to the initial size of the dsa.
-		 */
-		dsa_set_size_limit(dsa, pgstat_dsa_init_size());
+	/*
+	 * To ensure dshash is created in "plain" shared memory, temporarily
+	 * limit size of dsa to the initial size of the dsa.
+	 */
+	dsa_set_size_limit(dsa, pgstat_dsa_init_size());
 
-		/*
-		 * With the limit in place, create the dshash table. XXX: It'd be nice
-		 * if there were dshash_create_in_place().
-		 */
-		dsh = dshash_create(dsa, &dsh_params, NULL);
-		ctl->hash_handle = dshash_get_hash_table_handle(dsh);
+	/*
+	 * With the limit in place, create the dshash table. XXX: It'd be nice
+	 * if there were dshash_create_in_place().
+	 */
+	dsh = dshash_create(dsa, &dsh_params, NULL);
+	ctl->hash_handle = dshash_get_hash_table_handle(dsh);
 
-		/* lift limit set above */
-		dsa_set_size_limit(dsa, -1);
+	/* lift limit set above */
+	dsa_set_size_limit(dsa, -1);
 
-		/*
-		 * Postmaster will never access these again, thus free the local
-		 * dsa/dshash references.
-		 */
-		dshash_detach(dsh);
-		dsa_detach(dsa);
+	/*
+	 * Postmaster will never access these again, thus free the local
+	 * dsa/dshash references.
+	 */
+	dshash_detach(dsh);
+	dsa_detach(dsa);
 
-		pg_atomic_init_u64(&ctl->gc_request_count, 1);
+	pg_atomic_init_u64(&ctl->gc_request_count, 1);
 
-		/* Do the per-kind initialization */
-		for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
-		{
-			const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
-			char	   *ptr;
+	/* Do the per-kind initialization */
+	for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
+	{
+		const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
+		char	   *ptr;
 
-			if (!kind_info)
-				continue;
+		if (!kind_info)
+			continue;
 
-			/* initialize entry count tracking */
-			if (kind_info->track_entry_count)
-				pg_atomic_init_u64(&ctl->entry_counts[kind - 1], 0);
+		/* initialize entry count tracking */
+		if (kind_info->track_entry_count)
+			pg_atomic_init_u64(&ctl->entry_counts[kind - 1], 0);
 
-			/* initialize fixed-numbered stats */
-			if (kind_info->fixed_amount)
+		/* initialize fixed-numbered stats */
+		if (kind_info->fixed_amount)
+		{
+			if (pgstat_is_kind_builtin(kind))
+				ptr = ((char *) ctl) + kind_info->shared_ctl_off;
+			else
 			{
-				if (pgstat_is_kind_builtin(kind))
-					ptr = ((char *) ctl) + kind_info->shared_ctl_off;
-				else
-				{
-					int			idx = kind - PGSTAT_KIND_CUSTOM_MIN;
-
-					Assert(kind_info->shared_size != 0);
-					ctl->custom_data[idx] = ShmemAlloc(kind_info->shared_size);
-					ptr = ctl->custom_data[idx];
-				}
-
-				kind_info->init_shmem_cb(ptr);
+				int			idx = kind - PGSTAT_KIND_CUSTOM_MIN;
+
+				Assert(kind_info->shared_size != 0);
+				ctl->custom_data[idx] = ShmemAlloc(kind_info->shared_size);
+				ptr = ctl->custom_data[idx];
 			}
+
+			kind_info->init_shmem_cb(ptr);
 		}
 	}
-	else
-	{
-		Assert(found);
-	}
 }
 
 void
diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h
index da7503c57b6..3097e9bb1af 100644
--- a/src/include/access/nbtree.h
+++ b/src/include/access/nbtree.h
@@ -1300,8 +1300,6 @@ extern BTCycleId _bt_vacuum_cycleid(Relation rel);
 extern BTCycleId _bt_start_vacuum(Relation rel);
 extern void _bt_end_vacuum(Relation rel);
 extern void _bt_end_vacuum_callback(int code, Datum arg);
-extern Size BTreeShmemSize(void);
-extern void BTreeShmemInit(void);
 extern bytea *btoptions(Datum reloptions, bool validate);
 extern bool btproperty(Oid index_oid, int attno,
 					   IndexAMProperty prop, const char *propname,
diff --git a/src/include/access/syncscan.h b/src/include/access/syncscan.h
index 24cf33294e5..32f8332aaee 100644
--- a/src/include/access/syncscan.h
+++ b/src/include/access/syncscan.h
@@ -24,7 +24,5 @@ extern PGDLLIMPORT bool trace_syncscan;
 
 extern void ss_report_location(Relation rel, BlockNumber location);
 extern BlockNumber ss_get_location(Relation rel, BlockNumber relnblocks);
-extern void SyncScanShmemInit(void);
-extern Size SyncScanShmemSize(void);
 
 #endif
diff --git a/src/include/access/twophase.h b/src/include/access/twophase.h
index 761d56a5f3d..1d2ff42c9b7 100644
--- a/src/include/access/twophase.h
+++ b/src/include/access/twophase.h
@@ -33,9 +33,6 @@ typedef struct GlobalTransactionData *GlobalTransaction;
 /* GUC variable */
 extern PGDLLIMPORT int max_prepared_xacts;
 
-extern Size TwoPhaseShmemSize(void);
-extern void TwoPhaseShmemInit(void);
-
 extern void AtAbort_Twophase(void);
 extern void PostPrepare_Twophase(void);
 
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index dcc12eb8cbe..1a098a91444 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -246,8 +246,6 @@ extern char *GetMockAuthenticationNonce(void);
 extern bool DataChecksumsEnabled(void);
 extern bool GetDefaultCharSignedness(void);
 extern XLogRecPtr GetFakeLSNForUnloggedRel(void);
-extern Size XLOGShmemSize(void);
-extern void XLOGShmemInit(void);
 extern void BootStrapXLOG(uint32 data_checksum_version);
 extern void InitializeWalConsistencyChecking(void);
 extern void LocalProcessControlFile(bool reset);
diff --git a/src/include/access/xlogprefetcher.h b/src/include/access/xlogprefetcher.h
index 7ec40c4b78b..56a81676d92 100644
--- a/src/include/access/xlogprefetcher.h
+++ b/src/include/access/xlogprefetcher.h
@@ -34,9 +34,6 @@ typedef struct XLogPrefetcher XLogPrefetcher;
 
 extern void XLogPrefetchReconfigure(void);
 
-extern size_t XLogPrefetchShmemSize(void);
-extern void XLogPrefetchShmemInit(void);
-
 extern void XLogPrefetchResetStats(void);
 
 extern XLogPrefetcher *XLogPrefetcherAllocate(XLogReaderState *reader);
diff --git a/src/include/access/xlogrecovery.h b/src/include/access/xlogrecovery.h
index 2842106b285..ba7750dca0b 100644
--- a/src/include/access/xlogrecovery.h
+++ b/src/include/access/xlogrecovery.h
@@ -153,9 +153,6 @@ extern PGDLLIMPORT bool reachedConsistency;
 /* Are we currently in standby mode? */
 extern PGDLLIMPORT bool StandbyMode;
 
-extern Size XLogRecoveryShmemSize(void);
-extern void XLogRecoveryShmemInit(void);
-
 extern void InitWalRecovery(ControlFileData *ControlFile,
 							bool *wasShutdown_ptr, bool *haveBackupLabel_ptr,
 							bool *haveTblspcMap_ptr);
diff --git a/src/include/access/xlogwait.h b/src/include/access/xlogwait.h
index d12531d32b8..07157f220ea 100644
--- a/src/include/access/xlogwait.h
+++ b/src/include/access/xlogwait.h
@@ -100,8 +100,6 @@ typedef struct WaitLSNState
 
 extern PGDLLIMPORT WaitLSNState *waitLSNState;
 
-extern Size WaitLSNShmemSize(void);
-extern void WaitLSNShmemInit(void);
 extern XLogRecPtr GetCurrentLSNForWaitType(WaitLSNType lsnType);
 extern void WaitLSNWakeup(WaitLSNType lsnType, XLogRecPtr currentLSN);
 extern void WaitLSNCleanup(void);
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 8e3549c3752..2786a7c5ffb 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -541,10 +541,6 @@ typedef struct PgStat_BackendPending
  * Functions in pgstat.c
  */
 
-/* functions called from postmaster */
-extern Size StatsShmemSize(void);
-extern void StatsShmemInit(void);
-
 /* Functions called during server startup / shutdown */
 extern void pgstat_restore_stats(void);
 extern void pgstat_discard_stats(void);
diff --git a/src/include/postmaster/autovacuum.h b/src/include/postmaster/autovacuum.h
index 5aa0f3a8ac1..6ebfafe640d 100644
--- a/src/include/postmaster/autovacuum.h
+++ b/src/include/postmaster/autovacuum.h
@@ -62,8 +62,4 @@ pg_noreturn extern void AutoVacWorkerMain(const void *startup_data, size_t start
 extern bool AutoVacuumRequestWork(AutoVacuumWorkItemType type,
 								  Oid relationId, BlockNumber blkno);
 
-/* shared memory stuff */
-extern Size AutoVacuumShmemSize(void);
-extern void AutoVacuumShmemInit(void);
-
 #endif							/* AUTOVACUUM_H */
diff --git a/src/include/postmaster/bgworker_internals.h b/src/include/postmaster/bgworker_internals.h
index b789caf4034..b6261bc01df 100644
--- a/src/include/postmaster/bgworker_internals.h
+++ b/src/include/postmaster/bgworker_internals.h
@@ -41,8 +41,6 @@ typedef struct RegisteredBgWorker
 
 extern PGDLLIMPORT dlist_head BackgroundWorkerList;
 
-extern Size BackgroundWorkerShmemSize(void);
-extern void BackgroundWorkerShmemInit(void);
 extern void BackgroundWorkerStateChange(bool allow_new_workers);
 extern void ForgetBackgroundWorker(RegisteredBgWorker *rw);
 extern void ReportBackgroundWorkerPID(RegisteredBgWorker *rw);
diff --git a/src/include/postmaster/bgwriter.h b/src/include/postmaster/bgwriter.h
index 47470cba893..36eea0b1ab0 100644
--- a/src/include/postmaster/bgwriter.h
+++ b/src/include/postmaster/bgwriter.h
@@ -39,9 +39,6 @@ extern bool ForwardSyncRequest(const FileTag *ftag, SyncRequestType type);
 
 extern void AbsorbSyncRequests(void);
 
-extern Size CheckpointerShmemSize(void);
-extern void CheckpointerShmemInit(void);
-
 extern bool FirstCallSinceLastCheckpoint(void);
 
 #endif							/* _BGWRITER_H */
diff --git a/src/include/postmaster/pgarch.h b/src/include/postmaster/pgarch.h
index faa7609cd81..9772bb573a1 100644
--- a/src/include/postmaster/pgarch.h
+++ b/src/include/postmaster/pgarch.h
@@ -26,8 +26,6 @@
 #define MAX_XFN_CHARS	40
 #define VALID_XFN_CHARS "0123456789ABCDEF.history.backup.partial"
 
-extern Size PgArchShmemSize(void);
-extern void PgArchShmemInit(void);
 extern bool PgArchCanRestart(void);
 pg_noreturn extern void PgArchiverMain(const void *startup_data, size_t startup_data_len);
 extern void PgArchWakeup(void);
diff --git a/src/include/postmaster/walsummarizer.h b/src/include/postmaster/walsummarizer.h
index a4c055066b4..b9a755fadbc 100644
--- a/src/include/postmaster/walsummarizer.h
+++ b/src/include/postmaster/walsummarizer.h
@@ -19,8 +19,6 @@
 extern PGDLLIMPORT bool summarize_wal;
 extern PGDLLIMPORT int wal_summary_keep_time;
 
-extern Size WalSummarizerShmemSize(void);
-extern void WalSummarizerShmemInit(void);
 pg_noreturn extern void WalSummarizerMain(const void *startup_data, size_t startup_data_len);
 
 extern void GetWalSummarizerState(TimeLineID *summarized_tli,
diff --git a/src/include/replication/logicalctl.h b/src/include/replication/logicalctl.h
index 495554c532c..0bc1302f130 100644
--- a/src/include/replication/logicalctl.h
+++ b/src/include/replication/logicalctl.h
@@ -14,8 +14,6 @@
 #ifndef LOGICALCTL_H
 #define LOGICALCTL_H
 
-extern Size LogicalDecodingCtlShmemSize(void);
-extern void LogicalDecodingCtlShmemInit(void);
 extern void StartupLogicalDecodingStatus(bool last_status);
 extern void InitializeProcessXLogLogicalInfo(void);
 extern bool ProcessBarrierUpdateXLogLogicalInfo(void);
diff --git a/src/include/replication/logicallauncher.h b/src/include/replication/logicallauncher.h
index 504b710536a..5f0c1b9c682 100644
--- a/src/include/replication/logicallauncher.h
+++ b/src/include/replication/logicallauncher.h
@@ -19,9 +19,6 @@ extern PGDLLIMPORT int max_parallel_apply_workers_per_subscription;
 extern void ApplyLauncherRegister(void);
 extern void ApplyLauncherMain(Datum main_arg);
 
-extern Size ApplyLauncherShmemSize(void);
-extern void ApplyLauncherShmemInit(void);
-
 extern void ApplyLauncherForgetWorkerStartTime(Oid subid);
 
 extern void ApplyLauncherWakeupAtCommit(void);
diff --git a/src/include/replication/origin.h b/src/include/replication/origin.h
index eb46b41b4b7..a69faf6eaaf 100644
--- a/src/include/replication/origin.h
+++ b/src/include/replication/origin.h
@@ -84,8 +84,4 @@ extern void replorigin_redo(XLogReaderState *record);
 extern void replorigin_desc(StringInfo buf, XLogReaderState *record);
 extern const char *replorigin_identify(uint8 info);
 
-/* shared memory allocation */
-extern Size ReplicationOriginShmemSize(void);
-extern void ReplicationOriginShmemInit(void);
-
 #endif							/* PG_ORIGIN_H */
diff --git a/src/include/replication/slot.h b/src/include/replication/slot.h
index 4b4709f6e2c..1a3557de607 100644
--- a/src/include/replication/slot.h
+++ b/src/include/replication/slot.h
@@ -327,10 +327,6 @@ extern PGDLLIMPORT int max_replication_slots;
 extern PGDLLIMPORT char *synchronized_standby_slots;
 extern PGDLLIMPORT int idle_replication_slot_timeout_secs;
 
-/* shmem initialization functions */
-extern Size ReplicationSlotsShmemSize(void);
-extern void ReplicationSlotsShmemInit(void);
-
 /* management of individual slots */
 extern void ReplicationSlotCreate(const char *name, bool db_specific,
 								  ReplicationSlotPersistency persistency,
diff --git a/src/include/replication/slotsync.h b/src/include/replication/slotsync.h
index e546d0d050d..d2121cd3ed7 100644
--- a/src/include/replication/slotsync.h
+++ b/src/include/replication/slotsync.h
@@ -31,8 +31,6 @@ pg_noreturn extern void ReplSlotSyncWorkerMain(const void *startup_data, size_t
 extern void ShutDownSlotSync(void);
 extern bool SlotSyncWorkerCanRestart(void);
 extern bool IsSyncingReplicationSlots(void);
-extern Size SlotSyncShmemSize(void);
-extern void SlotSyncShmemInit(void);
 extern void SyncReplicationSlots(WalReceiverConn *wrconn);
 
 #endif							/* SLOTSYNC_H */
diff --git a/src/include/replication/walreceiver.h b/src/include/replication/walreceiver.h
index 85d24c87298..47c07574d4d 100644
--- a/src/include/replication/walreceiver.h
+++ b/src/include/replication/walreceiver.h
@@ -491,8 +491,6 @@ pg_noreturn extern void WalReceiverMain(const void *startup_data, size_t startup
 extern void WalRcvRequestApplyReply(void);
 
 /* prototypes for functions in walreceiverfuncs.c */
-extern Size WalRcvShmemSize(void);
-extern void WalRcvShmemInit(void);
 extern void ShutdownWalRcv(void);
 extern bool WalRcvStreaming(void);
 extern bool WalRcvRunning(void);
diff --git a/src/include/replication/walsender.h b/src/include/replication/walsender.h
index a4df3b8e0ae..8952c848d19 100644
--- a/src/include/replication/walsender.h
+++ b/src/include/replication/walsender.h
@@ -41,8 +41,6 @@ extern void WalSndErrorCleanup(void);
 extern void PhysicalWakeupLogicalWalSnd(void);
 extern XLogRecPtr GetStandbyFlushRecPtr(TimeLineID *tli);
 extern void WalSndSignals(void);
-extern Size WalSndShmemSize(void);
-extern void WalSndShmemInit(void);
 extern void WalSndWakeup(bool physical, bool logical);
 extern void WalSndInitStopping(void);
 extern void WalSndWaitStopping(void);
diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h
index 8d1e16b5d51..d7f8a8c1e63 100644
--- a/src/include/storage/buf_internals.h
+++ b/src/include/storage/buf_internals.h
@@ -570,12 +570,8 @@ extern bool StrategyRejectBuffer(BufferAccessStrategy strategy,
 extern int	StrategySyncStart(uint32 *complete_passes, uint32 *num_buf_alloc);
 extern void StrategyNotifyBgWriter(int bgwprocno);
 
-extern Size StrategyShmemSize(void);
-extern void StrategyInitialize(bool init);
-
 /* buf_table.c */
-extern Size BufTableShmemSize(int size);
-extern void InitBufTable(int size);
+extern void BufTableShmemRequest(int size);
 extern uint32 BufTableHashCode(BufferTag *tagPtr);
 extern int	BufTableLookup(BufferTag *tagPtr, uint32 hashcode);
 extern int	BufTableInsert(BufferTag *tagPtr, uint32 hashcode, int buf_id);
diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h
index 4017896f951..26441886035 100644
--- a/src/include/storage/bufmgr.h
+++ b/src/include/storage/bufmgr.h
@@ -369,10 +369,6 @@ extern void MarkDirtyAllUnpinnedBuffers(int32 *buffers_dirtied,
 										int32 *buffers_already_dirty,
 										int32 *buffers_skipped);
 
-/* in buf_init.c */
-extern void BufferManagerShmemInit(void);
-extern Size BufferManagerShmemSize(void);
-
 /* in localbuf.c */
 extern void AtProcExit_LocalBuffers(void);
 
diff --git a/src/include/storage/lock.h b/src/include/storage/lock.h
index fa68e6ecece..ee3cb1dc203 100644
--- a/src/include/storage/lock.h
+++ b/src/include/storage/lock.h
@@ -375,8 +375,6 @@ typedef enum
 /*
  * function prototypes
  */
-extern void LockManagerShmemInit(void);
-extern Size LockManagerShmemSize(void);
 extern void InitLockManagerAccess(void);
 extern LockMethod GetLocksMethodTable(const LOCK *lock);
 extern LockMethod GetLockTagsMethodTable(const LOCKTAG *locktag);
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index e8e06be30c2..206d6b586ad 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -25,10 +25,18 @@ PG_SHMEM_SUBSYSTEM(DSMRegistryShmemCallbacks)
 
 /* xlog, clog, and buffers */
 PG_SHMEM_SUBSYSTEM(VarsupShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(XLOGShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(XLogPrefetchShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(XLogRecoveryShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(CLOGShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(CommitTsShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(SUBTRANSShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(MultiXactShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(BufferManagerShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(StrategyCtlShmemCallbacks)
+
+/* lock manager */
+PG_SHMEM_SUBSYSTEM(LockManagerShmemCallbacks)
 
 /* predicate lock manager */
 PG_SHMEM_SUBSYSTEM(PredicateLockShmemCallbacks)
@@ -36,6 +44,9 @@ PG_SHMEM_SUBSYSTEM(PredicateLockShmemCallbacks)
 /* process table */
 PG_SHMEM_SUBSYSTEM(ProcGlobalShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(ProcArrayShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(BackendStatusShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(TwoPhaseShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(BackgroundWorkerShmemCallbacks)
 
 /* shared-inval messaging */
 PG_SHMEM_SUBSYSTEM(SharedInvalShmemCallbacks)
@@ -43,11 +54,26 @@ PG_SHMEM_SUBSYSTEM(SharedInvalShmemCallbacks)
 /* interprocess signaling mechanisms */
 PG_SHMEM_SUBSYSTEM(PMSignalShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(ProcSignalShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(CheckpointerShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(AutoVacuumShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(ReplicationSlotsShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(ReplicationOriginShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(WalSndShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(WalRcvShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
 
 /* other modules that need some shared memory space */
+PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(SyncScanShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(AsyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(StatsShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(WaitEventCustomShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(InjectionPointShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(WaitLSNShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(LogicalDecodingCtlShmemCallbacks)
 
 /* AIO subsystem. This delegates to the method-specific callbacks */
 PG_SHMEM_SUBSYSTEM(AioShmemCallbacks)
diff --git a/src/include/utils/backend_status.h b/src/include/utils/backend_status.h
index ddd06304e97..a334e096e4a 100644
--- a/src/include/utils/backend_status.h
+++ b/src/include/utils/backend_status.h
@@ -298,14 +298,6 @@ extern PGDLLIMPORT int pgstat_track_activity_query_size;
 extern PGDLLIMPORT PgBackendStatus *MyBEEntry;
 
 
-/* ----------
- * Functions called from postmaster
- * ----------
- */
-extern Size BackendStatusShmemSize(void);
-extern void BackendStatusShmemInit(void);
-
-
 /* ----------
  * Functions called from backends
  * ----------
diff --git a/src/test/modules/injection_points/injection_points.c b/src/test/modules/injection_points/injection_points.c
index d59c5ad0582..c9ab721c3fd 100644
--- a/src/test/modules/injection_points/injection_points.c
+++ b/src/test/modules/injection_points/injection_points.c
@@ -107,9 +107,13 @@ extern PGDLLEXPORT void injection_wait(const char *name,
 /* track if injection points attached in this process are linked to it */
 static bool injection_point_local = false;
 
-/* Shared memory init callbacks */
-static shmem_request_hook_type prev_shmem_request_hook = NULL;
-static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
+static void injection_shmem_request(void *arg);
+static void injection_shmem_init(void *arg);
+
+static const ShmemCallbacks injection_shmem_callbacks = {
+	.request_fn = injection_shmem_request,
+	.init_fn = injection_shmem_init,
+};
 
 /*
  * Routine for shared memory area initialization, used as a callback
@@ -126,44 +130,26 @@ injection_point_init_state(void *ptr, void *arg)
 	ConditionVariableInit(&state->wait_point);
 }
 
-/* Shared memory initialization when loading module */
 static void
-injection_shmem_request(void)
+injection_shmem_request(void *arg)
 {
-	Size		size;
-
-	if (prev_shmem_request_hook)
-		prev_shmem_request_hook();
+	static ShmemStructDesc InjectionPointsShmemDesc;
 
-	size = MAXALIGN(sizeof(InjectionPointSharedState));
-	RequestAddinShmemSpace(size);
+	ShmemRequestStruct(&InjectionPointsShmemDesc, &(ShmemRequestStructOpts) {
+		.name = "injection_points",
+		.size = sizeof(InjectionPointSharedState),
+		.ptr = (void **) &inj_state,
+	});
 }
 
 static void
-injection_shmem_startup(void)
+injection_shmem_init(void *arg)
 {
-	bool		found;
-
-	if (prev_shmem_startup_hook)
-		prev_shmem_startup_hook();
-
-	/* Create or attach to the shared memory state */
-	LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
-
-	inj_state = ShmemInitStruct("injection_points",
-								sizeof(InjectionPointSharedState),
-								&found);
-
-	if (!found)
-	{
-		/*
-		 * First time through, so initialize.  This is shared with the dynamic
-		 * initialization using a DSM.
-		 */
-		injection_point_init_state(inj_state, NULL);
-	}
-
-	LWLockRelease(AddinShmemInitLock);
+	/*
+	 * First time through, so initialize.  This is shared with the dynamic
+	 * initialization using a DSM.
+	 */
+	injection_point_init_state(inj_state, NULL);
 }
 
 /*
@@ -601,9 +587,5 @@ _PG_init(void)
 	if (!process_shared_preload_libraries_in_progress)
 		return;
 
-	/* Shared memory initialization */
-	prev_shmem_request_hook = shmem_request_hook;
-	shmem_request_hook = injection_shmem_request;
-	prev_shmem_startup_hook = shmem_startup_hook;
-	shmem_startup_hook = injection_shmem_startup;
+	RegisterShmemCallbacks(&injection_shmem_callbacks);
 }
-- 
2.47.3



view thread (75+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected], [email protected]
  Subject: Re: Better shared data structure management and resizable shared data structures
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox