On Wed, Dec 2, 2026 at 6:48 PM John Naylor <[email protected]> wrote:
Hi John, Thanks for digging in. v4 is a single patch now and takes your review in several directions at once; the changes and answers are below. To recap for anyone joining: a bounded sort (ORDER BY ... LIMIT N) today switches to a heap of N tuples once 2*N tuples are in memory, and pays O(log N) per admitted tuple. This patch instead buffers up to Max(2*N, 1000) tuples and, whenever the buffer fills, uses quickselect to keep the N smallest and drop the rest: O(1) amortized per tuple. On reversed input, where every tuple is admitted, a large bounded sort runs in a fraction of the heap's time (about 0.2x at LIMIT 10000 here); random input matches the heap, since the first comparison rejects almost every tuple before it is buffered. What v4 does, beyond the algorithm: - Dropped the two not-for-commit patches. The GUC and the context-swap experiment are gone from the series; this is one patch meant for commit. I'll take the bump-context work to Chengpeng Yan's thread (see below), where it belongs. - Removed the dead work_mem heap fallback. You and Claude were right: I built with coverage and the fallback branch never executed, and under trace_sort the "wide rows" case logged "switching to bounded quickselect" and finished as quickselect, never reverting. There is now one in-memory strategy. The heap (make_bounded_heap) stays, but is reached only when work_mem is already full at the 2*N switch. - EXPLAIN now a quickselect bounded sort reports a new "top-N quickselect" method; the heap path still reports "top-N heapsort". This touches the subselect and incremental_sort expected output. - cost_sort() models quickselect. The bounded branch was costing the heap's N*log2(2N); it now charges one comparison per input plus the final sort of the N survivors, which is strictly below the old estimate. No plan changes in the regression suite beyond the label. - Removed the recursion budget, per your "over-engineered"; a normal quicksort has no such stopper. - The 1000-tuple floor now has a derivation in the comment (it keeps the prune amortized for small N and fits the initial memtuples array), not just an assertion. - Added a parallel ORDER BY ... LIMIT regression test. Bounded sorts run only in the leader, which prune_bounded_select() asserts; the test guards that the serial assumption holds when the plan goes parallel. On the specific points: > Maybe we can add some slack in that case. [...] I'd rather not have > two implementations if we can avoid it Done: no fallback, one strategy. A bounded sort can still exceed work_mem when later tuples are wider than earlier ones, exactly as the heap comment already warned, since N survivors must be kept; v4 keeps pruning with quickselect rather than switching methods. > Further, the 2*N threshold is just a heuristic [...] A higher > multiplier should be even better for amortizing the quickselect > passes. Agreed. On reversed input a larger prune buffer is monotonically better (text key at LIMIT 64: 0.45x of master at a 0-floor, 0.31x at 256, 0.28x at 4096), and random input is flat. I've left the buffer at Max(2*N, 1000) in v4 so this patch stays about removing the fallback and fixing the reporting, but a larger prune buffer decoupled from the 2*N switch, as you describe, looks worth its own step once this shape is settled. > it seems like an easy prerequisite to Andres' idea of copying the > survivors into a new context [...] That would allow a bump context Not as easy as I'd hoped, and the reason is why 0003 is gone. A rejected tuple is copied into the sort's tuple context before the bounded check runs, so it has to be freed, which a bump context cannot do per chunk. Avoiding that needs the compare-before-copy change from Chengpeng's thread; until then the buffer keeps the aset context. I've noted this in the commit message and will follow up on that thread rather than carry a half-step here. > This seems over-engineered. We don't have any recursion stoppers for > normal sorts Removed, as above. > It's probably okay to special-case N=1 if we have to, as a separate > patch. Agreed. LIMIT 1/2 on reversed input is the only case where the heap still wins; a running minimum for N=1 is a clean separate patch and I've left it out. > TODO for later if we get that far: cost_sort() still costs a heap. Handled in this version (see above). The abbreviated-key question is the remaining one I'd still call a follow-up. v4 attached, rebased onto dc168a83874. This is the piece I'd like reviewed for commit. -greg
v4-0001-Use-quickselect-instead-of-a-heap-for-bounded-sor.patch
Description: Binary data
