From c00e468625508f890746cd360eb61cd1a9629db8 Mon Sep 17 00:00:00 2001
From: David Geier <geidav.pg@gmail.com>
Date: Fri, 4 Dec 2026 08:07:14 +0100
Subject: [PATCH v1 4/6] Remove no longer needed sort support buffer

---
 src/backend/utils/adt/numeric.c | 63 ++++++++-------------------------
 1 file changed, 15 insertions(+), 48 deletions(-)

diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 28c9b1972cb..ae6b925cc51 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -340,7 +340,6 @@ typedef struct
  */
 typedef struct
 {
-	void	   *buf;			/* buffer for short varlenas */
 	int64		input_count;	/* number of non-null values seen */
 	bool		estimating;		/* true if estimating cardinality */
 
@@ -2157,12 +2156,6 @@ numeric_sortsupport(PG_FUNCTION_ARGS)
 
 		nss = palloc_object(NumericSortSupport);
 
-		/*
-		 * palloc a buffer for handling unaligned packed values in addition to
-		 * the support struct
-		 */
-		nss->buf = palloc(VARATT_SHORT_MAX + VARHDRSZ + 1);
-
 		nss->input_count = 0;
 		nss->estimating = true;
 		initHyperLogLog(&nss->abbr_card, 10);
@@ -2188,52 +2181,26 @@ static Datum
 numeric_abbrev_convert(Datum original_datum, SortSupport ssup)
 {
 	NumericSortSupport *nss = ssup->ssup_extra;
-	void	   *original_varatt = PG_DETOAST_DATUM_PACKED(original_datum);
-	Numeric		value;
+	Numeric		value = DatumGetNumericPacked(original_datum);
+	NumericVar	var;
 	Datum		result;
 
 	nss->input_count += 1;
 
-	/*
-	 * This is to handle packed datums without needing a palloc/pfree cycle;
-	 * we keep and reuse a buffer large enough to handle any short datum.
-	 */
-	if (VARATT_IS_SHORT(original_varatt))
-	{
-		void	   *buf = nss->buf;
-		Size		sz = VARSIZE_SHORT(original_varatt) - VARHDRSZ_SHORT;
-
-		Assert(sz <= VARATT_SHORT_MAX - VARHDRSZ_SHORT);
+	init_var_from_num(value, &var);
 
-		SET_VARSIZE(buf, VARHDRSZ + sz);
-		memcpy(VARDATA(buf), VARDATA_SHORT(original_varatt), sz);
-
-		value = (Numeric) buf;
-	}
+	if (var.sign == NUMERIC_PINF)
+		result = NUMERIC_ABBREV_PINF;
+	else if (var.sign == NUMERIC_NINF)
+		result = NUMERIC_ABBREV_NINF;
+	else if (var.sign == NUMERIC_NAN)
+		result = NUMERIC_ABBREV_NAN;
 	else
-		value = (Numeric) original_varatt;
-
-	if (NUMERIC_IS_SPECIAL(value))
-	{
-		if (NUMERIC_IS_PINF(value))
-			result = NUMERIC_ABBREV_PINF;
-		else if (NUMERIC_IS_NINF(value))
-			result = NUMERIC_ABBREV_NINF;
-		else
-			result = NUMERIC_ABBREV_NAN;
-	}
-	else
-	{
-		NumericVar	var;
-
-		init_var_from_num(value, &var);
-
 		result = numeric_abbrev_convert_var(&var, nss);
-	}
 
 	/* should happen only for external/compressed toasts */
-	if (original_varatt != DatumGetPointer(original_datum))
-		pfree(original_varatt);
+	if (value != DatumGetPointer(original_datum))
+		pfree(value);
 
 	return result;
 }
@@ -2412,16 +2379,16 @@ numeric_abbrev_convert_var(const NumericVar *var, NumericSortSupport *nss)
 		switch (ndigits)
 		{
 			default:
-				result |= ((int64) var->digits[3]);
+				result |= ((int64) read_digit(var->digits, 3)) << 0;
 				pg_fallthrough;
 			case 3:
-				result |= ((int64) var->digits[2]) << 14;
+				result |= ((int64) read_digit(var->digits, 2)) << 14;
 				pg_fallthrough;
 			case 2:
-				result |= ((int64) var->digits[1]) << 28;
+				result |= ((int64) read_digit(var->digits, 1)) << 28;
 				pg_fallthrough;
 			case 1:
-				result |= ((int64) var->digits[0]) << 42;
+				result |= ((int64) read_digit(var->digits, 0)) << 42;
 				break;
 		}
 	}
-- 
2.53.0

