pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Remove redundant grouping and DISTINCT columns.
Date: Wed, 18 Jan 2023 17:38:09 +0000
Message-ID: <E1pICO0-004JlS-Mr@gemulon.postgresql.org> (raw)

Remove redundant grouping and DISTINCT columns.

Avoid explicitly grouping by columns that we know are redundant
for sorting, for example we need group by only one of x and y in
        SELECT ... WHERE x = y GROUP BY x, y
This comes up more often than you might think, as shown by the
changes in the regression tests.  It's nearly free to detect too,
since we are just piggybacking on the existing logic that detects
redundant pathkeys.  (In some of the existing plans that change,
it's visible that a sort step preceding the grouping step already
didn't bother to sort by the redundant column, making the old plan
a bit silly-looking.)

To do this, build processed_groupClause and processed_distinctClause
lists that omit any provably-redundant sort items, and consult those
not the originals where relevant.  This means that within the
planner, one should usually consult root->processed_groupClause or
root->processed_distinctClause if one wants to know which columns
are to be grouped on; but to check whether grouping or distinct-ing
is happening at all, check non-NIL-ness of parse->groupClause or
parse->distinctClause.  This is comparable to longstanding rules
about handling the HAVING clause, so I don't think it'll be a huge
maintenance problem.

nodeAgg.c also needs minor mods, because it's now possible to generate
AGG_PLAIN and AGG_SORTED Agg nodes with zero grouping columns.

Patch by me; thanks to Richard Guo and David Rowley for review.

Discussion: https://postgr.es/m/185315.1672179489@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/8d83a5d0a2673174dc478e707de1f502935391a5

Modified Files
--------------
contrib/pg_trgm/expected/pg_trgm.out              |   6 +-
contrib/postgres_fdw/deparse.c                    |   7 +
contrib/postgres_fdw/expected/postgres_fdw.out    |  38 ++-
contrib/postgres_fdw/postgres_fdw.c               |  12 +-
src/backend/executor/nodeAgg.c                    |   7 +-
src/backend/optimizer/path/pathkeys.c             |  50 +++-
src/backend/optimizer/plan/createplan.c           |   4 +-
src/backend/optimizer/plan/planner.c              | 291 +++++++++++++---------
src/backend/optimizer/prep/prepjointree.c         |   2 +
src/include/nodes/pathnodes.h                     |  28 +++
src/include/optimizer/paths.h                     |   5 +
src/test/regress/expected/aggregates.out          |   4 +-
src/test/regress/expected/groupingsets.out        |   3 +-
src/test/regress/expected/partition_aggregate.out |   9 +-
src/test/regress/expected/partition_join.out      |   6 +-
src/test/regress/expected/select_distinct.out     |   2 +-
16 files changed, 303 insertions(+), 171 deletions(-)



Message-ID: <E1pICO0-004JlS-Mr@gemulon.postgresql.org>
Permalink:  ../E1pICO0-004JlS-Mr@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1pICO0-004JlS-Mr@gemulon.postgresql.org

 · 

reply

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: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Remove redundant grouping and DISTINCT columns.
  In-Reply-To: <E1pICO0-004JlS-Mr@gemulon.postgresql.org>
  References: <E1pICO0-004JlS-Mr@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