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 (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 (RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClausematch_rowcompare_to_indexcol (RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClauseexpand_indexqual_rowcompare (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 (IndexOptInfo *index, int indexcol)
 
bool match_index_to_operand (Node *operand, int indexcol, IndexOptInfo *index)
 
bool is_pseudo_constant_for_index (Node *expr, IndexOptInfo *index)
 

Macro Definition Documentation

◆ IndexCollMatchesExprColl

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

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:44

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 1966 of file indxpath.c.

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().

1970 {
1971  ListCell *lc;
1972 
1973  foreach(lc, root->join_info_list)
1974  {
1975  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
1976 
1977  if (sjinfo->jointype == JOIN_SEMI &&
1978  bms_is_member(cur_relid, sjinfo->syn_lefthand) &&
1979  bms_is_member(outer_relid, sjinfo->syn_righthand))
1980  {
1981  /* Estimate number of unique-ified rows */
1982  double nraw;
1983  double nunique;
1984 
1985  nraw = approximate_joinrel_size(root, sjinfo->syn_righthand);
1986  nunique = estimate_num_groups(root,
1987  sjinfo->semi_rhs_exprs,
1988  nraw,
1989  NULL);
1990  if (rowcount > nunique)
1991  rowcount = nunique;
1992  }
1993  }
1994  return rowcount;
1995 }
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset)
Definition: selfuncs.c:3360
List * join_info_list
Definition: pathnodes.h:281
static double approximate_joinrel_size(PlannerInfo *root, Relids relids)
Definition: indxpath.c:2009
Relids syn_lefthand
Definition: pathnodes.h:2179
Relids syn_righthand
Definition: pathnodes.h:2180
List * semi_rhs_exprs
Definition: pathnodes.h:2188
#define lfirst(lc)
Definition: pg_list.h:169
JoinType jointype
Definition: pathnodes.h:2181
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ approximate_joinrel_size()

static double approximate_joinrel_size ( PlannerInfo root,
Relids  relids 
)
static

Definition at line 2009 of file indxpath.c.

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().

2010 {
2011  double rowcount = 1.0;
2012  int relid;
2013 
2014  relid = -1;
2015  while ((relid = bms_next_member(relids, relid)) >= 0)
2016  {
2017  RelOptInfo *rel;
2018 
2019  /* Paranoia: ignore bogus relid indexes */
2020  if (relid >= root->simple_rel_array_size)
2021  continue;
2022  rel = root->simple_rel_array[relid];
2023  if (rel == NULL)
2024  continue;
2025  Assert(rel->relid == relid); /* sanity check on array */
2026 
2027  /* Relation could be proven empty, if so ignore */
2028  if (IS_DUMMY_REL(rel))
2029  continue;
2030 
2031  /* Otherwise, rel's rows estimate should be valid by now */
2032  Assert(rel->rows > 0);
2033 
2034  /* Accumulate product */
2035  rowcount *= rel->rows;
2036  }
2037  return rowcount;
2038 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1420
int simple_rel_array_size
Definition: pathnodes.h:202
Index relid
Definition: pathnodes.h:692
double rows
Definition: pathnodes.h:667
#define Assert(condition)
Definition: c.h:746

◆ bitmap_and_cost_est()

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

Definition at line 1631 of file indxpath.c.

References bitmap_scan_cost_est(), and create_bitmap_and_path().

Referenced by choose_bitmap_and().

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

◆ bitmap_scan_cost_est()

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

Definition at line 1597 of file indxpath.c.

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().

1598 {
1599  BitmapHeapPath bpath;
1600 
1601  /* Set up a dummy BitmapHeapPath */
1602  bpath.path.type = T_BitmapHeapPath;
1603  bpath.path.pathtype = T_BitmapHeapScan;
1604  bpath.path.parent = rel;
1605  bpath.path.pathtarget = rel->reltarget;
1606  bpath.path.param_info = ipath->param_info;
1607  bpath.path.pathkeys = NIL;
1608  bpath.bitmapqual = ipath;
1609 
1610  /*
1611  * Check the cost of temporary path without considering parallelism.
1612  * Parallel bitmap heap path will be considered at later stage.
1613  */
1614  bpath.path.parallel_workers = 0;
1615 
1616  /* Now we can do cost_bitmap_heap_scan */
1617  cost_bitmap_heap_scan(&bpath.path, root, rel,
1618  bpath.path.param_info,
1619  ipath,
1620  get_loop_count(root, rel->relid,
1621  PATH_REQ_OUTER(ipath)));
1622 
1623  return bpath.path.total_cost;
1624 }
#define NIL
Definition: pg_list.h:65
PathTarget * pathtarget
Definition: pathnodes.h:1147
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:952
NodeTag type
Definition: pathnodes.h:1142
int parallel_workers
Definition: pathnodes.h:1153
ParamPathInfo * param_info
Definition: pathnodes.h:1149
NodeTag pathtype
Definition: pathnodes.h:1144
RelOptInfo * parent
Definition: pathnodes.h:1146
Path * bitmapqual
Definition: pathnodes.h:1282
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1165
Index relid
Definition: pathnodes.h:692
static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
Definition: indxpath.c:1913
Cost total_cost
Definition: pathnodes.h:1158
List * pathkeys
Definition: pathnodes.h:1160
struct PathTarget * reltarget
Definition: pathnodes.h:675

◆ bms_equal_any()

static bool bms_equal_any ( Relids  relids,
List relids_list 
)
static

Definition at line 700 of file indxpath.c.

References bms_equal(), and lfirst.

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

701 {
702  ListCell *lc;
703 
704  foreach(lc, relids_list)
705  {
706  if (bms_equal(relids, (Relids) lfirst(lc)))
707  return true;
708  }
709  return false;
710 }
#define lfirst(lc)
Definition: pg_list.h:169
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ 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 851 of file indxpath.c.

References add_partial_path(), IndexOptInfo::amcanorderbyop, IndexOptInfo::amcanparallel, IndexOptInfo::amhasgetbitmap, IndexOptInfo::amhasgettuple, IndexOptInfo::amoptionalkey, IndexOptInfo::amsearcharray, 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, IndexOptInfo::nkeycolumns, NoMovementScanDirection, Path::parallel_workers, IndexPath::path, pfree(), PlannerInfo::query_pathkeys, RelOptInfo::relid, IndexClause::rinfo, IndexOptInfo::sortopfamily, ST_ANYSCAN, ST_BITMAPSCAN, ST_INDEXSCAN, and truncate_useless_pathkeys().

Referenced by build_paths_for_OR(), and get_index_paths().

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

◆ build_paths_for_OR()

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

Definition at line 1156 of file indxpath.c.

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

Referenced by generate_bitmap_or_paths().

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

◆ check_index_only()

static bool check_index_only ( RelOptInfo rel,
IndexOptInfo index 
)
static

Definition at line 1801 of file indxpath.c.

References bms_add_member(), bms_del_members(), bms_free(), bms_is_subset(), IndexOptInfo::canreturn, RestrictInfo::clause, enable_indexonlyscan, PathTarget::exprs, FirstLowInvalidHeapAttributeNumber, i, IndexOptInfo::indexkeys, IndexOptInfo::indrestrictinfo, lfirst, IndexOptInfo::ncolumns, pull_varattnos(), RelOptInfo::relid, and RelOptInfo::reltarget.

Referenced by build_index_paths().

1802 {
1803  bool result;
1804  Bitmapset *attrs_used = NULL;
1805  Bitmapset *index_canreturn_attrs = NULL;
1806  Bitmapset *index_cannotreturn_attrs = NULL;
1807  ListCell *lc;
1808  int i;
1809 
1810  /* Index-only scans must be enabled */
1811  if (!enable_indexonlyscan)
1812  return false;
1813 
1814  /*
1815  * Check that all needed attributes of the relation are available from the
1816  * index.
1817  */
1818 
1819  /*
1820  * First, identify all the attributes needed for joins or final output.
1821  * Note: we must look at rel's targetlist, not the attr_needed data,
1822  * because attr_needed isn't computed for inheritance child rels.
1823  */
1824  pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
1825 
1826  /*
1827  * Add all the attributes used by restriction clauses; but consider only
1828  * those clauses not implied by the index predicate, since ones that are
1829  * so implied don't need to be checked explicitly in the plan.
1830  *
1831  * Note: attributes used only in index quals would not be needed at
1832  * runtime either, if we are certain that the index is not lossy. However
1833  * it'd be complicated to account for that accurately, and it doesn't
1834  * matter in most cases, since we'd conclude that such attributes are
1835  * available from the index anyway.
1836  */
1837  foreach(lc, index->indrestrictinfo)
1838  {
1839  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1840 
1841  pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
1842  }
1843 
1844  /*
1845  * Construct a bitmapset of columns that the index can return back in an
1846  * index-only scan. If there are multiple index columns containing the
1847  * same attribute, all of them must be capable of returning the value,
1848  * since we might recheck operators on any of them. (Potentially we could
1849  * be smarter about that, but it's such a weird situation that it doesn't
1850  * seem worth spending a lot of sweat on.)
1851  */
1852  for (i = 0; i < index->ncolumns; i++)
1853  {
1854  int attno = index->indexkeys[i];
1855 
1856  /*
1857  * For the moment, we just ignore index expressions. It might be nice
1858  * to do something with them, later.
1859  */
1860  if (attno == 0)
1861  continue;
1862 
1863  if (index->canreturn[i])
1864  index_canreturn_attrs =
1865  bms_add_member(index_canreturn_attrs,
1867  else
1868  index_cannotreturn_attrs =
1869  bms_add_member(index_cannotreturn_attrs,
1871  }
1872 
1873  index_canreturn_attrs = bms_del_members(index_canreturn_attrs,
1874  index_cannotreturn_attrs);
1875 
1876  /* Do we have all the necessary attributes? */
1877  result = bms_is_subset(attrs_used, index_canreturn_attrs);
1878 
1879  bms_free(attrs_used);
1880  bms_free(index_canreturn_attrs);
1881  bms_free(index_cannotreturn_attrs);
1882 
1883  return result;
1884 }
Definition: nodes.h:528
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Index relid
Definition: pathnodes.h:692
Expr * clause
Definition: pathnodes.h:1987
List * exprs
Definition: pathnodes.h:1076
List * indrestrictinfo
Definition: pathnodes.h:847
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:928
int i
int * indexkeys
Definition: pathnodes.h:829
bool * canreturn
Definition: pathnodes.h:838
struct PathTarget * reltarget
Definition: pathnodes.h:675
bool enable_indexonlyscan
Definition: costsize.c:134

◆ check_index_predicates()

void check_index_predicates ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 3308 of file indxpath.c.

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

Referenced by set_plain_rel_size(), and set_tablesample_rel_size().

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

◆ choose_bitmap_and()

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

Definition at line 1358 of file indxpath.c.

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, 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().

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

◆ classify_index_clause_usage()

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

Definition at line 1660 of file indxpath.c.

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().

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

◆ 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 429 of file indxpath.c.

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

Referenced by create_index_paths().

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

◆ 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 495 of file indxpath.c.

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().

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

◆ create_index_paths()

void create_index_paths ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 231 of file indxpath.c.

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, IndexOptInfo::indpred, lappend(), RelOptInfo::lateral_relids, lfirst, list_concat(), match_eclass_clauses_to_index(), match_join_clauses_to_index(), match_restriction_clauses_to_index(), MemSet, NIL, IndexOptInfo::nkeycolumns, IndexClauseSet::nonempty, PATH_REQ_OUTER, IndexOptInfo::predOK, and RelOptInfo::relid.

Referenced by set_plain_rel_pathlist().

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

◆ ec_member_matches_indexcol()

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

Definition at line 3441 of file indxpath.c.

References Assert, EquivalenceClass::ec_collation, EquivalenceClass::ec_opfamilies, EquivalenceMember::em_expr, IndexOptInfo::indexcollations, IndexCollMatchesExprColl, list_member_oid(), match_index_to_operand(), IndexOptInfo::opfamily, and IndexOptInfo::relam.

Referenced by match_eclass_clauses_to_index().

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

◆ eclass_already_used()

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

Definition at line 676 of file indxpath.c.

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

Referenced by consider_index_join_outer_rels().

678 {
679  ListCell *lc;
680 
681  foreach(lc, indexjoinclauses)
682  {
683  IndexClause *iclause = (IndexClause *) lfirst(lc);
684  RestrictInfo *rinfo = iclause->rinfo;
685 
686  if (rinfo->parent_ec == parent_ec &&
687  bms_is_subset(rinfo->clause_relids, oldrelids))
688  return true;
689  }
690  return false;
691 }
Relids clause_relids
Definition: pathnodes.h:2002
EquivalenceClass * parent_ec
Definition: pathnodes.h:2021
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
struct RestrictInfo * rinfo
Definition: pathnodes.h:1255
#define lfirst(lc)
Definition: pg_list.h:169

◆ expand_indexqual_rowcompare()

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

Definition at line 2859 of file indxpath.c.

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, IndexClause::indexcol, IndexOptInfo::indexcollations, 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, IndexOptInfo::nkeycolumns, OidIsValid, RowCompareExpr::opfamilies, IndexOptInfo::opfamily, RowCompareExpr::opnos, pull_varnos(), RowCompareExpr::rargs, RowCompareExpr::rctype, IndexOptInfo::rel, RelOptInfo::relid, and IndexClause::rinfo.

Referenced by match_rowcompare_to_indexcol().

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

◆ find_indexpath_quals()

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

Definition at line 1728 of file indxpath.c.

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().

1729 {
1730  if (IsA(bitmapqual, BitmapAndPath))
1731  {
1732  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1733  ListCell *l;
1734 
1735  foreach(l, apath->bitmapquals)
1736  {
1737  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1738  }
1739  }
1740  else if (IsA(bitmapqual, BitmapOrPath))
1741  {
1742  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1743  ListCell *l;
1744 
1745  foreach(l, opath->bitmapquals)
1746  {
1747  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1748  }
1749  }
1750  else if (IsA(bitmapqual, IndexPath))
1751  {
1752  IndexPath *ipath = (IndexPath *) bitmapqual;
1753  ListCell *l;
1754 
1755  foreach(l, ipath->indexclauses)
1756  {
1757  IndexClause *iclause = (IndexClause *) lfirst(l);
1758 
1759  *quals = lappend(*quals, iclause->rinfo->clause);
1760  }
1761  *preds = list_concat(*preds, ipath->indexinfo->indpred);
1762  }
1763  else
1764  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1765 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
IndexOptInfo * indexinfo
Definition: pathnodes.h:1209
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
List * indexclauses
Definition: pathnodes.h:1210
List * bitmapquals
Definition: pathnodes.h:1294
List * bitmapquals
Definition: pathnodes.h:1307
static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
Definition: indxpath.c:1728
#define ERROR
Definition: elog.h:43
struct RestrictInfo * rinfo
Definition: pathnodes.h:1255
List * lappend(List *list, void *datum)
Definition: list.c:321
Expr * clause
Definition: pathnodes.h:1987
#define lfirst(lc)
Definition: pg_list.h:169
#define nodeTag(nodeptr)
Definition: nodes.h:533
#define elog(elevel,...)
Definition: elog.h:214
List * indpred
Definition: pathnodes.h:843

◆ find_list_position()

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

Definition at line 1775 of file indxpath.c.

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

Referenced by classify_index_clause_usage().

1776 {
1777  int i;
1778  ListCell *lc;
1779 
1780  i = 0;
1781  foreach(lc, *nodelist)
1782  {
1783  Node *oldnode = (Node *) lfirst(lc);
1784 
1785  if (equal(node, oldnode))
1786  return i;
1787  i++;
1788  }
1789 
1790  *nodelist = lappend(*nodelist, node);
1791 
1792  return i;
1793 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3032
Definition: nodes.h:528
List * lappend(List *list, void *datum)
Definition: list.c:321
#define lfirst(lc)
Definition: pg_list.h:169
int i

◆ generate_bitmap_or_paths()

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

Definition at line 1251 of file indxpath.c.

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

Referenced by create_index_paths().

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

◆ 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 2622 of file indxpath.c.

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

Referenced by match_funcclause_to_indexcol(), and match_opclause_to_indexcol().

2628 {
2629  Oid prosupport = get_func_support(funcid);
2631  List *sresult;
2632 
2633  if (!OidIsValid(prosupport))
2634  return NULL;
2635 
2637  req.root = root;
2638  req.funcid = funcid;
2639  req.node = (Node *) rinfo->clause;
2640  req.indexarg = indexarg;
2641  req.index = index;
2642  req.indexcol = indexcol;
2643  req.opfamily = index->opfamily[indexcol];
2644  req.indexcollation = index->indexcollations[indexcol];
2645 
2646  req.lossy = true; /* default assumption */
2647 
2648  sresult = (List *)
2649  DatumGetPointer(OidFunctionCall1(prosupport,
2650  PointerGetDatum(&req)));
2651 
2652  if (sresult != NIL)
2653  {
2654  IndexClause *iclause = makeNode(IndexClause);
2655  List *indexquals = NIL;
2656  ListCell *lc;
2657 
2658  /*
2659  * The support function API says it should just give back bare
2660  * clauses, so here we must wrap each one in a RestrictInfo.
2661  */
2662  foreach(lc, sresult)
2663  {
2664  Expr *clause = (Expr *) lfirst(lc);
2665 
2666  indexquals = lappend(indexquals, make_simple_restrictinfo(clause));
2667  }
2668 
2669  iclause->rinfo = rinfo;
2670  iclause->indexquals = indexquals;
2671  iclause->lossy = req.lossy;
2672  iclause->indexcol = indexcol;
2673  iclause->indexcols = NIL;
2674 
2675  return iclause;
2676  }
2677 
2678  return NULL;
2679 }
#define NIL
Definition: pg_list.h:65
Oid * indexcollations
Definition: pathnodes.h:831
#define PointerGetDatum(X)
Definition: postgres.h:556
Definition: nodes.h:528
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
struct IndexOptInfo * index
Definition: supportnodes.h:232
List * indexcols
Definition: pathnodes.h:1259
AttrNumber indexcol
Definition: pathnodes.h:1258
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:662
struct RestrictInfo * rinfo
Definition: pathnodes.h:1255
List * indexquals
Definition: pathnodes.h:1256
List * lappend(List *list, void *datum)
Definition: list.c:321
Expr * clause
Definition: pathnodes.h:1987
RegProcedure get_func_support(Oid funcid)
Definition: lsyscache.c:1770
#define makeNode(_type_)
Definition: nodes.h:576
#define lfirst(lc)
Definition: pg_list.h:169
#define DatumGetPointer(X)
Definition: postgres.h:549
Oid * opfamily
Definition: pathnodes.h:832
Definition: pg_list.h:50
#define make_simple_restrictinfo(clause)
Definition: restrictinfo.h:21
struct PlannerInfo * root
Definition: supportnodes.h:228

◆ get_index_paths()

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

Definition at line 729 of file indxpath.c.

References add_path(), IndexOptInfo::amhasgetbitmap, IndexOptInfo::amhasgettuple, build_index_paths(), lappend(), lfirst, list_concat(), NIL, Path::pathkeys, IndexOptInfo::predOK, ST_ANYSCAN, and ST_BITMAPSCAN.

Referenced by create_index_paths(), and get_join_index_paths().

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

◆ 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 598 of file indxpath.c.

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

Referenced by consider_index_join_outer_rels().

606 {
607  IndexClauseSet clauseset;
608  int indexcol;
609 
610  /* If we already considered this relids set, don't repeat the work */
611  if (bms_equal_any(relids, *considered_relids))
612  return;
613 
614  /* Identify indexclauses usable with this relids set */
615  MemSet(&clauseset, 0, sizeof(clauseset));
616 
617  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
618  {
619  ListCell *lc;
620 
621  /* First find applicable simple join clauses */
622  foreach(lc, jclauseset->indexclauses[indexcol])
623  {
624  IndexClause *iclause = (IndexClause *) lfirst(lc);
625 
626  if (bms_is_subset(iclause->rinfo->clause_relids, relids))
627  clauseset.indexclauses[indexcol] =
628  lappend(clauseset.indexclauses[indexcol], iclause);
629  }
630 
631  /*
632  * Add applicable eclass join clauses. The clauses generated for each
633  * column are redundant (cf generate_implied_equalities_for_column),
634  * so we need at most one. This is the only exception to the general
635  * rule of using all available index clauses.
636  */
637  foreach(lc, eclauseset->indexclauses[indexcol])
638  {
639  IndexClause *iclause = (IndexClause *) lfirst(lc);
640 
641  if (bms_is_subset(iclause->rinfo->clause_relids, relids))
642  {
643  clauseset.indexclauses[indexcol] =
644  lappend(clauseset.indexclauses[indexcol], iclause);
645  break;
646  }
647  }
648 
649  /* Add restriction clauses */
650  clauseset.indexclauses[indexcol] =
651  list_concat(clauseset.indexclauses[indexcol],
652  rclauseset->indexclauses[indexcol]);
653 
654  if (clauseset.indexclauses[indexcol] != NIL)
655  clauseset.nonempty = true;
656  }
657 
658  /* We should have found something, else caller passed silly relids */
659  Assert(clauseset.nonempty);
660 
661  /* Build index path(s) using the collected set of clauses */
662  get_index_paths(root, rel, index, &clauseset, bitindexpaths);
663 
664  /*
665  * Remember we considered paths for this set of relids.
666  */
667  *considered_relids = lappend(*considered_relids, relids);
668 }
#define NIL
Definition: pg_list.h:65
bool nonempty
Definition: indxpath.c:54
Relids clause_relids
Definition: pathnodes.h:2002
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
#define MemSet(start, val, len)
Definition: c.h:950
static bool bms_equal_any(Relids relids, List *relids_list)
Definition: indxpath.c:700
static void get_index_paths(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauses, List **bitindexpaths)
Definition: indxpath.c:729
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
struct RestrictInfo * rinfo
Definition: pathnodes.h:1255
List * indexclauses[INDEX_MAX_KEYS]
Definition: indxpath.c:56
List * lappend(List *list, void *datum)
Definition: list.c:321
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
int nkeycolumns
Definition: pathnodes.h:828

◆ get_loop_count()

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

Definition at line 1913 of file indxpath.c.

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().

1914 {
1915  double result;
1916  int outer_relid;
1917 
1918  /* For a non-parameterized path, just return 1.0 quickly */
1919  if (outer_relids == NULL)
1920  return 1.0;
1921 
1922  result = 0.0;
1923  outer_relid = -1;
1924  while ((outer_relid = bms_next_member(outer_relids, outer_relid)) >= 0)
1925  {
1926  RelOptInfo *outer_rel;
1927  double rowcount;
1928 
1929  /* Paranoia: ignore bogus relid indexes */
1930  if (outer_relid >= root->simple_rel_array_size)
1931  continue;
1932  outer_rel = root->simple_rel_array[outer_relid];
1933  if (outer_rel == NULL)
1934  continue;
1935  Assert(outer_rel->relid == outer_relid); /* sanity check on array */
1936 
1937  /* Other relation could be proven empty, if so ignore */
1938  if (IS_DUMMY_REL(outer_rel))
1939  continue;
1940 
1941  /* Otherwise, rel's rows estimate should be valid by now */
1942  Assert(outer_rel->rows > 0);
1943 
1944  /* Check to see if rel is on the inside of any semijoins */
1945  rowcount = adjust_rowcount_for_semijoins(root,
1946  cur_relid,
1947  outer_relid,
1948  outer_rel->rows);
1949 
1950  /* Remember smallest row count estimate among the outer rels */
1951  if (result == 0.0 || result > rowcount)
1952  result = rowcount;
1953  }
1954  /* Return 1.0 if we found no valid relations (shouldn't happen) */
1955  return (result > 0.0) ? result : 1.0;
1956 }
static double adjust_rowcount_for_semijoins(PlannerInfo *root, Index cur_relid, Index outer_relid, double rowcount)
Definition: indxpath.c:1966
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1420
int simple_rel_array_size
Definition: pathnodes.h:202
Index relid
Definition: pathnodes.h:692
double rows
Definition: pathnodes.h:667
#define Assert(condition)
Definition: c.h:746

◆ indexcol_is_bool_constant_for_query()

bool indexcol_is_bool_constant_for_query ( IndexOptInfo index,
int  indexcol 
)

Definition at line 3670 of file indxpath.c.

References RelOptInfo::baserestrictinfo, lfirst, match_boolean_index_clause(), IndexOptInfo::opfamily, RestrictInfo::pseudoconstant, and IndexOptInfo::rel.

Referenced by build_index_pathkeys().

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

◆ is_pseudo_constant_for_index()

bool is_pseudo_constant_for_index ( Node expr,
IndexOptInfo index 
)

Definition at line 3804 of file indxpath.c.

References bms_is_member(), contain_volatile_functions(), pull_varnos(), IndexOptInfo::rel, and RelOptInfo::relid.

3805 {
3806  /* pull_varnos is cheaper than volatility check, so do that first */
3807  if (bms_is_member(index->rel->relid, pull_varnos(expr)))
3808  return false; /* no good, contains Var of table */
3809  if (contain_volatile_functions(expr))
3810  return false; /* no good, volatile comparison value */
3811  return true;
3812 }
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
RelOptInfo * rel
Definition: pathnodes.h:819
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: pathnodes.h:692
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ match_boolean_index_clause()

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

Definition at line 2371 of file indxpath.c.

References arg, BooleanTest::arg, BooleanTest::booltesttype, RestrictInfo::clause, get_notclausearg(), 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().

2374 {
2375  Node *clause = (Node *) rinfo->clause;
2376  Expr *op = NULL;
2377 
2378  /* Direct match? */
2379  if (match_index_to_operand(clause, indexcol, index))
2380  {
2381  /* convert to indexkey = TRUE */
2382  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2383  (Expr *) clause,
2384  (Expr *) makeBoolConst(true, false),
2386  }
2387  /* NOT clause? */
2388  else if (is_notclause(clause))
2389  {
2390  Node *arg = (Node *) get_notclausearg((Expr *) clause);
2391 
2392  if (match_index_to_operand(arg, indexcol, index))
2393  {
2394  /* convert to indexkey = FALSE */
2395  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2396  (Expr *) arg,
2397  (Expr *) makeBoolConst(false, false),
2399  }
2400  }
2401 
2402  /*
2403  * Since we only consider clauses at top level of WHERE, we can convert
2404  * indexkey IS TRUE and indexkey IS FALSE to index searches as well. The
2405  * different meaning for NULL isn't important.
2406  */
2407  else if (clause && IsA(clause, BooleanTest))
2408  {
2409  BooleanTest *btest = (BooleanTest *) clause;
2410  Node *arg = (Node *) btest->arg;
2411 
2412  if (btest->booltesttype == IS_TRUE &&
2413  match_index_to_operand(arg, indexcol, index))
2414  {
2415  /* convert to indexkey = TRUE */
2416  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2417  (Expr *) arg,
2418  (Expr *) makeBoolConst(true, false),
2420  }
2421  else if (btest->booltesttype == IS_FALSE &&
2422  match_index_to_operand(arg, indexcol, index))
2423  {
2424  /* convert to indexkey = FALSE */
2425  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2426  (Expr *) arg,
2427  (Expr *) makeBoolConst(false, false),
2429  }
2430  }
2431 
2432  /*
2433  * If we successfully made an operator clause from the given qual, we must
2434  * wrap it in an IndexClause. It's not lossy.
2435  */
2436  if (op)
2437  {
2438  IndexClause *iclause = makeNode(IndexClause);
2439 
2440  iclause->rinfo = rinfo;
2442  iclause->lossy = false;
2443  iclause->indexcol = indexcol;
2444  iclause->indexcols = NIL;
2445  return iclause;
2446  }
2447 
2448  return NULL;
2449 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3719
Definition: nodes.h:528
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:609
#define list_make1(x1)
Definition: pg_list.h:206
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:357
List * indexcols
Definition: pathnodes.h:1259
AttrNumber indexcol
Definition: pathnodes.h:1258
struct RestrictInfo * rinfo
Definition: pathnodes.h:1255
Expr * arg
Definition: primnodes.h:1245
List * indexquals
Definition: pathnodes.h:1256
Expr * clause
Definition: pathnodes.h:1987
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:115
BoolTestType booltesttype
Definition: primnodes.h:1246
#define InvalidOid
Definition: postgres_ext.h:36
static Expr * get_notclausearg(const void *notclause)
Definition: nodeFuncs.h:124
#define makeNode(_type_)
Definition: nodes.h:576
void * arg
#define make_simple_restrictinfo(clause)
Definition: restrictinfo.h:21

◆ match_clause_to_index()

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

Definition at line 2167 of file indxpath.c.

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

Referenced by match_clauses_to_index(), and match_join_clauses_to_index().

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

◆ match_clause_to_indexcol()

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

Definition at line 2286 of file indxpath.c.

References IndexOptInfo::amsearchnulls, 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, IndexOptInfo::opfamily, and IndexClause::rinfo.

Referenced by match_clause_to_index().

2290 {
2291  IndexClause *iclause;
2292  Expr *clause = rinfo->clause;
2293  Oid opfamily;
2294 
2295  Assert(indexcol < index->nkeycolumns);
2296 
2297  /*
2298  * Historically this code has coped with NULL clauses. That's probably
2299  * not possible anymore, but we might as well continue to cope.
2300  */
2301  if (clause == NULL)
2302  return NULL;
2303 
2304  /* First check for boolean-index cases. */
2305  opfamily = index->opfamily[indexcol];
2306  if (IsBooleanOpfamily(opfamily))
2307  {
2308  iclause = match_boolean_index_clause(rinfo, indexcol, index);
2309  if (iclause)
2310  return iclause;
2311  }
2312 
2313  /*
2314  * Clause must be an opclause, funcclause, ScalarArrayOpExpr, or
2315  * RowCompareExpr. Or, if the index supports it, we can handle IS
2316  * NULL/NOT NULL clauses.
2317  */
2318  if (IsA(clause, OpExpr))
2319  {
2320  return match_opclause_to_indexcol(root, rinfo, indexcol, index);
2321  }
2322  else if (IsA(clause, FuncExpr))
2323  {
2324  return match_funcclause_to_indexcol(root, rinfo, indexcol, index);
2325  }
2326  else if (IsA(clause, ScalarArrayOpExpr))
2327  {
2328  return match_saopclause_to_indexcol(rinfo, indexcol, index);
2329  }
2330  else if (IsA(clause, RowCompareExpr))
2331  {
2332  return match_rowcompare_to_indexcol(rinfo, indexcol, index);
2333  }
2334  else if (index->amsearchnulls && IsA(clause, NullTest))
2335  {
2336  NullTest *nt = (NullTest *) clause;
2337 
2338  if (!nt->argisrow &&
2339  match_index_to_operand((Node *) nt->arg, indexcol, index))
2340  {
2341  iclause = makeNode(IndexClause);
2342  iclause->rinfo = rinfo;
2343  iclause->indexquals = list_make1(rinfo);
2344  iclause->lossy = false;
2345  iclause->indexcol = indexcol;
2346  iclause->indexcols = NIL;
2347  return iclause;
2348  }
2349  }
2350 
2351  return NULL;
2352 }
#define NIL
Definition: pg_list.h:65
static IndexClause * match_boolean_index_clause(RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2371
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3719
Definition: nodes.h:528
static IndexClause * match_funcclause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2576
unsigned int Oid
Definition: postgres_ext.h:31
#define list_make1(x1)
Definition: pg_list.h:206
List * indexcols
Definition: pathnodes.h:1259
Expr * arg
Definition: primnodes.h:1222
AttrNumber indexcol
Definition: pathnodes.h:1258
struct RestrictInfo * rinfo
Definition: pathnodes.h:1255
List * indexquals
Definition: pathnodes.h:1256
static IndexClause * match_opclause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2457
static IndexClause * match_saopclause_to_indexcol(RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2687
Expr * clause
Definition: pathnodes.h:1987
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
Oid * opfamily
Definition: pathnodes.h:832
bool argisrow
Definition: primnodes.h:1224
bool amsearchnulls
Definition: pathnodes.h:862
static IndexClause * match_rowcompare_to_indexcol(RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2754

◆ 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 3194 of file indxpath.c.

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

Referenced by match_pathkeys_to_index().

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

◆ match_clauses_to_index()

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

Definition at line 2134 of file indxpath.c.

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().

2138 {
2139  ListCell *lc;
2140 
2141  foreach(lc, clauses)
2142  {
2143  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
2144 
2145  match_clause_to_index(root, rinfo, index, clauseset);
2146  }
2147 }
static void match_clause_to_index(PlannerInfo *root, RestrictInfo *rinfo, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2167
#define lfirst_node(type, lc)
Definition: pg_list.h:172

◆ match_eclass_clauses_to_index()

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

Definition at line 2096 of file indxpath.c.

References arg, ec_member_matches_indexcol(), generate_implied_equalities_for_column(), RelOptInfo::has_eclass_joins, ec_member_matches_arg::index, ec_member_matches_arg::indexcol, RelOptInfo::lateral_referencers, match_clauses_to_index(), IndexOptInfo::nkeycolumns, and IndexOptInfo::rel.

Referenced by create_index_paths().

2098 {
2099  int indexcol;
2100 
2101  /* No work if rel is not in any such ECs */
2102  if (!index->rel->has_eclass_joins)
2103  return;
2104 
2105  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
2106  {
2108  List *clauses;
2109 
2110  /* Generate clauses, skipping any that join to lateral_referencers */
2111  arg.index = index;
2112  arg.indexcol = indexcol;
2114  index->rel,
2116  (void *) &arg,
2117  index->rel->lateral_referencers);
2118 
2119  /*
2120  * We have to check whether the results actually do match the index,
2121  * since for non-btree indexes the EC's equality operators might not
2122  * be in the index opclass (cf ec_member_matches_indexcol).
2123  */
2124  match_clauses_to_index(root, clauses, index, clauseset);
2125  }
2126 }
bool has_eclass_joins
Definition: pathnodes.h:732
static void match_clauses_to_index(PlannerInfo *root, List *clauses, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2134
static bool ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, EquivalenceMember *em, void *arg)
Definition: indxpath.c:3441
RelOptInfo * rel
Definition: pathnodes.h:819
Relids lateral_referencers
Definition: pathnodes.h:700
IndexOptInfo * index
Definition: indxpath.c:72
int nkeycolumns
Definition: pathnodes.h:828
void * arg
Definition: pg_list.h:50
List * generate_implied_equalities_for_column(PlannerInfo *root, RelOptInfo *rel, ec_matches_callback_type callback, void *callback_arg, Relids prohibited_rels)
Definition: equivclass.c:2605

◆ match_funcclause_to_indexcol()

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

Definition at line 2576 of file indxpath.c.

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

Referenced by match_clause_to_indexcol().

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

◆ match_index_to_operand()

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

Definition at line 3719 of file indxpath.c.

References arg, elog, equal(), ERROR, i, IndexOptInfo::indexkeys, IndexOptInfo::indexprs, IsA, lfirst, list_head(), lnext(), IndexOptInfo::rel, and RelOptInfo::relid.

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().

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

◆ 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 2066 of file indxpath.c.

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

Referenced by create_index_paths().

2070 {
2071  ListCell *lc;
2072 
2073  /* Scan the rel's join clauses */
2074  foreach(lc, rel->joininfo)
2075  {
2076  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2077 
2078  /* Check if clause can be moved to this rel */
2079  if (!join_clause_is_movable_to(rinfo, rel))
2080  continue;
2081 
2082  /* Potentially usable, so see if it matches the index or is an OR */
2083  if (restriction_is_or_clause(rinfo))
2084  *joinorclauses = lappend(*joinorclauses, rinfo);
2085  else
2086  match_clause_to_index(root, rinfo, index, clauseset);
2087  }
2088 }
static void match_clause_to_index(PlannerInfo *root, RestrictInfo *rinfo, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2167
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:361
List * joininfo
Definition: pathnodes.h:730
List * lappend(List *list, void *datum)
Definition: list.c:321
#define lfirst(lc)
Definition: pg_list.h:169
bool join_clause_is_movable_to(RestrictInfo *rinfo, RelOptInfo *baserel)
Definition: restrictinfo.c:504

◆ match_opclause_to_indexcol()

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

Definition at line 2457 of file indxpath.c.

References OpExpr::args, bms_is_member(), RestrictInfo::clause, commute_restrictinfo(), contain_volatile_functions(), get_commutator(), get_index_clause_from_support(), IndexClause::indexcol, IndexOptInfo::indexcollations, 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(), IndexOptInfo::opfamily, OpExpr::opfuncid, OpExpr::opno, IndexOptInfo::rel, RelOptInfo::relid, RestrictInfo::right_relids, IndexClause::rinfo, and set_opfuncid().

Referenced by match_clause_to_indexcol().

2461 {
2462  IndexClause *iclause;
2463  OpExpr *clause = (OpExpr *) rinfo->clause;
2464  Node *leftop,
2465  *rightop;
2466  Oid expr_op;
2467  Oid expr_coll;
2468  Index index_relid;
2469  Oid opfamily;
2470  Oid idxcollation;
2471 
2472  /*
2473  * Only binary operators need apply. (In theory, a planner support
2474  * function could do something with a unary operator, but it seems
2475  * unlikely to be worth the cycles to check.)
2476  */
2477  if (list_length(clause->args) != 2)
2478  return NULL;
2479 
2480  leftop = (Node *) linitial(clause->args);
2481  rightop = (Node *) lsecond(clause->args);
2482  expr_op = clause->opno;
2483  expr_coll = clause->inputcollid;
2484 
2485  index_relid = index->rel->relid;
2486  opfamily = index->opfamily[indexcol];
2487  idxcollation = index->indexcollations[indexcol];
2488 
2489  /*
2490  * Check for clauses of the form: (indexkey operator constant) or
2491  * (constant operator indexkey). See match_clause_to_indexcol's notes
2492  * about const-ness.
2493  *
2494  * Note that we don't ask the support function about clauses that don't
2495  * have one of these forms. Again, in principle it might be possible to
2496  * do something, but it seems unlikely to be worth the cycles to check.
2497  */
2498  if (match_index_to_operand(leftop, indexcol, index) &&
2499  !bms_is_member(index_relid, rinfo->right_relids) &&
2500  !contain_volatile_functions(rightop))
2501  {
2502  if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2503  op_in_opfamily(expr_op, opfamily))
2504  {
2505  iclause = makeNode(IndexClause);
2506  iclause->rinfo = rinfo;
2507  iclause->indexquals = list_make1(rinfo);
2508  iclause->lossy = false;
2509  iclause->indexcol = indexcol;
2510  iclause->indexcols = NIL;
2511  return iclause;
2512  }
2513 
2514  /*
2515  * If we didn't find a member of the index's opfamily, try the support
2516  * function for the operator's underlying function.
2517  */
2518  set_opfuncid(clause); /* make sure we have opfuncid */
2519  return get_index_clause_from_support(root,
2520  rinfo,
2521  clause->opfuncid,
2522  0, /* indexarg on left */
2523  indexcol,
2524  index);
2525  }
2526 
2527  if (match_index_to_operand(rightop, indexcol, index) &&
2528  !bms_is_member(index_relid, rinfo->left_relids) &&
2529  !contain_volatile_functions(leftop))
2530  {
2531  if (IndexCollMatchesExprColl(idxcollation, expr_coll))
2532  {
2533  Oid comm_op = get_commutator(expr_op);
2534 
2535  if (OidIsValid(comm_op) &&
2536  op_in_opfamily(comm_op, opfamily))
2537  {
2538  RestrictInfo *commrinfo;
2539 
2540  /* Build a commuted OpExpr and RestrictInfo */
2541  commrinfo = commute_restrictinfo(rinfo, comm_op);
2542 
2543  /* Make an IndexClause showing that as a derived qual */
2544  iclause = makeNode(IndexClause);
2545  iclause->rinfo = rinfo;
2546  iclause->indexquals = list_make1(commrinfo);
2547  iclause->lossy = false;
2548  iclause->indexcol = indexcol;
2549  iclause->indexcols = NIL;
2550  return iclause;
2551  }
2552  }
2553 
2554  /*
2555  * If we didn't find a member of the index's opfamily, try the support
2556  * function for the operator's underlying function.
2557  */
2558  set_opfuncid(clause); /* make sure we have opfuncid */
2559  return get_index_clause_from_support(root,
2560  rinfo,
2561  clause->opfuncid,
2562  1, /* indexarg on right */
2563  indexcol,
2564  index);
2565  }
2566 
2567  return NULL;
2568 }
#define NIL
Definition: pg_list.h:65
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:64
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1421
Oid * indexcollations
Definition: pathnodes.h:831
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3719
Definition: nodes.h:528
static IndexClause * get_index_clause_from_support(PlannerInfo *root, RestrictInfo *rinfo, Oid funcid, int indexarg, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2622
Relids left_relids
Definition: pathnodes.h:2014
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define lsecond(l)
Definition: pg_list.h:179
#define list_make1(x1)
Definition: pg_list.h:206
RelOptInfo * rel
Definition: pathnodes.h:819
#define linitial(l)
Definition: pg_list.h:174
List * indexcols
Definition: pathnodes.h:1259
AttrNumber indexcol
Definition: pathnodes.h:1258
RestrictInfo * commute_restrictinfo(RestrictInfo *rinfo, Oid comm_op)
Definition: restrictinfo.c:306
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:40
struct RestrictInfo * rinfo
Definition: pathnodes.h:1255
List * indexquals
Definition: pathnodes.h:1256
Index relid
Definition: pathnodes.h:692
Expr * clause
Definition: pathnodes.h:1987
Oid opfuncid
Definition: primnodes.h:517
unsigned int Index
Definition: c.h:483
#define makeNode(_type_)
Definition: nodes.h:576
Relids right_relids
Definition: pathnodes.h:2015
static int list_length(const List *l)
Definition: pg_list.h:149
Oid inputcollid
Definition: primnodes.h:521
Oid * opfamily
Definition: pathnodes.h:832
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1667
Oid opno
Definition: primnodes.h:516
List * args
Definition: primnodes.h:522
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ 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 3079 of file indxpath.c.

References IndexOptInfo::amcanorderbyop, 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(), member, NIL, IndexOptInfo::nkeycolumns, PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, IndexOptInfo::rel, and RelOptInfo::relids.

Referenced by build_index_paths().

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

◆ match_restriction_clauses_to_index()

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

Definition at line 2051 of file indxpath.c.

References IndexOptInfo::indrestrictinfo, and match_clauses_to_index().

Referenced by create_index_paths().

2054 {
2055  /* We can ignore clauses that are implied by the index predicate */
2056  match_clauses_to_index(root, index->indrestrictinfo, index, clauseset);
2057 }
static void match_clauses_to_index(PlannerInfo *root, List *clauses, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2134
List * indrestrictinfo
Definition: pathnodes.h:847

◆ match_rowcompare_to_indexcol()

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

Definition at line 2754 of file indxpath.c.

References bms_is_member(), BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, RestrictInfo::clause, contain_volatile_functions(), expand_indexqual_rowcompare(), get_commutator(), get_op_opfamily_strategy(), IndexOptInfo::indexcollations, IndexCollMatchesExprColl, RowCompareExpr::inputcollids, InvalidOid, RowCompareExpr::largs, linitial, linitial_oid, match_index_to_operand(), IndexOptInfo::opfamily, RowCompareExpr::opnos, pull_varnos(), RowCompareExpr::rargs, IndexOptInfo::rel, IndexOptInfo::relam, and RelOptInfo::relid.

Referenced by match_clause_to_indexcol().

2757 {
2758  RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
2759  Index index_relid;
2760  Oid opfamily;
2761  Oid idxcollation;
2762  Node *leftop,
2763  *rightop;
2764  bool var_on_left;
2765  Oid expr_op;
2766  Oid expr_coll;
2767 
2768  /* Forget it if we're not dealing with a btree index */
2769  if (index->relam != BTREE_AM_OID)
2770  return NULL;
2771 
2772  index_relid = index->rel->relid;
2773  opfamily = index->opfamily[indexcol];
2774  idxcollation = index->indexcollations[indexcol];
2775 
2776  /*
2777  * We could do the matching on the basis of insisting that the opfamily
2778  * shown in the RowCompareExpr be the same as the index column's opfamily,
2779  * but that could fail in the presence of reverse-sort opfamilies: it'd be
2780  * a matter of chance whether RowCompareExpr had picked the forward or
2781  * reverse-sort family. So look only at the operator, and match if it is
2782  * a member of the index's opfamily (after commutation, if the indexkey is
2783  * on the right). We'll worry later about whether any additional
2784  * operators are matchable to the index.
2785  */
2786  leftop = (Node *) linitial(clause->largs);
2787  rightop = (Node *) linitial(clause->rargs);
2788  expr_op = linitial_oid(clause->opnos);
2789  expr_coll = linitial_oid(clause->inputcollids);
2790 
2791  /* Collations must match, if relevant */
2792  if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
2793  return NULL;
2794 
2795  /*
2796  * These syntactic tests are the same as in match_opclause_to_indexcol()
2797  */
2798  if (match_index_to_operand(leftop, indexcol, index) &&
2799  !bms_is_member(index_relid, pull_varnos(rightop)) &&
2800  !contain_volatile_functions(rightop))
2801  {
2802  /* OK, indexkey is on left */
2803  var_on_left = true;
2804  }
2805  else if (match_index_to_operand(rightop, indexcol, index) &&
2806  !bms_is_member(index_relid, pull_varnos(leftop)) &&
2807  !contain_volatile_functions(leftop))
2808  {
2809  /* indexkey is on right, so commute the operator */
2810  expr_op = get_commutator(expr_op);
2811  if (expr_op == InvalidOid)
2812  return NULL;
2813  var_on_left = false;
2814  }
2815  else
2816  return NULL;
2817 
2818  /* We're good if the operator is the right type of opfamily member */
2819  switch (get_op_opfamily_strategy(expr_op, opfamily))
2820  {
2821  case BTLessStrategyNumber:
2825  return expand_indexqual_rowcompare(rinfo,
2826  indexcol,
2827  index,
2828  expr_op,
2829  var_on_left);
2830  }
2831 
2832  return NULL;
2833 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1421
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
Oid * indexcollations
Definition: pathnodes.h:831
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3719
Definition: nodes.h:528
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
unsigned int Oid
Definition: postgres_ext.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
RelOptInfo * rel
Definition: pathnodes.h:819
#define linitial(l)
Definition: pg_list.h:174
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:40
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: pathnodes.h:692
Expr * clause
Definition: pathnodes.h:1987
static IndexClause * expand_indexqual_rowcompare(RestrictInfo *rinfo, int indexcol, IndexOptInfo *index, Oid expr_op, bool var_on_left)
Definition: indxpath.c:2859
unsigned int Index
Definition: c.h:483
#define InvalidOid
Definition: postgres_ext.h:36
#define linitial_oid(l)
Definition: pg_list.h:176
Oid * opfamily
Definition: pathnodes.h:832
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Definition: lsyscache.c:81
List * inputcollids
Definition: primnodes.h:1076
#define BTLessStrategyNumber
Definition: stratnum.h:29
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32

◆ match_saopclause_to_indexcol()

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

Definition at line 2687 of file indxpath.c.

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

Referenced by match_clause_to_indexcol().

2690 {
2691  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
2692  Node *leftop,
2693  *rightop;
2694  Relids right_relids;
2695  Oid expr_op;
2696  Oid expr_coll;
2697  Index index_relid;
2698  Oid opfamily;
2699  Oid idxcollation;
2700 
2701  /* We only accept ANY clauses, not ALL */
2702  if (!saop->useOr)
2703  return NULL;
2704  leftop = (Node *) linitial(saop->args);
2705  rightop = (Node *) lsecond(saop->args);
2706  right_relids = pull_varnos(rightop);
2707  expr_op = saop->opno;
2708  expr_coll = saop->inputcollid;
2709 
2710  index_relid = index->rel->relid;
2711  opfamily = index->opfamily[indexcol];
2712  idxcollation = index->indexcollations[indexcol];
2713 
2714  /*
2715  * We must have indexkey on the left and a pseudo-constant array argument.
2716  */
2717  if (match_index_to_operand(leftop, indexcol, index) &&
2718  !bms_is_member(index_relid, right_relids) &&
2719  !contain_volatile_functions(rightop))
2720  {
2721  if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2722  op_in_opfamily(expr_op, opfamily))
2723  {
2724  IndexClause *iclause = makeNode(IndexClause);
2725 
2726  iclause->rinfo = rinfo;
2727  iclause->indexquals = list_make1(rinfo);
2728  iclause->lossy = false;
2729  iclause->indexcol = indexcol;
2730  iclause->indexcols = NIL;
2731  return iclause;
2732  }
2733 
2734  /*
2735  * We do not currently ask support functions about ScalarArrayOpExprs,
2736  * though in principle we could.
2737  */
2738  }
2739 
2740  return NULL;
2741 }
#define NIL
Definition: pg_list.h:65
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:64
Oid * indexcollations
Definition: pathnodes.h:831
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3719
Definition: nodes.h:528
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
unsigned int Oid
Definition: postgres_ext.h:31
#define lsecond(l)
Definition: pg_list.h:179
#define list_make1(x1)
Definition: pg_list.h:206
RelOptInfo * rel
Definition: pathnodes.h:819
#define linitial(l)
Definition: pg_list.h:174
List * indexcols
Definition: pathnodes.h:1259
AttrNumber indexcol
Definition: pathnodes.h:1258
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:40
struct RestrictInfo * rinfo
Definition: pathnodes.h:1255
List * indexquals
Definition: pathnodes.h:1256
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: pathnodes.h:692
Expr * clause
Definition: pathnodes.h:1987
unsigned int Index
Definition: c.h:483
#define makeNode(_type_)
Definition: nodes.h:576
Oid * opfamily
Definition: pathnodes.h:832
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ path_usage_comparator()

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

Definition at line 1564 of file indxpath.c.

References cost_bitmap_tree_node(), and PathClauseUsage::path.

Referenced by choose_bitmap_and().

1565 {
1566  PathClauseUsage *pa = *(PathClauseUsage *const *) a;
1567  PathClauseUsage *pb = *(PathClauseUsage *const *) b;
1568  Cost acost;
1569  Cost bcost;
1570  Selectivity aselec;
1571  Selectivity bselec;
1572 
1573  cost_bitmap_tree_node(pa->path, &acost, &aselec);
1574  cost_bitmap_tree_node(pb->path, &bcost, &bselec);
1575 
1576  /*
1577  * If costs are the same, sort by selectivity.
1578  */
1579  if (acost < bcost)
1580  return -1;
1581  if (acost > bcost)
1582  return 1;
1583 
1584  if (aselec < bselec)
1585  return -1;
1586  if (aselec > bselec)
1587  return 1;
1588 
1589  return 0;
1590 }
Path * path
Definition: indxpath.c:62
double Selectivity
Definition: nodes.h:661
double Cost
Definition: nodes.h:662
void cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec)
Definition: costsize.c:1053

◆ relation_has_unique_index_for()

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

Definition at line 3499 of file indxpath.c.

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

Referenced by create_unique_path(), and rel_is_distinct_for().

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