v2-0008-Make-ShmemIndex-visible-in-the-pg_shmem_allocatio.patch

text/x-patch

Filename: v2-0008-Make-ShmemIndex-visible-in-the-pg_shmem_allocatio.patch
Type: text/x-patch
Part: 7
Message: Re: Shared hash table allocations

Patch

Format: format-patch
Series: patch v2-0008
Subject: Make ShmemIndex visible in the pg_shmem_allocations view
File+
src/backend/storage/ipc/shmem.c 22 6
From b6678b4fb04670d594880855a8bd07f0acb1b7a9 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon, 30 Mar 2026 18:21:11 +0300
Subject: [PATCH v2 8/8] Make ShmemIndex visible in the pg_shmem_allocations
 view

Reviewed-by: Tomas Vondra <tomas@vondra.me>
Discussion: https://www.postgresql.org/message-id/01ab1d41-3eda-4705-8bbd-af898f5007f1@iki.fi
---
 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