From 839ce611611d9e212370de533c930a13bc46c439 Mon Sep 17 00:00:00 2001
From: David Geier <geidav.pg@gmail.com>
Date: Fri, 4 Dec 2026 08:07:58 +0100
Subject: [PATCH v1 5/6] Optimize hash_numeric() / hash_numeric_extended()

---
 src/backend/utils/adt/numeric.c | 66 ++++++++++++++++-----------------
 1 file changed, 32 insertions(+), 34 deletions(-)

diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index ae6b925cc51..9c017453961 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -2714,21 +2714,22 @@ in_range_numeric_numeric(PG_FUNCTION_ARGS)
 Datum
 hash_numeric(PG_FUNCTION_ARGS)
 {
-	Numeric		key = PG_GETARG_NUMERIC(0);
+	Numeric		key = PG_GETARG_NUMERIC_PACKED(0);
+	NumericVar	var;
 	uint32		digit_hash;
 	uint32		result;
-	int			weight;
 	int			start_offset;
 	int			end_offset;
 	int			i;
 	int			hash_len;
-	NumericDigit *digits;
+
+	init_var_from_num(key, &var);
 
 	/* If it's NaN or infinity, don't try to hash the rest of the fields */
-	if (NUMERIC_IS_SPECIAL(key))
+	if (var.sign == NUMERIC_NAN || var.sign == NUMERIC_PINF ||
+		var.sign == NUMERIC_NINF)
 		PG_RETURN_UINT32(0);
 
-	weight = NUMERIC_WEIGHT(key);
 	start_offset = 0;
 	end_offset = 0;
 
@@ -2738,10 +2739,9 @@ hash_numeric(PG_FUNCTION_ARGS)
 	 * zeros are suppressed, but we're paranoid. Note that we measure the
 	 * starting and ending offsets in units of NumericDigits, not bytes.
 	 */
-	digits = NUMERIC_DIGITS(key);
-	for (i = 0; i < NUMERIC_NDIGITS(key); i++)
+	for (i = 0; i < var.ndigits; i++)
 	{
-		if (digits[i] != (NumericDigit) 0)
+		if (read_digit(var.digits, i) != (NumericDigit) 0)
 			break;
 
 		start_offset++;
@@ -2750,26 +2750,26 @@ hash_numeric(PG_FUNCTION_ARGS)
 		 * The weight is effectively the # of digits before the decimal point,
 		 * so decrement it for each leading zero we skip.
 		 */
-		weight--;
+		var.weight--;
 	}
 
 	/*
 	 * If there are no non-zero digits, then the value of the number is zero,
 	 * regardless of any other fields.
 	 */
-	if (NUMERIC_NDIGITS(key) == start_offset)
+	if (var.ndigits == start_offset)
 		PG_RETURN_UINT32(-1);
 
-	for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
+	for (i = var.ndigits - 1; i >= 0; i--)
 	{
-		if (digits[i] != (NumericDigit) 0)
+		if (read_digit(var.digits, i) != (NumericDigit) 0)
 			break;
 
 		end_offset++;
 	}
 
 	/* If we get here, there should be at least one non-zero digit */
-	Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
+	Assert(start_offset + end_offset < var.ndigits);
 
 	/*
 	 * Note that we don't hash on the Numeric's scale, since two numerics can
@@ -2777,13 +2777,12 @@ hash_numeric(PG_FUNCTION_ARGS)
 	 * sign, although we could: since a sign difference implies inequality,
 	 * this shouldn't affect correctness.
 	 */
-	hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
-	digit_hash = hash_bytes((unsigned char *) (NUMERIC_DIGITS(key)
-											   + start_offset),
+	hash_len = var.ndigits - start_offset - end_offset;
+	digit_hash = hash_bytes((unsigned char *) (var.digits + start_offset),
 							hash_len * sizeof(NumericDigit));
 
 	/* Mix in the weight, via XOR */
-	result = digit_hash ^ weight;
+	result = digit_hash ^ var.weight;
 
 	PG_RETURN_UINT32(result);
 }
@@ -2795,56 +2794,55 @@ hash_numeric(PG_FUNCTION_ARGS)
 Datum
 hash_numeric_extended(PG_FUNCTION_ARGS)
 {
-	Numeric		key = PG_GETARG_NUMERIC(0);
+	Numeric		key = PG_GETARG_NUMERIC_PACKED(0);
+	NumericVar	var;
 	uint64		seed = PG_GETARG_INT64(1);
 	uint64		digit_hash;
 	uint64		result;
-	int			weight;
 	int			start_offset;
 	int			end_offset;
 	int			i;
 	int			hash_len;
-	NumericDigit *digits;
+
+	init_var_from_num(key, &var);
 
 	/* If it's NaN or infinity, don't try to hash the rest of the fields */
-	if (NUMERIC_IS_SPECIAL(key))
+	if (var.sign == NUMERIC_NAN || var.sign == NUMERIC_PINF ||
+		var.sign == NUMERIC_NINF)
 		PG_RETURN_UINT64(seed);
 
-	weight = NUMERIC_WEIGHT(key);
 	start_offset = 0;
 	end_offset = 0;
 
-	digits = NUMERIC_DIGITS(key);
-	for (i = 0; i < NUMERIC_NDIGITS(key); i++)
+	for (i = 0; i < var.ndigits; i++)
 	{
-		if (digits[i] != (NumericDigit) 0)
+		if (read_digit(var.digits, i) != (NumericDigit) 0)
 			break;
 
 		start_offset++;
 
-		weight--;
+		var.weight--;
 	}
 
-	if (NUMERIC_NDIGITS(key) == start_offset)
+	if (var.ndigits == start_offset)
 		PG_RETURN_UINT64(seed - 1);
 
-	for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
+	for (i = var.ndigits - 1; i >= 0; i--)
 	{
-		if (digits[i] != (NumericDigit) 0)
+		if (read_digit(var.digits, i) != (NumericDigit) 0)
 			break;
 
 		end_offset++;
 	}
 
-	Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
+	Assert(start_offset + end_offset < var.ndigits);
 
-	hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
-	digit_hash = hash_bytes_extended((unsigned char *) (NUMERIC_DIGITS(key)
-														+ start_offset),
+	hash_len = var.ndigits - start_offset - end_offset;
+	digit_hash = hash_bytes_extended((unsigned char *) (var.digits + start_offset),
 									 hash_len * sizeof(NumericDigit),
 									 seed);
 
-	result = digit_hash ^ weight;
+	result = digit_hash ^ var.weight;
 
 	PG_RETURN_UINT64(result);
 }
-- 
2.53.0

