From 2c8a2f6f2a45c6a736f2f5ad494e74acf05180aa Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon, 30 Mar 2026 18:20:52 +0300
Subject: [PATCH v2 1/8] Use ShmemInitStruct to allocate shmem for semaphores

This makes them visible visible in pg_shmem_allocations

Reviewed-by: Tomas Vondra <tomas@vondra.me>
Discussion: https://www.postgresql.org/message-id/01ab1d41-3eda-4705-8bbd-af898f5007f1@iki.fi
---
 src/backend/port/posix_sema.c | 4 +++-
 src/backend/port/sysv_sema.c  | 5 ++++-
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/backend/port/posix_sema.c b/src/backend/port/posix_sema.c
index e368e5ee7ed..40205b7d400 100644
--- a/src/backend/port/posix_sema.c
+++ b/src/backend/port/posix_sema.c
@@ -196,6 +196,7 @@ void
 PGReserveSemaphores(int maxSemas)
 {
 	struct stat statbuf;
+	bool		found;
 
 	/*
 	 * We use the data directory's inode number to seed the search for free
@@ -216,7 +217,8 @@ PGReserveSemaphores(int maxSemas)
 #else
 
 	sharedSemas = (PGSemaphore)
-		ShmemAlloc(PGSemaphoreShmemSize(maxSemas));
+		ShmemInitStruct("Semaphores", PGSemaphoreShmemSize(maxSemas), &found);
+	Assert(!found);
 #endif
 
 	numSems = 0;
diff --git a/src/backend/port/sysv_sema.c b/src/backend/port/sysv_sema.c
index 86c4d359ef7..4b2bf84072f 100644
--- a/src/backend/port/sysv_sema.c
+++ b/src/backend/port/sysv_sema.c
@@ -330,6 +330,7 @@ void
 PGReserveSemaphores(int maxSemas)
 {
 	struct stat statbuf;
+	bool		found;
 
 	/*
 	 * We use the data directory's inode number to seed the search for free
@@ -344,7 +345,9 @@ PGReserveSemaphores(int maxSemas)
 						DataDir)));
 
 	sharedSemas = (PGSemaphore)
-		ShmemAlloc(PGSemaphoreShmemSize(maxSemas));
+		ShmemInitStruct("Semaphores", PGSemaphoreShmemSize(maxSemas), &found);
+	Assert(!found);
+
 	numSharedSemas = 0;
 	maxSharedSemas = maxSemas;
 
-- 
2.47.3

