| From: | John Naylor <johncnaylorls(at)gmail(dot)com> |
|---|---|
| To: | Chengpeng Yan <chengpeng_yan(at)outlook(dot)com> |
| Cc: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Andres Freund <andres(at)anarazel(dot)de> |
| Subject: | Re: [PATCH] Avoid copying tuples that a bounded sort will discard |
| Date: | 2026-12-01 10:29:41 |
| Message-ID: | CANWCAZZQTQOg_r-OcCjEkK2A_N18vYeqsz=iFd4XJQWSGXP5gg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Sun, Nov 29, 2026 at 9:40 PM Chengpeng Yan <chengpeng_yan(at)outlook(dot)com> wrote:
> 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.
Nice!
> 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
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Amit Kapila | 2026-12-01 10:56:09 | Re: ALTER DEFAULT PRIVILEGES IN SCHEMA pg_conflict silently has no effect |
| Previous Message | shveta malik | 2026-12-01 10:14:50 | Re: DOC: Minor updates for logical replication conflicts |