Fix sorting on extra grouping keys of child relations

A Var that is needed by a join but is not a GROUP BY expression
becomes an extra grouping key for partial aggregation.  The
EquivalenceClass for sorting on it is created when the grouped paths
are generated, which is after the children of appendrels and
partitionwise joins have received their EC members, so it has no child
members.  Sort-based partial aggregation on a child relation then
failed with "could not find pathkey item to sort", as with a
partitionwise join whose join clause includes a non-equality
condition.

To fix, add the missing child members when the grouping pathkeys of a
child relation are built.  This is done by the new function
add_child_rel_pathkey_equivalences(), which handles both child base
relations and child joins, including keys that are nullable by an
outer join within the child join.

Reported-by: Robert Haas <[email protected]>
Author: Richard Guo <[email protected]>
Discussion: 
https://postgr.es/m/ca+tgmob7ism9ykrm44vjuduacchw-fy54mv5njptztl9uny...@mail.gmail.com
Backpatch-through: 19

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/94a907baf3be0b875af2277e08b776ab9577890a

Modified Files
--------------
src/backend/optimizer/path/allpaths.c         |   4 +
src/backend/optimizer/path/equivclass.c       |  78 +++++++++
src/include/optimizer/paths.h                 |   3 +
src/test/regress/expected/eager_aggregate.out | 229 ++++++++++++++++++++++++++
src/test/regress/sql/eager_aggregate.sql      |  27 +++
5 files changed, 341 insertions(+)

Reply via email to