Simplify PathKey checking code
authorDavid Rowley <drowley@postgresql.org>
Thu, 15 Feb 2024 05:01:28 +0000 (18:01 +1300)
committerDavid Rowley <drowley@postgresql.org>
Thu, 15 Feb 2024 05:01:28 +0000 (18:01 +1300)
pathkeys_useful_for_ordering() contained some needless checks to return
0 when either root->query_pathkeys or pathkeys lists were empty.  This is
already handled by pathkeys_count_contained_in(), so let's have it do the
work instead of having redundant checks.

Similarly, in pathkeys_useful_for_grouping(), checking pathkeys is an
empty list just before looping over it isn't required.  Technically,
neither is the list empty check for group_pathkeys, but I felt a bit
more work would have to be done to get the equivalent behavior if we'd
left it up to the foreach loop to call list_member_ptr().

This was noticed by Andy while he was reviewing a patch to improve the
UNION planner.  Since that patch adds another function similar to
pathkeys_useful_for_ordering() and since I wasn't planning to copy these
redundant checks over to the new function, let's adjust the existing
code so that both functions will be consistent.

Author: Andy Fan
Discussion: https://postgr.es/m/87o7cti48f.fsf@163.com

src/backend/optimizer/path/pathkeys.c

index bcc1b7a9ebe213549a6f9f2cff9cb218c584cf0d..da6f457a3bf45fb4dad0c87d88f5c620224c4f0f 100644 (file)
@@ -2143,12 +2143,6 @@ pathkeys_useful_for_ordering(PlannerInfo *root, List *pathkeys)
 {
    int         n_common_pathkeys;
 
-   if (root->query_pathkeys == NIL)
-       return 0;               /* no special ordering requested */
-
-   if (pathkeys == NIL)
-       return 0;               /* unordered path */
-
    (void) pathkeys_count_contained_in(root->query_pathkeys, pathkeys,
                                       &n_common_pathkeys);
 
@@ -2184,10 +2178,6 @@ pathkeys_useful_for_grouping(PlannerInfo *root, List *pathkeys)
    if (root->group_pathkeys == NIL)
        return 0;
 
-   /* unordered path */
-   if (pathkeys == NIL)
-       return 0;
-
    /* walk the pathkeys and search for matching group key */
    foreach(key, pathkeys)
    {