| From: | David Geier <geidav(dot)pg(at)gmail(dot)com> |
|---|---|
| To: | Andrei Lepikhov <lepihov(at)gmail(dot)com>, Josh Hurst <joshhurst77(at)gmail(dot)com>, chenhuimo(dot)mch(at)qq(dot)com |
| Cc: | pgsql-hackers(at)lists(dot)postgresql(dot)org, "andreas(at)proxel(dot)se" <andreas(at)proxel(dot)se> |
| Subject: | Re: Use local buffer for packed numerics |
| Date: | 2026-12-03 11:38:16 |
| Message-ID: | 52a48fb7-702a-42d0-8a17-d1f19a0e6010@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi Andrei, Chenhui, Josh,
Having read through the four threads [1], [2], [3], [4], I think the
approaches can be summarized as follows:
A. Packed-aware comparisons and aggregates [3]
Chenhui's approach makes selected comparison and aggregate paths
packed-aware. For SUM/AVG/VARIANCE, it initializes a NumericVar from
the packed input and otherwise uses the existing accumulation
algorithms. Unaligned digits are copied into a stack buffer. For MIN/
MAX, it also returns the selected original argument rather than an
expanded copy.
B. Local-buffer unpacking [1]
Andrei's approach expands short varlenas into an aligned buffer on the
stack, avoiding palloc()/pfree() when the input fits in the buffer.
Larger inputs fall back to allocation. This preserves the existing
numeric code and does not require alignment-safe reads, but still
copies the input.
C. Equality and hashing shortcuts [2]
Andrei's second approach handles equality and hashing directly from
the stored representation. Equality uses specialized shortcuts,
including memcmp() for suitable inputs, with a fallback where needed.
Hashing retains the existing hash algorithm but reads the weight and
digit bytes without expanding the input.
D. Fast arithmetic for small inputs [4]
Josh's approach adds fixed-width arithmetic fast paths for small
inputs. Instead of using the general digit-array addition/subtraction
routines, it converts operands spanning at most four base-10000
positions into equally scaled int64 values, performs the operation,
and converts back. Multiplication uses a specialized uint64
partial-product calculation for operands of up to four digits each.
Temporary result digits are stored on the stack.
E. Compact aggregate state [4]
Josh's SUM/AVG patch stores small positive and negative digit buffers
directly inside the aggregate state, reusing space otherwise reserved
for the unused sum-of-squares accumulator. This avoids separate buffer
allocations until the sum outgrows the inline storage. It also adds a
direct accumulation fast path when no carry propagation or resizing
is needed.
F. Generalized packed-aware access
The approach I outlined makes init_var_from_num() handle both packed
and unpacked inputs, centralizing all representation decoding.
Alignment-safe digit reads then let the existing algorithms operate
directly on the packed input without allocating or copying.
This should cover the input-access benefits of the approaches in [3]
and [1], without separate packed implementations, while being
incrementally applicable to all code in numeric.c.
The hashing changes in [2] should largely be covered by using the
shared packed-aware helpers. Equality-specific shortcuts, fast
arithmetic, and compact aggregate states are separate optimizations
that can be built on top.
Does that sound like a fair characterization? I think it helps
distinguish the common input-access optimization from algorithmic and
aggregate-state improvements that we can evaluate independently.
I'm planning to shortly open a new thread for approach F. This would get
us a decent speedup for pretty much all NUMERIC functionality. We could
then add your more targeted optimizations for subsets of NUMERIC on top.
Beyond that we should settle on a common benchmark. If the benchmark is
not specific enough for some of the optimizations, we can extend it or
at least use it for showing there are no regressions. I attached the
latest version of my NUMERIC benchmark. I extended it to also cover
special NUMERICs and more functionality (hashing, in/out, ...).
--
David Geier
[1]
https://www.postgresql.org/message-id/flat/5f99450c-4b45-4b82-8498-15461411037c%40gmail.com
[2]
https://www.postgresql.org/message-id/flat/959ad09c-1d66-4260-9c2a-17b2dff03f14%40gmail.com
[3]
https://www.postgresql.org/message-id/flat/tencent_D999DB30A62C6B4D956CA9BADF35CFE91408%40qq.com
[4]
https://www.postgresql.org/message-id/flat/CALJT8Ej89oee83Q-t_x6M1t5QDDRX5U4BzgT0Q-UfHBw%3D7Cb1g%40mail.gmail.com
| Attachment | Content-Type | Size |
|---|---|---|
| numeric_benchmark.sql | text/plain | 29.2 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Jan Nidzwetzki | 2026-12-03 11:40:25 | Re: pg_rewind WAL segments deletion pitfall |
| Previous Message | shveta malik | 2026-12-03 11:34:34 | Re: Include schema-qualified names in publication error messages (generalised to all of LR). |