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/[email protected]
[3] 
https://www.postgresql.org/message-id/CAApHDvraHHcaMp27MRW9j%3D%2BJwJ9_K8QujyhzxFcrWQpEv6DF9Q%40mail.gmail.com

--
Best regards,
Chengpeng Yan



Attachment: setup.sql
Description: setup.sql

Attachment: bench.sh
Description: bench.sh

Attachment: v1-0002-Avoid-copying-tuples-that-a-bounded-sort-will-dis.patch
Description: v1-0002-Avoid-copying-tuples-that-a-bounded-sort-will-dis.patch

Attachment: v1-0001-Skip-copying-rejected-Datums-in-bounded-sorts.patch
Description: v1-0001-Skip-copying-rejected-Datums-in-bounded-sorts.patch

Reply via email to