From: Tomas Vondra <tomas.vondra@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Implement Incremental Sort
Date: Mon, 06 Apr 2020 19:36:15 +0000
Message-ID: <E1jLXXb-0000S2-Ho@gemulon.postgresql.org> (raw)
Implement Incremental Sort
Incremental Sort is an optimized variant of multikey sort for cases when
the input is already sorted by a prefix of the requested sort keys. For
example when the relation is already sorted by (key1, key2) and we need
to sort it by (key1, key2, key3) we can simply split the input rows into
groups having equal values in (key1, key2), and only sort/compare the
remaining column key3.
This has a number of benefits:
- Reduced memory consumption, because only a single group (determined by
values in the sorted prefix) needs to be kept in memory. This may also
eliminate the need to spill to disk.
- Lower startup cost, because Incremental Sort produce results after each
prefix group, which is beneficial for plans where startup cost matters
(like for example queries with LIMIT clause).
We consider both Sort and Incremental Sort, and decide based on costing.
The implemented algorithm operates in two different modes:
- Fetching a minimum number of tuples without check of equality on the
prefix keys, and sorting on all columns when safe.
- Fetching all tuples for a single prefix group and then sorting by
comparing only the remaining (non-prefix) keys.
We always start in the first mode, and employ a heuristic to switch into
the second mode if we believe it's beneficial - the goal is to minimize
the number of unnecessary comparions while keeping memory consumption
below work_mem.
This is a very old patch series. The idea was originally proposed by
Alexander Korotkov back in 2013, and then revived in 2017. In 2018 the
patch was taken over by James Coleman, who wrote and rewrote most of the
current code.
There were many reviewers/contributors since 2013 - I've done my best to
pick the most active ones, and listed them in this commit message.
Author: James Coleman, Alexander Korotkov
Reviewed-by: Tomas Vondra, Andreas Karlsson, Marti Raudsepp, Peter Geoghegan, Robert Haas, Thomas Munro, Antonin Houska, Andres Freund, Alexander Kuzmenkov
Discussion: https://postgr.es/m/CAPpHfdscOX5an71nHd8WSUH6GNOCf=V7wgDaTXdDd9=goN-gfA@mail.gmail.com
Discussion: https://postgr.es/m/CAPpHfds1waRZ=NOmueYq0sx1ZSCnt+5QJvizT8ndT2=etZEeAQ@mail.gmail.com
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/d2d8a229bc58a2014dce1c7a4fcdb6c5ab9fb8da
Modified Files
--------------
doc/src/sgml/config.sgml | 14 +
doc/src/sgml/perform.sgml | 42 +-
src/backend/commands/explain.c | 239 +++-
src/backend/executor/Makefile | 1 +
src/backend/executor/execAmi.c | 14 +
src/backend/executor/execParallel.c | 18 +
src/backend/executor/execProcnode.c | 34 +
src/backend/executor/nodeIncrementalSort.c | 1263 +++++++++++++++++
src/backend/executor/nodeSort.c | 3 +-
src/backend/nodes/copyfuncs.c | 49 +-
src/backend/nodes/outfuncs.c | 25 +-
src/backend/nodes/readfuncs.c | 37 +-
src/backend/optimizer/path/allpaths.c | 4 +
src/backend/optimizer/path/costsize.c | 178 ++-
src/backend/optimizer/path/pathkeys.c | 72 +-
src/backend/optimizer/plan/createplan.c | 120 +-
src/backend/optimizer/plan/planner.c | 85 +-
src/backend/optimizer/plan/setrefs.c | 1 +
src/backend/optimizer/plan/subselect.c | 1 +
src/backend/optimizer/util/pathnode.c | 51 +
src/backend/utils/misc/guc.c | 9 +
src/backend/utils/misc/postgresql.conf.sample | 1 +
src/backend/utils/sort/tuplesort.c | 306 ++++-
src/include/executor/execdebug.h | 2 +
src/include/executor/nodeIncrementalSort.h | 28 +
src/include/nodes/execnodes.h | 80 ++
src/include/nodes/nodes.h | 3 +
src/include/nodes/pathnodes.h | 9 +
src/include/nodes/plannodes.h | 10 +
src/include/optimizer/cost.h | 6 +
src/include/optimizer/pathnode.h | 6 +
src/include/optimizer/paths.h | 1 +
src/include/utils/tuplesort.h | 16 +-
.../expected/drop-index-concurrently-1.out | 2 +-
src/test/regress/expected/incremental_sort.out | 1441 ++++++++++++++++++++
src/test/regress/expected/partition_aggregate.out | 2 +
src/test/regress/expected/sysviews.out | 3 +-
src/test/regress/parallel_schedule | 2 +-
src/test/regress/serial_schedule | 1 +
src/test/regress/sql/incremental_sort.sql | 213 +++
src/test/regress/sql/partition_aggregate.sql | 2 +
41 files changed, 4239 insertions(+), 155 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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: tomas.vondra@postgresql.org
Subject: Re: pgsql: Implement Incremental Sort
In-Reply-To: <E1jLXXb-0000S2-Ho@gemulon.postgresql.org>
References: <E1jLXXb-0000S2-Ho@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