From a0edb157f68b0c2fb443c0a75150fab69cd7db7b Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: Mon, 2 Nov 2026 15:00:10 +0530
Subject: [PATCH v20261102 2/2] Introduce SRS_SIZED

... as mentioned in the commit message of previous commit.
---
 src/backend/storage/ipc/ipci.c  |  4 ----
 src/backend/storage/ipc/shmem.c | 13 +++++++++++--
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 64e16aeaa69..1c4cd27d2be 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -52,10 +52,6 @@ RequestAddinShmemSpace(Size size)
 /*
  * CalculateShmemSize
  *		Calculates the amount of shared memory needed.
- *
- * Expected to be called only once after collecting all shared memory requests
- * but before allocating the shared memory segment at the startup or server
- * restart.
  */
 Size
 CalculateShmemSize(void)
diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index 71bf22d2438..1eb70cf58d3 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -181,7 +181,7 @@ static List *pending_shmem_requests;	/* List of ShmemRequests */
  * right order.
  *
  * Postmaster:
- *   INITIAL -> REQUESTING -> INITIALIZING -> DONE
+ *   INITIAL -> REQUESTING -> SIZED -> INITIALIZING -> DONE
  *
  * Backends in EXEC_BACKEND mode:
  *   INITIAL -> REQUESTING -> ATTACHING -> DONE
@@ -201,6 +201,13 @@ enum shmem_request_state
 	 */
 	SRS_REQUESTING,
 
+	/*
+	 * The required size of the shared memory segment has been calculated. No
+	 * further requests may be registered before creating the shared memory
+	 * segment.
+	 */
+	SRS_SIZED,
+
 	/*
 	 * Postmaster has finished all shmem requests, and is now initializing the
 	 * shared memory segment.  init_fn callbacks are called in this state.
@@ -420,6 +427,8 @@ ShmemGetRequestedSize(void)
 		size = add_size(size, request->options->size);
 	}
 
+	shmem_request_state = SRS_SIZED;
+
 	return size;
 }
 
@@ -695,7 +704,7 @@ InitShmemAllocator(PGShmemHeader *seghdr)
 	}
 	else
 	{
-		Assert(shmem_request_state == SRS_REQUESTING);
+		Assert(shmem_request_state == SRS_SIZED);
 		shmem_request_state = SRS_INITIALIZING;
 	}
 
-- 
2.34.1

