PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pathkeys.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pathkeys.c
4  * Utilities for matching and building path keys
5  *
6  * See src/backend/optimizer/README for a great deal of information about
7  * the nature and use of path keys.
8  *
9  *
10  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  * IDENTIFICATION
14  * src/backend/optimizer/path/pathkeys.c
15  *
16  *-------------------------------------------------------------------------
17  */
18 #include "postgres.h"
19 
20 #include "access/stratnum.h"
21 #include "nodes/makefuncs.h"
22 #include "nodes/nodeFuncs.h"
23 #include "nodes/plannodes.h"
24 #include "optimizer/clauses.h"
25 #include "optimizer/pathnode.h"
26 #include "optimizer/paths.h"
27 #include "optimizer/tlist.h"
28 #include "utils/lsyscache.h"
29 
30 
31 static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys);
32 static bool right_merge_direction(PlannerInfo *root, PathKey *pathkey);
33 
34 
35 /****************************************************************************
36  * PATHKEY CONSTRUCTION AND REDUNDANCY TESTING
37  ****************************************************************************/
38 
39 /*
40  * make_canonical_pathkey
41  * Given the parameters for a PathKey, find any pre-existing matching
42  * pathkey in the query's list of "canonical" pathkeys. Make a new
43  * entry if there's not one already.
44  *
45  * Note that this function must not be used until after we have completed
46  * merging EquivalenceClasses. (We don't try to enforce that here; instead,
47  * equivclass.c will complain if a merge occurs after root->canon_pathkeys
48  * has become nonempty.)
49  */
50 PathKey *
52  EquivalenceClass *eclass, Oid opfamily,
53  int strategy, bool nulls_first)
54 {
55  PathKey *pk;
56  ListCell *lc;
57  MemoryContext oldcontext;
58 
59  /* The passed eclass might be non-canonical, so chase up to the top */
60  while (eclass->ec_merged)
61  eclass = eclass->ec_merged;
62 
63  foreach(lc, root->canon_pathkeys)
64  {
65  pk = (PathKey *) lfirst(lc);
66  if (eclass == pk->pk_eclass &&
67  opfamily == pk->pk_opfamily &&
68  strategy == pk->pk_strategy &&
69  nulls_first == pk->pk_nulls_first)
70  return pk;
71  }
72 
73  /*
74  * Be sure canonical pathkeys are allocated in the main planning context.
75  * Not an issue in normal planning, but it is for GEQO.
76  */
77  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
78 
79  pk = makeNode(PathKey);
80  pk->pk_eclass = eclass;
81  pk->pk_opfamily = opfamily;
82  pk->pk_strategy = strategy;
83  pk->pk_nulls_first = nulls_first;
84 
85  root->canon_pathkeys = lappend(root->canon_pathkeys, pk);
86 
87  MemoryContextSwitchTo(oldcontext);
88 
89  return pk;
90 }
91 
92 /*
93  * pathkey_is_redundant
94  * Is a pathkey redundant with one already in the given list?
95  *
96  * We detect two cases:
97  *
98  * 1. If the new pathkey's equivalence class contains a constant, and isn't
99  * below an outer join, then we can disregard it as a sort key. An example:
100  * SELECT ... WHERE x = 42 ORDER BY x, y;
101  * We may as well just sort by y. Note that because of opfamily matching,
102  * this is semantically correct: we know that the equality constraint is one
103  * that actually binds the variable to a single value in the terms of any
104  * ordering operator that might go with the eclass. This rule not only lets
105  * us simplify (or even skip) explicit sorts, but also allows matching index
106  * sort orders to a query when there are don't-care index columns.
107  *
108  * 2. If the new pathkey's equivalence class is the same as that of any
109  * existing member of the pathkey list, then it is redundant. Some examples:
110  * SELECT ... ORDER BY x, x;
111  * SELECT ... ORDER BY x, x DESC;
112  * SELECT ... WHERE x = y ORDER BY x, y;
113  * In all these cases the second sort key cannot distinguish values that are
114  * considered equal by the first, and so there's no point in using it.
115  * Note in particular that we need not compare opfamily (all the opfamilies
116  * of the EC have the same notion of equality) nor sort direction.
117  *
118  * Both the given pathkey and the list members must be canonical for this
119  * to work properly, but that's okay since we no longer ever construct any
120  * non-canonical pathkeys. (Note: the notion of a pathkey *list* being
121  * canonical includes the additional requirement of no redundant entries,
122  * which is exactly what we are checking for here.)
123  *
124  * Because the equivclass.c machinery forms only one copy of any EC per query,
125  * pointer comparison is enough to decide whether canonical ECs are the same.
126  */
127 static bool
128 pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
129 {
130  EquivalenceClass *new_ec = new_pathkey->pk_eclass;
131  ListCell *lc;
132 
133  /* Check for EC containing a constant --- unconditionally redundant */
134  if (EC_MUST_BE_REDUNDANT(new_ec))
135  return true;
136 
137  /* If same EC already used in list, then redundant */
138  foreach(lc, pathkeys)
139  {
140  PathKey *old_pathkey = (PathKey *) lfirst(lc);
141 
142  if (new_ec == old_pathkey->pk_eclass)
143  return true;
144  }
145 
146  return false;
147 }
148 
149 /*
150  * make_pathkey_from_sortinfo
151  * Given an expression and sort-order information, create a PathKey.
152  * The result is always a "canonical" PathKey, but it might be redundant.
153  *
154  * expr is the expression, and nullable_relids is the set of base relids
155  * that are potentially nullable below it.
156  *
157  * If the PathKey is being generated from a SortGroupClause, sortref should be
158  * the SortGroupClause's SortGroupRef; otherwise zero.
159  *
160  * If rel is not NULL, it identifies a specific relation we're considering
161  * a path for, and indicates that child EC members for that relation can be
162  * considered. Otherwise child members are ignored. (See the comments for
163  * get_eclass_for_sort_expr.)
164  *
165  * create_it is TRUE if we should create any missing EquivalenceClass
166  * needed to represent the sort key. If it's FALSE, we return NULL if the
167  * sort key isn't already present in any EquivalenceClass.
168  */
169 static PathKey *
171  Expr *expr,
172  Relids nullable_relids,
173  Oid opfamily,
174  Oid opcintype,
175  Oid collation,
176  bool reverse_sort,
177  bool nulls_first,
178  Index sortref,
179  Relids rel,
180  bool create_it)
181 {
182  int16 strategy;
183  Oid equality_op;
184  List *opfamilies;
186 
187  strategy = reverse_sort ? BTGreaterStrategyNumber : BTLessStrategyNumber;
188 
189  /*
190  * EquivalenceClasses need to contain opfamily lists based on the family
191  * membership of mergejoinable equality operators, which could belong to
192  * more than one opfamily. So we have to look up the opfamily's equality
193  * operator and get its membership.
194  */
195  equality_op = get_opfamily_member(opfamily,
196  opcintype,
197  opcintype,
199  if (!OidIsValid(equality_op)) /* shouldn't happen */
200  elog(ERROR, "could not find equality operator for opfamily %u",
201  opfamily);
202  opfamilies = get_mergejoin_opfamilies(equality_op);
203  if (!opfamilies) /* certainly should find some */
204  elog(ERROR, "could not find opfamilies for equality operator %u",
205  equality_op);
206 
207  /* Now find or (optionally) create a matching EquivalenceClass */
208  eclass = get_eclass_for_sort_expr(root, expr, nullable_relids,
209  opfamilies, opcintype, collation,
210  sortref, rel, create_it);
211 
212  /* Fail if no EC and !create_it */
213  if (!eclass)
214  return NULL;
215 
216  /* And finally we can find or create a PathKey node */
217  return make_canonical_pathkey(root, eclass, opfamily,
218  strategy, nulls_first);
219 }
220 
221 /*
222  * make_pathkey_from_sortop
223  * Like make_pathkey_from_sortinfo, but work from a sort operator.
224  *
225  * This should eventually go away, but we need to restructure SortGroupClause
226  * first.
227  */
228 static PathKey *
230  Expr *expr,
231  Relids nullable_relids,
232  Oid ordering_op,
233  bool nulls_first,
234  Index sortref,
235  bool create_it)
236 {
237  Oid opfamily,
238  opcintype,
239  collation;
240  int16 strategy;
241 
242  /* Find the operator in pg_amop --- failure shouldn't happen */
243  if (!get_ordering_op_properties(ordering_op,
244  &opfamily, &opcintype, &strategy))
245  elog(ERROR, "operator %u is not a valid ordering operator",
246  ordering_op);
247 
248  /* Because SortGroupClause doesn't carry collation, consult the expr */
249  collation = exprCollation((Node *) expr);
250 
251  return make_pathkey_from_sortinfo(root,
252  expr,
253  nullable_relids,
254  opfamily,
255  opcintype,
256  collation,
257  (strategy == BTGreaterStrategyNumber),
258  nulls_first,
259  sortref,
260  NULL,
261  create_it);
262 }
263 
264 
265 /****************************************************************************
266  * PATHKEY COMPARISONS
267  ****************************************************************************/
268 
269 /*
270  * compare_pathkeys
271  * Compare two pathkeys to see if they are equivalent, and if not whether
272  * one is "better" than the other.
273  *
274  * We assume the pathkeys are canonical, and so they can be checked for
275  * equality by simple pointer comparison.
276  */
278 compare_pathkeys(List *keys1, List *keys2)
279 {
280  ListCell *key1,
281  *key2;
282 
283  /*
284  * Fall out quickly if we are passed two identical lists. This mostly
285  * catches the case where both are NIL, but that's common enough to
286  * warrant the test.
287  */
288  if (keys1 == keys2)
289  return PATHKEYS_EQUAL;
290 
291  forboth(key1, keys1, key2, keys2)
292  {
293  PathKey *pathkey1 = (PathKey *) lfirst(key1);
294  PathKey *pathkey2 = (PathKey *) lfirst(key2);
295 
296  if (pathkey1 != pathkey2)
297  return PATHKEYS_DIFFERENT; /* no need to keep looking */
298  }
299 
300  /*
301  * If we reached the end of only one list, the other is longer and
302  * therefore not a subset.
303  */
304  if (key1 != NULL)
305  return PATHKEYS_BETTER1; /* key1 is longer */
306  if (key2 != NULL)
307  return PATHKEYS_BETTER2; /* key2 is longer */
308  return PATHKEYS_EQUAL;
309 }
310 
311 /*
312  * pathkeys_contained_in
313  * Common special case of compare_pathkeys: we just want to know
314  * if keys2 are at least as well sorted as keys1.
315  */
316 bool
318 {
319  switch (compare_pathkeys(keys1, keys2))
320  {
321  case PATHKEYS_EQUAL:
322  case PATHKEYS_BETTER2:
323  return true;
324  default:
325  break;
326  }
327  return false;
328 }
329 
330 /*
331  * get_cheapest_path_for_pathkeys
332  * Find the cheapest path (according to the specified criterion) that
333  * satisfies the given pathkeys and parameterization.
334  * Return NULL if no such path.
335  *
336  * 'paths' is a list of possible paths that all generate the same relation
337  * 'pathkeys' represents a required ordering (in canonical form!)
338  * 'required_outer' denotes allowable outer relations for parameterized paths
339  * 'cost_criterion' is STARTUP_COST or TOTAL_COST
340  */
341 Path *
343  Relids required_outer,
344  CostSelector cost_criterion)
345 {
346  Path *matched_path = NULL;
347  ListCell *l;
348 
349  foreach(l, paths)
350  {
351  Path *path = (Path *) lfirst(l);
352 
353  /*
354  * Since cost comparison is a lot cheaper than pathkey comparison, do
355  * that first. (XXX is that still true?)
356  */
357  if (matched_path != NULL &&
358  compare_path_costs(matched_path, path, cost_criterion) <= 0)
359  continue;
360 
361  if (pathkeys_contained_in(pathkeys, path->pathkeys) &&
362  bms_is_subset(PATH_REQ_OUTER(path), required_outer))
363  matched_path = path;
364  }
365  return matched_path;
366 }
367 
368 /*
369  * get_cheapest_fractional_path_for_pathkeys
370  * Find the cheapest path (for retrieving a specified fraction of all
371  * the tuples) that satisfies the given pathkeys and parameterization.
372  * Return NULL if no such path.
373  *
374  * See compare_fractional_path_costs() for the interpretation of the fraction
375  * parameter.
376  *
377  * 'paths' is a list of possible paths that all generate the same relation
378  * 'pathkeys' represents a required ordering (in canonical form!)
379  * 'required_outer' denotes allowable outer relations for parameterized paths
380  * 'fraction' is the fraction of the total tuples expected to be retrieved
381  */
382 Path *
384  List *pathkeys,
385  Relids required_outer,
386  double fraction)
387 {
388  Path *matched_path = NULL;
389  ListCell *l;
390 
391  foreach(l, paths)
392  {
393  Path *path = (Path *) lfirst(l);
394 
395  /*
396  * Since cost comparison is a lot cheaper than pathkey comparison, do
397  * that first. (XXX is that still true?)
398  */
399  if (matched_path != NULL &&
400  compare_fractional_path_costs(matched_path, path, fraction) <= 0)
401  continue;
402 
403  if (pathkeys_contained_in(pathkeys, path->pathkeys) &&
404  bms_is_subset(PATH_REQ_OUTER(path), required_outer))
405  matched_path = path;
406  }
407  return matched_path;
408 }
409 
410 /****************************************************************************
411  * NEW PATHKEY FORMATION
412  ****************************************************************************/
413 
414 /*
415  * build_index_pathkeys
416  * Build a pathkeys list that describes the ordering induced by an index
417  * scan using the given index. (Note that an unordered index doesn't
418  * induce any ordering, so we return NIL.)
419  *
420  * If 'scandir' is BackwardScanDirection, build pathkeys representing a
421  * backwards scan of the index.
422  *
423  * The result is canonical, meaning that redundant pathkeys are removed;
424  * it may therefore have fewer entries than there are index columns.
425  *
426  * Another reason for stopping early is that we may be able to tell that
427  * an index column's sort order is uninteresting for this query. However,
428  * that test is just based on the existence of an EquivalenceClass and not
429  * on position in pathkey lists, so it's not complete. Caller should call
430  * truncate_useless_pathkeys() to possibly remove more pathkeys.
431  */
432 List *
435  ScanDirection scandir)
436 {
437  List *retval = NIL;
438  ListCell *lc;
439  int i;
440 
441  if (index->sortopfamily == NULL)
442  return NIL; /* non-orderable index */
443 
444  i = 0;
445  foreach(lc, index->indextlist)
446  {
447  TargetEntry *indextle = (TargetEntry *) lfirst(lc);
448  Expr *indexkey;
449  bool reverse_sort;
450  bool nulls_first;
451  PathKey *cpathkey;
452 
453  /* We assume we don't need to make a copy of the tlist item */
454  indexkey = indextle->expr;
455 
456  if (ScanDirectionIsBackward(scandir))
457  {
458  reverse_sort = !index->reverse_sort[i];
459  nulls_first = !index->nulls_first[i];
460  }
461  else
462  {
463  reverse_sort = index->reverse_sort[i];
464  nulls_first = index->nulls_first[i];
465  }
466 
467  /*
468  * OK, try to make a canonical pathkey for this sort key. Note we're
469  * underneath any outer joins, so nullable_relids should be NULL.
470  */
471  cpathkey = make_pathkey_from_sortinfo(root,
472  indexkey,
473  NULL,
474  index->sortopfamily[i],
475  index->opcintype[i],
476  index->indexcollations[i],
477  reverse_sort,
478  nulls_first,
479  0,
480  index->rel->relids,
481  false);
482 
483  if (cpathkey)
484  {
485  /*
486  * We found the sort key in an EquivalenceClass, so it's relevant
487  * for this query. Add it to list, unless it's redundant.
488  */
489  if (!pathkey_is_redundant(cpathkey, retval))
490  retval = lappend(retval, cpathkey);
491  }
492  else
493  {
494  /*
495  * Boolean index keys might be redundant even if they do not
496  * appear in an EquivalenceClass, because of our special treatment
497  * of boolean equality conditions --- see the comment for
498  * indexcol_is_bool_constant_for_query(). If that applies, we can
499  * continue to examine lower-order index columns. Otherwise, the
500  * sort key is not an interesting sort order for this query, so we
501  * should stop considering index columns; any lower-order sort
502  * keys won't be useful either.
503  */
505  break;
506  }
507 
508  i++;
509  }
510 
511  return retval;
512 }
513 
514 /*
515  * build_expression_pathkey
516  * Build a pathkeys list that describes an ordering by a single expression
517  * using the given sort operator.
518  *
519  * expr, nullable_relids, and rel are as for make_pathkey_from_sortinfo.
520  * We induce the other arguments assuming default sort order for the operator.
521  *
522  * Similarly to make_pathkey_from_sortinfo, the result is NIL if create_it
523  * is false and the expression isn't already in some EquivalenceClass.
524  */
525 List *
527  Expr *expr,
528  Relids nullable_relids,
529  Oid opno,
530  Relids rel,
531  bool create_it)
532 {
533  List *pathkeys;
534  Oid opfamily,
535  opcintype;
536  int16 strategy;
537  PathKey *cpathkey;
538 
539  /* Find the operator in pg_amop --- failure shouldn't happen */
540  if (!get_ordering_op_properties(opno,
541  &opfamily, &opcintype, &strategy))
542  elog(ERROR, "operator %u is not a valid ordering operator",
543  opno);
544 
545  cpathkey = make_pathkey_from_sortinfo(root,
546  expr,
547  nullable_relids,
548  opfamily,
549  opcintype,
550  exprCollation((Node *) expr),
551  (strategy == BTGreaterStrategyNumber),
552  (strategy == BTGreaterStrategyNumber),
553  0,
554  rel,
555  create_it);
556 
557  if (cpathkey)
558  pathkeys = list_make1(cpathkey);
559  else
560  pathkeys = NIL;
561 
562  return pathkeys;
563 }
564 
565 /*
566  * convert_subquery_pathkeys
567  * Build a pathkeys list that describes the ordering of a subquery's
568  * result, in the terms of the outer query. This is essentially a
569  * task of conversion.
570  *
571  * 'rel': outer query's RelOptInfo for the subquery relation.
572  * 'subquery_pathkeys': the subquery's output pathkeys, in its terms.
573  * 'subquery_tlist': the subquery's output targetlist, in its terms.
574  *
575  * It is not necessary for caller to do truncate_useless_pathkeys(),
576  * because we select keys in a way that takes usefulness of the keys into
577  * account.
578  */
579 List *
581  List *subquery_pathkeys,
582  List *subquery_tlist)
583 {
584  List *retval = NIL;
585  int retvallen = 0;
586  int outer_query_keys = list_length(root->query_pathkeys);
587  ListCell *i;
588 
589  foreach(i, subquery_pathkeys)
590  {
591  PathKey *sub_pathkey = (PathKey *) lfirst(i);
592  EquivalenceClass *sub_eclass = sub_pathkey->pk_eclass;
593  PathKey *best_pathkey = NULL;
594 
595  if (sub_eclass->ec_has_volatile)
596  {
597  /*
598  * If the sub_pathkey's EquivalenceClass is volatile, then it must
599  * have come from an ORDER BY clause, and we have to match it to
600  * that same targetlist entry.
601  */
602  TargetEntry *tle;
603 
604  if (sub_eclass->ec_sortref == 0) /* can't happen */
605  elog(ERROR, "volatile EquivalenceClass has no sortref");
606  tle = get_sortgroupref_tle(sub_eclass->ec_sortref, subquery_tlist);
607  Assert(tle);
608  /* resjunk items aren't visible to outer query */
609  if (!tle->resjunk)
610  {
611  /* We can represent this sub_pathkey */
612  EquivalenceMember *sub_member;
613  Expr *outer_expr;
614  EquivalenceClass *outer_ec;
615 
616  Assert(list_length(sub_eclass->ec_members) == 1);
617  sub_member = (EquivalenceMember *) linitial(sub_eclass->ec_members);
618  outer_expr = (Expr *) makeVarFromTargetEntry(rel->relid, tle);
619 
620  /*
621  * Note: it might look funny to be setting sortref = 0 for a
622  * reference to a volatile sub_eclass. However, the
623  * expression is *not* volatile in the outer query: it's just
624  * a Var referencing whatever the subquery emitted. (IOW, the
625  * outer query isn't going to re-execute the volatile
626  * expression itself.) So this is okay. Likewise, it's
627  * correct to pass nullable_relids = NULL, because we're
628  * underneath any outer joins appearing in the outer query.
629  */
630  outer_ec =
632  outer_expr,
633  NULL,
634  sub_eclass->ec_opfamilies,
635  sub_member->em_datatype,
636  sub_eclass->ec_collation,
637  0,
638  rel->relids,
639  false);
640 
641  /*
642  * If we don't find a matching EC, sub-pathkey isn't
643  * interesting to the outer query
644  */
645  if (outer_ec)
646  best_pathkey =
648  outer_ec,
649  sub_pathkey->pk_opfamily,
650  sub_pathkey->pk_strategy,
651  sub_pathkey->pk_nulls_first);
652  }
653  }
654  else
655  {
656  /*
657  * Otherwise, the sub_pathkey's EquivalenceClass could contain
658  * multiple elements (representing knowledge that multiple items
659  * are effectively equal). Each element might match none, one, or
660  * more of the output columns that are visible to the outer query.
661  * This means we may have multiple possible representations of the
662  * sub_pathkey in the context of the outer query. Ideally we
663  * would generate them all and put them all into an EC of the
664  * outer query, thereby propagating equality knowledge up to the
665  * outer query. Right now we cannot do so, because the outer
666  * query's EquivalenceClasses are already frozen when this is
667  * called. Instead we prefer the one that has the highest "score"
668  * (number of EC peers, plus one if it matches the outer
669  * query_pathkeys). This is the most likely to be useful in the
670  * outer query.
671  */
672  int best_score = -1;
673  ListCell *j;
674 
675  foreach(j, sub_eclass->ec_members)
676  {
677  EquivalenceMember *sub_member = (EquivalenceMember *) lfirst(j);
678  Expr *sub_expr = sub_member->em_expr;
679  Oid sub_expr_type = sub_member->em_datatype;
680  Oid sub_expr_coll = sub_eclass->ec_collation;
681  ListCell *k;
682 
683  if (sub_member->em_is_child)
684  continue; /* ignore children here */
685 
686  foreach(k, subquery_tlist)
687  {
688  TargetEntry *tle = (TargetEntry *) lfirst(k);
689  Expr *tle_expr;
690  Expr *outer_expr;
691  EquivalenceClass *outer_ec;
692  PathKey *outer_pk;
693  int score;
694 
695  /* resjunk items aren't visible to outer query */
696  if (tle->resjunk)
697  continue;
698 
699  /*
700  * The targetlist entry is considered to match if it
701  * matches after sort-key canonicalization. That is
702  * needed since the sub_expr has been through the same
703  * process.
704  */
705  tle_expr = canonicalize_ec_expression(tle->expr,
706  sub_expr_type,
707  sub_expr_coll);
708  if (!equal(tle_expr, sub_expr))
709  continue;
710 
711  /*
712  * Build a representation of this targetlist entry as an
713  * outer Var.
714  */
715  outer_expr = (Expr *) makeVarFromTargetEntry(rel->relid,
716  tle);
717 
718  /* See if we have a matching EC for that */
719  outer_ec = get_eclass_for_sort_expr(root,
720  outer_expr,
721  NULL,
722  sub_eclass->ec_opfamilies,
723  sub_expr_type,
724  sub_expr_coll,
725  0,
726  rel->relids,
727  false);
728 
729  /*
730  * If we don't find a matching EC, this sub-pathkey isn't
731  * interesting to the outer query
732  */
733  if (!outer_ec)
734  continue;
735 
736  outer_pk = make_canonical_pathkey(root,
737  outer_ec,
738  sub_pathkey->pk_opfamily,
739  sub_pathkey->pk_strategy,
740  sub_pathkey->pk_nulls_first);
741  /* score = # of equivalence peers */
742  score = list_length(outer_ec->ec_members) - 1;
743  /* +1 if it matches the proper query_pathkeys item */
744  if (retvallen < outer_query_keys &&
745  list_nth(root->query_pathkeys, retvallen) == outer_pk)
746  score++;
747  if (score > best_score)
748  {
749  best_pathkey = outer_pk;
750  best_score = score;
751  }
752  }
753  }
754  }
755 
756  /*
757  * If we couldn't find a representation of this sub_pathkey, we're
758  * done (we can't use the ones to its right, either).
759  */
760  if (!best_pathkey)
761  break;
762 
763  /*
764  * Eliminate redundant ordering info; could happen if outer query
765  * equivalences subquery keys...
766  */
767  if (!pathkey_is_redundant(best_pathkey, retval))
768  {
769  retval = lappend(retval, best_pathkey);
770  retvallen++;
771  }
772  }
773 
774  return retval;
775 }
776 
777 /*
778  * build_join_pathkeys
779  * Build the path keys for a join relation constructed by mergejoin or
780  * nestloop join. This is normally the same as the outer path's keys.
781  *
782  * EXCEPTION: in a FULL or RIGHT join, we cannot treat the result as
783  * having the outer path's path keys, because null lefthand rows may be
784  * inserted at random points. It must be treated as unsorted.
785  *
786  * We truncate away any pathkeys that are uninteresting for higher joins.
787  *
788  * 'joinrel' is the join relation that paths are being formed for
789  * 'jointype' is the join type (inner, left, full, etc)
790  * 'outer_pathkeys' is the list of the current outer path's path keys
791  *
792  * Returns the list of new path keys.
793  */
794 List *
796  RelOptInfo *joinrel,
797  JoinType jointype,
798  List *outer_pathkeys)
799 {
800  if (jointype == JOIN_FULL || jointype == JOIN_RIGHT)
801  return NIL;
802 
803  /*
804  * This used to be quite a complex bit of code, but now that all pathkey
805  * sublists start out life canonicalized, we don't have to do a darn thing
806  * here!
807  *
808  * We do, however, need to truncate the pathkeys list, since it may
809  * contain pathkeys that were useful for forming this joinrel but are
810  * uninteresting to higher levels.
811  */
812  return truncate_useless_pathkeys(root, joinrel, outer_pathkeys);
813 }
814 
815 /****************************************************************************
816  * PATHKEYS AND SORT CLAUSES
817  ****************************************************************************/
818 
819 /*
820  * make_pathkeys_for_sortclauses
821  * Generate a pathkeys list that represents the sort order specified
822  * by a list of SortGroupClauses
823  *
824  * The resulting PathKeys are always in canonical form. (Actually, there
825  * is no longer any code anywhere that creates non-canonical PathKeys.)
826  *
827  * We assume that root->nullable_baserels is the set of base relids that could
828  * have gone to NULL below the SortGroupClause expressions. This is okay if
829  * the expressions came from the query's top level (ORDER BY, DISTINCT, etc)
830  * and if this function is only invoked after deconstruct_jointree. In the
831  * future we might have to make callers pass in the appropriate
832  * nullable-relids set, but for now it seems unnecessary.
833  *
834  * 'sortclauses' is a list of SortGroupClause nodes
835  * 'tlist' is the targetlist to find the referenced tlist entries in
836  */
837 List *
839  List *sortclauses,
840  List *tlist)
841 {
842  List *pathkeys = NIL;
843  ListCell *l;
844 
845  foreach(l, sortclauses)
846  {
847  SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
848  Expr *sortkey;
849  PathKey *pathkey;
850 
851  sortkey = (Expr *) get_sortgroupclause_expr(sortcl, tlist);
852  Assert(OidIsValid(sortcl->sortop));
853  pathkey = make_pathkey_from_sortop(root,
854  sortkey,
855  root->nullable_baserels,
856  sortcl->sortop,
857  sortcl->nulls_first,
858  sortcl->tleSortGroupRef,
859  true);
860 
861  /* Canonical form eliminates redundant ordering keys */
862  if (!pathkey_is_redundant(pathkey, pathkeys))
863  pathkeys = lappend(pathkeys, pathkey);
864  }
865  return pathkeys;
866 }
867 
868 /****************************************************************************
869  * PATHKEYS AND MERGECLAUSES
870  ****************************************************************************/
871 
872 /*
873  * initialize_mergeclause_eclasses
874  * Set the EquivalenceClass links in a mergeclause restrictinfo.
875  *
876  * RestrictInfo contains fields in which we may cache pointers to
877  * EquivalenceClasses for the left and right inputs of the mergeclause.
878  * (If the mergeclause is a true equivalence clause these will be the
879  * same EquivalenceClass, otherwise not.) If the mergeclause is either
880  * used to generate an EquivalenceClass, or derived from an EquivalenceClass,
881  * then it's easy to set up the left_ec and right_ec members --- otherwise,
882  * this function should be called to set them up. We will generate new
883  * EquivalenceClauses if necessary to represent the mergeclause's left and
884  * right sides.
885  *
886  * Note this is called before EC merging is complete, so the links won't
887  * necessarily point to canonical ECs. Before they are actually used for
888  * anything, update_mergeclause_eclasses must be called to ensure that
889  * they've been updated to point to canonical ECs.
890  */
891 void
893 {
894  Expr *clause = restrictinfo->clause;
895  Oid lefttype,
896  righttype;
897 
898  /* Should be a mergeclause ... */
899  Assert(restrictinfo->mergeopfamilies != NIL);
900  /* ... with links not yet set */
901  Assert(restrictinfo->left_ec == NULL);
902  Assert(restrictinfo->right_ec == NULL);
903 
904  /* Need the declared input types of the operator */
905  op_input_types(((OpExpr *) clause)->opno, &lefttype, &righttype);
906 
907  /* Find or create a matching EquivalenceClass for each side */
908  restrictinfo->left_ec =
910  (Expr *) get_leftop(clause),
911  restrictinfo->nullable_relids,
912  restrictinfo->mergeopfamilies,
913  lefttype,
914  ((OpExpr *) clause)->inputcollid,
915  0,
916  NULL,
917  true);
918  restrictinfo->right_ec =
920  (Expr *) get_rightop(clause),
921  restrictinfo->nullable_relids,
922  restrictinfo->mergeopfamilies,
923  righttype,
924  ((OpExpr *) clause)->inputcollid,
925  0,
926  NULL,
927  true);
928 }
929 
930 /*
931  * update_mergeclause_eclasses
932  * Make the cached EquivalenceClass links valid in a mergeclause
933  * restrictinfo.
934  *
935  * These pointers should have been set by process_equivalence or
936  * initialize_mergeclause_eclasses, but they might have been set to
937  * non-canonical ECs that got merged later. Chase up to the canonical
938  * merged parent if so.
939  */
940 void
942 {
943  /* Should be a merge clause ... */
944  Assert(restrictinfo->mergeopfamilies != NIL);
945  /* ... with pointers already set */
946  Assert(restrictinfo->left_ec != NULL);
947  Assert(restrictinfo->right_ec != NULL);
948 
949  /* Chase up to the top as needed */
950  while (restrictinfo->left_ec->ec_merged)
951  restrictinfo->left_ec = restrictinfo->left_ec->ec_merged;
952  while (restrictinfo->right_ec->ec_merged)
953  restrictinfo->right_ec = restrictinfo->right_ec->ec_merged;
954 }
955 
956 /*
957  * find_mergeclauses_for_pathkeys
958  * This routine attempts to find a set of mergeclauses that can be
959  * used with a specified ordering for one of the input relations.
960  * If successful, it returns a list of mergeclauses.
961  *
962  * 'pathkeys' is a pathkeys list showing the ordering of an input path.
963  * 'outer_keys' is TRUE if these keys are for the outer input path,
964  * FALSE if for inner.
965  * 'restrictinfos' is a list of mergejoinable restriction clauses for the
966  * join relation being formed.
967  *
968  * The restrictinfos must be marked (via outer_is_left) to show which side
969  * of each clause is associated with the current outer path. (See
970  * select_mergejoin_clauses())
971  *
972  * The result is NIL if no merge can be done, else a maximal list of
973  * usable mergeclauses (represented as a list of their restrictinfo nodes).
974  */
975 List *
977  List *pathkeys,
978  bool outer_keys,
979  List *restrictinfos)
980 {
981  List *mergeclauses = NIL;
982  ListCell *i;
983 
984  /* make sure we have eclasses cached in the clauses */
985  foreach(i, restrictinfos)
986  {
987  RestrictInfo *rinfo = (RestrictInfo *) lfirst(i);
988 
989  update_mergeclause_eclasses(root, rinfo);
990  }
991 
992  foreach(i, pathkeys)
993  {
994  PathKey *pathkey = (PathKey *) lfirst(i);
995  EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
996  List *matched_restrictinfos = NIL;
997  ListCell *j;
998 
999  /*----------
1000  * A mergejoin clause matches a pathkey if it has the same EC.
1001  * If there are multiple matching clauses, take them all. In plain
1002  * inner-join scenarios we expect only one match, because
1003  * equivalence-class processing will have removed any redundant
1004  * mergeclauses. However, in outer-join scenarios there might be
1005  * multiple matches. An example is
1006  *
1007  * select * from a full join b
1008  * on a.v1 = b.v1 and a.v2 = b.v2 and a.v1 = b.v2;
1009  *
1010  * Given the pathkeys ({a.v1}, {a.v2}) it is okay to return all three
1011  * clauses (in the order a.v1=b.v1, a.v1=b.v2, a.v2=b.v2) and indeed
1012  * we *must* do so or we will be unable to form a valid plan.
1013  *
1014  * We expect that the given pathkeys list is canonical, which means
1015  * no two members have the same EC, so it's not possible for this
1016  * code to enter the same mergeclause into the result list twice.
1017  *
1018  * It's possible that multiple matching clauses might have different
1019  * ECs on the other side, in which case the order we put them into our
1020  * result makes a difference in the pathkeys required for the other
1021  * input path. However this routine hasn't got any info about which
1022  * order would be best, so we don't worry about that.
1023  *
1024  * It's also possible that the selected mergejoin clauses produce
1025  * a noncanonical ordering of pathkeys for the other side, ie, we
1026  * might select clauses that reference b.v1, b.v2, b.v1 in that
1027  * order. This is not harmful in itself, though it suggests that
1028  * the clauses are partially redundant. Since it happens only with
1029  * redundant query conditions, we don't bother to eliminate it.
1030  * make_inner_pathkeys_for_merge() has to delete duplicates when
1031  * it constructs the canonical pathkeys list, and we also have to
1032  * deal with the case in create_mergejoin_plan().
1033  *----------
1034  */
1035  foreach(j, restrictinfos)
1036  {
1037  RestrictInfo *rinfo = (RestrictInfo *) lfirst(j);
1038  EquivalenceClass *clause_ec;
1039 
1040  if (outer_keys)
1041  clause_ec = rinfo->outer_is_left ?
1042  rinfo->left_ec : rinfo->right_ec;
1043  else
1044  clause_ec = rinfo->outer_is_left ?
1045  rinfo->right_ec : rinfo->left_ec;
1046  if (clause_ec == pathkey_ec)
1047  matched_restrictinfos = lappend(matched_restrictinfos, rinfo);
1048  }
1049 
1050  /*
1051  * If we didn't find a mergeclause, we're done --- any additional
1052  * sort-key positions in the pathkeys are useless. (But we can still
1053  * mergejoin if we found at least one mergeclause.)
1054  */
1055  if (matched_restrictinfos == NIL)
1056  break;
1057 
1058  /*
1059  * If we did find usable mergeclause(s) for this sort-key position,
1060  * add them to result list.
1061  */
1062  mergeclauses = list_concat(mergeclauses, matched_restrictinfos);
1063  }
1064 
1065  return mergeclauses;
1066 }
1067 
1068 /*
1069  * select_outer_pathkeys_for_merge
1070  * Builds a pathkey list representing a possible sort ordering
1071  * that can be used with the given mergeclauses.
1072  *
1073  * 'mergeclauses' is a list of RestrictInfos for mergejoin clauses
1074  * that will be used in a merge join.
1075  * 'joinrel' is the join relation we are trying to construct.
1076  *
1077  * The restrictinfos must be marked (via outer_is_left) to show which side
1078  * of each clause is associated with the current outer path. (See
1079  * select_mergejoin_clauses())
1080  *
1081  * Returns a pathkeys list that can be applied to the outer relation.
1082  *
1083  * Since we assume here that a sort is required, there is no particular use
1084  * in matching any available ordering of the outerrel. (joinpath.c has an
1085  * entirely separate code path for considering sort-free mergejoins.) Rather,
1086  * it's interesting to try to match the requested query_pathkeys so that a
1087  * second output sort may be avoided; and failing that, we try to list "more
1088  * popular" keys (those with the most unmatched EquivalenceClass peers)
1089  * earlier, in hopes of making the resulting ordering useful for as many
1090  * higher-level mergejoins as possible.
1091  */
1092 List *
1094  List *mergeclauses,
1095  RelOptInfo *joinrel)
1096 {
1097  List *pathkeys = NIL;
1098  int nClauses = list_length(mergeclauses);
1099  EquivalenceClass **ecs;
1100  int *scores;
1101  int necs;
1102  ListCell *lc;
1103  int j;
1104 
1105  /* Might have no mergeclauses */
1106  if (nClauses == 0)
1107  return NIL;
1108 
1109  /*
1110  * Make arrays of the ECs used by the mergeclauses (dropping any
1111  * duplicates) and their "popularity" scores.
1112  */
1113  ecs = (EquivalenceClass **) palloc(nClauses * sizeof(EquivalenceClass *));
1114  scores = (int *) palloc(nClauses * sizeof(int));
1115  necs = 0;
1116 
1117  foreach(lc, mergeclauses)
1118  {
1119  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1120  EquivalenceClass *oeclass;
1121  int score;
1122  ListCell *lc2;
1123 
1124  /* get the outer eclass */
1125  update_mergeclause_eclasses(root, rinfo);
1126 
1127  if (rinfo->outer_is_left)
1128  oeclass = rinfo->left_ec;
1129  else
1130  oeclass = rinfo->right_ec;
1131 
1132  /* reject duplicates */
1133  for (j = 0; j < necs; j++)
1134  {
1135  if (ecs[j] == oeclass)
1136  break;
1137  }
1138  if (j < necs)
1139  continue;
1140 
1141  /* compute score */
1142  score = 0;
1143  foreach(lc2, oeclass->ec_members)
1144  {
1146 
1147  /* Potential future join partner? */
1148  if (!em->em_is_const && !em->em_is_child &&
1149  !bms_overlap(em->em_relids, joinrel->relids))
1150  score++;
1151  }
1152 
1153  ecs[necs] = oeclass;
1154  scores[necs] = score;
1155  necs++;
1156  }
1157 
1158  /*
1159  * Find out if we have all the ECs mentioned in query_pathkeys; if so we
1160  * can generate a sort order that's also useful for final output. There is
1161  * no percentage in a partial match, though, so we have to have 'em all.
1162  */
1163  if (root->query_pathkeys)
1164  {
1165  foreach(lc, root->query_pathkeys)
1166  {
1167  PathKey *query_pathkey = (PathKey *) lfirst(lc);
1168  EquivalenceClass *query_ec = query_pathkey->pk_eclass;
1169 
1170  for (j = 0; j < necs; j++)
1171  {
1172  if (ecs[j] == query_ec)
1173  break; /* found match */
1174  }
1175  if (j >= necs)
1176  break; /* didn't find match */
1177  }
1178  /* if we got to the end of the list, we have them all */
1179  if (lc == NULL)
1180  {
1181  /* copy query_pathkeys as starting point for our output */
1182  pathkeys = list_copy(root->query_pathkeys);
1183  /* mark their ECs as already-emitted */
1184  foreach(lc, root->query_pathkeys)
1185  {
1186  PathKey *query_pathkey = (PathKey *) lfirst(lc);
1187  EquivalenceClass *query_ec = query_pathkey->pk_eclass;
1188 
1189  for (j = 0; j < necs; j++)
1190  {
1191  if (ecs[j] == query_ec)
1192  {
1193  scores[j] = -1;
1194  break;
1195  }
1196  }
1197  }
1198  }
1199  }
1200 
1201  /*
1202  * Add remaining ECs to the list in popularity order, using a default sort
1203  * ordering. (We could use qsort() here, but the list length is usually
1204  * so small it's not worth it.)
1205  */
1206  for (;;)
1207  {
1208  int best_j;
1209  int best_score;
1210  EquivalenceClass *ec;
1211  PathKey *pathkey;
1212 
1213  best_j = 0;
1214  best_score = scores[0];
1215  for (j = 1; j < necs; j++)
1216  {
1217  if (scores[j] > best_score)
1218  {
1219  best_j = j;
1220  best_score = scores[j];
1221  }
1222  }
1223  if (best_score < 0)
1224  break; /* all done */
1225  ec = ecs[best_j];
1226  scores[best_j] = -1;
1227  pathkey = make_canonical_pathkey(root,
1228  ec,
1231  false);
1232  /* can't be redundant because no duplicate ECs */
1233  Assert(!pathkey_is_redundant(pathkey, pathkeys));
1234  pathkeys = lappend(pathkeys, pathkey);
1235  }
1236 
1237  pfree(ecs);
1238  pfree(scores);
1239 
1240  return pathkeys;
1241 }
1242 
1243 /*
1244  * make_inner_pathkeys_for_merge
1245  * Builds a pathkey list representing the explicit sort order that
1246  * must be applied to an inner path to make it usable with the
1247  * given mergeclauses.
1248  *
1249  * 'mergeclauses' is a list of RestrictInfos for mergejoin clauses
1250  * that will be used in a merge join.
1251  * 'outer_pathkeys' are the already-known canonical pathkeys for the outer
1252  * side of the join.
1253  *
1254  * The restrictinfos must be marked (via outer_is_left) to show which side
1255  * of each clause is associated with the current outer path. (See
1256  * select_mergejoin_clauses())
1257  *
1258  * Returns a pathkeys list that can be applied to the inner relation.
1259  *
1260  * Note that it is not this routine's job to decide whether sorting is
1261  * actually needed for a particular input path. Assume a sort is necessary;
1262  * just make the keys, eh?
1263  */
1264 List *
1266  List *mergeclauses,
1267  List *outer_pathkeys)
1268 {
1269  List *pathkeys = NIL;
1270  EquivalenceClass *lastoeclass;
1271  PathKey *opathkey;
1272  ListCell *lc;
1273  ListCell *lop;
1274 
1275  lastoeclass = NULL;
1276  opathkey = NULL;
1277  lop = list_head(outer_pathkeys);
1278 
1279  foreach(lc, mergeclauses)
1280  {
1281  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1282  EquivalenceClass *oeclass;
1283  EquivalenceClass *ieclass;
1284  PathKey *pathkey;
1285 
1286  update_mergeclause_eclasses(root, rinfo);
1287 
1288  if (rinfo->outer_is_left)
1289  {
1290  oeclass = rinfo->left_ec;
1291  ieclass = rinfo->right_ec;
1292  }
1293  else
1294  {
1295  oeclass = rinfo->right_ec;
1296  ieclass = rinfo->left_ec;
1297  }
1298 
1299  /* outer eclass should match current or next pathkeys */
1300  /* we check this carefully for debugging reasons */
1301  if (oeclass != lastoeclass)
1302  {
1303  if (!lop)
1304  elog(ERROR, "too few pathkeys for mergeclauses");
1305  opathkey = (PathKey *) lfirst(lop);
1306  lop = lnext(lop);
1307  lastoeclass = opathkey->pk_eclass;
1308  if (oeclass != lastoeclass)
1309  elog(ERROR, "outer pathkeys do not match mergeclause");
1310  }
1311 
1312  /*
1313  * Often, we'll have same EC on both sides, in which case the outer
1314  * pathkey is also canonical for the inner side, and we can skip a
1315  * useless search.
1316  */
1317  if (ieclass == oeclass)
1318  pathkey = opathkey;
1319  else
1320  pathkey = make_canonical_pathkey(root,
1321  ieclass,
1322  opathkey->pk_opfamily,
1323  opathkey->pk_strategy,
1324  opathkey->pk_nulls_first);
1325 
1326  /*
1327  * Don't generate redundant pathkeys (can happen if multiple
1328  * mergeclauses refer to same EC).
1329  */
1330  if (!pathkey_is_redundant(pathkey, pathkeys))
1331  pathkeys = lappend(pathkeys, pathkey);
1332  }
1333 
1334  return pathkeys;
1335 }
1336 
1337 /****************************************************************************
1338  * PATHKEY USEFULNESS CHECKS
1339  *
1340  * We only want to remember as many of the pathkeys of a path as have some
1341  * potential use, either for subsequent mergejoins or for meeting the query's
1342  * requested output ordering. This ensures that add_path() won't consider
1343  * a path to have a usefully different ordering unless it really is useful.
1344  * These routines check for usefulness of given pathkeys.
1345  ****************************************************************************/
1346 
1347 /*
1348  * pathkeys_useful_for_merging
1349  * Count the number of pathkeys that may be useful for mergejoins
1350  * above the given relation.
1351  *
1352  * We consider a pathkey potentially useful if it corresponds to the merge
1353  * ordering of either side of any joinclause for the rel. This might be
1354  * overoptimistic, since joinclauses that require different other relations
1355  * might never be usable at the same time, but trying to be exact is likely
1356  * to be more trouble than it's worth.
1357  *
1358  * To avoid doubling the number of mergejoin paths considered, we would like
1359  * to consider only one of the two scan directions (ASC or DESC) as useful
1360  * for merging for any given target column. The choice is arbitrary unless
1361  * one of the directions happens to match an ORDER BY key, in which case
1362  * that direction should be preferred, in hopes of avoiding a final sort step.
1363  * right_merge_direction() implements this heuristic.
1364  */
1365 static int
1367 {
1368  int useful = 0;
1369  ListCell *i;
1370 
1371  foreach(i, pathkeys)
1372  {
1373  PathKey *pathkey = (PathKey *) lfirst(i);
1374  bool matched = false;
1375  ListCell *j;
1376 
1377  /* If "wrong" direction, not useful for merging */
1378  if (!right_merge_direction(root, pathkey))
1379  break;
1380 
1381  /*
1382  * First look into the EquivalenceClass of the pathkey, to see if
1383  * there are any members not yet joined to the rel. If so, it's
1384  * surely possible to generate a mergejoin clause using them.
1385  */
1386  if (rel->has_eclass_joins &&
1387  eclass_useful_for_merging(root, pathkey->pk_eclass, rel))
1388  matched = true;
1389  else
1390  {
1391  /*
1392  * Otherwise search the rel's joininfo list, which contains
1393  * non-EquivalenceClass-derivable join clauses that might
1394  * nonetheless be mergejoinable.
1395  */
1396  foreach(j, rel->joininfo)
1397  {
1398  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(j);
1399 
1400  if (restrictinfo->mergeopfamilies == NIL)
1401  continue;
1402  update_mergeclause_eclasses(root, restrictinfo);
1403 
1404  if (pathkey->pk_eclass == restrictinfo->left_ec ||
1405  pathkey->pk_eclass == restrictinfo->right_ec)
1406  {
1407  matched = true;
1408  break;
1409  }
1410  }
1411  }
1412 
1413  /*
1414  * If we didn't find a mergeclause, we're done --- any additional
1415  * sort-key positions in the pathkeys are useless. (But we can still
1416  * mergejoin if we found at least one mergeclause.)
1417  */
1418  if (matched)
1419  useful++;
1420  else
1421  break;
1422  }
1423 
1424  return useful;
1425 }
1426 
1427 /*
1428  * right_merge_direction
1429  * Check whether the pathkey embodies the preferred sort direction
1430  * for merging its target column.
1431  */
1432 static bool
1434 {
1435  ListCell *l;
1436 
1437  foreach(l, root->query_pathkeys)
1438  {
1439  PathKey *query_pathkey = (PathKey *) lfirst(l);
1440 
1441  if (pathkey->pk_eclass == query_pathkey->pk_eclass &&
1442  pathkey->pk_opfamily == query_pathkey->pk_opfamily)
1443  {
1444  /*
1445  * Found a matching query sort column. Prefer this pathkey's
1446  * direction iff it matches. Note that we ignore pk_nulls_first,
1447  * which means that a sort might be needed anyway ... but we still
1448  * want to prefer only one of the two possible directions, and we
1449  * might as well use this one.
1450  */
1451  return (pathkey->pk_strategy == query_pathkey->pk_strategy);
1452  }
1453  }
1454 
1455  /* If no matching ORDER BY request, prefer the ASC direction */
1456  return (pathkey->pk_strategy == BTLessStrategyNumber);
1457 }
1458 
1459 /*
1460  * pathkeys_useful_for_ordering
1461  * Count the number of pathkeys that are useful for meeting the
1462  * query's requested output ordering.
1463  *
1464  * Unlike merge pathkeys, this is an all-or-nothing affair: it does us
1465  * no good to order by just the first key(s) of the requested ordering.
1466  * So the result is always either 0 or list_length(root->query_pathkeys).
1467  */
1468 static int
1470 {
1471  if (root->query_pathkeys == NIL)
1472  return 0; /* no special ordering requested */
1473 
1474  if (pathkeys == NIL)
1475  return 0; /* unordered path */
1476 
1477  if (pathkeys_contained_in(root->query_pathkeys, pathkeys))
1478  {
1479  /* It's useful ... or at least the first N keys are */
1480  return list_length(root->query_pathkeys);
1481  }
1482 
1483  return 0; /* path ordering not useful */
1484 }
1485 
1486 /*
1487  * truncate_useless_pathkeys
1488  * Shorten the given pathkey list to just the useful pathkeys.
1489  */
1490 List *
1492  RelOptInfo *rel,
1493  List *pathkeys)
1494 {
1495  int nuseful;
1496  int nuseful2;
1497 
1498  nuseful = pathkeys_useful_for_merging(root, rel, pathkeys);
1499  nuseful2 = pathkeys_useful_for_ordering(root, pathkeys);
1500  if (nuseful2 > nuseful)
1501  nuseful = nuseful2;
1502 
1503  /*
1504  * Note: not safe to modify input list destructively, but we can avoid
1505  * copying the list if we're not actually going to change it
1506  */
1507  if (nuseful == 0)
1508  return NIL;
1509  else if (nuseful == list_length(pathkeys))
1510  return pathkeys;
1511  else
1512  return list_truncate(list_copy(pathkeys), nuseful);
1513 }
1514 
1515 /*
1516  * has_useful_pathkeys
1517  * Detect whether the specified rel could have any pathkeys that are
1518  * useful according to truncate_useless_pathkeys().
1519  *
1520  * This is a cheap test that lets us skip building pathkeys at all in very
1521  * simple queries. It's OK to err in the direction of returning "true" when
1522  * there really aren't any usable pathkeys, but erring in the other direction
1523  * is bad --- so keep this in sync with the routines above!
1524  *
1525  * We could make the test more complex, for example checking to see if any of
1526  * the joinclauses are really mergejoinable, but that likely wouldn't win
1527  * often enough to repay the extra cycles. Queries with neither a join nor
1528  * a sort are reasonably common, though, so this much work seems worthwhile.
1529  */
1530 bool
1532 {
1533  if (rel->joininfo != NIL || rel->has_eclass_joins)
1534  return true; /* might be able to use pathkeys for merging */
1535  if (root->query_pathkeys != NIL)
1536  return true; /* might be able to use them for ordering */
1537  return false; /* definitely useless */
1538 }
bool has_eclass_joins
Definition: relation.h:551
signed short int16
Definition: c.h:252
#define NIL
Definition: pg_list.h:69
List * build_expression_pathkey(PlannerInfo *root, Expr *expr, Relids nullable_relids, Oid opno, Relids rel, bool create_it)
Definition: pathkeys.c:526
static PathKey * make_pathkey_from_sortop(PlannerInfo *root, Expr *expr, Relids nullable_relids, Oid ordering_op, bool nulls_first, Index sortref, bool create_it)
Definition: pathkeys.c:229
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
Oid * indexcollations
Definition: relation.h:600
void initialize_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: pathkeys.c:892
List * query_pathkeys
Definition: relation.h:257
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2870
List * make_pathkeys_for_sortclauses(PlannerInfo *root, List *sortclauses, List *tlist)
Definition: pathkeys.c:838
List * indextlist
Definition: relation.h:613
List * build_index_pathkeys(PlannerInfo *root, IndexOptInfo *index, ScanDirection scandir)
Definition: pathkeys.c:433
bool eclass_useful_for_merging(PlannerInfo *root, EquivalenceClass *eclass, RelOptInfo *rel)
Definition: equivclass.c:2393
List * get_mergejoin_opfamilies(Oid opno)
Definition: lsyscache.c:363
EquivalenceClass * get_eclass_for_sort_expr(PlannerInfo *root, Expr *expr, Relids nullable_relids, List *opfamilies, Oid opcintype, Oid collation, Index sortref, Relids rel, bool create_it)
Definition: equivclass.c:581
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Var * makeVarFromTargetEntry(Index varno, TargetEntry *tle)
Definition: makefuncs.c:104
List * list_truncate(List *list, int new_size)
Definition: list.c:350
Index tleSortGroupRef
Definition: parsenodes.h:1102
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:382
List * list_copy(const List *oldlist)
Definition: list.c:1160
Index ec_sortref
Definition: relation.h:723
Definition: nodes.h:508
List * make_inner_pathkeys_for_merge(PlannerInfo *root, List *mergeclauses, List *outer_pathkeys)
Definition: pathkeys.c:1265
Oid * sortopfamily
Definition: relation.h:603
List * list_concat(List *list1, List *list2)
Definition: list.c:321
PathKey * make_canonical_pathkey(PlannerInfo *root, EquivalenceClass *eclass, Oid opfamily, int strategy, bool nulls_first)
Definition: pathkeys.c:51
static int pathkeys_useful_for_ordering(PlannerInfo *root, List *pathkeys)
Definition: pathkeys.c:1469
EquivalenceClass * right_ec
Definition: relation.h:1686
PathKeysComparison compare_pathkeys(List *keys1, List *keys2)
Definition: pathkeys.c:278
unsigned int Oid
Definition: postgres_ext.h:31
List * truncate_useless_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
Definition: pathkeys.c:1491
#define OidIsValid(objectId)
Definition: c.h:533
List * build_join_pathkeys(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, List *outer_pathkeys)
Definition: pathkeys.c:795
List * mergeopfamilies
Definition: relation.h:1682
int pk_strategy
Definition: relation.h:793
static struct cvec * eclass(struct vars *v, chr c, int cases)
Definition: regc_locale.c:508
JoinType
Definition: nodes.h:665
Definition: type.h:90
#define list_make1(x1)
Definition: pg_list.h:133
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
void pfree(void *pointer)
Definition: mcxt.c:992
RelOptInfo * rel
Definition: relation.h:590
bool resjunk
Definition: primnodes.h:1337
#define linitial(l)
Definition: pg_list.h:110
bool pk_nulls_first
Definition: relation.h:794
#define ERROR
Definition: elog.h:43
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
Definition: equivclass.c:456
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:307
int compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
Definition: pathnode.c:61
void * list_nth(const List *list, int n)
Definition: list.c:410
List * joininfo
Definition: relation.h:549
List * convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *subquery_pathkeys, List *subquery_tlist)
Definition: pathkeys.c:580
bool outer_is_left
Definition: relation.h:1692
ScanDirection
Definition: sdir.h:22
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
Relids relids
Definition: relation.h:490
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1135
Path * get_cheapest_fractional_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, double fraction)
Definition: pathkeys.c:383
static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
Definition: pathkeys.c:128
#define lnext(lc)
Definition: pg_list.h:105
List * canon_pathkeys
Definition: relation.h:234
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:348
#define EC_MUST_BE_REDUNDANT(eclass)
Definition: relation.h:733
Index relid
Definition: relation.h:518
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1637
List * ec_opfamilies
Definition: relation.h:712
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
Relids nullable_relids
Definition: relation.h:1661
Relids em_relids
Definition: relation.h:763
unsigned int Index
Definition: c.h:361
List * select_outer_pathkeys_for_merge(PlannerInfo *root, List *mergeclauses, RelOptInfo *joinrel)
Definition: pathkeys.c:1093
List * pathkeys
Definition: relation.h:909
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1330
int compare_fractional_path_costs(Path *path1, Path *path2, double fraction)
Definition: pathnode.c:107
#define PATH_REQ_OUTER(path)
Definition: relation.h:914
EquivalenceClass * pk_eclass
Definition: relation.h:791
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, int16 *strategy)
Definition: lsyscache.c:204
#define linitial_oid(l)
Definition: pg_list.h:112
static int list_length(const List *l)
Definition: pg_list.h:89
bool ec_has_volatile
Definition: relation.h:720
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:748
CostSelector
Definition: relation.h:34
Oid * opcintype
Definition: relation.h:602
Oid pk_opfamily
Definition: relation.h:792
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
bool indexcol_is_bool_constant_for_query(IndexOptInfo *index, int indexcol)
Definition: indxpath.c:3114
List * find_mergeclauses_for_pathkeys(PlannerInfo *root, List *pathkeys, bool outer_keys, List *restrictinfos)
Definition: pathkeys.c:976
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:442
PathKeysComparison
Definition: paths.h:173
void * palloc(Size size)
Definition: mcxt.c:891
EquivalenceClass * left_ec
Definition: relation.h:1685
Relids nullable_baserels
Definition: relation.h:201
int i
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:1531
MemoryContext planner_cxt
Definition: relation.h:282
#define elog
Definition: elog.h:219
static PathKey * make_pathkey_from_sortinfo(PlannerInfo *root, Expr *expr, Relids nullable_relids, Oid opfamily, Oid opcintype, Oid collation, bool reverse_sort, bool nulls_first, Index sortref, Relids rel, bool create_it)
Definition: pathkeys.c:170
bool * nulls_first
Definition: relation.h:605
bool * reverse_sort
Definition: relation.h:604
#define BTLessStrategyNumber
Definition: stratnum.h:29
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion)
Definition: pathkeys.c:342
Definition: pg_list.h:45
static int pathkeys_useful_for_merging(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
Definition: pathkeys.c:1366
void update_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: pathkeys.c:941
Definition: relation.h:888
struct EquivalenceClass * ec_merged
Definition: relation.h:726
#define BTEqualStrategyNumber
Definition: stratnum.h:31
List * ec_members
Definition: relation.h:714
static bool right_merge_direction(PlannerInfo *root, PathKey *pathkey)
Definition: pathkeys.c:1433