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 <[email protected]>
Author: David Rowley <[email protected]>
Tested-by: shihao zhong <[email protected]>
Discussion: 
https://postgr.es/m/cagrkxqrxmir9ebxbt7vef0jf_9njaxvcgrtrdstzofnkhfv...@mail.gmail.com
Backpatch-through: 15

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/4af67e0e97bce148d5b0bbb89f80f7c1ed94d10b

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(-)

Reply via email to