Incremental Sort crash on a rescan that becomes bounded

Поиск
Список
Период
Сортировка
Искать

Incremental Sort crash on a rescan that becomes bounded

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi hackers,

This fails an assertion in tuplesort_set_bound():

set enable_seqscan = off;
set enable_bitmapscan = off;
select o.n, count(*) from (values (null::int), (3)) o(n),
lateral (select unique1 from tenk1 where hundred < 2
order by hundred, unique1 limit o.n) s
group by o.n;

Incremental Sort creates its sort states on the first scan and reuses
them on rescan. They only allow a bound if the first scan was bounded,
so a later scan that sets a bound fails the assertion. Since 17 such a
state uses a Bump context. A bounded sort frees the tuples it throws
away, which Bump can't do. So release builds can crash too.

The attached patch ends the sort state on such a rescan. The second
patch is the version for 15.

Thanks,
Shihao

Re: Incremental Sort crash on a rescan that becomes bounded

От:
David Rowley <dgrowleyml@gmail.com>
Дата:
Вложения:
On Sun, 15 Nov 2026 at 17:15, shihao zhong  wrote:
> The attached patch ends the sort state on such a rescan. The second
> patch is the version for 15.

Thanks for finding and reporting this.

Is the patch an AI fix? IMO, it's a pretty inefficient way to make it
work correctly. It seems wasteful to me to have to recreate the
tuplesort just because the boundedness requirements have changed. It
looks fine to me to adjust the sortopt flags before calling
tuplesort_reset(), as that calls tuplesort_free() and that does
"MemoryContextReset(state->base.sortcontext);". The tuplecontext
context is a sub-context of sortcontext, so gets destroyed then. When
tuplesort_reset() later calls tuplesort_begin_batch(), that will
create the Bump or AllocSet context for the tuples.

I don't see an issue adding a new external function in the
backbranches. Maybe in master we could adjust the tuplesort_reset()
function to take the allowbounded parameter.

Re: Incremental Sort crash on a rescan that becomes bounded

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi David, 

Thanks for reviewing my patch

> Is the patch an AI fix?...

I recently used AI to verify all the assertions and find this issue then
I picked that direction and had it draft V1. Then I  tested it myself.

> it's a pretty inefficient way to make it
> work correctly. It seems wasteful to me to have to recreate the
> tuplesort just because the boundedness requirements have changed

Agree... I copied ExecReScanSort() for V1 and did not think
about reusing the state.

I also thought about doing the tuplesort_reset direction a little bit,
but did not go with that direction.

> Maybe in master we could adjust the tuplesort_reset()
> function to take the allowbounded parameter.

I think we should do that.

I tested V2 locally and it looks good.

Thanks,
Shihao


Re: Incremental Sort crash on a rescan that becomes bounded

От:
David Rowley <dgrowleyml@gmail.com>
Дата:
On Tue, 17 Nov 2026 at 05:21, shihao zhong  wrote:
> > Maybe in master we could adjust the tuplesort_reset()
> > function to take the allowbounded parameter.
>
> I think we should do that.

I've done that in master only

> I tested V2 locally and it looks good.

... and pushed the fix to all supported branches.

David


FAQ