From 671ac63b3f443b22b4e268c7f6d1f48ee15e99df Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Mon, 6 Apr 2026 15:16:05 -0400
Subject: [PATCH v10 1/2] Assert no duplicate keys in shm_toc_insert()

shm_toc_insert() accepts duplicate keys, and a duplicate would go
undetected. Add an assertion to catch this. Keys are defined throughout
the executor, so it is useful of having a way to detect collisions.
---
 src/backend/storage/ipc/shm_toc.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/backend/storage/ipc/shm_toc.c b/src/backend/storage/ipc/shm_toc.c
index e74a0b97ac0..f3bbb24bfdd 100644
--- a/src/backend/storage/ipc/shm_toc.c
+++ b/src/backend/storage/ipc/shm_toc.c
@@ -201,6 +201,13 @@ shm_toc_insert(shm_toc *toc, uint64 key, void *address)
 	}
 
 	Assert(offset < total_bytes);
+
+#ifdef USE_ASSERT_CHECKING
+	/* Verify no duplicate keys */
+	for (Size i = 0; i < nentry; i++)
+		Assert(vtoc->toc_entry[i].key != key);
+#endif
+
 	vtoc->toc_entry[nentry].key = key;
 	vtoc->toc_entry[nentry].offset = offset;
 
-- 
2.43.0

