PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pathkeys.c File Reference
#include "postgres.h"
#include "access/stratnum.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/plannodes.h"
#include "optimizer/clauses.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/tlist.h"
#include "utils/lsyscache.h"
Include dependency graph for pathkeys.c:

Go to the source code of this file.

Functions

static bool pathkey_is_redundant (PathKey *new_pathkey, List *pathkeys)
 
static bool right_merge_direction (PlannerInfo *root, PathKey *pathkey)
 
PathKeymake_canonical_pathkey (PlannerInfo *root, EquivalenceClass *eclass, Oid opfamily, int strategy, bool nulls_first)
 
static PathKeymake_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)
 
static PathKeymake_pathkey_from_sortop (PlannerInfo *root, Expr *expr, Relids nullable_relids, Oid ordering_op, bool nulls_first, Index sortref, bool create_it)
 
PathKeysComparison compare_pathkeys (List *keys1, List *keys2)
 
bool pathkeys_contained_in (List *keys1, List *keys2)
 
Pathget_cheapest_path_for_pathkeys (List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion)
 
Pathget_cheapest_fractional_path_for_pathkeys (List *paths, List *pathkeys, Relids required_outer, double fraction)
 
Listbuild_index_pathkeys (PlannerInfo *root, IndexOptInfo *index, ScanDirection scandir)
 
Listbuild_expression_pathkey (PlannerInfo *root, Expr *expr, Relids nullable_relids, Oid opno, Relids rel, bool create_it)
 
Listconvert_subquery_pathkeys (PlannerInfo *root, RelOptInfo *rel, List *subquery_pathkeys, List *subquery_tlist)
 
Listbuild_join_pathkeys (PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, List *outer_pathkeys)
 
Listmake_pathkeys_for_sortclauses (PlannerInfo *root, List *sortclauses, List *tlist)
 
void initialize_mergeclause_eclasses (PlannerInfo *root, RestrictInfo *restrictinfo)
 
void update_mergeclause_eclasses (PlannerInfo *root, RestrictInfo *restrictinfo)
 
Listfind_mergeclauses_for_pathkeys (PlannerInfo *root, List *pathkeys, bool outer_keys, List *restrictinfos)
 
Listselect_outer_pathkeys_for_merge (PlannerInfo *root, List *mergeclauses, RelOptInfo *joinrel)
 
Listmake_inner_pathkeys_for_merge (PlannerInfo *root, List *mergeclauses, List *outer_pathkeys)
 
static int pathkeys_useful_for_merging (PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
 
static int pathkeys_useful_for_ordering (PlannerInfo *root, List *pathkeys)
 
Listtruncate_useless_pathkeys (PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
 
bool has_useful_pathkeys (PlannerInfo *root, RelOptInfo *rel)
 

Function Documentation

List* build_expression_pathkey ( PlannerInfo root,
Expr expr,
Relids  nullable_relids,
Oid  opno,
Relids  rel,
bool  create_it 
)

Definition at line 526 of file pathkeys.c.

References BTGreaterStrategyNumber, elog, ERROR, exprCollation(), get_ordering_op_properties(), list_make1, make_pathkey_from_sortinfo(), and NIL.

Referenced by set_function_pathlist().

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 }
signed short int16
Definition: c.h:252
#define NIL
Definition: pg_list.h:69
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
Definition: nodes.h:508
unsigned int Oid
Definition: postgres_ext.h:31
#define list_make1(x1)
Definition: pg_list.h:133
#define ERROR
Definition: elog.h:43
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, int16 *strategy)
Definition: lsyscache.c:204
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
#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
Definition: pg_list.h:45
List* build_index_pathkeys ( PlannerInfo root,
IndexOptInfo index,
ScanDirection  scandir 
)

Definition at line 433 of file pathkeys.c.

References TargetEntry::expr, i, indexcol_is_bool_constant_for_query(), IndexOptInfo::indexcollations, IndexOptInfo::indextlist, lappend(), lfirst, make_pathkey_from_sortinfo(), NIL, NULL, IndexOptInfo::nulls_first, IndexOptInfo::opcintype, pathkey_is_redundant(), IndexOptInfo::rel, RelOptInfo::relids, IndexOptInfo::reverse_sort, ScanDirectionIsBackward, and IndexOptInfo::sortopfamily.

Referenced by build_index_paths().

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 }
#define NIL
Definition: pg_list.h:69
Oid * indexcollations
Definition: relation.h:600
List * indextlist
Definition: relation.h:613
Oid * sortopfamily
Definition: relation.h:603
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
RelOptInfo * rel
Definition: relation.h:590
Relids relids
Definition: relation.h:490
static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
Definition: pathkeys.c:128
List * lappend(List *list, void *datum)
Definition: list.c:128
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1330
Oid * opcintype
Definition: relation.h:602
bool indexcol_is_bool_constant_for_query(IndexOptInfo *index, int indexcol)
Definition: indxpath.c:3112
int i
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
Definition: pg_list.h:45
List* build_join_pathkeys ( PlannerInfo root,
RelOptInfo joinrel,
JoinType  jointype,
List outer_pathkeys 
)

Definition at line 795 of file pathkeys.c.

References JOIN_FULL, JOIN_RIGHT, NIL, and truncate_useless_pathkeys().

Referenced by consider_parallel_nestloop(), match_unsorted_outer(), and sort_inner_and_outer().

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 }
#define NIL
Definition: pg_list.h:69
List * truncate_useless_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
Definition: pathkeys.c:1491
PathKeysComparison compare_pathkeys ( List keys1,
List keys2 
)

Definition at line 278 of file pathkeys.c.

References forboth, lfirst, NULL, PATHKEYS_BETTER1, PATHKEYS_BETTER2, PATHKEYS_DIFFERENT, and PATHKEYS_EQUAL.

Referenced by add_partial_path(), add_partial_path_precheck(), add_path(), add_path_precheck(), pathkeys_contained_in(), set_append_rel_pathlist(), and set_cheapest().

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 }
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
List* convert_subquery_pathkeys ( PlannerInfo root,
RelOptInfo rel,
List subquery_pathkeys,
List subquery_tlist 
)

Definition at line 580 of file pathkeys.c.

References Assert, canonicalize_ec_expression(), EquivalenceClass::ec_collation, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_opfamilies, EquivalenceClass::ec_sortref, elog, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, equal(), ERROR, TargetEntry::expr, get_eclass_for_sort_expr(), get_sortgroupref_tle(), i, lappend(), lfirst, linitial, list_length(), list_nth(), make_canonical_pathkey(), makeVarFromTargetEntry(), NIL, NULL, pathkey_is_redundant(), PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, PlannerInfo::query_pathkeys, RelOptInfo::relid, RelOptInfo::relids, and TargetEntry::resjunk.

Referenced by set_subquery_pathlist().

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 }
#define NIL
Definition: pg_list.h:69
List * query_pathkeys
Definition: relation.h:257
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2870
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
Var * makeVarFromTargetEntry(Index varno, TargetEntry *tle)
Definition: makefuncs.c:104
Index ec_sortref
Definition: relation.h:723
PathKey * make_canonical_pathkey(PlannerInfo *root, EquivalenceClass *eclass, Oid opfamily, int strategy, bool nulls_first)
Definition: pathkeys.c:51
unsigned int Oid
Definition: postgres_ext.h:31
int pk_strategy
Definition: relation.h:793
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
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
Definition: equivclass.c:456
void * list_nth(const List *list, int n)
Definition: list.c:410
Relids relids
Definition: relation.h:490
static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
Definition: pathkeys.c:128
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:348
Index relid
Definition: relation.h:518
List * lappend(List *list, void *datum)
Definition: list.c:128
List * ec_opfamilies
Definition: relation.h:712
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1330
EquivalenceClass * pk_eclass
Definition: relation.h:791
static int list_length(const List *l)
Definition: pg_list.h:89
bool ec_has_volatile
Definition: relation.h:720
Oid pk_opfamily
Definition: relation.h:792
int i
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
List * ec_members
Definition: relation.h:714
List* find_mergeclauses_for_pathkeys ( PlannerInfo root,
List pathkeys,
bool  outer_keys,
List restrictinfos 
)

Definition at line 976 of file pathkeys.c.

References i, lappend(), RestrictInfo::left_ec, lfirst, list_concat(), NIL, RestrictInfo::outer_is_left, PathKey::pk_eclass, RestrictInfo::right_ec, and update_mergeclause_eclasses().

Referenced by generate_mergejoin_paths(), and sort_inner_and_outer().

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 }
#define NIL
Definition: pg_list.h:69
List * list_concat(List *list1, List *list2)
Definition: list.c:321
EquivalenceClass * right_ec
Definition: relation.h:1686
bool outer_is_left
Definition: relation.h:1692
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:791
EquivalenceClass * left_ec
Definition: relation.h:1685
int i
Definition: pg_list.h:45
void update_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: pathkeys.c:941
Path* get_cheapest_fractional_path_for_pathkeys ( List paths,
List pathkeys,
Relids  required_outer,
double  fraction 
)

Definition at line 383 of file pathkeys.c.

References bms_is_subset(), compare_fractional_path_costs(), lfirst, NULL, PATH_REQ_OUTER, Path::pathkeys, and pathkeys_contained_in().

Referenced by build_minmax_path().

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 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:307
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
List * pathkeys
Definition: relation.h:909
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
int compare_fractional_path_costs(Path *path1, Path *path2, double fraction)
Definition: pathnode.c:107
#define PATH_REQ_OUTER(path)
Definition: relation.h:914
Definition: relation.h:888
Path* get_cheapest_path_for_pathkeys ( List paths,
List pathkeys,
Relids  required_outer,
CostSelector  cost_criterion 
)

Definition at line 342 of file pathkeys.c.

References bms_is_subset(), compare_path_costs(), lfirst, NULL, PATH_REQ_OUTER, Path::pathkeys, and pathkeys_contained_in().

Referenced by generate_mergeappend_paths(), generate_mergejoin_paths(), and get_cheapest_parameterized_child_path().

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 }
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
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
List * pathkeys
Definition: relation.h:909
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
#define PATH_REQ_OUTER(path)
Definition: relation.h:914
Definition: relation.h:888
bool has_useful_pathkeys ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 1531 of file pathkeys.c.

References RelOptInfo::has_eclass_joins, RelOptInfo::joininfo, NIL, and PlannerInfo::query_pathkeys.

Referenced by build_index_paths(), and set_append_rel_size().

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
#define NIL
Definition: pg_list.h:69
List * query_pathkeys
Definition: relation.h:257
List * joininfo
Definition: relation.h:549
void initialize_mergeclause_eclasses ( PlannerInfo root,
RestrictInfo restrictinfo 
)

Definition at line 892 of file pathkeys.c.

References Assert, RestrictInfo::clause, get_eclass_for_sort_expr(), get_leftop(), get_rightop(), RestrictInfo::left_ec, RestrictInfo::mergeopfamilies, NIL, NULL, RestrictInfo::nullable_relids, op_input_types(), and RestrictInfo::right_ec.

Referenced by distribute_qual_to_rels().

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 }
#define NIL
Definition: pg_list.h:69
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
EquivalenceClass * right_ec
Definition: relation.h:1686
unsigned int Oid
Definition: postgres_ext.h:31
List * mergeopfamilies
Definition: relation.h:1682
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1135
Expr * clause
Definition: relation.h:1637
Relids nullable_relids
Definition: relation.h:1661
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
EquivalenceClass * left_ec
Definition: relation.h:1685
PathKey* make_canonical_pathkey ( PlannerInfo root,
EquivalenceClass eclass,
Oid  opfamily,
int  strategy,
bool  nulls_first 
)

Definition at line 51 of file pathkeys.c.

References PlannerInfo::canon_pathkeys, EquivalenceClass::ec_merged, eclass(), lappend(), lfirst, makeNode, MemoryContextSwitchTo(), PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, and PlannerInfo::planner_cxt.

Referenced by convert_subquery_pathkeys(), get_useful_pathkeys_for_relation(), make_inner_pathkeys_for_merge(), make_pathkey_from_sortinfo(), and select_outer_pathkeys_for_merge().

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 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int pk_strategy
Definition: relation.h:793
static struct cvec * eclass(struct vars *v, chr c, int cases)
Definition: regc_locale.c:508
bool pk_nulls_first
Definition: relation.h:794
List * canon_pathkeys
Definition: relation.h:234
List * lappend(List *list, void *datum)
Definition: list.c:128
#define makeNode(_type_)
Definition: nodes.h:556
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:791
Oid pk_opfamily
Definition: relation.h:792
MemoryContext planner_cxt
Definition: relation.h:282
struct EquivalenceClass * ec_merged
Definition: relation.h:726
List* make_inner_pathkeys_for_merge ( PlannerInfo root,
List mergeclauses,
List outer_pathkeys 
)

Definition at line 1265 of file pathkeys.c.

References elog, ERROR, lappend(), RestrictInfo::left_ec, lfirst, list_head(), lnext, make_canonical_pathkey(), NIL, NULL, RestrictInfo::outer_is_left, pathkey_is_redundant(), PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, RestrictInfo::right_ec, and update_mergeclause_eclasses().

Referenced by generate_mergejoin_paths(), and sort_inner_and_outer().

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 }
#define NIL
Definition: pg_list.h:69
PathKey * make_canonical_pathkey(PlannerInfo *root, EquivalenceClass *eclass, Oid opfamily, int strategy, bool nulls_first)
Definition: pathkeys.c:51
EquivalenceClass * right_ec
Definition: relation.h:1686
int pk_strategy
Definition: relation.h:793
bool pk_nulls_first
Definition: relation.h:794
#define ERROR
Definition: elog.h:43
bool outer_is_left
Definition: relation.h:1692
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
Definition: pathkeys.c:128
#define lnext(lc)
Definition: pg_list.h:105
List * lappend(List *list, void *datum)
Definition: list.c:128
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:791
Oid pk_opfamily
Definition: relation.h:792
EquivalenceClass * left_ec
Definition: relation.h:1685
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
void update_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: pathkeys.c:941
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 
)
static

Definition at line 170 of file pathkeys.c.

References BTEqualStrategyNumber, BTGreaterStrategyNumber, BTLessStrategyNumber, eclass(), elog, ERROR, get_eclass_for_sort_expr(), get_mergejoin_opfamilies(), get_opfamily_member(), make_canonical_pathkey(), NULL, and OidIsValid.

Referenced by build_expression_pathkey(), build_index_pathkeys(), and make_pathkey_from_sortop().

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 }
signed short int16
Definition: c.h:252
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
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
PathKey * make_canonical_pathkey(PlannerInfo *root, EquivalenceClass *eclass, Oid opfamily, int strategy, bool nulls_first)
Definition: pathkeys.c:51
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
static struct cvec * eclass(struct vars *v, chr c, int cases)
Definition: regc_locale.c:508
#define ERROR
Definition: elog.h:43
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
#define NULL
Definition: c.h:226
#define elog
Definition: elog.h:219
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:45
#define BTEqualStrategyNumber
Definition: stratnum.h:31
static PathKey* make_pathkey_from_sortop ( PlannerInfo root,
Expr expr,
Relids  nullable_relids,
Oid  ordering_op,
bool  nulls_first,
Index  sortref,
bool  create_it 
)
static

Definition at line 229 of file pathkeys.c.

References BTGreaterStrategyNumber, elog, ERROR, exprCollation(), get_ordering_op_properties(), make_pathkey_from_sortinfo(), and NULL.

Referenced by make_pathkeys_for_sortclauses().

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 }
signed short int16
Definition: c.h:252
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
Definition: nodes.h:508
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define NULL
Definition: c.h:226
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, int16 *strategy)
Definition: lsyscache.c:204
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
#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
List* make_pathkeys_for_sortclauses ( PlannerInfo root,
List sortclauses,
List tlist 
)

Definition at line 838 of file pathkeys.c.

References Assert, get_sortgroupclause_expr(), lappend(), lfirst, make_pathkey_from_sortop(), NIL, PlannerInfo::nullable_baserels, SortGroupClause::nulls_first, OidIsValid, pathkey_is_redundant(), SortGroupClause::sortop, and SortGroupClause::tleSortGroupRef.

Referenced by generate_nonunion_path(), get_column_info_for_window(), grouping_planner(), make_pathkeys_for_window(), make_union_unique(), minmax_qp_callback(), and standard_qp_callback().

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 }
#define NIL
Definition: pg_list.h:69
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
Index tleSortGroupRef
Definition: parsenodes.h:1102
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:382
#define OidIsValid(objectId)
Definition: c.h:534
static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
Definition: pathkeys.c:128
List * lappend(List *list, void *datum)
Definition: list.c:128
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
Relids nullable_baserels
Definition: relation.h:201
Definition: pg_list.h:45
static bool pathkey_is_redundant ( PathKey new_pathkey,
List pathkeys 
)
static

Definition at line 128 of file pathkeys.c.

References EC_MUST_BE_REDUNDANT, lfirst, and PathKey::pk_eclass.

Referenced by build_index_pathkeys(), convert_subquery_pathkeys(), make_inner_pathkeys_for_merge(), make_pathkeys_for_sortclauses(), and select_outer_pathkeys_for_merge().

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 }
#define EC_MUST_BE_REDUNDANT(eclass)
Definition: relation.h:733
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:791
bool pathkeys_contained_in ( List keys1,
List keys2 
)

Definition at line 317 of file pathkeys.c.

References compare_pathkeys(), PATHKEYS_BETTER2, and PATHKEYS_EQUAL.

Referenced by create_distinct_paths(), create_grouping_paths(), create_merge_append_path(), create_merge_append_plan(), create_one_window_path(), create_ordered_paths(), create_window_paths(), generate_mergejoin_paths(), get_cheapest_fractional_path_for_pathkeys(), get_cheapest_path_for_pathkeys(), pathkeys_useful_for_ordering(), and try_mergejoin_path().

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 }
PathKeysComparison compare_pathkeys(List *keys1, List *keys2)
Definition: pathkeys.c:278
static int pathkeys_useful_for_merging ( PlannerInfo root,
RelOptInfo rel,
List pathkeys 
)
static

Definition at line 1366 of file pathkeys.c.

References eclass_useful_for_merging(), RelOptInfo::has_eclass_joins, i, RelOptInfo::joininfo, RestrictInfo::left_ec, lfirst, RestrictInfo::mergeopfamilies, NIL, PathKey::pk_eclass, RestrictInfo::right_ec, right_merge_direction(), and update_mergeclause_eclasses().

Referenced by truncate_useless_pathkeys().

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 }
bool has_eclass_joins
Definition: relation.h:551
#define NIL
Definition: pg_list.h:69
bool eclass_useful_for_merging(PlannerInfo *root, EquivalenceClass *eclass, RelOptInfo *rel)
Definition: equivclass.c:2393
EquivalenceClass * right_ec
Definition: relation.h:1686
List * mergeopfamilies
Definition: relation.h:1682
List * joininfo
Definition: relation.h:549
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:791
EquivalenceClass * left_ec
Definition: relation.h:1685
int i
void update_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: pathkeys.c:941
static bool right_merge_direction(PlannerInfo *root, PathKey *pathkey)
Definition: pathkeys.c:1433
static int pathkeys_useful_for_ordering ( PlannerInfo root,
List pathkeys 
)
static

Definition at line 1469 of file pathkeys.c.

References list_length(), NIL, pathkeys_contained_in(), and PlannerInfo::query_pathkeys.

Referenced by truncate_useless_pathkeys().

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 }
#define NIL
Definition: pg_list.h:69
List * query_pathkeys
Definition: relation.h:257
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
static int list_length(const List *l)
Definition: pg_list.h:89
static bool right_merge_direction ( PlannerInfo root,
PathKey pathkey 
)
static

Definition at line 1433 of file pathkeys.c.

References BTLessStrategyNumber, lfirst, PathKey::pk_eclass, PathKey::pk_opfamily, PathKey::pk_strategy, and PlannerInfo::query_pathkeys.

Referenced by pathkeys_useful_for_merging().

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 }
List * query_pathkeys
Definition: relation.h:257
int pk_strategy
Definition: relation.h:793
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:791
Oid pk_opfamily
Definition: relation.h:792
#define BTLessStrategyNumber
Definition: stratnum.h:29
List* select_outer_pathkeys_for_merge ( PlannerInfo root,
List mergeclauses,
RelOptInfo joinrel 
)

Definition at line 1093 of file pathkeys.c.

References Assert, bms_overlap(), BTLessStrategyNumber, EquivalenceClass::ec_members, EquivalenceClass::ec_opfamilies, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_relids, lappend(), RestrictInfo::left_ec, lfirst, linitial_oid, list_copy(), list_length(), make_canonical_pathkey(), NIL, NULL, RestrictInfo::outer_is_left, palloc(), pathkey_is_redundant(), pfree(), PathKey::pk_eclass, PlannerInfo::query_pathkeys, RelOptInfo::relids, RestrictInfo::right_ec, and update_mergeclause_eclasses().

Referenced by sort_inner_and_outer().

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 }
#define NIL
Definition: pg_list.h:69
List * query_pathkeys
Definition: relation.h:257
List * list_copy(const List *oldlist)
Definition: list.c:1160
PathKey * make_canonical_pathkey(PlannerInfo *root, EquivalenceClass *eclass, Oid opfamily, int strategy, bool nulls_first)
Definition: pathkeys.c:51
EquivalenceClass * right_ec
Definition: relation.h:1686
void pfree(void *pointer)
Definition: mcxt.c:992
bool outer_is_left
Definition: relation.h:1692
Relids relids
Definition: relation.h:490
static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
Definition: pathkeys.c:128
List * lappend(List *list, void *datum)
Definition: list.c:128
List * ec_opfamilies
Definition: relation.h:712
Relids em_relids
Definition: relation.h:763
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:791
#define linitial_oid(l)
Definition: pg_list.h:112
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:442
void * palloc(Size size)
Definition: mcxt.c:891
EquivalenceClass * left_ec
Definition: relation.h:1685
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:45
void update_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: pathkeys.c:941
List * ec_members
Definition: relation.h:714
List* truncate_useless_pathkeys ( PlannerInfo root,
RelOptInfo rel,
List pathkeys 
)

Definition at line 1491 of file pathkeys.c.

References list_copy(), list_length(), list_truncate(), NIL, pathkeys_useful_for_merging(), and pathkeys_useful_for_ordering().

Referenced by build_index_paths(), and build_join_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 }
#define NIL
Definition: pg_list.h:69
List * list_truncate(List *list, int new_size)
Definition: list.c:350
List * list_copy(const List *oldlist)
Definition: list.c:1160
static int pathkeys_useful_for_ordering(PlannerInfo *root, List *pathkeys)
Definition: pathkeys.c:1469
static int list_length(const List *l)
Definition: pg_list.h:89
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 at line 941 of file pathkeys.c.

References Assert, EquivalenceClass::ec_merged, RestrictInfo::left_ec, RestrictInfo::mergeopfamilies, NIL, NULL, and RestrictInfo::right_ec.

Referenced by find_mergeclauses_for_pathkeys(), get_useful_ecs_for_relation(), make_inner_pathkeys_for_merge(), pathkeys_useful_for_merging(), select_mergejoin_clauses(), and select_outer_pathkeys_for_merge().

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 }
#define NIL
Definition: pg_list.h:69
EquivalenceClass * right_ec
Definition: relation.h:1686
List * mergeopfamilies
Definition: relation.h:1682
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
EquivalenceClass * left_ec
Definition: relation.h:1685
struct EquivalenceClass * ec_merged
Definition: relation.h:726