Hi,

After reviewing the current state of the numeric type [1], I noticed there is
still room to improve CPU and memory usage. Here's the first piece of
optimisation ideas.

The support function that sorts abbreviated numerics already uses a local buffer
to avoid calling palloc and pfree for each input value. I believe this method
could work for other numeric operations too.

This makes sense because almost every numeric that comes out of a heap tuple has
a one-byte varlena header and needs to be unpacked into a standard format before
use. Right now, each operator calls palloc() for every numeric value. Using a
preallocated local buffer could save some processing time.

This simple patchset shows how this could work. The first patch, 0001, adds a
structure called NumericLocalBuf, which should be a local variable in the main
operator. It also adds a function, numeric_unpack_local, that unpacks a numeric
into this static buffer or calls palloc if the value is too large.

The second patch, 0002, adds this setup to other numeric-related operations. It
acts as a helper to test how well this optimisation works.

>From what I've seen, the usual improvement is between 15% and 30%, with up to
35% speedup in multi-value corner cases. But of course, it depends on the
workload. An example:

CREATE TABLE t_num AS
SELECT i AS id,
  round((random() * 1e8)::numeric, 2)::numeric(12,2) AS a,
  ((i % 1000) * 1.37)::numeric(12,2) AS k1k,
  random()::numeric AS payload
FROM generate_series(1, 5000000) i;

SELECT k1k, a FROM t_num ORDER BY k1k, a OFFSET 4999999;

On my laptop, I see stable 6400ms for the current master branch and 4900ms with
this patchset applied.

What's interesting: it might provide a nice 10% speedup or more for sum(numeric)
because unpack is the only allocation that is needed by the aggregate. So, here
we save not only the palloc() call, but the context reset effort that should be
made on each value.

[1] https://www.pgedge.com/blog/why-is-postgresql-s-numeric-type-so-slow

-- 
regards, Andrei Lepikhov,
pgEdge
From c82d6c992966d0e6e9da6d974691b1e91a2310ac Mon Sep 17 00:00:00 2001
From: "Andrei V. Lepikhov" <[email protected]>
Date: Mon, 10 Aug 2026 20:17:03 +0200
Subject: [PATCH v0 1/3] Unpack short numerics on the local buffer

The hash, comparison and sortsupport functions for numeric are hot, but every
call ran DatumGetNumeric() on its input. Numerics come out of a tuple with
a one-byte varlena header and need one allocation per value per call.  None of
these functions lets the value outlive the call, so unpack into a local buffer,
falling back to DatumGetNumeric() only for compressed and out-of-line values.

The buffer is sized from the varlena short-header, so that every value which
can arrive with a one-byte header fits in it.

The comparison functions free a detoasted copy, as they did before.  The hash
functions never freed theirs; they keep leaving it to the caller's memory
context, so for them PG_GETARG_NUMERIC_LOCAL() simply replaces
PG_GETARG_NUMERIC().
---
 src/backend/utils/adt/numeric.c  | 189 +++++++++++++++++--------------
 src/tools/pgindent/typedefs.list |   1 +
 2 files changed, 104 insertions(+), 86 deletions(-)

diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 7deea2046f1..965ff8c27e5 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -494,6 +494,65 @@ static void dump_var(const char *str, NumericVar *var);
        (weight) <= NUMERIC_SHORT_WEIGHT_MAX && \
        (weight) >= NUMERIC_SHORT_WEIGHT_MIN)
 
+#define NUMERIC_LOCAL_DATASZ   (VARATT_SHORT_MAX - VARHDRSZ_SHORT)
+
+typedef union NumericLocalBuf
+{
+       struct NumericData num;
+       char            data[VARHDRSZ + NUMERIC_LOCAL_DATASZ];
+} NumericLocalBuf;
+
+/*
+ * Return a four-byte-header Numeric for X, using *buf when possible.  Sets
+ * *needfree if the result is a palloc'd detoasted copy.  Callers that must
+ * not leak memory, such as the comparison functions, pfree it.  The others use
+ * DatumGetNumericLocal() below and leave the copy to the caller's memory
+ * context.
+ */
+static inline Numeric
+numeric_unpack_local(Datum X, NumericLocalBuf *buf, bool *needfree)
+{
+       varlena    *attr = (varlena *) DatumGetPointer(X);
+
+       *needfree = false;
+
+       /*
+        * VARATT_IS_SHORT() is true for external TOAST pointers too, for which
+        * VARSIZE_SHORT() returns 0.  Exclude them here so that they reach the
+        * detoasting path below instead of the copy.
+        */
+       if (VARATT_IS_SHORT(attr) && !VARATT_IS_EXTERNAL(attr))
+       {
+               Size            data_size = VARSIZE_SHORT(attr) - 
VARHDRSZ_SHORT;
+
+               SET_VARSIZE(&buf->num, data_size + VARHDRSZ);
+               memcpy(VARDATA(&buf->num), VARDATA_SHORT(attr), data_size);
+               return &buf->num;
+       }
+       else if (likely(!VARATT_IS_EXTENDED(attr)))
+               return (Numeric) attr;
+
+       /* compressed or out-of-line */
+       *needfree = true;
+       return DatumGetNumeric(X);
+}
+
+/*
+ * DatumGetNumeric() that unpacks a short-header value into *buf instead of
+ * palloc'ing a copy.  The result may point into *buf, so it must not be
+ * returned or kept beyond the caller's frame.
+ */
+static inline Numeric
+DatumGetNumericLocal(Datum X, NumericLocalBuf *buf)
+{
+       bool            needfree;
+
+       return numeric_unpack_local(X, buf, &needfree);
+}
+
+#define PG_GETARG_NUMERIC_LOCAL(n, buf) \
+       DatumGetNumericLocal(PG_GETARG_DATUM(n), (buf))
+
 static void alloc_var(NumericVar *var, int ndigits);
 static void free_var(NumericVar *var);
 static void zero_var(NumericVar *var);
@@ -2273,32 +2332,44 @@ numeric_abbrev_abort(int memtupcount, SortSupport ssup)
 }
 
 /*
- * Non-fmgr interface to the comparison routine to allow sortsupport to elide
- * 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.
+ * Compare two numeric Datums without palloc'ing short-header inputs; see
+ * numeric_unpack_local().  This is the body of numeric_fast_cmp() and of all
+ * the fmgr-callable comparison functions below; it is forced inline so that
+ * each of them stays a single call into cmp_numerics().
  */
-static int
-numeric_fast_cmp(Datum x, Datum y, SortSupport ssup)
+static pg_always_inline int
+cmp_numeric_datums(Datum x, Datum y)
 {
-       Numeric         nx = DatumGetNumeric(x);
-       Numeric         ny = DatumGetNumeric(y);
+       NumericLocalBuf bufx;
+       NumericLocalBuf bufy;
+       bool            freex;
+       bool            freey;
+       Numeric         nx = numeric_unpack_local(x, &bufx, &freex);
+       Numeric         ny = numeric_unpack_local(y, &bufy, &freey);
        int                     result;
 
        result = cmp_numerics(nx, ny);
 
-       if (nx != DatumGetPointer(x))
+       if (unlikely(freex))
                pfree(nx);
-       if (ny != DatumGetPointer(y))
+       if (unlikely(freey))
                pfree(ny);
 
        return result;
 }
 
+/*
+ * Non-fmgr interface to the comparison routine to allow sortsupport to elide
+ * 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.
+ */
+static int
+numeric_fast_cmp(Datum x, Datum y, SortSupport ssup)
+{
+       return cmp_numeric_datums(x, y);
+}
+
 /*
  * Compare abbreviations of values. (Abbreviations may be equal where the true
  * values differ, but if the abbreviations differ, they must reflect the
@@ -2418,107 +2489,51 @@ 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);
-       int                     result;
-
-       result = cmp_numerics(num1, num2);
-
-       PG_FREE_IF_COPY(num1, 0);
-       PG_FREE_IF_COPY(num2, 1);
-
-       PG_RETURN_INT32(result);
+       PG_RETURN_INT32(cmp_numeric_datums(PG_GETARG_DATUM(0),
+                                                                          
PG_GETARG_DATUM(1)));
 }
 
 
 Datum
 numeric_eq(PG_FUNCTION_ARGS)
 {
-       Numeric         num1 = PG_GETARG_NUMERIC(0);
-       Numeric         num2 = PG_GETARG_NUMERIC(1);
-       bool            result;
-
-       result = cmp_numerics(num1, num2) == 0;
-
-       PG_FREE_IF_COPY(num1, 0);
-       PG_FREE_IF_COPY(num2, 1);
-
-       PG_RETURN_BOOL(result);
+       PG_RETURN_BOOL(cmp_numeric_datums(PG_GETARG_DATUM(0),
+                                                                         
PG_GETARG_DATUM(1)) == 0);
 }
 
 Datum
 numeric_ne(PG_FUNCTION_ARGS)
 {
-       Numeric         num1 = PG_GETARG_NUMERIC(0);
-       Numeric         num2 = PG_GETARG_NUMERIC(1);
-       bool            result;
-
-       result = cmp_numerics(num1, num2) != 0;
-
-       PG_FREE_IF_COPY(num1, 0);
-       PG_FREE_IF_COPY(num2, 1);
-
-       PG_RETURN_BOOL(result);
+       PG_RETURN_BOOL(cmp_numeric_datums(PG_GETARG_DATUM(0),
+                                                                         
PG_GETARG_DATUM(1)) != 0);
 }
 
 Datum
 numeric_gt(PG_FUNCTION_ARGS)
 {
-       Numeric         num1 = PG_GETARG_NUMERIC(0);
-       Numeric         num2 = PG_GETARG_NUMERIC(1);
-       bool            result;
-
-       result = cmp_numerics(num1, num2) > 0;
-
-       PG_FREE_IF_COPY(num1, 0);
-       PG_FREE_IF_COPY(num2, 1);
-
-       PG_RETURN_BOOL(result);
+       PG_RETURN_BOOL(cmp_numeric_datums(PG_GETARG_DATUM(0),
+                                                                         
PG_GETARG_DATUM(1)) > 0);
 }
 
 Datum
 numeric_ge(PG_FUNCTION_ARGS)
 {
-       Numeric         num1 = PG_GETARG_NUMERIC(0);
-       Numeric         num2 = PG_GETARG_NUMERIC(1);
-       bool            result;
-
-       result = cmp_numerics(num1, num2) >= 0;
-
-       PG_FREE_IF_COPY(num1, 0);
-       PG_FREE_IF_COPY(num2, 1);
-
-       PG_RETURN_BOOL(result);
+       PG_RETURN_BOOL(cmp_numeric_datums(PG_GETARG_DATUM(0),
+                                                                         
PG_GETARG_DATUM(1)) >= 0);
 }
 
 Datum
 numeric_lt(PG_FUNCTION_ARGS)
 {
-       Numeric         num1 = PG_GETARG_NUMERIC(0);
-       Numeric         num2 = PG_GETARG_NUMERIC(1);
-       bool            result;
-
-       result = cmp_numerics(num1, num2) < 0;
-
-       PG_FREE_IF_COPY(num1, 0);
-       PG_FREE_IF_COPY(num2, 1);
-
-       PG_RETURN_BOOL(result);
+       PG_RETURN_BOOL(cmp_numeric_datums(PG_GETARG_DATUM(0),
+                                                                         
PG_GETARG_DATUM(1)) < 0);
 }
 
 Datum
 numeric_le(PG_FUNCTION_ARGS)
 {
-       Numeric         num1 = PG_GETARG_NUMERIC(0);
-       Numeric         num2 = PG_GETARG_NUMERIC(1);
-       bool            result;
-
-       result = cmp_numerics(num1, num2) <= 0;
-
-       PG_FREE_IF_COPY(num1, 0);
-       PG_FREE_IF_COPY(num2, 1);
-
-       PG_RETURN_BOOL(result);
+       PG_RETURN_BOOL(cmp_numeric_datums(PG_GETARG_DATUM(0),
+                                                                         
PG_GETARG_DATUM(1)) <= 0);
 }
 
 static int
@@ -2716,7 +2731,8 @@ in_range_numeric_numeric(PG_FUNCTION_ARGS)
 Datum
 hash_numeric(PG_FUNCTION_ARGS)
 {
-       Numeric         key = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         key = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        uint32          digit_hash;
        uint32          result;
        int                     weight;
@@ -2797,7 +2813,8 @@ hash_numeric(PG_FUNCTION_ARGS)
 Datum
 hash_numeric_extended(PG_FUNCTION_ARGS)
 {
-       Numeric         key = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         key = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        uint64          seed = PG_GETARG_INT64(1);
        uint64          digit_hash;
        uint64          result;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index f10eb469266..6445598e167 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -1845,6 +1845,7 @@ NullingRelsMatch
 Numeric
 NumericAggState
 NumericDigit
+NumericLocalBuf
 NumericSortSupport
 NumericSumAccum
 NumericVar
-- 
2.56.1

From e02d76b121a2fe0c78695abf1387b3e7c5948c6a Mon Sep 17 00:00:00 2001
From: "Andrei V. Lepikhov" <[email protected]>
Date: Tue, 17 Nov 2026 06:53:21 +0100
Subject: [PATCH v0 2/3] Unpack short numerics on the stack in the remaining
 functions

---
 src/backend/utils/adt/numeric.c | 136 +++++++++++++++++---------------
 1 file changed, 74 insertions(+), 62 deletions(-)

diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 965ff8c27e5..ba5502df5a7 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 */
 
@@ -857,7 +856,8 @@ invalid_syntax:
 Datum
 numeric_out(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        NumericVar      x;
        char       *str;
 
@@ -1204,7 +1204,8 @@ numeric_recv(PG_FUNCTION_ARGS)
 Datum
 numeric_send(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf numbuf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &numbuf);
        NumericVar      x;
        StringInfoData buf;
        int                     i;
@@ -1442,7 +1443,8 @@ numerictypmodout(PG_FUNCTION_ARGS)
 Datum
 numeric_abs(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        Numeric         res;
 
        /*
@@ -1469,7 +1471,8 @@ numeric_abs(PG_FUNCTION_ARGS)
 Datum
 numeric_uminus(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        Numeric         res;
 
        /*
@@ -1559,7 +1562,8 @@ numeric_sign_internal(Numeric num)
 Datum
 numeric_sign(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
 
        /*
         * Handle NaN (infinities can be handled normally)
@@ -1592,7 +1596,8 @@ numeric_sign(PG_FUNCTION_ARGS)
 Datum
 numeric_round(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        int32           scale = PG_GETARG_INT32(1);
        Numeric         res;
        NumericVar      arg;
@@ -1646,7 +1651,8 @@ numeric_round(PG_FUNCTION_ARGS)
 Datum
 numeric_trunc(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        int32           scale = PG_GETARG_INT32(1);
        Numeric         res;
        NumericVar      arg;
@@ -1696,7 +1702,8 @@ numeric_trunc(PG_FUNCTION_ARGS)
 Datum
 numeric_ceil(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        Numeric         res;
        NumericVar      result;
 
@@ -1724,7 +1731,8 @@ numeric_ceil(PG_FUNCTION_ARGS)
 Datum
 numeric_floor(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        Numeric         res;
        NumericVar      result;
 
@@ -2016,9 +2024,12 @@ generate_series_numeric_support(PG_FUNCTION_ARGS)
 Datum
 width_bucket_numeric(PG_FUNCTION_ARGS)
 {
-       Numeric         operand = PG_GETARG_NUMERIC(0);
-       Numeric         bound1 = PG_GETARG_NUMERIC(1);
-       Numeric         bound2 = PG_GETARG_NUMERIC(2);
+       NumericLocalBuf buf1;
+       NumericLocalBuf buf2;
+       NumericLocalBuf buf3;
+       Numeric         operand = PG_GETARG_NUMERIC_LOCAL(0, &buf1);
+       Numeric         bound1 = PG_GETARG_NUMERIC_LOCAL(1, &buf2);
+       Numeric         bound2 = PG_GETARG_NUMERIC_LOCAL(2, &buf3);
        int32           count = PG_GETARG_INT32(3);
        NumericVar      count_var;
        NumericVar      result_var;
@@ -2185,12 +2196,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);
@@ -2216,31 +2221,13 @@ 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;
+       NumericLocalBuf buf;
+       bool            needfree;
+       Numeric         value = numeric_unpack_local(original_datum, &buf, 
&needfree);
        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);
-
-               SET_VARSIZE(buf, VARHDRSZ + sz);
-               memcpy(VARDATA(buf), VARDATA_SHORT(original_varatt), sz);
-
-               value = (Numeric) buf;
-       }
-       else
-               value = (Numeric) original_varatt;
-
        if (NUMERIC_IS_SPECIAL(value))
        {
                if (NUMERIC_IS_PINF(value))
@@ -2259,9 +2246,9 @@ numeric_abbrev_convert(Datum original_datum, SortSupport 
ssup)
                result = numeric_abbrev_convert_var(&var, nss);
        }
 
-       /* should happen only for external/compressed toasts */
-       if (original_varatt != DatumGetPointer(original_datum))
-               pfree(original_varatt);
+       /* the sort's memory context is not reset per call */
+       if (unlikely(needfree))
+               pfree(value);
 
        return result;
 }
@@ -2596,9 +2583,18 @@ cmp_numerics(Numeric num1, Numeric num2)
 Datum
 in_range_numeric_numeric(PG_FUNCTION_ARGS)
 {
-       Numeric         val = PG_GETARG_NUMERIC(0);
-       Numeric         base = PG_GETARG_NUMERIC(1);
-       Numeric         offset = PG_GETARG_NUMERIC(2);
+       NumericLocalBuf buf1;
+       NumericLocalBuf buf2;
+       NumericLocalBuf buf3;
+       bool            free1;
+       bool            free2;
+       bool            free3;
+       Numeric         val = numeric_unpack_local(PG_GETARG_DATUM(0), &buf1,
+                                                                               
   &free1);
+       Numeric         base = numeric_unpack_local(PG_GETARG_DATUM(1), &buf2,
+                                                                               
        &free2);
+       Numeric         offset = numeric_unpack_local(PG_GETARG_DATUM(2), &buf3,
+                                                                               
          &free3);
        bool            sub = PG_GETARG_BOOL(3);
        bool            less = PG_GETARG_BOOL(4);
        bool            result;
@@ -2721,9 +2717,12 @@ in_range_numeric_numeric(PG_FUNCTION_ARGS)
                free_var(&sum);
        }
 
-       PG_FREE_IF_COPY(val, 0);
-       PG_FREE_IF_COPY(base, 1);
-       PG_FREE_IF_COPY(offset, 2);
+       if (unlikely(free1))
+               pfree(val);
+       if (unlikely(free2))
+               pfree(base);
+       if (unlikely(free3))
+               pfree(offset);
 
        PG_RETURN_BOOL(result);
 }
@@ -3272,8 +3271,10 @@ division_by_zero:
 Datum
 numeric_div_trunc(PG_FUNCTION_ARGS)
 {
-       Numeric         num1 = PG_GETARG_NUMERIC(0);
-       Numeric         num2 = PG_GETARG_NUMERIC(1);
+       NumericLocalBuf buf1;
+       NumericLocalBuf buf2;
+       Numeric         num1 = PG_GETARG_NUMERIC_LOCAL(0, &buf1);
+       Numeric         num2 = PG_GETARG_NUMERIC_LOCAL(1, &buf2);
        NumericVar      arg1;
        NumericVar      arg2;
        NumericVar      result;
@@ -3361,8 +3362,10 @@ numeric_div_trunc(PG_FUNCTION_ARGS)
 Datum
 numeric_mod(PG_FUNCTION_ARGS)
 {
-       Numeric         num1 = PG_GETARG_NUMERIC(0);
-       Numeric         num2 = PG_GETARG_NUMERIC(1);
+       NumericLocalBuf buf1;
+       NumericLocalBuf buf2;
+       Numeric         num1 = PG_GETARG_NUMERIC_LOCAL(0, &buf1);
+       Numeric         num2 = PG_GETARG_NUMERIC_LOCAL(1, &buf2);
        Numeric         res;
 
        res = numeric_mod_safe(num1, num2, NULL);
@@ -4132,7 +4135,8 @@ numeric_power(PG_FUNCTION_ARGS)
 Datum
 numeric_scale(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
 
        if (NUMERIC_IS_SPECIAL(num))
                PG_RETURN_NULL();
@@ -4197,7 +4201,8 @@ get_min_scale(NumericVar *var)
 Datum
 numeric_min_scale(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        NumericVar      arg;
        int                     min_scale;
 
@@ -4217,7 +4222,8 @@ numeric_min_scale(PG_FUNCTION_ARGS)
 Datum
 numeric_trim_scale(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        Numeric         res;
        NumericVar      result;
 
@@ -4427,7 +4433,8 @@ numeric_int4_safe(Numeric num, Node *escontext)
 Datum
 numeric_int4(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        int32           result;
 
        result = numeric_int4_safe(num, fcinfo->context);
@@ -4503,7 +4510,8 @@ numeric_int8_safe(Numeric num, Node *escontext)
 Datum
 numeric_int8(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        int64           result;
 
        result = numeric_int8_safe(num, fcinfo->context);
@@ -4527,7 +4535,8 @@ int2_numeric(PG_FUNCTION_ARGS)
 Datum
 numeric_int2(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        NumericVar      x;
        int64           val;
        int16           result;
@@ -4603,7 +4612,8 @@ float8_numeric(PG_FUNCTION_ARGS)
 Datum
 numeric_float8(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        char       *tmp;
        Datum           result;
 
@@ -4640,7 +4650,8 @@ numeric_float8(PG_FUNCTION_ARGS)
 Datum
 numeric_float8_no_overflow(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        double          val;
 
        if (NUMERIC_IS_SPECIAL(num))
@@ -4702,7 +4713,8 @@ float4_numeric(PG_FUNCTION_ARGS)
 Datum
 numeric_float4(PG_FUNCTION_ARGS)
 {
-       Numeric         num = PG_GETARG_NUMERIC(0);
+       NumericLocalBuf buf;
+       Numeric         num = PG_GETARG_NUMERIC_LOCAL(0, &buf);
        char       *tmp;
        Datum           result;
 
-- 
2.56.1

Reply via email to