pgsql: Reordering DISTINCT keys to match input path's pathkeys

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Reordering DISTINCT keys to match input path's pathkeys
Date: 2024-11-26 00:28:52
Message-ID: E1tFjRk-003XIl-L7@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Reordering DISTINCT keys to match input path's pathkeys

The ordering of DISTINCT items is semantically insignificant, so we
can reorder them as needed. In fact, in the parser, we absorb the
sorting semantics of the sortClause as much as possible into the
distinctClause, ensuring that one clause is a prefix of the other.
This can help avoid a possible need to re-sort.

In this commit, we attempt to adjust the DISTINCT keys to match the
input path's pathkeys. This can likewise help avoid re-sorting, or
allow us to use incremental-sort to save efforts.

For DISTINCT ON expressions, the parser already ensures that they
match the initial ORDER BY expressions. When reordering the DISTINCT
keys, we must ensure that the resulting pathkey list matches the
initial distinctClause pathkeys.

This introduces a new GUC, enable_distinct_reordering, which allows
the optimization to be disabled if needed.

Author: Richard Guo
Reviewed-by: Andrei Lepikhov
Discussion: https://postgr.es/m/CAMbWs48dR26cCcX0f=8bja2JKQPcU64136kHk=xekHT9xschiQ@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a8ccf4e93a7eeaae66007bbf78cf9183ceb1b371

Modified Files
--------------
doc/src/sgml/config.sgml | 14 +
src/backend/optimizer/path/pathkeys.c | 38 ++
src/backend/optimizer/plan/planner.c | 456 +++++++++++++----------
src/backend/utils/misc/guc_tables.c | 10 +
src/backend/utils/misc/postgresql.conf.sample | 1 +
src/include/optimizer/optimizer.h | 1 +
src/test/regress/expected/select_distinct.out | 132 +++++++
src/test/regress/expected/select_distinct_on.out | 125 +++++++
src/test/regress/expected/sysviews.out | 3 +-
src/test/regress/sql/select_distinct.sql | 53 +++
src/test/regress/sql/select_distinct_on.sql | 40 ++
11 files changed, 672 insertions(+), 201 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2024-11-26 00:45:51 pgsql: pg_amcheck: Use CppAsString2() for relkind and relpersistence in
Previous Message Tom Lane 2024-11-25 23:10:00 pgsql: Fix NULLIF()'s handling of read-write expanded objects.