| From: | John Naylor <johncnaylorls(at)gmail(dot)com> |
|---|---|
| To: | Greg Burd <greg(at)burd(dot)me> |
| Cc: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Subject: | Re: Quickselect instead of a heap for ORDER BY ... LIMIT N large bounded sorts |
| Date: | 2026-12-02 11:48:24 |
| Message-ID: | CANWCAZaKbP9NHznx57V1ZtjDqA-1vJkbp_+LArNUY2GHUFPbvA@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
(This is not a full review, just getting some thoughts out)
On Thu, Oct 29, 2026 at 10:27 PM Greg Burd <greg(at)burd(dot)me> wrote:
> I swept floors of 0, 64, 256, 1024 and 4096
> first.
They need mopping as well. ;-)
> So LIMIT 1 and 2 on reversed input are still slower than the heap (v1
> at LIMIT 1 was 2.65x). For N = 1 the heap does one comparison and one
> replace per tuple, and a select over 1000 tuples can't match that.
> Random input at LIMIT 1 is 0.99x, because there the first comparison
> rejects nearly everything. If that LIMIT 1/2 corner matters, I'd
> rather special-case N = 1 as a running minimum than keep the heap for
> it.
It's probably okay to special-case N=1 if we have to, as a separate patch.
> The heap now only runs when bound tuples alone no longer fit in
> work_mem, as a memory fallback. 0001 has a regress test that forces
> that path.
Hmm. Bounded sorts already aren't guaranteed to stay within work_mem:
* Note that once we enter TSS_BOUNDED state we will always try to
* complete the sort that way. In the worst case, if later input
* tuples are larger than earlier ones, this might cause us to
* exceed workMem significantly.
Maybe we can add some slack in that case. Claude suggests N/(2 x log2
N) as the crossover point where quickselect would lose to heap, but
we'd need some experiments to verify. I'd rather not have two
implementations if we can avoid it (except now with the temp GUC for
comparing). Plus, if we can move to bump context later that should
prevent real-life memory increases compared to v19.
(If I turn out to be misguided, Claude told me it proved the new test
doesn't actually reach that path. I can share the gdb script.)
Further, the 2*N threshold is just a heuristic where heapsort becomes
cheaper than a quicksort. A higher multiplier should be even better
for amortizing the quickselect passes. The prune threshold doesn't
have to be the switch threshold, but if quickselect is faster, a
higher threshold is probably justified anyway.
> > Also, with quickselect, it seems like an easy prerequisite to Andres'
> > idea of copying the survivors into a new context instead of
> > individually freeing the losers? That would allow a bump context
>
> Yes, and that's now in v2-0003. At each prune it copies the bound
> survivors into a spare aset context and resets the old one. The reset
> drops the losers, and everything the admission check freed since the
> last prune, in one go. It needs a tuplelen callback, which only the
> MinimalTuple and Datum variants (the only bounded-sort users) get. It's
> still aset, because a rejected tuple is copied into tuplecontext before
> the bounded check runs, so it still has to be pfree'd. A bump context
> needs that pre-check from Andres' mail too, which I haven't done.
There is a start in that direction:
https://www.postgresql.org/message-id/E0AE9A56-56AE-4E38-8B07-6FA30DF685C4%40outlook.com
+ if (--budget < 0)
+ {
+ qsort_tuple(a + lo, len, state->base.comparetup, state);
+ return;
+ }
This seems over-engineered. We don't have any recursion stoppers for
normal sorts, and we expect those to be bigger than bounded sorts.
TODO for later if we get that far: cost_sort() still costs a heap.
--
John Naylor
Amazon Web Services
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Robert Haas | 2026-12-02 11:41:48 | Re: [PATCH 0/2] Parallelizing the archiver |