From 87063f3d4e1d632fd117746adc2bb748d8630754 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Fri, 27 Mar 2026 23:34:59 +0200
Subject: [PATCH v1 8/8] Make ShmemIndex visible in the pg_shmem_allocations
 view

---
 src/backend/storage/ipc/shmem.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index 2ee7d06e4d3..08dd7ab7a1c 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -137,6 +137,7 @@ InitShmemAllocator(PGShmemHeader *seghdr)
 	HASHCTL		info;
 	int			hash_flags;
 	shmem_hash_allocator allocator;
+	Size		size = 0;
 
 #ifndef EXEC_BACKEND
 	Assert(!IsUnderPostmaster);
@@ -195,16 +196,14 @@ InitShmemAllocator(PGShmemHeader *seghdr)
 
 	if (!IsUnderPostmaster)
 	{
-		size_t		size = hash_estimate_size(SHMEM_INDEX_SIZE, info.entrysize);
-		char	   *location = ShmemAlloc(size);
+		size = hash_estimate_size(SHMEM_INDEX_SIZE, info.entrysize);
+		ShmemAllocator->index = (HASHHDR *) ShmemAlloc(size);
 
-		allocator.next = location;
-		allocator.end = location + size;
+		allocator.next = (char *) ShmemAllocator->index;
+		allocator.end = (char *) ShmemAllocator->index + size;
 		info.alloc_arg = &allocator;
-
 		info.hctl = NULL;
 		hash_flags |= HASH_ALLOC | HASH_FIXED_SIZE;
-		ShmemAllocator->index = (HASHHDR *) location;
 	}
 	else
 	{
@@ -213,6 +212,23 @@ InitShmemAllocator(PGShmemHeader *seghdr)
 	}
 	ShmemIndex = hash_create("ShmemIndex", SHMEM_INDEX_SIZE, &info, hash_flags);
 	Assert(ShmemIndex != NULL);
+
+	/*
+	 * Add an entry for the ShmemIndex itself into ShmemIndex, so that it's
+	 * visible in the pg_shmem_allocations view
+	 */
+	if (!IsUnderPostmaster)
+	{
+		ShmemIndexEnt *result;
+		bool		found;
+
+		result = (ShmemIndexEnt *)
+			hash_search(ShmemIndex, "ShmemIndex", HASH_ENTER, &found);
+		Assert(!found);
+		result->size = size;
+		result->allocated_size = size;
+		result->location = ShmemAllocator->index;
+	}
 }
 
 /*
-- 
2.47.3

