From 422a6106f6aed5fa36a39a00c7be8fe447f08133 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Sat, 28 Mar 2026 01:46:46 +0200
Subject: [PATCH v9 10/16] Convert wait_event.c to new interface

---
 src/backend/storage/ipc/ipci.c          |  2 -
 src/backend/utils/activity/wait_event.c | 96 ++++++++++++-------------
 src/include/storage/subsystemlist.h     |  1 +
 src/include/utils/wait_event.h          |  2 -
 4 files changed, 48 insertions(+), 53 deletions(-)

diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 2ae4b9c2ac2..b90717c1f58 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -137,7 +137,6 @@ CalculateShmemSize(void)
 	size = add_size(size, SyncScanShmemSize());
 	size = add_size(size, AsyncShmemSize());
 	size = add_size(size, StatsShmemSize());
-	size = add_size(size, WaitEventCustomShmemSize());
 	size = add_size(size, SlotSyncShmemSize());
 	size = add_size(size, AioShmemSize());
 	size = add_size(size, WaitLSNShmemSize());
@@ -350,7 +349,6 @@ CreateOrAttachShmemStructs(void)
 	SyncScanShmemInit();
 	AsyncShmemInit();
 	StatsShmemInit();
-	WaitEventCustomShmemInit();
 	AioShmemInit();
 	WaitLSNShmemInit();
 	LogicalDecodingCtlShmemInit();
diff --git a/src/backend/utils/activity/wait_event.c b/src/backend/utils/activity/wait_event.c
index e5a2289f0b0..08af61ff448 100644
--- a/src/backend/utils/activity/wait_event.c
+++ b/src/backend/utils/activity/wait_event.c
@@ -25,6 +25,7 @@
 #include "storage/lmgr.h"
 #include "storage/lwlock.h"
 #include "storage/shmem.h"
+#include "storage/subsystems.h"
 #include "storage/spin.h"
 #include "utils/wait_event.h"
 
@@ -97,61 +98,58 @@ static WaitEventCustomCounterData *WaitEventCustomCounter;
 static uint32 WaitEventCustomNew(uint32 classId, const char *wait_event_name);
 static const char *GetWaitEventCustomIdentifier(uint32 wait_event_info);
 
+static void WaitEventCustomShmemRequest(void *arg);
+static void WaitEventCustomShmemInit(void *arg);
+
+const ShmemCallbacks WaitEventCustomShmemCallbacks = {
+	.request_fn = WaitEventCustomShmemRequest,
+	.init_fn = WaitEventCustomShmemInit,
+};
+
 /*
- *  Return the space for dynamic shared hash tables and dynamic allocation counter.
+ * Register shmem space for dynamic shared hash and dynamic allocation counter.
  */
-Size
-WaitEventCustomShmemSize(void)
+static void
+WaitEventCustomShmemRequest(void *arg)
 {
-	Size		sz;
-
-	sz = MAXALIGN(sizeof(WaitEventCustomCounterData));
-	sz = add_size(sz, hash_estimate_size(WAIT_EVENT_CUSTOM_HASH_MAX_SIZE,
-										 sizeof(WaitEventCustomEntryByInfo)));
-	sz = add_size(sz, hash_estimate_size(WAIT_EVENT_CUSTOM_HASH_MAX_SIZE,
-										 sizeof(WaitEventCustomEntryByName)));
-	return sz;
+	static ShmemStructDesc WaitEventCustomCounterShmemDesc;
+	static ShmemHashDesc WaitEventCustomHashByInfoDesc;
+	static ShmemHashDesc WaitEventCustomHashByNameDesc;
+
+	ShmemRequestStruct(&WaitEventCustomCounterShmemDesc,
+					   .name = "WaitEventCustomCounterData",
+					   .size = sizeof(WaitEventCustomCounterData),
+					   .ptr = (void **) &WaitEventCustomCounter,
+		);
+	ShmemRequestHash(&WaitEventCustomHashByInfoDesc,
+					 .name = "WaitEventCustom hash by wait event information",
+					 .ptr = &WaitEventCustomHashByInfo,
+
+					 .init_size = WAIT_EVENT_CUSTOM_HASH_INIT_SIZE,
+					 .max_size = WAIT_EVENT_CUSTOM_HASH_MAX_SIZE,
+					 .hash_info.keysize = sizeof(uint32),
+					 .hash_info.entrysize = sizeof(WaitEventCustomEntryByInfo),
+					 .hash_flags = HASH_ELEM | HASH_BLOBS,
+		);
+	ShmemRequestHash(&WaitEventCustomHashByNameDesc,
+					 .name = "WaitEventCustom hash by name",
+					 .ptr = &WaitEventCustomHashByName,
+
+					 .init_size = WAIT_EVENT_CUSTOM_HASH_INIT_SIZE,
+					 .max_size = WAIT_EVENT_CUSTOM_HASH_MAX_SIZE,
+	/* key is a NULL-terminated string */
+					 .hash_info.keysize = sizeof(char[NAMEDATALEN]),
+					 .hash_info.entrysize = sizeof(WaitEventCustomEntryByName),
+					 .hash_flags = HASH_ELEM | HASH_STRINGS,
+		);
 }
 
-/*
- * Allocate shmem space for dynamic shared hash and dynamic allocation counter.
- */
-void
-WaitEventCustomShmemInit(void)
+static void
+WaitEventCustomShmemInit(void *arg)
 {
-	bool		found;
-	HASHCTL		info;
-
-	WaitEventCustomCounter = (WaitEventCustomCounterData *)
-		ShmemInitStruct("WaitEventCustomCounterData",
-						sizeof(WaitEventCustomCounterData), &found);
-
-	if (!found)
-	{
-		/* initialize the allocation counter and its spinlock. */
-		WaitEventCustomCounter->nextId = WAIT_EVENT_CUSTOM_INITIAL_ID;
-		SpinLockInit(&WaitEventCustomCounter->mutex);
-	}
-
-	/* initialize or attach the hash tables to store custom wait events */
-	info.keysize = sizeof(uint32);
-	info.entrysize = sizeof(WaitEventCustomEntryByInfo);
-	WaitEventCustomHashByInfo =
-		ShmemInitHash("WaitEventCustom hash by wait event information",
-					  WAIT_EVENT_CUSTOM_HASH_INIT_SIZE,
-					  WAIT_EVENT_CUSTOM_HASH_MAX_SIZE,
-					  &info,
-					  HASH_ELEM | HASH_BLOBS);
-
-	/* key is a NULL-terminated string */
-	info.keysize = sizeof(char[NAMEDATALEN]);
-	info.entrysize = sizeof(WaitEventCustomEntryByName);
-	WaitEventCustomHashByName =
-		ShmemInitHash("WaitEventCustom hash by name",
-					  WAIT_EVENT_CUSTOM_HASH_INIT_SIZE,
-					  WAIT_EVENT_CUSTOM_HASH_MAX_SIZE,
-					  &info,
-					  HASH_ELEM | HASH_STRINGS);
+	/* initialize the allocation counter and its spinlock. */
+	WaitEventCustomCounter->nextId = WAIT_EVENT_CUSTOM_INITIAL_ID;
+	SpinLockInit(&WaitEventCustomCounter->mutex);
 }
 
 /*
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 65da6f17c5d..7dfbd03d6e5 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -20,4 +20,5 @@
  * of these matter.
  */
 
+PG_SHMEM_SUBSYSTEM(WaitEventCustomShmemCallbacks)
 PG_SHMEM_SUBSYSTEM(InjectionPointShmemCallbacks)
diff --git a/src/include/utils/wait_event.h b/src/include/utils/wait_event.h
index 34c27cc3dc3..86ee348220d 100644
--- a/src/include/utils/wait_event.h
+++ b/src/include/utils/wait_event.h
@@ -42,8 +42,6 @@ extern PGDLLIMPORT uint32 *my_wait_event_info;
 extern uint32 WaitEventExtensionNew(const char *wait_event_name);
 extern uint32 WaitEventInjectionPointNew(const char *wait_event_name);
 
-extern void WaitEventCustomShmemInit(void);
-extern Size WaitEventCustomShmemSize(void);
 extern char **GetWaitEventCustomNames(uint32 classId, int *nwaitevents);
 
 /* ----------
-- 
2.47.3

