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 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 bool IsBooleanOpfamily (Oid opfamily)
 
static IndexClausematch_boolean_index_clause (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClausematch_opclause_to_indexcol (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClausematch_funcclause_to_indexcol (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClauseget_index_clause_from_support (PlannerInfo *root, RestrictInfo *rinfo, Oid funcid, int indexarg, int indexcol, IndexOptInfo *index)
 
static IndexClausematch_saopclause_to_indexcol (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClausematch_rowcompare_to_indexcol (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
 
static IndexClauseexpand_indexqual_rowcompare (PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index, Oid expr_op, bool var_on_left)
 
static void match_pathkeys_to_index (IndexOptInfo *index, List *pathkeys, List **orderby_clauses_p, List **clause_columns_p)
 
static Exprmatch_clause_to_ordering_op (IndexOptInfo *index, int indexcol, Expr *clause, Oid pk_opfamily)
 
static bool ec_member_matches_indexcol (PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, EquivalenceMember *em, void *arg)
 
void create_index_paths (PlannerInfo *root, RelOptInfo *rel)
 
void check_index_predicates (PlannerInfo *root, RelOptInfo *rel)
 
bool relation_has_unique_index_for (PlannerInfo *root, RelOptInfo *rel, List *restrictlist, List *exprlist, List *oprlist)
 
bool indexcol_is_bool_constant_for_query (PlannerInfo *root, IndexOptInfo *index, int indexcol)
 
bool match_index_to_operand (Node *operand, int indexcol, IndexOptInfo *index)
 
bool is_pseudo_constant_for_index (PlannerInfo *root, Node *expr, IndexOptInfo *index)
 

Macro Definition Documentation

◆ IndexCollMatchesExprColl

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

Definition at line 40 of file indxpath.c.

Enumeration Type Documentation

◆ ScanTypeControl

Enumerator
ST_INDEXSCAN 
ST_BITMAPSCAN 
ST_ANYSCAN 

Definition at line 44 of file indxpath.c.

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

Function Documentation

◆ adjust_rowcount_for_semijoins()

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

Definition at line 1937 of file indxpath.c.

1941 {
1942  ListCell *lc;
1943 
1944  foreach(lc, root->join_info_list)
1945  {
1946  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
1947 
1948  if (sjinfo->jointype == JOIN_SEMI &&
1949  bms_is_member(cur_relid, sjinfo->syn_lefthand) &&
1950  bms_is_member(outer_relid, sjinfo->syn_righthand))
1951  {
1952  /* Estimate number of unique-ified rows */
1953  double nraw;
1954  double nunique;
1955 
1956  nraw = approximate_joinrel_size(root, sjinfo->syn_righthand);
1957  nunique = estimate_num_groups(root,
1958  sjinfo->semi_rhs_exprs,
1959  nraw,
1960  NULL,
1961  NULL);
1962  if (rowcount > nunique)
1963  rowcount = nunique;
1964  }
1965  }
1966  return rowcount;
1967 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:428
static double approximate_joinrel_size(PlannerInfo *root, Relids relids)
Definition: indxpath.c:1981
@ JOIN_SEMI
Definition: nodes.h:307
#define lfirst(lc)
Definition: pg_list.h:170
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, EstimationInfo *estinfo)
Definition: selfuncs.c:3385
List * join_info_list
Definition: pathnodes.h:333
Relids syn_lefthand
Definition: pathnodes.h:2705
List * semi_rhs_exprs
Definition: pathnodes.h:2714
JoinType jointype
Definition: pathnodes.h:2707
Relids syn_righthand
Definition: pathnodes.h:2706

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

Referenced by get_loop_count().

◆ approximate_joinrel_size()

static double approximate_joinrel_size ( PlannerInfo root,
Relids  relids 
)
static

Definition at line 1981 of file indxpath.c.

1982 {
1983  double rowcount = 1.0;
1984  int relid;
1985 
1986  relid = -1;
1987  while ((relid = bms_next_member(relids, relid)) >= 0)
1988  {
1989  RelOptInfo *rel;
1990 
1991  /* Paranoia: ignore bogus relid indexes */
1992  if (relid >= root->simple_rel_array_size)
1993  continue;
1994  rel = root->simple_rel_array[relid];
1995  if (rel == NULL)
1996  continue;
1997  Assert(rel->relid == relid); /* sanity check on array */
1998 
1999  /* Relation could be proven empty, if so ignore */
2000  if (IS_DUMMY_REL(rel))
2001  continue;
2002 
2003  /* Otherwise, rel's rows estimate should be valid by now */
2004  Assert(rel->rows > 0);
2005 
2006  /* Accumulate product */
2007  rowcount *= rel->rows;
2008  }
2009  return rowcount;
2010 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1047
Assert(fmt[strlen(fmt) - 1] !='\n')
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1823
int simple_rel_array_size
Definition: pathnodes.h:232
Index relid
Definition: pathnodes.h:871
Cardinality rows
Definition: pathnodes.h:830

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

Referenced by adjust_rowcount_for_semijoins().

◆ bitmap_and_cost_est()

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

Definition at line 1615 of file indxpath.c.

1616 {
1617  BitmapAndPath *apath;
1618 
1619  /*
1620  * Might as well build a real BitmapAndPath here, as the work is slightly
1621  * too complicated to be worth repeating just to save one palloc.
1622  */
1623  apath = create_bitmap_and_path(root, rel, paths);
1624 
1625  return bitmap_scan_cost_est(root, rel, (Path *) apath);
1626 }
static Cost bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel, Path *ipath)
Definition: indxpath.c:1581
BitmapAndPath * create_bitmap_and_path(PlannerInfo *root, RelOptInfo *rel, List *bitmapquals)
Definition: pathnode.c:1079

References bitmap_scan_cost_est(), and create_bitmap_and_path().

Referenced by choose_bitmap_and().

◆ bitmap_scan_cost_est()

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

Definition at line 1581 of file indxpath.c.

1582 {
1583  BitmapHeapPath bpath;
1584 
1585  /* Set up a dummy BitmapHeapPath */
1586  bpath.path.type = T_BitmapHeapPath;
1587  bpath.path.pathtype = T_BitmapHeapScan;
1588  bpath.path.parent = rel;
1589  bpath.path.pathtarget = rel->reltarget;
1590  bpath.path.param_info = ipath->param_info;
1591  bpath.path.pathkeys = NIL;
1592  bpath.bitmapqual = ipath;
1593 
1594  /*
1595  * Check the cost of temporary path without considering parallelism.
1596  * Parallel bitmap heap path will be considered at later stage.
1597  */
1598  bpath.path.parallel_workers = 0;
1599 
1600  /* Now we can do cost_bitmap_heap_scan */
1601  cost_bitmap_heap_scan(&bpath.path, root, rel,
1602  bpath.path.param_info,
1603  ipath,
1604  get_loop_count(root, rel->relid,
1605  PATH_REQ_OUTER(ipath)));
1606 
1607  return bpath.path.total_cost;
1608 }
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:983
static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
Definition: indxpath.c:1884
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1556
#define NIL
Definition: pg_list.h:66
Path * bitmapqual
Definition: pathnodes.h:1675
List * pathkeys
Definition: pathnodes.h:1552
NodeTag pathtype
Definition: pathnodes.h:1513
int parallel_workers
Definition: pathnodes.h:1544
Cost total_cost
Definition: pathnodes.h:1549
struct PathTarget * reltarget
Definition: pathnodes.h:846

References BitmapHeapPath::bitmapqual, cost_bitmap_heap_scan(), get_loop_count(), NIL, Path::parallel_workers, BitmapHeapPath::path, PATH_REQ_OUTER, Path::pathkeys, Path::pathtype, RelOptInfo::relid, RelOptInfo::reltarget, and Path::total_cost.

Referenced by bitmap_and_cost_est(), and choose_bitmap_and().

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

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

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

Referenced by build_paths_for_OR(), and get_index_paths().

◆ build_paths_for_OR()

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

Definition at line 1140 of file indxpath.c.

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

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

Referenced by generate_bitmap_or_paths().

◆ check_index_only()

static bool check_index_only ( RelOptInfo rel,
IndexOptInfo index 
)
static

Definition at line 1785 of file indxpath.c.

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

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

Referenced by build_index_paths().

◆ check_index_predicates()

void check_index_predicates ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 3302 of file indxpath.c.

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

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

Referenced by set_plain_rel_size(), and set_tablesample_rel_size().

◆ choose_bitmap_and()

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

Definition at line 1342 of file indxpath.c.

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

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

Referenced by create_index_paths(), and generate_bitmap_or_paths().

◆ classify_index_clause_usage()

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

Definition at line 1644 of file indxpath.c.

1645 {
1646  PathClauseUsage *result;
1647  Bitmapset *clauseids;
1648  ListCell *lc;
1649 
1650  result = (PathClauseUsage *) palloc(sizeof(PathClauseUsage));
1651  result->path = path;
1652 
1653  /* Recursively find the quals and preds used by the path */
1654  result->quals = NIL;
1655  result->preds = NIL;
1656  find_indexpath_quals(path, &result->quals, &result->preds);
1657 
1658  /*
1659  * Some machine-generated queries have outlandish numbers of qual clauses.
1660  * To avoid getting into O(N^2) behavior even in this preliminary
1661  * classification step, we want to limit the number of entries we can
1662  * accumulate in *clauselist. Treat any path with more than 100 quals +
1663  * preds as unclassifiable, which will cause calling code to consider it
1664  * distinct from all other paths.
1665  */
1666  if (list_length(result->quals) + list_length(result->preds) > 100)
1667  {
1668  result->clauseids = NULL;
1669  result->unclassifiable = true;
1670  return result;
1671  }
1672 
1673  /* Build up a bitmapset representing the quals and preds */
1674  clauseids = NULL;
1675  foreach(lc, result->quals)
1676  {
1677  Node *node = (Node *) lfirst(lc);
1678 
1679  clauseids = bms_add_member(clauseids,
1680  find_list_position(node, clauselist));
1681  }
1682  foreach(lc, result->preds)
1683  {
1684  Node *node = (Node *) lfirst(lc);
1685 
1686  clauseids = bms_add_member(clauseids,
1687  find_list_position(node, clauselist));
1688  }
1689  result->clauseids = clauseids;
1690  result->unclassifiable = false;
1691 
1692  return result;
1693 }
static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
Definition: indxpath.c:1712
static int find_list_position(Node *node, List **nodelist)
Definition: indxpath.c:1759

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

Referenced by choose_bitmap_and().

◆ consider_index_join_clauses()

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

Definition at line 432 of file indxpath.c.

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

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

Referenced by create_index_paths().

◆ consider_index_join_outer_rels()

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

Definition at line 498 of file indxpath.c.

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

References BMS_DIFFERENT, bms_subset_compare(), bms_union(), eclass_already_used(), get_join_index_paths(), lfirst, list_length(), list_member(), list_nth(), and IndexClause::rinfo.

Referenced by consider_index_join_clauses().

◆ create_index_paths()

void create_index_paths ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 235 of file indxpath.c.

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

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

Referenced by set_plain_rel_pathlist().

◆ ec_member_matches_indexcol()

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

Definition at line 3436 of file indxpath.c.

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

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

Referenced by match_eclass_clauses_to_index().

◆ eclass_already_used()

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

Definition at line 679 of file indxpath.c.

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

References bms_is_subset(), lfirst, and IndexClause::rinfo.

Referenced by consider_index_join_outer_rels().

◆ expand_indexqual_rowcompare()

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

Definition at line 2853 of file indxpath.c.

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

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

Referenced by match_rowcompare_to_indexcol().

◆ find_indexpath_quals()

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

Definition at line 1712 of file indxpath.c.

1713 {
1714  if (IsA(bitmapqual, BitmapAndPath))
1715  {
1716  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1717  ListCell *l;
1718 
1719  foreach(l, apath->bitmapquals)
1720  {
1721  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1722  }
1723  }
1724  else if (IsA(bitmapqual, BitmapOrPath))
1725  {
1726  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1727  ListCell *l;
1728 
1729  foreach(l, opath->bitmapquals)
1730  {
1731  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1732  }
1733  }
1734  else if (IsA(bitmapqual, IndexPath))
1735  {
1736  IndexPath *ipath = (IndexPath *) bitmapqual;
1737  ListCell *l;
1738 
1739  foreach(l, ipath->indexclauses)
1740  {
1741  IndexClause *iclause = (IndexClause *) lfirst(l);
1742 
1743  *quals = lappend(*quals, iclause->rinfo->clause);
1744  }
1745  *preds = list_concat(*preds, ipath->indexinfo->indpred);
1746  }
1747  else
1748  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1749 }
#define nodeTag(nodeptr)
Definition: nodes.h:122
List * bitmapquals
Definition: pathnodes.h:1687
List * bitmapquals
Definition: pathnodes.h:1700
List * indpred
Definition: pathnodes.h:1119
List * indexclauses
Definition: pathnodes.h:1601
IndexOptInfo * indexinfo
Definition: pathnodes.h:1600

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

Referenced by classify_index_clause_usage().

◆ find_list_position()

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

Definition at line 1759 of file indxpath.c.

1760 {
1761  int i;
1762  ListCell *lc;
1763 
1764  i = 0;
1765  foreach(lc, *nodelist)
1766  {
1767  Node *oldnode = (Node *) lfirst(lc);
1768 
1769  if (equal(node, oldnode))
1770  return i;
1771  i++;
1772  }
1773 
1774  *nodelist = lappend(*nodelist, node);
1775 
1776  return i;
1777 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:225

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

Referenced by classify_index_clause_usage().

◆ generate_bitmap_or_paths()

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

Definition at line 1235 of file indxpath.c.

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

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

Referenced by create_index_paths().

◆ get_index_clause_from_support()

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

Definition at line 2612 of file indxpath.c.

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

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

Referenced by match_funcclause_to_indexcol(), and match_opclause_to_indexcol().

◆ get_index_paths()

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

Definition at line 713 of file indxpath.c.

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

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

Referenced by create_index_paths(), and get_join_index_paths().

◆ get_join_index_paths()

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

Definition at line 601 of file indxpath.c.

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

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

Referenced by consider_index_join_outer_rels().

◆ get_loop_count()

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

Definition at line 1884 of file indxpath.c.

1885 {
1886  double result;
1887  int outer_relid;
1888 
1889  /* For a non-parameterized path, just return 1.0 quickly */
1890  if (outer_relids == NULL)
1891  return 1.0;
1892 
1893  result = 0.0;
1894  outer_relid = -1;
1895  while ((outer_relid = bms_next_member(outer_relids, outer_relid)) >= 0)
1896  {
1897  RelOptInfo *outer_rel;
1898  double rowcount;
1899 
1900  /* Paranoia: ignore bogus relid indexes */
1901  if (outer_relid >= root->simple_rel_array_size)
1902  continue;
1903  outer_rel = root->simple_rel_array[outer_relid];
1904  if (outer_rel == NULL)
1905  continue;
1906  Assert(outer_rel->relid == outer_relid); /* sanity check on array */
1907 
1908  /* Other relation could be proven empty, if so ignore */
1909  if (IS_DUMMY_REL(outer_rel))
1910  continue;
1911 
1912  /* Otherwise, rel's rows estimate should be valid by now */
1913  Assert(outer_rel->rows > 0);
1914 
1915  /* Check to see if rel is on the inside of any semijoins */
1916  rowcount = adjust_rowcount_for_semijoins(root,
1917  cur_relid,
1918  outer_relid,
1919  outer_rel->rows);
1920 
1921  /* Remember smallest row count estimate among the outer rels */
1922  if (result == 0.0 || result > rowcount)
1923  result = rowcount;
1924  }
1925  /* Return 1.0 if we found no valid relations (shouldn't happen) */
1926  return (result > 0.0) ? result : 1.0;
1927 }
static double adjust_rowcount_for_semijoins(PlannerInfo *root, Index cur_relid, Index outer_relid, double rowcount)
Definition: indxpath.c:1937

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

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

◆ indexcol_is_bool_constant_for_query()

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

Definition at line 3665 of file indxpath.c.

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

References IsBooleanOpfamily(), lfirst, and match_boolean_index_clause().

Referenced by build_index_pathkeys().

◆ is_pseudo_constant_for_index()

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

Definition at line 3801 of file indxpath.c.

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

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

◆ IsBooleanOpfamily()

static bool IsBooleanOpfamily ( Oid  opfamily)
static

Definition at line 2335 of file indxpath.c.

2336 {
2337  if (opfamily < FirstNormalObjectId)
2338  return IsBuiltinBooleanOpfamily(opfamily);
2339  else
2340  return op_in_opfamily(BooleanEqualOperator, opfamily);
2341 }
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:65
#define FirstNormalObjectId
Definition: transam.h:197

References FirstNormalObjectId, and op_in_opfamily().

Referenced by indexcol_is_bool_constant_for_query(), and match_clause_to_indexcol().

◆ match_boolean_index_clause()

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

Definition at line 2360 of file indxpath.c.

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

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

Referenced by indexcol_is_bool_constant_for_query(), and match_clause_to_indexcol().

◆ match_clause_to_index()

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

Definition at line 2139 of file indxpath.c.

2143 {
2144  int indexcol;
2145 
2146  /*
2147  * Never match pseudoconstants to indexes. (Normally a match could not
2148  * happen anyway, since a pseudoconstant clause couldn't contain a Var,
2149  * but what if someone builds an expression index on a constant? It's not
2150  * totally unreasonable to do so with a partial index, either.)
2151  */
2152  if (rinfo->pseudoconstant)
2153  return;
2154 
2155  /*
2156  * If clause can't be used as an indexqual because it must wait till after
2157  * some lower-security-level restriction clause, reject it.
2158  */
2159  if (!restriction_is_securely_promotable(rinfo, index->rel))
2160  return;
2161 
2162  /* OK, check each index key column for a match */
2163  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
2164  {
2165  IndexClause *iclause;
2166  ListCell *lc;
2167 
2168  /* Ignore duplicates */
2169  foreach(lc, clauseset->indexclauses[indexcol])
2170  {
2171  iclause = (IndexClause *) lfirst(lc);
2172 
2173  if (iclause->rinfo == rinfo)
2174  return;
2175  }
2176 
2177  /* OK, try to match the clause to the index column */
2178  iclause = match_clause_to_indexcol(root,
2179  rinfo,
2180  indexcol,
2181  index);
2182  if (iclause)
2183  {
2184  /* Success, so record it */
2185  clauseset->indexclauses[indexcol] =
2186  lappend(clauseset->indexclauses[indexcol], iclause);
2187  clauseset->nonempty = true;
2188  return;
2189  }
2190  }
2191 }
static IndexClause * match_clause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2258
bool restriction_is_securely_promotable(RestrictInfo *restrictinfo, RelOptInfo *rel)
Definition: restrictinfo.c:399

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

Referenced by match_clauses_to_index(), and match_join_clauses_to_index().

◆ match_clause_to_indexcol()

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

Definition at line 2258 of file indxpath.c.

2262 {
2263  IndexClause *iclause;
2264  Expr *clause = rinfo->clause;
2265  Oid opfamily;
2266 
2267  Assert(indexcol < index->nkeycolumns);
2268 
2269  /*
2270  * Historically this code has coped with NULL clauses. That's probably
2271  * not possible anymore, but we might as well continue to cope.
2272  */
2273  if (clause == NULL)
2274  return NULL;
2275 
2276  /* First check for boolean-index cases. */
2277  opfamily = index->opfamily[indexcol];
2278  if (IsBooleanOpfamily(opfamily))
2279  {
2280  iclause = match_boolean_index_clause(root, rinfo, indexcol, index);
2281  if (iclause)
2282  return iclause;
2283  }
2284 
2285  /*
2286  * Clause must be an opclause, funcclause, ScalarArrayOpExpr, or
2287  * RowCompareExpr. Or, if the index supports it, we can handle IS
2288  * NULL/NOT NULL clauses.
2289  */
2290  if (IsA(clause, OpExpr))
2291  {
2292  return match_opclause_to_indexcol(root, rinfo, indexcol, index);
2293  }
2294  else if (IsA(clause, FuncExpr))
2295  {
2296  return match_funcclause_to_indexcol(root, rinfo, indexcol, index);
2297  }
2298  else if (IsA(clause, ScalarArrayOpExpr))
2299  {
2300  return match_saopclause_to_indexcol(root, rinfo, indexcol, index);
2301  }
2302  else if (IsA(clause, RowCompareExpr))
2303  {
2304  return match_rowcompare_to_indexcol(root, rinfo, indexcol, index);
2305  }
2306  else if (index->amsearchnulls && IsA(clause, NullTest))
2307  {
2308  NullTest *nt = (NullTest *) clause;
2309 
2310  if (!nt->argisrow &&
2311  match_index_to_operand((Node *) nt->arg, indexcol, index))
2312  {
2313  iclause = makeNode(IndexClause);
2314  iclause->rinfo = rinfo;
2315  iclause->indexquals = list_make1(rinfo);
2316  iclause->lossy = false;
2317  iclause->indexcol = indexcol;
2318  iclause->indexcols = NIL;
2319  return iclause;
2320  }
2321  }
2322 
2323  return NULL;
2324 }
static IndexClause * match_saopclause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2678
static IndexClause * match_opclause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2447
static IndexClause * match_rowcompare_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2746
static IndexClause * match_funcclause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2566
bool argisrow
Definition: primnodes.h:1367
Expr * arg
Definition: primnodes.h:1365

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

Referenced by match_clause_to_index().

◆ match_clause_to_ordering_op()

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

Definition at line 3188 of file indxpath.c.

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

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

Referenced by match_pathkeys_to_index().

◆ match_clauses_to_index()

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

Definition at line 2106 of file indxpath.c.

2110 {
2111  ListCell *lc;
2112 
2113  foreach(lc, clauses)
2114  {
2115  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
2116 
2117  match_clause_to_index(root, rinfo, index, clauseset);
2118  }
2119 }
static void match_clause_to_index(PlannerInfo *root, RestrictInfo *rinfo, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2139

References lfirst_node, and match_clause_to_index().

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

◆ match_eclass_clauses_to_index()

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

Definition at line 2068 of file indxpath.c.

2070 {
2071  int indexcol;
2072 
2073  /* No work if rel is not in any such ECs */
2074  if (!index->rel->has_eclass_joins)
2075  return;
2076 
2077  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
2078  {
2080  List *clauses;
2081 
2082  /* Generate clauses, skipping any that join to lateral_referencers */
2083  arg.index = index;
2084  arg.indexcol = indexcol;
2086  index->rel,
2088  (void *) &arg,
2089  index->rel->lateral_referencers);
2090 
2091  /*
2092  * We have to check whether the results actually do match the index,
2093  * since for non-btree indexes the EC's equality operators might not
2094  * be in the index opclass (cf ec_member_matches_indexcol).
2095  */
2096  match_clauses_to_index(root, clauses, index, clauseset);
2097  }
2098 }
List * generate_implied_equalities_for_column(PlannerInfo *root, RelOptInfo *rel, ec_matches_callback_type callback, void *callback_arg, Relids prohibited_rels)
Definition: equivclass.c:2833
static bool ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, EquivalenceMember *em, void *arg)
Definition: indxpath.c:3436

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

Referenced by create_index_paths().

◆ match_funcclause_to_indexcol()

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

Definition at line 2566 of file indxpath.c.

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

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

Referenced by match_clause_to_indexcol().

◆ match_index_to_operand()

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

Definition at line 3716 of file indxpath.c.

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

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

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

◆ match_join_clauses_to_index()

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

Definition at line 2038 of file indxpath.c.

2042 {
2043  ListCell *lc;
2044 
2045  /* Scan the rel's join clauses */
2046  foreach(lc, rel->joininfo)
2047  {
2048  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2049 
2050  /* Check if clause can be moved to this rel */
2051  if (!join_clause_is_movable_to(rinfo, rel))
2052  continue;
2053 
2054  /* Potentially usable, so see if it matches the index or is an OR */
2055  if (restriction_is_or_clause(rinfo))
2056  *joinorclauses = lappend(*joinorclauses, rinfo);
2057  else
2058  match_clause_to_index(root, rinfo, index, clauseset);
2059  }
2060 }

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

Referenced by create_index_paths().

◆ match_opclause_to_indexcol()

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

Definition at line 2447 of file indxpath.c.

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

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

Referenced by match_clause_to_indexcol().

◆ match_pathkeys_to_index()

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

Definition at line 3073 of file indxpath.c.

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

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

Referenced by build_index_paths().

◆ match_restriction_clauses_to_index()

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

Definition at line 2023 of file indxpath.c.

2026 {
2027  /* We can ignore clauses that are implied by the index predicate */
2028  match_clauses_to_index(root, index->indrestrictinfo, index, clauseset);
2029 }

References match_clauses_to_index().

Referenced by create_index_paths().

◆ match_rowcompare_to_indexcol()

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

Definition at line 2746 of file indxpath.c.

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

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

Referenced by match_clause_to_indexcol().

◆ match_saopclause_to_indexcol()

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

Definition at line 2678 of file indxpath.c.

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

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

Referenced by match_clause_to_indexcol().

◆ path_usage_comparator()

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

Definition at line 1548 of file indxpath.c.

1549 {
1550  PathClauseUsage *pa = *(PathClauseUsage *const *) a;
1551  PathClauseUsage *pb = *(PathClauseUsage *const *) b;
1552  Cost acost;
1553  Cost bcost;
1554  Selectivity aselec;
1555  Selectivity bselec;
1556 
1557  cost_bitmap_tree_node(pa->path, &acost, &aselec);
1558  cost_bitmap_tree_node(pb->path, &bcost, &bselec);
1559 
1560  /*
1561  * If costs are the same, sort by selectivity.
1562  */
1563  if (acost < bcost)
1564  return -1;
1565  if (acost > bcost)
1566  return 1;
1567 
1568  if (aselec < bselec)
1569  return -1;
1570  if (aselec > bselec)
1571  return 1;
1572 
1573  return 0;
1574 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69

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

Referenced by choose_bitmap_and().

◆ relation_has_unique_index_for()

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

Definition at line 3494 of file indxpath.c.

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

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

Referenced by create_unique_path(), and rel_is_distinct_for().