From 4e44685467e6ecd40bb558bd6fa1663d7f3c566b Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Sat, 4 Jul 2026 15:29:58 +0200
Subject: [PATCH v1 3/6] Test the hash_make and foreach_hash macros in a C++
 extension

The string-vs-blob key detection of hash_make uses _Generic in C and
needs a separate std::is_same based implementation in C++. Exercise the
macros from test_cplusplusext, including StaticAssert checks on the key
detection, so that a divergence between the two is caught at build time.
---
 .../test_cplusplusext/test_cplusplusext.cpp   | 48 +++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/src/test/modules/test_cplusplusext/test_cplusplusext.cpp b/src/test/modules/test_cplusplusext/test_cplusplusext.cpp
index 93cd7dd07f7..f9192228443 100644
--- a/src/test/modules/test_cplusplusext/test_cplusplusext.cpp
+++ b/src/test/modules/test_cplusplusext/test_cplusplusext.cpp
@@ -19,6 +19,7 @@ extern "C" {
 #include "fmgr.h"
 #include "nodes/pg_list.h"
 #include "nodes/primnodes.h"
+#include "utils/hsearch.h"
 
 PG_MODULE_MAGIC;
 
@@ -58,6 +59,53 @@ test_cplusplus_add(PG_FUNCTION_ARGS)
 	pfree(node);
 	pfree(copy);
 
+	/* Test hash macros compile under C++ */
+	{
+		typedef struct
+		{
+			Oid			oid;
+			int			data;
+		} OidEntry;
+
+		typedef struct
+		{
+			char		name[NAMEDATALEN];
+			int			value;
+		} NameEntry;
+
+		HTAB	   *htab;
+
+		StaticAssertStmt(!HASH_KEY_AS_STRING(OidEntry, oid),
+						 "Oid key should use HASH_BLOBS");
+		StaticAssertStmt(!HASH_TYPE_AS_STRING(Oid),
+						 "Oid hashset should use HASH_BLOBS");
+		StaticAssertStmt(HASH_KEY_AS_STRING(NameEntry, name),
+						 "char[] key should use HASH_STRINGS");
+		StaticAssertStmt(HASH_TYPE_AS_STRING(NameData),
+						 "NameData should use HASH_STRINGS");
+
+		htab = hash_make(OidEntry, oid, "C++ oid hash", 8);
+		foreach_hash(OidEntry, myOidEntry, htab)
+		{
+			(void) myOidEntry;
+		}
+		hash_destroy(htab);
+
+		htab = hash_make(NameEntry, name, "C++ name hash", 8);
+		hash_destroy(htab);
+
+		htab = hash_make(OidEntry, oid, "C++ cxt hash", 8,
+						 .mcxt = CurrentMemoryContext);
+		hash_destroy(htab);
+
+		htab = hash_make(OidEntry, oid, "C++ fn hash", 8,
+						 .hash = NULL, .match = NULL);
+		hash_destroy(htab);
+
+		htab = hashset_make(Oid, "C++ oid hashset", 8);
+		hash_destroy(htab);
+	}
+
 	switch (a)
 	{
 		case 1:
-- 
2.56.1

