postgresql/src/tools
Richard Guo a1b754558a Consider opfamily and collation when removing redundant GROUP BY columns
remove_useless_groupby_columns() uses a relation's unique indexes to
prove that some GROUP BY columns are functionally dependent on others,
and so can be dropped from the GROUP BY clause.  The match between
index columns and GROUP BY columns was done by attno alone, ignoring
two equality-relation issues.

A type may belong to multiple btree opfamilies whose notions of
equality differ.  The record type, for instance, has record_ops
(per-field equality) and record_image_ops (bytewise equality).  A
unique index under one opfamily does not prove uniqueness under the
equality used by GROUP BY when the SortGroupClause's eqop comes from a
different opfamily.

Likewise, since nondeterministic collations were introduced in PG 12,
two collations may disagree on equality, and a unique index under one
collation does not prove uniqueness under another.

In either case, rows that the index considers distinct can collapse
into a single GROUP BY group, taking ungrouped columns of differing
values with them, so the planner drops a column that is not in fact
functionally dependent and produces wrong results.

Fix by requiring, for each unique-index key column, that some GROUP BY
item on the same column has an eqop in the index's opfamily and a
collation that agrees on equality with the index's collation.  This
mirrors the combined check relation_has_unique_index_for() applies to
join clauses.

This is a v18 regression: commit bd10ec529 extended
remove_useless_groupby_columns() from primary-key constraints to
arbitrary unique indexes.  Before that, the function consulted only
primary keys, whose enforcement index is required by parse_utilcmd.c
to use the default opclass and the column's declared collation, so
neither mismatch could arise.  Back-patch to v18 only.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49t6uArWoTT-cHY+nhsi23nJJKcF9Xb9cYGzaZ9kNJ98g@mail.gmail.com
Backpatch-through: 18
2026-05-08 12:45:51 +09:00
..
ci ci: Improve OpenBSD core dump backtrace handling. 2025-11-06 21:14:05 +13:00
editors Make Emacs perl-mode indent more like perltidy. 2019-01-13 11:32:31 -08:00
ifaddrs Update copyright for 2026 2026-01-01 13:24:10 -05:00
perlcheck Activate perlcritic InputOutput::RequireCheckedSyscalls and fix resulting warnings 2024-03-19 07:09:31 +01:00
pg_bsd_indent Fix a set of typos and grammar issues across the tree 2026-04-21 14:46:22 +09:00
pginclude libpq: Split PGOAUTHDEBUG=UNSAFE into multiple options 2026-04-07 08:15:14 -07:00
pgindent Consider opfamily and collation when removing redundant GROUP BY columns 2026-05-08 12:45:51 +09:00
add_commit_links.pl Update copyright for 2026 2026-01-01 13:24:10 -05:00
ccsym tools/ccsym: update for modern versions of gcc 2015-01-20 13:02:58 -05:00
check_bison_recursion.pl Update copyright for 2026 2026-01-01 13:24:10 -05:00
copyright.pl Update copyright for 2026 2026-01-01 13:24:10 -05:00
darwin_sysroot Move darwin sysroot determination into separate file 2022-09-01 16:54:19 -07:00
find_badmacros Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
find_meson meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
find_static Fix trap in a few shell scripts 2022-09-20 18:50:16 +02:00
find_typedef Refer to OS X as "macOS", except for the port name which is still "darwin". 2016-09-25 15:40:57 -04:00
gen_export.pl Restore AIX support. 2026-02-23 13:34:22 -05:00
gen_keywordlist.pl Update copyright for 2026 2026-01-01 13:24:10 -05:00
generate_editorconfig.py Add script to keep .editorconfig in sync with .gitattributes 2025-02-01 10:09:45 +01:00
git-external-diff Preserve information on use of git-external-diff 2018-05-24 23:45:31 +09:30
git_changelog Update copyright for 2026 2026-01-01 13:24:10 -05:00
install_files meson: make install_test_files more generic, rename to install_files 2023-03-23 21:20:18 -07:00
make_ctags Fix make_etags breakage on certain platforms. 2023-06-14 11:02:50 +09:00
make_etags Fix make_etags failure on Mac. 2023-02-15 09:52:42 +09:00
make_mkid Add another pgdefine path check, and a cvs-git change. 2011-08-26 21:52:35 -04:00
mark_pgdllimport.pl Update copyright for 2026 2026-01-01 13:24:10 -05:00
msvc_gendef.pl MSVC: Support building for AArch64. 2026-01-07 13:42:57 -06:00
PerfectHash.pm Update copyright for 2026 2026-01-01 13:24:10 -05:00
pgflex pgflex: propagate environment to flex subprocess 2025-06-30 12:24:48 +02:00
pgtest pgtest: fix spacing 2023-08-14 14:03:29 -04:00
rcgen meson: Add windows resource files 2022-10-05 09:56:05 -07:00
RELEASE_CHANGES Remove gen_node_support.pl's ad-hoc ABI stability check. 2026-04-21 10:58:00 -04:00
testwrap Add 'make check-tests' behavior to the meson based builds 2025-11-21 17:12:22 -05:00
valgrind.supp Fix typos and inconsistencies in code and comments 2026-01-05 09:19:15 +09:00
version_stamp.pl Update copyright for 2026 2026-01-01 13:24:10 -05:00
win32tzlist.pl Update copyright for 2026 2026-01-01 13:24:10 -05:00