A set of proposed patches would add PostgreSQL numeric fast paths for bounded values such as numeric(18,4). In a benchmark of a multi-aggregate query, runtime fell from about 9.4 seconds to 2.8 seconds and memory use dropped from roughly 1.8 GB to 480 MiB.
The patches are on the pgsql-hackers mailing list and haven’t been merged into community PostgreSQL.
Why numeric costs so much
The PostgreSQL numeric fast paths target a common situation: real systems usually bound the size and precision of their numbers, so a limited case like numeric(18,4) is worth optimizing separately.
Numeric values sit on disk in a compact form with a 1-byte header, but the functions that work on them expect a four-byte header and aligned memory. So almost every operation allocates memory and copies the value before it does any work. With sorting, many input values or lots of GROUP BY groups, that overhead adds CPU use and can spill to disk.
The PostgreSQL numeric fast paths follow one pattern. The check looks at the value’s two top header bits, which distinguish long positive, long negative, short and special values like NaN or infinity, plus the varlena header and length. Check the value’s header, without touching its digits, to see whether it qualifies. If so, compute the answer the short way. If not, run the existing code unchanged. Results match the old code bit for bit, including the hashes used by hash indexes and partitioning. The storage format doesn’t change and there are no new settings, so nothing needs enabling.
The cost is the entry check, which is also the main regression risk.

Four changes
A static buffer. Short values are unpacked into a fixed local buffer instead of allocated memory. Only compressed or TOASTed values still allocate. Sorting 5 million rows dropped from about 6,400 ms to 4,900 ms on a 2019 Intel MacBook Pro, typically a 15% to 30% gain.
No unpacking for equality and hashing. Equality can be settled by comparing sign, weight and digit bytes with memcmp. Reported cuts range from 27% to 44% for different comparison cases, and GROUP BY hashing improves 23% to 48%.
Integer arithmetic. Addition, subtraction and multiplication on small values run in 64-bit integers and write the result straight into storage format. Gains vary by chip. Addition improved 45% on Intel, 35% on AMD and 30% on Arm, and a TPC-H Q1-like query improved 37%, 22% and 22%. With clang 19 the gain on one query was 17% to 25%.
Smaller aggregate state. sum and avg keep their running state in an int128 and only fall back to the full structure on overflow. That shrinks the per-group structure to 64 bytes. With 500k groups, sum(v) memory fell from 192 MB to 80 MB, a 58% drop.

The downsides
Wide operands such as numeric(30,6) show no real change, within about 3% noise. One case clearly regresses: when both numbers are short but far apart in digit positions and the fast path rejects them late. That addition runs 6% slower on AMD and 3% slower on Arm. Columns such as numeric(24,2) still take the general path, though int128 might extend it.

What this means for users
Numeric hashing needs only the weight and the digit bytes, so NaN and infinity simply hash to 0 and no unpacking is needed. The same trick might work for other types: the author wonders whether short bytea fields could gain a speedup too.
The big wins land in workloads that group on many keys or aggregate money-like columns, such as ERP systems. In the combined benchmark, a table of 3 million rows formed 592,000 groups with values from 0.00 to 999.99. The double precision version of the query didn’t change, as expected.
One PostgreSQL fork has reportedly adopted the patch set already.
How to help it land
Reviewers and testers can find the work in the thread “Use local buffer for packed numerics” on pgsql-hackers. Benchmarks and regression reports on real data are especially useful. Two other contributors, David Geier and Josh Hurst, are also working on numeric performance, so a faster numeric type may arrive in PostgreSQL 20.
Source: danolivo.substack.com