Skip to content

Commit 82e0e29

Browse files
committed
Fix YA incremental sort bug.
switchToPresortedPrefixMode() did the wrong thing if it detected a batch boundary just at the last tuple of a fullsort group. The initially-reported symptom was a "retrieved too many tuples in a bounded sort" error, but the test case added here just silently gives the wrong answer without this patch. I (tgl) am not really happy about committing this patch without review from the incremental-sort authors, but they seem AWOL and we are hard against a release deadline. This does demonstrably make some cases better, anyway. Per bug #16846 from Yoran Heling. Back-patch to v13 where incremental sort was introduced. Neil Chen Discussion: https://postgr.es/m/16846-ae49f51ac379a4cb@postgresql.org
1 parent c34787f commit 82e0e29

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/backend/executor/nodeIncrementalSort.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@ switchToPresortedPrefixMode(PlanState *pstate)
394394
* current prefix key group.
395395
*/
396396
ExecClearTuple(node->group_pivot);
397+
398+
/*
399+
* Also make sure we take the didn't-consume-all-the-tuples
400+
* path below, even if this happened to be the last tuple of
401+
* the batch.
402+
*/
403+
lastTuple = false;
397404
break;
398405
}
399406
}

src/test/regress/expected/incremental_sort.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,17 @@ select * from (select * from t order by a) s order by a, b limit 70;
675675
9 | 70
676676
(70 rows)
677677

678+
-- Checks case where we hit a group boundary at the last tuple of a batch.
679+
select * from (select * from t order by a) s order by a, b limit 5;
680+
a | b
681+
---+---
682+
1 | 1
683+
2 | 2
684+
3 | 3
685+
4 | 4
686+
9 | 5
687+
(5 rows)
688+
678689
-- Test rescan.
679690
begin;
680691
-- We force the planner to choose a plan with incremental sort on the right side

src/test/regress/sql/incremental_sort.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ insert into t(a, b) select (case when i < 5 then i else 9 end), i from generate_
149149
analyze t;
150150
explain (costs off) select * from (select * from t order by a) s order by a, b limit 70;
151151
select * from (select * from t order by a) s order by a, b limit 70;
152+
-- Checks case where we hit a group boundary at the last tuple of a batch.
153+
select * from (select * from t order by a) s order by a, b limit 5;
154+
152155
-- Test rescan.
153156
begin;
154157
-- We force the planner to choose a plan with incremental sort on the right side

0 commit comments

Comments
 (0)