postgresql/src
David Rowley d8a295389b Strip off ORDER BY/DISTINCT aggregate pathkeys in create_agg_path
1349d2790 added code to adjust the PlannerInfo.group_pathkeys so that
ORDER BY / DISTINCT aggregate functions could obtain pre-sorted inputs
to allow faster execution.  That commit forgot to adjust the pathkeys in
create_agg_path().  Some code in there assumed that it was always fine
to make the AggPath's pathkeys the same as its subpath's.  That seems to
have been ok up until 1349d2790, but since that commit adds pathkeys for
columns which are within the aggregate function, those columns won't be
available above the aggregate node.  This can result in "could not find
pathkey item to sort" during create_plan().

The fix here is to strip off the additional pathkeys added by
adjust_group_pathkeys_for_groupagg().  It seems that the pathkeys here
will only ever be group_pathkeys, so all we need to do is check if the
length of the pathkey list is longer than the num_groupby_pathkeys and
get rid of the additional ones only if we see extras.

Reported-by: Justin Pryzby
Reviewed-by: Richard Guo
Discussion: https://postgr.es/m/ZQhYYRhUxpW3PSf9%40telsasoft.com
Backpatch-through: 16, where 1349d2790 was introduced
2023-10-09 16:37:05 +13:00
..
backend Strip off ORDER BY/DISTINCT aggregate pathkeys in create_agg_path 2023-10-09 16:37:05 +13:00
bin Restore proper linkage of pg_char_to_encoding() and friends. 2023-10-07 12:08:10 -04:00
common Restore proper linkage of pg_char_to_encoding() and friends. 2023-10-07 12:08:10 -04:00
fe_utils Allow using syncfs() in frontend utilities. 2023-09-06 16:27:16 -07:00
include Remove debug_print_rel and replace usages with pprint 2023-10-09 15:53:16 +13:00
interfaces Add some const qualifiers 2023-09-26 11:28:57 +01:00
makefiles Remove --disable-thread-safety and related code. 2023-07-12 08:20:43 +12:00
pl Remove environment sensitivity in pl/tcl regression test. 2023-09-29 20:21:10 -04:00
port Meson: check for pg_config_paths.h left over from make 2023-08-24 10:33:53 +12:00
template Use unnamed POSIX semaphores on Cygwin. 2023-01-06 10:33:28 +13:00
test Add test for checking the line length of --help output 2023-10-06 11:56:19 +02:00
timezone Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
tools Return data from heap_page_prune via a struct. 2023-09-28 10:36:34 -04:00
tutorial Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
.gitignore Convert cvsignore to gitignore, and add .gitignore for build targets. 2010-09-22 12:57:04 +02:00
DEVELOPERS Replace a couple of references to files that no longer exist in the source 2009-05-04 08:08:47 +00:00
Makefile Integrate pg_bsd_indent into our build/test infrastructure. 2023-02-12 12:22:21 -05:00
Makefile.global.in Suppress macOS warnings about duplicate libraries in link commands. 2023-09-29 14:07:30 -04:00
Makefile.shlib Stop using "-multiply_defined suppress" on macOS. 2023-09-26 21:06:21 -04:00
meson.build Integrate pg_bsd_indent into our build/test infrastructure. 2023-02-12 12:22:21 -05:00
nls-global.mk Fix for make unportability 2022-07-13 09:15:01 +02:00