pgsql: Remove redundant grouping and DISTINCT columns. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Remove redundant grouping and DISTINCT columns.
Date
Msg-id E1pICO0-004JlS-Mr@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
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(-)


pgsql-committers by date:

Previous
From: Amit Kapila
Date:
Subject: pgsql: Display the leader apply worker's PID for parallel apply workers
Next
From: Tom Lane
Date:
Subject: pgsql: Get rid of the "new" and "old" entries in a view's rangetable.