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

1934 {
1935  ListCell *lc;
1936 
1937  foreach(lc, root->join_info_list)
1938  {
1939  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
1940 
1941  if (sjinfo->jointype == JOIN_SEMI &&
1942  bms_is_member(cur_relid, sjinfo->syn_lefthand) &&
1943  bms_is_member(outer_relid, sjinfo->syn_righthand))
1944  {
1945  /* Estimate number of unique-ified rows */
1946  double nraw;
1947  double nunique;
1948 
1949  nraw = approximate_joinrel_size(root, sjinfo->syn_righthand);
1950  nunique = estimate_num_groups(root,
1951  sjinfo->semi_rhs_exprs,
1952  nraw,
1953  NULL,
1954  NULL);
1955  if (rowcount > nunique)
1956  rowcount = nunique;
1957  }
1958  }
1959  return rowcount;
1960 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:444
static double approximate_joinrel_size(PlannerInfo *root, Relids relids)
Definition: indxpath.c:1974
@ JOIN_SEMI
Definition: nodes.h:318
#define lfirst(lc)
Definition: pg_list.h:172
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, EstimationInfo *estinfo)
Definition: selfuncs.c:3386
List * join_info_list
Definition: pathnodes.h:340
Relids syn_lefthand
Definition: pathnodes.h:2831
List * semi_rhs_exprs
Definition: pathnodes.h:2843
JoinType jointype
Definition: pathnodes.h:2833
Relids syn_righthand
Definition: pathnodes.h:2832

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

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

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

1609 {
1610  BitmapAndPath *apath;
1611 
1612  /*
1613  * Might as well build a real BitmapAndPath here, as the work is slightly
1614  * too complicated to be worth repeating just to save one palloc.
1615  */
1616  apath = create_bitmap_and_path(root, rel, paths);
1617 
1618  return bitmap_scan_cost_est(root, rel, (Path *) apath);
1619 }
static Cost bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel, Path *ipath)
Definition: indxpath.c:1574
BitmapAndPath * create_bitmap_and_path(PlannerInfo *root, RelOptInfo *rel, List *bitmapquals)
Definition: pathnode.c:1077

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

1575 {
1576  BitmapHeapPath bpath;
1577 
1578  /* Set up a dummy BitmapHeapPath */
1579  bpath.path.type = T_BitmapHeapPath;
1580  bpath.path.pathtype = T_BitmapHeapScan;
1581  bpath.path.parent = rel;
1582  bpath.path.pathtarget = rel->reltarget;
1583  bpath.path.param_info = ipath->param_info;
1584  bpath.path.pathkeys = NIL;
1585  bpath.bitmapqual = ipath;
1586 
1587  /*
1588  * Check the cost of temporary path without considering parallelism.
1589  * Parallel bitmap heap path will be considered at later stage.
1590  */
1591  bpath.path.parallel_workers = 0;
1592 
1593  /* Now we can do cost_bitmap_heap_scan */
1594  cost_bitmap_heap_scan(&bpath.path, root, rel,
1595  bpath.path.param_info,
1596  ipath,
1597  get_loop_count(root, rel->relid,
1598  PATH_REQ_OUTER(ipath)));
1599 
1600  return bpath.path.total_cost;
1601 }
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:985
static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
Definition: indxpath.c:1877
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1643
#define NIL
Definition: pg_list.h:68
Path * bitmapqual
Definition: pathnodes.h:1759
List * pathkeys
Definition: pathnodes.h:1639
NodeTag pathtype
Definition: pathnodes.h:1600
int parallel_workers
Definition: pathnodes.h:1631
Cost total_cost
Definition: pathnodes.h:1636
struct PathTarget * reltarget
Definition: pathnodes.h:884

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 
953  /* Compute loop_count for cost estimation purposes */
954  loop_count = get_loop_count(root, rel->relid, outer_relids);
955 
956  /*
957  * 2. Compute pathkeys describing index's ordering, if any, then see how
958  * many of them are actually useful for this query. This is not relevant
959  * if we are only trying to build bitmap indexscans, nor if we have to
960  * assume the scan is unordered.
961  */
962  pathkeys_possibly_useful = (scantype != ST_BITMAPSCAN &&
963  !found_lower_saop_clause &&
964  has_useful_pathkeys(root, rel));
965  index_is_ordered = (index->sortopfamily != NULL);
966  if (index_is_ordered && pathkeys_possibly_useful)
967  {
968  index_pathkeys = build_index_pathkeys(root, index,
970  useful_pathkeys = truncate_useless_pathkeys(root, rel,
971  index_pathkeys);
972  orderbyclauses = NIL;
973  orderbyclausecols = NIL;
974  }
975  else if (index->amcanorderbyop && pathkeys_possibly_useful)
976  {
977  /* see if we can generate ordering operators for query_pathkeys */
979  &orderbyclauses,
980  &orderbyclausecols);
981  if (orderbyclauses)
982  useful_pathkeys = root->query_pathkeys;
983  else
984  useful_pathkeys = NIL;
985  }
986  else
987  {
988  useful_pathkeys = NIL;
989  orderbyclauses = NIL;
990  orderbyclausecols = NIL;
991  }
992 
993  /*
994  * 3. Check if an index-only scan is possible. If we're not building
995  * plain indexscans, this isn't relevant since bitmap scans don't support
996  * index data retrieval anyway.
997  */
998  index_only_scan = (scantype != ST_BITMAPSCAN &&
999  check_index_only(rel, index));
1000 
1001  /*
1002  * 4. Generate an indexscan path if there are relevant restriction clauses
1003  * in the current clauses, OR the index ordering is potentially useful for
1004  * later merging or final output ordering, OR the index has a useful
1005  * predicate, OR an index-only scan is possible.
1006  */
1007  if (index_clauses != NIL || useful_pathkeys != NIL || useful_predicate ||
1008  index_only_scan)
1009  {
1010  ipath = create_index_path(root, index,
1011  index_clauses,
1012  orderbyclauses,
1013  orderbyclausecols,
1014  useful_pathkeys,
1016  index_only_scan,
1017  outer_relids,
1018  loop_count,
1019  false);
1020  result = lappend(result, ipath);
1021 
1022  /*
1023  * If appropriate, consider parallel index scan. We don't allow
1024  * parallel index scan for bitmap index scans.
1025  */
1026  if (index->amcanparallel &&
1027  rel->consider_parallel && outer_relids == NULL &&
1028  scantype != ST_BITMAPSCAN)
1029  {
1030  ipath = create_index_path(root, index,
1031  index_clauses,
1032  orderbyclauses,
1033  orderbyclausecols,
1034  useful_pathkeys,
1036  index_only_scan,
1037  outer_relids,
1038  loop_count,
1039  true);
1040 
1041  /*
1042  * if, after costing the path, we find that it's not worth using
1043  * parallel workers, just free it.
1044  */
1045  if (ipath->path.parallel_workers > 0)
1046  add_partial_path(rel, (Path *) ipath);
1047  else
1048  pfree(ipath);
1049  }
1050  }
1051 
1052  /*
1053  * 5. If the index is ordered, a backwards scan might be interesting.
1054  */
1055  if (index_is_ordered && pathkeys_possibly_useful)
1056  {
1057  index_pathkeys = build_index_pathkeys(root, index,
1059  useful_pathkeys = truncate_useless_pathkeys(root, rel,
1060  index_pathkeys);
1061  if (useful_pathkeys != NIL)
1062  {
1063  ipath = create_index_path(root, index,
1064  index_clauses,
1065  NIL,
1066  NIL,
1067  useful_pathkeys,
1069  index_only_scan,
1070  outer_relids,
1071  loop_count,
1072  false);
1073  result = lappend(result, ipath);
1074 
1075  /* If appropriate, consider parallel index scan */
1076  if (index->amcanparallel &&
1077  rel->consider_parallel && outer_relids == NULL &&
1078  scantype != ST_BITMAPSCAN)
1079  {
1080  ipath = create_index_path(root, index,
1081  index_clauses,
1082  NIL,
1083  NIL,
1084  useful_pathkeys,
1086  index_only_scan,
1087  outer_relids,
1088  loop_count,
1089  true);
1090 
1091  /*
1092  * if, after costing the path, we find that it's not worth
1093  * using parallel workers, just free it.
1094  */
1095  if (ipath->path.parallel_workers > 0)
1096  add_partial_path(rel, (Path *) ipath);
1097  else
1098  pfree(ipath);
1099  }
1100  }
1101  }
1102 
1103  return result;
1104 }
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:818
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:792
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
static bool check_index_only(RelOptInfo *rel, IndexOptInfo *index)
Definition: indxpath.c:1778
static void match_pathkeys_to_index(IndexOptInfo *index, List *pathkeys, List **orderby_clauses_p, List **clause_columns_p)
Definition: indxpath.c:3066
List * lappend(List *list, void *datum)
Definition: list.c:338
void pfree(void *pointer)
Definition: mcxt.c:1436
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:1983
List * truncate_useless_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
Definition: pathkeys.c:1943
List * build_index_pathkeys(PlannerInfo *root, IndexOptInfo *index, ScanDirection scandir)
Definition: pathkeys.c:539
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:995
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:749
@ BackwardScanDirection
Definition: sdir.h:26
@ ForwardScanDirection
Definition: sdir.h:28
List * indexclauses[INDEX_MAX_KEYS]
Definition: indxpath.c:56
struct RestrictInfo * rinfo
Definition: pathnodes.h:1732
Path path
Definition: pathnodes.h:1683
Definition: pg_list.h:54
List * query_pathkeys
Definition: pathnodes.h:385
bool consider_parallel
Definition: pathnodes.h:878
Relids lateral_relids
Definition: pathnodes.h:904
Expr * clause
Definition: pathnodes.h:2513
Definition: type.h:95

References add_partial_path(), Assert(), BackwardScanDirection, bms_add_members(), bms_copy(), bms_del_member(), 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, 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 1133 of file indxpath.c.

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

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

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

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

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

References PlannerInfo::all_query_rels, PlannerInfo::all_result_relids, Assert(), RelOptInfo::baserestrictinfo, bms_del_members(), 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, RelOptInfo::nulling_relids, 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 1335 of file indxpath.c.

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

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

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:369
@ BMS_DIFFERENT
Definition: bitmapset.h:65
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:299

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:4174
static Path * choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths)
Definition: indxpath.c:1335
static void match_join_clauses_to_index(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauseset, List **joinorclauses)
Definition: indxpath.c:2031
static void match_eclass_clauses_to_index(PlannerInfo *root, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2061
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:2016
static List * generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel, List *clauses, List *other_clauses)
Definition: indxpath.c:1228
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:1044
#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 3433 of file indxpath.c.

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

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

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

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

1706 {
1707  if (IsA(bitmapqual, BitmapAndPath))
1708  {
1709  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1710  ListCell *l;
1711 
1712  foreach(l, apath->bitmapquals)
1713  {
1714  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1715  }
1716  }
1717  else if (IsA(bitmapqual, BitmapOrPath))
1718  {
1719  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1720  ListCell *l;
1721 
1722  foreach(l, opath->bitmapquals)
1723  {
1724  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1725  }
1726  }
1727  else if (IsA(bitmapqual, IndexPath))
1728  {
1729  IndexPath *ipath = (IndexPath *) bitmapqual;
1730  ListCell *l;
1731 
1732  foreach(l, ipath->indexclauses)
1733  {
1734  IndexClause *iclause = (IndexClause *) lfirst(l);
1735 
1736  *quals = lappend(*quals, iclause->rinfo->clause);
1737  }
1738  *preds = list_concat(*preds, ipath->indexinfo->indpred);
1739  }
1740  else
1741  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1742 }
#define nodeTag(nodeptr)
Definition: nodes.h:133
List * bitmapquals
Definition: pathnodes.h:1771
List * bitmapquals
Definition: pathnodes.h:1784
List * indpred
Definition: pathnodes.h:1156
List * indexclauses
Definition: pathnodes.h:1685
IndexOptInfo * indexinfo
Definition: pathnodes.h:1684

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

1753 {
1754  int i;
1755  ListCell *lc;
1756 
1757  i = 0;
1758  foreach(lc, *nodelist)
1759  {
1760  Node *oldnode = (Node *) lfirst(lc);
1761 
1762  if (equal(node, oldnode))
1763  return i;
1764  i++;
1765  }
1766 
1767  *nodelist = lappend(*nodelist, node);
1768 
1769  return i;
1770 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223

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

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

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

2611 {
2612  Oid prosupport = get_func_support(funcid);
2614  List *sresult;
2615 
2616  if (!OidIsValid(prosupport))
2617  return NULL;
2618 
2619  req.type = T_SupportRequestIndexCondition;
2620  req.root = root;
2621  req.funcid = funcid;
2622  req.node = (Node *) rinfo->clause;
2623  req.indexarg = indexarg;
2624  req.index = index;
2625  req.indexcol = indexcol;
2626  req.opfamily = index->opfamily[indexcol];
2627  req.indexcollation = index->indexcollations[indexcol];
2628 
2629  req.lossy = true; /* default assumption */
2630 
2631  sresult = (List *)
2632  DatumGetPointer(OidFunctionCall1(prosupport,
2633  PointerGetDatum(&req)));
2634 
2635  if (sresult != NIL)
2636  {
2637  IndexClause *iclause = makeNode(IndexClause);
2638  List *indexquals = NIL;
2639  ListCell *lc;
2640 
2641  /*
2642  * The support function API says it should just give back bare
2643  * clauses, so here we must wrap each one in a RestrictInfo.
2644  */
2645  foreach(lc, sresult)
2646  {
2647  Expr *clause = (Expr *) lfirst(lc);
2648 
2649  indexquals = lappend(indexquals,
2650  make_simple_restrictinfo(root, clause));
2651  }
2652 
2653  iclause->rinfo = rinfo;
2654  iclause->indexquals = indexquals;
2655  iclause->lossy = req.lossy;
2656  iclause->indexcol = indexcol;
2657  iclause->indexcols = NIL;
2658 
2659  return iclause;
2660  }
2661 
2662  return NULL;
2663 }
#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:322
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
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:1690

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

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

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

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

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

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

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

◆ IsBooleanOpfamily()

static bool IsBooleanOpfamily ( Oid  opfamily)
static

Definition at line 2328 of file indxpath.c.

2329 {
2330  if (opfamily < FirstNormalObjectId)
2331  return IsBuiltinBooleanOpfamily(opfamily);
2332  else
2333  return op_in_opfamily(BooleanEqualOperator, opfamily);
2334 }
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 2353 of file indxpath.c.

2357 {
2358  Node *clause = (Node *) rinfo->clause;
2359  Expr *op = NULL;
2360 
2361  /* Direct match? */
2362  if (match_index_to_operand(clause, indexcol, index))
2363  {
2364  /* convert to indexkey = TRUE */
2365  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2366  (Expr *) clause,
2367  (Expr *) makeBoolConst(true, false),
2369  }
2370  /* NOT clause? */
2371  else if (is_notclause(clause))
2372  {
2373  Node *arg = (Node *) get_notclausearg((Expr *) clause);
2374 
2375  if (match_index_to_operand(arg, indexcol, index))
2376  {
2377  /* convert to indexkey = FALSE */
2378  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2379  (Expr *) arg,
2380  (Expr *) makeBoolConst(false, false),
2382  }
2383  }
2384 
2385  /*
2386  * Since we only consider clauses at top level of WHERE, we can convert
2387  * indexkey IS TRUE and indexkey IS FALSE to index searches as well. The
2388  * different meaning for NULL isn't important.
2389  */
2390  else if (clause && IsA(clause, BooleanTest))
2391  {
2392  BooleanTest *btest = (BooleanTest *) clause;
2393  Node *arg = (Node *) btest->arg;
2394 
2395  if (btest->booltesttype == IS_TRUE &&
2396  match_index_to_operand(arg, indexcol, index))
2397  {
2398  /* convert to indexkey = TRUE */
2399  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2400  (Expr *) arg,
2401  (Expr *) makeBoolConst(true, false),
2403  }
2404  else if (btest->booltesttype == IS_FALSE &&
2405  match_index_to_operand(arg, indexcol, index))
2406  {
2407  /* convert to indexkey = FALSE */
2408  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2409  (Expr *) arg,
2410  (Expr *) makeBoolConst(false, false),
2412  }
2413  }
2414 
2415  /*
2416  * If we successfully made an operator clause from the given qual, we must
2417  * wrap it in an IndexClause. It's not lossy.
2418  */
2419  if (op)
2420  {
2421  IndexClause *iclause = makeNode(IndexClause);
2422 
2423  iclause->rinfo = rinfo;
2424  iclause->indexquals = list_make1(make_simple_restrictinfo(root, op));
2425  iclause->lossy = false;
2426  iclause->indexcol = indexcol;
2427  iclause->indexcols = NIL;
2428  return iclause;
2429  }
2430 
2431  return NULL;
2432 }
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:360
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:1657
@ IS_FALSE
Definition: primnodes.h:1657
BoolTestType booltesttype
Definition: primnodes.h:1664
Expr * arg
Definition: primnodes.h:1663

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

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

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

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

References NullTest::arg, 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 3181 of file indxpath.c.

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

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

2103 {
2104  ListCell *lc;
2105 
2106  foreach(lc, clauses)
2107  {
2108  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
2109 
2110  match_clause_to_index(root, rinfo, index, clauseset);
2111  }
2112 }
static void match_clause_to_index(PlannerInfo *root, RestrictInfo *rinfo, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2132

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

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

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

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

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

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

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

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

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

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

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

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

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

2019 {
2020  /* We can ignore clauses that are implied by the index predicate */
2021  match_clauses_to_index(root, index->indrestrictinfo, index, clauseset);
2022 }

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

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

References bms_is_member(), BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, RestrictInfo::clause, contain_volatile_functions(), expand_indexqual_rowcompare(), get_commutator(), get_op_opfamily_strategy(), if(), IndexCollMatchesExprColl, InvalidOid, RowCompareExpr::largs, linitial, linitial_oid, match_index_to_operand(), 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 2671 of file indxpath.c.

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

References ScalarArrayOpExpr::args, bms_is_member(), RestrictInfo::clause, contain_volatile_functions(), if(), IndexClause::indexcol, IndexCollMatchesExprColl, IndexClause::indexcols, IndexClause::indexquals, 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 1541 of file indxpath.c.

1542 {
1543  PathClauseUsage *pa = *(PathClauseUsage *const *) a;
1544  PathClauseUsage *pb = *(PathClauseUsage *const *) b;
1545  Cost acost;
1546  Cost bcost;
1547  Selectivity aselec;
1548  Selectivity bselec;
1549 
1550  cost_bitmap_tree_node(pa->path, &acost, &aselec);
1551  cost_bitmap_tree_node(pb->path, &bcost, &bselec);
1552 
1553  /*
1554  * If costs are the same, sort by selectivity.
1555  */
1556  if (acost < bcost)
1557  return -1;
1558  if (acost > bcost)
1559  return 1;
1560 
1561  if (aselec < bselec)
1562  return -1;
1563  if (aselec > bselec)
1564  return 1;
1565 
1566  return 0;
1567 }
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 3491 of file indxpath.c.

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