Fix usage of aggregate pathkeys in group_keys_reorder_by_pathkeys()
Alexander Korotkov <akorotkov@postgresql.org>
Fix usage of aggregate pathkeys in group_keys_reorder_by_pathkeys() group_keys_reorder_by_pathkeys() function searched for matching pathkeys within root->group_pathkeys. That could lead to picking an aggregate pathkey and using its pathkey->pk_eclass->ec_sortref as an argument of get_sortgroupref_clause_noerr(). Given that ec_sortref of an aggregate pathkey references aggregate targetlist not query targetlist, this leads to incorrect query optimization. Fix this by looking for matching pathkeys only within the first num_groupby_pathkeys pathkeys. Reported-by: David G. Johnston Discussion: https://postgr.es/m/CAKFQuwY3Ek%3DcLThgd8FdaSc5JRDVt0FaV00gMcWra%2BTAR4gGUw%40mail.gmail.com Author: Andrei Lepikhov, Alexander Korotkov
Files
| Path | Change | +/− |
|---|---|---|
| src/backend/optimizer/path/pathkeys.c | modified | +21 −4 |
| src/test/regress/expected/aggregates.out | modified | +24 −0 |
| src/test/regress/sql/aggregates.sql | modified | +19 −0 |
Discussion
- v17 Possible Union All Bug 13 messages · 2024-01-23 → 2024-02-08