PostgreSQL Source Code  git master
indxpath.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/stratnum.h"
#include "access/sysattr.h"
#include "catalog/pg_am.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_opfamily.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "optimizer/cost.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/prep.h"
#include "optimizer/restrictinfo.h"
#include "utils/lsyscache.h"
#include "utils/selfuncs.h"
Include dependency graph for indxpath.c:

Go to the source code of this file.

Data Structures

struct  IndexClauseSet
 
struct  PathClauseUsage
 
struct  ec_member_matches_arg
 

Macros

#define IndexCollMatchesExprColl(idxcollation, exprcollation)    ((idxcollation) == InvalidOid || (idxcollation) == (exprcollation))
 

Enumerations

enum  ScanTypeControl { ST_INDEXSCAN , ST_BITMAPSCAN , ST_ANYSCAN }
 

Functions

static void consider_index_join_clauses (PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *rclauseset, IndexClauseSet *jclauseset, IndexClauseSet *eclauseset, List **bitindexpaths)
 
static void consider_index_join_outer_rels (PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *rclauseset, IndexClauseSet *jclauseset, IndexClauseSet *eclauseset, List **bitindexpaths, List *indexjoinclauses, int considered_clauses, List **considered_relids)
 
static void get_join_index_paths (PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *rclauseset, IndexClauseSet *jclauseset, IndexClauseSet *eclauseset, List **bitindexpaths, Relids relids, List **considered_relids)
 
static bool eclass_already_used (EquivalenceClass *parent_ec, Relids oldrelids, List *indexjoinclauses)
 
static bool bms_equal_any (Relids relids, List *relids_list)
 
static void get_index_paths (PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauses, List **bitindexpaths)
 
static Listbuild_index_paths (PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauses, bool useful_predicate, ScanTypeControl scantype, bool *skip_nonnative_saop, bool *skip_lower_saop)
 
static Listbuild_paths_for_OR (PlannerInfo *root, RelOptInfo *rel, List *clauses, List *other_clauses)
 
static Listgenerate_bitmap_or_paths (PlannerInfo *root, RelOptInfo *rel, List *clauses, List *other_clauses)
 
static Pathchoose_bitmap_and (PlannerInfo *root, RelOptInfo *rel, List *paths)
 
static int path_usage_comparator (const void *a, const void *b)
 
static Cost bitmap_scan_cost_est (PlannerInfo *root, RelOptInfo *rel, Path *ipath)
 
static Cost bitmap_and_cost_est (PlannerInfo *root, RelOptInfo *rel, List *paths)
 
static PathClauseUsageclassify_index_clause_usage (Path *path, List **clauselist)
 
static void find_indexpath_quals (Path *bitmapqual, List **quals, List **preds)
 
static int find_list_position (Node *node, List **nodelist)
 
static bool check_index_only (RelOptInfo *rel, IndexOptInfo *index)
 
static double get_loop_count (PlannerInfo *root, Index cur_relid, Relids outer_relids)
 
static double adjust_rowcount_for_semijoins (PlannerInfo *root, Index cur_relid, Index outer_relid, double rowcount)
 
static double approximate_joinrel_size (PlannerInfo *root, Relids relids)
 
static void match_restriction_clauses_to_index (PlannerInfo *root, IndexOptInfo *index, IndexClauseSet *clauseset)
 
static void match_join_clauses_to_index (PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauseset, List **joinorclauses)
 
static void match_eclass_clauses_to_index (PlannerInfo *root, IndexOptInfo *index, IndexClauseSet *clauseset)
 
static void match_clauses_to_index (PlannerInfo *root, List *clauses, IndexOptInfo *index, IndexClauseSet *clauseset)
 
static void match_clause_to_index (PlannerInfo *root, RestrictInfo *rinfo, IndexOptInfo *index, IndexClauseSet *clauseset)
 
static IndexClausematch_clause_to_indexcol (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClausematch_boolean_index_clause (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClausematch_opclause_to_indexcol (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClausematch_funcclause_to_indexcol (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClauseget_index_clause_from_support (PlannerInfo *root, RestrictInfo *rinfo, Oid funcid, int indexarg, int indexcol, IndexOptInfo *index)
 
static IndexClausematch_saopclause_to_indexcol (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClausematch_rowcompare_to_indexcol (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClauseexpand_indexqual_rowcompare (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index, Oid expr_op, bool var_on_left)
 
static void match_pathkeys_to_index (IndexOptInfo *index, List *pathkeys, List **orderby_clauses_p, List **clause_columns_p)
 
static Exprmatch_clause_to_ordering_op (IndexOptInfo *index, int indexcol, Expr *clause, Oid pk_opfamily)
 
static bool ec_member_matches_indexcol (PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, EquivalenceMember *em, void *arg)
 
void create_index_paths (PlannerInfo *root, RelOptInfo *rel)
 
void check_index_predicates (PlannerInfo *root, RelOptInfo *rel)
 
bool relation_has_unique_index_for (PlannerInfo *root, RelOptInfo *rel, List *restrictlist, List *exprlist, List *oprlist)
 
bool indexcol_is_bool_constant_for_query (PlannerInfo *root, IndexOptInfo *index, int indexcol)
 
bool match_index_to_operand (Node *operand, int indexcol, IndexOptInfo *index)
 
bool is_pseudo_constant_for_index (PlannerInfo *root, Node *expr, IndexOptInfo *index)
 

Macro Definition Documentation

◆ IndexCollMatchesExprColl

#define IndexCollMatchesExprColl (   idxcollation,
  exprcollation 
)     ((idxcollation) == InvalidOid || (idxcollation) == (exprcollation))

Definition at line 40 of file indxpath.c.

Enumeration Type Documentation

◆ ScanTypeControl

Enumerator
ST_INDEXSCAN 
ST_BITMAPSCAN 
ST_ANYSCAN 

Definition at line 44 of file indxpath.c.

45 {
46  ST_INDEXSCAN, /* must support amgettuple */
47  ST_BITMAPSCAN, /* must support amgetbitmap */
48  ST_ANYSCAN /* either is okay */
ScanTypeControl
Definition: indxpath.c:45
@ ST_ANYSCAN
Definition: indxpath.c:48
@ ST_BITMAPSCAN
Definition: indxpath.c:47
@ ST_INDEXSCAN
Definition: indxpath.c:46

Function Documentation

◆ adjust_rowcount_for_semijoins()

static double adjust_rowcount_for_semijoins ( PlannerInfo root,
Index  cur_relid,
Index  outer_relid,
double  rowcount 
)
static

Definition at line 1957 of file indxpath.c.

1961 {
1962  ListCell *lc;
1963 
1964  foreach(lc, root->join_info_list)
1965  {
1966  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
1967 
1968  if (sjinfo->jointype == JOIN_SEMI &&
1969  bms_is_member(cur_relid, sjinfo->syn_lefthand) &&
1970  bms_is_member(outer_relid, sjinfo->syn_righthand))
1971  {
1972  /* Estimate number of unique-ified rows */
1973  double nraw;
1974  double nunique;
1975 
1976  nraw = approximate_joinrel_size(root, sjinfo->syn_righthand);
1977  nunique = estimate_num_groups(root,
1978  sjinfo->semi_rhs_exprs,
1979  nraw,
1980  NULL,
1981  NULL);
1982  if (rowcount > nunique)
1983  rowcount = nunique;
1984  }
1985  }
1986  return rowcount;
1987 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
static double approximate_joinrel_size(PlannerInfo *root, Relids relids)
Definition: indxpath.c:2001
@ JOIN_SEMI
Definition: nodes.h:763
#define lfirst(lc)
Definition: pg_list.h:169
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, EstimationInfo *estinfo)
Definition: selfuncs.c:3368
List * join_info_list
Definition: pathnodes.h:267
Relids syn_lefthand
Definition: pathnodes.h:2274
List * semi_rhs_exprs
Definition: pathnodes.h:2283
JoinType jointype
Definition: pathnodes.h:2276
Relids syn_righthand
Definition: pathnodes.h:2275

References approximate_joinrel_size(), bms_is_member(), estimate_num_groups(), PlannerInfo::join_info_list, JOIN_SEMI, SpecialJoinInfo::jointype, lfirst, SpecialJoinInfo::semi_rhs_exprs, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by get_loop_count().

◆ approximate_joinrel_size()

static double approximate_joinrel_size ( PlannerInfo root,
Relids  relids 
)
static

Definition at line 2001 of file indxpath.c.

2002 {
2003  double rowcount = 1.0;
2004  int relid;
2005 
2006  relid = -1;
2007  while ((relid = bms_next_member(relids, relid)) >= 0)
2008  {
2009  RelOptInfo *rel;
2010 
2011  /* Paranoia: ignore bogus relid indexes */
2012  if (relid >= root->simple_rel_array_size)
2013  continue;
2014  rel = root->simple_rel_array[relid];
2015  if (rel == NULL)
2016  continue;
2017  Assert(rel->relid == relid); /* sanity check on array */
2018 
2019  /* Relation could be proven empty, if so ignore */
2020  if (IS_DUMMY_REL(rel))
2021  continue;
2022 
2023  /* Otherwise, rel's rows estimate should be valid by now */
2024  Assert(rel->rows > 0);
2025 
2026  /* Accumulate product */
2027  rowcount *= rel->rows;
2028  }
2029  return rowcount;
2030 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1045
Assert(fmt[strlen(fmt) - 1] !='\n')
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1478
int simple_rel_array_size
Definition: pathnodes.h:187
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:186
Index relid
Definition: pathnodes.h:710
Cardinality rows
Definition: pathnodes.h:685

References Assert(), bms_next_member(), IS_DUMMY_REL, RelOptInfo::relid, RelOptInfo::rows, PlannerInfo::simple_rel_array, and PlannerInfo::simple_rel_array_size.

Referenced by adjust_rowcount_for_semijoins().

◆ bitmap_and_cost_est()

static Cost bitmap_and_cost_est ( PlannerInfo root,
RelOptInfo rel,
List paths 
)
static

Definition at line 1635 of file indxpath.c.

1636 {
1637  BitmapAndPath *apath;
1638 
1639  /*
1640  * Might as well build a real BitmapAndPath here, as the work is slightly
1641  * too complicated to be worth repeating just to save one palloc.
1642  */
1643  apath = create_bitmap_and_path(root, rel, paths);
1644 
1645  return bitmap_scan_cost_est(root, rel, (Path *) apath);
1646 }
static Cost bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel, Path *ipath)
Definition: indxpath.c:1601
BitmapAndPath * create_bitmap_and_path(PlannerInfo *root, RelOptInfo *rel, List *bitmapquals)
Definition: pathnode.c:1079

References bitmap_scan_cost_est(), and create_bitmap_and_path().

Referenced by choose_bitmap_and().

◆ bitmap_scan_cost_est()

static Cost bitmap_scan_cost_est ( PlannerInfo root,
RelOptInfo rel,
Path ipath 
)
static

Definition at line 1601 of file indxpath.c.

1602 {
1603  BitmapHeapPath bpath;
1604 
1605  /* Set up a dummy BitmapHeapPath */
1606  bpath.path.type = T_BitmapHeapPath;
1607  bpath.path.pathtype = T_BitmapHeapScan;
1608  bpath.path.parent = rel;
1609  bpath.path.pathtarget = rel->reltarget;
1610  bpath.path.param_info = ipath->param_info;
1611  bpath.path.pathkeys = NIL;
1612  bpath.bitmapqual = ipath;
1613 
1614  /*
1615  * Check the cost of temporary path without considering parallelism.
1616  * Parallel bitmap heap path will be considered at later stage.
1617  */
1618  bpath.path.parallel_workers = 0;
1619 
1620  /* Now we can do cost_bitmap_heap_scan */
1621  cost_bitmap_heap_scan(&bpath.path, root, rel,
1622  bpath.path.param_info,
1623  ipath,
1624  get_loop_count(root, rel->relid,
1625  PATH_REQ_OUTER(ipath)));
1626 
1627  return bpath.path.total_cost;
1628 }
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:956
static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
Definition: indxpath.c:1904
@ T_BitmapHeapPath
Definition: nodes.h:244
@ T_BitmapHeapScan
Definition: nodes.h:61
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1213
#define NIL
Definition: pg_list.h:65
Path * bitmapqual
Definition: pathnodes.h:1330
List * pathkeys
Definition: pathnodes.h:1208
NodeTag type
Definition: pathnodes.h:1190
NodeTag pathtype
Definition: pathnodes.h:1192
RelOptInfo * parent
Definition: pathnodes.h:1194
int parallel_workers
Definition: pathnodes.h:1201
ParamPathInfo * param_info
Definition: pathnodes.h:1197
PathTarget * pathtarget
Definition: pathnodes.h:1195
Cost total_cost
Definition: pathnodes.h:1206
struct PathTarget * reltarget
Definition: pathnodes.h:693

References BitmapHeapPath::bitmapqual, cost_bitmap_heap_scan(), get_loop_count(), NIL, Path::parallel_workers, Path::param_info, Path::parent, BitmapHeapPath::path, PATH_REQ_OUTER, Path::pathkeys, Path::pathtarget, Path::pathtype, RelOptInfo::relid, RelOptInfo::reltarget, T_BitmapHeapPath, T_BitmapHeapScan, Path::total_cost, and Path::type.

Referenced by bitmap_and_cost_est(), and choose_bitmap_and().

◆ bms_equal_any()

static bool bms_equal_any ( Relids  relids,
List relids_list 
)
static

Definition at line 704 of file indxpath.c.

705 {
706  ListCell *lc;
707 
708  foreach(lc, relids_list)
709  {
710  if (bms_equal(relids, (Relids) lfirst(lc)))
711  return true;
712  }
713  return false;
714 }
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

References bms_equal(), and lfirst.

Referenced by consider_index_join_outer_rels(), create_index_paths(), and get_join_index_paths().

◆ build_index_paths()

static List * build_index_paths ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet clauses,
bool  useful_predicate,
ScanTypeControl  scantype,
bool skip_nonnative_saop,
bool skip_lower_saop 
)
static

Definition at line 855 of file indxpath.c.

861 {
862  List *result = NIL;
863  IndexPath *ipath;
864  List *index_clauses;
865  Relids outer_relids;
866  double loop_count;
867  List *orderbyclauses;
868  List *orderbyclausecols;
869  List *index_pathkeys;
870  List *useful_pathkeys;
871  bool found_lower_saop_clause;
872  bool pathkeys_possibly_useful;
873  bool index_is_ordered;
874  bool index_only_scan;
875  int indexcol;
876 
877  /*
878  * Check that index supports the desired scan type(s)
879  */
880  switch (scantype)
881  {
882  case ST_INDEXSCAN:
883  if (!index->amhasgettuple)
884  return NIL;
885  break;
886  case ST_BITMAPSCAN:
887  if (!index->amhasgetbitmap)
888  return NIL;
889  break;
890  case ST_ANYSCAN:
891  /* either or both are OK */
892  break;
893  }
894 
895  /*
896  * 1. Combine the per-column IndexClause lists into an overall list.
897  *
898  * In the resulting list, clauses are ordered by index key, so that the
899  * column numbers form a nondecreasing sequence. (This order is depended
900  * on by btree and possibly other places.) The list can be empty, if the
901  * index AM allows that.
902  *
903  * found_lower_saop_clause is set true if we accept a ScalarArrayOpExpr
904  * index clause for a non-first index column. This prevents us from
905  * assuming that the scan result is ordered. (Actually, the result is
906  * still ordered if there are equality constraints for all earlier
907  * columns, but it seems too expensive and non-modular for this code to be
908  * aware of that refinement.)
909  *
910  * We also build a Relids set showing which outer rels are required by the
911  * selected clauses. Any lateral_relids are included in that, but not
912  * otherwise accounted for.
913  */
914  index_clauses = NIL;
915  found_lower_saop_clause = false;
916  outer_relids = bms_copy(rel->lateral_relids);
917  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
918  {
919  ListCell *lc;
920 
921  foreach(lc, clauses->indexclauses[indexcol])
922  {
923  IndexClause *iclause = (IndexClause *) lfirst(lc);
924  RestrictInfo *rinfo = iclause->rinfo;
925 
926  /* We might need to omit ScalarArrayOpExpr clauses */
927  if (IsA(rinfo->clause, ScalarArrayOpExpr))
928  {
929  if (!index->amsearcharray)
930  {
931  if (skip_nonnative_saop)
932  {
933  /* Ignore because not supported by index */
934  *skip_nonnative_saop = true;
935  continue;
936  }
937  /* Caller had better intend this only for bitmap scan */
938  Assert(scantype == ST_BITMAPSCAN);
939  }
940  if (indexcol > 0)
941  {
942  if (skip_lower_saop)
943  {
944  /* Caller doesn't want to lose index ordering */
945  *skip_lower_saop = true;
946  continue;
947  }
948  found_lower_saop_clause = true;
949  }
950  }
951 
952  /* OK to include this clause */
953  index_clauses = lappend(index_clauses, iclause);
954  outer_relids = bms_add_members(outer_relids,
955  rinfo->clause_relids);
956  }
957 
958  /*
959  * If no clauses match the first index column, check for amoptionalkey
960  * restriction. We can't generate a scan over an index with
961  * amoptionalkey = false unless there's at least one index clause.
962  * (When working on columns after the first, this test cannot fail. It
963  * is always okay for columns after the first to not have any
964  * clauses.)
965  */
966  if (index_clauses == NIL && !index->amoptionalkey)
967  return NIL;
968  }
969 
970  /* We do not want the index's rel itself listed in outer_relids */
971  outer_relids = bms_del_member(outer_relids, rel->relid);
972  /* Enforce convention that outer_relids is exactly NULL if empty */
973  if (bms_is_empty(outer_relids))
974  outer_relids = NULL;
975 
976  /* Compute loop_count for cost estimation purposes */
977  loop_count = get_loop_count(root, rel->relid, outer_relids);
978 
979  /*
980  * 2. Compute pathkeys describing index's ordering, if any, then see how
981  * many of them are actually useful for this query. This is not relevant
982  * if we are only trying to build bitmap indexscans, nor if we have to
983  * assume the scan is unordered.
984  */
985  pathkeys_possibly_useful = (scantype != ST_BITMAPSCAN &&
986  !found_lower_saop_clause &&
987  has_useful_pathkeys(root, rel));
988  index_is_ordered = (index->sortopfamily != NULL);
989  if (index_is_ordered && pathkeys_possibly_useful)
990  {
991  index_pathkeys = build_index_pathkeys(root, index,
993  useful_pathkeys = truncate_useless_pathkeys(root, rel,
994  index_pathkeys);
995  orderbyclauses = NIL;
996  orderbyclausecols = NIL;
997  }
998  else if (index->amcanorderbyop && pathkeys_possibly_useful)
999  {
1000  /* see if we can generate ordering operators for query_pathkeys */
1002  &orderbyclauses,
1003  &orderbyclausecols);
1004  if (orderbyclauses)
1005  useful_pathkeys = root->query_pathkeys;
1006  else
1007  useful_pathkeys = NIL;
1008  }
1009  else
1010  {
1011  useful_pathkeys = NIL;
1012  orderbyclauses = NIL;
1013  orderbyclausecols = NIL;
1014  }
1015 
1016  /*
1017  * 3. Check if an index-only scan is possible. If we're not building
1018  * plain indexscans, this isn't relevant since bitmap scans don't support
1019  * index data retrieval anyway.
1020  */
1021  index_only_scan = (scantype != ST_BITMAPSCAN &&
1022  check_index_only(rel, index));
1023 
1024  /*
1025  * 4. Generate an indexscan path if there are relevant restriction clauses
1026  * in the current clauses, OR the index ordering is potentially useful for
1027  * later merging or final output ordering, OR the index has a useful
1028  * predicate, OR an index-only scan is possible.
1029  */
1030  if (index_clauses != NIL || useful_pathkeys != NIL || useful_predicate ||
1031  index_only_scan)
1032  {
1033  ipath = create_index_path(root, index,
1034  index_clauses,
1035  orderbyclauses,
1036  orderbyclausecols,
1037  useful_pathkeys,
1038  index_is_ordered ?
1041  index_only_scan,
1042  outer_relids,
1043  loop_count,
1044  false);
1045  result = lappend(result, ipath);
1046 
1047  /*
1048  * If appropriate, consider parallel index scan. We don't allow
1049  * parallel index scan for bitmap index scans.
1050  */
1051  if (index->amcanparallel &&
1052  rel->consider_parallel && outer_relids == NULL &&
1053  scantype != ST_BITMAPSCAN)
1054  {
1055  ipath = create_index_path(root, index,
1056  index_clauses,
1057  orderbyclauses,
1058  orderbyclausecols,
1059  useful_pathkeys,
1060  index_is_ordered ?
1063  index_only_scan,
1064  outer_relids,
1065  loop_count,
1066  true);
1067 
1068  /*
1069  * if, after costing the path, we find that it's not worth using
1070  * parallel workers, just free it.
1071  */
1072  if (ipath->path.parallel_workers > 0)
1073  add_partial_path(rel, (Path *) ipath);
1074  else
1075  pfree(ipath);
1076  }
1077  }
1078 
1079  /*
1080  * 5. If the index is ordered, a backwards scan might be interesting.
1081  */
1082  if (index_is_ordered && pathkeys_possibly_useful)
1083  {
1084  index_pathkeys = build_index_pathkeys(root, index,
1086  useful_pathkeys = truncate_useless_pathkeys(root, rel,
1087  index_pathkeys);
1088  if (useful_pathkeys != NIL)
1089  {
1090  ipath = create_index_path(root, index,
1091  index_clauses,
1092  NIL,
1093  NIL,
1094  useful_pathkeys,
1096  index_only_scan,
1097  outer_relids,
1098  loop_count,
1099  false);
1100  result = lappend(result, ipath);
1101 
1102  /* If appropriate, consider parallel index scan */
1103  if (index->amcanparallel &&
1104  rel->consider_parallel && outer_relids == NULL &&
1105  scantype != ST_BITMAPSCAN)
1106  {
1107  ipath = create_index_path(root, index,
1108  index_clauses,
1109  NIL,
1110  NIL,
1111  useful_pathkeys,
1113  index_only_scan,
1114  outer_relids,
1115  loop_count,
1116  true);
1117 
1118  /*
1119  * if, after costing the path, we find that it's not worth
1120  * using parallel workers, just free it.
1121  */
1122  if (ipath->path.parallel_workers > 0)
1123  add_partial_path(rel, (Path *) ipath);
1124  else
1125  pfree(ipath);
1126  }
1127  }
1128  }
1129 
1130  return result;
1131 }
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:795
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:775
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:703
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
static bool check_index_only(RelOptInfo *rel, IndexOptInfo *index)
Definition: indxpath.c:1805
static void match_pathkeys_to_index(IndexOptInfo *index, List *pathkeys, List **orderby_clauses_p, List **clause_columns_p)
Definition: indxpath.c:3078
List * lappend(List *list, void *datum)
Definition: list.c:336
void pfree(void *pointer)
Definition: mcxt.c:1175
#define IsA(nodeptr, _type_)
Definition: nodes.h:624
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:2484
List * truncate_useless_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
Definition: pathkeys.c:2441
List * build_index_pathkeys(PlannerInfo *root, IndexOptInfo *index, ScanDirection scandir)
Definition: pathkeys.c:1046
IndexPath * create_index_path(PlannerInfo *root, IndexOptInfo *index, List *indexclauses, List *indexorderbys, List *indexorderbycols, List *pathkeys, ScanDirection indexscandir, bool indexonly, Relids required_outer, double loop_count, bool partial_path)
Definition: pathnode.c:997
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:749
@ NoMovementScanDirection
Definition: sdir.h:25
@ BackwardScanDirection
Definition: sdir.h:24
@ ForwardScanDirection
Definition: sdir.h:26
List * indexclauses[INDEX_MAX_KEYS]
Definition: indxpath.c:56
struct RestrictInfo * rinfo
Definition: pathnodes.h:1303
Path path
Definition: pathnodes.h:1256
Definition: pg_list.h:51
List * query_pathkeys
Definition: pathnodes.h:295
bool consider_parallel
Definition: pathnodes.h:690
Relids lateral_relids
Definition: pathnodes.h:707
Relids clause_relids
Definition: pathnodes.h:2093
Expr * clause
Definition: pathnodes.h:2075
Definition: type.h:90

References add_partial_path(), Assert(), BackwardScanDirection, bms_add_members(), bms_copy(), bms_del_member(), bms_is_empty(), build_index_pathkeys(), check_index_only(), RestrictInfo::clause, RestrictInfo::clause_relids, RelOptInfo::consider_parallel, create_index_path(), ForwardScanDirection, get_loop_count(), has_useful_pathkeys(), IndexClauseSet::indexclauses, IsA, lappend(), RelOptInfo::lateral_relids, lfirst, match_pathkeys_to_index(), NIL, NoMovementScanDirection, Path::parallel_workers, IndexPath::path, pfree(), PlannerInfo::query_pathkeys, RelOptInfo::relid, IndexClause::rinfo, ST_ANYSCAN, ST_BITMAPSCAN, ST_INDEXSCAN, and truncate_useless_pathkeys().

Referenced by build_paths_for_OR(), and get_index_paths().

◆ build_paths_for_OR()

static List * build_paths_for_OR ( PlannerInfo root,
RelOptInfo rel,
List clauses,
List other_clauses 
)
static

Definition at line 1160 of file indxpath.c.

1162 {
1163  List *result = NIL;
1164  List *all_clauses = NIL; /* not computed till needed */
1165  ListCell *lc;
1166 
1167  foreach(lc, rel->indexlist)
1168  {
1170  IndexClauseSet clauseset;
1171  List *indexpaths;
1172  bool useful_predicate;
1173 
1174  /* Ignore index if it doesn't support bitmap scans */
1175  if (!index->amhasgetbitmap)
1176  continue;
1177 
1178  /*
1179  * Ignore partial indexes that do not match the query. If a partial
1180  * index is marked predOK then we know it's OK. Otherwise, we have to
1181  * test whether the added clauses are sufficient to imply the
1182  * predicate. If so, we can use the index in the current context.
1183  *
1184  * We set useful_predicate to true iff the predicate was proven using
1185  * the current set of clauses. This is needed to prevent matching a
1186  * predOK index to an arm of an OR, which would be a legal but
1187  * pointlessly inefficient plan. (A better plan will be generated by
1188  * just scanning the predOK index alone, no OR.)
1189  */
1190  useful_predicate = false;
1191  if (index->indpred != NIL)
1192  {
1193  if (index->predOK)
1194  {
1195  /* Usable, but don't set useful_predicate */
1196  }
1197  else
1198  {
1199  /* Form all_clauses if not done already */
1200  if (all_clauses == NIL)
1201  all_clauses = list_concat_copy(clauses, other_clauses);
1202 
1203  if (!predicate_implied_by(index->indpred, all_clauses, false))
1204  continue; /* can't use it at all */
1205 
1206  if (!predicate_implied_by(index->indpred, other_clauses, false))
1207  useful_predicate = true;
1208  }
1209  }
1210 
1211  /*
1212  * Identify the restriction clauses that can match the index.
1213  */
1214  MemSet(&clauseset, 0, sizeof(clauseset));
1215  match_clauses_to_index(root, clauses, index, &clauseset);
1216 
1217  /*
1218  * If no matches so far, and the index predicate isn't useful, we
1219  * don't want it.
1220  */
1221  if (!clauseset.nonempty && !useful_predicate)
1222  continue;
1223 
1224  /*
1225  * Add "other" restriction clauses to the clauseset.
1226  */
1227  match_clauses_to_index(root, other_clauses, index, &clauseset);
1228 
1229  /*
1230  * Construct paths if possible.
1231  */
1232  indexpaths = build_index_paths(root, rel,
1233  index, &clauseset,
1234  useful_predicate,
1235  ST_BITMAPSCAN,
1236  NULL,
1237  NULL);
1238  result = list_concat(result, indexpaths);
1239  }
1240 
1241  return result;
1242 }
#define MemSet(start, val, len)
Definition: c.h:1008
static List * build_index_paths(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauses, bool useful_predicate, ScanTypeControl scantype, bool *skip_nonnative_saop, bool *skip_lower_saop)
Definition: indxpath.c:855
static void match_clauses_to_index(PlannerInfo *root, List *clauses, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2126
List * list_concat(List *list1, const List *list2)
Definition: list.c:540
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:577
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:151
bool nonempty
Definition: indxpath.c:54
List * indexlist
Definition: pathnodes.h:719

References build_index_paths(), RelOptInfo::indexlist, lfirst, list_concat(), list_concat_copy(), match_clauses_to_index(), MemSet, NIL, IndexClauseSet::nonempty, predicate_implied_by(), and ST_BITMAPSCAN.

Referenced by generate_bitmap_or_paths().

◆ check_index_only()

static bool check_index_only ( RelOptInfo rel,
IndexOptInfo index 
)
static

Definition at line 1805 of file indxpath.c.

1806 {
1807  bool result;
1808  Bitmapset *attrs_used = NULL;
1809  Bitmapset *index_canreturn_attrs = NULL;
1810  ListCell *lc;
1811  int i;
1812 
1813  /* Index-only scans must be enabled */
1814  if (!enable_indexonlyscan)
1815  return false;
1816 
1817  /*
1818  * Check that all needed attributes of the relation are available from the
1819  * index.
1820  */
1821 
1822  /*
1823  * First, identify all the attributes needed for joins or final output.
1824  * Note: we must look at rel's targetlist, not the attr_needed data,
1825  * because attr_needed isn't computed for inheritance child rels.
1826  */
1827  pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
1828 
1829  /*
1830  * Add all the attributes used by restriction clauses; but consider only
1831  * those clauses not implied by the index predicate, since ones that are
1832  * so implied don't need to be checked explicitly in the plan.
1833  *
1834  * Note: attributes used only in index quals would not be needed at
1835  * runtime either, if we are certain that the index is not lossy. However
1836  * it'd be complicated to account for that accurately, and it doesn't
1837  * matter in most cases, since we'd conclude that such attributes are
1838  * available from the index anyway.
1839  */
1840  foreach(lc, index->indrestrictinfo)
1841  {
1842  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1843 
1844  pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
1845  }
1846 
1847  /*
1848  * Construct a bitmapset of columns that the index can return back in an
1849  * index-only scan.
1850  */
1851  for (i = 0; i < index->ncolumns; i++)
1852  {
1853  int attno = index->indexkeys[i];
1854 
1855  /*
1856  * For the moment, we just ignore index expressions. It might be nice
1857  * to do something with them, later.
1858  */
1859  if (attno == 0)
1860  continue;
1861 
1862  if (index->canreturn[i])
1863  index_canreturn_attrs =
1864  bms_add_member(index_canreturn_attrs,
1866  }
1867 
1868  /* Do we have all the necessary attributes? */
1869  result = bms_is_subset(attrs_used, index_canreturn_attrs);
1870 
1871  bms_free(attrs_used);
1872  bms_free(index_canreturn_attrs);
1873 
1874  return result;
1875 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:738
bool enable_indexonlyscan
Definition: costsize.c:136
int i
Definition: isn.c:73
Definition: nodes.h:574
List * exprs
Definition: pathnodes.h:1122
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:288

References bms_add_member(), bms_free(), bms_is_subset(), RestrictInfo::clause, enable_indexonlyscan, PathTarget::exprs, FirstLowInvalidHeapAttributeNumber, i, lfirst, pull_varattnos(), RelOptInfo::relid, and RelOptInfo::reltarget.

Referenced by build_index_paths().

◆ check_index_predicates()

void check_index_predicates ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 3307 of file indxpath.c.

3308 {
3309  List *clauselist;
3310  bool have_partial;
3311  bool is_target_rel;
3312  Relids otherrels;
3313  ListCell *lc;
3314 
3315  /* Indexes are available only on base or "other" member relations. */
3316  Assert(IS_SIMPLE_REL(rel));
3317 
3318  /*
3319  * Initialize the indrestrictinfo lists to be identical to
3320  * baserestrictinfo, and check whether there are any partial indexes. If
3321  * not, this is all we need to do.
3322  */
3323  have_partial = false;
3324  foreach(lc, rel->indexlist)
3325  {
3327 
3328  index->indrestrictinfo = rel->baserestrictinfo;
3329  if (index->indpred)
3330  have_partial = true;
3331  }
3332  if (!have_partial)
3333  return;
3334 
3335  /*
3336  * Construct a list of clauses that we can assume true for the purpose of
3337  * proving the index(es) usable. Restriction clauses for the rel are
3338  * always usable, and so are any join clauses that are "movable to" this
3339  * rel. Also, we can consider any EC-derivable join clauses (which must
3340  * be "movable to" this rel, by definition).
3341  */
3342  clauselist = list_copy(rel->baserestrictinfo);
3343 
3344  /* Scan the rel's join clauses */
3345  foreach(lc, rel->joininfo)
3346  {
3347  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3348 
3349  /* Check if clause can be moved to this rel */
3350  if (!join_clause_is_movable_to(rinfo, rel))
3351  continue;
3352 
3353  clauselist = lappend(clauselist, rinfo);
3354  }
3355 
3356  /*
3357  * Add on any equivalence-derivable join clauses. Computing the correct
3358  * relid sets for generate_join_implied_equalities is slightly tricky
3359  * because the rel could be a child rel rather than a true baserel, and in
3360  * that case we must remove its parents' relid(s) from all_baserels.
3361  */
3362  if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3363  otherrels = bms_difference(root->all_baserels,
3364  find_childrel_parents(root, rel));
3365  else
3366  otherrels = bms_difference(root->all_baserels, rel->relids);
3367 
3368  if (!bms_is_empty(otherrels))
3369  clauselist =
3370  list_concat(clauselist,
3372  bms_union(rel->relids,
3373  otherrels),
3374  otherrels,
3375  rel));
3376 
3377  /*
3378  * Normally we remove quals that are implied by a partial index's
3379  * predicate from indrestrictinfo, indicating that they need not be
3380  * checked explicitly by an indexscan plan using this index. However, if
3381  * the rel is a target relation of UPDATE/DELETE/SELECT FOR UPDATE, we
3382  * cannot remove such quals from the plan, because they need to be in the
3383  * plan so that they will be properly rechecked by EvalPlanQual testing.
3384  * Some day we might want to remove such quals from the main plan anyway
3385  * and pass them through to EvalPlanQual via a side channel; but for now,
3386  * we just don't remove implied quals at all for target relations.
3387  */
3388  is_target_rel = (bms_is_member(rel->relid, root->all_result_relids) ||
3389  get_plan_rowmark(root->rowMarks, rel->relid) != NULL);
3390 
3391  /*
3392  * Now try to prove each index predicate true, and compute the
3393  * indrestrictinfo lists for partial indexes. Note that we compute the
3394  * indrestrictinfo list even for non-predOK indexes; this might seem
3395  * wasteful, but we may be able to use such indexes in OR clauses, cf
3396  * generate_bitmap_or_paths().
3397  */
3398  foreach(lc, rel->indexlist)
3399  {
3401  ListCell *lcr;
3402 
3403  if (index->indpred == NIL)
3404  continue; /* ignore non-partial indexes here */
3405 
3406  if (!index->predOK) /* don't repeat work if already proven OK */
3407  index->predOK = predicate_implied_by(index->indpred, clauselist,
3408  false);
3409 
3410  /* If rel is an update target, leave indrestrictinfo as set above */
3411  if (is_target_rel)
3412  continue;
3413 
3414  /* Else compute indrestrictinfo as the non-implied quals */
3415  index->indrestrictinfo = NIL;
3416  foreach(lcr, rel->baserestrictinfo)
3417  {
3418  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcr);
3419 
3420  /* predicate_implied_by() assumes first arg is immutable */
3421  if (contain_mutable_functions((Node *) rinfo->clause) ||
3423  index->indpred, false))
3424  index->indrestrictinfo = lappend(index->indrestrictinfo, rinfo);
3425  }
3426  }
3427 }
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:291
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:368
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1403
List * list_copy(const List *oldlist)
Definition: list.c:1532
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:655
@ RELOPT_OTHER_MEMBER_REL
Definition: pathnodes.h:644
#define list_make1(x1)
Definition: pg_list.h:206
PlanRowMark * get_plan_rowmark(List *rowmarks, Index rtindex)
Definition: preptlist.c:487
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1261
bool join_clause_is_movable_to(RestrictInfo *rinfo, RelOptInfo *baserel)
Definition: restrictinfo.c:527
List * rowMarks
Definition: pathnodes.h:289
Relids all_baserels
Definition: pathnodes.h:210
Relids all_result_relids
Definition: pathnodes.h:277
List * baserestrictinfo
Definition: pathnodes.h:746
List * joininfo
Definition: pathnodes.h:750
Relids relids
Definition: pathnodes.h:682
RelOptKind reloptkind
Definition: pathnodes.h:679

References PlannerInfo::all_baserels, PlannerInfo::all_result_relids, Assert(), RelOptInfo::baserestrictinfo, bms_difference(), bms_is_empty(), bms_is_member(), bms_union(), RestrictInfo::clause, contain_mutable_functions(), find_childrel_parents(), generate_join_implied_equalities(), get_plan_rowmark(), RelOptInfo::indexlist, IS_SIMPLE_REL, join_clause_is_movable_to(), RelOptInfo::joininfo, lappend(), lfirst, list_concat(), list_copy(), list_make1, NIL, predicate_implied_by(), RelOptInfo::relid, RelOptInfo::relids, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, and PlannerInfo::rowMarks.

Referenced by set_plain_rel_size(), and set_tablesample_rel_size().

◆ choose_bitmap_and()

static Path * choose_bitmap_and ( PlannerInfo root,
RelOptInfo rel,
List paths 
)
static

Definition at line 1362 of file indxpath.c.

1363 {
1364  int npaths = list_length(paths);
1365  PathClauseUsage **pathinfoarray;
1366  PathClauseUsage *pathinfo;
1367  List *clauselist;
1368  List *bestpaths = NIL;
1369  Cost bestcost = 0;
1370  int i,
1371  j;
1372  ListCell *l;
1373 
1374  Assert(npaths > 0); /* else caller error */
1375  if (npaths == 1)
1376  return (Path *) linitial(paths); /* easy case */
1377 
1378  /*
1379  * In theory we should consider every nonempty subset of the given paths.
1380  * In practice that seems like overkill, given the crude nature of the
1381  * estimates, not to mention the possible effects of higher-level AND and
1382  * OR clauses. Moreover, it's completely impractical if there are a large
1383  * number of paths, since the work would grow as O(2^N).
1384  *
1385  * As a heuristic, we first check for paths using exactly the same sets of
1386  * WHERE clauses + index predicate conditions, and reject all but the
1387  * cheapest-to-scan in any such group. This primarily gets rid of indexes
1388  * that include the interesting columns but also irrelevant columns. (In
1389  * situations where the DBA has gone overboard on creating variant
1390  * indexes, this can make for a very large reduction in the number of
1391  * paths considered further.)
1392  *
1393  * We then sort the surviving paths with the cheapest-to-scan first, and
1394  * for each path, consider using that path alone as the basis for a bitmap
1395  * scan. Then we consider bitmap AND scans formed from that path plus
1396  * each subsequent (higher-cost) path, adding on a subsequent path if it
1397  * results in a reduction in the estimated total scan cost. This means we
1398  * consider about O(N^2) rather than O(2^N) path combinations, which is
1399  * quite tolerable, especially given than N is usually reasonably small
1400  * because of the prefiltering step. The cheapest of these is returned.
1401  *
1402  * We will only consider AND combinations in which no two indexes use the
1403  * same WHERE clause. This is a bit of a kluge: it's needed because
1404  * costsize.c and clausesel.c aren't very smart about redundant clauses.
1405  * They will usually double-count the redundant clauses, producing a
1406  * too-small selectivity that makes a redundant AND step look like it
1407  * reduces the total cost. Perhaps someday that code will be smarter and
1408  * we can remove this limitation. (But note that this also defends
1409  * against flat-out duplicate input paths, which can happen because
1410  * match_join_clauses_to_index will find the same OR join clauses that
1411  * extract_restriction_or_clauses has pulled OR restriction clauses out
1412  * of.)
1413  *
1414  * For the same reason, we reject AND combinations in which an index
1415  * predicate clause duplicates another clause. Here we find it necessary
1416  * to be even stricter: we'll reject a partial index if any of its
1417  * predicate clauses are implied by the set of WHERE clauses and predicate
1418  * clauses used so far. This covers cases such as a condition "x = 42"
1419  * used with a plain index, followed by a clauseless scan of a partial
1420  * index "WHERE x >= 40 AND x < 50". The partial index has been accepted
1421  * only because "x = 42" was present, and so allowing it would partially
1422  * double-count selectivity. (We could use predicate_implied_by on
1423  * regular qual clauses too, to have a more intelligent, but much more
1424  * expensive, check for redundancy --- but in most cases simple equality
1425  * seems to suffice.)
1426  */
1427 
1428  /*
1429  * Extract clause usage info and detect any paths that use exactly the
1430  * same set of clauses; keep only the cheapest-to-scan of any such groups.
1431  * The surviving paths are put into an array for qsort'ing.
1432  */
1433  pathinfoarray = (PathClauseUsage **)
1434  palloc(npaths * sizeof(PathClauseUsage *));
1435  clauselist = NIL;
1436  npaths = 0;
1437  foreach(l, paths)
1438  {
1439  Path *ipath = (Path *) lfirst(l);
1440 
1441  pathinfo = classify_index_clause_usage(ipath, &clauselist);
1442 
1443  /* If it's unclassifiable, treat it as distinct from all others */
1444  if (pathinfo->unclassifiable)
1445  {
1446  pathinfoarray[npaths++] = pathinfo;
1447  continue;
1448  }
1449 
1450  for (i = 0; i < npaths; i++)
1451  {
1452  if (!pathinfoarray[i]->unclassifiable &&
1453  bms_equal(pathinfo->clauseids, pathinfoarray[i]->clauseids))
1454  break;
1455  }
1456  if (i < npaths)
1457  {
1458  /* duplicate clauseids, keep the cheaper one */
1459  Cost ncost;
1460  Cost ocost;
1461  Selectivity nselec;
1462  Selectivity oselec;
1463 
1464  cost_bitmap_tree_node(pathinfo->path, &ncost, &nselec);
1465  cost_bitmap_tree_node(pathinfoarray[i]->path, &ocost, &oselec);
1466  if (ncost < ocost)
1467  pathinfoarray[i] = pathinfo;
1468  }
1469  else
1470  {
1471  /* not duplicate clauseids, add to array */
1472  pathinfoarray[npaths++] = pathinfo;
1473  }
1474  }
1475 
1476  /* If only one surviving path, we're done */
1477  if (npaths == 1)
1478  return pathinfoarray[0]->path;
1479 
1480  /* Sort the surviving paths by index access cost */
1481  qsort(pathinfoarray, npaths, sizeof(PathClauseUsage *),
1483 
1484  /*
1485  * For each surviving index, consider it as an "AND group leader", and see
1486  * whether adding on any of the later indexes results in an AND path with
1487  * cheaper total cost than before. Then take the cheapest AND group.
1488  *
1489  * Note: paths that are either clauseless or unclassifiable will have
1490  * empty clauseids, so that they will not be rejected by the clauseids
1491  * filter here, nor will they cause later paths to be rejected by it.
1492  */
1493  for (i = 0; i < npaths; i++)
1494  {
1495  Cost costsofar;
1496  List *qualsofar;
1497  Bitmapset *clauseidsofar;
1498 
1499  pathinfo = pathinfoarray[i];
1500  paths = list_make1(pathinfo->path);
1501  costsofar = bitmap_scan_cost_est(root, rel, pathinfo->path);
1502  qualsofar = list_concat_copy(pathinfo->quals, pathinfo->preds);
1503  clauseidsofar = bms_copy(pathinfo->clauseids);
1504 
1505  for (j = i + 1; j < npaths; j++)
1506  {
1507  Cost newcost;
1508 
1509  pathinfo = pathinfoarray[j];
1510  /* Check for redundancy */
1511  if (bms_overlap(pathinfo->clauseids, clauseidsofar))
1512  continue; /* consider it redundant */
1513  if (pathinfo->preds)
1514  {
1515  bool redundant = false;
1516 
1517  /* we check each predicate clause separately */
1518  foreach(l, pathinfo->preds)
1519  {
1520  Node *np = (Node *) lfirst(l);
1521 
1522  if (predicate_implied_by(list_make1(np), qualsofar, false))
1523  {
1524  redundant = true;
1525  break; /* out of inner foreach loop */
1526  }
1527  }
1528  if (redundant)
1529  continue;
1530  }
1531  /* tentatively add new path to paths, so we can estimate cost */
1532  paths = lappend(paths, pathinfo->path);
1533  newcost = bitmap_and_cost_est(root, rel, paths);
1534  if (newcost < costsofar)
1535  {
1536  /* keep new path in paths, update subsidiary variables */
1537  costsofar = newcost;
1538  qualsofar = list_concat(qualsofar, pathinfo->quals);
1539  qualsofar = list_concat(qualsofar, pathinfo->preds);
1540  clauseidsofar = bms_add_members(clauseidsofar,
1541  pathinfo->clauseids);
1542  }
1543  else
1544  {
1545  /* reject new path, remove it from paths list */
1546  paths = list_truncate(paths, list_length(paths) - 1);
1547  }
1548  }
1549 
1550  /* Keep the cheapest AND-group (or singleton) */
1551  if (i == 0 || costsofar < bestcost)
1552  {
1553  bestpaths = paths;
1554  bestcost = costsofar;
1555  }
1556 
1557  /* some easy cleanup (we don't try real hard though) */
1558  list_free(qualsofar);
1559  }
1560 
1561  if (list_length(bestpaths) == 1)
1562  return (Path *) linitial(bestpaths); /* no need for AND */
1563  return (Path *) create_bitmap_and_path(root, rel, bestpaths);
1564 }
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
void cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec)
Definition: costsize.c:1057
static Cost bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel, List *paths)
Definition: indxpath.c:1635
static PathClauseUsage * classify_index_clause_usage(Path *path, List **clauselist)
Definition: indxpath.c:1664
static int path_usage_comparator(const void *a, const void *b)
Definition: indxpath.c:1568
int j
Definition: isn.c:74
List * list_truncate(List *list, int new_size)
Definition: list.c:610
void list_free(List *list)
Definition: list.c:1505
void * palloc(Size size)
Definition: mcxt.c:1068
double Cost
Definition: nodes.h:707
double Selectivity
Definition: nodes.h:706
static int list_length(const List *l)
Definition: pg_list.h:149
#define linitial(l)
Definition: pg_list.h:174
#define qsort(a, b, c, d)
Definition: port.h:495
List * quals
Definition: indxpath.c:63
List * preds
Definition: indxpath.c:64
Bitmapset * clauseids
Definition: indxpath.c:65
bool unclassifiable
Definition: indxpath.c:66
Path * path
Definition: indxpath.c:62

References Assert(), bitmap_and_cost_est(), bitmap_scan_cost_est(), bms_add_members(), bms_copy(), bms_equal(), bms_overlap(), classify_index_clause_usage(), PathClauseUsage::clauseids, cost_bitmap_tree_node(), create_bitmap_and_path(), i, j, lappend(), lfirst, linitial, list_concat(), list_concat_copy(), list_free(), list_length(), list_make1, list_truncate(), NIL, palloc(), PathClauseUsage::path, path_usage_comparator(), predicate_implied_by(), PathClauseUsage::preds, qsort, PathClauseUsage::quals, and PathClauseUsage::unclassifiable.

Referenced by create_index_paths(), and generate_bitmap_or_paths().

◆ classify_index_clause_usage()

static PathClauseUsage * classify_index_clause_usage ( Path path,
List **  clauselist 
)
static

Definition at line 1664 of file indxpath.c.

1665 {
1666  PathClauseUsage *result;
1667  Bitmapset *clauseids;
1668  ListCell *lc;
1669 
1670  result = (PathClauseUsage *) palloc(sizeof(PathClauseUsage));
1671  result->path = path;
1672 
1673  /* Recursively find the quals and preds used by the path */
1674  result->quals = NIL;
1675  result->preds = NIL;
1676  find_indexpath_quals(path, &result->quals, &result->preds);
1677 
1678  /*
1679  * Some machine-generated queries have outlandish numbers of qual clauses.
1680  * To avoid getting into O(N^2) behavior even in this preliminary
1681  * classification step, we want to limit the number of entries we can
1682  * accumulate in *clauselist. Treat any path with more than 100 quals +
1683  * preds as unclassifiable, which will cause calling code to consider it
1684  * distinct from all other paths.
1685  */
1686  if (list_length(result->quals) + list_length(result->preds) > 100)
1687  {
1688  result->clauseids = NULL;
1689  result->unclassifiable = true;
1690  return result;
1691  }
1692 
1693  /* Build up a bitmapset representing the quals and preds */
1694  clauseids = NULL;
1695  foreach(lc, result->quals)
1696  {
1697  Node *node = (Node *) lfirst(lc);
1698 
1699  clauseids = bms_add_member(clauseids,
1700  find_list_position(node, clauselist));
1701  }
1702  foreach(lc, result->preds)
1703  {
1704  Node *node = (Node *) lfirst(lc);
1705 
1706  clauseids = bms_add_member(clauseids,
1707  find_list_position(node, clauselist));
1708  }
1709  result->clauseids = clauseids;
1710  result->unclassifiable = false;
1711 
1712  return result;
1713 }
static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
Definition: indxpath.c:1732
static int find_list_position(Node *node, List **nodelist)
Definition: indxpath.c:1779

References bms_add_member(), PathClauseUsage::clauseids, find_indexpath_quals(), find_list_position(), lfirst, list_length(), NIL, palloc(), PathClauseUsage::path, PathClauseUsage::preds, PathClauseUsage::quals, and PathClauseUsage::unclassifiable.

Referenced by choose_bitmap_and().

◆ consider_index_join_clauses()

static void consider_index_join_clauses ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet rclauseset,
IndexClauseSet jclauseset,
IndexClauseSet eclauseset,
List **  bitindexpaths 
)
static

Definition at line 433 of file indxpath.c.

439 {
440  int considered_clauses = 0;
441  List *considered_relids = NIL;
442  int indexcol;
443 
444  /*
445  * The strategy here is to identify every potentially useful set of outer
446  * rels that can provide indexable join clauses. For each such set,
447  * select all the join clauses available from those outer rels, add on all
448  * the indexable restriction clauses, and generate plain and/or bitmap
449  * index paths for that set of clauses. This is based on the assumption
450  * that it's always better to apply a clause as an indexqual than as a
451  * filter (qpqual); which is where an available clause would end up being
452  * applied if we omit it from the indexquals.
453  *
454  * This looks expensive, but in most practical cases there won't be very
455  * many distinct sets of outer rels to consider. As a safety valve when
456  * that's not true, we use a heuristic: limit the number of outer rel sets
457  * considered to a multiple of the number of clauses considered. (We'll
458  * always consider using each individual join clause, though.)
459  *
460  * For simplicity in selecting relevant clauses, we represent each set of
461  * outer rels as a maximum set of clause_relids --- that is, the indexed
462  * relation itself is also included in the relids set. considered_relids
463  * lists all relids sets we've already tried.
464  */
465  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
466  {
467  /* Consider each applicable simple join clause */
468  considered_clauses += list_length(jclauseset->indexclauses[indexcol]);
470  rclauseset, jclauseset, eclauseset,
471  bitindexpaths,
472  jclauseset->indexclauses[indexcol],
473  considered_clauses,
474  &considered_relids);
475  /* Consider each applicable eclass join clause */
476  considered_clauses += list_length(eclauseset->indexclauses[indexcol]);
478  rclauseset, jclauseset, eclauseset,
479  bitindexpaths,
480  eclauseset->indexclauses[indexcol],
481  considered_clauses,
482  &considered_relids);
483  }
484 }
static void consider_index_join_outer_rels(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *rclauseset, IndexClauseSet *jclauseset, IndexClauseSet *eclauseset, List **bitindexpaths, List *indexjoinclauses, int considered_clauses, List **considered_relids)
Definition: indxpath.c:499

References consider_index_join_outer_rels(), IndexClauseSet::indexclauses, list_length(), and NIL.

Referenced by create_index_paths().

◆ consider_index_join_outer_rels()

static void consider_index_join_outer_rels ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet rclauseset,
IndexClauseSet jclauseset,
IndexClauseSet eclauseset,
List **  bitindexpaths,
List indexjoinclauses,
int  considered_clauses,
List **  considered_relids 
)
static

Definition at line 499 of file indxpath.c.

508 {
509  ListCell *lc;
510 
511  /* Examine relids of each joinclause in the given list */
512  foreach(lc, indexjoinclauses)
513  {
514  IndexClause *iclause = (IndexClause *) lfirst(lc);
515  Relids clause_relids = iclause->rinfo->clause_relids;
516  EquivalenceClass *parent_ec = iclause->rinfo->parent_ec;
517  int num_considered_relids;
518 
519  /* If we already tried its relids set, no need to do so again */
520  if (bms_equal_any(clause_relids, *considered_relids))
521  continue;
522 
523  /*
524  * Generate the union of this clause's relids set with each
525  * previously-tried set. This ensures we try this clause along with
526  * every interesting subset of previous clauses. However, to avoid
527  * exponential growth of planning time when there are many clauses,
528  * limit the number of relid sets accepted to 10 * considered_clauses.
529  *
530  * Note: get_join_index_paths appends entries to *considered_relids,
531  * but we do not need to visit such newly-added entries within this
532  * loop, so we don't use foreach() here. No real harm would be done
533  * if we did visit them, since the subset check would reject them; but
534  * it would waste some cycles.
535  */
536  num_considered_relids = list_length(*considered_relids);
537  for (int pos = 0; pos < num_considered_relids; pos++)
538  {
539  Relids oldrelids = (Relids) list_nth(*considered_relids, pos);
540 
541  /*
542  * If either is a subset of the other, no new set is possible.
543  * This isn't a complete test for redundancy, but it's easy and
544  * cheap. get_join_index_paths will check more carefully if we
545  * already generated the same relids set.
546  */
547  if (bms_subset_compare(clause_relids, oldrelids) != BMS_DIFFERENT)
548  continue;
549 
550  /*
551  * If this clause was derived from an equivalence class, the
552  * clause list may contain other clauses derived from the same
553  * eclass. We should not consider that combining this clause with
554  * one of those clauses generates a usefully different
555  * parameterization; so skip if any clause derived from the same
556  * eclass would already have been included when using oldrelids.
557  */
558  if (parent_ec &&
559  eclass_already_used(parent_ec, oldrelids,
560  indexjoinclauses))
561  continue;
562 
563  /*
564  * If the number of relid sets considered exceeds our heuristic
565  * limit, stop considering combinations of clauses. We'll still
566  * consider the current clause alone, though (below this loop).
567  */
568  if (list_length(*considered_relids) >= 10 * considered_clauses)
569  break;
570 
571  /* OK, try the union set */
572  get_join_index_paths(root, rel, index,
573  rclauseset, jclauseset, eclauseset,
574  bitindexpaths,
575  bms_union(clause_relids, oldrelids),
576  considered_relids);
577  }
578 
579  /* Also try this set of relids by itself */
580  get_join_index_paths(root, rel, index,
581  rclauseset, jclauseset, eclauseset,
582  bitindexpaths,
583  clause_relids,
584  considered_relids);
585  }
586 }
BMS_Comparison bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:352
@ BMS_DIFFERENT
Definition: bitmapset.h:62
static void get_join_index_paths(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *rclauseset, IndexClauseSet *jclauseset, IndexClauseSet *eclauseset, List **bitindexpaths, Relids relids, List **considered_relids)
Definition: indxpath.c:602
static bool eclass_already_used(EquivalenceClass *parent_ec, Relids oldrelids, List *indexjoinclauses)
Definition: indxpath.c:680
static bool bms_equal_any(Relids relids, List *relids_list)
Definition: indxpath.c:704
Bitmapset * Relids
Definition: pathnodes.h:28
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
EquivalenceClass * parent_ec
Definition: pathnodes.h:2112

References BMS_DIFFERENT, bms_equal_any(), bms_subset_compare(), bms_union(), RestrictInfo::clause_relids, eclass_already_used(), get_join_index_paths(), lfirst, list_length(), list_nth(), RestrictInfo::parent_ec, and IndexClause::rinfo.

Referenced by consider_index_join_clauses().

◆ create_index_paths()

void create_index_paths ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 235 of file indxpath.c.

236 {
237  List *indexpaths;
238  List *bitindexpaths;
239  List *bitjoinpaths;
240  List *joinorclauses;
241  IndexClauseSet rclauseset;
242  IndexClauseSet jclauseset;
243  IndexClauseSet eclauseset;
244  ListCell *lc;
245 
246  /* Skip the whole mess if no indexes */
247  if (rel->indexlist == NIL)
248  return;
249 
250  /* Bitmap paths are collected and then dealt with at the end */
251  bitindexpaths = bitjoinpaths = joinorclauses = NIL;
252 
253  /* Examine each index in turn */
254  foreach(lc, rel->indexlist)
255  {
257 
258  /* Protect limited-size array in IndexClauseSets */
259  Assert(index->nkeycolumns <= INDEX_MAX_KEYS);
260 
261  /*
262  * Ignore partial indexes that do not match the query.
263  * (generate_bitmap_or_paths() might be able to do something with
264  * them, but that's of no concern here.)
265  */
266  if (index->indpred != NIL && !index->predOK)
267  continue;
268 
269  /*
270  * Identify the restriction clauses that can match the index.
271  */
272  MemSet(&rclauseset, 0, sizeof(rclauseset));
273  match_restriction_clauses_to_index(root, index, &rclauseset);
274 
275  /*
276  * Build index paths from the restriction clauses. These will be
277  * non-parameterized paths. Plain paths go directly to add_path(),
278  * bitmap paths are added to bitindexpaths to be handled below.
279  */
280  get_index_paths(root, rel, index, &rclauseset,
281  &bitindexpaths);
282 
283  /*
284  * Identify the join clauses that can match the index. For the moment
285  * we keep them separate from the restriction clauses. Note that this
286  * step finds only "loose" join clauses that have not been merged into
287  * EquivalenceClasses. Also, collect join OR clauses for later.
288  */
289  MemSet(&jclauseset, 0, sizeof(jclauseset));
291  &jclauseset, &joinorclauses);
292 
293  /*
294  * Look for EquivalenceClasses that can generate joinclauses matching
295  * the index.
296  */
297  MemSet(&eclauseset, 0, sizeof(eclauseset));
299  &eclauseset);
300 
301  /*
302  * If we found any plain or eclass join clauses, build parameterized
303  * index paths using them.
304  */
305  if (jclauseset.nonempty || eclauseset.nonempty)
307  &rclauseset,
308  &jclauseset,
309  &eclauseset,
310  &bitjoinpaths);
311  }
312 
313  /*
314  * Generate BitmapOrPaths for any suitable OR-clauses present in the
315  * restriction list. Add these to bitindexpaths.
316  */
317  indexpaths = generate_bitmap_or_paths(root, rel,
318  rel->baserestrictinfo, NIL);
319  bitindexpaths = list_concat(bitindexpaths, indexpaths);
320 
321  /*
322  * Likewise, generate BitmapOrPaths for any suitable OR-clauses present in
323  * the joinclause list. Add these to bitjoinpaths.
324  */
325  indexpaths = generate_bitmap_or_paths(root, rel,
326  joinorclauses, rel->baserestrictinfo);
327  bitjoinpaths = list_concat(bitjoinpaths, indexpaths);
328 
329  /*
330  * If we found anything usable, generate a BitmapHeapPath for the most
331  * promising combination of restriction bitmap index paths. Note there
332  * will be only one such path no matter how many indexes exist. This
333  * should be sufficient since there's basically only one figure of merit
334  * (total cost) for such a path.
335  */
336  if (bitindexpaths != NIL)
337  {
338  Path *bitmapqual;
339  BitmapHeapPath *bpath;
340 
341  bitmapqual = choose_bitmap_and(root, rel, bitindexpaths);
342  bpath = create_bitmap_heap_path(root, rel, bitmapqual,
343  rel->lateral_relids, 1.0, 0);
344  add_path(rel, (Path *) bpath);
345 
346  /* create a partial bitmap heap path */
347  if (rel->consider_parallel && rel->lateral_relids == NULL)
348  create_partial_bitmap_paths(root, rel, bitmapqual);
349  }
350 
351  /*
352  * Likewise, if we found anything usable, generate BitmapHeapPaths for the
353  * most promising combinations of join bitmap index paths. Our strategy
354  * is to generate one such path for each distinct parameterization seen
355  * among the available bitmap index paths. This may look pretty
356  * expensive, but usually there won't be very many distinct
357  * parameterizations. (This logic is quite similar to that in
358  * consider_index_join_clauses, but we're working with whole paths not
359  * individual clauses.)
360  */
361  if (bitjoinpaths != NIL)
362  {
363  List *all_path_outers;
364  ListCell *lc;
365 
366  /* Identify each distinct parameterization seen in bitjoinpaths */
367  all_path_outers = NIL;
368  foreach(lc, bitjoinpaths)
369  {
370  Path *path = (Path *) lfirst(lc);
371  Relids required_outer = PATH_REQ_OUTER(path);
372 
373  if (!bms_equal_any(required_outer, all_path_outers))
374  all_path_outers = lappend(all_path_outers, required_outer);
375  }
376 
377  /* Now, for each distinct parameterization set ... */
378  foreach(lc, all_path_outers)
379  {
380  Relids max_outers = (Relids) lfirst(lc);
381  List *this_path_set;
382  Path *bitmapqual;
383  Relids required_outer;
384  double loop_count;
385  BitmapHeapPath *bpath;
386  ListCell *lcp;
387 
388  /* Identify all the bitmap join paths needing no more than that */
389  this_path_set = NIL;
390  foreach(lcp, bitjoinpaths)
391  {
392  Path *path = (Path *) lfirst(lcp);
393 
394  if (bms_is_subset(PATH_REQ_OUTER(path), max_outers))
395  this_path_set = lappend(this_path_set, path);
396  }
397 
398  /*
399  * Add in restriction bitmap paths, since they can be used
400  * together with any join paths.
401  */
402  this_path_set = list_concat(this_path_set, bitindexpaths);
403 
404  /* Select best AND combination for this parameterization */
405  bitmapqual = choose_bitmap_and(root, rel, this_path_set);
406 
407  /* And push that path into the mix */
408  required_outer = PATH_REQ_OUTER(bitmapqual);
409  loop_count = get_loop_count(root, rel->relid, required_outer);
410  bpath = create_bitmap_heap_path(root, rel, bitmapqual,
411  required_outer, loop_count, 0);
412  add_path(rel, (Path *) bpath);
413  }
414  }
415 }
void create_partial_bitmap_paths(PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual)
Definition: allpaths.c:4058
static Path * choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths)
Definition: indxpath.c:1362
static void match_join_clauses_to_index(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauseset, List **joinorclauses)
Definition: indxpath.c:2058
static void match_eclass_clauses_to_index(PlannerInfo *root, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2088
static void get_index_paths(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauses, List **bitindexpaths)
Definition: indxpath.c:733
static void consider_index_join_clauses(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *rclauseset, IndexClauseSet *jclauseset, IndexClauseSet *eclauseset, List **bitindexpaths)
Definition: indxpath.c:433
static void match_restriction_clauses_to_index(PlannerInfo *root, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2043
static List * generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel, List *clauses, List *other_clauses)
Definition: indxpath.c:1255
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
BitmapHeapPath * create_bitmap_heap_path(PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual, Relids required_outer, double loop_count, int parallel_degree)
Definition: pathnode.c:1046
#define INDEX_MAX_KEYS

References add_path(), Assert(), RelOptInfo::baserestrictinfo, bms_equal_any(), bms_is_subset(), choose_bitmap_and(), consider_index_join_clauses(), RelOptInfo::consider_parallel, create_bitmap_heap_path(), create_partial_bitmap_paths(), generate_bitmap_or_paths(), get_index_paths(), get_loop_count(), INDEX_MAX_KEYS, RelOptInfo::indexlist, lappend(), RelOptInfo::lateral_relids, lfirst, list_concat(), match_eclass_clauses_to_index(), match_join_clauses_to_index(), match_restriction_clauses_to_index(), MemSet, NIL, IndexClauseSet::nonempty, PATH_REQ_OUTER, and RelOptInfo::relid.

Referenced by set_plain_rel_pathlist().

◆ ec_member_matches_indexcol()

static bool ec_member_matches_indexcol ( PlannerInfo root,
RelOptInfo rel,
EquivalenceClass ec,
EquivalenceMember em,
void *  arg 
)
static

Definition at line 3440 of file indxpath.c.

3443 {
3444  IndexOptInfo *index = ((ec_member_matches_arg *) arg)->index;
3445  int indexcol = ((ec_member_matches_arg *) arg)->indexcol;
3446  Oid curFamily;
3447  Oid curCollation;
3448 
3449  Assert(indexcol < index->nkeycolumns);
3450 
3451  curFamily = index->opfamily[indexcol];
3452  curCollation = index->indexcollations[indexcol];
3453 
3454  /*
3455  * If it's a btree index, we can reject it if its opfamily isn't
3456  * compatible with the EC, since no clause generated from the EC could be
3457  * used with the index. For non-btree indexes, we can't easily tell
3458  * whether clauses generated from the EC could be used with the index, so
3459  * don't check the opfamily. This might mean we return "true" for a
3460  * useless EC, so we have to recheck the results of
3461  * generate_implied_equalities_for_column; see
3462  * match_eclass_clauses_to_index.
3463  */
3464  if (index->relam == BTREE_AM_OID &&
3465  !list_member_oid(ec->ec_opfamilies, curFamily))
3466  return false;
3467 
3468  /* We insist on collation match for all index types, though */
3469  if (!IndexCollMatchesExprColl(curCollation, ec->ec_collation))
3470  return false;
3471 
3472  return match_index_to_operand((Node *) em->em_expr, indexcol, index);
3473 }
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:40
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3720
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:701
void * arg
unsigned int Oid
Definition: postgres_ext.h:31
List * ec_opfamilies
Definition: pathnodes.h:989

References arg, Assert(), EquivalenceClass::ec_collation, EquivalenceClass::ec_opfamilies, EquivalenceMember::em_expr, IndexCollMatchesExprColl, list_member_oid(), and match_index_to_operand().

Referenced by match_eclass_clauses_to_index().

◆ eclass_already_used()

static bool eclass_already_used ( EquivalenceClass parent_ec,
Relids  oldrelids,
List indexjoinclauses 
)
static

Definition at line 680 of file indxpath.c.

682 {
683  ListCell *lc;
684 
685  foreach(lc, indexjoinclauses)
686  {
687  IndexClause *iclause = (IndexClause *) lfirst(lc);
688  RestrictInfo *rinfo = iclause->rinfo;
689 
690  if (rinfo->parent_ec == parent_ec &&
691  bms_is_subset(rinfo->clause_relids, oldrelids))
692  return true;
693  }
694  return false;
695 }

References bms_is_subset(), RestrictInfo::clause_relids, lfirst, RestrictInfo::parent_ec, and IndexClause::rinfo.

Referenced by consider_index_join_outer_rels().

◆ expand_indexqual_rowcompare()

static IndexClause * expand_indexqual_rowcompare ( PlannerInfo root,
RestrictInfo rinfo,
int  indexcol,
IndexOptInfo index,
Oid  expr_op,
bool  var_on_left 
)
static

Definition at line 2856 of file indxpath.c.

2862 {
2863  IndexClause *iclause = makeNode(IndexClause);
2864  RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
2865  int op_strategy;
2866  Oid op_lefttype;
2867  Oid op_righttype;
2868  int matching_cols;
2869  List *expr_ops;
2870  List *opfamilies;
2871  List *lefttypes;
2872  List *righttypes;
2873  List *new_ops;
2874  List *var_args;
2875  List *non_var_args;
2876 
2877  iclause->rinfo = rinfo;
2878  iclause->indexcol = indexcol;
2879 
2880  if (var_on_left)
2881  {
2882  var_args = clause->largs;
2883  non_var_args = clause->rargs;
2884  }
2885  else
2886  {
2887  var_args = clause->rargs;
2888  non_var_args = clause->largs;
2889  }
2890 
2891  get_op_opfamily_properties(expr_op, index->opfamily[indexcol], false,
2892  &op_strategy,
2893  &op_lefttype,
2894  &op_righttype);
2895 
2896  /* Initialize returned list of which index columns are used */
2897  iclause->indexcols = list_make1_int(indexcol);
2898 
2899  /* Build lists of ops, opfamilies and operator datatypes in case needed */
2900  expr_ops = list_make1_oid(expr_op);
2901  opfamilies = list_make1_oid(index->opfamily[indexcol]);
2902  lefttypes = list_make1_oid(op_lefttype);
2903  righttypes = list_make1_oid(op_righttype);
2904 
2905  /*
2906  * See how many of the remaining columns match some index column in the
2907  * same way. As in match_clause_to_indexcol(), the "other" side of any
2908  * potential index condition is OK as long as it doesn't use Vars from the
2909  * indexed relation.
2910  */
2911  matching_cols = 1;
2912 
2913  while (matching_cols < list_length(var_args))
2914  {
2915  Node *varop = (Node *) list_nth(var_args, matching_cols);
2916  Node *constop = (Node *) list_nth(non_var_args, matching_cols);
2917  int i;
2918 
2919  expr_op = list_nth_oid(clause->opnos, matching_cols);
2920  if (!var_on_left)
2921  {
2922  /* indexkey is on right, so commute the operator */
2923  expr_op = get_commutator(expr_op);
2924  if (expr_op == InvalidOid)
2925  break; /* operator is not usable */
2926  }
2927  if (bms_is_member(index->rel->relid, pull_varnos(root, constop)))
2928  break; /* no good, Var on wrong side */
2929  if (contain_volatile_functions(constop))
2930  break; /* no good, volatile comparison value */
2931 
2932  /*
2933  * The Var side can match any key column of the index.
2934  */
2935  for (i = 0; i < index->nkeycolumns; i++)
2936  {
2937  if (match_index_to_operand(varop, i, index) &&
2938  get_op_opfamily_strategy(expr_op,
2939  index->opfamily[i]) == op_strategy &&
2940  IndexCollMatchesExprColl(index->indexcollations[i],
2941  list_nth_oid(clause->inputcollids,
2942  matching_cols)))
2943  break;
2944  }
2945  if (i >= index->nkeycolumns)
2946  break; /* no match found */
2947 
2948  /* Add column number to returned list */
2949  iclause->indexcols = lappend_int(iclause->indexcols, i);
2950 
2951  /* Add operator info to lists */
2952  get_op_opfamily_properties(expr_op, index->opfamily[i], false,
2953  &op_strategy,
2954  &op_lefttype,
2955  &op_righttype);
2956  expr_ops = lappend_oid(expr_ops, expr_op);
2957  opfamilies = lappend_oid(opfamilies, index->opfamily[i]);
2958  lefttypes = lappend_oid(lefttypes, op_lefttype);
2959  righttypes = lappend_oid(righttypes, op_righttype);
2960 
2961  /* This column matches, keep scanning */
2962  matching_cols++;
2963  }
2964 
2965  /* Result is non-lossy if all columns are usable as index quals */
2966  iclause->lossy = (matching_cols != list_length(clause->opnos));
2967 
2968  /*
2969  * We can use rinfo->clause as-is if we have var on left and it's all
2970  * usable as index quals.
2971  */
2972  if (var_on_left && !iclause->lossy)
2973  iclause->indexquals = list_make1(rinfo);
2974  else
2975  {
2976  /*
2977  * We have to generate a modified rowcompare (possibly just one
2978  * OpExpr). The painful part of this is changing < to <= or > to >=,
2979  * so deal with that first.
2980  */
2981  if (!iclause->lossy)
2982  {
2983  /* very easy, just use the commuted operators */
2984  new_ops = expr_ops;
2985  }
2986  else if (op_strategy == BTLessEqualStrategyNumber ||
2987  op_strategy == BTGreaterEqualStrategyNumber)
2988  {
2989  /* easy, just use the same (possibly commuted) operators */
2990  new_ops = list_truncate(expr_ops, matching_cols);
2991  }
2992  else
2993  {
2994  ListCell *opfamilies_cell;
2995  ListCell *lefttypes_cell;
2996  ListCell *righttypes_cell;
2997 
2998  if (op_strategy == BTLessStrategyNumber)
2999  op_strategy = BTLessEqualStrategyNumber;
3000  else if (op_strategy == BTGreaterStrategyNumber)
3001  op_strategy = BTGreaterEqualStrategyNumber;
3002  else
3003  elog(ERROR, "unexpected strategy number %d", op_strategy);
3004  new_ops = NIL;
3005  forthree(opfamilies_cell, opfamilies,
3006  lefttypes_cell, lefttypes,
3007  righttypes_cell, righttypes)
3008  {
3009  Oid opfam = lfirst_oid(opfamilies_cell);
3010  Oid lefttype = lfirst_oid(lefttypes_cell);
3011  Oid righttype = lfirst_oid(righttypes_cell);
3012 
3013  expr_op = get_opfamily_member(opfam, lefttype, righttype,
3014  op_strategy);
3015  if (!OidIsValid(expr_op)) /* should not happen */
3016  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
3017  op_strategy, lefttype, righttype, opfam);
3018  new_ops = lappend_oid(new_ops, expr_op);
3019  }
3020  }
3021 
3022  /* If we have more than one matching col, create a subset rowcompare */
3023  if (matching_cols > 1)
3024  {
3026 
3027  rc->rctype = (RowCompareType) op_strategy;
3028  rc->opnos = new_ops;
3030  matching_cols);
3032  matching_cols);
3033  rc->largs = list_truncate(copyObject(var_args),
3034  matching_cols);
3035  rc->rargs = list_truncate(copyObject(non_var_args),
3036  matching_cols);
3038  (Expr *) rc));
3039  }
3040  else
3041  {
3042  Expr *op;
3043 
3044  /* We don't report an index column list in this case */
3045  iclause->indexcols = NIL;
3046 
3047  op = make_opclause(linitial_oid(new_ops), BOOLOID, false,
3048  copyObject(linitial(var_args)),
3049  copyObject(linitial(non_var_args)),
3050  InvalidOid,
3051  linitial_oid(clause->inputcollids));
3052  iclause->indexquals = list_make1(make_simple_restrictinfo(root, op));
3053  }
3054  }
3055 
3056  return iclause;
3057 }
#define OidIsValid(objectId)
Definition: c.h:710
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:496
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:134
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Definition: lsyscache.c:81
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:164
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1490
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:611
#define copyObject(obj)
Definition: nodes.h:689
#define makeNode(_type_)
Definition: nodes.h:621
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:300
#define list_make1_oid(x1)
Definition: pg_list.h:236
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:491
#define list_make1_int(x1)
Definition: pg_list.h:221
#define linitial_oid(l)
Definition: pg_list.h:176
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define InvalidOid
Definition: postgres_ext.h:36
RowCompareType
Definition: primnodes.h:1110
#define make_simple_restrictinfo(root, clause)
Definition: restrictinfo.h:21
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
AttrNumber indexcol
Definition: pathnodes.h:1306
List * indexcols
Definition: pathnodes.h:1307
List * indexquals
Definition: pathnodes.h:1304
RowCompareType rctype
Definition: primnodes.h:1123
List * opfamilies
Definition: primnodes.h:1125
List * inputcollids
Definition: primnodes.h:1126
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:100

References bms_is_member(), BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, RestrictInfo::clause, contain_volatile_functions(), copyObject, elog, ERROR, forthree, get_commutator(), get_op_opfamily_properties(), get_op_opfamily_strategy(), get_opfamily_member(), i, if(), IndexClause::indexcol, IndexCollMatchesExprColl, IndexClause::indexcols, IndexClause::indexquals, RowCompareExpr::inputcollids, InvalidOid, lappend_int(), lappend_oid(), RowCompareExpr::largs, lfirst_oid, linitial, linitial_oid, list_copy(), list_length(), list_make1, list_make1_int, list_make1_oid, list_nth(), list_nth_oid(), list_truncate(), IndexClause::lossy, make_opclause(), make_simple_restrictinfo, makeNode, match_index_to_operand(), NIL, OidIsValid, RowCompareExpr::opfamilies, RowCompareExpr::opnos, pull_varnos(), RowCompareExpr::rargs, RowCompareExpr::rctype, and IndexClause::rinfo.

Referenced by match_rowcompare_to_indexcol().

◆ find_indexpath_quals()

static void find_indexpath_quals ( Path bitmapqual,
List **  quals,
List **  preds 
)
static

Definition at line 1732 of file indxpath.c.

1733 {
1734  if (IsA(bitmapqual, BitmapAndPath))
1735  {
1736  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1737  ListCell *l;
1738 
1739  foreach(l, apath->bitmapquals)
1740  {
1741  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1742  }
1743  }
1744  else if (IsA(bitmapqual, BitmapOrPath))
1745  {
1746  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1747  ListCell *l;
1748 
1749  foreach(l, opath->bitmapquals)
1750  {
1751  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1752  }
1753  }
1754  else if (IsA(bitmapqual, IndexPath))
1755  {
1756  IndexPath *ipath = (IndexPath *) bitmapqual;
1757  ListCell *l;
1758 
1759  foreach(l, ipath->indexclauses)
1760  {
1761  IndexClause *iclause = (IndexClause *) lfirst(l);
1762 
1763  *quals = lappend(*quals, iclause->rinfo->clause);
1764  }
1765  *preds = list_concat(*preds, ipath->indexinfo->indpred);
1766  }
1767  else
1768  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1769 }
#define nodeTag(nodeptr)
Definition: nodes.h:578
List * bitmapquals
Definition: pathnodes.h:1342
List * bitmapquals
Definition: pathnodes.h:1355
List * indpred
Definition: pathnodes.h:865
List * indexclauses
Definition: pathnodes.h:1258
IndexOptInfo * indexinfo
Definition: pathnodes.h:1257

References BitmapAndPath::bitmapquals, BitmapOrPath::bitmapquals, RestrictInfo::clause, elog, ERROR, IndexPath::indexclauses, IndexPath::indexinfo, IndexOptInfo::indpred, IsA, lappend(), lfirst, list_concat(), nodeTag, and IndexClause::rinfo.

Referenced by classify_index_clause_usage().

◆ find_list_position()

static int find_list_position ( Node node,
List **  nodelist 
)
static

Definition at line 1779 of file indxpath.c.

1780 {
1781  int i;
1782  ListCell *lc;
1783 
1784  i = 0;
1785  foreach(lc, *nodelist)
1786  {
1787  Node *oldnode = (Node *) lfirst(lc);
1788 
1789  if (equal(node, oldnode))
1790  return i;
1791  i++;
1792  }
1793 
1794  *nodelist = lappend(*nodelist, node);
1795 
1796  return i;
1797 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3564

References equal(), i, lappend(), and lfirst.

Referenced by classify_index_clause_usage().

◆ generate_bitmap_or_paths()

static List * generate_bitmap_or_paths ( PlannerInfo root,
RelOptInfo rel,
List clauses,
List other_clauses 
)
static

Definition at line 1255 of file indxpath.c.

1257 {
1258  List *result = NIL;
1259  List *all_clauses;
1260  ListCell *lc;
1261 
1262  /*
1263  * We can use both the current and other clauses as context for
1264  * build_paths_for_OR; no need to remove ORs from the lists.
1265  */
1266  all_clauses = list_concat_copy(clauses, other_clauses);
1267 
1268  foreach(lc, clauses)
1269  {
1270  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1271  List *pathlist;
1272  Path *bitmapqual;
1273  ListCell *j;
1274 
1275  /* Ignore RestrictInfos that aren't ORs */
1276  if (!restriction_is_or_clause(rinfo))
1277  continue;
1278 
1279  /*
1280  * We must be able to match at least one index to each of the arms of
1281  * the OR, else we can't use it.
1282  */
1283  pathlist = NIL;
1284  foreach(j, ((BoolExpr *) rinfo->orclause)->args)
1285  {
1286  Node *orarg = (Node *) lfirst(j);
1287  List *indlist;
1288 
1289  /* OR arguments should be ANDs or sub-RestrictInfos */
1290  if (is_andclause(orarg))
1291  {
1292  List *andargs = ((BoolExpr *) orarg)->args;
1293 
1294  indlist = build_paths_for_OR(root, rel,
1295  andargs,
1296  all_clauses);
1297 
1298  /* Recurse in case there are sub-ORs */
1299  indlist = list_concat(indlist,
1300  generate_bitmap_or_paths(root, rel,
1301  andargs,
1302  all_clauses));
1303  }
1304  else
1305  {
1306  RestrictInfo *rinfo = castNode(RestrictInfo, orarg);
1307  List *orargs;
1308 
1310  orargs = list_make1(rinfo);
1311 
1312  indlist = build_paths_for_OR(root, rel,
1313  orargs,
1314  all_clauses);
1315  }
1316 
1317  /*
1318  * If nothing matched this arm, we can't do anything with this OR
1319  * clause.
1320  */
1321  if (indlist == NIL)
1322  {
1323  pathlist = NIL;
1324  break;
1325  }
1326 
1327  /*
1328  * OK, pick the most promising AND combination, and add it to
1329  * pathlist.
1330  */
1331  bitmapqual = choose_bitmap_and(root, rel, indlist);
1332  pathlist = lappend(pathlist, bitmapqual);
1333  }
1334 
1335  /*
1336  * If we have a match for every arm, then turn them into a
1337  * BitmapOrPath, and add to result list.
1338  */
1339  if (pathlist != NIL)
1340  {
1341  bitmapqual = (Path *) create_bitmap_or_path(root, rel, pathlist);
1342  result = lappend(result, bitmapqual);
1343  }
1344  }
1345 
1346  return result;
1347 }
static List * build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel, List *clauses, List *other_clauses)
Definition: indxpath.c:1160
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:95
#define castNode(_type_, nodeptr)
Definition: nodes.h:642
BitmapOrPath * create_bitmap_or_path(PlannerInfo *root, RelOptInfo *rel, List *bitmapquals)
Definition: pathnode.c:1131
#define lfirst_node(type, lc)
Definition: pg_list.h:172
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:384
Expr * orclause
Definition: pathnodes.h:2109

References Assert(), build_paths_for_OR(), castNode, choose_bitmap_and(), create_bitmap_or_path(), is_andclause(), j, lappend(), lfirst, lfirst_node, list_concat(), list_concat_copy(), list_make1, NIL, RestrictInfo::orclause, and restriction_is_or_clause().

Referenced by create_index_paths().

◆ get_index_clause_from_support()

static IndexClause * get_index_clause_from_support ( PlannerInfo root,
RestrictInfo rinfo,
Oid  funcid,
int  indexarg,
int  indexcol,
IndexOptInfo index 
)
static

Definition at line 2615 of file indxpath.c.

2621 {
2622  Oid prosupport = get_func_support(funcid);
2624  List *sresult;
2625 
2626  if (!OidIsValid(prosupport))
2627  return NULL;
2628 
2630  req.root = root;
2631  req.funcid = funcid;
2632  req.node = (Node *) rinfo->clause;
2633  req.indexarg = indexarg;
2634  req.index = index;
2635  req.indexcol = indexcol;
2636  req.opfamily = index->opfamily[indexcol];
2637  req.indexcollation = index->indexcollations[indexcol];
2638 
2639  req.lossy = true; /* default assumption */
2640 
2641  sresult = (List *)
2642  DatumGetPointer(OidFunctionCall1(prosupport,
2643  PointerGetDatum(&req)));
2644 
2645  if (sresult != NIL)
2646  {
2647  IndexClause *iclause = makeNode(IndexClause);
2648  List *indexquals = NIL;
2649  ListCell *lc;
2650 
2651  /*
2652  * The support function API says it should just give back bare
2653  * clauses, so here we must wrap each one in a RestrictInfo.
2654  */
2655  foreach(lc, sresult)
2656  {
2657  Expr *clause = (Expr *) lfirst(lc);
2658 
2659  indexquals = lappend(indexquals,
2660  make_simple_restrictinfo(root, clause));
2661  }
2662 
2663  iclause->rinfo = rinfo;
2664  iclause->indexquals = indexquals;
2665  iclause->lossy = req.lossy;
2666  iclause->indexcol = indexcol;
2667  iclause->indexcols = NIL;
2668 
2669  return iclause;
2670  }
2671 
2672  return NULL;
2673 }
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:669
RegProcedure get_func_support(Oid funcid)
Definition: lsyscache.c:1839
@ T_SupportRequestIndexCondition
Definition: nodes.h:563
#define DatumGetPointer(X)
Definition: postgres.h:593
#define PointerGetDatum(X)
Definition: postgres.h:600
struct IndexOptInfo * index
Definition: supportnodes.h:232
struct PlannerInfo * root
Definition: supportnodes.h:228

References RestrictInfo::clause, DatumGetPointer, SupportRequestIndexCondition::funcid, get_func_support(), SupportRequestIndexCondition::index, SupportRequestIndexCondition::indexarg, IndexClause::indexcol, SupportRequestIndexCondition::indexcol, SupportRequestIndexCondition::indexcollation, IndexClause::indexcols, IndexClause::indexquals, lappend(), lfirst, IndexClause::lossy, SupportRequestIndexCondition::lossy, make_simple_restrictinfo, makeNode, NIL, SupportRequestIndexCondition::node, OidFunctionCall1, OidIsValid, SupportRequestIndexCondition::opfamily, PointerGetDatum, IndexClause::rinfo, SupportRequestIndexCondition::root, T_SupportRequestIndexCondition, and SupportRequestIndexCondition::type.

Referenced by match_funcclause_to_indexcol(), and match_opclause_to_indexcol().

◆ get_index_paths()

static void get_index_paths ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet clauses,
List **  bitindexpaths 
)
static

Definition at line 733 of file indxpath.c.

736 {
737  List *indexpaths;
738  bool skip_nonnative_saop = false;
739  bool skip_lower_saop = false;
740  ListCell *lc;
741 
742  /*
743  * Build simple index paths using the clauses. Allow ScalarArrayOpExpr
744  * clauses only if the index AM supports them natively, and skip any such
745  * clauses for index columns after the first (so that we produce ordered
746  * paths if possible).
747  */
748  indexpaths = build_index_paths(root, rel,
749  index, clauses,
750  index->predOK,
751  ST_ANYSCAN,
752  &skip_nonnative_saop,
753  &skip_lower_saop);
754 
755  /*
756  * If we skipped any lower-order ScalarArrayOpExprs on an index with an AM
757  * that supports them, then try again including those clauses. This will
758  * produce paths with more selectivity but no ordering.
759  */
760  if (skip_lower_saop)
761  {
762  indexpaths = list_concat(indexpaths,
763  build_index_paths(root, rel,
764  index, clauses,
765  index->predOK,
766  ST_ANYSCAN,
767  &skip_nonnative_saop,
768  NULL));
769  }
770 
771  /*
772  * Submit all the ones that can form plain IndexScan plans to add_path. (A
773  * plain IndexPath can represent either a plain IndexScan or an
774  * IndexOnlyScan, but for our purposes here that distinction does not
775  * matter. However, some of the indexes might support only bitmap scans,
776  * and those we mustn't submit to add_path here.)
777  *
778  * Also, pick out the ones that are usable as bitmap scans. For that, we
779  * must discard indexes that don't support bitmap scans, and we also are
780  * only interested in paths that have some selectivity; we should discard
781  * anything that was generated solely for ordering purposes.
782  */
783  foreach(lc, indexpaths)
784  {
785  IndexPath *ipath = (IndexPath *) lfirst(lc);
786 
787  if (index->amhasgettuple)
788  add_path(rel, (Path *) ipath);
789 
790  if (index->amhasgetbitmap &&
791  (ipath->path.pathkeys == NIL ||
792  ipath->indexselectivity < 1.0))
793  *bitindexpaths = lappend(*bitindexpaths, ipath);
794  }
795 
796  /*
797  * If there were ScalarArrayOpExpr clauses that the index can't handle
798  * natively, generate bitmap scan paths relying on executor-managed
799  * ScalarArrayOpExpr.
800  */
801  if (skip_nonnative_saop)
802  {
803  indexpaths = build_index_paths(root, rel,
804  index, clauses,
805  false,
807  NULL,
808  NULL);
809  *bitindexpaths = list_concat(*bitindexpaths, indexpaths);
810  }
811 }
Selectivity indexselectivity
Definition: pathnodes.h:1263

References add_path(), build_index_paths(), IndexPath::indexselectivity, lappend(), lfirst, list_concat(), NIL, IndexPath::path, Path::pathkeys, ST_ANYSCAN, and ST_BITMAPSCAN.

Referenced by create_index_paths(), and get_join_index_paths().

◆ get_join_index_paths()

static void get_join_index_paths ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet rclauseset,
IndexClauseSet jclauseset,
IndexClauseSet eclauseset,
List **  bitindexpaths,
Relids  relids,
List **  considered_relids 
)
static

Definition at line 602 of file indxpath.c.

610 {
611  IndexClauseSet clauseset;
612  int indexcol;
613 
614  /* If we already considered this relids set, don't repeat the work */
615  if (bms_equal_any(relids, *considered_relids))
616  return;
617 
618  /* Identify indexclauses usable with this relids set */
619  MemSet(&clauseset, 0, sizeof(clauseset));
620 
621  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
622  {
623  ListCell *lc;
624 
625  /* First find applicable simple join clauses */
626  foreach(lc, jclauseset->indexclauses[indexcol])
627  {
628  IndexClause *iclause = (IndexClause *) lfirst(lc);
629 
630  if (bms_is_subset(iclause->rinfo->clause_relids, relids))
631  clauseset.indexclauses[indexcol] =
632  lappend(clauseset.indexclauses[indexcol], iclause);
633  }
634 
635  /*
636  * Add applicable eclass join clauses. The clauses generated for each
637  * column are redundant (cf generate_implied_equalities_for_column),
638  * so we need at most one. This is the only exception to the general
639  * rule of using all available index clauses.
640  */
641  foreach(lc, eclauseset->indexclauses[indexcol])
642  {
643  IndexClause *iclause = (IndexClause *) lfirst(lc);
644 
645  if (bms_is_subset(iclause->rinfo->clause_relids, relids))
646  {
647  clauseset.indexclauses[indexcol] =
648  lappend(clauseset.indexclauses[indexcol], iclause);
649  break;
650  }
651  }
652 
653  /* Add restriction clauses */
654  clauseset.indexclauses[indexcol] =
655  list_concat(clauseset.indexclauses[indexcol],
656  rclauseset->indexclauses[indexcol]);
657 
658  if (clauseset.indexclauses[indexcol] != NIL)
659  clauseset.nonempty = true;
660  }
661 
662  /* We should have found something, else caller passed silly relids */
663  Assert(clauseset.nonempty);
664 
665  /* Build index path(s) using the collected set of clauses */
666  get_index_paths(root, rel, index, &clauseset, bitindexpaths);
667 
668  /*
669  * Remember we considered paths for this set of relids.
670  */
671  *considered_relids = lappend(*considered_relids, relids);
672 }

References Assert(), bms_equal_any(), bms_is_subset(), RestrictInfo::clause_relids, get_index_paths(), IndexClauseSet::indexclauses, lappend(), lfirst, list_concat(), MemSet, NIL, IndexClauseSet::nonempty, and IndexClause::rinfo.

Referenced by consider_index_join_outer_rels().

◆ get_loop_count()

static double get_loop_count ( PlannerInfo root,
Index  cur_relid,
Relids  outer_relids 
)
static

Definition at line 1904 of file indxpath.c.

1905 {
1906  double result;
1907  int outer_relid;
1908 
1909  /* For a non-parameterized path, just return 1.0 quickly */
1910  if (outer_relids == NULL)
1911  return 1.0;
1912 
1913  result = 0.0;
1914  outer_relid = -1;
1915  while ((outer_relid = bms_next_member(outer_relids, outer_relid)) >= 0)
1916  {
1917  RelOptInfo *outer_rel;
1918  double rowcount;
1919 
1920  /* Paranoia: ignore bogus relid indexes */
1921  if (outer_relid >= root->simple_rel_array_size)
1922  continue;
1923  outer_rel = root->simple_rel_array[outer_relid];
1924  if (outer_rel == NULL)
1925  continue;
1926  Assert(outer_rel->relid == outer_relid); /* sanity check on array */
1927 
1928  /* Other relation could be proven empty, if so ignore */
1929  if (IS_DUMMY_REL(outer_rel))
1930  continue;
1931 
1932  /* Otherwise, rel's rows estimate should be valid by now */
1933  Assert(outer_rel->rows > 0);
1934 
1935  /* Check to see if rel is on the inside of any semijoins */
1936  rowcount = adjust_rowcount_for_semijoins(root,
1937  cur_relid,
1938  outer_relid,
1939  outer_rel->rows);
1940 
1941  /* Remember smallest row count estimate among the outer rels */
1942  if (result == 0.0 || result > rowcount)
1943  result = rowcount;
1944  }
1945  /* Return 1.0 if we found no valid relations (shouldn't happen) */
1946  return (result > 0.0) ? result : 1.0;
1947 }
static double adjust_rowcount_for_semijoins(PlannerInfo *root, Index cur_relid, Index outer_relid, double rowcount)
Definition: indxpath.c:1957

References adjust_rowcount_for_semijoins(), Assert(), bms_next_member(), IS_DUMMY_REL, RelOptInfo::relid, RelOptInfo::rows, PlannerInfo::simple_rel_array, and PlannerInfo::simple_rel_array_size.

Referenced by bitmap_scan_cost_est(), build_index_paths(), and create_index_paths().

◆ indexcol_is_bool_constant_for_query()

bool indexcol_is_bool_constant_for_query ( PlannerInfo root,
IndexOptInfo index,
int  indexcol 
)

Definition at line 3669 of file indxpath.c.

3672 {
3673  ListCell *lc;
3674 
3675  /* If the index isn't boolean, we can't possibly get a match */
3676  if (!IsBooleanOpfamily(index->opfamily[indexcol]))
3677  return false;
3678 
3679  /* Check each restriction clause for the index's rel */
3680  foreach(lc, index->rel->baserestrictinfo)
3681  {
3682  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3683 
3684  /*
3685  * As in match_clause_to_indexcol, never match pseudoconstants to
3686  * indexes. (It might be semantically okay to do so here, but the
3687  * odds of getting a match are negligible, so don't waste the cycles.)
3688  */
3689  if (rinfo->pseudoconstant)
3690  continue;
3691 
3692  /* See if we can match the clause's expression to the index column */
3693  if (match_boolean_index_clause(root, rinfo, indexcol, index))
3694  return true;
3695  }
3696 
3697  return false;
3698 }
static IndexClause * match_boolean_index_clause(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2363
bool pseudoconstant
Definition: pathnodes.h:2083

References lfirst, match_boolean_index_clause(), and RestrictInfo::pseudoconstant.

Referenced by build_index_pathkeys().

◆ is_pseudo_constant_for_index()

bool is_pseudo_constant_for_index ( PlannerInfo root,
Node expr,
IndexOptInfo index 
)

Definition at line 3805 of file indxpath.c.

3806 {
3807  /* pull_varnos is cheaper than volatility check, so do that first */
3808  if (bms_is_member(index->rel->relid, pull_varnos(root, expr)))
3809  return false; /* no good, contains Var of table */
3810  if (contain_volatile_functions(expr))
3811  return false; /* no good, volatile comparison value */
3812  return true;
3813 }

References bms_is_member(), contain_volatile_functions(), and pull_varnos().

◆ match_boolean_index_clause()

static IndexClause * match_boolean_index_clause ( PlannerInfo root,
RestrictInfo rinfo,
int  indexcol,
IndexOptInfo index 
)
static

Definition at line 2363 of file indxpath.c.

2367 {
2368  Node *clause = (Node *) rinfo->clause;
2369  Expr *op = NULL;
2370 
2371  /* Direct match? */
2372  if (match_index_to_operand(clause, indexcol, index))
2373  {
2374  /* convert to indexkey = TRUE */
2375  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2376  (Expr *) clause,
2377  (Expr *) makeBoolConst(true, false),
2379  }
2380  /* NOT clause? */
2381  else if (is_notclause(clause))
2382  {
2383  Node *arg = (Node *) get_notclausearg((Expr *) clause);
2384 
2385  if (match_index_to_operand(arg, indexcol, index))
2386  {
2387  /* convert to indexkey = FALSE */
2388  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2389  (Expr *) arg,
2390  (Expr *) makeBoolConst(false, false),
2392  }
2393  }
2394 
2395  /*
2396  * Since we only consider clauses at top level of WHERE, we can convert
2397  * indexkey IS TRUE and indexkey IS FALSE to index searches as well. The
2398  * different meaning for NULL isn't important.
2399  */
2400  else if (clause && IsA(clause, BooleanTest))
2401  {
2402  BooleanTest *btest = (BooleanTest *) clause;
2403  Node *arg = (Node *) btest->arg;
2404 
2405  if (btest->booltesttype == IS_TRUE &&
2406  match_index_to_operand(arg, indexcol, index))
2407  {
2408  /* convert to indexkey = TRUE */
2409  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2410  (Expr *) arg,
2411  (Expr *) makeBoolConst(true, false),
2413  }
2414  else if (btest->booltesttype == IS_FALSE &&
2415  match_index_to_operand(arg, indexcol, index))
2416  {
2417  /* convert to indexkey = FALSE */
2418  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2419  (Expr *) arg,
2420  (Expr *) makeBoolConst(false, false),
2422  }
2423  }
2424 
2425  /*
2426  * If we successfully made an operator clause from the given qual, we must
2427  * wrap it in an IndexClause. It's not lossy.
2428  */
2429  if (op)
2430  {
2431  IndexClause *iclause = makeNode(IndexClause);
2432 
2433  iclause->rinfo = rinfo;
2434  iclause->indexquals = list_make1(make_simple_restrictinfo(root, op));
2435  iclause->lossy = false;
2436  iclause->indexcol = indexcol;
2437  iclause->indexcols = NIL;
2438  return iclause;
2439  }
2440 
2441  return NULL;
2442 }
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:358
static Expr * get_notclausearg(const void *notclause)
Definition: nodeFuncs.h:122
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:113
@ IS_TRUE
Definition: primnodes.h:1543
@ IS_FALSE
Definition: primnodes.h:1543
BoolTestType booltesttype
Definition: primnodes.h:1550
Expr * arg
Definition: primnodes.h:1549

References arg, BooleanTest::arg, BooleanTest::booltesttype, RestrictInfo::clause, get_notclausearg(), if(), IndexClause::indexcol, IndexClause::indexcols, IndexClause::indexquals, InvalidOid, IS_FALSE, is_notclause(), IS_TRUE, IsA, list_make1, IndexClause::lossy, make_opclause(), make_simple_restrictinfo, makeBoolConst(), makeNode, match_index_to_operand(), NIL, and IndexClause::rinfo.

Referenced by indexcol_is_bool_constant_for_query(), and match_clause_to_indexcol().

◆ match_clause_to_index()

static void match_clause_to_index ( PlannerInfo root,
RestrictInfo rinfo,
IndexOptInfo index,
IndexClauseSet clauseset 
)
static

Definition at line 2159 of file indxpath.c.

2163 {
2164  int indexcol;
2165 
2166  /*
2167  * Never match pseudoconstants to indexes. (Normally a match could not
2168  * happen anyway, since a pseudoconstant clause couldn't contain a Var,
2169  * but what if someone builds an expression index on a constant? It's not
2170  * totally unreasonable to do so with a partial index, either.)
2171  */
2172  if (rinfo->pseudoconstant)
2173  return;
2174 
2175  /*
2176  * If clause can't be used as an indexqual because it must wait till after
2177  * some lower-security-level restriction clause, reject it.
2178  */
2179  if (!restriction_is_securely_promotable(rinfo, index->rel))
2180  return;
2181 
2182  /* OK, check each index key column for a match */
2183  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
2184  {
2185  IndexClause *iclause;
2186  ListCell *lc;
2187 
2188  /* Ignore duplicates */
2189  foreach(lc, clauseset->indexclauses[indexcol])
2190  {
2191  IndexClause *iclause = (IndexClause *) lfirst(lc);
2192 
2193  if (iclause->rinfo == rinfo)
2194  return;
2195  }
2196 
2197  /* OK, try to match the clause to the index column */
2198  iclause = match_clause_to_indexcol(root,
2199  rinfo,
2200  indexcol,
2201  index);
2202  if (iclause)
2203  {
2204  /* Success, so record it */
2205  clauseset->indexclauses[indexcol] =
2206  lappend(clauseset->indexclauses[indexcol], iclause);
2207  clauseset->nonempty = true;
2208  return;
2209  }
2210  }
2211 }
static IndexClause * match_clause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2278
bool restriction_is_securely_promotable(RestrictInfo *restrictinfo, RelOptInfo *rel)
Definition: restrictinfo.c:399

References IndexClauseSet::indexclauses, lappend(), lfirst, match_clause_to_indexcol(), IndexClauseSet::nonempty, RestrictInfo::pseudoconstant, restriction_is_securely_promotable(), and IndexClause::rinfo.

Referenced by match_clauses_to_index(), and match_join_clauses_to_index().

◆ match_clause_to_indexcol()

static IndexClause * match_clause_to_indexcol ( PlannerInfo root,
RestrictInfo rinfo,
int  indexcol,
IndexOptInfo index 
)
static

Definition at line 2278 of file indxpath.c.

2282 {
2283  IndexClause *iclause;
2284  Expr *clause = rinfo->clause;
2285  Oid opfamily;
2286 
2287  Assert(indexcol < index->nkeycolumns);
2288 
2289  /*
2290  * Historically this code has coped with NULL clauses. That's probably
2291  * not possible anymore, but we might as well continue to cope.
2292  */
2293  if (clause == NULL)
2294  return NULL;
2295 
2296  /* First check for boolean-index cases. */
2297  opfamily = index->opfamily[indexcol];
2298  if (IsBooleanOpfamily(opfamily))
2299  {
2300  iclause = match_boolean_index_clause(root, rinfo, indexcol, index);
2301  if (iclause)
2302  return iclause;
2303  }
2304 
2305  /*
2306  * Clause must be an opclause, funcclause, ScalarArrayOpExpr, or
2307  * RowCompareExpr. Or, if the index supports it, we can handle IS
2308  * NULL/NOT NULL clauses.
2309  */
2310  if (IsA(clause, OpExpr))
2311  {
2312  return match_opclause_to_indexcol(root, rinfo, indexcol, index);
2313  }
2314  else if (IsA(clause, FuncExpr))
2315  {
2316  return match_funcclause_to_indexcol(root, rinfo, indexcol, index);
2317  }
2318  else if (IsA(clause, ScalarArrayOpExpr))
2319  {
2320  return match_saopclause_to_indexcol(root, rinfo, indexcol, index);
2321  }
2322  else if (IsA(clause, RowCompareExpr))
2323  {
2324  return match_rowcompare_to_indexcol(root, rinfo, indexcol, index);
2325  }
2326  else if (index->amsearchnulls && IsA(clause, NullTest))
2327  {
2328  NullTest *nt = (NullTest *) clause;
2329 
2330  if (!nt->argisrow &&
2331  match_index_to_operand((Node *) nt->arg, indexcol, index))
2332  {
2333  iclause = makeNode(IndexClause);
2334  iclause->rinfo = rinfo;
2335  iclause->indexquals = list_make1(rinfo);
2336  iclause->lossy = false;
2337  iclause->indexcol = indexcol;
2338  iclause->indexcols = NIL;
2339  return iclause;
2340  }
2341  }
2342 
2343  return NULL;
2344 }
static IndexClause * match_saopclause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2681
static IndexClause * match_opclause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2450
static IndexClause * match_rowcompare_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2749
static IndexClause * match_funcclause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2569
bool argisrow
Definition: primnodes.h:1528
Expr * arg
Definition: primnodes.h:1526

References NullTest::arg, NullTest::argisrow, Assert(), RestrictInfo::clause, IndexClause::indexcol, IndexClause::indexcols, IndexClause::indexquals, IsA, list_make1, IndexClause::lossy, makeNode, match_boolean_index_clause(), match_funcclause_to_indexcol(), match_index_to_operand(), match_opclause_to_indexcol(), match_rowcompare_to_indexcol(), match_saopclause_to_indexcol(), NIL, and IndexClause::rinfo.

Referenced by match_clause_to_index().

◆ match_clause_to_ordering_op()

static Expr * match_clause_to_ordering_op ( IndexOptInfo index,
int  indexcol,
Expr clause,
Oid  pk_opfamily 
)
static

Definition at line 3193 of file indxpath.c.

3197 {
3198  Oid opfamily;
3199  Oid idxcollation;
3200  Node *leftop,
3201  *rightop;
3202  Oid expr_op;
3203  Oid expr_coll;
3204  Oid sortfamily;
3205  bool commuted;
3206 
3207  Assert(indexcol < index->nkeycolumns);
3208 
3209  opfamily = index->opfamily[indexcol];
3210  idxcollation = index->indexcollations[indexcol];
3211 
3212  /*
3213  * Clause must be a binary opclause.
3214  */
3215  if (!is_opclause(clause))
3216  return NULL;
3217  leftop = get_leftop(clause);
3218  rightop = get_rightop(clause);
3219  if (!leftop || !rightop)
3220  return NULL;
3221  expr_op = ((OpExpr *) clause)->opno;
3222  expr_coll = ((OpExpr *) clause)->inputcollid;
3223 
3224  /*
3225  * We can forget the whole thing right away if wrong collation.
3226  */
3227  if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
3228  return NULL;
3229 
3230  /*
3231  * Check for clauses of the form: (indexkey operator constant) or
3232  * (constant operator indexkey).
3233  */
3234  if (match_index_to_operand(leftop, indexcol, index) &&
3235  !contain_var_clause(rightop) &&
3236  !contain_volatile_functions(rightop))
3237  {
3238  commuted = false;
3239  }
3240  else if (match_index_to_operand(rightop, indexcol, index) &&
3241  !contain_var_clause(leftop) &&
3242  !contain_volatile_functions(leftop))
3243  {
3244  /* Might match, but we need a commuted operator */
3245  expr_op = get_commutator(expr_op);
3246  if (expr_op == InvalidOid)
3247  return NULL;
3248  commuted = true;
3249  }
3250  else
3251  return NULL;
3252 
3253  /*
3254  * Is the (commuted) operator an ordering operator for the opfamily? And
3255  * if so, does it yield the right sorting semantics?
3256  */
3257  sortfamily = get_op_opfamily_sortfamily(expr_op, opfamily);
3258  if (sortfamily != pk_opfamily)
3259  return NULL;
3260 
3261  /* We have a match. Return clause or a commuted version thereof. */
3262  if (commuted)
3263  {
3264  OpExpr *newclause = makeNode(OpExpr);
3265 
3266  /* flat-copy all the fields of clause */
3267  memcpy(newclause, clause, sizeof(OpExpr));
3268 
3269  /* commute it */
3270  newclause->opno = expr_op;
3271  newclause->opfuncid = InvalidOid;
3272  newclause->args = list_make2(rightop, leftop);
3273 
3274  clause = (Expr *) newclause;
3275  }
3276 
3277  return clause;
3278 }
Oid get_op_opfamily_sortfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:106
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:64
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:83
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:71
#define list_make2(x1, x2)
Definition: pg_list.h:208
Oid opno
Definition: primnodes.h:551
List * args
Definition: primnodes.h:557
Oid opfuncid
Definition: primnodes.h:552
bool contain_var_clause(Node *node)
Definition: var.c:400

References OpExpr::args, Assert(), contain_var_clause(), contain_volatile_functions(), get_commutator(), get_leftop(), get_op_opfamily_sortfamily(), get_rightop(), IndexCollMatchesExprColl, InvalidOid, is_opclause(), list_make2, makeNode, match_index_to_operand(), OpExpr::opfuncid, and OpExpr::opno.

Referenced by match_pathkeys_to_index().

◆ match_clauses_to_index()

static void match_clauses_to_index ( PlannerInfo root,
List clauses,
IndexOptInfo index,
IndexClauseSet clauseset 
)
static

Definition at line 2126 of file indxpath.c.

2130 {
2131  ListCell *lc;
2132 
2133  foreach(lc, clauses)
2134  {
2135  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
2136 
2137  match_clause_to_index(root, rinfo, index, clauseset);
2138  }
2139 }
static void match_clause_to_index(PlannerInfo *root, RestrictInfo *rinfo, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2159

References lfirst_node, and match_clause_to_index().

Referenced by build_paths_for_OR(), match_eclass_clauses_to_index(), and match_restriction_clauses_to_index().

◆ match_eclass_clauses_to_index()

static void match_eclass_clauses_to_index ( PlannerInfo root,
IndexOptInfo index,
IndexClauseSet clauseset 
)
static

Definition at line 2088 of file indxpath.c.

2090 {
2091  int indexcol;
2092 
2093  /* No work if rel is not in any such ECs */
2094  if (!index->rel->has_eclass_joins)
2095  return;
2096 
2097  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
2098  {
2100  List *clauses;
2101 
2102  /* Generate clauses, skipping any that join to lateral_referencers */
2103  arg.index = index;
2104  arg.indexcol = indexcol;
2106  index->rel,
2108  (void *) &arg,
2109  index->rel->lateral_referencers);
2110 
2111  /*
2112  * We have to check whether the results actually do match the index,
2113  * since for non-btree indexes the EC's equality operators might not
2114  * be in the index opclass (cf ec_member_matches_indexcol).
2115  */
2116  match_clauses_to_index(root, clauses, index, clauseset);
2117  }
2118 }
List * generate_implied_equalities_for_column(PlannerInfo *root, RelOptInfo *rel, ec_matches_callback_type callback, void *callback_arg, Relids prohibited_rels)
Definition: equivclass.c:2832
static bool ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, EquivalenceMember *em, void *arg)
Definition: indxpath.c:3440

References arg, ec_member_matches_indexcol(), generate_implied_equalities_for_column(), and match_clauses_to_index().

Referenced by create_index_paths().

◆ match_funcclause_to_indexcol()

static IndexClause * match_funcclause_to_indexcol ( PlannerInfo root,
RestrictInfo rinfo,
int  indexcol,
IndexOptInfo index 
)
static

Definition at line 2569 of file indxpath.c.

2573 {
2574  FuncExpr *clause = (FuncExpr *) rinfo->clause;
2575  int indexarg;
2576  ListCell *lc;
2577 
2578  /*
2579  * We have no built-in intelligence about function clauses, but if there's
2580  * a planner support function, it might be able to do something. But, to
2581  * cut down on wasted planning cycles, only call the support function if
2582  * at least one argument matches the target index column.
2583  *
2584  * Note that we don't insist on the other arguments being pseudoconstants;
2585  * the support function has to check that. This is to allow cases where
2586  * only some of the other arguments need to be included in the indexqual.
2587  */
2588  indexarg = 0;
2589  foreach(lc, clause->args)
2590  {
2591  Node *op = (Node *) lfirst(lc);
2592 
2593  if (match_index_to_operand(op, indexcol, index))
2594  {
2595  return get_index_clause_from_support(root,
2596  rinfo,
2597  clause->funcid,
2598  indexarg,
2599  indexcol,
2600  index);
2601  }
2602 
2603  indexarg++;
2604  }
2605 
2606  return NULL;
2607 }
static IndexClause * get_index_clause_from_support(PlannerInfo *root, RestrictInfo *rinfo, Oid funcid, int indexarg, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2615
Oid funcid
Definition: primnodes.h:504
List * args
Definition: primnodes.h:512

References FuncExpr::args, RestrictInfo::clause, FuncExpr::funcid, get_index_clause_from_support(), lfirst, and match_index_to_operand().

Referenced by match_clause_to_indexcol().

◆ match_index_to_operand()

bool match_index_to_operand ( Node operand,
int  indexcol,
IndexOptInfo index 
)

Definition at line 3720 of file indxpath.c.

3723 {
3724  int indkey;
3725 
3726  /*
3727  * Ignore any RelabelType node above the operand. This is needed to be
3728  * able to apply indexscanning in binary-compatible-operator cases. Note:
3729  * we can assume there is at most one RelabelType node;
3730  * eval_const_expressions() will have simplified if more than one.
3731  */
3732  if (operand && IsA(operand, RelabelType))
3733  operand = (Node *) ((RelabelType *) operand)->arg;
3734 
3735  indkey = index->indexkeys[indexcol];
3736  if (indkey != 0)
3737  {
3738  /*
3739  * Simple index column; operand must be a matching Var.
3740  */
3741  if (operand && IsA(operand, Var) &&
3742  index->rel->relid == ((Var *) operand)->varno &&
3743  indkey == ((Var *) operand)->varattno)
3744  return true;
3745  }
3746  else
3747  {
3748  /*
3749  * Index expression; find the correct expression. (This search could
3750  * be avoided, at the cost of complicating all the callers of this
3751  * routine; doesn't seem worth it.)
3752  */
3753  ListCell *indexpr_item;
3754  int i;
3755  Node *indexkey;
3756 
3757  indexpr_item = list_head(index->indexprs);
3758  for (i = 0; i < indexcol; i++)
3759  {
3760  if (index->indexkeys[i] == 0)
3761  {
3762  if (indexpr_item == NULL)
3763  elog(ERROR, "wrong number of index expressions");
3764  indexpr_item = lnext(index->indexprs, indexpr_item);
3765  }
3766  }
3767  if (indexpr_item == NULL)
3768  elog(ERROR, "wrong number of index expressions");
3769  indexkey = (Node *) lfirst(indexpr_item);
3770 
3771  /*
3772  * Does it match the operand? Again, strip any relabeling.
3773  */
3774  if (indexkey && IsA(indexkey, RelabelType))
3775  indexkey = (Node *) ((RelabelType *) indexkey)->arg;
3776 
3777  if (equal(indexkey, operand))
3778  return true;
3779  }
3780 
3781  return false;
3782 }
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
Definition: primnodes.h:196

References arg, elog, equal(), ERROR, i, IsA, lfirst, list_head(), and lnext().

Referenced by ec_member_matches_indexcol(), expand_indexqual_rowcompare(), get_actual_variable_range(), match_boolean_index_clause(), match_clause_to_indexcol(), match_clause_to_ordering_op(), match_funcclause_to_indexcol(), match_opclause_to_indexcol(), match_rowcompare_to_indexcol(), match_saopclause_to_indexcol(), and relation_has_unique_index_for().

◆ match_join_clauses_to_index()

static void match_join_clauses_to_index ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet clauseset,
List **  joinorclauses 
)
static

Definition at line 2058 of file indxpath.c.

2062 {
2063  ListCell *lc;
2064 
2065  /* Scan the rel's join clauses */
2066  foreach(lc, rel->joininfo)
2067  {
2068  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2069 
2070  /* Check if clause can be moved to this rel */
2071  if (!join_clause_is_movable_to(rinfo, rel))
2072  continue;
2073 
2074  /* Potentially usable, so see if it matches the index or is an OR */
2075  if (restriction_is_or_clause(rinfo))
2076  *joinorclauses = lappend(*joinorclauses, rinfo);
2077  else
2078  match_clause_to_index(root, rinfo, index, clauseset);
2079  }
2080 }

References join_clause_is_movable_to(), RelOptInfo::joininfo, lappend(), lfirst, match_clause_to_index(), and restriction_is_or_clause().

Referenced by create_index_paths().

◆ match_opclause_to_indexcol()

static IndexClause * match_opclause_to_indexcol ( PlannerInfo root,
RestrictInfo rinfo,
int  indexcol,
IndexOptInfo index 
)
static

Definition at line 2450 of file indxpath.c.

2454 {
2455  IndexClause *iclause;
2456  OpExpr *clause = (OpExpr *) rinfo->clause;
2457  Node *leftop,
2458  *rightop;
2459  Oid expr_op;
2460  Oid expr_coll;
2461  Index index_relid;
2462  Oid opfamily;
2463  Oid idxcollation;
2464 
2465  /*
2466  * Only binary operators need apply. (In theory, a planner support
2467  * function could do something with a unary operator, but it seems
2468  * unlikely to be worth the cycles to check.)
2469  */
2470  if (list_length(clause->args) != 2)
2471  return NULL;
2472 
2473  leftop = (Node *) linitial(clause->args);
2474  rightop = (Node *) lsecond(clause->args);
2475  expr_op = clause->opno;
2476  expr_coll = clause->inputcollid;
2477 
2478  index_relid = index->rel->relid;
2479  opfamily = index->opfamily[indexcol];
2480  idxcollation = index->indexcollations[indexcol];
2481 
2482  /*
2483  * Check for clauses of the form: (indexkey operator constant) or
2484  * (constant operator indexkey). See match_clause_to_indexcol's notes
2485  * about const-ness.
2486  *
2487  * Note that we don't ask the support function about clauses that don't
2488  * have one of these forms. Again, in principle it might be possible to
2489  * do something, but it seems unlikely to be worth the cycles to check.
2490  */
2491  if (match_index_to_operand(leftop, indexcol, index) &&
2492  !bms_is_member(index_relid, rinfo->right_relids) &&
2493  !contain_volatile_functions(rightop))
2494  {
2495  if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2496  op_in_opfamily(expr_op, opfamily))
2497  {
2498  iclause = makeNode(IndexClause);
2499  iclause->rinfo = rinfo;
2500  iclause->indexquals = list_make1(rinfo);
2501  iclause->lossy = false;
2502  iclause->indexcol = indexcol;
2503  iclause->indexcols = NIL;
2504  return iclause;
2505  }
2506 
2507  /*
2508  * If we didn't find a member of the index's opfamily, try the support
2509  * function for the operator's underlying function.
2510  */
2511  set_opfuncid(clause); /* make sure we have opfuncid */
2512  return get_index_clause_from_support(root,
2513  rinfo,
2514  clause->opfuncid,
2515  0, /* indexarg on left */
2516  indexcol,
2517  index);
2518  }
2519 
2520  if (match_index_to_operand(rightop, indexcol, index) &&
2521  !bms_is_member(index_relid, rinfo->left_relids) &&
2522  !contain_volatile_functions(leftop))
2523  {
2524  if (IndexCollMatchesExprColl(idxcollation, expr_coll))
2525  {
2526  Oid comm_op = get_commutator(expr_op);
2527 
2528  if (OidIsValid(comm_op) &&
2529  op_in_opfamily(comm_op, opfamily))
2530  {
2531  RestrictInfo *commrinfo;
2532 
2533  /* Build a commuted OpExpr and RestrictInfo */
2534  commrinfo = commute_restrictinfo(rinfo, comm_op);
2535 
2536  /* Make an IndexClause showing that as a derived qual */
2537  iclause = makeNode(IndexClause);
2538  iclause->rinfo = rinfo;
2539  iclause->indexquals = list_make1(commrinfo);
2540  iclause->lossy = false;
2541  iclause->indexcol = indexcol;
2542  iclause->indexcols = NIL;
2543  return iclause;
2544  }
2545  }
2546 
2547  /*
2548  * If we didn't find a member of the index's opfamily, try the support
2549  * function for the operator's underlying function.
2550  */
2551  set_opfuncid(clause); /* make sure we have opfuncid */
2552  return get_index_clause_from_support(root,
2553  rinfo,
2554  clause->opfuncid,
2555  1, /* indexarg on right */
2556  indexcol,
2557  index);
2558  }
2559 
2560  return NULL;
2561 }
unsigned int Index
Definition: c.h:549
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:64
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1794
#define lsecond(l)
Definition: pg_list.h:179
RestrictInfo * commute_restrictinfo(RestrictInfo *rinfo, Oid comm_op)
Definition: restrictinfo.c:327
Oid inputcollid
Definition: primnodes.h:556
Relids right_relids
Definition: pathnodes.h:2106
Relids left_relids
Definition: pathnodes.h:2105

References OpExpr::args, bms_is_member(), RestrictInfo::clause, commute_restrictinfo(), contain_volatile_functions(), get_commutator(), get_index_clause_from_support(), if(), IndexClause::indexcol, IndexCollMatchesExprColl, IndexClause::indexcols, IndexClause::indexquals, OpExpr::inputcollid, RestrictInfo::left_relids, linitial, list_length(), list_make1, IndexClause::lossy, lsecond, makeNode, match_index_to_operand(), NIL, OidIsValid, op_in_opfamily(), OpExpr::opfuncid, OpExpr::opno, RestrictInfo::right_relids, IndexClause::rinfo, and set_opfuncid().

Referenced by match_clause_to_indexcol().

◆ match_pathkeys_to_index()

static void match_pathkeys_to_index ( IndexOptInfo index,
List pathkeys,
List **  orderby_clauses_p,
List **  clause_columns_p 
)
static

Definition at line 3078 of file indxpath.c.

3081 {
3082  List *orderby_clauses = NIL;
3083  List *clause_columns = NIL;
3084  ListCell *lc1;
3085 
3086  *orderby_clauses_p = NIL; /* set default results */
3087  *clause_columns_p = NIL;
3088 
3089  /* Only indexes with the amcanorderbyop property are interesting here */
3090  if (!index->amcanorderbyop)
3091  return;
3092 
3093  foreach(lc1, pathkeys)
3094  {
3095  PathKey *pathkey = (PathKey *) lfirst(lc1);
3096  bool found = false;
3097  ListCell *lc2;
3098 
3099  /*
3100  * Note: for any failure to match, we just return NIL immediately.
3101  * There is no value in matching just some of the pathkeys.
3102  */
3103 
3104  /* Pathkey must request default sort order for the target opfamily */
3105  if (pathkey->pk_strategy != BTLessStrategyNumber ||
3106  pathkey->pk_nulls_first)
3107  return;
3108 
3109  /* If eclass is volatile, no hope of using an indexscan */
3110  if (pathkey->pk_eclass->ec_has_volatile)
3111  return;
3112 
3113  /*
3114  * Try to match eclass member expression(s) to index. Note that child
3115  * EC members are considered, but only when they belong to the target
3116  * relation. (Unlike regular members, the same expression could be a
3117  * child member of more than one EC. Therefore, the same index could
3118  * be considered to match more than one pathkey list, which is OK
3119  * here. See also get_eclass_for_sort_expr.)
3120  */
3121  foreach(lc2, pathkey->pk_eclass->ec_members)
3122  {
3123  EquivalenceMember *member = (EquivalenceMember *) lfirst(lc2);
3124  int indexcol;
3125 
3126  /* No possibility of match if it references other relations */
3127  if (!bms_equal(member->em_relids, index->rel->relids))
3128  continue;
3129 
3130  /*
3131  * We allow any column of the index to match each pathkey; they
3132  * don't have to match left-to-right as you might expect. This is
3133  * correct for GiST, and it doesn't matter for SP-GiST because
3134  * that doesn't handle multiple columns anyway, and no other
3135  * existing AMs support amcanorderbyop. We might need different
3136  * logic in future for other implementations.
3137  */
3138  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
3139  {
3140  Expr *expr;
3141 
3143  indexcol,
3144  member->em_expr,
3145  pathkey->pk_opfamily);
3146  if (expr)
3147  {
3148  orderby_clauses = lappend(orderby_clauses, expr);
3149  clause_columns = lappend_int(clause_columns, indexcol);
3150  found = true;
3151  break;
3152  }
3153  }
3154 
3155  if (found) /* don't want to look at remaining members */
3156  break;
3157  }
3158 
3159  if (!found) /* fail if no match for this pathkey */
3160  return;
3161  }
3162 
3163  *orderby_clauses_p = orderby_clauses; /* success! */
3164  *clause_columns_p = clause_columns;
3165 }
static Expr * match_clause_to_ordering_op(IndexOptInfo *index, int indexcol, Expr *clause, Oid pk_opfamily)
Definition: indxpath.c:3193
List * ec_members
Definition: pathnodes.h:991
bool ec_has_volatile
Definition: pathnodes.h:997
bool pk_nulls_first
Definition: pathnodes.h:1071
int pk_strategy
Definition: pathnodes.h:1070
EquivalenceClass * pk_eclass
Definition: pathnodes.h:1068
Oid pk_opfamily
Definition: pathnodes.h:1069

References bms_equal(), BTLessStrategyNumber, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceMember::em_expr, EquivalenceMember::em_relids, lappend(), lappend_int(), lfirst, match_clause_to_ordering_op(), NIL, PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_opfamily, and PathKey::pk_strategy.

Referenced by build_index_paths().

◆ match_restriction_clauses_to_index()

static void match_restriction_clauses_to_index ( PlannerInfo root,
IndexOptInfo index,
IndexClauseSet clauseset 
)
static

Definition at line 2043 of file indxpath.c.

2046 {
2047  /* We can ignore clauses that are implied by the index predicate */
2048  match_clauses_to_index(root, index->indrestrictinfo, index, clauseset);
2049 }

References match_clauses_to_index().

Referenced by create_index_paths().

◆ match_rowcompare_to_indexcol()

static IndexClause * match_rowcompare_to_indexcol ( PlannerInfo root,
RestrictInfo rinfo,
int  indexcol,
IndexOptInfo index 
)
static

Definition at line 2749 of file indxpath.c.

2753 {
2754  RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
2755  Index index_relid;
2756  Oid opfamily;
2757  Oid idxcollation;
2758  Node *leftop,
2759  *rightop;
2760  bool var_on_left;
2761  Oid expr_op;
2762  Oid expr_coll;
2763 
2764  /* Forget it if we're not dealing with a btree index */
2765  if (index->relam != BTREE_AM_OID)
2766  return NULL;
2767 
2768  index_relid = index->rel->relid;
2769  opfamily = index->opfamily[indexcol];
2770  idxcollation = index->indexcollations[indexcol];
2771 
2772  /*
2773  * We could do the matching on the basis of insisting that the opfamily
2774  * shown in the RowCompareExpr be the same as the index column's opfamily,
2775  * but that could fail in the presence of reverse-sort opfamilies: it'd be
2776  * a matter of chance whether RowCompareExpr had picked the forward or
2777  * reverse-sort family. So look only at the operator, and match if it is
2778  * a member of the index's opfamily (after commutation, if the indexkey is
2779  * on the right). We'll worry later about whether any additional
2780  * operators are matchable to the index.
2781  */
2782  leftop = (Node *) linitial(clause->largs);
2783  rightop = (Node *) linitial(clause->rargs);
2784  expr_op = linitial_oid(clause->opnos);
2785  expr_coll = linitial_oid(clause->inputcollids);
2786 
2787  /* Collations must match, if relevant */
2788  if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
2789  return NULL;
2790 
2791  /*
2792  * These syntactic tests are the same as in match_opclause_to_indexcol()
2793  */
2794  if (match_index_to_operand(leftop, indexcol, index) &&
2795  !bms_is_member(index_relid, pull_varnos(root, rightop)) &&
2796  !contain_volatile_functions(rightop))
2797  {
2798  /* OK, indexkey is on left */
2799  var_on_left = true;
2800  }
2801  else if (match_index_to_operand(rightop, indexcol, index) &&
2802  !bms_is_member(index_relid, pull_varnos(root, leftop)) &&
2803  !contain_volatile_functions(leftop))
2804  {
2805  /* indexkey is on right, so commute the operator */
2806  expr_op = get_commutator(expr_op);
2807  if (expr_op == InvalidOid)
2808  return NULL;
2809  var_on_left = false;
2810  }
2811  else
2812  return NULL;
2813 
2814  /* We're good if the operator is the right type of opfamily member */
2815  switch (get_op_opfamily_strategy(expr_op, opfamily))
2816  {
2817  case BTLessStrategyNumber:
2821  return expand_indexqual_rowcompare(root,
2822  rinfo,
2823  indexcol,
2824  index,
2825  expr_op,
2826  var_on_left);
2827  }
2828 
2829  return NULL;
2830 }
static IndexClause * expand_indexqual_rowcompare(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index, Oid expr_op, bool var_on_left)
Definition: indxpath.c:2856

References bms_is_member(), BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, RestrictInfo::clause, contain_volatile_functions(), expand_indexqual_rowcompare(), get_commutator(), get_op_opfamily_strategy(), if(), IndexCollMatchesExprColl, RowCompareExpr::inputcollids, InvalidOid, RowCompareExpr::largs, linitial, linitial_oid, match_index_to_operand(), RowCompareExpr::opnos, pull_varnos(), and RowCompareExpr::rargs.

Referenced by match_clause_to_indexcol().

◆ match_saopclause_to_indexcol()

static IndexClause * match_saopclause_to_indexcol ( PlannerInfo root,
RestrictInfo rinfo,
int  indexcol,
IndexOptInfo index 
)
static

Definition at line 2681 of file indxpath.c.

2685 {
2686  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
2687  Node *leftop,
2688  *rightop;
2689  Relids right_relids;
2690  Oid expr_op;
2691  Oid expr_coll;
2692  Index index_relid;
2693  Oid opfamily;
2694  Oid idxcollation;
2695 
2696  /* We only accept ANY clauses, not ALL */
2697  if (!saop->useOr)
2698  return NULL;
2699  leftop = (Node *) linitial(saop->args);
2700  rightop = (Node *) lsecond(saop->args);
2701  right_relids = pull_varnos(root, rightop);
2702  expr_op = saop->opno;
2703  expr_coll = saop->inputcollid;
2704 
2705  index_relid = index->rel->relid;
2706  opfamily = index->opfamily[indexcol];
2707  idxcollation = index->indexcollations[indexcol];
2708 
2709  /*
2710  * We must have indexkey on the left and a pseudo-constant array argument.
2711  */
2712  if (match_index_to_operand(leftop, indexcol, index) &&
2713  !bms_is_member(index_relid, right_relids) &&
2714  !contain_volatile_functions(rightop))
2715  {
2716  if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2717  op_in_opfamily(expr_op, opfamily))
2718  {
2719  IndexClause *iclause = makeNode(IndexClause);
2720 
2721  iclause->rinfo = rinfo;
2722  iclause->indexquals = list_make1(rinfo);
2723  iclause->lossy = false;
2724  iclause->indexcol = indexcol;
2725  iclause->indexcols = NIL;
2726  return iclause;
2727  }
2728 
2729  /*
2730  * We do not currently ask support functions about ScalarArrayOpExprs,
2731  * though in principle we could.
2732  */
2733  }
2734 
2735  return NULL;
2736 }

References ScalarArrayOpExpr::args, bms_is_member(), RestrictInfo::clause, contain_volatile_functions(), if(), IndexClause::indexcol, IndexCollMatchesExprColl, IndexClause::indexcols, IndexClause::indexquals, ScalarArrayOpExpr::inputcollid, linitial, list_make1, IndexClause::lossy, lsecond, makeNode, match_index_to_operand(), NIL, op_in_opfamily(), ScalarArrayOpExpr::opno, pull_varnos(), IndexClause::rinfo, and ScalarArrayOpExpr::useOr.

Referenced by match_clause_to_indexcol().

◆ path_usage_comparator()

static int path_usage_comparator ( const void *  a,
const void *  b 
)
static

Definition at line 1568 of file indxpath.c.

1569 {
1570  PathClauseUsage *pa = *(PathClauseUsage *const *) a;
1571  PathClauseUsage *pb = *(PathClauseUsage *const *) b;
1572  Cost acost;
1573  Cost bcost;
1574  Selectivity aselec;
1575  Selectivity bselec;
1576 
1577  cost_bitmap_tree_node(pa->path, &acost, &aselec);
1578  cost_bitmap_tree_node(pb->path, &bcost, &bselec);
1579 
1580  /*
1581  * If costs are the same, sort by selectivity.
1582  */
1583  if (acost < bcost)
1584  return -1;
1585  if (acost > bcost)
1586  return 1;
1587 
1588  if (aselec < bselec)
1589  return -1;
1590  if (aselec > bselec)
1591  return 1;
1592 
1593  return 0;
1594 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69

References a, b, cost_bitmap_tree_node(), and PathClauseUsage::path.

Referenced by choose_bitmap_and().

◆ relation_has_unique_index_for()

bool relation_has_unique_index_for ( PlannerInfo root,
RelOptInfo rel,
List restrictlist,
List exprlist,
List oprlist 
)

Definition at line 3498 of file indxpath.c.

3501 {
3502  ListCell *ic;
3503 
3504  Assert(list_length(exprlist) == list_length(oprlist));
3505 
3506  /* Short-circuit if no indexes... */
3507  if (rel->indexlist == NIL)
3508  return false;
3509 
3510  /*
3511  * Examine the rel's restriction clauses for usable var = const clauses
3512  * that we can add to the restrictlist.
3513  */
3514  foreach(ic, rel->baserestrictinfo)
3515  {
3516  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(ic);
3517 
3518  /*
3519  * Note: can_join won't be set for a restriction clause, but
3520  * mergeopfamilies will be if it has a mergejoinable operator and
3521  * doesn't contain volatile functions.
3522  */
3523  if (restrictinfo->mergeopfamilies == NIL)
3524  continue; /* not mergejoinable */
3525 
3526  /*
3527  * The clause certainly doesn't refer to anything but the given rel.
3528  * If either side is pseudoconstant then we can use it.
3529  */
3530  if (bms_is_empty(restrictinfo->left_relids))
3531  {
3532  /* righthand side is inner */
3533  restrictinfo->outer_is_left = true;
3534  }
3535  else if (bms_is_empty(restrictinfo->right_relids))
3536  {
3537  /* lefthand side is inner */
3538  restrictinfo->outer_is_left = false;
3539  }
3540  else
3541  continue;
3542 
3543  /* OK, add to list */
3544  restrictlist = lappend(restrictlist, restrictinfo);
3545  }
3546 
3547  /* Short-circuit the easy case */
3548  if (restrictlist == NIL && exprlist == NIL)
3549  return false;
3550 
3551  /* Examine each index of the relation ... */
3552  foreach(ic, rel->indexlist)
3553  {
3554  IndexOptInfo *ind = (IndexOptInfo *) lfirst(ic);
3555  int c;
3556 
3557  /*
3558  * If the index is not unique, or not immediately enforced, or if it's
3559  * a partial index that doesn't match the query, it's useless here.
3560  */
3561  if (!ind->unique || !ind->immediate ||
3562  (ind->indpred != NIL && !ind->predOK))
3563  continue;
3564 
3565  /*
3566  * Try to find each index column in the lists of conditions. This is
3567  * O(N^2) or worse, but we expect all the lists to be short.
3568  */
3569  for (c = 0; c < ind->nkeycolumns; c++)
3570  {
3571  bool matched = false;
3572  ListCell *lc;
3573  ListCell *lc2;
3574 
3575  foreach(lc, restrictlist)
3576  {
3577  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3578  Node *rexpr;
3579 
3580  /*
3581  * The condition's equality operator must be a member of the
3582  * index opfamily, else it is not asserting the right kind of
3583  * equality behavior for this index. We check this first
3584  * since it's probably cheaper than match_index_to_operand().
3585  */
3586  if (!list_member_oid(rinfo->mergeopfamilies, ind->opfamily[c]))
3587  continue;
3588 
3589  /*
3590  * XXX at some point we may need to check collations here too.
3591  * For the moment we assume all collations reduce to the same
3592  * notion of equality.
3593  */
3594 
3595  /* OK, see if the condition operand matches the index key */
3596  if (rinfo->outer_is_left)
3597  rexpr = get_rightop(rinfo->clause);
3598  else
3599  rexpr = get_leftop(rinfo->clause);
3600 
3601  if (match_index_to_operand(rexpr, c, ind))
3602  {
3603  matched = true; /* column is unique */
3604  break;
3605  }
3606  }
3607 
3608  if (matched)
3609  continue;
3610 
3611  forboth(lc, exprlist, lc2, oprlist)
3612  {
3613  Node *expr = (Node *) lfirst(lc);
3614  Oid opr = lfirst_oid(lc2);
3615 
3616  /* See if the expression matches the index key */
3617  if (!match_index_to_operand(expr, c, ind))
3618  continue;
3619 
3620  /*
3621  * The equality operator must be a member of the index
3622  * opfamily, else it is not asserting the right kind of
3623  * equality behavior for this index. We assume the caller
3624  * determined it is an equality operator, so we don't need to
3625  * check any more tightly than this.
3626  */
3627  if (!op_in_opfamily(opr, ind->opfamily[c]))
3628  continue;
3629 
3630  /*
3631  * XXX at some point we may need to check collations here too.
3632  * For the moment we assume all collations reduce to the same
3633  * notion of equality.
3634  */
3635 
3636  matched = true; /* column is unique */
3637  break;
3638  }
3639 
3640  if (!matched)
3641  break; /* no match; this index doesn't help us */
3642  }
3643 
3644  /* Matched all key columns of this index? */
3645  if (c == ind->nkeycolumns)
3646  return true;
3647  }
3648 
3649  return false;
3650 }
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
char * c
bool outer_is_left
Definition: pathnodes.h:2133
List * mergeopfamilies
Definition: pathnodes.h:2123

References Assert(), RelOptInfo::baserestrictinfo, bms_is_empty(), RestrictInfo::clause, forboth, get_leftop(), get_rightop(), RelOptInfo::indexlist, lappend(), RestrictInfo::left_relids, lfirst, lfirst_oid, list_length(), list_member_oid(), match_index_to_operand(), RestrictInfo::mergeopfamilies, NIL, op_in_opfamily(), RestrictInfo::outer_is_left, and RestrictInfo::right_relids.

Referenced by create_unique_path(), and rel_is_distinct_for().