diff --git a/src/backend/optimizer/path/pathkeys.c b/src/backend/optimizer/path/pathkeys.c index 928bbae8bd..a1d3f1dd3c 100644 --- a/src/backend/optimizer/path/pathkeys.c +++ b/src/backend/optimizer/path/pathkeys.c @@ -980,8 +980,8 @@ find_mergeclauses_for_outer_pathkeys(PlannerInfo *root, { PathKey *pathkey = (PathKey *) lfirst(i); EquivalenceClass *pathkey_ec = pathkey->pk_eclass; - List *matched_restrictinfos = NIL; ListCell *j; + bool found = false; /*---------- * A mergejoin clause matches a pathkey if it has the same EC. @@ -1027,8 +1027,12 @@ find_mergeclauses_for_outer_pathkeys(PlannerInfo *root, clause_ec = rinfo->outer_is_left ? rinfo->left_ec : rinfo->right_ec; - if (clause_ec == pathkey_ec) - matched_restrictinfos = lappend(matched_restrictinfos, rinfo); + + if (clause_ec != pathkey_ec) + continue; + + mergeclauses = lappend(mergeclauses, rinfo); + found = true; } /* @@ -1036,14 +1040,8 @@ find_mergeclauses_for_outer_pathkeys(PlannerInfo *root, * sort-key positions in the pathkeys are useless. (But we can still * mergejoin if we found at least one mergeclause.) */ - if (matched_restrictinfos == NIL) + if (!found) break; - - /* - * If we did find usable mergeclause(s) for this sort-key position, - * add them to result list. - */ - mergeclauses = list_concat(mergeclauses, matched_restrictinfos); } return mergeclauses;