From e5866917ec0236d6a74710c8762fecf54dbee52c Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: Thu, 29 Jan 2026 11:16:45 +0530
Subject: [PATCH v20260129 1/2] Clarify Shmem access by Postmaster and
 standalone backends

InitShmemAccess is called by a standbalone backend or the Postmaster. It is also
called by other backends in EXEC_BACKEND case. InitShmemAllocation() is called
only by the Postmaster. Add Asserts to document this fact.

The code which creates ShmemIndex is already called only for the Postmaster or a
standalone backend and is adequately documented. Rest of the code may be called
by any backend.

Author: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
---
 src/backend/storage/ipc/shmem.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index 1b536363152..b997a043145 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -99,10 +99,19 @@ Datum		pg_numa_available(PG_FUNCTION_ARGS);
 
 /*
  *	InitShmemAccess() --- set up basic pointers to shared memory.
+ *
+ * In EXEC_BACKEND case, this function is called by the Postmaster as well as
+ * every backend to setup the shared memory access. In non-EXEC_BACKEND case, it
+ * is called only by a standalone backend or the Postmaster, who passes the
+ * setup to the child backends via fork().
  */
 void
 InitShmemAccess(PGShmemHeader *seghdr)
 {
+#ifndef EXEC_BACKEND
+	Assert(!IsUnderPostmaster);
+#endif
+
 	ShmemSegHdr = seghdr;
 	ShmemBase = seghdr;
 	ShmemEnd = (char *) ShmemBase + seghdr->totalsize;
@@ -119,6 +128,7 @@ InitShmemAllocation(void)
 	PGShmemHeader *shmhdr = ShmemSegHdr;
 	char	   *aligned;
 
+	Assert(!IsUnderPostmaster);
 	Assert(shmhdr != NULL);
 
 	/*

base-commit: 20a8f783e15c503125c2ed0ae890510879f62801
-- 
2.34.1

