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

Go to the source code of this file.

Data Structures

struct  IndexClauseSet
 
struct  PathClauseUsage
 
struct  ec_member_matches_arg
 

Macros

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

Enumerations

enum  ScanTypeControl { ST_INDEXSCAN, ST_BITMAPSCAN, ST_ANYSCAN }
 

Functions

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

Macro Definition Documentation

◆ IndexCollMatchesExprColl

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

Enumeration Type Documentation

◆ ScanTypeControl

Enumerator
ST_INDEXSCAN 
ST_BITMAPSCAN 
ST_ANYSCAN 

Definition at line 44 of file indxpath.c.

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

Function Documentation

◆ adjust_rowcount_for_semijoins()

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

Definition at line 2053 of file indxpath.c.

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

Referenced by get_loop_count().

2057 {
2058  ListCell *lc;
2059 
2060  foreach(lc, root->join_info_list)
2061  {
2062  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
2063 
2064  if (sjinfo->jointype == JOIN_SEMI &&
2065  bms_is_member(cur_relid, sjinfo->syn_lefthand) &&
2066  bms_is_member(outer_relid, sjinfo->syn_righthand))
2067  {
2068  /* Estimate number of unique-ified rows */
2069  double nraw;
2070  double nunique;
2071 
2072  nraw = approximate_joinrel_size(root, sjinfo->syn_righthand);
2073  nunique = estimate_num_groups(root,
2074  sjinfo->semi_rhs_exprs,
2075  nraw,
2076  NULL);
2077  if (rowcount > nunique)
2078  rowcount = nunique;
2079  }
2080  }
2081  return rowcount;
2082 }
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset)
Definition: selfuncs.c:3044
List * join_info_list
Definition: pathnodes.h:283
static double approximate_joinrel_size(PlannerInfo *root, Relids relids)
Definition: indxpath.c:2096
Relids syn_lefthand
Definition: pathnodes.h:2137
Relids syn_righthand
Definition: pathnodes.h:2138
List * semi_rhs_exprs
Definition: pathnodes.h:2146
#define lfirst(lc)
Definition: pg_list.h:190
JoinType jointype
Definition: pathnodes.h:2139
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ approximate_joinrel_size()

static double approximate_joinrel_size ( PlannerInfo root,
Relids  relids 
)
static

Definition at line 2096 of file indxpath.c.

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

Referenced by adjust_rowcount_for_semijoins().

2097 {
2098  double rowcount = 1.0;
2099  int relid;
2100 
2101  relid = -1;
2102  while ((relid = bms_next_member(relids, relid)) >= 0)
2103  {
2104  RelOptInfo *rel;
2105 
2106  /* Paranoia: ignore bogus relid indexes */
2107  if (relid >= root->simple_rel_array_size)
2108  continue;
2109  rel = root->simple_rel_array[relid];
2110  if (rel == NULL)
2111  continue;
2112  Assert(rel->relid == relid); /* sanity check on array */
2113 
2114  /* Relation could be proven empty, if so ignore */
2115  if (IS_DUMMY_REL(rel))
2116  continue;
2117 
2118  /* Otherwise, rel's rows estimate should be valid by now */
2119  Assert(rel->rows > 0);
2120 
2121  /* Accumulate product */
2122  rowcount *= rel->rows;
2123  }
2124  return rowcount;
2125 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:203
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1390
int simple_rel_array_size
Definition: pathnodes.h:204
Index relid
Definition: pathnodes.h:671
double rows
Definition: pathnodes.h:646
#define Assert(condition)
Definition: c.h:739

◆ bitmap_and_cost_est()

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

Definition at line 1644 of file indxpath.c.

References BitmapHeapPath::bitmapqual, BitmapAndPath::bitmapquals, cost_bitmap_and_node(), cost_bitmap_heap_scan(), get_baserel_parampathinfo(), get_bitmap_tree_required_outer(), get_loop_count(), NIL, Path::parallel_workers, Path::param_info, Path::parent, BitmapHeapPath::path, BitmapAndPath::path, Path::pathkeys, Path::pathtarget, Path::pathtype, RelOptInfo::relid, RelOptInfo::reltarget, T_BitmapAnd, T_BitmapAndPath, T_BitmapHeapPath, T_BitmapHeapScan, Path::total_cost, and Path::type.

Referenced by choose_bitmap_and().

1645 {
1646  BitmapAndPath apath;
1647  BitmapHeapPath bpath;
1648  Relids required_outer;
1649 
1650  /* Set up a dummy BitmapAndPath */
1651  apath.path.type = T_BitmapAndPath;
1652  apath.path.pathtype = T_BitmapAnd;
1653  apath.path.parent = rel;
1654  apath.path.pathtarget = rel->reltarget;
1655  apath.path.param_info = NULL; /* not used in bitmap trees */
1656  apath.path.pathkeys = NIL;
1657  apath.bitmapquals = paths;
1658  cost_bitmap_and_node(&apath, root);
1659 
1660  /* Identify required outer rels, in case it's a parameterized scan */
1661  required_outer = get_bitmap_tree_required_outer((Path *) &apath);
1662 
1663  /* Set up a dummy BitmapHeapPath */
1664  bpath.path.type = T_BitmapHeapPath;
1665  bpath.path.pathtype = T_BitmapHeapScan;
1666  bpath.path.parent = rel;
1667  bpath.path.pathtarget = rel->reltarget;
1668  bpath.path.param_info = get_baserel_parampathinfo(root, rel,
1669  required_outer);
1670  bpath.path.pathkeys = NIL;
1671  bpath.bitmapqual = (Path *) &apath;
1672 
1673  /*
1674  * Check the cost of temporary path without considering parallelism.
1675  * Parallel bitmap heap path will be considered at later stage.
1676  */
1677  bpath.path.parallel_workers = 0;
1678 
1679  /* Now we can do cost_bitmap_heap_scan */
1680  cost_bitmap_heap_scan(&bpath.path, root, rel,
1681  bpath.path.param_info,
1682  (Path *) &apath,
1683  get_loop_count(root, rel->relid, required_outer));
1684 
1685  return bpath.path.total_cost;
1686 }
#define NIL
Definition: pg_list.h:65
PathTarget * pathtarget
Definition: pathnodes.h:1117
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:940
ParamPathInfo * get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
Definition: relnode.c:1264
NodeTag type
Definition: pathnodes.h:1112
int parallel_workers
Definition: pathnodes.h:1123
void cost_bitmap_and_node(BitmapAndPath *path, PlannerInfo *root)
Definition: costsize.c:1084
ParamPathInfo * param_info
Definition: pathnodes.h:1119
List * bitmapquals
Definition: pathnodes.h:1264
NodeTag pathtype
Definition: pathnodes.h:1114
RelOptInfo * parent
Definition: pathnodes.h:1116
Path * bitmapqual
Definition: pathnodes.h:1252
Index relid
Definition: pathnodes.h:671
static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
Definition: indxpath.c:2000
Cost total_cost
Definition: pathnodes.h:1128
List * pathkeys
Definition: pathnodes.h:1130
static Relids get_bitmap_tree_required_outer(Path *bitmapqual)
Definition: indxpath.c:1767
struct PathTarget * reltarget
Definition: pathnodes.h:654

◆ bitmap_scan_cost_est()

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

Definition at line 1608 of file indxpath.c.

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

Referenced by choose_bitmap_and().

1609 {
1610  BitmapHeapPath bpath;
1611  Relids required_outer;
1612 
1613  /* Identify required outer rels, in case it's a parameterized scan */
1614  required_outer = get_bitmap_tree_required_outer(ipath);
1615 
1616  /* Set up a dummy BitmapHeapPath */
1617  bpath.path.type = T_BitmapHeapPath;
1618  bpath.path.pathtype = T_BitmapHeapScan;
1619  bpath.path.parent = rel;
1620  bpath.path.pathtarget = rel->reltarget;
1621  bpath.path.param_info = get_baserel_parampathinfo(root, rel,
1622  required_outer);
1623  bpath.path.pathkeys = NIL;
1624  bpath.bitmapqual = ipath;
1625 
1626  /*
1627  * Check the cost of temporary path without considering parallelism.
1628  * Parallel bitmap heap path will be considered at later stage.
1629  */
1630  bpath.path.parallel_workers = 0;
1631  cost_bitmap_heap_scan(&bpath.path, root, rel,
1632  bpath.path.param_info,
1633  ipath,
1634  get_loop_count(root, rel->relid, required_outer));
1635 
1636  return bpath.path.total_cost;
1637 }
#define NIL
Definition: pg_list.h:65
PathTarget * pathtarget
Definition: pathnodes.h:1117
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:940
ParamPathInfo * get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
Definition: relnode.c:1264
NodeTag type
Definition: pathnodes.h:1112
int parallel_workers
Definition: pathnodes.h:1123
ParamPathInfo * param_info
Definition: pathnodes.h:1119
NodeTag pathtype
Definition: pathnodes.h:1114
RelOptInfo * parent
Definition: pathnodes.h:1116
Path * bitmapqual
Definition: pathnodes.h:1252
Index relid
Definition: pathnodes.h:671
static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
Definition: indxpath.c:2000
Cost total_cost
Definition: pathnodes.h:1128
List * pathkeys
Definition: pathnodes.h:1130
static Relids get_bitmap_tree_required_outer(Path *bitmapqual)
Definition: indxpath.c:1767
struct PathTarget * reltarget
Definition: pathnodes.h:654

◆ bms_equal_any()

static bool bms_equal_any ( Relids  relids,
List relids_list 
)
static

Definition at line 710 of file indxpath.c.

References bms_equal(), and lfirst.

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

711 {
712  ListCell *lc;
713 
714  foreach(lc, relids_list)
715  {
716  if (bms_equal(relids, (Relids) lfirst(lc)))
717  return true;
718  }
719  return false;
720 }
#define lfirst(lc)
Definition: pg_list.h:190
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ build_index_paths()

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

Definition at line 861 of file indxpath.c.

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

Referenced by build_paths_for_OR(), and get_index_paths().

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

◆ build_paths_for_OR()

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

Definition at line 1166 of file indxpath.c.

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

Referenced by generate_bitmap_or_paths().

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

◆ check_index_only()

static bool check_index_only ( RelOptInfo rel,
IndexOptInfo index 
)
static

Definition at line 1888 of file indxpath.c.

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

Referenced by build_index_paths().

1889 {
1890  bool result;
1891  Bitmapset *attrs_used = NULL;
1892  Bitmapset *index_canreturn_attrs = NULL;
1893  Bitmapset *index_cannotreturn_attrs = NULL;
1894  ListCell *lc;
1895  int i;
1896 
1897  /* Index-only scans must be enabled */
1898  if (!enable_indexonlyscan)
1899  return false;
1900 
1901  /*
1902  * Check that all needed attributes of the relation are available from the
1903  * index.
1904  */
1905 
1906  /*
1907  * First, identify all the attributes needed for joins or final output.
1908  * Note: we must look at rel's targetlist, not the attr_needed data,
1909  * because attr_needed isn't computed for inheritance child rels.
1910  */
1911  pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
1912 
1913  /*
1914  * Add all the attributes used by restriction clauses; but consider only
1915  * those clauses not implied by the index predicate, since ones that are
1916  * so implied don't need to be checked explicitly in the plan.
1917  *
1918  * Note: attributes used only in index quals would not be needed at
1919  * runtime either, if we are certain that the index is not lossy. However
1920  * it'd be complicated to account for that accurately, and it doesn't
1921  * matter in most cases, since we'd conclude that such attributes are
1922  * available from the index anyway.
1923  */
1924  foreach(lc, index->indrestrictinfo)
1925  {
1926  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1927 
1928  pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
1929  }
1930 
1931  /*
1932  * Construct a bitmapset of columns that the index can return back in an
1933  * index-only scan. If there are multiple index columns containing the
1934  * same attribute, all of them must be capable of returning the value,
1935  * since we might recheck operators on any of them. (Potentially we could
1936  * be smarter about that, but it's such a weird situation that it doesn't
1937  * seem worth spending a lot of sweat on.)
1938  */
1939  for (i = 0; i < index->ncolumns; i++)
1940  {
1941  int attno = index->indexkeys[i];
1942 
1943  /*
1944  * For the moment, we just ignore index expressions. It might be nice
1945  * to do something with them, later.
1946  */
1947  if (attno == 0)
1948  continue;
1949 
1950  if (index->canreturn[i])
1951  index_canreturn_attrs =
1952  bms_add_member(index_canreturn_attrs,
1954  else
1955  index_cannotreturn_attrs =
1956  bms_add_member(index_cannotreturn_attrs,
1958  }
1959 
1960  index_canreturn_attrs = bms_del_members(index_canreturn_attrs,
1961  index_cannotreturn_attrs);
1962 
1963  /* Do we have all the necessary attributes? */
1964  result = bms_is_subset(attrs_used, index_canreturn_attrs);
1965 
1966  bms_free(attrs_used);
1967  bms_free(index_canreturn_attrs);
1968  bms_free(index_cannotreturn_attrs);
1969 
1970  return result;
1971 }
Definition: nodes.h:525
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Index relid
Definition: pathnodes.h:671
Expr * clause
Definition: pathnodes.h:1945
List * exprs
Definition: pathnodes.h:1046
List * indrestrictinfo
Definition: pathnodes.h:820
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:928
int i
int * indexkeys
Definition: pathnodes.h:803
bool * canreturn
Definition: pathnodes.h:811
struct PathTarget * reltarget
Definition: pathnodes.h:654
bool enable_indexonlyscan
Definition: costsize.c:126

◆ check_index_predicates()

void check_index_predicates ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 3395 of file indxpath.c.

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

Referenced by set_plain_rel_size(), and set_tablesample_rel_size().

3396 {
3397  List *clauselist;
3398  bool have_partial;
3399  bool is_target_rel;
3400  Relids otherrels;
3401  ListCell *lc;
3402 
3403  /* Indexes are available only on base or "other" member relations. */
3404  Assert(IS_SIMPLE_REL(rel));
3405 
3406  /*
3407  * Initialize the indrestrictinfo lists to be identical to
3408  * baserestrictinfo, and check whether there are any partial indexes. If
3409  * not, this is all we need to do.
3410  */
3411  have_partial = false;
3412  foreach(lc, rel->indexlist)
3413  {
3415 
3416  index->indrestrictinfo = rel->baserestrictinfo;
3417  if (index->indpred)
3418  have_partial = true;
3419  }
3420  if (!have_partial)
3421  return;
3422 
3423  /*
3424  * Construct a list of clauses that we can assume true for the purpose of
3425  * proving the index(es) usable. Restriction clauses for the rel are
3426  * always usable, and so are any join clauses that are "movable to" this
3427  * rel. Also, we can consider any EC-derivable join clauses (which must
3428  * be "movable to" this rel, by definition).
3429  */
3430  clauselist = list_copy(rel->baserestrictinfo);
3431 
3432  /* Scan the rel's join clauses */
3433  foreach(lc, rel->joininfo)
3434  {
3435  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3436 
3437  /* Check if clause can be moved to this rel */
3438  if (!join_clause_is_movable_to(rinfo, rel))
3439  continue;
3440 
3441  clauselist = lappend(clauselist, rinfo);
3442  }
3443 
3444  /*
3445  * Add on any equivalence-derivable join clauses. Computing the correct
3446  * relid sets for generate_join_implied_equalities is slightly tricky
3447  * because the rel could be a child rel rather than a true baserel, and in
3448  * that case we must remove its parents' relid(s) from all_baserels.
3449  */
3450  if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3451  otherrels = bms_difference(root->all_baserels,
3452  find_childrel_parents(root, rel));
3453  else
3454  otherrels = bms_difference(root->all_baserels, rel->relids);
3455 
3456  if (!bms_is_empty(otherrels))
3457  clauselist =
3458  list_concat(clauselist,
3460  bms_union(rel->relids,
3461  otherrels),
3462  otherrels,
3463  rel));
3464 
3465  /*
3466  * Normally we remove quals that are implied by a partial index's
3467  * predicate from indrestrictinfo, indicating that they need not be
3468  * checked explicitly by an indexscan plan using this index. However, if
3469  * the rel is a target relation of UPDATE/DELETE/SELECT FOR UPDATE, we
3470  * cannot remove such quals from the plan, because they need to be in the
3471  * plan so that they will be properly rechecked by EvalPlanQual testing.
3472  * Some day we might want to remove such quals from the main plan anyway
3473  * and pass them through to EvalPlanQual via a side channel; but for now,
3474  * we just don't remove implied quals at all for target relations.
3475  */
3476  is_target_rel = (rel->relid == root->parse->resultRelation ||
3477  get_plan_rowmark(root->rowMarks, rel->relid) != NULL);
3478 
3479  /*
3480  * Now try to prove each index predicate true, and compute the
3481  * indrestrictinfo lists for partial indexes. Note that we compute the
3482  * indrestrictinfo list even for non-predOK indexes; this might seem
3483  * wasteful, but we may be able to use such indexes in OR clauses, cf
3484  * generate_bitmap_or_paths().
3485  */
3486  foreach(lc, rel->indexlist)
3487  {
3488  IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
3489  ListCell *lcr;
3490 
3491  if (index->indpred == NIL)
3492  continue; /* ignore non-partial indexes here */
3493 
3494  if (!index->predOK) /* don't repeat work if already proven OK */
3495  index->predOK = predicate_implied_by(index->indpred, clauselist,
3496  false);
3497 
3498  /* If rel is an update target, leave indrestrictinfo as set above */
3499  if (is_target_rel)
3500  continue;
3501 
3502  /* Else compute indrestrictinfo as the non-implied quals */
3503  index->indrestrictinfo = NIL;
3504  foreach(lcr, rel->baserestrictinfo)
3505  {
3506  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcr);
3507 
3508  /* predicate_implied_by() assumes first arg is immutable */
3509  if (contain_mutable_functions((Node *) rinfo->clause) ||
3511  index->indpred, false))
3512  index->indrestrictinfo = lappend(index->indrestrictinfo, rinfo);
3513  }
3514  }
3515 }
#define NIL
Definition: pg_list.h:65
List * rowMarks
Definition: pathnodes.h:292
Query * parse
Definition: pathnodes.h:179
RelOptKind reloptkind
Definition: pathnodes.h:640
List * baserestrictinfo
Definition: pathnodes.h:705
int resultRelation
Definition: parsenodes.h:122
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:291
List * list_copy(const List *oldlist)
Definition: list.c:1404
Definition: nodes.h:525
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:616
Definition: type.h:89
#define list_make1(x1)
Definition: pg_list.h:227
Relids all_baserels
Definition: pathnodes.h:227
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1126
List * joininfo
Definition: pathnodes.h:709
Relids relids
Definition: pathnodes.h:643
Index relid
Definition: pathnodes.h:671
List * lappend(List *list, void *datum)
Definition: list.c:322
Expr * clause
Definition: pathnodes.h:1945
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
List * indrestrictinfo
Definition: pathnodes.h:820
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1228
List * indexlist
Definition: pathnodes.h:680
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
bool join_clause_is_movable_to(RestrictInfo *rinfo, RelOptInfo *baserel)
Definition: restrictinfo.c:504
PlanRowMark * get_plan_rowmark(List *rowmarks, Index rtindex)
Definition: preptlist.c:425
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:645
List * indpred
Definition: pathnodes.h:816
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:151
Definition: pg_list.h:50

◆ choose_bitmap_and()

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

Definition at line 1368 of file indxpath.c.

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

Referenced by create_index_paths(), and generate_bitmap_or_paths().

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

◆ classify_index_clause_usage()

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

Definition at line 1704 of file indxpath.c.

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

Referenced by choose_bitmap_and().

1705 {
1706  PathClauseUsage *result;
1707  Bitmapset *clauseids;
1708  ListCell *lc;
1709 
1710  result = (PathClauseUsage *) palloc(sizeof(PathClauseUsage));
1711  result->path = path;
1712 
1713  /* Recursively find the quals and preds used by the path */
1714  result->quals = NIL;
1715  result->preds = NIL;
1716  find_indexpath_quals(path, &result->quals, &result->preds);
1717 
1718  /*
1719  * Some machine-generated queries have outlandish numbers of qual clauses.
1720  * To avoid getting into O(N^2) behavior even in this preliminary
1721  * classification step, we want to limit the number of entries we can
1722  * accumulate in *clauselist. Treat any path with more than 100 quals +
1723  * preds as unclassifiable, which will cause calling code to consider it
1724  * distinct from all other paths.
1725  */
1726  if (list_length(result->quals) + list_length(result->preds) > 100)
1727  {
1728  result->clauseids = NULL;
1729  result->unclassifiable = true;
1730  return result;
1731  }
1732 
1733  /* Build up a bitmapset representing the quals and preds */
1734  clauseids = NULL;
1735  foreach(lc, result->quals)
1736  {
1737  Node *node = (Node *) lfirst(lc);
1738 
1739  clauseids = bms_add_member(clauseids,
1740  find_list_position(node, clauselist));
1741  }
1742  foreach(lc, result->preds)
1743  {
1744  Node *node = (Node *) lfirst(lc);
1745 
1746  clauseids = bms_add_member(clauseids,
1747  find_list_position(node, clauselist));
1748  }
1749  result->clauseids = clauseids;
1750  result->unclassifiable = false;
1751 
1752  return result;
1753 }
#define NIL
Definition: pg_list.h:65
static int find_list_position(Node *node, List **nodelist)
Definition: indxpath.c:1862
Path * path
Definition: indxpath.c:62
List * quals
Definition: indxpath.c:63
Definition: nodes.h:525
Bitmapset * clauseids
Definition: indxpath.c:65
static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
Definition: indxpath.c:1815
bool unclassifiable
Definition: indxpath.c:66
List * preds
Definition: indxpath.c:64
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
void * palloc(Size size)
Definition: mcxt.c:949

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

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

Referenced by create_index_paths().

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

◆ consider_index_join_outer_rels()

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

Definition at line 505 of file indxpath.c.

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

Referenced by consider_index_join_clauses().

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

◆ create_index_paths()

void create_index_paths ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 232 of file indxpath.c.

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

Referenced by set_plain_rel_pathlist().

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

◆ ec_member_matches_indexcol()

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

Definition at line 3528 of file indxpath.c.

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

Referenced by match_eclass_clauses_to_index().

3531 {
3532  IndexOptInfo *index = ((ec_member_matches_arg *) arg)->index;
3533  int indexcol = ((ec_member_matches_arg *) arg)->indexcol;
3534  Oid curFamily;
3535  Oid curCollation;
3536 
3537  Assert(indexcol < index->nkeycolumns);
3538 
3539  curFamily = index->opfamily[indexcol];
3540  curCollation = index->indexcollations[indexcol];
3541 
3542  /*
3543  * If it's a btree index, we can reject it if its opfamily isn't
3544  * compatible with the EC, since no clause generated from the EC could be
3545  * used with the index. For non-btree indexes, we can't easily tell
3546  * whether clauses generated from the EC could be used with the index, so
3547  * don't check the opfamily. This might mean we return "true" for a
3548  * useless EC, so we have to recheck the results of
3549  * generate_implied_equalities_for_column; see
3550  * match_eclass_clauses_to_index.
3551  */
3552  if (index->relam == BTREE_AM_OID &&
3553  !list_member_oid(ec->ec_opfamilies, curFamily))
3554  return false;
3555 
3556  /* We insist on collation match for all index types, though */
3557  if (!IndexCollMatchesExprColl(curCollation, ec->ec_collation))
3558  return false;
3559 
3560  return match_index_to_operand((Node *) em->em_expr, indexcol, index);
3561 }
Oid * indexcollations
Definition: pathnodes.h:805
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3806
Definition: nodes.h:525
unsigned int Oid
Definition: postgres_ext.h:31
Definition: type.h:89
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:40
List * ec_opfamilies
Definition: pathnodes.h:934
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:675
#define Assert(condition)
Definition: c.h:739
Oid * opfamily
Definition: pathnodes.h:806
void * arg

◆ eclass_already_used()

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

Definition at line 686 of file indxpath.c.

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

Referenced by consider_index_join_outer_rels().

688 {
689  ListCell *lc;
690 
691  foreach(lc, indexjoinclauses)
692  {
693  IndexClause *iclause = (IndexClause *) lfirst(lc);
694  RestrictInfo *rinfo = iclause->rinfo;
695 
696  if (rinfo->parent_ec == parent_ec &&
697  bms_is_subset(rinfo->clause_relids, oldrelids))
698  return true;
699  }
700  return false;
701 }
Relids clause_relids
Definition: pathnodes.h:1960
EquivalenceClass * parent_ec
Definition: pathnodes.h:1979
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
struct RestrictInfo * rinfo
Definition: pathnodes.h:1225
#define lfirst(lc)
Definition: pg_list.h:190

◆ expand_indexqual_rowcompare()

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

Definition at line 2946 of file indxpath.c.

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

Referenced by match_rowcompare_to_indexcol().

2951 {
2952  IndexClause *iclause = makeNode(IndexClause);
2953  RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
2954  int op_strategy;
2955  Oid op_lefttype;
2956  Oid op_righttype;
2957  int matching_cols;
2958  List *expr_ops;
2959  List *opfamilies;
2960  List *lefttypes;
2961  List *righttypes;
2962  List *new_ops;
2963  List *var_args;
2964  List *non_var_args;
2965 
2966  iclause->rinfo = rinfo;
2967  iclause->indexcol = indexcol;
2968 
2969  if (var_on_left)
2970  {
2971  var_args = clause->largs;
2972  non_var_args = clause->rargs;
2973  }
2974  else
2975  {
2976  var_args = clause->rargs;
2977  non_var_args = clause->largs;
2978  }
2979 
2980  get_op_opfamily_properties(expr_op, index->opfamily[indexcol], false,
2981  &op_strategy,
2982  &op_lefttype,
2983  &op_righttype);
2984 
2985  /* Initialize returned list of which index columns are used */
2986  iclause->indexcols = list_make1_int(indexcol);
2987 
2988  /* Build lists of ops, opfamilies and operator datatypes in case needed */
2989  expr_ops = list_make1_oid(expr_op);
2990  opfamilies = list_make1_oid(index->opfamily[indexcol]);
2991  lefttypes = list_make1_oid(op_lefttype);
2992  righttypes = list_make1_oid(op_righttype);
2993 
2994  /*
2995  * See how many of the remaining columns match some index column in the
2996  * same way. As in match_clause_to_indexcol(), the "other" side of any
2997  * potential index condition is OK as long as it doesn't use Vars from the
2998  * indexed relation.
2999  */
3000  matching_cols = 1;
3001 
3002  while (matching_cols < list_length(var_args))
3003  {
3004  Node *varop = (Node *) list_nth(var_args, matching_cols);
3005  Node *constop = (Node *) list_nth(non_var_args, matching_cols);
3006  int i;
3007 
3008  expr_op = list_nth_oid(clause->opnos, matching_cols);
3009  if (!var_on_left)
3010  {
3011  /* indexkey is on right, so commute the operator */
3012  expr_op = get_commutator(expr_op);
3013  if (expr_op == InvalidOid)
3014  break; /* operator is not usable */
3015  }
3016  if (bms_is_member(index->rel->relid, pull_varnos(constop)))
3017  break; /* no good, Var on wrong side */
3018  if (contain_volatile_functions(constop))
3019  break; /* no good, volatile comparison value */
3020 
3021  /*
3022  * The Var side can match any key column of the index.
3023  */
3024  for (i = 0; i < index->nkeycolumns; i++)
3025  {
3026  if (match_index_to_operand(varop, i, index) &&
3027  get_op_opfamily_strategy(expr_op,
3028  index->opfamily[i]) == op_strategy &&
3030  list_nth_oid(clause->inputcollids,
3031  matching_cols)))
3032  break;
3033  }
3034  if (i >= index->nkeycolumns)
3035  break; /* no match found */
3036 
3037  /* Add column number to returned list */
3038  iclause->indexcols = lappend_int(iclause->indexcols, i);
3039 
3040  /* Add operator info to lists */
3041  get_op_opfamily_properties(expr_op, index->opfamily[i], false,
3042  &op_strategy,
3043  &op_lefttype,
3044  &op_righttype);
3045  expr_ops = lappend_oid(expr_ops, expr_op);
3046  opfamilies = lappend_oid(opfamilies, index->opfamily[i]);
3047  lefttypes = lappend_oid(lefttypes, op_lefttype);
3048  righttypes = lappend_oid(righttypes, op_righttype);
3049 
3050  /* This column matches, keep scanning */
3051  matching_cols++;
3052  }
3053 
3054  /* Result is non-lossy if all columns are usable as index quals */
3055  iclause->lossy = (matching_cols != list_length(clause->opnos));
3056 
3057  /*
3058  * We can use rinfo->clause as-is if we have var on left and it's all
3059  * usable as index quals.
3060  */
3061  if (var_on_left && !iclause->lossy)
3062  iclause->indexquals = list_make1(rinfo);
3063  else
3064  {
3065  /*
3066  * We have to generate a modified rowcompare (possibly just one
3067  * OpExpr). The painful part of this is changing < to <= or > to >=,
3068  * so deal with that first.
3069  */
3070  if (!iclause->lossy)
3071  {
3072  /* very easy, just use the commuted operators */
3073  new_ops = expr_ops;
3074  }
3075  else if (op_strategy == BTLessEqualStrategyNumber ||
3076  op_strategy == BTGreaterEqualStrategyNumber)
3077  {
3078  /* easy, just use the same (possibly commuted) operators */
3079  new_ops = list_truncate(expr_ops, matching_cols);
3080  }
3081  else
3082  {
3083  ListCell *opfamilies_cell;
3084  ListCell *lefttypes_cell;
3085  ListCell *righttypes_cell;
3086 
3087  if (op_strategy == BTLessStrategyNumber)
3088  op_strategy = BTLessEqualStrategyNumber;
3089  else if (op_strategy == BTGreaterStrategyNumber)
3090  op_strategy = BTGreaterEqualStrategyNumber;
3091  else
3092  elog(ERROR, "unexpected strategy number %d", op_strategy);
3093  new_ops = NIL;
3094  forthree(opfamilies_cell, opfamilies,
3095  lefttypes_cell, lefttypes,
3096  righttypes_cell, righttypes)
3097  {
3098  Oid opfam = lfirst_oid(opfamilies_cell);
3099  Oid lefttype = lfirst_oid(lefttypes_cell);
3100  Oid righttype = lfirst_oid(righttypes_cell);
3101 
3102  expr_op = get_opfamily_member(opfam, lefttype, righttype,
3103  op_strategy);
3104  if (!OidIsValid(expr_op)) /* should not happen */
3105  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
3106  op_strategy, lefttype, righttype, opfam);
3107  new_ops = lappend_oid(new_ops, expr_op);
3108  }
3109  }
3110 
3111  /* If we have more than one matching col, create a subset rowcompare */
3112  if (matching_cols > 1)
3113  {
3115 
3116  rc->rctype = (RowCompareType) op_strategy;
3117  rc->opnos = new_ops;
3119  matching_cols);
3121  matching_cols);
3122  rc->largs = list_truncate(copyObject(var_args),
3123  matching_cols);
3124  rc->rargs = list_truncate(copyObject(non_var_args),
3125  matching_cols);
3126  iclause->indexquals = list_make1(make_simple_restrictinfo((Expr *) rc));
3127  }
3128  else
3129  {
3130  Expr *op;
3131 
3132  /* We don't report an index column list in this case */
3133  iclause->indexcols = NIL;
3134 
3135  op = make_opclause(linitial_oid(new_ops), BOOLOID, false,
3136  copyObject(linitial(var_args)),
3137  copyObject(linitial(non_var_args)),
3138  InvalidOid,
3139  linitial_oid(clause->inputcollids));
3141  }
3142  }
3143 
3144  return iclause;
3145 }
#define NIL
Definition: pg_list.h:65
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1311
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
Oid * indexcollations
Definition: pathnodes.h:805
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3806
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:464
RowCompareType rctype
Definition: primnodes.h:1056
List * opfamilies
Definition: primnodes.h:1058
List * list_truncate(List *list, int new_size)
Definition: list.c:586
List * list_copy(const List *oldlist)
Definition: list.c:1404
Definition: nodes.h:525
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:358
#define OidIsValid(objectId)
Definition: c.h:645
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:607
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:299
#define list_make1(x1)
Definition: pg_list.h:227
RelOptInfo * rel
Definition: pathnodes.h:793
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
List * indexcols
Definition: pathnodes.h:1229
AttrNumber indexcol
Definition: pathnodes.h:1228
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:40
struct RestrictInfo * rinfo
Definition: pathnodes.h:1225
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
#define list_make1_int(x1)
Definition: pg_list.h:238
List * indexquals
Definition: pathnodes.h:1226
Relids pull_varnos(Node *node)
Definition: var.c:95
List * lappend_int(List *list, int datum)
Definition: list.c:340
Index relid
Definition: pathnodes.h:671
Expr * clause
Definition: pathnodes.h:1945
#define list_make1_oid(x1)
Definition: pg_list.h:249
#define InvalidOid
Definition: postgres_ext.h:36
RowCompareType
Definition: primnodes.h:1042
#define makeNode(_type_)
Definition: nodes.h:573
#define linitial_oid(l)
Definition: pg_list.h:197
static int list_length(const List *l)
Definition: pg_list.h:169
int nkeycolumns
Definition: pathnodes.h:802
Oid * opfamily
Definition: pathnodes.h:806
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Definition: lsyscache.c:80
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:133
#define elog(elevel,...)
Definition: elog.h:228
int i
#define copyObject(obj)
Definition: nodes.h:641
List * inputcollids
Definition: primnodes.h:1059
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
#define make_simple_restrictinfo(clause)
Definition: restrictinfo.h:21
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ find_indexpath_quals()

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

Definition at line 1815 of file indxpath.c.

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

Referenced by classify_index_clause_usage().

1816 {
1817  if (IsA(bitmapqual, BitmapAndPath))
1818  {
1819  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1820  ListCell *l;
1821 
1822  foreach(l, apath->bitmapquals)
1823  {
1824  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1825  }
1826  }
1827  else if (IsA(bitmapqual, BitmapOrPath))
1828  {
1829  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1830  ListCell *l;
1831 
1832  foreach(l, opath->bitmapquals)
1833  {
1834  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1835  }
1836  }
1837  else if (IsA(bitmapqual, IndexPath))
1838  {
1839  IndexPath *ipath = (IndexPath *) bitmapqual;
1840  ListCell *l;
1841 
1842  foreach(l, ipath->indexclauses)
1843  {
1844  IndexClause *iclause = (IndexClause *) lfirst(l);
1845 
1846  *quals = lappend(*quals, iclause->rinfo->clause);
1847  }
1848  *preds = list_concat(*preds, ipath->indexinfo->indpred);
1849  }
1850  else
1851  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1852 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
IndexOptInfo * indexinfo
Definition: pathnodes.h:1179
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
List * indexclauses
Definition: pathnodes.h:1180
List * bitmapquals
Definition: pathnodes.h:1264
List * bitmapquals
Definition: pathnodes.h:1277
static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
Definition: indxpath.c:1815
#define ERROR
Definition: elog.h:43
struct RestrictInfo * rinfo
Definition: pathnodes.h:1225
List * lappend(List *list, void *datum)
Definition: list.c:322
Expr * clause
Definition: pathnodes.h:1945
#define lfirst(lc)
Definition: pg_list.h:190
#define nodeTag(nodeptr)
Definition: nodes.h:530
#define elog(elevel,...)
Definition: elog.h:228
List * indpred
Definition: pathnodes.h:816

◆ find_list_position()

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

Definition at line 1862 of file indxpath.c.

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

Referenced by classify_index_clause_usage().

1863 {
1864  int i;
1865  ListCell *lc;
1866 
1867  i = 0;
1868  foreach(lc, *nodelist)
1869  {
1870  Node *oldnode = (Node *) lfirst(lc);
1871 
1872  if (equal(node, oldnode))
1873  return i;
1874  i++;
1875  }
1876 
1877  *nodelist = lappend(*nodelist, node);
1878 
1879  return i;
1880 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3011
Definition: nodes.h:525
List * lappend(List *list, void *datum)
Definition: list.c:322
#define lfirst(lc)
Definition: pg_list.h:190
int i

◆ generate_bitmap_or_paths()

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

Definition at line 1261 of file indxpath.c.

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

Referenced by create_index_paths().

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

◆ get_bitmap_tree_required_outer()

static Relids get_bitmap_tree_required_outer ( Path bitmapqual)
static

Definition at line 1767 of file indxpath.c.

References bms_copy(), bms_join(), elog, ERROR, IsA, lfirst, nodeTag, and PATH_REQ_OUTER.

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

1768 {
1769  Relids result = NULL;
1770  ListCell *lc;
1771 
1772  if (IsA(bitmapqual, IndexPath))
1773  {
1774  return bms_copy(PATH_REQ_OUTER(bitmapqual));
1775  }
1776  else if (IsA(bitmapqual, BitmapAndPath))
1777  {
1778  foreach(lc, ((BitmapAndPath *) bitmapqual)->bitmapquals)
1779  {
1780  result = bms_join(result,
1782  }
1783  }
1784  else if (IsA(bitmapqual, BitmapOrPath))
1785  {
1786  foreach(lc, ((BitmapOrPath *) bitmapqual)->bitmapquals)
1787  {
1788  result = bms_join(result,
1790  }
1791  }
1792  else
1793  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1794 
1795  return result;
1796 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
#define ERROR
Definition: elog.h:43
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:949
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1135
#define lfirst(lc)
Definition: pg_list.h:190
#define nodeTag(nodeptr)
Definition: nodes.h:530
#define elog(elevel,...)
Definition: elog.h:228
static Relids get_bitmap_tree_required_outer(Path *bitmapqual)
Definition: indxpath.c:1767

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

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

Referenced by match_funcclause_to_indexcol(), and match_opclause_to_indexcol().

2715 {
2716  Oid prosupport = get_func_support(funcid);
2718  List *sresult;
2719 
2720  if (!OidIsValid(prosupport))
2721  return NULL;
2722 
2724  req.root = root;
2725  req.funcid = funcid;
2726  req.node = (Node *) rinfo->clause;
2727  req.indexarg = indexarg;
2728  req.index = index;
2729  req.indexcol = indexcol;
2730  req.opfamily = index->opfamily[indexcol];
2731  req.indexcollation = index->indexcollations[indexcol];
2732 
2733  req.lossy = true; /* default assumption */
2734 
2735  sresult = (List *)
2736  DatumGetPointer(OidFunctionCall1(prosupport,
2737  PointerGetDatum(&req)));
2738 
2739  if (sresult != NIL)
2740  {
2741  IndexClause *iclause = makeNode(IndexClause);
2742  List *indexquals = NIL;
2743  ListCell *lc;
2744 
2745  /*
2746  * The support function API says it should just give back bare
2747  * clauses, so here we must wrap each one in a RestrictInfo.
2748  */
2749  foreach(lc, sresult)
2750  {
2751  Expr *clause = (Expr *) lfirst(lc);
2752 
2753  indexquals = lappend(indexquals, make_simple_restrictinfo(clause));
2754  }
2755 
2756  iclause->rinfo = rinfo;
2757  iclause->indexquals = indexquals;
2758  iclause->lossy = req.lossy;
2759  iclause->indexcol = indexcol;
2760  iclause->indexcols = NIL;
2761 
2762  return iclause;
2763  }
2764 
2765  return NULL;
2766 }
#define NIL
Definition: pg_list.h:65
Oid * indexcollations
Definition: pathnodes.h:805
#define PointerGetDatum(X)
Definition: postgres.h:556
Definition: nodes.h:525
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
struct IndexOptInfo * index
Definition: supportnodes.h:232
List * indexcols
Definition: pathnodes.h:1229
AttrNumber indexcol
Definition: pathnodes.h:1228
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:653
struct RestrictInfo * rinfo
Definition: pathnodes.h:1225
List * indexquals
Definition: pathnodes.h:1226
List * lappend(List *list, void *datum)
Definition: list.c:322
Expr * clause
Definition: pathnodes.h:1945
RegProcedure get_func_support(Oid funcid)
Definition: lsyscache.c:1660
#define makeNode(_type_)
Definition: nodes.h:573
#define lfirst(lc)
Definition: pg_list.h:190
#define DatumGetPointer(X)
Definition: postgres.h:549
Oid * opfamily
Definition: pathnodes.h:806
Definition: pg_list.h:50
#define make_simple_restrictinfo(clause)
Definition: restrictinfo.h:21
struct PlannerInfo * root
Definition: supportnodes.h:228

◆ get_index_paths()

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

Definition at line 739 of file indxpath.c.

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

Referenced by create_index_paths(), and get_join_index_paths().

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

◆ get_join_index_paths()

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

Definition at line 608 of file indxpath.c.

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

Referenced by consider_index_join_outer_rels().

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

◆ get_loop_count()

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

Definition at line 2000 of file indxpath.c.

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

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

2001 {
2002  double result;
2003  int outer_relid;
2004 
2005  /* For a non-parameterized path, just return 1.0 quickly */
2006  if (outer_relids == NULL)
2007  return 1.0;
2008 
2009  result = 0.0;
2010  outer_relid = -1;
2011  while ((outer_relid = bms_next_member(outer_relids, outer_relid)) >= 0)
2012  {
2013  RelOptInfo *outer_rel;
2014  double rowcount;
2015 
2016  /* Paranoia: ignore bogus relid indexes */
2017  if (outer_relid >= root->simple_rel_array_size)
2018  continue;
2019  outer_rel = root->simple_rel_array[outer_relid];
2020  if (outer_rel == NULL)
2021  continue;
2022  Assert(outer_rel->relid == outer_relid); /* sanity check on array */
2023 
2024  /* Other relation could be proven empty, if so ignore */
2025  if (IS_DUMMY_REL(outer_rel))
2026  continue;
2027 
2028  /* Otherwise, rel's rows estimate should be valid by now */
2029  Assert(outer_rel->rows > 0);
2030 
2031  /* Check to see if rel is on the inside of any semijoins */
2032  rowcount = adjust_rowcount_for_semijoins(root,
2033  cur_relid,
2034  outer_relid,
2035  outer_rel->rows);
2036 
2037  /* Remember smallest row count estimate among the outer rels */
2038  if (result == 0.0 || result > rowcount)
2039  result = rowcount;
2040  }
2041  /* Return 1.0 if we found no valid relations (shouldn't happen) */
2042  return (result > 0.0) ? result : 1.0;
2043 }
static double adjust_rowcount_for_semijoins(PlannerInfo *root, Index cur_relid, Index outer_relid, double rowcount)
Definition: indxpath.c:2053
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:203
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1390
int simple_rel_array_size
Definition: pathnodes.h:204
Index relid
Definition: pathnodes.h:671
double rows
Definition: pathnodes.h:646
#define Assert(condition)
Definition: c.h:739

◆ indexcol_is_bool_constant_for_query()

bool indexcol_is_bool_constant_for_query ( IndexOptInfo index,
int  indexcol 
)

Definition at line 3757 of file indxpath.c.

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

Referenced by build_index_pathkeys().

3758 {
3759  ListCell *lc;
3760 
3761  /* If the index isn't boolean, we can't possibly get a match */
3762  if (!IsBooleanOpfamily(index->opfamily[indexcol]))
3763  return false;
3764 
3765  /* Check each restriction clause for the index's rel */
3766  foreach(lc, index->rel->baserestrictinfo)
3767  {
3768  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3769 
3770  /*
3771  * As in match_clause_to_indexcol, never match pseudoconstants to
3772  * indexes. (It might be semantically okay to do so here, but the
3773  * odds of getting a match are negligible, so don't waste the cycles.)
3774  */
3775  if (rinfo->pseudoconstant)
3776  continue;
3777 
3778  /* See if we can match the clause's expression to the index column */
3779  if (match_boolean_index_clause(rinfo, indexcol, index))
3780  return true;
3781  }
3782 
3783  return false;
3784 }
static IndexClause * match_boolean_index_clause(RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2458
List * baserestrictinfo
Definition: pathnodes.h:705
bool pseudoconstant
Definition: pathnodes.h:1953
RelOptInfo * rel
Definition: pathnodes.h:793
#define lfirst(lc)
Definition: pg_list.h:190
Oid * opfamily
Definition: pathnodes.h:806

◆ is_pseudo_constant_for_index()

bool is_pseudo_constant_for_index ( Node expr,
IndexOptInfo index 
)

Definition at line 3891 of file indxpath.c.

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

3892 {
3893  /* pull_varnos is cheaper than volatility check, so do that first */
3894  if (bms_is_member(index->rel->relid, pull_varnos(expr)))
3895  return false; /* no good, contains Var of table */
3896  if (contain_volatile_functions(expr))
3897  return false; /* no good, volatile comparison value */
3898  return true;
3899 }
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
RelOptInfo * rel
Definition: pathnodes.h:793
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: pathnodes.h:671
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ match_boolean_index_clause()

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

Definition at line 2458 of file indxpath.c.

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

Referenced by indexcol_is_bool_constant_for_query(), and match_clause_to_indexcol().

2461 {
2462  Node *clause = (Node *) rinfo->clause;
2463  Expr *op = NULL;
2464 
2465  /* Direct match? */
2466  if (match_index_to_operand(clause, indexcol, index))
2467  {
2468  /* convert to indexkey = TRUE */
2469  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2470  (Expr *) clause,
2471  (Expr *) makeBoolConst(true, false),
2473  }
2474  /* NOT clause? */
2475  else if (is_notclause(clause))
2476  {
2477  Node *arg = (Node *) get_notclausearg((Expr *) clause);
2478 
2479  if (match_index_to_operand(arg, indexcol, index))
2480  {
2481  /* convert to indexkey = FALSE */
2482  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2483  (Expr *) arg,
2484  (Expr *) makeBoolConst(false, false),
2486  }
2487  }
2488 
2489  /*
2490  * Since we only consider clauses at top level of WHERE, we can convert
2491  * indexkey IS TRUE and indexkey IS FALSE to index searches as well. The
2492  * different meaning for NULL isn't important.
2493  */
2494  else if (clause && IsA(clause, BooleanTest))
2495  {
2496  BooleanTest *btest = (BooleanTest *) clause;
2497  Node *arg = (Node *) btest->arg;
2498 
2499  if (btest->booltesttype == IS_TRUE &&
2500  match_index_to_operand(arg, indexcol, index))
2501  {
2502  /* convert to indexkey = TRUE */
2503  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2504  (Expr *) arg,
2505  (Expr *) makeBoolConst(true, false),
2507  }
2508  else if (btest->booltesttype == IS_FALSE &&
2509  match_index_to_operand(arg, indexcol, index))
2510  {
2511  /* convert to indexkey = FALSE */
2512  op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2513  (Expr *) arg,
2514  (Expr *) makeBoolConst(false, false),
2516  }
2517  }
2518 
2519  /*
2520  * If we successfully made an operator clause from the given qual, we must
2521  * wrap it in an IndexClause. It's not lossy.
2522  */
2523  if (op)
2524  {
2525  IndexClause *iclause = makeNode(IndexClause);
2526 
2527  iclause->rinfo = rinfo;
2529  iclause->lossy = false;
2530  iclause->indexcol = indexcol;
2531  iclause->indexcols = NIL;
2532  return iclause;
2533  }
2534 
2535  return NULL;
2536 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3806
Definition: nodes.h:525
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:607
#define list_make1(x1)
Definition: pg_list.h:227
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:355
List * indexcols
Definition: pathnodes.h:1229
AttrNumber indexcol
Definition: pathnodes.h:1228
struct RestrictInfo * rinfo
Definition: pathnodes.h:1225
Expr * arg
Definition: primnodes.h:1228
List * indexquals
Definition: pathnodes.h:1226
Expr * clause
Definition: pathnodes.h:1945
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:112
BoolTestType booltesttype
Definition: primnodes.h:1229
#define InvalidOid
Definition: postgres_ext.h:36
static Expr * get_notclausearg(const void *notclause)
Definition: nodeFuncs.h:121
#define makeNode(_type_)
Definition: nodes.h:573
void * arg
#define make_simple_restrictinfo(clause)
Definition: restrictinfo.h:21

◆ match_clause_to_index()

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

Definition at line 2254 of file indxpath.c.

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

Referenced by match_clauses_to_index(), and match_join_clauses_to_index().

2258 {
2259  int indexcol;
2260 
2261  /*
2262  * Never match pseudoconstants to indexes. (Normally a match could not
2263  * happen anyway, since a pseudoconstant clause couldn't contain a Var,
2264  * but what if someone builds an expression index on a constant? It's not
2265  * totally unreasonable to do so with a partial index, either.)
2266  */
2267  if (rinfo->pseudoconstant)
2268  return;
2269 
2270  /*
2271  * If clause can't be used as an indexqual because it must wait till after
2272  * some lower-security-level restriction clause, reject it.
2273  */
2274  if (!restriction_is_securely_promotable(rinfo, index->rel))
2275  return;
2276 
2277  /* OK, check each index key column for a match */
2278  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
2279  {
2280  IndexClause *iclause;
2281  ListCell *lc;
2282 
2283  /* Ignore duplicates */
2284  foreach(lc, clauseset->indexclauses[indexcol])
2285  {
2286  IndexClause *iclause = (IndexClause *) lfirst(lc);
2287 
2288  if (iclause->rinfo == rinfo)
2289  return;
2290  }
2291 
2292  /* OK, try to match the clause to the index column */
2293  iclause = match_clause_to_indexcol(root,
2294  rinfo,
2295  indexcol,
2296  index);
2297  if (iclause)
2298  {
2299  /* Success, so record it */
2300  clauseset->indexclauses[indexcol] =
2301  lappend(clauseset->indexclauses[indexcol], iclause);
2302  clauseset->nonempty = true;
2303  return;
2304  }
2305  }
2306 }
static IndexClause * match_clause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2373
bool nonempty
Definition: indxpath.c:54
bool pseudoconstant
Definition: pathnodes.h:1953
RelOptInfo * rel
Definition: pathnodes.h:793
struct RestrictInfo * rinfo
Definition: pathnodes.h:1225
List * indexclauses[INDEX_MAX_KEYS]
Definition: indxpath.c:56
List * lappend(List *list, void *datum)
Definition: list.c:322
#define lfirst(lc)
Definition: pg_list.h:190
bool restriction_is_securely_promotable(RestrictInfo *restrictinfo, RelOptInfo *rel)
Definition: restrictinfo.c:376
int nkeycolumns
Definition: pathnodes.h:802

◆ match_clause_to_indexcol()

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

Definition at line 2373 of file indxpath.c.

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

Referenced by match_clause_to_index().

2377 {
2378  IndexClause *iclause;
2379  Expr *clause = rinfo->clause;
2380  Oid opfamily;
2381 
2382  Assert(indexcol < index->nkeycolumns);
2383 
2384  /*
2385  * Historically this code has coped with NULL clauses. That's probably
2386  * not possible anymore, but we might as well continue to cope.
2387  */
2388  if (clause == NULL)
2389  return NULL;
2390 
2391  /* First check for boolean-index cases. */
2392  opfamily = index->opfamily[indexcol];
2393  if (IsBooleanOpfamily(opfamily))
2394  {
2395  iclause = match_boolean_index_clause(rinfo, indexcol, index);
2396  if (iclause)
2397  return iclause;
2398  }
2399 
2400  /*
2401  * Clause must be an opclause, funcclause, ScalarArrayOpExpr, or
2402  * RowCompareExpr. Or, if the index supports it, we can handle IS
2403  * NULL/NOT NULL clauses.
2404  */
2405  if (IsA(clause, OpExpr))
2406  {
2407  return match_opclause_to_indexcol(root, rinfo, indexcol, index);
2408  }
2409  else if (IsA(clause, FuncExpr))
2410  {
2411  return match_funcclause_to_indexcol(root, rinfo, indexcol, index);
2412  }
2413  else if (IsA(clause, ScalarArrayOpExpr))
2414  {
2415  return match_saopclause_to_indexcol(rinfo, indexcol, index);
2416  }
2417  else if (IsA(clause, RowCompareExpr))
2418  {
2419  return match_rowcompare_to_indexcol(rinfo, indexcol, index);
2420  }
2421  else if (index->amsearchnulls && IsA(clause, NullTest))
2422  {
2423  NullTest *nt = (NullTest *) clause;
2424 
2425  if (!nt->argisrow &&
2426  match_index_to_operand((Node *) nt->arg, indexcol, index))
2427  {
2428  iclause = makeNode(IndexClause);
2429  iclause->rinfo = rinfo;
2430  iclause->indexquals = list_make1(rinfo);
2431  iclause->lossy = false;
2432  iclause->indexcol = indexcol;
2433  iclause->indexcols = NIL;
2434  return iclause;
2435  }
2436  }
2437 
2438  return NULL;
2439 }
#define NIL
Definition: pg_list.h:65
static IndexClause * match_boolean_index_clause(RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2458
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3806
Definition: nodes.h:525
static IndexClause * match_funcclause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2663
unsigned int Oid
Definition: postgres_ext.h:31
#define list_make1(x1)
Definition: pg_list.h:227
List * indexcols
Definition: pathnodes.h:1229
Expr * arg
Definition: primnodes.h:1205
AttrNumber indexcol
Definition: pathnodes.h:1228
struct RestrictInfo * rinfo
Definition: pathnodes.h:1225
List * indexquals
Definition: pathnodes.h:1226
static IndexClause * match_opclause_to_indexcol(PlannerInfo *root, RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2544
static IndexClause * match_saopclause_to_indexcol(RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2774
Expr * clause
Definition: pathnodes.h:1945
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
Oid * opfamily
Definition: pathnodes.h:806
bool argisrow
Definition: primnodes.h:1207
bool amsearchnulls
Definition: pathnodes.h:835
static IndexClause * match_rowcompare_to_indexcol(RestrictInfo *rinfo, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2841

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

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

Referenced by match_pathkeys_to_index().

3285 {
3286  Oid opfamily;
3287  Oid idxcollation;
3288  Node *leftop,
3289  *rightop;
3290  Oid expr_op;
3291  Oid expr_coll;
3292  Oid sortfamily;
3293  bool commuted;
3294 
3295  Assert(indexcol < index->nkeycolumns);
3296 
3297  opfamily = index->opfamily[indexcol];
3298  idxcollation = index->indexcollations[indexcol];
3299 
3300  /*
3301  * Clause must be a binary opclause.
3302  */
3303  if (!is_opclause(clause))
3304  return NULL;
3305  leftop = get_leftop(clause);
3306  rightop = get_rightop(clause);
3307  if (!leftop || !rightop)
3308  return NULL;
3309  expr_op = ((OpExpr *) clause)->opno;
3310  expr_coll = ((OpExpr *) clause)->inputcollid;
3311 
3312  /*
3313  * We can forget the whole thing right away if wrong collation.
3314  */
3315  if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
3316  return NULL;
3317 
3318  /*
3319  * Check for clauses of the form: (indexkey operator constant) or
3320  * (constant operator indexkey).
3321  */
3322  if (match_index_to_operand(leftop, indexcol, index) &&
3323  !contain_var_clause(rightop) &&
3324  !contain_volatile_functions(rightop))
3325  {
3326  commuted = false;
3327  }
3328  else if (match_index_to_operand(rightop, indexcol, index) &&
3329  !contain_var_clause(leftop) &&
3330  !contain_volatile_functions(leftop))
3331  {
3332  /* Might match, but we need a commuted operator */
3333  expr_op = get_commutator(expr_op);
3334  if (expr_op == InvalidOid)
3335  return NULL;
3336  commuted = true;
3337  }
3338  else
3339  return NULL;
3340 
3341  /*
3342  * Is the (commuted) operator an ordering operator for the opfamily? And
3343  * if so, does it yield the right sorting semantics?
3344  */
3345  sortfamily = get_op_opfamily_sortfamily(expr_op, opfamily);
3346  if (sortfamily != pk_opfamily)
3347  return NULL;
3348 
3349  /* We have a match. Return clause or a commuted version thereof. */
3350  if (commuted)
3351  {
3352  OpExpr *newclause = makeNode(OpExpr);
3353 
3354  /* flat-copy all the fields of clause */
3355  memcpy(newclause, clause, sizeof(OpExpr));
3356 
3357  /* commute it */
3358  newclause->opno = expr_op;
3359  newclause->opfuncid = InvalidOid;
3360  newclause->args = list_make2(rightop, leftop);
3361 
3362  clause = (Expr *) newclause;
3363  }
3364 
3365  return clause;
3366 }
#define list_make2(x1, x2)
Definition: pg_list.h:229
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1311
Oid * indexcollations
Definition: pathnodes.h:805
Oid get_op_opfamily_sortfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:105
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3806
Definition: nodes.h:525
bool contain_var_clause(Node *node)
Definition: var.c:331
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
unsigned int Oid
Definition: postgres_ext.h:31
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:40
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:70
Oid opfuncid
Definition: primnodes.h:503
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:82
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
Oid * opfamily
Definition: pathnodes.h:806
Oid opno
Definition: primnodes.h:502
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:63
List * args
Definition: primnodes.h:508

◆ match_clauses_to_index()

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

Definition at line 2221 of file indxpath.c.

References lfirst_node, and match_clause_to_index().

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

2225 {
2226  ListCell *lc;
2227 
2228  foreach(lc, clauses)
2229  {
2230  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
2231 
2232  match_clause_to_index(root, rinfo, index, clauseset);
2233  }
2234 }
static void match_clause_to_index(PlannerInfo *root, RestrictInfo *rinfo, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2254
#define lfirst_node(type, lc)
Definition: pg_list.h:193

◆ match_eclass_clauses_to_index()

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

Definition at line 2183 of file indxpath.c.

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

Referenced by create_index_paths().

2185 {
2186  int indexcol;
2187 
2188  /* No work if rel is not in any such ECs */
2189  if (!index->rel->has_eclass_joins)
2190  return;
2191 
2192  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
2193  {
2195  List *clauses;
2196 
2197  /* Generate clauses, skipping any that join to lateral_referencers */
2198  arg.index = index;
2199  arg.indexcol = indexcol;
2201  index->rel,
2203  (void *) &arg,
2204  index->rel->lateral_referencers);
2205 
2206  /*
2207  * We have to check whether the results actually do match the index,
2208  * since for non-btree indexes the EC's equality operators might not
2209  * be in the index opclass (cf ec_member_matches_indexcol).
2210  */
2211  match_clauses_to_index(root, clauses, index, clauseset);
2212  }
2213 }
bool has_eclass_joins
Definition: pathnodes.h:711
static void match_clauses_to_index(PlannerInfo *root, List *clauses, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2221
static bool ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, EquivalenceMember *em, void *arg)
Definition: indxpath.c:3528
RelOptInfo * rel
Definition: pathnodes.h:793
Relids lateral_referencers
Definition: pathnodes.h:679
IndexOptInfo * index
Definition: indxpath.c:72
int nkeycolumns
Definition: pathnodes.h:802
void * arg
Definition: pg_list.h:50
List * generate_implied_equalities_for_column(PlannerInfo *root, RelOptInfo *rel, ec_matches_callback_type callback, void *callback_arg, Relids prohibited_rels)
Definition: equivclass.c:2497

◆ match_funcclause_to_indexcol()

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

Definition at line 2663 of file indxpath.c.

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

Referenced by match_clause_to_indexcol().

2667 {
2668  FuncExpr *clause = (FuncExpr *) rinfo->clause;
2669  int indexarg;
2670  ListCell *lc;
2671 
2672  /*
2673  * We have no built-in intelligence about function clauses, but if there's
2674  * a planner support function, it might be able to do something. But, to
2675  * cut down on wasted planning cycles, only call the support function if
2676  * at least one argument matches the target index column.
2677  *
2678  * Note that we don't insist on the other arguments being pseudoconstants;
2679  * the support function has to check that. This is to allow cases where
2680  * only some of the other arguments need to be included in the indexqual.
2681  */
2682  indexarg = 0;
2683  foreach(lc, clause->args)
2684  {
2685  Node *op = (Node *) lfirst(lc);
2686 
2687  if (match_index_to_operand(op, indexcol, index))
2688  {
2689  return get_index_clause_from_support(root,
2690  rinfo,
2691  clause->funcid,
2692  indexarg,
2693  indexcol,
2694  index);
2695  }
2696 
2697  indexarg++;
2698  }
2699 
2700  return NULL;
2701 }
List * args
Definition: primnodes.h:463
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3806
Definition: nodes.h:525
static IndexClause * get_index_clause_from_support(PlannerInfo *root, RestrictInfo *rinfo, Oid funcid, int indexarg, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2709
Oid funcid
Definition: primnodes.h:455
Expr * clause
Definition: pathnodes.h:1945
#define lfirst(lc)
Definition: pg_list.h:190

◆ match_index_to_operand()

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

Definition at line 3806 of file indxpath.c.

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

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

3809 {
3810  int indkey;
3811 
3812  /*
3813  * Ignore any RelabelType node above the operand. This is needed to be
3814  * able to apply indexscanning in binary-compatible-operator cases. Note:
3815  * we can assume there is at most one RelabelType node;
3816  * eval_const_expressions() will have simplified if more than one.
3817  */
3818  if (operand && IsA(operand, RelabelType))
3819  operand = (Node *) ((RelabelType *) operand)->arg;
3820 
3821  indkey = index->indexkeys[indexcol];
3822  if (indkey != 0)
3823  {
3824  /*
3825  * Simple index column; operand must be a matching Var.
3826  */
3827  if (operand && IsA(operand, Var) &&
3828  index->rel->relid == ((Var *) operand)->varno &&
3829  indkey == ((Var *) operand)->varattno)
3830  return true;
3831  }
3832  else
3833  {
3834  /*
3835  * Index expression; find the correct expression. (This search could
3836  * be avoided, at the cost of complicating all the callers of this
3837  * routine; doesn't seem worth it.)
3838  */
3839  ListCell *indexpr_item;
3840  int i;
3841  Node *indexkey;
3842 
3843  indexpr_item = list_head(index->indexprs);
3844  for (i = 0; i < indexcol; i++)
3845  {
3846  if (index->indexkeys[i] == 0)
3847  {
3848  if (indexpr_item == NULL)
3849  elog(ERROR, "wrong number of index expressions");
3850  indexpr_item = lnext(index->indexprs, indexpr_item);
3851  }
3852  }
3853  if (indexpr_item == NULL)
3854  elog(ERROR, "wrong number of index expressions");
3855  indexkey = (Node *) lfirst(indexpr_item);
3856 
3857  /*
3858  * Does it match the operand? Again, strip any relabeling.
3859  */
3860  if (indexkey && IsA(indexkey, RelabelType))
3861  indexkey = (Node *) ((RelabelType *) indexkey)->arg;
3862 
3863  if (equal(indexkey, operand))
3864  return true;
3865  }
3866 
3867  return false;
3868 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3011
Definition: nodes.h:525
Definition: primnodes.h:167
RelOptInfo * rel
Definition: pathnodes.h:793
#define ERROR
Definition: elog.h:43
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
Index relid
Definition: pathnodes.h:671
#define lfirst(lc)
Definition: pg_list.h:190
#define elog(elevel,...)
Definition: elog.h:228
int i
void * arg
int * indexkeys
Definition: pathnodes.h:803
List * indexprs
Definition: pathnodes.h:815

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

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

Referenced by create_index_paths().

2157 {
2158  ListCell *lc;
2159 
2160  /* Scan the rel's join clauses */
2161  foreach(lc, rel->joininfo)
2162  {
2163  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2164 
2165  /* Check if clause can be moved to this rel */
2166  if (!join_clause_is_movable_to(rinfo, rel))
2167  continue;
2168 
2169  /* Potentially usable, so see if it matches the index or is an OR */
2170  if (restriction_is_or_clause(rinfo))
2171  *joinorclauses = lappend(*joinorclauses, rinfo);
2172  else
2173  match_clause_to_index(root, rinfo, index, clauseset);
2174  }
2175 }
static void match_clause_to_index(PlannerInfo *root, RestrictInfo *rinfo, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2254
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:361
List * joininfo
Definition: pathnodes.h:709
List * lappend(List *list, void *datum)
Definition: list.c:322
#define lfirst(lc)
Definition: pg_list.h:190
bool join_clause_is_movable_to(RestrictInfo *rinfo, RelOptInfo *baserel)
Definition: restrictinfo.c:504

◆ match_opclause_to_indexcol()

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

Definition at line 2544 of file indxpath.c.

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

Referenced by match_clause_to_indexcol().

2548 {
2549  IndexClause *iclause;
2550  OpExpr *clause = (OpExpr *) rinfo->clause;
2551  Node *leftop,
2552  *rightop;
2553  Oid expr_op;
2554  Oid expr_coll;
2555  Index index_relid;
2556  Oid opfamily;
2557  Oid idxcollation;
2558 
2559  /*
2560  * Only binary operators need apply. (In theory, a planner support
2561  * function could do something with a unary operator, but it seems
2562  * unlikely to be worth the cycles to check.)
2563  */
2564  if (list_length(clause->args) != 2)
2565  return NULL;
2566 
2567  leftop = (Node *) linitial(clause->args);
2568  rightop = (Node *) lsecond(clause->args);
2569  expr_op = clause->opno;
2570  expr_coll = clause->inputcollid;
2571 
2572  index_relid = index->rel->relid;
2573  opfamily = index->opfamily[indexcol];
2574  idxcollation = index->indexcollations[indexcol];
2575 
2576  /*
2577  * Check for clauses of the form: (indexkey operator constant) or
2578  * (constant operator indexkey). See match_clause_to_indexcol's notes
2579  * about const-ness.
2580  *
2581  * Note that we don't ask the support function about clauses that don't
2582  * have one of these forms. Again, in principle it might be possible to
2583  * do something, but it seems unlikely to be worth the cycles to check.
2584  */
2585  if (match_index_to_operand(leftop, indexcol, index) &&
2586  !bms_is_member(index_relid, rinfo->right_relids) &&
2587  !contain_volatile_functions(rightop))
2588  {
2589  if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2590  op_in_opfamily(expr_op, opfamily))
2591  {
2592  iclause = makeNode(IndexClause);
2593  iclause->rinfo = rinfo;
2594  iclause->indexquals = list_make1(rinfo);
2595  iclause->lossy = false;
2596  iclause->indexcol = indexcol;
2597  iclause->indexcols = NIL;
2598  return iclause;
2599  }
2600 
2601  /*
2602  * If we didn't find a member of the index's opfamily, try the support
2603  * function for the operator's underlying function.
2604  */
2605  set_opfuncid(clause); /* make sure we have opfuncid */
2606  return get_index_clause_from_support(root,
2607  rinfo,
2608  clause->opfuncid,
2609  0, /* indexarg on left */
2610  indexcol,
2611  index);
2612  }
2613 
2614  if (match_index_to_operand(rightop, indexcol, index) &&
2615  !bms_is_member(index_relid, rinfo->left_relids) &&
2616  !contain_volatile_functions(leftop))
2617  {
2618  if (IndexCollMatchesExprColl(idxcollation, expr_coll))
2619  {
2620  Oid comm_op = get_commutator(expr_op);
2621 
2622  if (OidIsValid(comm_op) &&
2623  op_in_opfamily(comm_op, opfamily))
2624  {
2625  RestrictInfo *commrinfo;
2626 
2627  /* Build a commuted OpExpr and RestrictInfo */
2628  commrinfo = commute_restrictinfo(rinfo, comm_op);
2629 
2630  /* Make an IndexClause showing that as a derived qual */
2631  iclause = makeNode(IndexClause);
2632  iclause->rinfo = rinfo;
2633  iclause->indexquals = list_make1(commrinfo);
2634  iclause->lossy = false;
2635  iclause->indexcol = indexcol;
2636  iclause->indexcols = NIL;
2637  return iclause;
2638  }
2639  }
2640 
2641  /*
2642  * If we didn't find a member of the index's opfamily, try the support
2643  * function for the operator's underlying function.
2644  */
2645  set_opfuncid(clause); /* make sure we have opfuncid */
2646  return get_index_clause_from_support(root,
2647  rinfo,
2648  clause->opfuncid,
2649  1, /* indexarg on right */
2650  indexcol,
2651  index);
2652  }
2653 
2654  return NULL;
2655 }
#define NIL
Definition: pg_list.h:65
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:63
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1311
Oid * indexcollations
Definition: pathnodes.h:805
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3806
Definition: nodes.h:525
static IndexClause * get_index_clause_from_support(PlannerInfo *root, RestrictInfo *rinfo, Oid funcid, int indexarg, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:2709
Relids left_relids
Definition: pathnodes.h:1972
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
#define lsecond(l)
Definition: pg_list.h:200
#define list_make1(x1)
Definition: pg_list.h:227
RelOptInfo * rel
Definition: pathnodes.h:793
#define linitial(l)
Definition: pg_list.h:195
List * indexcols
Definition: pathnodes.h:1229
AttrNumber indexcol
Definition: pathnodes.h:1228
RestrictInfo * commute_restrictinfo(RestrictInfo *rinfo, Oid comm_op)
Definition: restrictinfo.c:306
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:40
struct RestrictInfo * rinfo
Definition: pathnodes.h:1225
List * indexquals
Definition: pathnodes.h:1226
Index relid
Definition: pathnodes.h:671
Expr * clause
Definition: pathnodes.h:1945
Oid opfuncid
Definition: primnodes.h:503
unsigned int Index
Definition: c.h:476
#define makeNode(_type_)
Definition: nodes.h:573
Relids right_relids
Definition: pathnodes.h:1973
static int list_length(const List *l)
Definition: pg_list.h:169
Oid inputcollid
Definition: primnodes.h:507
Oid * opfamily
Definition: pathnodes.h:806
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1618
Oid opno
Definition: primnodes.h:502
List * args
Definition: primnodes.h:508
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ match_pathkeys_to_index()

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

Definition at line 3166 of file indxpath.c.

References IndexOptInfo::amcanorderbyop, bms_equal(), BTLessStrategyNumber, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceMember::em_expr, EquivalenceMember::em_relids, lappend(), lappend_int(), lfirst, match_clause_to_ordering_op(), member, NIL, IndexOptInfo::nkeycolumns, PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, IndexOptInfo::rel, and RelOptInfo::relids.

Referenced by build_index_paths().

3169 {
3170  List *orderby_clauses = NIL;
3171  List *clause_columns = NIL;
3172  ListCell *lc1;
3173 
3174  *orderby_clauses_p = NIL; /* set default results */
3175  *clause_columns_p = NIL;
3176 
3177  /* Only indexes with the amcanorderbyop property are interesting here */
3178  if (!index->amcanorderbyop)
3179  return;
3180 
3181  foreach(lc1, pathkeys)
3182  {
3183  PathKey *pathkey = (PathKey *) lfirst(lc1);
3184  bool found = false;
3185  ListCell *lc2;
3186 
3187  /*
3188  * Note: for any failure to match, we just return NIL immediately.
3189  * There is no value in matching just some of the pathkeys.
3190  */
3191 
3192  /* Pathkey must request default sort order for the target opfamily */
3193  if (pathkey->pk_strategy != BTLessStrategyNumber ||
3194  pathkey->pk_nulls_first)
3195  return;
3196 
3197  /* If eclass is volatile, no hope of using an indexscan */
3198  if (pathkey->pk_eclass->ec_has_volatile)
3199  return;
3200 
3201  /*
3202  * Try to match eclass member expression(s) to index. Note that child
3203  * EC members are considered, but only when they belong to the target
3204  * relation. (Unlike regular members, the same expression could be a
3205  * child member of more than one EC. Therefore, the same index could
3206  * be considered to match more than one pathkey list, which is OK
3207  * here. See also get_eclass_for_sort_expr.)
3208  */
3209  foreach(lc2, pathkey->pk_eclass->ec_members)
3210  {
3212  int indexcol;
3213 
3214  /* No possibility of match if it references other relations */
3215  if (!bms_equal(member->em_relids, index->rel->relids))
3216  continue;
3217 
3218  /*
3219  * We allow any column of the index to match each pathkey; they
3220  * don't have to match left-to-right as you might expect. This is
3221  * correct for GiST, and it doesn't matter for SP-GiST because
3222  * that doesn't handle multiple columns anyway, and no other
3223  * existing AMs support amcanorderbyop. We might need different
3224  * logic in future for other implementations.
3225  */
3226  for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
3227  {
3228  Expr *expr;
3229 
3230  expr = match_clause_to_ordering_op(index,
3231  indexcol,
3232  member->em_expr,
3233  pathkey->pk_opfamily);
3234  if (expr)
3235  {
3236  orderby_clauses = lappend(orderby_clauses, expr);
3237  clause_columns = lappend_int(clause_columns, indexcol);
3238  found = true;
3239  break;
3240  }
3241  }
3242 
3243  if (found) /* don't want to look at remaining members */
3244  break;
3245  }
3246 
3247  if (!found) /* fail if no match for this pathkey */
3248  return;
3249  }
3250 
3251  *orderby_clauses_p = orderby_clauses; /* success! */
3252  *clause_columns_p = clause_columns;
3253 }
#define NIL
Definition: pg_list.h:65
static Expr * match_clause_to_ordering_op(IndexOptInfo *index, int indexcol, Expr *clause, Oid pk_opfamily)
Definition: indxpath.c:3281
int pk_strategy
Definition: pathnodes.h:1015
Oid member
RelOptInfo * rel
Definition: pathnodes.h:793
bool pk_nulls_first
Definition: pathnodes.h:1016
bool amcanorderbyop
Definition: pathnodes.h:832
Relids relids
Definition: pathnodes.h:643
List * lappend_int(List *list, int datum)
Definition: list.c:340
List * lappend(List *list, void *datum)
Definition: list.c:322
#define lfirst(lc)
Definition: pg_list.h:190
EquivalenceClass * pk_eclass
Definition: pathnodes.h:1013
bool ec_has_volatile
Definition: pathnodes.h:942
int nkeycolumns
Definition: pathnodes.h:802
Oid pk_opfamily
Definition: pathnodes.h:1014
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:50
List * ec_members
Definition: pathnodes.h:936
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ match_restriction_clauses_to_index()

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

Definition at line 2138 of file indxpath.c.

References IndexOptInfo::indrestrictinfo, and match_clauses_to_index().

Referenced by create_index_paths().

2141 {
2142  /* We can ignore clauses that are implied by the index predicate */
2143  match_clauses_to_index(root, index->indrestrictinfo, index, clauseset);
2144 }
static void match_clauses_to_index(PlannerInfo *root, List *clauses, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2221
List * indrestrictinfo
Definition: pathnodes.h:820

◆ match_rowcompare_to_indexcol()

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

Definition at line 2841 of file indxpath.c.

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

Referenced by match_clause_to_indexcol().

2844 {
2845  RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
2846  Index index_relid;
2847  Oid opfamily;
2848  Oid idxcollation;
2849  Node *leftop,
2850  *rightop;
2851  bool var_on_left;
2852  Oid expr_op;
2853  Oid expr_coll;
2854 
2855  /* Forget it if we're not dealing with a btree index */
2856  if (index->relam != BTREE_AM_OID)
2857  return NULL;
2858 
2859  index_relid = index->rel->relid;
2860  opfamily = index->opfamily[indexcol];
2861  idxcollation = index->indexcollations[indexcol];
2862 
2863  /*
2864  * We could do the matching on the basis of insisting that the opfamily
2865  * shown in the RowCompareExpr be the same as the index column's opfamily,
2866  * but that could fail in the presence of reverse-sort opfamilies: it'd be
2867  * a matter of chance whether RowCompareExpr had picked the forward or
2868  * reverse-sort family. So look only at the operator, and match if it is
2869  * a member of the index's opfamily (after commutation, if the indexkey is
2870  * on the right). We'll worry later about whether any additional
2871  * operators are matchable to the index.
2872  */
2873  leftop = (Node *) linitial(clause->largs);
2874  rightop = (Node *) linitial(clause->rargs);
2875  expr_op = linitial_oid(clause->opnos);
2876  expr_coll = linitial_oid(clause->inputcollids);
2877 
2878  /* Collations must match, if relevant */
2879  if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
2880  return NULL;
2881 
2882  /*
2883  * These syntactic tests are the same as in match_opclause_to_indexcol()
2884  */
2885  if (match_index_to_operand(leftop, indexcol, index) &&
2886  !bms_is_member(index_relid, pull_varnos(rightop)) &&
2887  !contain_volatile_functions(rightop))
2888  {
2889  /* OK, indexkey is on left */
2890  var_on_left = true;
2891  }
2892  else if (match_index_to_operand(rightop, indexcol, index) &&
2893  !bms_is_member(index_relid, pull_varnos(leftop)) &&
2894  !contain_volatile_functions(leftop))
2895  {
2896  /* indexkey is on right, so commute the operator */
2897  expr_op = get_commutator(expr_op);
2898  if (expr_op == InvalidOid)
2899  return NULL;
2900  var_on_left = false;
2901  }
2902  else
2903  return NULL;
2904 
2905  /* We're good if the operator is the right type of opfamily member */
2906  switch (get_op_opfamily_strategy(expr_op, opfamily))
2907  {
2908  case BTLessStrategyNumber:
2912  return expand_indexqual_rowcompare(rinfo,
2913  indexcol,
2914  index,
2915  expr_op,
2916  var_on_left);
2917  }
2918 
2919  return NULL;
2920 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1311
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
Oid * indexcollations
Definition: pathnodes.h:805
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3806
Definition: nodes.h:525
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
unsigned int Oid
Definition: postgres_ext.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
RelOptInfo * rel
Definition: pathnodes.h:793
#define linitial(l)
Definition: pg_list.h:195
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:40
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: pathnodes.h:671
Expr * clause
Definition: pathnodes.h:1945
static IndexClause * expand_indexqual_rowcompare(RestrictInfo *rinfo, int indexcol, IndexOptInfo *index, Oid expr_op, bool var_on_left)
Definition: indxpath.c:2946
unsigned int Index
Definition: c.h:476
#define InvalidOid
Definition: postgres_ext.h:36
#define linitial_oid(l)
Definition: pg_list.h:197
Oid * opfamily
Definition: pathnodes.h:806
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Definition: lsyscache.c:80
List * inputcollids
Definition: primnodes.h:1059
#define BTLessStrategyNumber
Definition: stratnum.h:29
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32

◆ match_saopclause_to_indexcol()

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

Definition at line 2774 of file indxpath.c.

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

Referenced by match_clause_to_indexcol().

2777 {
2778  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
2779  Node *leftop,
2780  *rightop;
2781  Relids right_relids;
2782  Oid expr_op;
2783  Oid expr_coll;
2784  Index index_relid;
2785  Oid opfamily;
2786  Oid idxcollation;
2787 
2788  /* We only accept ANY clauses, not ALL */
2789  if (!saop->useOr)
2790  return NULL;
2791  leftop = (Node *) linitial(saop->args);
2792  rightop = (Node *) lsecond(saop->args);
2793  right_relids = pull_varnos(rightop);
2794  expr_op = saop->opno;
2795  expr_coll = saop->inputcollid;
2796 
2797  index_relid = index->rel->relid;
2798  opfamily = index->opfamily[indexcol];
2799  idxcollation = index->indexcollations[indexcol];
2800 
2801  /*
2802  * We must have indexkey on the left and a pseudo-constant array argument.
2803  */
2804  if (match_index_to_operand(leftop, indexcol, index) &&
2805  !bms_is_member(index_relid, right_relids) &&
2806  !contain_volatile_functions(rightop))
2807  {
2808  if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2809  op_in_opfamily(expr_op, opfamily))
2810  {
2811  IndexClause *iclause = makeNode(IndexClause);
2812 
2813  iclause->rinfo = rinfo;
2814  iclause->indexquals = list_make1(rinfo);
2815  iclause->lossy = false;
2816  iclause->indexcol = indexcol;
2817  iclause->indexcols = NIL;
2818  return iclause;
2819  }
2820 
2821  /*
2822  * We do not currently ask support functions about ScalarArrayOpExprs,
2823  * though in principle we could.
2824  */
2825  }
2826 
2827  return NULL;
2828 }
#define NIL
Definition: pg_list.h:65
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:63
Oid * indexcollations
Definition: pathnodes.h:805
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3806
Definition: nodes.h:525
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
unsigned int Oid
Definition: postgres_ext.h:31
#define lsecond(l)
Definition: pg_list.h:200
#define list_make1(x1)
Definition: pg_list.h:227
RelOptInfo * rel
Definition: pathnodes.h:793
#define linitial(l)
Definition: pg_list.h:195
List * indexcols
Definition: pathnodes.h:1229
AttrNumber indexcol
Definition: pathnodes.h:1228
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:40
struct RestrictInfo * rinfo
Definition: pathnodes.h:1225
List * indexquals
Definition: pathnodes.h:1226
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: pathnodes.h:671
Expr * clause
Definition: pathnodes.h:1945
unsigned int Index
Definition: c.h:476
#define makeNode(_type_)
Definition: nodes.h:573
Oid * opfamily
Definition: pathnodes.h:806
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ path_usage_comparator()

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

Definition at line 1574 of file indxpath.c.

References cost_bitmap_tree_node(), and PathClauseUsage::path.

Referenced by choose_bitmap_and().

1575 {
1576  PathClauseUsage *pa = *(PathClauseUsage *const *) a;
1577  PathClauseUsage *pb = *(PathClauseUsage *const *) b;
1578  Cost acost;
1579  Cost bcost;
1580  Selectivity aselec;
1581  Selectivity bselec;
1582 
1583  cost_bitmap_tree_node(pa->path, &acost, &aselec);
1584  cost_bitmap_tree_node(pb->path, &bcost, &bselec);
1585 
1586  /*
1587  * If costs are the same, sort by selectivity.
1588  */
1589  if (acost < bcost)
1590  return -1;
1591  if (acost > bcost)
1592  return 1;
1593 
1594  if (aselec < bselec)
1595  return -1;
1596  if (aselec > bselec)
1597  return 1;
1598 
1599  return 0;
1600 }
Path * path
Definition: indxpath.c:62
double Selectivity
Definition: nodes.h:658
double Cost
Definition: nodes.h:659
void cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec)
Definition: costsize.c:1041

◆ relation_has_unique_index_for()

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

Definition at line 3586 of file indxpath.c.

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

Referenced by create_unique_path(), and rel_is_distinct_for().

3589 {
3590  ListCell *ic;
3591 
3592  Assert(list_length(exprlist) == list_length(oprlist));
3593 
3594  /* Short-circuit if no indexes... */
3595  if (rel->indexlist == NIL)
3596  return false;
3597 
3598  /*
3599  * Examine the rel's restriction clauses for usable var = const clauses
3600  * that we can add to the restrictlist.
3601  */
3602  foreach(ic, rel->baserestrictinfo)
3603  {
3604  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(ic);
3605 
3606  /*
3607  * Note: can_join won't be set for a restriction clause, but
3608  * mergeopfamilies will be if it has a mergejoinable operator and
3609  * doesn't contain volatile functions.
3610  */
3611  if (restrictinfo->mergeopfamilies == NIL)
3612  continue; /* not mergejoinable */
3613 
3614  /*
3615  * The clause certainly doesn't refer to anything but the given rel.
3616  * If either side is pseudoconstant then we can use it.
3617  */
3618  if (bms_is_empty(restrictinfo->left_relids))
3619  {
3620  /* righthand side is inner */
3621  restrictinfo->outer_is_left = true;
3622  }
3623  else if (bms_is_empty(restrictinfo->right_relids))
3624  {
3625  /* lefthand side is inner */
3626  restrictinfo->outer_is_left = false;
3627  }
3628  else
3629  continue;
3630 
3631  /* OK, add to list */
3632  restrictlist = lappend(restrictlist, restrictinfo);
3633  }
3634 
3635  /* Short-circuit the easy case */
3636  if (restrictlist == NIL && exprlist == NIL)
3637  return false;
3638 
3639  /* Examine each index of the relation ... */
3640  foreach(ic, rel->indexlist)
3641  {
3642  IndexOptInfo *ind = (IndexOptInfo *) lfirst(ic);
3643  int c;
3644 
3645  /*
3646  * If the index is not unique, or not immediately enforced, or if it's
3647  * a partial index that doesn't match the query, it's useless here.
3648  */
3649  if (!ind->unique || !ind->immediate ||
3650  (ind->indpred != NIL && !ind->predOK))
3651  continue;
3652 
3653  /*
3654  * Try to find each index column in the lists of conditions. This is
3655  * O(N^2) or worse, but we expect all the lists to be short.
3656  */
3657  for (c = 0; c < ind->nkeycolumns; c++)
3658  {
3659  bool matched = false;
3660  ListCell *lc;
3661  ListCell *lc2;
3662 
3663  foreach(lc, restrictlist)
3664  {
3665  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3666  Node *rexpr;
3667 
3668  /*
3669  * The condition's equality operator must be a member of the
3670  * index opfamily, else it is not asserting the right kind of
3671  * equality behavior for this index. We check this first
3672  * since it's probably cheaper than match_index_to_operand().
3673  */
3674  if (!list_member_oid(rinfo->mergeopfamilies, ind->opfamily[c]))
3675  continue;
3676 
3677  /*
3678  * XXX at some point we may need to check collations here too.
3679  * For the moment we assume all collations reduce to the same
3680  * notion of equality.
3681  */
3682 
3683  /* OK, see if the condition operand matches the index key */
3684  if (rinfo->outer_is_left)
3685  rexpr = get_rightop(rinfo->clause);
3686  else
3687  rexpr = get_leftop(rinfo->clause);
3688 
3689  if (match_index_to_operand(rexpr, c, ind))
3690  {
3691  matched = true; /* column is unique */
3692  break;
3693  }
3694  }
3695 
3696  if (matched)
3697  continue;
3698 
3699  forboth(lc, exprlist, lc2, oprlist)
3700  {
3701  Node *expr = (Node *) lfirst(lc);
3702  Oid opr = lfirst_oid(lc2);
3703 
3704  /* See if the expression matches the index key */
3705  if (!match_index_to_operand(expr, c, ind))
3706  continue;
3707 
3708  /*
3709  * The equality operator must be a member of the index
3710  * opfamily, else it is not asserting the right kind of
3711  * equality behavior for this index. We assume the caller
3712  * determined it is an equality operator, so we don't need to
3713  * check any more tightly than this.
3714  */
3715  if (!op_in_opfamily(opr, ind->opfamily[c]))
3716  continue;
3717 
3718  /*
3719  * XXX at some point we may need to check collations here too.
3720  * For the moment we assume all collations reduce to the same
3721  * notion of equality.
3722  */
3723 
3724  matched = true; /* column is unique */
3725  break;
3726  }
3727 
3728  if (!matched)
3729  break; /* no match; this index doesn't help us */
3730  }
3731 
3732  /* Matched all key columns of this index? */
3733  if (c == ind->nkeycolumns)
3734  return true;
3735  }
3736 
3737  return false;
3738 }
#define NIL
Definition: pg_list.h:65
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:63
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3806
List * baserestrictinfo
Definition: pathnodes.h:705
Definition: nodes.h:525
Relids left_relids
Definition: pathnodes.h:1972
bool immediate
Definition: pathnodes.h:828
unsigned int Oid
Definition: postgres_ext.h:31
List * mergeopfamilies
Definition: pathnodes.h:1990
char * c
bool outer_is_left
Definition: pathnodes.h:2000
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:70
List * lappend(List *list, void *datum)
Definition: list.c:322
Expr * clause
Definition: pathnodes.h:1945
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
List * indexlist
Definition: pathnodes.h:680
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:82
Relids right_relids
Definition: pathnodes.h:1973
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:675
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
int nkeycolumns
Definition: pathnodes.h:802
Oid * opfamily
Definition: pathnodes.h:806
List * indpred
Definition: pathnodes.h:816
#define lfirst_oid(lc)
Definition: pg_list.h:192