From 3085fb075872be955450ce52f2102b163cda44f0 Mon Sep 17 00:00:00 2001
From: shihao zhong <zhong950419@gmail.com>
Date: Mon, 9 Nov 2026 22:53:01 -0500
Subject: [PATCH 1/2] Make hashname() honor nondeterministic collations

Names that are equal under a nondeterministic collation could hash
differently, giving wrong query results.
---
 src/backend/access/hash/hashfunc.c | 37 ++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c
index 97c2c5a6a4c..a1072391560 100644
--- a/src/backend/access/hash/hashfunc.c
+++ b/src/backend/access/hash/hashfunc.c
@@ -26,6 +26,7 @@
 
 #include "postgres.h"
 
+#include "catalog/pg_collation.h"
 #include "common/hashfn.h"
 #include "utils/builtins.h"
 #include "utils/float.h"
@@ -249,10 +250,33 @@ hashoidvectorextended(PG_FUNCTION_ARGS)
 							 PG_GETARG_INT64(1));
 }
 
+/*
+ * Is collid a nondeterministic collation?  nameeq() honors those, so name
+ * values must then be hashed the way hashtext() does it.
+ */
+static bool
+name_needs_text_hash(Oid collid)
+{
+	return OidIsValid(collid) && collid != C_COLLATION_OID &&
+		!pg_newlocale_from_collation(collid)->deterministic;
+}
+
 Datum
 hashname(PG_FUNCTION_ARGS)
 {
 	char	   *key = NameStr(*PG_GETARG_NAME(0));
+	Oid			collid = PG_GET_COLLATION();
+
+	if (name_needs_text_hash(collid))
+	{
+		text	   *t = cstring_to_text(key);
+		Datum		result;
+
+		result = DirectFunctionCall1Coll(hashtext, collid,
+										 PointerGetDatum(t));
+		pfree(t);
+		return result;
+	}
 
 	return hash_any((unsigned char *) key, strlen(key));
 }
@@ -261,6 +285,19 @@ Datum
 hashnameextended(PG_FUNCTION_ARGS)
 {
 	char	   *key = NameStr(*PG_GETARG_NAME(0));
+	Oid			collid = PG_GET_COLLATION();
+
+	if (name_needs_text_hash(collid))
+	{
+		text	   *t = cstring_to_text(key);
+		Datum		result;
+
+		result = DirectFunctionCall2Coll(hashtextextended, collid,
+										 PointerGetDatum(t),
+										 PG_GETARG_DATUM(1));
+		pfree(t);
+		return result;
+	}
 
 	return hash_any_extended((unsigned char *) key, strlen(key),
 							 PG_GETARG_INT64(1));
-- 
2.37.1 (Apple Git-137.1)

