| From: | David Rowley <drowley(at)postgresql(dot)org> |
|---|---|
| To: | pgsql-committers(at)lists(dot)postgresql(dot)org |
| Subject: | pgsql: Fix possible crash with Incremental Sort when boundedness change |
| Date: | 2026-11-18 01:14:39 |
| Message-ID: | E1xTUFy-00000000ALJ-1pEI@gemulon.postgresql.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-committers |
Fix possible crash 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. If the initial tuplesort was created without
TUPLESORT_ALLOWBOUNDED, and a subsequent scan became bounded, then
tuplesort would mistakenly store the tuples in a Bump context, but
bounded sorts pfree() tuples, which Bump does not support as it does not
use chunk headers.
This can happen when a LIMIT node's tuple limit isn't a Const, as the
actual limit may be known for some scans but not others. If the LIMIT
isn't known for the first scan, then the tuplesorts will be initialized
without the TUPLESORT_ALLOWBOUNDED flag, and if a later scan obtains a
bound, then the tuplesort will mistakenly use a Bump context type.
Here we fix this by adding a new external function for tuplesort which
allows the TUPLESORT_ALLOWBOUNDED flag to be changed. 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. Ideally tuplesort_reset() would grow an extra
boolean parameter to control this on reset, but we can't change the
signature of that function in the backbranches.
Versions prior to v17 didn't have 6ed83d5fa or Bump memory contexts.
Non-bounded sorts use Generation contexts in v15 and v16, which do
support pfree(). This change is still required there to fix an Assert
failure in tuplesort_set_bound() and to resolve inefficient memory
usage as the Generation context is not well suited for pfreeing chunks
in an arbitrary order. AllocSet is better suited as it can reuse pfree'd
memory.
Reported-by: shihao zhong <zhong950419(at)gmail(dot)com>
Author: David Rowley <dgrowleyml(at)gmail(dot)com>
Tested-by: shihao zhong <zhong950419(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAGRkXqRXMiR9EBxBT7VEF0jF_9NJAxVcGrtRdsTZOfNkHfVtsg@mail.gmail.com
Backpatch-through: 15
Branch
------
REL_19_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/882f6b32f4ae42d32d63730985658040e88e7dc0
Modified Files
--------------
src/backend/executor/nodeIncrementalSort.c | 10 ++++++-
src/backend/utils/sort/tuplesort.c | 16 +++++++++++
src/include/utils/tuplesort.h | 2 ++
src/test/regress/expected/incremental_sort.out | 37 ++++++++++++++++++++++++++
src/test/regress/sql/incremental_sort.sql | 19 +++++++++++++
5 files changed, 83 insertions(+), 1 deletion(-)
| From | Date | Subject | |
|---|---|---|---|
| Next Message | David Rowley | 2026-11-18 01:14:53 | pgsql: Fix possible crash with Incremental Sort when boundedness change |
| Previous Message | David Rowley | 2026-11-18 01:14:27 | pgsql: Fix possible crash with Incremental Sort when boundedness change |