When working on a5a68dd6d I noticed that truncate_useless_pathkeys()
uses a bunch of different static helper functions that are mostly the
same as each other. Most of them differ only in which field of
PlannerInfo they look at.
The attached aims to clean that up by making 2 reusable functions.
I've also fixed a few other things I noticed along the way.
Here's a list of what I've changed:
1. Add count_common_leading_pathkeys_ordered() function to check for
leading common pathkeys and use that for sort_pathkeys,
window_pathkeys and window_pathkeys.
2. Add count_common_leading_pathkeys_unordered() to check for leading
common pathkeys that exist in any portion of the other list of
pathkeys. Use this for group_pathkeys and distinct_pathkeys.
3. Add some short-circuiting to truncate_useless_pathkeys() as there's
no point in trying to trim down the list when some other operation has
already figured out that it needs all of the pathkeys.
4. Remove the stray " if (root->group_pathkeys != NIL) return true"
from has_useful_pathkeys().
I thought #3 might be useful for queries such as:
SELECT DISTINCT <dozens of columns> FROM ... ORDER BY <dozens of columns>
as truncate_useless_pathkeys() may no longer have to call the more
costly count_common_leading_pathkeys_unordered() function if all the
pathkeys were deemed useful to the ORDER BY clause.
pathkeys_useful_for_merging() looked the most expensive, so I put that
last.
I don't have any proof in the form of benchmarks that #3 is
worthwhile, but I felt compelled to do it as it's simple and there are
certainly cases where it could save some amount of effort.
#4 isn't required as standard_qp_callback will set query_pathkeys if
there's a GROUP BY. I guess the person who wrote that code might have
been confused with the "might be able to use them for ordering"
comment. I changed that comment in the hopes nobody else falls for the
same mistake.
The patch saves about 4 dozen lines of code.
David