From 787a0a969e643d14ec1cb911277b44d4c7f97cd1 Mon Sep 17 00:00:00 2001
From: David Geier <geidav.pg@gmail.com>
Date: Mon, 30 Nov 2026 09:45:28 +0100
Subject: [PATCH v1 2/6] Optimize cmp_numerics()

---
 src/backend/utils/adt/numeric.c | 93 ++++++++++++++++++---------------
 src/include/utils/numeric.h     | 13 +++--
 2 files changed, 61 insertions(+), 45 deletions(-)

diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 7bb6dc1e31c..5874620ecb7 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -467,6 +467,15 @@ static const int round_powers[4] = {0, 1000, 100, 10};
  * ----------
  */
 
+pg_always_inline
+static NumericDigit
+read_digit(const NumericDigit *digits, size_t index)
+{
+	NumericDigit digit;
+	memcpy(&digit, digits + index, sizeof(digit));
+	return digit;
+}
+
 #ifdef NUMERIC_DEBUG
 static void dump_numeric(const char *str, Numeric num);
 static void dump_var(const char *str, NumericVar *var);
@@ -2299,16 +2308,12 @@ numeric_abbrev_abort(int memtupcount, SortSupport ssup)
  * the fmgr call.  The saving here is small given how slow numeric comparisons
  * are, but it is a required part of the sort support API when abbreviations
  * are performed.
- *
- * Two palloc/pfree cycles could be saved here by using persistent buffers for
- * aligning short-varlena inputs, but this has not so far been considered to
- * be worth the effort.
  */
 static int
 numeric_fast_cmp(Datum x, Datum y, SortSupport ssup)
 {
-	Numeric		nx = DatumGetNumeric(x);
-	Numeric		ny = DatumGetNumeric(y);
+	Numeric		nx = DatumGetNumericPacked(x);
+	Numeric		ny = DatumGetNumericPacked(y);
 	int			result;
 
 	result = cmp_numerics(nx, ny);
@@ -2440,8 +2445,8 @@ numeric_abbrev_convert_var(const NumericVar *var, NumericSortSupport *nss)
 Datum
 numeric_cmp(PG_FUNCTION_ARGS)
 {
-	Numeric		num1 = PG_GETARG_NUMERIC(0);
-	Numeric		num2 = PG_GETARG_NUMERIC(1);
+	Numeric		num1 = PG_GETARG_NUMERIC_PACKED(0);
+	Numeric		num2 = PG_GETARG_NUMERIC_PACKED(1);
 	int			result;
 
 	result = cmp_numerics(num1, num2);
@@ -2456,8 +2461,8 @@ numeric_cmp(PG_FUNCTION_ARGS)
 Datum
 numeric_eq(PG_FUNCTION_ARGS)
 {
-	Numeric		num1 = PG_GETARG_NUMERIC(0);
-	Numeric		num2 = PG_GETARG_NUMERIC(1);
+	Numeric		num1 = PG_GETARG_NUMERIC_PACKED(0);
+	Numeric		num2 = PG_GETARG_NUMERIC_PACKED(1);
 	bool		result;
 
 	result = cmp_numerics(num1, num2) == 0;
@@ -2471,8 +2476,8 @@ numeric_eq(PG_FUNCTION_ARGS)
 Datum
 numeric_ne(PG_FUNCTION_ARGS)
 {
-	Numeric		num1 = PG_GETARG_NUMERIC(0);
-	Numeric		num2 = PG_GETARG_NUMERIC(1);
+	Numeric		num1 = PG_GETARG_NUMERIC_PACKED(0);
+	Numeric		num2 = PG_GETARG_NUMERIC_PACKED(1);
 	bool		result;
 
 	result = cmp_numerics(num1, num2) != 0;
@@ -2486,8 +2491,8 @@ numeric_ne(PG_FUNCTION_ARGS)
 Datum
 numeric_gt(PG_FUNCTION_ARGS)
 {
-	Numeric		num1 = PG_GETARG_NUMERIC(0);
-	Numeric		num2 = PG_GETARG_NUMERIC(1);
+	Numeric		num1 = PG_GETARG_NUMERIC_PACKED(0);
+	Numeric		num2 = PG_GETARG_NUMERIC_PACKED(1);
 	bool		result;
 
 	result = cmp_numerics(num1, num2) > 0;
@@ -2501,8 +2506,8 @@ numeric_gt(PG_FUNCTION_ARGS)
 Datum
 numeric_ge(PG_FUNCTION_ARGS)
 {
-	Numeric		num1 = PG_GETARG_NUMERIC(0);
-	Numeric		num2 = PG_GETARG_NUMERIC(1);
+	Numeric		num1 = PG_GETARG_NUMERIC_PACKED(0);
+	Numeric		num2 = PG_GETARG_NUMERIC_PACKED(1);
 	bool		result;
 
 	result = cmp_numerics(num1, num2) >= 0;
@@ -2516,8 +2521,8 @@ numeric_ge(PG_FUNCTION_ARGS)
 Datum
 numeric_lt(PG_FUNCTION_ARGS)
 {
-	Numeric		num1 = PG_GETARG_NUMERIC(0);
-	Numeric		num2 = PG_GETARG_NUMERIC(1);
+	Numeric		num1 = PG_GETARG_NUMERIC_PACKED(0);
+	Numeric		num2 = PG_GETARG_NUMERIC_PACKED(1);
 	bool		result;
 
 	result = cmp_numerics(num1, num2) < 0;
@@ -2531,8 +2536,8 @@ numeric_lt(PG_FUNCTION_ARGS)
 Datum
 numeric_le(PG_FUNCTION_ARGS)
 {
-	Numeric		num1 = PG_GETARG_NUMERIC(0);
-	Numeric		num2 = PG_GETARG_NUMERIC(1);
+	Numeric		num1 = PG_GETARG_NUMERIC_PACKED(0);
+	Numeric		num2 = PG_GETARG_NUMERIC_PACKED(1);
 	bool		result;
 
 	result = cmp_numerics(num1, num2) <= 0;
@@ -2547,51 +2552,55 @@ static int
 cmp_numerics(Numeric num1, Numeric num2)
 {
 	int			result;
+	NumericVar	var1, var2;
+
+	init_var_from_num(num1, &var1);
+	init_var_from_num(num2, &var2);
 
 	/*
 	 * We consider all NANs to be equal and larger than any non-NAN (including
 	 * Infinity).  This is somewhat arbitrary; the important thing is to have
 	 * a consistent sort order.
 	 */
-	if (NUMERIC_IS_SPECIAL(num1))
+	if (var1.sign == NUMERIC_PINF || var1.sign == NUMERIC_NINF || var1.sign == NUMERIC_NAN)
 	{
-		if (NUMERIC_IS_NAN(num1))
+		if (var1.sign == NUMERIC_NAN)
 		{
-			if (NUMERIC_IS_NAN(num2))
+			if (var2.sign == NUMERIC_NAN)
 				result = 0;		/* NAN = NAN */
 			else
 				result = 1;		/* NAN > non-NAN */
 		}
-		else if (NUMERIC_IS_PINF(num1))
+		else if (var1.sign == NUMERIC_PINF)
 		{
-			if (NUMERIC_IS_NAN(num2))
+			if (var2.sign == NUMERIC_NAN)
 				result = -1;	/* PINF < NAN */
-			else if (NUMERIC_IS_PINF(num2))
+			else if (var2.sign == NUMERIC_PINF)
 				result = 0;		/* PINF = PINF */
 			else
 				result = 1;		/* PINF > anything else */
 		}
 		else					/* num1 must be NINF */
 		{
-			if (NUMERIC_IS_NINF(num2))
+			if (var2.sign == NUMERIC_NINF)
 				result = 0;		/* NINF = NINF */
 			else
 				result = -1;	/* NINF < anything else */
 		}
 	}
-	else if (NUMERIC_IS_SPECIAL(num2))
+	else if (var2.sign == NUMERIC_PINF || var2.sign == NUMERIC_NINF || var2.sign == NUMERIC_NAN)
 	{
-		if (NUMERIC_IS_NINF(num2))
+		if (var2.sign == NUMERIC_NINF)
 			result = 1;			/* normal > NINF */
 		else
 			result = -1;		/* normal < NAN or PINF */
 	}
 	else
 	{
-		result = cmp_var_common(NUMERIC_DIGITS(num1), NUMERIC_NDIGITS(num1),
-								NUMERIC_WEIGHT(num1), NUMERIC_SIGN(num1),
-								NUMERIC_DIGITS(num2), NUMERIC_NDIGITS(num2),
-								NUMERIC_WEIGHT(num2), NUMERIC_SIGN(num2));
+		result = cmp_var_common(var1.digits, var1.ndigits,
+								var1.weight, var1.sign,
+								var2.digits, var2.ndigits,
+								var2.weight, var2.sign);
 	}
 
 	return result;
@@ -3475,8 +3484,8 @@ numeric_inc(PG_FUNCTION_ARGS)
 Datum
 numeric_smaller(PG_FUNCTION_ARGS)
 {
-	Numeric		num1 = PG_GETARG_NUMERIC(0);
-	Numeric		num2 = PG_GETARG_NUMERIC(1);
+	Numeric		num1 = PG_GETARG_NUMERIC_PACKED(0);
+	Numeric		num2 = PG_GETARG_NUMERIC_PACKED(1);
 
 	/*
 	 * Use cmp_numerics so that this will agree with the comparison operators,
@@ -3497,8 +3506,8 @@ numeric_smaller(PG_FUNCTION_ARGS)
 Datum
 numeric_larger(PG_FUNCTION_ARGS)
 {
-	Numeric		num1 = PG_GETARG_NUMERIC(0);
-	Numeric		num2 = PG_GETARG_NUMERIC(1);
+	Numeric		num1 = PG_GETARG_NUMERIC_PACKED(0);
+	Numeric		num2 = PG_GETARG_NUMERIC_PACKED(1);
 
 	/*
 	 * Use cmp_numerics so that this will agree with the comparison operators,
@@ -11572,13 +11581,13 @@ cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight,
 
 	while (var1weight > var2weight && i1 < var1ndigits)
 	{
-		if (var1digits[i1++] != 0)
+		if (read_digit(var1digits, i1++) != 0)
 			return 1;
 		var1weight--;
 	}
 	while (var2weight > var1weight && i2 < var2ndigits)
 	{
-		if (var2digits[i2++] != 0)
+		if (read_digit(var2digits, i2++) != 0)
 			return -1;
 		var2weight--;
 	}
@@ -11589,7 +11598,7 @@ cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight,
 	{
 		while (i1 < var1ndigits && i2 < var2ndigits)
 		{
-			int			stat = var1digits[i1++] - var2digits[i2++];
+			int	stat = read_digit(var1digits, i1++) - read_digit(var2digits, i2++);
 
 			if (stat)
 			{
@@ -11606,12 +11615,12 @@ cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight,
 	 */
 	while (i1 < var1ndigits)
 	{
-		if (var1digits[i1++] != 0)
+		if (read_digit(var1digits, i1++) != 0)
 			return 1;
 	}
 	while (i2 < var2ndigits)
 	{
-		if (var2digits[i2++] != 0)
+		if (read_digit(var2digits, i2++) != 0)
 			return -1;
 	}
 
diff --git a/src/include/utils/numeric.h b/src/include/utils/numeric.h
index dc8adfce711..02455b19275 100644
--- a/src/include/utils/numeric.h
+++ b/src/include/utils/numeric.h
@@ -66,6 +66,12 @@ DatumGetNumeric(Datum X)
 	return (Numeric) PG_DETOAST_DATUM(X);
 }
 
+static inline Numeric
+DatumGetNumericPacked(Datum X)
+{
+	return (Numeric) PG_DETOAST_DATUM_PACKED(X);
+}
+
 static inline Numeric
 DatumGetNumericCopy(Datum X)
 {
@@ -78,9 +84,10 @@ NumericGetDatum(Numeric X)
 	return PointerGetDatum(X);
 }
 
-#define PG_GETARG_NUMERIC(n)	  DatumGetNumeric(PG_GETARG_DATUM(n))
-#define PG_GETARG_NUMERIC_COPY(n) DatumGetNumericCopy(PG_GETARG_DATUM(n))
-#define PG_RETURN_NUMERIC(x)	  return NumericGetDatum(x)
+#define PG_GETARG_NUMERIC(n)		DatumGetNumeric(PG_GETARG_DATUM(n))
+#define PG_GETARG_NUMERIC_PACKED(n)	DatumGetNumericPacked(PG_GETARG_DATUM(n))
+#define PG_GETARG_NUMERIC_COPY(n)	DatumGetNumericCopy(PG_GETARG_DATUM(n))
+#define PG_RETURN_NUMERIC(x)		return NumericGetDatum(x)
 
 /*
  * Utility functions in numeric.c
-- 
2.53.0

