Don't remove surplus columns from GROUP BY for inheritance parents
authorDavid Rowley <drowley@postgresql.org>
Wed, 3 Jul 2019 11:46:26 +0000 (23:46 +1200)
committerDavid Rowley <drowley@postgresql.org>
Wed, 3 Jul 2019 11:46:26 +0000 (23:46 +1200)
commit388d05a5e1551e34e3908c2bfad21860adfcc5a7
treeb8449c37fb64d26c3619bbefd95812e9e71f1b1c
parent78aaffd285c4a66452004f8f2c4ff2771cee4cb4
Don't remove surplus columns from GROUP BY for inheritance parents

d4c3a156c added code to remove columns that were not part of a table's
PRIMARY KEY constraint from the GROUP BY clause when all the primary key
columns were present in the group by.  This is fine to do since we know
that there will only be one row per group coming from this relation.
However, the logic failed to consider inheritance parent relations.  These
can have child relations without a primary key, but even if they did, they
could duplicate one of the parent's rows or one from another child
relation.  In this case, those additional GROUP BY columns are required.

Fix this by disabling the optimization for inheritance parent tables.
In v11 and beyond, partitioned tables are fine since partitions cannot
overlap and before v11 partitioned tables could not have a primary key.

Reported-by: Manuel Rigger
Discussion: http://postgr.es/m/CA+u7OA7VLKf_vEr6kLF3MnWSA9LToJYncgpNX2tQ-oWzYCBQAw@mail.gmail.com
Backpatch-through: 9.6
src/backend/optimizer/plan/planner.c
src/test/regress/expected/aggregates.out
src/test/regress/sql/aggregates.sql