1 /*-------------------------------------------------------------------------
4 * Utilities for matching and building path keys
6 * See src/backend/optimizer/README for a great deal of information about
7 * the nature and use of path keys.
10 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
14 * $PostgreSQL: pgsql/src/backend/optimizer/path/pathkeys.c,v 1.98 2009/07/17 23:19:34 tgl Exp $
16 *-------------------------------------------------------------------------
20 #include "access/skey.h"
21 #include "catalog/pg_type.h"
22 #include "nodes/makefuncs.h"
23 #include "nodes/nodeFuncs.h"
24 #include "nodes/plannodes.h"
25 #include "optimizer/clauses.h"
26 #include "optimizer/pathnode.h"
27 #include "optimizer/paths.h"
28 #include "optimizer/tlist.h"
29 #include "parser/parsetree.h"
30 #include "utils/lsyscache.h"
33 static PathKey *makePathKey(EquivalenceClass *eclass, Oid opfamily,
34 int strategy, bool nulls_first);
35 static PathKey *make_canonical_pathkey(PlannerInfo *root,
36 EquivalenceClass *eclass, Oid opfamily,
37 int strategy, bool nulls_first);
38 static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys);
39 static PathKey *make_pathkey_from_sortinfo(PlannerInfo *root,
40 Expr *expr, Oid ordering_op,
44 static Var *find_indexkey_var(PlannerInfo *root, RelOptInfo *rel,
46 static bool right_merge_direction(PlannerInfo *root, PathKey *pathkey);
49 /****************************************************************************
50 * PATHKEY CONSTRUCTION AND REDUNDANCY TESTING
51 ****************************************************************************/
55 * create a PathKey node
57 * This does not promise to create a canonical PathKey, it's merely a
58 * convenience routine to build the specified node.
61 makePathKey(EquivalenceClass *eclass, Oid opfamily,
62 int strategy, bool nulls_first)
64 PathKey *pk = makeNode(PathKey);
66 pk->pk_eclass = eclass;
67 pk->pk_opfamily = opfamily;
68 pk->pk_strategy = strategy;
69 pk->pk_nulls_first = nulls_first;
75 * make_canonical_pathkey
76 * Given the parameters for a PathKey, find any pre-existing matching
77 * pathkey in the query's list of "canonical" pathkeys. Make a new
78 * entry if there's not one already.
80 * Note that this function must not be used until after we have completed
81 * merging EquivalenceClasses.
84 make_canonical_pathkey(PlannerInfo *root,
85 EquivalenceClass *eclass, Oid opfamily,
86 int strategy, bool nulls_first)
90 MemoryContext oldcontext;
92 /* The passed eclass might be non-canonical, so chase up to the top */
93 while (eclass->ec_merged)
94 eclass = eclass->ec_merged;
96 foreach(lc, root->canon_pathkeys)
98 pk = (PathKey *) lfirst(lc);
99 if (eclass == pk->pk_eclass &&
100 opfamily == pk->pk_opfamily &&
101 strategy == pk->pk_strategy &&
102 nulls_first == pk->pk_nulls_first)
107 * Be sure canonical pathkeys are allocated in the main planning context.
108 * Not an issue in normal planning, but it is for GEQO.
110 oldcontext = MemoryContextSwitchTo(root->planner_cxt);
112 pk = makePathKey(eclass, opfamily, strategy, nulls_first);
113 root->canon_pathkeys = lappend(root->canon_pathkeys, pk);
115 MemoryContextSwitchTo(oldcontext);
121 * pathkey_is_redundant
122 * Is a pathkey redundant with one already in the given list?
124 * Both the given pathkey and the list members must be canonical for this
125 * to work properly. We detect two cases:
127 * 1. If the new pathkey's equivalence class contains a constant, and isn't
128 * below an outer join, then we can disregard it as a sort key. An example:
129 * SELECT ... WHERE x = 42 ORDER BY x, y;
130 * We may as well just sort by y. Note that because of opfamily matching,
131 * this is semantically correct: we know that the equality constraint is one
132 * that actually binds the variable to a single value in the terms of any
133 * ordering operator that might go with the eclass. This rule not only lets
134 * us simplify (or even skip) explicit sorts, but also allows matching index
135 * sort orders to a query when there are don't-care index columns.
137 * 2. If the new pathkey's equivalence class is the same as that of any
138 * existing member of the pathkey list, then it is redundant. Some examples:
139 * SELECT ... ORDER BY x, x;
140 * SELECT ... ORDER BY x, x DESC;
141 * SELECT ... WHERE x = y ORDER BY x, y;
142 * In all these cases the second sort key cannot distinguish values that are
143 * considered equal by the first, and so there's no point in using it.
144 * Note in particular that we need not compare opfamily (all the opfamilies
145 * of the EC have the same notion of equality) nor sort direction.
147 * Because the equivclass.c machinery forms only one copy of any EC per query,
148 * pointer comparison is enough to decide whether canonical ECs are the same.
151 pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
153 EquivalenceClass *new_ec = new_pathkey->pk_eclass;
156 /* Assert we've been given canonical pathkeys */
157 Assert(!new_ec->ec_merged);
159 /* Check for EC containing a constant --- unconditionally redundant */
160 if (EC_MUST_BE_REDUNDANT(new_ec))
163 /* If same EC already used in list, then redundant */
164 foreach(lc, pathkeys)
166 PathKey *old_pathkey = (PathKey *) lfirst(lc);
168 /* Assert we've been given canonical pathkeys */
169 Assert(!old_pathkey->pk_eclass->ec_merged);
171 if (new_ec == old_pathkey->pk_eclass)
179 * canonicalize_pathkeys
180 * Convert a not-necessarily-canonical pathkeys list to canonical form.
182 * Note that this function must not be used until after we have completed
183 * merging EquivalenceClasses.
186 canonicalize_pathkeys(PlannerInfo *root, List *pathkeys)
188 List *new_pathkeys = NIL;
193 PathKey *pathkey = (PathKey *) lfirst(l);
194 EquivalenceClass *eclass;
197 /* Find the canonical (merged) EquivalenceClass */
198 eclass = pathkey->pk_eclass;
199 while (eclass->ec_merged)
200 eclass = eclass->ec_merged;
203 * If we can tell it's redundant just from the EC, skip.
204 * pathkey_is_redundant would notice that, but we needn't even bother
205 * constructing the node...
207 if (EC_MUST_BE_REDUNDANT(eclass))
210 /* OK, build a canonicalized PathKey struct */
211 cpathkey = make_canonical_pathkey(root,
213 pathkey->pk_opfamily,
214 pathkey->pk_strategy,
215 pathkey->pk_nulls_first);
217 /* Add to list unless redundant */
218 if (!pathkey_is_redundant(cpathkey, new_pathkeys))
219 new_pathkeys = lappend(new_pathkeys, cpathkey);
225 * make_pathkey_from_sortinfo
226 * Given an expression, a sortop, and a nulls-first flag, create
227 * a PathKey. If canonicalize = true, the result is a "canonical"
228 * PathKey, otherwise not. (But note it might be redundant anyway.)
230 * If the PathKey is being generated from a SortGroupClause, sortref should be
231 * the SortGroupClause's SortGroupRef; otherwise zero.
233 * canonicalize should always be TRUE after EquivalenceClass merging has
234 * been performed, but FALSE if we haven't done EquivalenceClass merging yet.
237 make_pathkey_from_sortinfo(PlannerInfo *root,
238 Expr *expr, Oid ordering_op,
248 EquivalenceClass *eclass;
251 * An ordering operator fully determines the behavior of its opfamily, so
252 * could only meaningfully appear in one family --- or perhaps two if one
253 * builds a reverse-sort opfamily, but there's not much point in that
254 * anymore. But EquivalenceClasses need to contain opfamily lists based
255 * on the family membership of equality operators, which could easily be
256 * bigger. So, look up the equality operator that goes with the ordering
257 * operator (this should be unique) and get its membership.
260 /* Find the operator in pg_amop --- failure shouldn't happen */
261 if (!get_ordering_op_properties(ordering_op,
262 &opfamily, &opcintype, &strategy))
263 elog(ERROR, "operator %u is not a valid ordering operator",
265 /* Get matching equality operator */
266 equality_op = get_opfamily_member(opfamily,
269 BTEqualStrategyNumber);
270 if (!OidIsValid(equality_op)) /* shouldn't happen */
271 elog(ERROR, "could not find equality operator for ordering operator %u",
273 opfamilies = get_mergejoin_opfamilies(equality_op);
274 if (!opfamilies) /* certainly should find some */
275 elog(ERROR, "could not find opfamilies for ordering operator %u",
279 * When dealing with binary-compatible opclasses, we have to ensure that
280 * the exposed type of the expression tree matches the declared input type
281 * of the opclass, except when that is a polymorphic type (compare the
282 * behavior of parse_coerce.c). This ensures that we can correctly match
283 * the indexkey or sortclause expression to other expressions we find in
284 * the query, because arguments of ordinary operator expressions will be
285 * cast that way. (We have to do this for indexkeys because they are
286 * represented without any explicit relabel in pg_index, and for sort
287 * clauses because the parser is likewise cavalier about putting relabels
290 if (exprType((Node *) expr) != opcintype &&
291 !IsPolymorphicType(opcintype))
293 /* Strip any existing RelabelType, and add a new one if needed */
294 while (expr && IsA(expr, RelabelType))
295 expr = (Expr *) ((RelabelType *) expr)->arg;
296 if (exprType((Node *) expr) != opcintype)
297 expr = (Expr *) makeRelabelType(expr,
303 /* Now find or create a matching EquivalenceClass */
304 eclass = get_eclass_for_sort_expr(root, expr, opcintype, opfamilies,
307 /* And finally we can find or create a PathKey node */
309 return make_canonical_pathkey(root, eclass, opfamily,
310 strategy, nulls_first);
312 return makePathKey(eclass, opfamily, strategy, nulls_first);
316 /****************************************************************************
317 * PATHKEY COMPARISONS
318 ****************************************************************************/
322 * Compare two pathkeys to see if they are equivalent, and if not whether
323 * one is "better" than the other.
325 * This function may only be applied to canonicalized pathkey lists.
326 * In the canonical representation, pathkeys can be checked for equality
327 * by simple pointer comparison.
330 compare_pathkeys(List *keys1, List *keys2)
336 * Fall out quickly if we are passed two identical lists. This mostly
337 * catches the case where both are NIL, but that's common enough to
341 return PATHKEYS_EQUAL;
343 forboth(key1, keys1, key2, keys2)
345 PathKey *pathkey1 = (PathKey *) lfirst(key1);
346 PathKey *pathkey2 = (PathKey *) lfirst(key2);
349 * XXX would like to check that we've been given canonicalized input,
350 * but PlannerInfo not accessible here...
353 Assert(list_member_ptr(root->canon_pathkeys, pathkey1));
354 Assert(list_member_ptr(root->canon_pathkeys, pathkey2));
357 if (pathkey1 != pathkey2)
358 return PATHKEYS_DIFFERENT; /* no need to keep looking */
362 * If we reached the end of only one list, the other is longer and
363 * therefore not a subset.
366 return PATHKEYS_BETTER1; /* key1 is longer */
368 return PATHKEYS_BETTER2; /* key2 is longer */
369 return PATHKEYS_EQUAL;
373 * pathkeys_contained_in
374 * Common special case of compare_pathkeys: we just want to know
375 * if keys2 are at least as well sorted as keys1.
378 pathkeys_contained_in(List *keys1, List *keys2)
380 switch (compare_pathkeys(keys1, keys2))
383 case PATHKEYS_BETTER2:
392 * get_cheapest_path_for_pathkeys
393 * Find the cheapest path (according to the specified criterion) that
394 * satisfies the given pathkeys. Return NULL if no such path.
396 * 'paths' is a list of possible paths that all generate the same relation
397 * 'pathkeys' represents a required ordering (already canonicalized!)
398 * 'cost_criterion' is STARTUP_COST or TOTAL_COST
401 get_cheapest_path_for_pathkeys(List *paths, List *pathkeys,
402 CostSelector cost_criterion)
404 Path *matched_path = NULL;
409 Path *path = (Path *) lfirst(l);
412 * Since cost comparison is a lot cheaper than pathkey comparison, do
413 * that first. (XXX is that still true?)
415 if (matched_path != NULL &&
416 compare_path_costs(matched_path, path, cost_criterion) <= 0)
419 if (pathkeys_contained_in(pathkeys, path->pathkeys))
426 * get_cheapest_fractional_path_for_pathkeys
427 * Find the cheapest path (for retrieving a specified fraction of all
428 * the tuples) that satisfies the given pathkeys.
429 * Return NULL if no such path.
431 * See compare_fractional_path_costs() for the interpretation of the fraction
434 * 'paths' is a list of possible paths that all generate the same relation
435 * 'pathkeys' represents a required ordering (already canonicalized!)
436 * 'fraction' is the fraction of the total tuples expected to be retrieved
439 get_cheapest_fractional_path_for_pathkeys(List *paths,
443 Path *matched_path = NULL;
448 Path *path = (Path *) lfirst(l);
451 * Since cost comparison is a lot cheaper than pathkey comparison, do
454 if (matched_path != NULL &&
455 compare_fractional_path_costs(matched_path, path, fraction) <= 0)
458 if (pathkeys_contained_in(pathkeys, path->pathkeys))
464 /****************************************************************************
465 * NEW PATHKEY FORMATION
466 ****************************************************************************/
469 * build_index_pathkeys
470 * Build a pathkeys list that describes the ordering induced by an index
471 * scan using the given index. (Note that an unordered index doesn't
472 * induce any ordering; such an index will have no sortop OIDS in
473 * its sortops arrays, and we will return NIL.)
475 * If 'scandir' is BackwardScanDirection, attempt to build pathkeys
476 * representing a backwards scan of the index. Return NIL if can't do it.
478 * The result is canonical, meaning that redundant pathkeys are removed;
479 * it may therefore have fewer entries than there are index columns.
481 * We generate the full pathkeys list whether or not all are useful for the
482 * current query. Caller should do truncate_useless_pathkeys().
485 build_index_pathkeys(PlannerInfo *root,
487 ScanDirection scandir)
490 ListCell *indexprs_item = list_head(index->indexprs);
493 for (i = 0; i < index->ncolumns; i++)
501 if (ScanDirectionIsBackward(scandir))
503 sortop = index->revsortop[i];
504 nulls_first = !index->nulls_first[i];
508 sortop = index->fwdsortop[i];
509 nulls_first = index->nulls_first[i];
512 if (!OidIsValid(sortop))
513 break; /* no more orderable columns */
515 ikey = index->indexkeys[i];
518 /* simple index column */
519 indexkey = (Expr *) find_indexkey_var(root, index->rel, ikey);
523 /* expression --- assume we need not copy it */
524 if (indexprs_item == NULL)
525 elog(ERROR, "wrong number of index expressions");
526 indexkey = (Expr *) lfirst(indexprs_item);
527 indexprs_item = lnext(indexprs_item);
530 /* OK, make a canonical pathkey for this sort key */
531 cpathkey = make_pathkey_from_sortinfo(root,
538 /* Add to list unless redundant */
539 if (!pathkey_is_redundant(cpathkey, retval))
540 retval = lappend(retval, cpathkey);
547 * Find or make a Var node for the specified attribute of the rel.
549 * We first look for the var in the rel's target list, because that's
550 * easy and fast. But the var might not be there (this should normally
551 * only happen for vars that are used in WHERE restriction clauses,
552 * but not in join clauses or in the SELECT target list). In that case,
553 * gin up a Var node the hard way.
556 find_indexkey_var(PlannerInfo *root, RelOptInfo *rel, AttrNumber varattno)
564 foreach(temp, rel->reltargetlist)
566 Var *var = (Var *) lfirst(temp);
569 var->varattno == varattno)
574 reloid = getrelid(relid, root->parse->rtable);
575 get_atttypetypmod(reloid, varattno, &vartypeid, &type_mod);
577 return makeVar(relid, varattno, vartypeid, type_mod, 0);
581 * convert_subquery_pathkeys
582 * Build a pathkeys list that describes the ordering of a subquery's
583 * result, in the terms of the outer query. This is essentially a
584 * task of conversion.
586 * 'rel': outer query's RelOptInfo for the subquery relation.
587 * 'subquery_pathkeys': the subquery's output pathkeys, in its terms.
589 * It is not necessary for caller to do truncate_useless_pathkeys(),
590 * because we select keys in a way that takes usefulness of the keys into
594 convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel,
595 List *subquery_pathkeys)
599 int outer_query_keys = list_length(root->query_pathkeys);
600 List *sub_tlist = rel->subplan->targetlist;
603 foreach(i, subquery_pathkeys)
605 PathKey *sub_pathkey = (PathKey *) lfirst(i);
606 EquivalenceClass *sub_eclass = sub_pathkey->pk_eclass;
607 PathKey *best_pathkey = NULL;
609 if (sub_eclass->ec_has_volatile)
612 * If the sub_pathkey's EquivalenceClass is volatile, then it must
613 * have come from an ORDER BY clause, and we have to match it to
614 * that same targetlist entry.
618 if (sub_eclass->ec_sortref == 0) /* can't happen */
619 elog(ERROR, "volatile EquivalenceClass has no sortref");
620 tle = get_sortgroupref_tle(sub_eclass->ec_sortref, sub_tlist);
622 /* resjunk items aren't visible to outer query */
625 /* We can represent this sub_pathkey */
626 EquivalenceMember *sub_member;
628 EquivalenceClass *outer_ec;
630 Assert(list_length(sub_eclass->ec_members) == 1);
631 sub_member = (EquivalenceMember *) linitial(sub_eclass->ec_members);
632 outer_expr = (Expr *)
635 exprType((Node *) tle->expr),
636 exprTypmod((Node *) tle->expr),
639 get_eclass_for_sort_expr(root,
641 sub_member->em_datatype,
642 sub_eclass->ec_opfamilies,
645 make_canonical_pathkey(root,
647 sub_pathkey->pk_opfamily,
648 sub_pathkey->pk_strategy,
649 sub_pathkey->pk_nulls_first);
655 * Otherwise, the sub_pathkey's EquivalenceClass could contain
656 * multiple elements (representing knowledge that multiple items
657 * are effectively equal). Each element might match none, one, or
658 * more of the output columns that are visible to the outer query.
659 * This means we may have multiple possible representations of the
660 * sub_pathkey in the context of the outer query. Ideally we
661 * would generate them all and put them all into an EC of the
662 * outer query, thereby propagating equality knowledge up to the
663 * outer query. Right now we cannot do so, because the outer
664 * query's EquivalenceClasses are already frozen when this is
665 * called. Instead we prefer the one that has the highest "score"
666 * (number of EC peers, plus one if it matches the outer
667 * query_pathkeys). This is the most likely to be useful in the
673 foreach(j, sub_eclass->ec_members)
675 EquivalenceMember *sub_member = (EquivalenceMember *) lfirst(j);
676 Expr *sub_expr = sub_member->em_expr;
681 * We handle two cases: the sub_pathkey key can be either an
682 * exact match for a targetlist entry, or it could match after
683 * stripping RelabelType nodes. (We need that case since
684 * make_pathkey_from_sortinfo could add or remove
687 sub_stripped = sub_expr;
688 while (sub_stripped && IsA(sub_stripped, RelabelType))
689 sub_stripped = ((RelabelType *) sub_stripped)->arg;
691 foreach(k, sub_tlist)
693 TargetEntry *tle = (TargetEntry *) lfirst(k);
695 EquivalenceClass *outer_ec;
699 /* resjunk items aren't visible to outer query */
703 if (equal(tle->expr, sub_expr))
706 outer_expr = (Expr *)
709 exprType((Node *) tle->expr),
710 exprTypmod((Node *) tle->expr),
717 tle_stripped = tle->expr;
718 while (tle_stripped && IsA(tle_stripped, RelabelType))
719 tle_stripped = ((RelabelType *) tle_stripped)->arg;
721 if (equal(tle_stripped, sub_stripped))
723 /* Match after discarding RelabelType */
724 outer_expr = (Expr *)
727 exprType((Node *) tle->expr),
728 exprTypmod((Node *) tle->expr),
730 if (exprType((Node *) outer_expr) !=
731 exprType((Node *) sub_expr))
732 outer_expr = (Expr *)
733 makeRelabelType(outer_expr,
734 exprType((Node *) sub_expr),
742 /* Found a representation for this sub_pathkey */
743 outer_ec = get_eclass_for_sort_expr(root,
745 sub_member->em_datatype,
746 sub_eclass->ec_opfamilies,
748 outer_pk = make_canonical_pathkey(root,
750 sub_pathkey->pk_opfamily,
751 sub_pathkey->pk_strategy,
752 sub_pathkey->pk_nulls_first);
753 /* score = # of equivalence peers */
754 score = list_length(outer_ec->ec_members) - 1;
755 /* +1 if it matches the proper query_pathkeys item */
756 if (retvallen < outer_query_keys &&
757 list_nth(root->query_pathkeys, retvallen) == outer_pk)
759 if (score > best_score)
761 best_pathkey = outer_pk;
769 * If we couldn't find a representation of this sub_pathkey, we're
770 * done (we can't use the ones to its right, either).
776 * Eliminate redundant ordering info; could happen if outer query
777 * equivalences subquery keys...
779 if (!pathkey_is_redundant(best_pathkey, retval))
781 retval = lappend(retval, best_pathkey);
790 * build_join_pathkeys
791 * Build the path keys for a join relation constructed by mergejoin or
792 * nestloop join. This is normally the same as the outer path's keys.
794 * EXCEPTION: in a FULL or RIGHT join, we cannot treat the result as
795 * having the outer path's path keys, because null lefthand rows may be
796 * inserted at random points. It must be treated as unsorted.
798 * We truncate away any pathkeys that are uninteresting for higher joins.
800 * 'joinrel' is the join relation that paths are being formed for
801 * 'jointype' is the join type (inner, left, full, etc)
802 * 'outer_pathkeys' is the list of the current outer path's path keys
804 * Returns the list of new path keys.
807 build_join_pathkeys(PlannerInfo *root,
810 List *outer_pathkeys)
812 if (jointype == JOIN_FULL || jointype == JOIN_RIGHT)
816 * This used to be quite a complex bit of code, but now that all pathkey
817 * sublists start out life canonicalized, we don't have to do a darn thing
820 * We do, however, need to truncate the pathkeys list, since it may
821 * contain pathkeys that were useful for forming this joinrel but are
822 * uninteresting to higher levels.
824 return truncate_useless_pathkeys(root, joinrel, outer_pathkeys);
827 /****************************************************************************
828 * PATHKEYS AND SORT CLAUSES
829 ****************************************************************************/
832 * make_pathkeys_for_sortclauses
833 * Generate a pathkeys list that represents the sort order specified
834 * by a list of SortGroupClauses
836 * If canonicalize is TRUE, the resulting PathKeys are all in canonical form;
837 * otherwise not. canonicalize should always be TRUE after EquivalenceClass
838 * merging has been performed, but FALSE if we haven't done EquivalenceClass
839 * merging yet. (We provide this option because grouping_planner() needs to
840 * be able to represent requested pathkeys before the equivalence classes have
841 * been created for the query.)
843 * 'sortclauses' is a list of SortGroupClause nodes
844 * 'tlist' is the targetlist to find the referenced tlist entries in
847 make_pathkeys_for_sortclauses(PlannerInfo *root,
852 List *pathkeys = NIL;
855 foreach(l, sortclauses)
857 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
861 sortkey = (Expr *) get_sortgroupclause_expr(sortcl, tlist);
862 Assert(OidIsValid(sortcl->sortop));
863 pathkey = make_pathkey_from_sortinfo(root,
867 sortcl->tleSortGroupRef,
870 /* Canonical form eliminates redundant ordering keys */
873 if (!pathkey_is_redundant(pathkey, pathkeys))
874 pathkeys = lappend(pathkeys, pathkey);
877 pathkeys = lappend(pathkeys, pathkey);
882 /****************************************************************************
883 * PATHKEYS AND MERGECLAUSES
884 ****************************************************************************/
887 * cache_mergeclause_eclasses
888 * Make the cached EquivalenceClass links valid in a mergeclause
891 * RestrictInfo contains fields in which we may cache pointers to
892 * EquivalenceClasses for the left and right inputs of the mergeclause.
893 * (If the mergeclause is a true equivalence clause these will be the
894 * same EquivalenceClass, otherwise not.)
897 cache_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
899 Assert(restrictinfo->mergeopfamilies != NIL);
901 /* the cached values should be either both set or both not */
902 if (restrictinfo->left_ec == NULL)
904 Expr *clause = restrictinfo->clause;
908 /* Need the declared input types of the operator */
909 op_input_types(((OpExpr *) clause)->opno, &lefttype, &righttype);
911 /* Find or create a matching EquivalenceClass for each side */
912 restrictinfo->left_ec =
913 get_eclass_for_sort_expr(root,
914 (Expr *) get_leftop(clause),
916 restrictinfo->mergeopfamilies,
918 restrictinfo->right_ec =
919 get_eclass_for_sort_expr(root,
920 (Expr *) get_rightop(clause),
922 restrictinfo->mergeopfamilies,
926 Assert(restrictinfo->right_ec != NULL);
930 * find_mergeclauses_for_pathkeys
931 * This routine attempts to find a set of mergeclauses that can be
932 * used with a specified ordering for one of the input relations.
933 * If successful, it returns a list of mergeclauses.
935 * 'pathkeys' is a pathkeys list showing the ordering of an input path.
936 * 'outer_keys' is TRUE if these keys are for the outer input path,
937 * FALSE if for inner.
938 * 'restrictinfos' is a list of mergejoinable restriction clauses for the
939 * join relation being formed.
941 * The restrictinfos must be marked (via outer_is_left) to show which side
942 * of each clause is associated with the current outer path. (See
943 * select_mergejoin_clauses())
945 * The result is NIL if no merge can be done, else a maximal list of
946 * usable mergeclauses (represented as a list of their restrictinfo nodes).
949 find_mergeclauses_for_pathkeys(PlannerInfo *root,
954 List *mergeclauses = NIL;
957 /* make sure we have eclasses cached in the clauses */
958 foreach(i, restrictinfos)
960 RestrictInfo *rinfo = (RestrictInfo *) lfirst(i);
962 cache_mergeclause_eclasses(root, rinfo);
967 PathKey *pathkey = (PathKey *) lfirst(i);
968 EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
969 List *matched_restrictinfos = NIL;
973 * A mergejoin clause matches a pathkey if it has the same EC.
974 * If there are multiple matching clauses, take them all. In plain
975 * inner-join scenarios we expect only one match, because
976 * equivalence-class processing will have removed any redundant
977 * mergeclauses. However, in outer-join scenarios there might be
978 * multiple matches. An example is
980 * select * from a full join b
981 * on a.v1 = b.v1 and a.v2 = b.v2 and a.v1 = b.v2;
983 * Given the pathkeys ({a.v1}, {a.v2}) it is okay to return all three
984 * clauses (in the order a.v1=b.v1, a.v1=b.v2, a.v2=b.v2) and indeed
985 * we *must* do so or we will be unable to form a valid plan.
987 * We expect that the given pathkeys list is canonical, which means
988 * no two members have the same EC, so it's not possible for this
989 * code to enter the same mergeclause into the result list twice.
991 * It's possible that multiple matching clauses might have different
992 * ECs on the other side, in which case the order we put them into our
993 * result makes a difference in the pathkeys required for the other
994 * input path. However this routine hasn't got any info about which
995 * order would be best, so we don't worry about that.
997 * It's also possible that the selected mergejoin clauses produce
998 * a noncanonical ordering of pathkeys for the other side, ie, we
999 * might select clauses that reference b.v1, b.v2, b.v1 in that
1000 * order. This is not harmful in itself, though it suggests that
1001 * the clauses are partially redundant. Since it happens only with
1002 * redundant query conditions, we don't bother to eliminate it.
1003 * make_inner_pathkeys_for_merge() has to delete duplicates when
1004 * it constructs the canonical pathkeys list, and we also have to
1005 * deal with the case in create_mergejoin_plan().
1008 foreach(j, restrictinfos)
1010 RestrictInfo *rinfo = (RestrictInfo *) lfirst(j);
1011 EquivalenceClass *clause_ec;
1014 clause_ec = rinfo->outer_is_left ?
1015 rinfo->left_ec : rinfo->right_ec;
1017 clause_ec = rinfo->outer_is_left ?
1018 rinfo->right_ec : rinfo->left_ec;
1019 if (clause_ec == pathkey_ec)
1020 matched_restrictinfos = lappend(matched_restrictinfos, rinfo);
1024 * If we didn't find a mergeclause, we're done --- any additional
1025 * sort-key positions in the pathkeys are useless. (But we can still
1026 * mergejoin if we found at least one mergeclause.)
1028 if (matched_restrictinfos == NIL)
1032 * If we did find usable mergeclause(s) for this sort-key position,
1033 * add them to result list.
1035 mergeclauses = list_concat(mergeclauses, matched_restrictinfos);
1038 return mergeclauses;
1042 * select_outer_pathkeys_for_merge
1043 * Builds a pathkey list representing a possible sort ordering
1044 * that can be used with the given mergeclauses.
1046 * 'mergeclauses' is a list of RestrictInfos for mergejoin clauses
1047 * that will be used in a merge join.
1048 * 'joinrel' is the join relation we are trying to construct.
1050 * The restrictinfos must be marked (via outer_is_left) to show which side
1051 * of each clause is associated with the current outer path. (See
1052 * select_mergejoin_clauses())
1054 * Returns a pathkeys list that can be applied to the outer relation.
1056 * Since we assume here that a sort is required, there is no particular use
1057 * in matching any available ordering of the outerrel. (joinpath.c has an
1058 * entirely separate code path for considering sort-free mergejoins.) Rather,
1059 * it's interesting to try to match the requested query_pathkeys so that a
1060 * second output sort may be avoided; and failing that, we try to list "more
1061 * popular" keys (those with the most unmatched EquivalenceClass peers)
1062 * earlier, in hopes of making the resulting ordering useful for as many
1063 * higher-level mergejoins as possible.
1066 select_outer_pathkeys_for_merge(PlannerInfo *root,
1068 RelOptInfo *joinrel)
1070 List *pathkeys = NIL;
1071 int nClauses = list_length(mergeclauses);
1072 EquivalenceClass **ecs;
1078 /* Might have no mergeclauses */
1083 * Make arrays of the ECs used by the mergeclauses (dropping any
1084 * duplicates) and their "popularity" scores.
1086 ecs = (EquivalenceClass **) palloc(nClauses * sizeof(EquivalenceClass *));
1087 scores = (int *) palloc(nClauses * sizeof(int));
1090 foreach(lc, mergeclauses)
1092 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1093 EquivalenceClass *oeclass;
1097 /* get the outer eclass */
1098 cache_mergeclause_eclasses(root, rinfo);
1100 if (rinfo->outer_is_left)
1101 oeclass = rinfo->left_ec;
1103 oeclass = rinfo->right_ec;
1105 /* reject duplicates */
1106 for (j = 0; j < necs; j++)
1108 if (ecs[j] == oeclass)
1116 foreach(lc2, oeclass->ec_members)
1118 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
1120 /* Potential future join partner? */
1121 if (!em->em_is_const && !em->em_is_child &&
1122 !bms_overlap(em->em_relids, joinrel->relids))
1126 ecs[necs] = oeclass;
1127 scores[necs] = score;
1132 * Find out if we have all the ECs mentioned in query_pathkeys; if so we
1133 * can generate a sort order that's also useful for final output. There is
1134 * no percentage in a partial match, though, so we have to have 'em all.
1136 if (root->query_pathkeys)
1138 foreach(lc, root->query_pathkeys)
1140 PathKey *query_pathkey = (PathKey *) lfirst(lc);
1141 EquivalenceClass *query_ec = query_pathkey->pk_eclass;
1143 for (j = 0; j < necs; j++)
1145 if (ecs[j] == query_ec)
1146 break; /* found match */
1149 break; /* didn't find match */
1151 /* if we got to the end of the list, we have them all */
1154 /* copy query_pathkeys as starting point for our output */
1155 pathkeys = list_copy(root->query_pathkeys);
1156 /* mark their ECs as already-emitted */
1157 foreach(lc, root->query_pathkeys)
1159 PathKey *query_pathkey = (PathKey *) lfirst(lc);
1160 EquivalenceClass *query_ec = query_pathkey->pk_eclass;
1162 for (j = 0; j < necs; j++)
1164 if (ecs[j] == query_ec)
1175 * Add remaining ECs to the list in popularity order, using a default sort
1176 * ordering. (We could use qsort() here, but the list length is usually
1177 * so small it's not worth it.)
1183 EquivalenceClass *ec;
1187 best_score = scores[0];
1188 for (j = 1; j < necs; j++)
1190 if (scores[j] > best_score)
1193 best_score = scores[j];
1197 break; /* all done */
1199 scores[best_j] = -1;
1200 pathkey = make_canonical_pathkey(root,
1202 linitial_oid(ec->ec_opfamilies),
1203 BTLessStrategyNumber,
1205 /* can't be redundant because no duplicate ECs */
1206 Assert(!pathkey_is_redundant(pathkey, pathkeys));
1207 pathkeys = lappend(pathkeys, pathkey);
1217 * make_inner_pathkeys_for_merge
1218 * Builds a pathkey list representing the explicit sort order that
1219 * must be applied to an inner path to make it usable with the
1220 * given mergeclauses.
1222 * 'mergeclauses' is a list of RestrictInfos for mergejoin clauses
1223 * that will be used in a merge join.
1224 * 'outer_pathkeys' are the already-known canonical pathkeys for the outer
1227 * The restrictinfos must be marked (via outer_is_left) to show which side
1228 * of each clause is associated with the current outer path. (See
1229 * select_mergejoin_clauses())
1231 * Returns a pathkeys list that can be applied to the inner relation.
1233 * Note that it is not this routine's job to decide whether sorting is
1234 * actually needed for a particular input path. Assume a sort is necessary;
1235 * just make the keys, eh?
1238 make_inner_pathkeys_for_merge(PlannerInfo *root,
1240 List *outer_pathkeys)
1242 List *pathkeys = NIL;
1243 EquivalenceClass *lastoeclass;
1250 lop = list_head(outer_pathkeys);
1252 foreach(lc, mergeclauses)
1254 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1255 EquivalenceClass *oeclass;
1256 EquivalenceClass *ieclass;
1259 cache_mergeclause_eclasses(root, rinfo);
1261 if (rinfo->outer_is_left)
1263 oeclass = rinfo->left_ec;
1264 ieclass = rinfo->right_ec;
1268 oeclass = rinfo->right_ec;
1269 ieclass = rinfo->left_ec;
1272 /* outer eclass should match current or next pathkeys */
1273 /* we check this carefully for debugging reasons */
1274 if (oeclass != lastoeclass)
1277 elog(ERROR, "too few pathkeys for mergeclauses");
1278 opathkey = (PathKey *) lfirst(lop);
1280 lastoeclass = opathkey->pk_eclass;
1281 if (oeclass != lastoeclass)
1282 elog(ERROR, "outer pathkeys do not match mergeclause");
1286 * Often, we'll have same EC on both sides, in which case the outer
1287 * pathkey is also canonical for the inner side, and we can skip a
1290 if (ieclass == oeclass)
1293 pathkey = make_canonical_pathkey(root,
1295 opathkey->pk_opfamily,
1296 opathkey->pk_strategy,
1297 opathkey->pk_nulls_first);
1300 * Don't generate redundant pathkeys (can happen if multiple
1301 * mergeclauses refer to same EC).
1303 if (!pathkey_is_redundant(pathkey, pathkeys))
1304 pathkeys = lappend(pathkeys, pathkey);
1310 /****************************************************************************
1311 * PATHKEY USEFULNESS CHECKS
1313 * We only want to remember as many of the pathkeys of a path as have some
1314 * potential use, either for subsequent mergejoins or for meeting the query's
1315 * requested output ordering. This ensures that add_path() won't consider
1316 * a path to have a usefully different ordering unless it really is useful.
1317 * These routines check for usefulness of given pathkeys.
1318 ****************************************************************************/
1321 * pathkeys_useful_for_merging
1322 * Count the number of pathkeys that may be useful for mergejoins
1323 * above the given relation.
1325 * We consider a pathkey potentially useful if it corresponds to the merge
1326 * ordering of either side of any joinclause for the rel. This might be
1327 * overoptimistic, since joinclauses that require different other relations
1328 * might never be usable at the same time, but trying to be exact is likely
1329 * to be more trouble than it's worth.
1331 * To avoid doubling the number of mergejoin paths considered, we would like
1332 * to consider only one of the two scan directions (ASC or DESC) as useful
1333 * for merging for any given target column. The choice is arbitrary unless
1334 * one of the directions happens to match an ORDER BY key, in which case
1335 * that direction should be preferred, in hopes of avoiding a final sort step.
1336 * right_merge_direction() implements this heuristic.
1339 pathkeys_useful_for_merging(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
1344 foreach(i, pathkeys)
1346 PathKey *pathkey = (PathKey *) lfirst(i);
1347 bool matched = false;
1350 /* If "wrong" direction, not useful for merging */
1351 if (!right_merge_direction(root, pathkey))
1355 * First look into the EquivalenceClass of the pathkey, to see if
1356 * there are any members not yet joined to the rel. If so, it's
1357 * surely possible to generate a mergejoin clause using them.
1359 if (rel->has_eclass_joins &&
1360 eclass_useful_for_merging(pathkey->pk_eclass, rel))
1365 * Otherwise search the rel's joininfo list, which contains
1366 * non-EquivalenceClass-derivable join clauses that might
1367 * nonetheless be mergejoinable.
1369 foreach(j, rel->joininfo)
1371 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(j);
1373 if (restrictinfo->mergeopfamilies == NIL)
1375 cache_mergeclause_eclasses(root, restrictinfo);
1377 if (pathkey->pk_eclass == restrictinfo->left_ec ||
1378 pathkey->pk_eclass == restrictinfo->right_ec)
1387 * If we didn't find a mergeclause, we're done --- any additional
1388 * sort-key positions in the pathkeys are useless. (But we can still
1389 * mergejoin if we found at least one mergeclause.)
1401 * right_merge_direction
1402 * Check whether the pathkey embodies the preferred sort direction
1403 * for merging its target column.
1406 right_merge_direction(PlannerInfo *root, PathKey *pathkey)
1410 foreach(l, root->query_pathkeys)
1412 PathKey *query_pathkey = (PathKey *) lfirst(l);
1414 if (pathkey->pk_eclass == query_pathkey->pk_eclass &&
1415 pathkey->pk_opfamily == query_pathkey->pk_opfamily)
1418 * Found a matching query sort column. Prefer this pathkey's
1419 * direction iff it matches. Note that we ignore pk_nulls_first,
1420 * which means that a sort might be needed anyway ... but we still
1421 * want to prefer only one of the two possible directions, and we
1422 * might as well use this one.
1424 return (pathkey->pk_strategy == query_pathkey->pk_strategy);
1428 /* If no matching ORDER BY request, prefer the ASC direction */
1429 return (pathkey->pk_strategy == BTLessStrategyNumber);
1433 * pathkeys_useful_for_ordering
1434 * Count the number of pathkeys that are useful for meeting the
1435 * query's requested output ordering.
1437 * Unlike merge pathkeys, this is an all-or-nothing affair: it does us
1438 * no good to order by just the first key(s) of the requested ordering.
1439 * So the result is always either 0 or list_length(root->query_pathkeys).
1442 pathkeys_useful_for_ordering(PlannerInfo *root, List *pathkeys)
1444 if (root->query_pathkeys == NIL)
1445 return 0; /* no special ordering requested */
1447 if (pathkeys == NIL)
1448 return 0; /* unordered path */
1450 if (pathkeys_contained_in(root->query_pathkeys, pathkeys))
1452 /* It's useful ... or at least the first N keys are */
1453 return list_length(root->query_pathkeys);
1456 return 0; /* path ordering not useful */
1460 * truncate_useless_pathkeys
1461 * Shorten the given pathkey list to just the useful pathkeys.
1464 truncate_useless_pathkeys(PlannerInfo *root,
1471 nuseful = pathkeys_useful_for_merging(root, rel, pathkeys);
1472 nuseful2 = pathkeys_useful_for_ordering(root, pathkeys);
1473 if (nuseful2 > nuseful)
1477 * Note: not safe to modify input list destructively, but we can avoid
1478 * copying the list if we're not actually going to change it
1482 else if (nuseful == list_length(pathkeys))
1485 return list_truncate(list_copy(pathkeys), nuseful);
1489 * has_useful_pathkeys
1490 * Detect whether the specified rel could have any pathkeys that are
1491 * useful according to truncate_useless_pathkeys().
1493 * This is a cheap test that lets us skip building pathkeys at all in very
1494 * simple queries. It's OK to err in the direction of returning "true" when
1495 * there really aren't any usable pathkeys, but erring in the other direction
1496 * is bad --- so keep this in sync with the routines above!
1498 * We could make the test more complex, for example checking to see if any of
1499 * the joinclauses are really mergejoinable, but that likely wouldn't win
1500 * often enough to repay the extra cycles. Queries with neither a join nor
1501 * a sort are reasonably common, though, so this much work seems worthwhile.
1504 has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
1506 if (rel->joininfo != NIL || rel->has_eclass_joins)
1507 return true; /* might be able to use pathkeys for merging */
1508 if (root->query_pathkeys != NIL)
1509 return true; /* might be able to use them for ordering */
1510 return false; /* definitely useless */