From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Consider explicit incremental sort for Append and MergeAppend
Date: Tue, 08 Jul 2025 01:22:37 +0000
Message-ID: <E1uYx2b-005Blr-0P@gemulon.postgresql.org> (raw)
Consider explicit incremental sort for Append and MergeAppend
For an ordered Append or MergeAppend, we need to inject an explicit
sort into any subpath that is not already well enough ordered.
Currently, only explicit full sorts are considered; incremental sorts
are not yet taken into account.
In this patch, for subpaths of an ordered Append or MergeAppend, we
choose to use explicit incremental sort if it is enabled and there are
presorted keys.
The rationale is based on the assumption that incremental sort is
always faster than full sort when there are presorted keys, a premise
that has been applied in various parts of the code. In addition, the
current cost model tends to favor incremental sort as being cheaper
than full sort in the presence of presorted keys, making it reasonable
not to consider full sort in such cases.
No backpatch as this could result in plan changes.
Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Andrei Lepikhov <lepihov@gmail.com>
Reviewed-by: Robert Haas <robertmhaas@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_V7a2enTR+T3pOY_YZ-FU8ZsFYym2swOz4jNMqmSgyuw@mail.gmail.com
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/55a780e9476a753354a6db887e92125c7886ca6d
Modified Files
--------------
src/backend/optimizer/path/costsize.c | 54 ++++++++++++++-----
src/backend/optimizer/plan/createplan.c | 66 ++++++++++++++++++++---
src/backend/optimizer/util/pathnode.c | 73 +++++++++++++++++---------
src/include/optimizer/cost.h | 2 +-
src/test/regress/expected/incremental_sort.out | 40 ++++++++++++++
src/test/regress/expected/inherit.out | 10 ++--
src/test/regress/sql/incremental_sort.sql | 24 +++++++++
7 files changed, 217 insertions(+), 52 deletions(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-committers@postgresql.org
Cc: rguo@postgresql.org, pgsql-committers@lists.postgresql.org
Subject: Re: pgsql: Consider explicit incremental sort for Append and MergeAppend
In-Reply-To: <E1uYx2b-005Blr-0P@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox