| From: | Chengpeng Yan <chengpeng_yan(at)outlook(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Cc: | John Naylor <johncnaylorls(at)gmail(dot)com>, Andres Freund <andres(at)anarazel(dot)de> |
| Subject: | [PATCH] Avoid copying tuples that a bounded sort will discard |
| Date: | 2026-11-29 14:39:58 |
| Message-ID: | E0AE9A56-56AE-4E38-8B07-6FA30DF685C4@outlook.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
In [1], John pointed to Andres' idea [2] of checking whether a bounded
(top-N) sort would discard an input before copying it. Today tuplesort
copies every input first and only then compares it with the heap root.
Once the heap is full, almost every input loses that comparison, so for
ORDER BY ... LIMIT n over many rows most of the work goes into copying
and freeing tuples that are thrown away immediately. The attached
patches compare the leading key with the root first and copy only inputs
that may be kept.
0001 does this for Datum sorts of pass-by-reference types, e.g. SELECT
col FROM tab ORDER BY col LIMIT 10 on a text column. 0002 extends this
to heap and minimal tuples, e.g. SELECT * FROM tab ORDER BY col LIMIT
10. When the leading keys tie, the remaining keys decide after the
copy.
0002 also adds read_tuple_lines(), which reads every cache line of a
tuple before the pre-check fetches its leading key. Without it, the
pre-check touches only a few scattered bytes of each tuple; in a wide
row these miss the CPU cache and each waits on memory, so a scan of wide
rows held in memory was noticeably slower than master, whose copy reads
the whole tuple sequentially and lets the CPU fetch it ahead. Prefetch
hints made no difference on this machine, much like David's results for
prefetching in seqscans on M1 [3]. I'm not sure this approach is
acceptable, but it is the simplest, cheapest and most effective option I
have found; better ideas are welcome.
Tested on an Apple M4 Max with an -O2 build, database locale
en_US.UTF-8, shared_buffers = 4GB and max_parallel_workers_per_gather =
0. Each table has 1M rows, prewarmed into shared_buffers; every plan is
a top-N heapsort over a seq scan. The numbers are median latencies in
ms (lower is better) of 6 runs per query: bench.sh was run twice per
build, alternating between builds and restarting the server each time.
Ratio = master / patched (higher is better). Cases:
typical: the sort key is in random order
worst: the sort key is stored in reverse order, so every row is
kept and the check is pure overhead
0001: SELECT col FROM tab ORDER BY col LIMIT 10 on a text column
master patched ratio
typical, en_US.UTF-8 188.6 180.7 1.04
typical, "C" 34.2 24.2 1.41
worst, en_US.UTF-8 1433.7 1396.2 1.03
worst, "C" 72.7 69.6 1.04
0002: SELECT * FROM tab ORDER BY col LIMIT 10, where tab is narrow (two
int8 and one 100-byte text column) or wide (three 128-byte text columns,
then the int8 sort keys)
master patched ratio
typical, narrow 21.7 14.9 1.46
typical, wide 33.9 25.4 1.33
worst, narrow 37.3 39.0 0.96
worst, wide 52.4 55.6 0.94
setup.sql creates the tables, and bench.sh runs the queries; run it
against master and against the patched build.
[1] https://www.postgresql.org/message-id/CANWCAZYW8h7rEPzD0AR2oUuuf45x3MUeOFm0psk2ayAgQRmkTw%40mail.gmail.com
[2] https://www.postgresql.org/message-id/20230211202959.lkhrxiy7awsyctef@awork3.anarazel.de
[3] https://www.postgresql.org/message-id/CAApHDvraHHcaMp27MRW9j%3D%2BJwJ9_K8QujyhzxFcrWQpEv6DF9Q%40mail.gmail.com
--
Best regards,
Chengpeng Yan
| Attachment | Content-Type | Size |
|---|---|---|
| setup.sql | application/octet-stream | 875 bytes |
| bench.sh | application/octet-stream | 601 bytes |
| v1-0002-Avoid-copying-tuples-that-a-bounded-sort-will-dis.patch | application/octet-stream | 11.1 KB |
| v1-0001-Skip-copying-rejected-Datums-in-bounded-sorts.patch | application/octet-stream | 7.0 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Andrew Dunstan | 2026-11-29 14:39:52 | Re: Add ASCII fast path to Unicode normalization functions |