From d67f757f4b1fc9c4b8c7e39cb09463a3d68e4b8e Mon Sep 17 00:00:00 2001 From: David Rowley Date: Mon, 16 Nov 2026 13:28:32 +1300 Subject: [PATCH v2] Fix issue with Incremental Sort when boundedness changes ExecSetTupleBound() can adjust the IncrementalSortState bounded flag between rescans of the Incremental Sort node. Starting with 6ed83d5fa, tuplesort.c uses the Bump memory context type when bounded sorts are not required, as we never need to pfree individual tuples when not performing a bounded sort. When a LIMIT node exists and the tuple limit isn't a Const, the actual tuple limit could be available for some scans, but not others. If the LIMIT isn't known for the first scan, then the tuplesort will be initialized without the TUPLESORT_ALLOWBOUNDED flag, and if a later scan required a bound, then the tuplesort will mistakenly use a Bump context type, which cannot support the pfreeing of tuples. Here we fix this by adding a new external function for tuplesort which allows the sort options to change the TUPLESORT_ALLOWBOUNDED flag. This must be changed before calling tuplesort_reset() as that's the point when tuplesort_begin_batch() creates the tuplecontext as either an AllocSet context or Bump context. Versions prior to v17 didn't have 6ed83d5fa, but the fix is still required there to resolve an Assert failure. Reported-by: shihao zhong Author: David Rowley Discussion: https://postgr.es/m/CAGRkXqRXMiR9EBxBT7VEF0jF_9NJAxVcGrtRdsTZOfNkHfVtsg@mail.gmail.com Backpatch-through: 15 --- src/backend/executor/nodeIncrementalSort.c | 10 ++++- src/backend/utils/sort/tuplesort.c | 16 ++++++++ src/include/utils/tuplesort.h | 2 + .../regress/expected/incremental_sort.out | 37 +++++++++++++++++++ src/test/regress/sql/incremental_sort.sql | 19 ++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/backend/executor/nodeIncrementalSort.c b/src/backend/executor/nodeIncrementalSort.c index 613d7d1c349..97b7d7c764e 100644 --- a/src/backend/executor/nodeIncrementalSort.c +++ b/src/backend/executor/nodeIncrementalSort.c @@ -1102,12 +1102,20 @@ ExecReScanIncrementalSort(IncrementalSortState *node) * repay the setup cost, and because ExecIncrementalSort guards presorted * column functions by checking to see if the full sort state has been * initialized yet, setting the sort states to null here might actually - * cause a leak. + * cause a leak. The IncrementalSortState's bounded flag may change + * between scans, per ExecSetTupleBound(), so we must toggle that flag in + * the sort options before calling tuplesort_reset(). */ if (node->fullsort_state != NULL) + { + tuplesort_set_allowbounded(node->fullsort_state, node->bounded); tuplesort_reset(node->fullsort_state); + } if (node->prefixsort_state != NULL) + { + tuplesort_set_allowbounded(node->prefixsort_state, node->bounded); tuplesort_reset(node->prefixsort_state); + } /* * If chgParam of subnode is not null, then the plan will be re-scanned by diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index da3c41d7084..01e03f4ed4c 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -783,6 +783,22 @@ tuplesort_used_bound(Tuplesortstate *state) return state->boundUsed; } +/* + * tuplesort_set_allowbounded + * Set or unset TUPLESORT_ALLOWBOUNDED sort flag on an existing state. + * + * NOTE: tuplesort_reset() must be called after this function for the changes + * to take effect. + */ +void +tuplesort_set_allowbounded(Tuplesortstate *state, bool allowbounded) +{ + if (allowbounded) + state->base.sortopt |= TUPLESORT_ALLOWBOUNDED; + else + state->base.sortopt &= ~TUPLESORT_ALLOWBOUNDED; +} + /* * tuplesort_free * diff --git a/src/include/utils/tuplesort.h b/src/include/utils/tuplesort.h index da68f45acf2..29a21122bfc 100644 --- a/src/include/utils/tuplesort.h +++ b/src/include/utils/tuplesort.h @@ -352,6 +352,8 @@ extern bool tuplesort_gettuple_common(Tuplesortstate *state, bool forward, SortTuple *stup); extern bool tuplesort_skiptuples(Tuplesortstate *state, int64 ntuples, bool forward); +extern void tuplesort_set_allowbounded(Tuplesortstate *state, + bool allowbounded); extern void tuplesort_end(Tuplesortstate *state); extern void tuplesort_reset(Tuplesortstate *state); diff --git a/src/test/regress/expected/incremental_sort.out b/src/test/regress/expected/incremental_sort.out index 1e6e020fea8..93f8a032f85 100644 --- a/src/test/regress/expected/incremental_sort.out +++ b/src/test/regress/expected/incremental_sort.out @@ -722,6 +722,43 @@ select * from t left join (select * from (select * from t order by a) v order by (2 rows) rollback; +-- try rescan with a LIMIT node where some scans receive a LIMIT NULL and +-- others receive a LIMIT . +-- Ensure we get an Incremental Sort plan +explain (costs off) +select v.n, count(*) from (values (null::int), (3), (null::int)) v(n), +lateral (select unique1 from tenk1 where hundred < 2 + order by hundred, unique1 limit v.n) s +group by v.n +order by v.n; + QUERY PLAN +----------------------------------------------------------------------- + GroupAggregate + Group Key: "*VALUES*".column1 + -> Sort + Sort Key: "*VALUES*".column1 + -> Nested Loop + -> Values Scan on "*VALUES*" + -> Limit + -> Incremental Sort + Sort Key: tenk1.hundred, tenk1.unique1 + Presorted Key: tenk1.hundred + -> Index Scan using tenk1_hundred on tenk1 + Index Cond: (hundred < 2) +(12 rows) + +-- Check we get the correct results from that Plan +select v.n, count(*) from (values (null::int), (3), (null::int)) v(n), +lateral (select unique1 from tenk1 where hundred < 2 + order by hundred, unique1 limit v.n) s +group by v.n +order by v.n; + n | count +---+------- + 3 | 3 + | 400 +(2 rows) + -- Test EXPLAIN ANALYZE with both fullsort and presorted groups. select explain_analyze_without_memory('select * from (select * from t order by a) s order by a, b limit 70'); explain_analyze_without_memory diff --git a/src/test/regress/sql/incremental_sort.sql b/src/test/regress/sql/incremental_sort.sql index bbe658a7588..bf154c1ba3d 100644 --- a/src/test/regress/sql/incremental_sort.sql +++ b/src/test/regress/sql/incremental_sort.sql @@ -162,6 +162,25 @@ set local enable_sort = off; explain (costs off) select * from t left join (select * from (select * from t order by a) v order by a, b) s on s.a = t.a where t.a in (1, 2); select * from t left join (select * from (select * from t order by a) v order by a, b) s on s.a = t.a where t.a in (1, 2); rollback; + +-- try rescan with a LIMIT node where some scans receive a LIMIT NULL and +-- others receive a LIMIT . + +-- Ensure we get an Incremental Sort plan +explain (costs off) +select v.n, count(*) from (values (null::int), (3), (null::int)) v(n), +lateral (select unique1 from tenk1 where hundred < 2 + order by hundred, unique1 limit v.n) s +group by v.n +order by v.n; + +-- Check we get the correct results from that Plan +select v.n, count(*) from (values (null::int), (3), (null::int)) v(n), +lateral (select unique1 from tenk1 where hundred < 2 + order by hundred, unique1 limit v.n) s +group by v.n +order by v.n; + -- Test EXPLAIN ANALYZE with both fullsort and presorted groups. select explain_analyze_without_memory('select * from (select * from t order by a) s order by a, b limit 70'); select jsonb_pretty(explain_analyze_inc_sort_nodes_without_memory('select * from (select * from t order by a) s order by a, b limit 70')); -- 2.53.0