Re: [PATCH] Avoid copying tuples that a bounded sort will discard

From: Chengpeng Yan <chengpeng_yan(at)outlook(dot)com>
To: John Naylor <johncnaylorls(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Andres Freund <andres(at)anarazel(dot)de>, Greg Burd <greg(at)burd(dot)me>
Subject: Re: [PATCH] Avoid copying tuples that a bounded sort will discard
Date: 2026-12-03 14:38:53
Message-ID: 68A49E49-34CF-4053-A1E2-E8C6C8C0DA0C@outlook.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

> On Dec 1, 2026, at 18:29, John Naylor <johncnaylorls(at)gmail(dot)com> wrote:
>
>> 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.
>
> This doesn't cover virtual slots, so any join, projection, or
> aggregation will not take this path. I haven't tried, but I wonder if
> we can use slot_get_attr?
>
>> 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
>
> I can't reproduce this on my x86-64 machine, and in fact this prefetch
> step makes it slower than master. After removing the prefetch, I don't
> see any cases slower than master. More generally, we need to be
> careful when trying to force things to happen.
>
> + /* a tie on the leading key is settled by the remaining keys */
> + if (check == TUPLESORT_BOUNDED_TIE &&
> + state->base.comparetup_tiebreak(tuple, &state->memtuples[0],
> + state) <= 0)
> + {
> + free_sort_tuple(state, tuple);
> + CHECK_FOR_INTERRUPTS();
> + }
> + else
> + {
> + free_sort_tuple(state, &state->memtuples[0]);
> + tuplesort_heap_replace_top(state, tuple);
> + }
>
> I don't like partially testing for a tie before the copy, and having
> to remember to only tiebreak after it's copied. Can we either
>
> A. arrange so that the pre-copy comparison is fully authoritative? Or,
> B. just do the full comparison anyway, because we may not care? With
> the quick-select patch, we wouldn't have a min-heap anyway:
>
> https://www.postgresql.org/message-id/auNiWcC4iIAyjxEo@floki
>
> --
> John Naylor
> Amazon Web Services

Thanks for the review. v2 is attached, with these changes:

1. 0002 reads the leading key with slot_getattr(), so it covers every
slot type, and the code is much simpler.
2. read_tuple_lines() is removed.
3. I went with your option B. The pre-check now only decides whether an
input can be discarded; anything else is copied and goes through the
existing path unchanged. I had split the tie handling to avoid
comparing the leading key a second time, but keeping the two steps
independent is simpler.

0001 compares a pass-by-reference Datum with the heap root before
datumCopy() and returns early if it would be discarded. 0002 does the
same for tuples in tuplesort_puttupleslot().

I ran the benchmark on an x86-64 server and an aarch64 server; results
are master / patched median latency in ms, in bench.sh order. setup.sql
is unchanged; bench.sh adds one query, SELECT id, r FROM w ORDER BY r
LIMIT 10, which sorts the output of a projection (a virtual slot).

x86-64 (Intel Xeon):
master patched ratio
Datum, typical, en_US.UTF-8 84.0 70.1 1.20
Datum, typical, "C" 58.5 48.5 1.21
Datum, worst, en_US.UTF-8 737.7 754.4 0.98
Datum, worst, "C" 110.0 124.3 0.89
tuple, typical, narrow 41.4 27.4 1.51
tuple, typical, wide 81.6 84.5 0.97
tuple, typical, projection 137.7 99.6 1.38
tuple, worst, narrow 62.2 69.1 0.90
tuple, worst, wide 98.4 103.7 0.95

aarch64 (ARM server):
master patched ratio
Datum, typical, en_US.UTF-8 238.9 220.5 1.08
Datum, typical, "C" 193.0 132.3 1.46
Datum, worst, en_US.UTF-8 1446.0 1481.2 0.98
Datum, worst, "C" 291.4 291.6 1.00
tuple, typical, narrow 120.4 96.4 1.25
tuple, typical, wide 262.5 167.1 1.57
tuple, typical, projection 296.9 216.8 1.37
tuple, worst, narrow 223.2 215.9 1.03
tuple, worst, wide 301.8 334.5 0.90

Most results are as expected, except "tuple, typical, wide" on x86. In
some cases like this one, it seems related to hardware prefetching once
the copy is gone; I'm still looking into it, and would welcome your view
on whether such a hardware-dependent effect is acceptable.

--
Best regards,
Chengpeng Yan

Attachment Content-Type Size
v2-0002-Avoid-copying-tuples-that-a-bounded-sort-will-dis.patch application/octet-stream 2.1 KB
v2-0001-Skip-copying-rejected-Datums-in-bounded-sorts.patch application/octet-stream 4.7 KB
setup.sql application/octet-stream 875 bytes
bench.sh application/octet-stream 643 bytes

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Matthias van de Meent 2026-12-03 14:43:57 Re: Use local buffer for packed numerics
Previous Message Fujii Masao 2026-12-03 14:32:20 Re: Standby writes pages past minRecoveryPoint after crash recovery