PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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_collation.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_opfamily.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/predtest.h"
#include "optimizer/prep.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/var.h"
#include "utils/builtins.h"
#include "utils/bytea.h"
#include "utils/lsyscache.h"
#include "utils/pg_locale.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 IsBooleanOpfamily(opfamily)   ((opfamily) == BOOL_BTREE_FAM_OID || (opfamily) == BOOL_HASH_FAM_OID)
 
#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 (RelOptInfo *rel, 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 (IndexOptInfo *index, List *clauses, IndexClauseSet *clauseset)
 
static void match_clause_to_index (IndexOptInfo *index, RestrictInfo *rinfo, IndexClauseSet *clauseset)
 
static bool match_clause_to_indexcol (IndexOptInfo *index, int indexcol, RestrictInfo *rinfo)
 
static bool is_indexable_operator (Oid expr_op, Oid opfamily, bool indexkey_on_left)
 
static bool match_rowcompare_to_indexcol (IndexOptInfo *index, int indexcol, Oid opfamily, Oid idxcollation, RowCompareExpr *clause)
 
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)
 
static bool match_boolean_index_clause (Node *clause, int indexcol, IndexOptInfo *index)
 
static bool match_special_index_operator (Expr *clause, Oid opfamily, Oid idxcollation, bool indexkey_on_left)
 
static Exprexpand_boolean_index_clause (Node *clause, int indexcol, IndexOptInfo *index)
 
static Listexpand_indexqual_opclause (RestrictInfo *rinfo, Oid opfamily, Oid idxcollation)
 
static RestrictInfoexpand_indexqual_rowcompare (RestrictInfo *rinfo, IndexOptInfo *index, int indexcol)
 
static Listprefix_quals (Node *leftop, Oid opfamily, Oid collation, Const *prefix, Pattern_Prefix_Status pstatus)
 
static Listnetwork_prefix_quals (Node *leftop, Oid expr_op, Oid opfamily, Datum rightop)
 
static Datum string_to_datum (const char *str, Oid datatype)
 
static Conststring_to_const (const char *str, Oid datatype)
 
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)
 
void expand_indexqual_conditions (IndexOptInfo *index, List *indexclauses, List *indexclausecols, List **indexquals_p, List **indexqualcols_p)
 
Expradjust_rowcompare_for_index (RowCompareExpr *clause, IndexOptInfo *index, int indexcol, List **indexcolnos, bool *var_on_left_p)
 

Macro Definition Documentation

#define IndexCollMatchesExprColl (   idxcollation,
  exprcollation 
)    ((idxcollation) == InvalidOid || (idxcollation) == (exprcollation))
#define IsBooleanOpfamily (   opfamily)    ((opfamily) == BOOL_BTREE_FAM_OID || (opfamily) == BOOL_HASH_FAM_OID)

Enumeration Type Documentation

Enumerator
ST_INDEXSCAN 
ST_BITMAPSCAN 
ST_ANYSCAN 

Definition at line 50 of file indxpath.c.

51 {
52  ST_INDEXSCAN, /* must support amgettuple */
53  ST_BITMAPSCAN, /* must support amgetbitmap */
54  ST_ANYSCAN /* either is okay */
ScanTypeControl
Definition: indxpath.c:50

Function Documentation

Expr* adjust_rowcompare_for_index ( RowCompareExpr clause,
IndexOptInfo index,
int  indexcol,
List **  indexcolnos,
bool var_on_left_p 
)

Definition at line 3805 of file indxpath.c.

References Assert, bms_is_member(), BOOLOID, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, contain_volatile_functions(), copyObject(), elog, ERROR, get_commutator(), get_op_opfamily_properties(), get_op_opfamily_strategy(), get_opfamily_member(), i, IndexOptInfo::indexcollations, IndexCollMatchesExprColl, RowCompareExpr::inputcollids, InvalidOid, lappend_int(), lappend_oid(), RowCompareExpr::largs, lfirst, lfirst_oid, linitial, linitial_oid, list_copy(), list_head(), list_length(), list_make1_int, list_make1_oid, list_truncate(), lnext, make_opclause(), makeNode, match_index_to_operand(), IndexOptInfo::ncolumns, NIL, NULL, OidIsValid, RowCompareExpr::opfamilies, IndexOptInfo::opfamily, RowCompareExpr::opnos, pull_varnos(), RowCompareExpr::rargs, RowCompareExpr::rctype, IndexOptInfo::rel, RelOptInfo::relid, ROWCOMPARE_GE, and ROWCOMPARE_LE.

Referenced by expand_indexqual_rowcompare(), and fix_indexqual_references().

3810 {
3811  bool var_on_left;
3812  int op_strategy;
3813  Oid op_lefttype;
3814  Oid op_righttype;
3815  int matching_cols;
3816  Oid expr_op;
3817  List *opfamilies;
3818  List *lefttypes;
3819  List *righttypes;
3820  List *new_ops;
3821  ListCell *largs_cell;
3822  ListCell *rargs_cell;
3823  ListCell *opnos_cell;
3824  ListCell *collids_cell;
3825 
3826  /* We have to figure out (again) how the first col matches */
3827  var_on_left = match_index_to_operand((Node *) linitial(clause->largs),
3828  indexcol, index);
3829  Assert(var_on_left ||
3831  indexcol, index));
3832  *var_on_left_p = var_on_left;
3833 
3834  expr_op = linitial_oid(clause->opnos);
3835  if (!var_on_left)
3836  expr_op = get_commutator(expr_op);
3837  get_op_opfamily_properties(expr_op, index->opfamily[indexcol], false,
3838  &op_strategy,
3839  &op_lefttype,
3840  &op_righttype);
3841 
3842  /* Initialize returned list of which index columns are used */
3843  *indexcolnos = list_make1_int(indexcol);
3844 
3845  /* Build lists of the opfamilies and operator datatypes in case needed */
3846  opfamilies = list_make1_oid(index->opfamily[indexcol]);
3847  lefttypes = list_make1_oid(op_lefttype);
3848  righttypes = list_make1_oid(op_righttype);
3849 
3850  /*
3851  * See how many of the remaining columns match some index column in the
3852  * same way. As in match_clause_to_indexcol(), the "other" side of any
3853  * potential index condition is OK as long as it doesn't use Vars from the
3854  * indexed relation.
3855  */
3856  matching_cols = 1;
3857  largs_cell = lnext(list_head(clause->largs));
3858  rargs_cell = lnext(list_head(clause->rargs));
3859  opnos_cell = lnext(list_head(clause->opnos));
3860  collids_cell = lnext(list_head(clause->inputcollids));
3861 
3862  while (largs_cell != NULL)
3863  {
3864  Node *varop;
3865  Node *constop;
3866  int i;
3867 
3868  expr_op = lfirst_oid(opnos_cell);
3869  if (var_on_left)
3870  {
3871  varop = (Node *) lfirst(largs_cell);
3872  constop = (Node *) lfirst(rargs_cell);
3873  }
3874  else
3875  {
3876  varop = (Node *) lfirst(rargs_cell);
3877  constop = (Node *) lfirst(largs_cell);
3878  /* indexkey is on right, so commute the operator */
3879  expr_op = get_commutator(expr_op);
3880  if (expr_op == InvalidOid)
3881  break; /* operator is not usable */
3882  }
3883  if (bms_is_member(index->rel->relid, pull_varnos(constop)))
3884  break; /* no good, Var on wrong side */
3885  if (contain_volatile_functions(constop))
3886  break; /* no good, volatile comparison value */
3887 
3888  /*
3889  * The Var side can match any column of the index.
3890  */
3891  for (i = 0; i < index->ncolumns; i++)
3892  {
3893  if (match_index_to_operand(varop, i, index) &&
3894  get_op_opfamily_strategy(expr_op,
3895  index->opfamily[i]) == op_strategy &&
3897  lfirst_oid(collids_cell)))
3898  break;
3899  }
3900  if (i >= index->ncolumns)
3901  break; /* no match found */
3902 
3903  /* Add column number to returned list */
3904  *indexcolnos = lappend_int(*indexcolnos, i);
3905 
3906  /* Add opfamily and datatypes to lists */
3907  get_op_opfamily_properties(expr_op, index->opfamily[i], false,
3908  &op_strategy,
3909  &op_lefttype,
3910  &op_righttype);
3911  opfamilies = lappend_oid(opfamilies, index->opfamily[i]);
3912  lefttypes = lappend_oid(lefttypes, op_lefttype);
3913  righttypes = lappend_oid(righttypes, op_righttype);
3914 
3915  /* This column matches, keep scanning */
3916  matching_cols++;
3917  largs_cell = lnext(largs_cell);
3918  rargs_cell = lnext(rargs_cell);
3919  opnos_cell = lnext(opnos_cell);
3920  collids_cell = lnext(collids_cell);
3921  }
3922 
3923  /* Return clause as-is if it's all usable as index quals */
3924  if (matching_cols == list_length(clause->opnos))
3925  return (Expr *) clause;
3926 
3927  /*
3928  * We have to generate a subset rowcompare (possibly just one OpExpr). The
3929  * painful part of this is changing < to <= or > to >=, so deal with that
3930  * first.
3931  */
3932  if (op_strategy == BTLessEqualStrategyNumber ||
3933  op_strategy == BTGreaterEqualStrategyNumber)
3934  {
3935  /* easy, just use the same operators */
3936  new_ops = list_truncate(list_copy(clause->opnos), matching_cols);
3937  }
3938  else
3939  {
3940  ListCell *opfamilies_cell;
3941  ListCell *lefttypes_cell;
3942  ListCell *righttypes_cell;
3943 
3944  if (op_strategy == BTLessStrategyNumber)
3945  op_strategy = BTLessEqualStrategyNumber;
3946  else if (op_strategy == BTGreaterStrategyNumber)
3947  op_strategy = BTGreaterEqualStrategyNumber;
3948  else
3949  elog(ERROR, "unexpected strategy number %d", op_strategy);
3950  new_ops = NIL;
3951  lefttypes_cell = list_head(lefttypes);
3952  righttypes_cell = list_head(righttypes);
3953  foreach(opfamilies_cell, opfamilies)
3954  {
3955  Oid opfam = lfirst_oid(opfamilies_cell);
3956  Oid lefttype = lfirst_oid(lefttypes_cell);
3957  Oid righttype = lfirst_oid(righttypes_cell);
3958 
3959  expr_op = get_opfamily_member(opfam, lefttype, righttype,
3960  op_strategy);
3961  if (!OidIsValid(expr_op)) /* should not happen */
3962  elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
3963  op_strategy, lefttype, righttype, opfam);
3964  if (!var_on_left)
3965  {
3966  expr_op = get_commutator(expr_op);
3967  if (!OidIsValid(expr_op)) /* should not happen */
3968  elog(ERROR, "could not find commutator of member %d(%u,%u) of opfamily %u",
3969  op_strategy, lefttype, righttype, opfam);
3970  }
3971  new_ops = lappend_oid(new_ops, expr_op);
3972  lefttypes_cell = lnext(lefttypes_cell);
3973  righttypes_cell = lnext(righttypes_cell);
3974  }
3975  }
3976 
3977  /* If we have more than one matching col, create a subset rowcompare */
3978  if (matching_cols > 1)
3979  {
3981 
3982  if (var_on_left)
3983  rc->rctype = (RowCompareType) op_strategy;
3984  else
3985  rc->rctype = (op_strategy == BTLessEqualStrategyNumber) ?
3987  rc->opnos = new_ops;
3989  matching_cols);
3991  matching_cols);
3992  rc->largs = list_truncate((List *) copyObject(clause->largs),
3993  matching_cols);
3994  rc->rargs = list_truncate((List *) copyObject(clause->rargs),
3995  matching_cols);
3996  return (Expr *) rc;
3997  }
3998  else
3999  {
4000  return make_opclause(linitial_oid(new_ops), BOOLOID, false,
4001  copyObject(linitial(clause->largs)),
4002  copyObject(linitial(clause->rargs)),
4003  InvalidOid,
4004  linitial_oid(clause->inputcollids));
4005  }
4006 }
#define NIL
Definition: pg_list.h:69
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1281
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
Oid * indexcollations
Definition: relation.h:600
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3161
RowCompareType rctype
Definition: primnodes.h:1007
List * opfamilies
Definition: primnodes.h:1009
List * list_truncate(List *list, int new_size)
Definition: list.c:350
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:508
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: clauses.c:171
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:950
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
#define OidIsValid(objectId)
Definition: c.h:534
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
void * copyObject(const void *from)
Definition: copyfuncs.c:4475
RelOptInfo * rel
Definition: relation.h:590
#define linitial(l)
Definition: pg_list.h:110
#define ERROR
Definition: elog.h:43
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:46
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define list_make1_int(x1)
Definition: pg_list.h:139
int ncolumns
Definition: relation.h:598
#define lnext(lc)
Definition: pg_list.h:105
Relids pull_varnos(Node *node)
Definition: var.c:95
List * lappend_int(List *list, int datum)
Definition: list.c:146
Index relid
Definition: relation.h:518
#define list_make1_oid(x1)
Definition: pg_list.h:145
#define InvalidOid
Definition: postgres_ext.h:36
RowCompareType
Definition: primnodes.h:993
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
#define linitial_oid(l)
Definition: pg_list.h:112
static int list_length(const List *l)
Definition: pg_list.h:89
#define BOOLOID
Definition: pg_type.h:288
Oid * opfamily
Definition: relation.h:601
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
int i
#define elog
Definition: elog.h:219
List * inputcollids
Definition: primnodes.h:1010
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
#define lfirst_oid(lc)
Definition: pg_list.h:108
static double adjust_rowcount_for_semijoins ( PlannerInfo root,
Index  cur_relid,
Index  outer_relid,
double  rowcount 
)
static

Definition at line 2001 of file indxpath.c.

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

Referenced by get_loop_count().

2005 {
2006  ListCell *lc;
2007 
2008  foreach(lc, root->join_info_list)
2009  {
2010  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
2011 
2012  if (sjinfo->jointype == JOIN_SEMI &&
2013  bms_is_member(cur_relid, sjinfo->syn_lefthand) &&
2014  bms_is_member(outer_relid, sjinfo->syn_righthand))
2015  {
2016  /* Estimate number of unique-ified rows */
2017  double nraw;
2018  double nunique;
2019 
2020  nraw = approximate_joinrel_size(root, sjinfo->syn_righthand);
2021  nunique = estimate_num_groups(root,
2022  sjinfo->semi_rhs_exprs,
2023  nraw,
2024  NULL);
2025  if (rowcount > nunique)
2026  rowcount = nunique;
2027  }
2028  }
2029  return rowcount;
2030 }
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset)
Definition: selfuncs.c:3272
List * join_info_list
Definition: relation.h:247
static double approximate_joinrel_size(PlannerInfo *root, Relids relids)
Definition: indxpath.c:2044
Relids syn_lefthand
Definition: relation.h:1809
Relids syn_righthand
Definition: relation.h:1810
List * semi_rhs_exprs
Definition: relation.h:1818
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
JoinType jointype
Definition: relation.h:1811
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
static double approximate_joinrel_size ( PlannerInfo root,
Relids  relids 
)
static

Definition at line 2044 of file indxpath.c.

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

Referenced by adjust_rowcount_for_semijoins().

2045 {
2046  double rowcount = 1.0;
2047  int relid;
2048 
2049  relid = -1;
2050  while ((relid = bms_next_member(relids, relid)) >= 0)
2051  {
2052  RelOptInfo *rel;
2053 
2054  /* Paranoia: ignore bogus relid indexes */
2055  if (relid >= root->simple_rel_array_size)
2056  continue;
2057  rel = root->simple_rel_array[relid];
2058  if (rel == NULL)
2059  continue;
2060  Assert(rel->relid == relid); /* sanity check on array */
2061 
2062  /* Relation could be proven empty, if so ignore */
2063  if (IS_DUMMY_REL(rel))
2064  continue;
2065 
2066  /* Otherwise, rel's rows estimate should be valid by now */
2067  Assert(rel->rows > 0);
2068 
2069  /* Accumulate product */
2070  rowcount *= rel->rows;
2071  }
2072  return rowcount;
2073 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:907
struct RelOptInfo ** simple_rel_array
Definition: relation.h:176
#define IS_DUMMY_REL(r)
Definition: relation.h:1126
int simple_rel_array_size
Definition: relation.h:177
Index relid
Definition: relation.h:518
double rows
Definition: relation.h:493
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static Cost bitmap_and_cost_est ( PlannerInfo root,
RelOptInfo rel,
List paths 
)
static

Definition at line 1633 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, NULL, 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().

1634 {
1635  BitmapAndPath apath;
1636  BitmapHeapPath bpath;
1637  Relids required_outer;
1638 
1639  /* Set up a dummy BitmapAndPath */
1640  apath.path.type = T_BitmapAndPath;
1641  apath.path.pathtype = T_BitmapAnd;
1642  apath.path.parent = rel;
1643  apath.path.pathtarget = rel->reltarget;
1644  apath.path.param_info = NULL; /* not used in bitmap trees */
1645  apath.path.pathkeys = NIL;
1646  apath.bitmapquals = paths;
1647  cost_bitmap_and_node(&apath, root);
1648 
1649  /* Identify required outer rels, in case it's a parameterized scan */
1650  required_outer = get_bitmap_tree_required_outer((Path *) &apath);
1651 
1652  /* Set up a dummy BitmapHeapPath */
1653  bpath.path.type = T_BitmapHeapPath;
1654  bpath.path.pathtype = T_BitmapHeapScan;
1655  bpath.path.parent = rel;
1656  bpath.path.pathtarget = rel->reltarget;
1657  bpath.path.param_info = get_baserel_parampathinfo(root, rel,
1658  required_outer);
1659  bpath.path.pathkeys = NIL;
1660  bpath.bitmapqual = (Path *) &apath;
1661 
1662  /* Now we can do cost_bitmap_heap_scan */
1663  cost_bitmap_heap_scan(&bpath.path, root, rel,
1664  bpath.path.param_info,
1665  (Path *) &apath,
1666  get_loop_count(root, rel->relid, required_outer));
1667 
1668  return bpath.path.total_cost;
1669 }
#define NIL
Definition: pg_list.h:69
PathTarget * pathtarget
Definition: relation.h:895
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:853
ParamPathInfo * get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
Definition: relnode.c:1010
NodeTag type
Definition: relation.h:890
void cost_bitmap_and_node(BitmapAndPath *path, PlannerInfo *root)
Definition: costsize.c:983
ParamPathInfo * param_info
Definition: relation.h:897
List * bitmapquals
Definition: relation.h:1015
NodeTag pathtype
Definition: relation.h:892
RelOptInfo * parent
Definition: relation.h:894
Path * bitmapqual
Definition: relation.h:1003
Index relid
Definition: relation.h:518
static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
Definition: indxpath.c:1948
Cost total_cost
Definition: relation.h:907
List * pathkeys
Definition: relation.h:909
#define NULL
Definition: c.h:226
static Relids get_bitmap_tree_required_outer(Path *bitmapqual)
Definition: indxpath.c:1734
struct PathTarget * reltarget
Definition: relation.h:501
Definition: relation.h:888
static Cost bitmap_scan_cost_est ( PlannerInfo root,
RelOptInfo rel,
Path ipath 
)
static

Definition at line 1602 of file indxpath.c.

References BitmapHeapPath::bitmapqual, cost_bitmap_heap_scan(), get_baserel_parampathinfo(), get_bitmap_tree_required_outer(), get_loop_count(), NIL, 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().

1603 {
1604  BitmapHeapPath bpath;
1605  Relids required_outer;
1606 
1607  /* Identify required outer rels, in case it's a parameterized scan */
1608  required_outer = get_bitmap_tree_required_outer(ipath);
1609 
1610  /* Set up a dummy BitmapHeapPath */
1611  bpath.path.type = T_BitmapHeapPath;
1612  bpath.path.pathtype = T_BitmapHeapScan;
1613  bpath.path.parent = rel;
1614  bpath.path.pathtarget = rel->reltarget;
1615  bpath.path.param_info = get_baserel_parampathinfo(root, rel,
1616  required_outer);
1617  bpath.path.pathkeys = NIL;
1618  bpath.bitmapqual = ipath;
1619 
1620  cost_bitmap_heap_scan(&bpath.path, root, rel,
1621  bpath.path.param_info,
1622  ipath,
1623  get_loop_count(root, rel->relid, required_outer));
1624 
1625  return bpath.path.total_cost;
1626 }
#define NIL
Definition: pg_list.h:69
PathTarget * pathtarget
Definition: relation.h:895
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:853
ParamPathInfo * get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
Definition: relnode.c:1010
NodeTag type
Definition: relation.h:890
ParamPathInfo * param_info
Definition: relation.h:897
NodeTag pathtype
Definition: relation.h:892
RelOptInfo * parent
Definition: relation.h:894
Path * bitmapqual
Definition: relation.h:1003
Index relid
Definition: relation.h:518
static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
Definition: indxpath.c:1948
Cost total_cost
Definition: relation.h:907
List * pathkeys
Definition: relation.h:909
static Relids get_bitmap_tree_required_outer(Path *bitmapqual)
Definition: indxpath.c:1734
struct PathTarget * reltarget
Definition: relation.h:501
static bool bms_equal_any ( Relids  relids,
List relids_list 
)
static

Definition at line 704 of file indxpath.c.

References bms_equal(), and lfirst.

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

705 {
706  ListCell *lc;
707 
708  foreach(lc, relids_list)
709  {
710  if (bms_equal(relids, (Relids) lfirst(lc)))
711  return true;
712  }
713  return false;
714 }
#define lfirst(lc)
Definition: pg_list.h:106
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:130
static List * build_index_paths ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet clauses,
bool  useful_predicate,
ScanTypeControl  scantype,
bool skip_nonnative_saop,
bool skip_lower_saop 
)
static

Definition at line 855 of file indxpath.c.

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(), lappend_int(), RelOptInfo::lateral_relids, lfirst, match_pathkeys_to_index(), IndexOptInfo::ncolumns, NIL, NoMovementScanDirection, NULL, Path::parallel_workers, IndexPath::path, pfree(), PlannerInfo::query_pathkeys, RelOptInfo::relid, IndexOptInfo::sortopfamily, ST_ANYSCAN, ST_BITMAPSCAN, ST_INDEXSCAN, and truncate_useless_pathkeys().

Referenced by build_paths_for_OR(), and get_index_paths().

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

Definition at line 1165 of file indxpath.c.

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

Referenced by generate_bitmap_or_paths().

1167 {
1168  List *result = NIL;
1169  List *all_clauses = NIL; /* not computed till needed */
1170  ListCell *lc;
1171 
1172  foreach(lc, rel->indexlist)
1173  {
1175  IndexClauseSet clauseset;
1176  List *indexpaths;
1177  bool useful_predicate;
1178 
1179  /* Ignore index if it doesn't support bitmap scans */
1180  if (!index->amhasgetbitmap)
1181  continue;
1182 
1183  /*
1184  * Ignore partial indexes that do not match the query. If a partial
1185  * index is marked predOK then we know it's OK. Otherwise, we have to
1186  * test whether the added clauses are sufficient to imply the
1187  * predicate. If so, we can use the index in the current context.
1188  *
1189  * We set useful_predicate to true iff the predicate was proven using
1190  * the current set of clauses. This is needed to prevent matching a
1191  * predOK index to an arm of an OR, which would be a legal but
1192  * pointlessly inefficient plan. (A better plan will be generated by
1193  * just scanning the predOK index alone, no OR.)
1194  */
1195  useful_predicate = false;
1196  if (index->indpred != NIL)
1197  {
1198  if (index->predOK)
1199  {
1200  /* Usable, but don't set useful_predicate */
1201  }
1202  else
1203  {
1204  /* Form all_clauses if not done already */
1205  if (all_clauses == NIL)
1206  all_clauses = list_concat(list_copy(clauses),
1207  other_clauses);
1208 
1209  if (!predicate_implied_by(index->indpred, all_clauses))
1210  continue; /* can't use it at all */
1211 
1212  if (!predicate_implied_by(index->indpred, other_clauses))
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(index, clauses, &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(index, other_clauses, &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:69
bool predOK
Definition: relation.h:620
bool predicate_implied_by(List *predicate_list, List *restrictinfo_list)
Definition: predtest.c:128
bool nonempty
Definition: indxpath.c:60
static List * build_index_paths(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauses, bool useful_predicate, ScanTypeControl scantype, bool *skip_nonnative_saop, bool *skip_lower_saop)
Definition: indxpath.c:855
List * list_copy(const List *oldlist)
Definition: list.c:1160
#define MemSet(start, val, len)
Definition: c.h:853
List * list_concat(List *list1, List *list2)
Definition: list.c:321
Definition: type.h:90
bool amhasgetbitmap
Definition: relation.h:631
List * indexlist
Definition: relation.h:527
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
static void match_clauses_to_index(IndexOptInfo *index, List *clauses, IndexClauseSet *clauseset)
Definition: indxpath.c:2168
List * indpred
Definition: relation.h:611
Definition: pg_list.h:45
static bool check_index_only ( RelOptInfo rel,
IndexOptInfo index 
)
static

Definition at line 1849 of file indxpath.c.

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

Referenced by build_index_paths().

1850 {
1851  bool result;
1852  Bitmapset *attrs_used = NULL;
1853  Bitmapset *index_canreturn_attrs = NULL;
1854  ListCell *lc;
1855  int i;
1856 
1857  /* Index-only scans must be enabled */
1858  if (!enable_indexonlyscan)
1859  return false;
1860 
1861  /*
1862  * Check that all needed attributes of the relation are available from the
1863  * index.
1864  */
1865 
1866  /*
1867  * First, identify all the attributes needed for joins or final output.
1868  * Note: we must look at rel's targetlist, not the attr_needed data,
1869  * because attr_needed isn't computed for inheritance child rels.
1870  */
1871  pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
1872 
1873  /*
1874  * Add all the attributes used by restriction clauses; but consider only
1875  * those clauses not implied by the index predicate, since ones that are
1876  * so implied don't need to be checked explicitly in the plan.
1877  *
1878  * Note: attributes used only in index quals would not be needed at
1879  * runtime either, if we are certain that the index is not lossy. However
1880  * it'd be complicated to account for that accurately, and it doesn't
1881  * matter in most cases, since we'd conclude that such attributes are
1882  * available from the index anyway.
1883  */
1884  foreach(lc, index->indrestrictinfo)
1885  {
1886  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1887 
1888  pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
1889  }
1890 
1891  /*
1892  * Construct a bitmapset of columns that the index can return back in an
1893  * index-only scan.
1894  */
1895  for (i = 0; i < index->ncolumns; i++)
1896  {
1897  int attno = index->indexkeys[i];
1898 
1899  /*
1900  * For the moment, we just ignore index expressions. It might be nice
1901  * to do something with them, later.
1902  */
1903  if (attno == 0)
1904  continue;
1905 
1906  if (index->canreturn[i])
1907  index_canreturn_attrs =
1908  bms_add_member(index_canreturn_attrs,
1910  }
1911 
1912  /* Do we have all the necessary attributes? */
1913  result = bms_is_subset(attrs_used, index_canreturn_attrs);
1914 
1915  bms_free(attrs_used);
1916  bms_free(index_canreturn_attrs);
1917 
1918  return result;
1919 }
Definition: nodes.h:508
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
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:307
int ncolumns
Definition: relation.h:598
Index relid
Definition: relation.h:518
Expr * clause
Definition: relation.h:1637
List * exprs
Definition: relation.h:824
List * indrestrictinfo
Definition: relation.h:615
void bms_free(Bitmapset *a)
Definition: bitmapset.c:200
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
int i
int * indexkeys
Definition: relation.h:599
bool * canreturn
Definition: relation.h:606
struct PathTarget * reltarget
Definition: relation.h:501
bool enable_indexonlyscan
Definition: costsize.c:120
void check_index_predicates ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 2759 of file indxpath.c.

References PlannerInfo::all_baserels, 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, join_clause_is_movable_to(), RelOptInfo::joininfo, lappend(), lfirst, list_concat(), list_copy(), list_make1, NIL, NULL, 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().

2760 {
2761  List *clauselist;
2762  bool have_partial;
2763  bool is_target_rel;
2764  Relids otherrels;
2765  ListCell *lc;
2766 
2767  /*
2768  * Initialize the indrestrictinfo lists to be identical to
2769  * baserestrictinfo, and check whether there are any partial indexes. If
2770  * not, this is all we need to do.
2771  */
2772  have_partial = false;
2773  foreach(lc, rel->indexlist)
2774  {
2776 
2777  index->indrestrictinfo = rel->baserestrictinfo;
2778  if (index->indpred)
2779  have_partial = true;
2780  }
2781  if (!have_partial)
2782  return;
2783 
2784  /*
2785  * Construct a list of clauses that we can assume true for the purpose of
2786  * proving the index(es) usable. Restriction clauses for the rel are
2787  * always usable, and so are any join clauses that are "movable to" this
2788  * rel. Also, we can consider any EC-derivable join clauses (which must
2789  * be "movable to" this rel, by definition).
2790  */
2791  clauselist = list_copy(rel->baserestrictinfo);
2792 
2793  /* Scan the rel's join clauses */
2794  foreach(lc, rel->joininfo)
2795  {
2796  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2797 
2798  /* Check if clause can be moved to this rel */
2799  if (!join_clause_is_movable_to(rinfo, rel))
2800  continue;
2801 
2802  clauselist = lappend(clauselist, rinfo);
2803  }
2804 
2805  /*
2806  * Add on any equivalence-derivable join clauses. Computing the correct
2807  * relid sets for generate_join_implied_equalities is slightly tricky
2808  * because the rel could be a child rel rather than a true baserel, and in
2809  * that case we must remove its parents' relid(s) from all_baserels.
2810  */
2811  if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
2812  otherrels = bms_difference(root->all_baserels,
2813  find_childrel_parents(root, rel));
2814  else
2815  otherrels = bms_difference(root->all_baserels, rel->relids);
2816 
2817  if (!bms_is_empty(otherrels))
2818  clauselist =
2819  list_concat(clauselist,
2821  bms_union(rel->relids,
2822  otherrels),
2823  otherrels,
2824  rel));
2825 
2826  /*
2827  * Normally we remove quals that are implied by a partial index's
2828  * predicate from indrestrictinfo, indicating that they need not be
2829  * checked explicitly by an indexscan plan using this index. However, if
2830  * the rel is a target relation of UPDATE/DELETE/SELECT FOR UPDATE, we
2831  * cannot remove such quals from the plan, because they need to be in the
2832  * plan so that they will be properly rechecked by EvalPlanQual testing.
2833  * Some day we might want to remove such quals from the main plan anyway
2834  * and pass them through to EvalPlanQual via a side channel; but for now,
2835  * we just don't remove implied quals at all for target relations.
2836  */
2837  is_target_rel = (rel->relid == root->parse->resultRelation ||
2838  get_plan_rowmark(root->rowMarks, rel->relid) != NULL);
2839 
2840  /*
2841  * Now try to prove each index predicate true, and compute the
2842  * indrestrictinfo lists for partial indexes. Note that we compute the
2843  * indrestrictinfo list even for non-predOK indexes; this might seem
2844  * wasteful, but we may be able to use such indexes in OR clauses, cf
2845  * generate_bitmap_or_paths().
2846  */
2847  foreach(lc, rel->indexlist)
2848  {
2849  IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
2850  ListCell *lcr;
2851 
2852  if (index->indpred == NIL)
2853  continue; /* ignore non-partial indexes here */
2854 
2855  if (!index->predOK) /* don't repeat work if already proven OK */
2856  index->predOK = predicate_implied_by(index->indpred, clauselist);
2857 
2858  /* If rel is an update target, leave indrestrictinfo as set above */
2859  if (is_target_rel)
2860  continue;
2861 
2862  /* Else compute indrestrictinfo as the non-implied quals */
2863  index->indrestrictinfo = NIL;
2864  foreach(lcr, rel->baserestrictinfo)
2865  {
2866  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcr);
2867 
2868  /* predicate_implied_by() assumes first arg is immutable */
2869  if (contain_mutable_functions((Node *) rinfo->clause) ||
2871  index->indpred))
2872  index->indrestrictinfo = lappend(index->indrestrictinfo, rinfo);
2873  }
2874  }
2875 }
#define NIL
Definition: pg_list.h:69
List * rowMarks
Definition: relation.h:251
Query * parse
Definition: relation.h:152
bool predOK
Definition: relation.h:620
RelOptKind reloptkind
Definition: relation.h:487
bool predicate_implied_by(List *predicate_list, List *restrictinfo_list)
Definition: predtest.c:128
List * baserestrictinfo
Definition: relation.h:544
int resultRelation
Definition: parsenodes.h:113
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:283
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:508
List * list_concat(List *list1, List *list2)
Definition: list.c:321
Definition: type.h:90
#define list_make1(x1)
Definition: pg_list.h:133
Relids all_baserels
Definition: relation.h:193
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1033
List * joininfo
Definition: relation.h:549
Relids relids
Definition: relation.h:490
Index relid
Definition: relation.h:518
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1637
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:633
List * indrestrictinfo
Definition: relation.h:615
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:977
List * indexlist
Definition: relation.h:527
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:217
bool join_clause_is_movable_to(RestrictInfo *rinfo, RelOptInfo *baserel)
Definition: restrictinfo.c:435
PlanRowMark * get_plan_rowmark(List *rowmarks, Index rtindex)
Definition: preptlist.c:401
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:877
List * indpred
Definition: relation.h:611
Definition: pg_list.h:45
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_copy(), list_delete_cell(), list_free(), list_head(), list_length(), list_make1, lnext, NIL, NULL, palloc(), PathClauseUsage::path, path_usage_comparator(), predicate_implied_by(), PathClauseUsage::preds, qsort, and PathClauseUsage::quals.

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  for (i = 0; i < npaths; i++)
1449  {
1450  if (bms_equal(pathinfo->clauseids, pathinfoarray[i]->clauseids))
1451  break;
1452  }
1453  if (i < npaths)
1454  {
1455  /* duplicate clauseids, keep the cheaper one */
1456  Cost ncost;
1457  Cost ocost;
1458  Selectivity nselec;
1459  Selectivity oselec;
1460 
1461  cost_bitmap_tree_node(pathinfo->path, &ncost, &nselec);
1462  cost_bitmap_tree_node(pathinfoarray[i]->path, &ocost, &oselec);
1463  if (ncost < ocost)
1464  pathinfoarray[i] = pathinfo;
1465  }
1466  else
1467  {
1468  /* not duplicate clauseids, add to array */
1469  pathinfoarray[npaths++] = pathinfo;
1470  }
1471  }
1472 
1473  /* If only one surviving path, we're done */
1474  if (npaths == 1)
1475  return pathinfoarray[0]->path;
1476 
1477  /* Sort the surviving paths by index access cost */
1478  qsort(pathinfoarray, npaths, sizeof(PathClauseUsage *),
1480 
1481  /*
1482  * For each surviving index, consider it as an "AND group leader", and see
1483  * whether adding on any of the later indexes results in an AND path with
1484  * cheaper total cost than before. Then take the cheapest AND group.
1485  */
1486  for (i = 0; i < npaths; i++)
1487  {
1488  Cost costsofar;
1489  List *qualsofar;
1490  Bitmapset *clauseidsofar;
1491  ListCell *lastcell;
1492 
1493  pathinfo = pathinfoarray[i];
1494  paths = list_make1(pathinfo->path);
1495  costsofar = bitmap_scan_cost_est(root, rel, pathinfo->path);
1496  qualsofar = list_concat(list_copy(pathinfo->quals),
1497  list_copy(pathinfo->preds));
1498  clauseidsofar = bms_copy(pathinfo->clauseids);
1499  lastcell = list_head(paths); /* for quick deletions */
1500 
1501  for (j = i + 1; j < npaths; j++)
1502  {
1503  Cost newcost;
1504 
1505  pathinfo = pathinfoarray[j];
1506  /* Check for redundancy */
1507  if (bms_overlap(pathinfo->clauseids, clauseidsofar))
1508  continue; /* consider it redundant */
1509  if (pathinfo->preds)
1510  {
1511  bool redundant = false;
1512 
1513  /* we check each predicate clause separately */
1514  foreach(l, pathinfo->preds)
1515  {
1516  Node *np = (Node *) lfirst(l);
1517 
1518  if (predicate_implied_by(list_make1(np), qualsofar))
1519  {
1520  redundant = true;
1521  break; /* out of inner foreach loop */
1522  }
1523  }
1524  if (redundant)
1525  continue;
1526  }
1527  /* tentatively add new path to paths, so we can estimate cost */
1528  paths = lappend(paths, pathinfo->path);
1529  newcost = bitmap_and_cost_est(root, rel, paths);
1530  if (newcost < costsofar)
1531  {
1532  /* keep new path in paths, update subsidiary variables */
1533  costsofar = newcost;
1534  qualsofar = list_concat(qualsofar,
1535  list_copy(pathinfo->quals));
1536  qualsofar = list_concat(qualsofar,
1537  list_copy(pathinfo->preds));
1538  clauseidsofar = bms_add_members(clauseidsofar,
1539  pathinfo->clauseids);
1540  lastcell = lnext(lastcell);
1541  }
1542  else
1543  {
1544  /* reject new path, remove it from paths list */
1545  paths = list_delete_cell(paths, lnext(lastcell), lastcell);
1546  }
1547  Assert(lnext(lastcell) == NULL);
1548  }
1549 
1550  /* Keep the cheapest AND-group (or singleton) */
1551  if (i == 0 || costsofar < bestcost)
1552  {
1553  bestpaths = paths;
1554  bestcost = costsofar;
1555  }
1556 
1557  /* some easy cleanup (we don't try real hard though) */
1558  list_free(qualsofar);
1559  }
1560 
1561  if (list_length(bestpaths) == 1)
1562  return (Path *) linitial(bestpaths); /* no need for AND */
1563  return (Path *) create_bitmap_and_path(root, rel, bestpaths);
1564 }
#define NIL
Definition: pg_list.h:69
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:110
bool predicate_implied_by(List *predicate_list, List *restrictinfo_list)
Definition: predtest.c:128
Path * path
Definition: indxpath.c:68
List * quals
Definition: indxpath.c:69
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:508
List * list_concat(List *list1, List *list2)
Definition: list.c:321
double Selectivity
Definition: nodes.h:631
static Cost bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel, Path *ipath)
Definition: indxpath.c:1602
Bitmapset * clauseids
Definition: indxpath.c:71
#define list_make1(x1)
Definition: pg_list.h:133
#define linitial(l)
Definition: pg_list.h:110
static int path_usage_comparator(const void *a, const void *b)
Definition: indxpath.c:1568
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
static Cost bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel, List *paths)
Definition: indxpath.c:1633
List * lappend(List *list, void *datum)
Definition: list.c:128
BitmapAndPath * create_bitmap_and_path(PlannerInfo *root, RelOptInfo *rel, List *bitmapquals)
Definition: pathnode.c:1099
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
List * preds
Definition: indxpath.c:70
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static PathClauseUsage * classify_index_clause_usage(Path *path, List **clauselist)
Definition: indxpath.c:1687
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:442
void * palloc(Size size)
Definition: mcxt.c:891
void list_free(List *list)
Definition: list.c:1133
int i
#define qsort(a, b, c, d)
Definition: port.h:440
Definition: pg_list.h:45
Definition: relation.h:888
double Cost
Definition: nodes.h:632
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:725
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:130
void cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec)
Definition: costsize.c:940
static PathClauseUsage * classify_index_clause_usage ( Path path,
List **  clauselist 
)
static

Definition at line 1687 of file indxpath.c.

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

Referenced by choose_bitmap_and().

1688 {
1689  PathClauseUsage *result;
1690  Bitmapset *clauseids;
1691  ListCell *lc;
1692 
1693  result = (PathClauseUsage *) palloc(sizeof(PathClauseUsage));
1694  result->path = path;
1695 
1696  /* Recursively find the quals and preds used by the path */
1697  result->quals = NIL;
1698  result->preds = NIL;
1699  find_indexpath_quals(path, &result->quals, &result->preds);
1700 
1701  /* Build up a bitmapset representing the quals and preds */
1702  clauseids = NULL;
1703  foreach(lc, result->quals)
1704  {
1705  Node *node = (Node *) lfirst(lc);
1706 
1707  clauseids = bms_add_member(clauseids,
1708  find_list_position(node, clauselist));
1709  }
1710  foreach(lc, result->preds)
1711  {
1712  Node *node = (Node *) lfirst(lc);
1713 
1714  clauseids = bms_add_member(clauseids,
1715  find_list_position(node, clauselist));
1716  }
1717  result->clauseids = clauseids;
1718 
1719  return result;
1720 }
#define NIL
Definition: pg_list.h:69
static int find_list_position(Node *node, List **nodelist)
Definition: indxpath.c:1823
Path * path
Definition: indxpath.c:68
List * quals
Definition: indxpath.c:69
Definition: nodes.h:508
Bitmapset * clauseids
Definition: indxpath.c:71
static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
Definition: indxpath.c:1782
List * preds
Definition: indxpath.c:70
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
void * palloc(Size size)
Definition: mcxt.c:891
static void consider_index_join_clauses ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet rclauseset,
IndexClauseSet jclauseset,
IndexClauseSet eclauseset,
List **  bitindexpaths 
)
static

Definition at line 435 of file indxpath.c.

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

Referenced by create_index_paths().

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

Referenced by consider_index_join_clauses().

510 {
511  ListCell *lc;
512 
513  /* Examine relids of each joinclause in the given list */
514  foreach(lc, indexjoinclauses)
515  {
516  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
517  Relids clause_relids = rinfo->clause_relids;
518  ListCell *lc2;
519 
520  /* If we already tried its relids set, no need to do so again */
521  if (bms_equal_any(clause_relids, *considered_relids))
522  continue;
523 
524  /*
525  * Generate the union of this clause's relids set with each
526  * previously-tried set. This ensures we try this clause along with
527  * every interesting subset of previous clauses. However, to avoid
528  * exponential growth of planning time when there are many clauses,
529  * limit the number of relid sets accepted to 10 * considered_clauses.
530  *
531  * Note: get_join_index_paths adds entries to *considered_relids, but
532  * it prepends them to the list, so that we won't visit new entries
533  * during the inner foreach loop. No real harm would be done if we
534  * did, since the subset check would reject them; but it would waste
535  * some cycles.
536  */
537  foreach(lc2, *considered_relids)
538  {
539  Relids oldrelids = (Relids) lfirst(lc2);
540 
541  /*
542  * If either is a subset of the other, no new set is possible.
543  * This isn't a complete test for redundancy, but it's easy and
544  * cheap. get_join_index_paths will check more carefully if we
545  * already generated the same relids set.
546  */
547  if (bms_subset_compare(clause_relids, oldrelids) != BMS_DIFFERENT)
548  continue;
549 
550  /*
551  * If this clause was derived from an equivalence class, the
552  * clause list may contain other clauses derived from the same
553  * eclass. We should not consider that combining this clause with
554  * one of those clauses generates a usefully different
555  * parameterization; so skip if any clause derived from the same
556  * eclass would already have been included when using oldrelids.
557  */
558  if (rinfo->parent_ec &&
559  eclass_already_used(rinfo->parent_ec, oldrelids,
560  indexjoinclauses))
561  continue;
562 
563  /*
564  * If the number of relid sets considered exceeds our heuristic
565  * limit, stop considering combinations of clauses. We'll still
566  * consider the current clause alone, though (below this loop).
567  */
568  if (list_length(*considered_relids) >= 10 * considered_clauses)
569  break;
570 
571  /* OK, try the union set */
572  get_join_index_paths(root, rel, index,
573  rclauseset, jclauseset, eclauseset,
574  bitindexpaths,
575  bms_union(clause_relids, oldrelids),
576  considered_relids);
577  }
578 
579  /* Also try this set of relids by itself */
580  get_join_index_paths(root, rel, index,
581  rclauseset, jclauseset, eclauseset,
582  bitindexpaths,
583  clause_relids,
584  considered_relids);
585  }
586 }
Relids clause_relids
Definition: relation.h:1652
static bool bms_equal_any(Relids relids, List *relids_list)
Definition: indxpath.c:704
EquivalenceClass * parent_ec
Definition: relation.h:1671
static void get_join_index_paths(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *rclauseset, IndexClauseSet *jclauseset, IndexClauseSet *eclauseset, List **bitindexpaths, Relids relids, List **considered_relids)
Definition: indxpath.c:602
Bitmapset * Relids
Definition: relation.h:28
BMS_Comparison bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:344
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:217
static int list_length(const List *l)
Definition: pg_list.h:89
static bool eclass_already_used(EquivalenceClass *parent_ec, Relids oldrelids, List *indexjoinclauses)
Definition: indxpath.c:681
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(), create_bitmap_heap_path(), 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, IndexOptInfo::ncolumns, NIL, 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->ncolumns <= 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(rel, 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);
341  add_path(rel, (Path *) bpath);
342  }
343 
344  /*
345  * Likewise, if we found anything usable, generate BitmapHeapPaths for the
346  * most promising combinations of join bitmap index paths. Our strategy
347  * is to generate one such path for each distinct parameterization seen
348  * among the available bitmap index paths. This may look pretty
349  * expensive, but usually there won't be very many distinct
350  * parameterizations. (This logic is quite similar to that in
351  * consider_index_join_clauses, but we're working with whole paths not
352  * individual clauses.)
353  */
354  if (bitjoinpaths != NIL)
355  {
356  List *path_outer;
357  List *all_path_outers;
358  ListCell *lc;
359 
360  /*
361  * path_outer holds the parameterization of each path in bitjoinpaths
362  * (to save recalculating that several times), while all_path_outers
363  * holds all distinct parameterization sets.
364  */
365  path_outer = all_path_outers = NIL;
366  foreach(lc, bitjoinpaths)
367  {
368  Path *path = (Path *) lfirst(lc);
369  Relids required_outer;
370 
371  required_outer = get_bitmap_tree_required_outer(path);
372  path_outer = lappend(path_outer, required_outer);
373  if (!bms_equal_any(required_outer, all_path_outers))
374  all_path_outers = lappend(all_path_outers, required_outer);
375  }
376 
377  /* Now, for each distinct parameterization set ... */
378  foreach(lc, all_path_outers)
379  {
380  Relids max_outers = (Relids) lfirst(lc);
381  List *this_path_set;
382  Path *bitmapqual;
383  Relids required_outer;
384  double loop_count;
385  BitmapHeapPath *bpath;
386  ListCell *lcp;
387  ListCell *lco;
388 
389  /* Identify all the bitmap join paths needing no more than that */
390  this_path_set = NIL;
391  forboth(lcp, bitjoinpaths, lco, path_outer)
392  {
393  Path *path = (Path *) lfirst(lcp);
394  Relids p_outers = (Relids) lfirst(lco);
395 
396  if (bms_is_subset(p_outers, max_outers))
397  this_path_set = lappend(this_path_set, path);
398  }
399 
400  /*
401  * Add in restriction bitmap paths, since they can be used
402  * together with any join paths.
403  */
404  this_path_set = list_concat(this_path_set, bitindexpaths);
405 
406  /* Select best AND combination for this parameterization */
407  bitmapqual = choose_bitmap_and(root, rel, this_path_set);
408 
409  /* And push that path into the mix */
410  required_outer = get_bitmap_tree_required_outer(bitmapqual);
411  loop_count = get_loop_count(root, rel->relid, required_outer);
412  bpath = create_bitmap_heap_path(root, rel, bitmapqual,
413  required_outer, loop_count);
414  add_path(rel, (Path *) bpath);
415  }
416  }
417 }
#define NIL
Definition: pg_list.h:69
BitmapHeapPath * create_bitmap_heap_path(PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual, Relids required_outer, double loop_count)
Definition: pathnode.c:1067
bool predOK
Definition: relation.h:620
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
bool nonempty
Definition: indxpath.c:60
List * baserestrictinfo
Definition: relation.h:544
#define MemSet(start, val, len)
Definition: c.h:853
List * list_concat(List *list1, List *list2)
Definition: list.c:321
Definition: type.h:90
static bool bms_equal_any(Relids relids, List *relids_list)
Definition: indxpath.c:704
Relids lateral_relids
Definition: relation.h:515
static void match_eclass_clauses_to_index(PlannerInfo *root, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2130
static void get_index_paths(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauses, List **bitindexpaths)
Definition: indxpath.c:733
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:307
static Path * choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths)
Definition: indxpath.c:1368
int ncolumns
Definition: relation.h:598
Index relid
Definition: relation.h:518
Bitmapset * Relids
Definition: relation.h:28
List * lappend(List *list, void *datum)
Definition: list.c:128
static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
Definition: indxpath.c:1948
static void match_restriction_clauses_to_index(RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2086
static List * generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel, List *clauses, List *other_clauses)
Definition: indxpath.c:1261
List * indexlist
Definition: relation.h:527
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
#define INDEX_MAX_KEYS
static void match_join_clauses_to_index(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauseset, List **joinorclauses)
Definition: indxpath.c:2100
static Relids get_bitmap_tree_required_outer(Path *bitmapqual)
Definition: indxpath.c:1734
List * indpred
Definition: relation.h:611
Definition: pg_list.h:45
Definition: relation.h:888
static void consider_index_join_clauses(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *rclauseset, IndexClauseSet *jclauseset, IndexClauseSet *eclauseset, List **bitindexpaths)
Definition: indxpath.c:435
static bool ec_member_matches_indexcol ( PlannerInfo root,
RelOptInfo rel,
EquivalenceClass ec,
EquivalenceMember em,
void *  arg 
)
static

Definition at line 2888 of file indxpath.c.

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

2891 {
2892  IndexOptInfo *index = ((ec_member_matches_arg *) arg)->index;
2893  int indexcol = ((ec_member_matches_arg *) arg)->indexcol;
2894  Oid curFamily = index->opfamily[indexcol];
2895  Oid curCollation = index->indexcollations[indexcol];
2896 
2897  /*
2898  * If it's a btree index, we can reject it if its opfamily isn't
2899  * compatible with the EC, since no clause generated from the EC could be
2900  * used with the index. For non-btree indexes, we can't easily tell
2901  * whether clauses generated from the EC could be used with the index, so
2902  * don't check the opfamily. This might mean we return "true" for a
2903  * useless EC, so we have to recheck the results of
2904  * generate_implied_equalities_for_column; see
2905  * match_eclass_clauses_to_index.
2906  */
2907  if (index->relam == BTREE_AM_OID &&
2908  !list_member_oid(ec->ec_opfamilies, curFamily))
2909  return false;
2910 
2911  /* We insist on collation match for all index types, though */
2912  if (!IndexCollMatchesExprColl(curCollation, ec->ec_collation))
2913  return false;
2914 
2915  return match_index_to_operand((Node *) em->em_expr, indexcol, index);
2916 }
Oid * indexcollations
Definition: relation.h:600
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3161
#define BTREE_AM_OID
Definition: pg_am.h:70
Definition: nodes.h:508
unsigned int Oid
Definition: postgres_ext.h:31
Definition: type.h:90
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:46
List * ec_opfamilies
Definition: relation.h:712
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
Oid * opfamily
Definition: relation.h:601
void * arg
static bool eclass_already_used ( EquivalenceClass parent_ec,
Relids  oldrelids,
List indexjoinclauses 
)
static

Definition at line 681 of file indxpath.c.

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

Referenced by consider_index_join_outer_rels().

683 {
684  ListCell *lc;
685 
686  foreach(lc, indexjoinclauses)
687  {
688  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
689 
690  if (rinfo->parent_ec == parent_ec &&
691  bms_is_subset(rinfo->clause_relids, oldrelids))
692  return true;
693  }
694  return false;
695 }
Relids clause_relids
Definition: relation.h:1652
EquivalenceClass * parent_ec
Definition: relation.h:1671
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:307
#define lfirst(lc)
Definition: pg_list.h:106
static Expr * expand_boolean_index_clause ( Node clause,
int  indexcol,
IndexOptInfo index 
)
static

Definition at line 3592 of file indxpath.c.

References arg, BooleanTest::arg, Assert, BooleanEqualOperator, BOOLOID, BooleanTest::booltesttype, get_notclausearg(), InvalidOid, IS_FALSE, IS_TRUE, IsA, make_opclause(), makeBoolConst(), match_index_to_operand(), not_clause(), and NULL.

Referenced by expand_indexqual_conditions().

3595 {
3596  /* Direct match? */
3597  if (match_index_to_operand(clause, indexcol, index))
3598  {
3599  /* convert to indexkey = TRUE */
3601  (Expr *) clause,
3602  (Expr *) makeBoolConst(true, false),
3604  }
3605  /* NOT clause? */
3606  if (not_clause(clause))
3607  {
3608  Node *arg = (Node *) get_notclausearg((Expr *) clause);
3609 
3610  /* It must have matched the indexkey */
3611  Assert(match_index_to_operand(arg, indexcol, index));
3612  /* convert to indexkey = FALSE */
3614  (Expr *) arg,
3615  (Expr *) makeBoolConst(false, false),
3617  }
3618  if (clause && IsA(clause, BooleanTest))
3619  {
3620  BooleanTest *btest = (BooleanTest *) clause;
3621  Node *arg = (Node *) btest->arg;
3622 
3623  /* It must have matched the indexkey */
3624  Assert(match_index_to_operand(arg, indexcol, index));
3625  if (btest->booltesttype == IS_TRUE)
3626  {
3627  /* convert to indexkey = TRUE */
3629  (Expr *) arg,
3630  (Expr *) makeBoolConst(true, false),
3632  }
3633  if (btest->booltesttype == IS_FALSE)
3634  {
3635  /* convert to indexkey = FALSE */
3637  (Expr *) arg,
3638  (Expr *) makeBoolConst(false, false),
3640  }
3641  /* Oops */
3642  Assert(false);
3643  }
3644 
3645  return NULL;
3646 }
Expr * get_notclausearg(Expr *notclause)
Definition: clauses.c:264
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3161
Definition: nodes.h:508
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: clauses.c:171
#define BooleanEqualOperator
Definition: pg_operator.h:114
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:354
Expr * arg
Definition: primnodes.h:1179
bool not_clause(Node *clause)
Definition: clauses.c:235
BoolTestType booltesttype
Definition: primnodes.h:1180
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define BOOLOID
Definition: pg_type.h:288
void * arg
void expand_indexqual_conditions ( IndexOptInfo index,
List indexclauses,
List indexclausecols,
List **  indexquals_p,
List **  indexqualcols_p 
)

Definition at line 3507 of file indxpath.c.

References IndexOptInfo::amsearchnulls, Assert, RestrictInfo::clause, elog, ERROR, expand_boolean_index_clause(), expand_indexqual_opclause(), expand_indexqual_rowcompare(), forboth, IndexOptInfo::indexcollations, is_opclause, IsA, IsBooleanOpfamily, lappend(), lappend_int(), lfirst, lfirst_int, list_concat(), list_length(), make_simple_restrictinfo, NIL, nodeTag, and IndexOptInfo::opfamily.

Referenced by create_index_path().

3510 {
3511  List *indexquals = NIL;
3512  List *indexqualcols = NIL;
3513  ListCell *lcc,
3514  *lci;
3515 
3516  forboth(lcc, indexclauses, lci, indexclausecols)
3517  {
3518  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
3519  int indexcol = lfirst_int(lci);
3520  Expr *clause = rinfo->clause;
3521  Oid curFamily = index->opfamily[indexcol];
3522  Oid curCollation = index->indexcollations[indexcol];
3523 
3524  /* First check for boolean cases */
3525  if (IsBooleanOpfamily(curFamily))
3526  {
3527  Expr *boolqual;
3528 
3529  boolqual = expand_boolean_index_clause((Node *) clause,
3530  indexcol,
3531  index);
3532  if (boolqual)
3533  {
3534  indexquals = lappend(indexquals,
3535  make_simple_restrictinfo(boolqual));
3536  indexqualcols = lappend_int(indexqualcols, indexcol);
3537  continue;
3538  }
3539  }
3540 
3541  /*
3542  * Else it must be an opclause (usual case), ScalarArrayOp,
3543  * RowCompare, or NullTest
3544  */
3545  if (is_opclause(clause))
3546  {
3547  indexquals = list_concat(indexquals,
3549  curFamily,
3550  curCollation));
3551  /* expand_indexqual_opclause can produce multiple clauses */
3552  while (list_length(indexqualcols) < list_length(indexquals))
3553  indexqualcols = lappend_int(indexqualcols, indexcol);
3554  }
3555  else if (IsA(clause, ScalarArrayOpExpr))
3556  {
3557  /* no extra work at this time */
3558  indexquals = lappend(indexquals, rinfo);
3559  indexqualcols = lappend_int(indexqualcols, indexcol);
3560  }
3561  else if (IsA(clause, RowCompareExpr))
3562  {
3563  indexquals = lappend(indexquals,
3565  index,
3566  indexcol));
3567  indexqualcols = lappend_int(indexqualcols, indexcol);
3568  }
3569  else if (IsA(clause, NullTest))
3570  {
3571  Assert(index->amsearchnulls);
3572  indexquals = lappend(indexquals, rinfo);
3573  indexqualcols = lappend_int(indexqualcols, indexcol);
3574  }
3575  else
3576  elog(ERROR, "unsupported indexqual type: %d",
3577  (int) nodeTag(clause));
3578  }
3579 
3580  *indexquals_p = indexquals;
3581  *indexqualcols_p = indexqualcols;
3582 }
#define NIL
Definition: pg_list.h:69
#define IsBooleanOpfamily(opfamily)
Definition: indxpath.c:43
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
Oid * indexcollations
Definition: relation.h:600
Definition: nodes.h:508
List * list_concat(List *list1, List *list2)
Definition: list.c:321
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define is_opclause(clause)
Definition: clauses.h:20
#define lfirst_int(lc)
Definition: pg_list.h:107
static Expr * expand_boolean_index_clause(Node *clause, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3592
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1637
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
static RestrictInfo * expand_indexqual_rowcompare(RestrictInfo *rinfo, IndexOptInfo *index, int indexcol)
Definition: indxpath.c:3752
static int list_length(const List *l)
Definition: pg_list.h:89
#define nodeTag(nodeptr)
Definition: nodes.h:513
Oid * opfamily
Definition: relation.h:601
#define elog
Definition: elog.h:219
bool amsearchnulls
Definition: relation.h:629
static List * expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily, Oid idxcollation)
Definition: indxpath.c:3658
Definition: pg_list.h:45
#define make_simple_restrictinfo(clause)
Definition: restrictinfo.h:21
static List * expand_indexqual_opclause ( RestrictInfo rinfo,
Oid  opfamily,
Oid  idxcollation 
)
static

Definition at line 3658 of file indxpath.c.

References RestrictInfo::clause, Const::constvalue, get_leftop(), get_rightop(), list_make1, network_prefix_quals(), NULL, OID_BPCHAR_ICLIKE_OP, OID_BPCHAR_ICREGEXEQ_OP, OID_BPCHAR_LIKE_OP, OID_BPCHAR_REGEXEQ_OP, OID_BYTEA_LIKE_OP, OID_INET_SUB_OP, OID_INET_SUBEQ_OP, OID_NAME_ICLIKE_OP, OID_NAME_ICREGEXEQ_OP, OID_NAME_LIKE_OP, OID_NAME_REGEXEQ_OP, OID_TEXT_ICLIKE_OP, OID_TEXT_ICREGEXEQ_OP, OID_TEXT_LIKE_OP, OID_TEXT_REGEXEQ_OP, op_in_opfamily(), pattern_fixed_prefix(), Pattern_Type_Like, Pattern_Type_Like_IC, Pattern_Type_Regex, Pattern_Type_Regex_IC, and prefix_quals().

Referenced by expand_indexqual_conditions().

3659 {
3660  Expr *clause = rinfo->clause;
3661 
3662  /* we know these will succeed */
3663  Node *leftop = get_leftop(clause);
3664  Node *rightop = get_rightop(clause);
3665  Oid expr_op = ((OpExpr *) clause)->opno;
3666  Oid expr_coll = ((OpExpr *) clause)->inputcollid;
3667  Const *patt = (Const *) rightop;
3668  Const *prefix = NULL;
3669  Pattern_Prefix_Status pstatus;
3670 
3671  /*
3672  * LIKE and regex operators are not members of any btree index opfamily,
3673  * but they can be members of opfamilies for more exotic index types such
3674  * as GIN. Therefore, we should only do expansion if the operator is
3675  * actually not in the opfamily. But checking that requires a syscache
3676  * lookup, so it's best to first see if the operator is one we are
3677  * interested in.
3678  */
3679  switch (expr_op)
3680  {
3681  case OID_TEXT_LIKE_OP:
3682  case OID_BPCHAR_LIKE_OP:
3683  case OID_NAME_LIKE_OP:
3684  case OID_BYTEA_LIKE_OP:
3685  if (!op_in_opfamily(expr_op, opfamily))
3686  {
3687  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like, expr_coll,
3688  &prefix, NULL);
3689  return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
3690  }
3691  break;
3692 
3693  case OID_TEXT_ICLIKE_OP:
3694  case OID_BPCHAR_ICLIKE_OP:
3695  case OID_NAME_ICLIKE_OP:
3696  if (!op_in_opfamily(expr_op, opfamily))
3697  {
3698  /* the right-hand const is type text for all of these */
3699  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC, expr_coll,
3700  &prefix, NULL);
3701  return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
3702  }
3703  break;
3704 
3705  case OID_TEXT_REGEXEQ_OP:
3706  case OID_BPCHAR_REGEXEQ_OP:
3707  case OID_NAME_REGEXEQ_OP:
3708  if (!op_in_opfamily(expr_op, opfamily))
3709  {
3710  /* the right-hand const is type text for all of these */
3711  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex, expr_coll,
3712  &prefix, NULL);
3713  return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
3714  }
3715  break;
3716 
3717  case OID_TEXT_ICREGEXEQ_OP:
3719  case OID_NAME_ICREGEXEQ_OP:
3720  if (!op_in_opfamily(expr_op, opfamily))
3721  {
3722  /* the right-hand const is type text for all of these */
3723  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC, expr_coll,
3724  &prefix, NULL);
3725  return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
3726  }
3727  break;
3728 
3729  case OID_INET_SUB_OP:
3730  case OID_INET_SUBEQ_OP:
3731  if (!op_in_opfamily(expr_op, opfamily))
3732  {
3733  return network_prefix_quals(leftop, expr_op, opfamily,
3734  patt->constvalue);
3735  }
3736  break;
3737  }
3738 
3739  /* Default case: just make a list of the unmodified indexqual */
3740  return list_make1(rinfo);
3741 }
Datum constvalue
Definition: primnodes.h:174
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:63
#define OID_INET_SUBEQ_OP
Definition: pg_operator.h:1161
static List * network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily, Datum rightop)
Definition: indxpath.c:4141
#define OID_TEXT_REGEXEQ_OP
Definition: pg_operator.h:503
Definition: nodes.h:508
#define OID_TEXT_LIKE_OP
Definition: pg_operator.h:895
unsigned int Oid
Definition: postgres_ext.h:31
#define OID_BYTEA_LIKE_OP
Definition: pg_operator.h:1380
#define OID_NAME_ICREGEXEQ_OP
Definition: pg_operator.h:907
#define list_make1(x1)
Definition: pg_list.h:133
#define OID_BPCHAR_LIKE_OP
Definition: pg_operator.h:900
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
#define OID_NAME_ICLIKE_OP
Definition: pg_operator.h:1190
Expr * clause
Definition: relation.h:1637
static List * prefix_quals(Node *leftop, Oid opfamily, Oid collation, Const *prefix, Pattern_Prefix_Status pstatus)
Definition: indxpath.c:4015
Pattern_Prefix_Status pattern_fixed_prefix(Const *patt, Pattern_Type ptype, Oid collation, Const **prefix, Selectivity *rest_selec)
Definition: selfuncs.c:5471
#define NULL
Definition: c.h:226
#define OID_TEXT_ICREGEXEQ_OP
Definition: pg_operator.h:912
#define OID_BPCHAR_REGEXEQ_OP
Definition: pg_operator.h:759
#define OID_TEXT_ICLIKE_OP
Definition: pg_operator.h:1195
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
#define OID_BPCHAR_ICLIKE_OP
Definition: pg_operator.h:1200
#define OID_INET_SUB_OP
Definition: pg_operator.h:1158
#define OID_NAME_LIKE_OP
Definition: pg_operator.h:890
#define OID_BPCHAR_ICREGEXEQ_OP
Definition: pg_operator.h:917
#define OID_NAME_REGEXEQ_OP
Definition: pg_operator.h:498
Pattern_Prefix_Status
Definition: selfuncs.h:93
static RestrictInfo * expand_indexqual_rowcompare ( RestrictInfo rinfo,
IndexOptInfo index,
int  indexcol 
)
static

Definition at line 3752 of file indxpath.c.

References adjust_rowcompare_for_index(), RestrictInfo::clause, and make_simple_restrictinfo.

Referenced by expand_indexqual_conditions().

3755 {
3756  RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
3757  Expr *newclause;
3758  List *indexcolnos;
3759  bool var_on_left;
3760 
3761  newclause = adjust_rowcompare_for_index(clause,
3762  index,
3763  indexcol,
3764  &indexcolnos,
3765  &var_on_left);
3766 
3767  /*
3768  * If we didn't have to change the RowCompareExpr, return the original
3769  * RestrictInfo.
3770  */
3771  if (newclause == (Expr *) clause)
3772  return rinfo;
3773 
3774  /* Else we need a new RestrictInfo */
3775  return make_simple_restrictinfo(newclause);
3776 }
Expr * adjust_rowcompare_for_index(RowCompareExpr *clause, IndexOptInfo *index, int indexcol, List **indexcolnos, bool *var_on_left_p)
Definition: indxpath.c:3805
Expr * clause
Definition: relation.h:1637
Definition: pg_list.h:45
#define make_simple_restrictinfo(clause)
Definition: restrictinfo.h:21
static void find_indexpath_quals ( Path bitmapqual,
List **  quals,
List **  preds 
)
static

Definition at line 1782 of file indxpath.c.

References BitmapAndPath::bitmapquals, BitmapOrPath::bitmapquals, elog, ERROR, get_actual_clauses(), IndexPath::indexclauses, IndexPath::indexinfo, IndexOptInfo::indpred, IsA, lfirst, list_concat(), list_copy(), and nodeTag.

Referenced by classify_index_clause_usage().

1783 {
1784  if (IsA(bitmapqual, BitmapAndPath))
1785  {
1786  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1787  ListCell *l;
1788 
1789  foreach(l, apath->bitmapquals)
1790  {
1791  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1792  }
1793  }
1794  else if (IsA(bitmapqual, BitmapOrPath))
1795  {
1796  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1797  ListCell *l;
1798 
1799  foreach(l, opath->bitmapquals)
1800  {
1801  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1802  }
1803  }
1804  else if (IsA(bitmapqual, IndexPath))
1805  {
1806  IndexPath *ipath = (IndexPath *) bitmapqual;
1807 
1808  *quals = list_concat(*quals, get_actual_clauses(ipath->indexclauses));
1809  *preds = list_concat(*preds, list_copy(ipath->indexinfo->indpred));
1810  }
1811  else
1812  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1813 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
IndexOptInfo * indexinfo
Definition: relation.h:972
List * list_copy(const List *oldlist)
Definition: list.c:1160
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:331
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * indexclauses
Definition: relation.h:973
List * bitmapquals
Definition: relation.h:1015
List * bitmapquals
Definition: relation.h:1028
static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
Definition: indxpath.c:1782
#define ERROR
Definition: elog.h:43
#define lfirst(lc)
Definition: pg_list.h:106
#define nodeTag(nodeptr)
Definition: nodes.h:513
#define elog
Definition: elog.h:219
List * indpred
Definition: relation.h:611
Definition: relation.h:888
static int find_list_position ( Node node,
List **  nodelist 
)
static

Definition at line 1823 of file indxpath.c.

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

Referenced by classify_index_clause_usage().

1824 {
1825  int i;
1826  ListCell *lc;
1827 
1828  i = 0;
1829  foreach(lc, *nodelist)
1830  {
1831  Node *oldnode = (Node *) lfirst(lc);
1832 
1833  if (equal(node, oldnode))
1834  return i;
1835  i++;
1836  }
1837 
1838  *nodelist = lappend(*nodelist, node);
1839 
1840  return i;
1841 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2870
Definition: nodes.h:508
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
int i
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 and_clause(), Assert, build_paths_for_OR(), castNode, choose_bitmap_and(), create_bitmap_or_path(), lappend(), lfirst, list_concat(), list_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(list_copy(clauses), other_clauses);
1273 
1274  foreach(lc, clauses)
1275  {
1276  RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(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 (and_clause(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:69
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
Expr * orclause
Definition: relation.h:1668
BitmapOrPath * create_bitmap_or_path(PlannerInfo *root, RelOptInfo *rel, List *bitmapquals)
Definition: pathnode.c:1135
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:508
List * list_concat(List *list1, List *list2)
Definition: list.c:321
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:293
#define list_make1(x1)
Definition: pg_list.h:133
bool and_clause(Node *clause)
Definition: clauses.c:313
static Path * choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths)
Definition: indxpath.c:1368
List * lappend(List *list, void *datum)
Definition: list.c:128
static List * build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel, List *clauses, List *other_clauses)
Definition: indxpath.c:1165
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:671
#define lfirst(lc)
Definition: pg_list.h:106
Definition: pg_list.h:45
Definition: relation.h:888
static Relids get_bitmap_tree_required_outer ( Path bitmapqual)
static

Definition at line 1734 of file indxpath.c.

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

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

1735 {
1736  Relids result = NULL;
1737  ListCell *lc;
1738 
1739  if (IsA(bitmapqual, IndexPath))
1740  {
1741  return bms_copy(PATH_REQ_OUTER(bitmapqual));
1742  }
1743  else if (IsA(bitmapqual, BitmapAndPath))
1744  {
1745  foreach(lc, ((BitmapAndPath *) bitmapqual)->bitmapquals)
1746  {
1747  result = bms_join(result,
1749  }
1750  }
1751  else if (IsA(bitmapqual, BitmapOrPath))
1752  {
1753  foreach(lc, ((BitmapOrPath *) bitmapqual)->bitmapquals)
1754  {
1755  result = bms_join(result,
1757  }
1758  }
1759  else
1760  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1761 
1762  return result;
1763 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:110
#define ERROR
Definition: elog.h:43
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:808
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
#define PATH_REQ_OUTER(path)
Definition: relation.h:914
#define nodeTag(nodeptr)
Definition: nodes.h:513
#define elog
Definition: elog.h:219
static Relids get_bitmap_tree_required_outer(Path *bitmapqual)
Definition: indxpath.c:1734
Definition: relation.h:888
static void get_index_paths ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet clauses,
List **  bitindexpaths 
)
static

Definition at line 733 of file indxpath.c.

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

Referenced by create_index_paths(), and get_join_index_paths().

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

Definition at line 602 of file indxpath.c.

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

Referenced by consider_index_join_outer_rels().

610 {
611  IndexClauseSet clauseset;
612  int indexcol;
613 
614  /* If we already considered this relids set, don't repeat the work */
615  if (bms_equal_any(relids, *considered_relids))
616  return;
617 
618  /* Identify indexclauses usable with this relids set */
619  MemSet(&clauseset, 0, sizeof(clauseset));
620 
621  for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
622  {
623  ListCell *lc;
624 
625  /* First find applicable simple join clauses */
626  foreach(lc, jclauseset->indexclauses[indexcol])
627  {
628  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
629 
630  if (bms_is_subset(rinfo->clause_relids, relids))
631  clauseset.indexclauses[indexcol] =
632  lappend(clauseset.indexclauses[indexcol], rinfo);
633  }
634 
635  /*
636  * Add applicable eclass join clauses. The clauses generated for each
637  * column are redundant (cf generate_implied_equalities_for_column),
638  * so we need at most one. This is the only exception to the general
639  * rule of using all available index clauses.
640  */
641  foreach(lc, eclauseset->indexclauses[indexcol])
642  {
643  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
644 
645  if (bms_is_subset(rinfo->clause_relids, relids))
646  {
647  clauseset.indexclauses[indexcol] =
648  lappend(clauseset.indexclauses[indexcol], rinfo);
649  break;
650  }
651  }
652 
653  /* Add restriction clauses (this is nondestructive to rclauseset) */
654  clauseset.indexclauses[indexcol] =
655  list_concat(clauseset.indexclauses[indexcol],
656  rclauseset->indexclauses[indexcol]);
657 
658  if (clauseset.indexclauses[indexcol] != NIL)
659  clauseset.nonempty = true;
660  }
661 
662  /* We should have found something, else caller passed silly relids */
663  Assert(clauseset.nonempty);
664 
665  /* Build index path(s) using the collected set of clauses */
666  get_index_paths(root, rel, index, &clauseset, bitindexpaths);
667 
668  /*
669  * Remember we considered paths for this set of relids. We use lcons not
670  * lappend to avoid confusing the loop in consider_index_join_outer_rels.
671  */
672  *considered_relids = lcons(relids, *considered_relids);
673 }
#define NIL
Definition: pg_list.h:69
bool nonempty
Definition: indxpath.c:60
Relids clause_relids
Definition: relation.h:1652
#define MemSet(start, val, len)
Definition: c.h:853
List * list_concat(List *list1, List *list2)
Definition: list.c:321
static bool bms_equal_any(Relids relids, List *relids_list)
Definition: indxpath.c:704
static void get_index_paths(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauses, List **bitindexpaths)
Definition: indxpath.c:733
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:307
List * indexclauses[INDEX_MAX_KEYS]
Definition: indxpath.c:62
int ncolumns
Definition: relation.h:598
List * lappend(List *list, void *datum)
Definition: list.c:128
List * lcons(void *datum, List *list)
Definition: list.c:259
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
static double get_loop_count ( PlannerInfo root,
Index  cur_relid,
Relids  outer_relids 
)
static

Definition at line 1948 of file indxpath.c.

References adjust_rowcount_for_semijoins(), Assert, bms_next_member(), IS_DUMMY_REL, NULL, 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().

1949 {
1950  double result;
1951  int outer_relid;
1952 
1953  /* For a non-parameterized path, just return 1.0 quickly */
1954  if (outer_relids == NULL)
1955  return 1.0;
1956 
1957  result = 0.0;
1958  outer_relid = -1;
1959  while ((outer_relid = bms_next_member(outer_relids, outer_relid)) >= 0)
1960  {
1961  RelOptInfo *outer_rel;
1962  double rowcount;
1963 
1964  /* Paranoia: ignore bogus relid indexes */
1965  if (outer_relid >= root->simple_rel_array_size)
1966  continue;
1967  outer_rel = root->simple_rel_array[outer_relid];
1968  if (outer_rel == NULL)
1969  continue;
1970  Assert(outer_rel->relid == outer_relid); /* sanity check on array */
1971 
1972  /* Other relation could be proven empty, if so ignore */
1973  if (IS_DUMMY_REL(outer_rel))
1974  continue;
1975 
1976  /* Otherwise, rel's rows estimate should be valid by now */
1977  Assert(outer_rel->rows > 0);
1978 
1979  /* Check to see if rel is on the inside of any semijoins */
1980  rowcount = adjust_rowcount_for_semijoins(root,
1981  cur_relid,
1982  outer_relid,
1983  outer_rel->rows);
1984 
1985  /* Remember smallest row count estimate among the outer rels */
1986  if (result == 0.0 || result > rowcount)
1987  result = rowcount;
1988  }
1989  /* Return 1.0 if we found no valid relations (shouldn't happen) */
1990  return (result > 0.0) ? result : 1.0;
1991 }
static double adjust_rowcount_for_semijoins(PlannerInfo *root, Index cur_relid, Index outer_relid, double rowcount)
Definition: indxpath.c:2001
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:907
struct RelOptInfo ** simple_rel_array
Definition: relation.h:176
#define IS_DUMMY_REL(r)
Definition: relation.h:1126
int simple_rel_array_size
Definition: relation.h:177
Index relid
Definition: relation.h:518
double rows
Definition: relation.h:493
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
bool indexcol_is_bool_constant_for_query ( IndexOptInfo index,
int  indexcol 
)

Definition at line 3112 of file indxpath.c.

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

Referenced by build_index_pathkeys().

3113 {
3114  ListCell *lc;
3115 
3116  /* If the index isn't boolean, we can't possibly get a match */
3117  if (!IsBooleanOpfamily(index->opfamily[indexcol]))
3118  return false;
3119 
3120  /* Check each restriction clause for the index's rel */
3121  foreach(lc, index->rel->baserestrictinfo)
3122  {
3123  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3124 
3125  /*
3126  * As in match_clause_to_indexcol, never match pseudoconstants to
3127  * indexes. (It might be semantically okay to do so here, but the
3128  * odds of getting a match are negligible, so don't waste the cycles.)
3129  */
3130  if (rinfo->pseudoconstant)
3131  continue;
3132 
3133  /* See if we can match the clause's expression to the index column */
3134  if (match_boolean_index_clause((Node *) rinfo->clause, indexcol, index))
3135  return true;
3136  }
3137 
3138  return false;
3139 }
#define IsBooleanOpfamily(opfamily)
Definition: indxpath.c:43
List * baserestrictinfo
Definition: relation.h:544
bool pseudoconstant
Definition: relation.h:1645
Definition: nodes.h:508
RelOptInfo * rel
Definition: relation.h:590
static bool match_boolean_index_clause(Node *clause, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3287
Expr * clause
Definition: relation.h:1637
#define lfirst(lc)
Definition: pg_list.h:106
Oid * opfamily
Definition: relation.h:601
static bool is_indexable_operator ( Oid  expr_op,
Oid  opfamily,
bool  indexkey_on_left 
)
static

Definition at line 2428 of file indxpath.c.

References get_commutator(), InvalidOid, and op_in_opfamily().

Referenced by match_clause_to_indexcol().

2429 {
2430  /* Get the commuted operator if necessary */
2431  if (!indexkey_on_left)
2432  {
2433  expr_op = get_commutator(expr_op);
2434  if (expr_op == InvalidOid)
2435  return false;
2436  }
2437 
2438  /* OK if the (commuted) operator is a member of the index's opfamily */
2439  return op_in_opfamily(expr_op, opfamily);
2440 }
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:63
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1281
#define InvalidOid
Definition: postgres_ext.h:36
static bool match_boolean_index_clause ( Node clause,
int  indexcol,
IndexOptInfo index 
)
static

Definition at line 3287 of file indxpath.c.

References BooleanTest::arg, BooleanTest::booltesttype, get_notclausearg(), IS_FALSE, IS_TRUE, IsA, match_index_to_operand(), and not_clause().

Referenced by indexcol_is_bool_constant_for_query(), and match_clause_to_indexcol().

3290 {
3291  /* Direct match? */
3292  if (match_index_to_operand(clause, indexcol, index))
3293  return true;
3294  /* NOT clause? */
3295  if (not_clause(clause))
3296  {
3297  if (match_index_to_operand((Node *) get_notclausearg((Expr *) clause),
3298  indexcol, index))
3299  return true;
3300  }
3301 
3302  /*
3303  * Since we only consider clauses at top level of WHERE, we can convert
3304  * indexkey IS TRUE and indexkey IS FALSE to index searches as well. The
3305  * different meaning for NULL isn't important.
3306  */
3307  else if (clause && IsA(clause, BooleanTest))
3308  {
3309  BooleanTest *btest = (BooleanTest *) clause;
3310 
3311  if (btest->booltesttype == IS_TRUE ||
3312  btest->booltesttype == IS_FALSE)
3313  if (match_index_to_operand((Node *) btest->arg,
3314  indexcol, index))
3315  return true;
3316  }
3317  return false;
3318 }
Expr * get_notclausearg(Expr *notclause)
Definition: clauses.c:264
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3161
Definition: nodes.h:508
Expr * arg
Definition: primnodes.h:1179
bool not_clause(Node *clause)
Definition: clauses.c:235
BoolTestType booltesttype
Definition: primnodes.h:1180
static void match_clause_to_index ( IndexOptInfo index,
RestrictInfo rinfo,
IndexClauseSet clauseset 
)
static

Definition at line 2199 of file indxpath.c.

References IndexClauseSet::indexclauses, list_append_unique_ptr(), match_clause_to_indexcol(), IndexOptInfo::ncolumns, IndexClauseSet::nonempty, RestrictInfo::pseudoconstant, IndexOptInfo::rel, and restriction_is_securely_promotable().

Referenced by match_clauses_to_index(), and match_join_clauses_to_index().

2202 {
2203  int indexcol;
2204 
2205  /*
2206  * Never match pseudoconstants to indexes. (Normally a match could not
2207  * happen anyway, since a pseudoconstant clause couldn't contain a Var,
2208  * but what if someone builds an expression index on a constant? It's not
2209  * totally unreasonable to do so with a partial index, either.)
2210  */
2211  if (rinfo->pseudoconstant)
2212  return;
2213 
2214  /*
2215  * If clause can't be used as an indexqual because it must wait till after
2216  * some lower-security-level restriction clause, reject it.
2217  */
2218  if (!restriction_is_securely_promotable(rinfo, index->rel))
2219  return;
2220 
2221  /* OK, check each index column for a match */
2222  for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
2223  {
2224  if (match_clause_to_indexcol(index,
2225  indexcol,
2226  rinfo))
2227  {
2228  clauseset->indexclauses[indexcol] =
2229  list_append_unique_ptr(clauseset->indexclauses[indexcol],
2230  rinfo);
2231  clauseset->nonempty = true;
2232  return;
2233  }
2234  }
2235 }
bool nonempty
Definition: indxpath.c:60
bool pseudoconstant
Definition: relation.h:1645
RelOptInfo * rel
Definition: relation.h:590
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:975
static bool match_clause_to_indexcol(IndexOptInfo *index, int indexcol, RestrictInfo *rinfo)
Definition: indxpath.c:2300
List * indexclauses[INDEX_MAX_KEYS]
Definition: indxpath.c:62
int ncolumns
Definition: relation.h:598
bool restriction_is_securely_promotable(RestrictInfo *restrictinfo, RelOptInfo *rel)
Definition: restrictinfo.c:308
static bool match_clause_to_indexcol ( IndexOptInfo index,
int  indexcol,
RestrictInfo rinfo 
)
static

Definition at line 2300 of file indxpath.c.

References IndexOptInfo::amsearchnulls, NullTest::arg, NullTest::argisrow, ScalarArrayOpExpr::args, bms_is_member(), RestrictInfo::clause, contain_volatile_functions(), get_leftop(), get_rightop(), IndexOptInfo::indexcollations, IndexCollMatchesExprColl, ScalarArrayOpExpr::inputcollid, is_indexable_operator(), is_opclause, IsA, IsBooleanOpfamily, RestrictInfo::left_relids, linitial, lsecond, match_boolean_index_clause(), match_index_to_operand(), match_rowcompare_to_indexcol(), match_special_index_operator(), NULL, IndexOptInfo::opfamily, ScalarArrayOpExpr::opno, pull_varnos(), IndexOptInfo::rel, RelOptInfo::relid, RestrictInfo::right_relids, and ScalarArrayOpExpr::useOr.

Referenced by match_clause_to_index().

2303 {
2304  Expr *clause = rinfo->clause;
2305  Index index_relid = index->rel->relid;
2306  Oid opfamily = index->opfamily[indexcol];
2307  Oid idxcollation = index->indexcollations[indexcol];
2308  Node *leftop,
2309  *rightop;
2310  Relids left_relids;
2311  Relids right_relids;
2312  Oid expr_op;
2313  Oid expr_coll;
2314  bool plain_op;
2315 
2316  /* First check for boolean-index cases. */
2317  if (IsBooleanOpfamily(opfamily))
2318  {
2319  if (match_boolean_index_clause((Node *) clause, indexcol, index))
2320  return true;
2321  }
2322 
2323  /*
2324  * Clause must be a binary opclause, or possibly a ScalarArrayOpExpr
2325  * (which is always binary, by definition). Or it could be a
2326  * RowCompareExpr, which we pass off to match_rowcompare_to_indexcol().
2327  * Or, if the index supports it, we can handle IS NULL/NOT NULL clauses.
2328  */
2329  if (is_opclause(clause))
2330  {
2331  leftop = get_leftop(clause);
2332  rightop = get_rightop(clause);
2333  if (!leftop || !rightop)
2334  return false;
2335  left_relids = rinfo->left_relids;
2336  right_relids = rinfo->right_relids;
2337  expr_op = ((OpExpr *) clause)->opno;
2338  expr_coll = ((OpExpr *) clause)->inputcollid;
2339  plain_op = true;
2340  }
2341  else if (clause && IsA(clause, ScalarArrayOpExpr))
2342  {
2343  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
2344 
2345  /* We only accept ANY clauses, not ALL */
2346  if (!saop->useOr)
2347  return false;
2348  leftop = (Node *) linitial(saop->args);
2349  rightop = (Node *) lsecond(saop->args);
2350  left_relids = NULL; /* not actually needed */
2351  right_relids = pull_varnos(rightop);
2352  expr_op = saop->opno;
2353  expr_coll = saop->inputcollid;
2354  plain_op = false;
2355  }
2356  else if (clause && IsA(clause, RowCompareExpr))
2357  {
2358  return match_rowcompare_to_indexcol(index, indexcol,
2359  opfamily, idxcollation,
2360  (RowCompareExpr *) clause);
2361  }
2362  else if (index->amsearchnulls && IsA(clause, NullTest))
2363  {
2364  NullTest *nt = (NullTest *) clause;
2365 
2366  if (!nt->argisrow &&
2367  match_index_to_operand((Node *) nt->arg, indexcol, index))
2368  return true;
2369  return false;
2370  }
2371  else
2372  return false;
2373 
2374  /*
2375  * Check for clauses of the form: (indexkey operator constant) or
2376  * (constant operator indexkey). See above notes about const-ness.
2377  */
2378  if (match_index_to_operand(leftop, indexcol, index) &&
2379  !bms_is_member(index_relid, right_relids) &&
2380  !contain_volatile_functions(rightop))
2381  {
2382  if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2383  is_indexable_operator(expr_op, opfamily, true))
2384  return true;
2385 
2386  /*
2387  * If we didn't find a member of the index's opfamily, see whether it
2388  * is a "special" indexable operator.
2389  */
2390  if (plain_op &&
2391  match_special_index_operator(clause, opfamily,
2392  idxcollation, true))
2393  return true;
2394  return false;
2395  }
2396 
2397  if (plain_op &&
2398  match_index_to_operand(rightop, indexcol, index) &&
2399  !bms_is_member(index_relid, left_relids) &&
2400  !contain_volatile_functions(leftop))
2401  {
2402  if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2403  is_indexable_operator(expr_op, opfamily, false))
2404  return true;
2405 
2406  /*
2407  * If we didn't find a member of the index's opfamily, see whether it
2408  * is a "special" indexable operator.
2409  */
2410  if (match_special_index_operator(clause, opfamily,
2411  idxcollation, false))
2412  return true;
2413  return false;
2414  }
2415 
2416  return false;
2417 }
#define IsBooleanOpfamily(opfamily)
Definition: indxpath.c:43
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
Oid * indexcollations
Definition: relation.h:600
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3161
Definition: nodes.h:508
Relids left_relids
Definition: relation.h:1664
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:950
unsigned int Oid
Definition: postgres_ext.h:31
static bool match_rowcompare_to_indexcol(IndexOptInfo *index, int indexcol, Oid opfamily, Oid idxcollation, RowCompareExpr *clause)
Definition: indxpath.c:2448
#define lsecond(l)
Definition: pg_list.h:114
RelOptInfo * rel
Definition: relation.h:590
#define linitial(l)
Definition: pg_list.h:110
#define is_opclause(clause)
Definition: clauses.h:20
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
static bool match_boolean_index_clause(Node *clause, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3287
Expr * arg
Definition: primnodes.h:1156
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:46
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: relation.h:518
Expr * clause
Definition: relation.h:1637
unsigned int Index
Definition: c.h:362
Relids right_relids
Definition: relation.h:1665
#define NULL
Definition: c.h:226
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
Oid * opfamily
Definition: relation.h:601
static bool is_indexable_operator(Oid expr_op, Oid opfamily, bool indexkey_on_left)
Definition: indxpath.c:2428
static bool match_special_index_operator(Expr *clause, Oid opfamily, Oid idxcollation, bool indexkey_on_left)
Definition: indxpath.c:3331
bool argisrow
Definition: primnodes.h:1158
bool amsearchnulls
Definition: relation.h:629
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
static Expr * match_clause_to_ordering_op ( IndexOptInfo index,
int  indexcol,
Expr clause,
Oid  pk_opfamily 
)
static

Definition at line 2650 of file indxpath.c.

References OpExpr::args, 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(), NULL, IndexOptInfo::opfamily, OpExpr::opfuncid, and OpExpr::opno.

Referenced by match_pathkeys_to_index().

2654 {
2655  Oid opfamily = index->opfamily[indexcol];
2656  Oid idxcollation = index->indexcollations[indexcol];
2657  Node *leftop,
2658  *rightop;
2659  Oid expr_op;
2660  Oid expr_coll;
2661  Oid sortfamily;
2662  bool commuted;
2663 
2664  /*
2665  * Clause must be a binary opclause.
2666  */
2667  if (!is_opclause(clause))
2668  return NULL;
2669  leftop = get_leftop(clause);
2670  rightop = get_rightop(clause);
2671  if (!leftop || !rightop)
2672  return NULL;
2673  expr_op = ((OpExpr *) clause)->opno;
2674  expr_coll = ((OpExpr *) clause)->inputcollid;
2675 
2676  /*
2677  * We can forget the whole thing right away if wrong collation.
2678  */
2679  if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
2680  return NULL;
2681 
2682  /*
2683  * Check for clauses of the form: (indexkey operator constant) or
2684  * (constant operator indexkey).
2685  */
2686  if (match_index_to_operand(leftop, indexcol, index) &&
2687  !contain_var_clause(rightop) &&
2688  !contain_volatile_functions(rightop))
2689  {
2690  commuted = false;
2691  }
2692  else if (match_index_to_operand(rightop, indexcol, index) &&
2693  !contain_var_clause(leftop) &&
2694  !contain_volatile_functions(leftop))
2695  {
2696  /* Might match, but we need a commuted operator */
2697  expr_op = get_commutator(expr_op);
2698  if (expr_op == InvalidOid)
2699  return NULL;
2700  commuted = true;
2701  }
2702  else
2703  return NULL;
2704 
2705  /*
2706  * Is the (commuted) operator an ordering operator for the opfamily? And
2707  * if so, does it yield the right sorting semantics?
2708  */
2709  sortfamily = get_op_opfamily_sortfamily(expr_op, opfamily);
2710  if (sortfamily != pk_opfamily)
2711  return NULL;
2712 
2713  /* We have a match. Return clause or a commuted version thereof. */
2714  if (commuted)
2715  {
2716  OpExpr *newclause = makeNode(OpExpr);
2717 
2718  /* flat-copy all the fields of clause */
2719  memcpy(newclause, clause, sizeof(OpExpr));
2720 
2721  /* commute it */
2722  newclause->opno = expr_op;
2723  newclause->opfuncid = InvalidOid;
2724  newclause->args = list_make2(rightop, leftop);
2725 
2726  clause = (Expr *) newclause;
2727  }
2728 
2729  return clause;
2730 }
#define list_make2(x1, x2)
Definition: pg_list.h:134
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1281
Oid * indexcollations
Definition: relation.h:600
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:3161
Definition: nodes.h:508
bool contain_var_clause(Node *node)
Definition: var.c:331
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:950
unsigned int Oid
Definition: postgres_ext.h:31
#define is_opclause(clause)
Definition: clauses.h:20
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:46
Oid opfuncid
Definition: primnodes.h:474
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
Oid * opfamily
Definition: relation.h:601
Oid opno
Definition: primnodes.h:473
List * args
Definition: primnodes.h:479
static void match_clauses_to_index ( IndexOptInfo index,
List clauses,
IndexClauseSet clauseset 
)
static

Definition at line 2168 of file indxpath.c.

References castNode, lfirst, and match_clause_to_index().

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

2171 {
2172  ListCell *lc;
2173 
2174  foreach(lc, clauses)
2175  {
2176  RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(lc));
2177 
2178  match_clause_to_index(index, rinfo, clauseset);
2179  }
2180 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
static void match_clause_to_index(IndexOptInfo *index, RestrictInfo *rinfo, IndexClauseSet *clauseset)
Definition: indxpath.c:2199
#define lfirst(lc)
Definition: pg_list.h:106
static void match_eclass_clauses_to_index ( PlannerInfo root,
IndexOptInfo index,
IndexClauseSet clauseset 
)
static

Definition at line 2130 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::ncolumns, and IndexOptInfo::rel.

Referenced by create_index_paths().

2132 {
2133  int indexcol;
2134 
2135  /* No work if rel is not in any such ECs */
2136  if (!index->rel->has_eclass_joins)
2137  return;
2138 
2139  for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
2140  {
2142  List *clauses;
2143 
2144  /* Generate clauses, skipping any that join to lateral_referencers */
2145  arg.index = index;
2146  arg.indexcol = indexcol;
2148  index->rel,
2150  (void *) &arg,
2151  index->rel->lateral_referencers);
2152 
2153  /*
2154  * We have to check whether the results actually do match the index,
2155  * since for non-btree indexes the EC's equality operators might not
2156  * be in the index opclass (cf ec_member_matches_indexcol).
2157  */
2158  match_clauses_to_index(index, clauses, clauseset);
2159  }
2160 }
bool has_eclass_joins
Definition: relation.h:551
static bool ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, EquivalenceMember *em, void *arg)
Definition: indxpath.c:2888
RelOptInfo * rel
Definition: relation.h:590
int ncolumns
Definition: relation.h:598
Relids lateral_referencers
Definition: relation.h:526
IndexOptInfo * index
Definition: indxpath.c:77
static void match_clauses_to_index(IndexOptInfo *index, List *clauses, IndexClauseSet *clauseset)
Definition: indxpath.c:2168
void * arg
Definition: pg_list.h:45
List * generate_implied_equalities_for_column(PlannerInfo *root, RelOptInfo *rel, ec_matches_callback_type callback, void *callback_arg, Relids prohibited_rels)
Definition: equivclass.c:2172
bool match_index_to_operand ( Node operand,
int  indexcol,
IndexOptInfo index 
)

Definition at line 3161 of file indxpath.c.

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

Referenced by adjust_rowcompare_for_index(), deconstruct_indexquals(), ec_member_matches_indexcol(), expand_boolean_index_clause(), get_actual_variable_range(), match_boolean_index_clause(), match_clause_to_indexcol(), match_clause_to_ordering_op(), match_rowcompare_to_indexcol(), and relation_has_unique_index_for().

3164 {
3165  int indkey;
3166 
3167  /*
3168  * Ignore any RelabelType node above the operand. This is needed to be
3169  * able to apply indexscanning in binary-compatible-operator cases. Note:
3170  * we can assume there is at most one RelabelType node;
3171  * eval_const_expressions() will have simplified if more than one.
3172  */
3173  if (operand && IsA(operand, RelabelType))
3174  operand = (Node *) ((RelabelType *) operand)->arg;
3175 
3176  indkey = index->indexkeys[indexcol];
3177  if (indkey != 0)
3178  {
3179  /*
3180  * Simple index column; operand must be a matching Var.
3181  */
3182  if (operand && IsA(operand, Var) &&
3183  index->rel->relid == ((Var *) operand)->varno &&
3184  indkey == ((Var *) operand)->varattno)
3185  return true;
3186  }
3187  else
3188  {
3189  /*
3190  * Index expression; find the correct expression. (This search could
3191  * be avoided, at the cost of complicating all the callers of this
3192  * routine; doesn't seem worth it.)
3193  */
3194  ListCell *indexpr_item;
3195  int i;
3196  Node *indexkey;
3197 
3198  indexpr_item = list_head(index->indexprs);
3199  for (i = 0; i < indexcol; i++)
3200  {
3201  if (index->indexkeys[i] == 0)
3202  {
3203  if (indexpr_item == NULL)
3204  elog(ERROR, "wrong number of index expressions");
3205  indexpr_item = lnext(indexpr_item);
3206  }
3207  }
3208  if (indexpr_item == NULL)
3209  elog(ERROR, "wrong number of index expressions");
3210  indexkey = (Node *) lfirst(indexpr_item);
3211 
3212  /*
3213  * Does it match the operand? Again, strip any relabeling.
3214  */
3215  if (indexkey && IsA(indexkey, RelabelType))
3216  indexkey = (Node *) ((RelabelType *) indexkey)->arg;
3217 
3218  if (equal(indexkey, operand))
3219  return true;
3220  }
3221 
3222  return false;
3223 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2870
Definition: nodes.h:508
Definition: primnodes.h:141
RelOptInfo * rel
Definition: relation.h:590
#define ERROR
Definition: elog.h:43
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
Index relid
Definition: relation.h:518
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
int i
void * arg
int * indexkeys
Definition: relation.h:599
#define elog
Definition: elog.h:219
List * indexprs
Definition: relation.h:610
static void match_join_clauses_to_index ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet clauseset,
List **  joinorclauses 
)
static

Definition at line 2100 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().

2104 {
2105  ListCell *lc;
2106 
2107  /* Scan the rel's join clauses */
2108  foreach(lc, rel->joininfo)
2109  {
2110  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2111 
2112  /* Check if clause can be moved to this rel */
2113  if (!join_clause_is_movable_to(rinfo, rel))
2114  continue;
2115 
2116  /* Potentially usable, so see if it matches the index or is an OR */
2117  if (restriction_is_or_clause(rinfo))
2118  *joinorclauses = lappend(*joinorclauses, rinfo);
2119  else
2120  match_clause_to_index(index, rinfo, clauseset);
2121  }
2122 }
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:293
static void match_clause_to_index(IndexOptInfo *index, RestrictInfo *rinfo, IndexClauseSet *clauseset)
Definition: indxpath.c:2199
List * joininfo
Definition: relation.h:549
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
bool join_clause_is_movable_to(RestrictInfo *rinfo, RelOptInfo *baserel)
Definition: restrictinfo.c:435
static void match_pathkeys_to_index ( IndexOptInfo index,
List pathkeys,
List **  orderby_clauses_p,
List **  clause_columns_p 
)
static

Definition at line 2536 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(), IndexOptInfo::ncolumns, NIL, PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, IndexOptInfo::rel, and RelOptInfo::relids.

Referenced by build_index_paths().

2539 {
2540  List *orderby_clauses = NIL;
2541  List *clause_columns = NIL;
2542  ListCell *lc1;
2543 
2544  *orderby_clauses_p = NIL; /* set default results */
2545  *clause_columns_p = NIL;
2546 
2547  /* Only indexes with the amcanorderbyop property are interesting here */
2548  if (!index->amcanorderbyop)
2549  return;
2550 
2551  foreach(lc1, pathkeys)
2552  {
2553  PathKey *pathkey = (PathKey *) lfirst(lc1);
2554  bool found = false;
2555  ListCell *lc2;
2556 
2557  /*
2558  * Note: for any failure to match, we just return NIL immediately.
2559  * There is no value in matching just some of the pathkeys.
2560  */
2561 
2562  /* Pathkey must request default sort order for the target opfamily */
2563  if (pathkey->pk_strategy != BTLessStrategyNumber ||
2564  pathkey->pk_nulls_first)
2565  return;
2566 
2567  /* If eclass is volatile, no hope of using an indexscan */
2568  if (pathkey->pk_eclass->ec_has_volatile)
2569  return;
2570 
2571  /*
2572  * Try to match eclass member expression(s) to index. Note that child
2573  * EC members are considered, but only when they belong to the target
2574  * relation. (Unlike regular members, the same expression could be a
2575  * child member of more than one EC. Therefore, the same index could
2576  * be considered to match more than one pathkey list, which is OK
2577  * here. See also get_eclass_for_sort_expr.)
2578  */
2579  foreach(lc2, pathkey->pk_eclass->ec_members)
2580  {
2581  EquivalenceMember *member = (EquivalenceMember *) lfirst(lc2);
2582  int indexcol;
2583 
2584  /* No possibility of match if it references other relations */
2585  if (!bms_equal(member->em_relids, index->rel->relids))
2586  continue;
2587 
2588  /*
2589  * We allow any column of the index to match each pathkey; they
2590  * don't have to match left-to-right as you might expect. This is
2591  * correct for GiST, which is the sole existing AM supporting
2592  * amcanorderbyop. We might need different logic in future for
2593  * other implementations.
2594  */
2595  for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
2596  {
2597  Expr *expr;
2598 
2599  expr = match_clause_to_ordering_op(index,
2600  indexcol,
2601  member->em_expr,
2602  pathkey->pk_opfamily);
2603  if (expr)
2604  {
2605  orderby_clauses = lappend(orderby_clauses, expr);
2606  clause_columns = lappend_int(clause_columns, indexcol);
2607  found = true;
2608  break;
2609  }
2610  }
2611 
2612  if (found) /* don't want to look at remaining members */
2613  break;
2614  }
2615 
2616  if (!found) /* fail if no match for this pathkey */
2617  return;
2618  }
2619 
2620  *orderby_clauses_p = orderby_clauses; /* success! */
2621  *clause_columns_p = clause_columns;
2622 }
#define NIL
Definition: pg_list.h:69
static Expr * match_clause_to_ordering_op(IndexOptInfo *index, int indexcol, Expr *clause, Oid pk_opfamily)
Definition: indxpath.c:2650
int pk_strategy
Definition: relation.h:793
RelOptInfo * rel
Definition: relation.h:590
bool pk_nulls_first
Definition: relation.h:794
bool amcanorderbyop
Definition: relation.h:626
Relids relids
Definition: relation.h:490
int ncolumns
Definition: relation.h:598
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
Relids em_relids
Definition: relation.h:763
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:791
bool ec_has_volatile
Definition: relation.h:720
Oid pk_opfamily
Definition: relation.h:792
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:45
List * ec_members
Definition: relation.h:714
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:130
static void match_restriction_clauses_to_index ( RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet clauseset 
)
static

Definition at line 2086 of file indxpath.c.

References IndexOptInfo::indrestrictinfo, and match_clauses_to_index().

Referenced by create_index_paths().

2088 {
2089  /* We can ignore clauses that are implied by the index predicate */
2090  match_clauses_to_index(index, index->indrestrictinfo, clauseset);
2091 }
List * indrestrictinfo
Definition: relation.h:615
static void match_clauses_to_index(IndexOptInfo *index, List *clauses, IndexClauseSet *clauseset)
Definition: indxpath.c:2168
static bool match_rowcompare_to_indexcol ( IndexOptInfo index,
int  indexcol,
Oid  opfamily,
Oid  idxcollation,
RowCompareExpr clause 
)
static

Definition at line 2448 of file indxpath.c.

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

Referenced by match_clause_to_indexcol().

2453 {
2454  Index index_relid = index->rel->relid;
2455  Node *leftop,
2456  *rightop;
2457  Oid expr_op;
2458  Oid expr_coll;
2459 
2460  /* Forget it if we're not dealing with a btree index */
2461  if (index->relam != BTREE_AM_OID)
2462  return false;
2463 
2464  /*
2465  * We could do the matching on the basis of insisting that the opfamily
2466  * shown in the RowCompareExpr be the same as the index column's opfamily,
2467  * but that could fail in the presence of reverse-sort opfamilies: it'd be
2468  * a matter of chance whether RowCompareExpr had picked the forward or
2469  * reverse-sort family. So look only at the operator, and match if it is
2470  * a member of the index's opfamily (after commutation, if the indexkey is
2471  * on the right). We'll worry later about whether any additional
2472  * operators are matchable to the index.
2473  */
2474  leftop = (Node *) linitial(clause->largs);
2475  rightop = (Node *) linitial(clause->rargs);
2476  expr_op = linitial_oid(clause->opnos);
2477  expr_coll = linitial_oid(clause->inputcollids);
2478 
2479  /* Collations must match, if relevant */
2480  if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
2481  return false;
2482 
2483  /*
2484  * These syntactic tests are the same as in match_clause_to_indexcol()
2485  */
2486  if (match_index_to_operand(leftop, indexcol, index) &&
2487  !bms_is_member(index_relid, pull_varnos(rightop)) &&
2488  !contain_volatile_functions(rightop))
2489  {
2490  /* OK, indexkey is on left */
2491  }
2492  else if (match_index_to_operand(rightop, indexcol, index) &&
2493  !bms_is_member(index_relid, pull_varnos(leftop)) &&
2494  !contain_volatile_functions(leftop))
2495  {
2496  /* indexkey is on right, so commute the operator */
2497  expr_op = get_commutator(expr_op);
2498  if (expr_op == InvalidOid)
2499  return false;
2500  }
2501  else
2502  return false;
2503 
2504  /* We're good if the operator is the right type of opfamily member */
2505  switch (get_op_opfamily_strategy(expr_op, opfamily))
2506  {
2507  case BTLessStrategyNumber:
2511  return true;
2512  }
2513 
2514  return false;
2515 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1281
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3161
#define BTREE_AM_OID
Definition: pg_am.h:70
Definition: nodes.h:508
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:950
unsigned int Oid
Definition: postgres_ext.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
RelOptInfo * rel
Definition: relation.h:590
#define linitial(l)
Definition: pg_list.h:110
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:46
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: relation.h:518
unsigned int Index
Definition: c.h:362
#define InvalidOid
Definition: postgres_ext.h:36
#define linitial_oid(l)
Definition: pg_list.h:112
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Definition: lsyscache.c:80
List * inputcollids
Definition: primnodes.h:1010
#define BTLessStrategyNumber
Definition: stratnum.h:29
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
static bool match_special_index_operator ( Expr clause,
Oid  opfamily,
Oid  idxcollation,
bool  indexkey_on_left 
)
static

Definition at line 3331 of file indxpath.c.

References BPCHAR_BTREE_FAM_OID, BPCHAR_PATTERN_BTREE_FAM_OID, BYTEA_BTREE_FAM_OID, Const::constvalue, DatumGetPointer, get_rightop(), IsA, lc_collate_is_c(), NAME_BTREE_FAM_OID, NETWORK_BTREE_FAM_OID, NULL, OID_BPCHAR_ICLIKE_OP, OID_BPCHAR_ICREGEXEQ_OP, OID_BPCHAR_LIKE_OP, OID_BPCHAR_REGEXEQ_OP, OID_BYTEA_LIKE_OP, OID_INET_SUB_OP, OID_INET_SUBEQ_OP, OID_NAME_ICLIKE_OP, OID_NAME_ICREGEXEQ_OP, OID_NAME_LIKE_OP, OID_NAME_REGEXEQ_OP, OID_TEXT_ICLIKE_OP, OID_TEXT_ICREGEXEQ_OP, OID_TEXT_LIKE_OP, OID_TEXT_REGEXEQ_OP, pattern_fixed_prefix(), Pattern_Prefix_Exact, Pattern_Prefix_None, Pattern_Type_Like, Pattern_Type_Like_IC, Pattern_Type_Regex, Pattern_Type_Regex_IC, pfree(), TEXT_BTREE_FAM_OID, TEXT_PATTERN_BTREE_FAM_OID, and TEXT_SPGIST_FAM_OID.

Referenced by match_clause_to_indexcol().

3333 {
3334  bool isIndexable = false;
3335  Node *rightop;
3336  Oid expr_op;
3337  Oid expr_coll;
3338  Const *patt;
3339  Const *prefix = NULL;
3341 
3342  /*
3343  * Currently, all known special operators require the indexkey on the
3344  * left, but this test could be pushed into the switch statement if some
3345  * are added that do not...
3346  */
3347  if (!indexkey_on_left)
3348  return false;
3349 
3350  /* we know these will succeed */
3351  rightop = get_rightop(clause);
3352  expr_op = ((OpExpr *) clause)->opno;
3353  expr_coll = ((OpExpr *) clause)->inputcollid;
3354 
3355  /* again, required for all current special ops: */
3356  if (!IsA(rightop, Const) ||
3357  ((Const *) rightop)->constisnull)
3358  return false;
3359  patt = (Const *) rightop;
3360 
3361  switch (expr_op)
3362  {
3363  case OID_TEXT_LIKE_OP:
3364  case OID_BPCHAR_LIKE_OP:
3365  case OID_NAME_LIKE_OP:
3366  /* the right-hand const is type text for all of these */
3367  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like, expr_coll,
3368  &prefix, NULL);
3369  isIndexable = (pstatus != Pattern_Prefix_None);
3370  break;
3371 
3372  case OID_BYTEA_LIKE_OP:
3373  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like, expr_coll,
3374  &prefix, NULL);
3375  isIndexable = (pstatus != Pattern_Prefix_None);
3376  break;
3377 
3378  case OID_TEXT_ICLIKE_OP:
3379  case OID_BPCHAR_ICLIKE_OP:
3380  case OID_NAME_ICLIKE_OP:
3381  /* the right-hand const is type text for all of these */
3382  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC, expr_coll,
3383  &prefix, NULL);
3384  isIndexable = (pstatus != Pattern_Prefix_None);
3385  break;
3386 
3387  case OID_TEXT_REGEXEQ_OP:
3388  case OID_BPCHAR_REGEXEQ_OP:
3389  case OID_NAME_REGEXEQ_OP:
3390  /* the right-hand const is type text for all of these */
3391  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex, expr_coll,
3392  &prefix, NULL);
3393  isIndexable = (pstatus != Pattern_Prefix_None);
3394  break;
3395 
3396  case OID_TEXT_ICREGEXEQ_OP:
3398  case OID_NAME_ICREGEXEQ_OP:
3399  /* the right-hand const is type text for all of these */
3400  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC, expr_coll,
3401  &prefix, NULL);
3402  isIndexable = (pstatus != Pattern_Prefix_None);
3403  break;
3404 
3405  case OID_INET_SUB_OP:
3406  case OID_INET_SUBEQ_OP:
3407  isIndexable = true;
3408  break;
3409  }
3410 
3411  if (prefix)
3412  {
3413  pfree(DatumGetPointer(prefix->constvalue));
3414  pfree(prefix);
3415  }
3416 
3417  /* done if the expression doesn't look indexable */
3418  if (!isIndexable)
3419  return false;
3420 
3421  /*
3422  * Must also check that index's opfamily supports the operators we will
3423  * want to apply. (A hash index, for example, will not support ">=".)
3424  * Currently, only btree and spgist support the operators we need.
3425  *
3426  * Note: actually, in the Pattern_Prefix_Exact case, we only need "=" so a
3427  * hash index would work. Currently it doesn't seem worth checking for
3428  * that, however.
3429  *
3430  * We insist on the opfamily being the specific one we expect, else we'd
3431  * do the wrong thing if someone were to make a reverse-sort opfamily with
3432  * the same operators.
3433  *
3434  * The non-pattern opclasses will not sort the way we need in most non-C
3435  * locales. We can use such an index anyway for an exact match (simple
3436  * equality), but not for prefix-match cases. Note that here we are
3437  * looking at the index's collation, not the expression's collation --
3438  * this test is *not* dependent on the LIKE/regex operator's collation.
3439  */
3440  switch (expr_op)
3441  {
3442  case OID_TEXT_LIKE_OP:
3443  case OID_TEXT_ICLIKE_OP:
3444  case OID_TEXT_REGEXEQ_OP:
3445  case OID_TEXT_ICREGEXEQ_OP:
3446  isIndexable =
3447  (opfamily == TEXT_PATTERN_BTREE_FAM_OID) ||
3448  (opfamily == TEXT_SPGIST_FAM_OID) ||
3449  (opfamily == TEXT_BTREE_FAM_OID &&
3450  (pstatus == Pattern_Prefix_Exact ||
3451  lc_collate_is_c(idxcollation)));
3452  break;
3453 
3454  case OID_BPCHAR_LIKE_OP:
3455  case OID_BPCHAR_ICLIKE_OP:
3456  case OID_BPCHAR_REGEXEQ_OP:
3458  isIndexable =
3459  (opfamily == BPCHAR_PATTERN_BTREE_FAM_OID) ||
3460  (opfamily == BPCHAR_BTREE_FAM_OID &&
3461  (pstatus == Pattern_Prefix_Exact ||
3462  lc_collate_is_c(idxcollation)));
3463  break;
3464 
3465  case OID_NAME_LIKE_OP:
3466  case OID_NAME_ICLIKE_OP:
3467  case OID_NAME_REGEXEQ_OP:
3468  case OID_NAME_ICREGEXEQ_OP:
3469  /* name uses locale-insensitive sorting */
3470  isIndexable = (opfamily == NAME_BTREE_FAM_OID);
3471  break;
3472 
3473  case OID_BYTEA_LIKE_OP:
3474  isIndexable = (opfamily == BYTEA_BTREE_FAM_OID);
3475  break;
3476 
3477  case OID_INET_SUB_OP:
3478  case OID_INET_SUBEQ_OP:
3479  isIndexable = (opfamily == NETWORK_BTREE_FAM_OID);
3480  break;
3481  }
3482 
3483  return isIndexable;
3484 }
Datum constvalue
Definition: primnodes.h:174
#define NAME_BTREE_FAM_OID
Definition: pg_opfamily.h:91
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
#define OID_INET_SUBEQ_OP
Definition: pg_operator.h:1161
#define NETWORK_BTREE_FAM_OID
Definition: pg_opfamily.h:79
#define BPCHAR_BTREE_FAM_OID
Definition: pg_opfamily.h:68
#define OID_TEXT_REGEXEQ_OP
Definition: pg_operator.h:503
Definition: nodes.h:508
#define OID_TEXT_LIKE_OP
Definition: pg_operator.h:895
#define TEXT_SPGIST_FAM_OID
Definition: pg_opfamily.h:153
unsigned int Oid
Definition: postgres_ext.h:31
#define OID_BYTEA_LIKE_OP
Definition: pg_operator.h:1380
#define OID_NAME_ICREGEXEQ_OP
Definition: pg_operator.h:907
#define BYTEA_BTREE_FAM_OID
Definition: pg_opfamily.h:71
void pfree(void *pointer)
Definition: mcxt.c:992
#define TEXT_PATTERN_BTREE_FAM_OID
Definition: pg_opfamily.h:113
#define OID_BPCHAR_LIKE_OP
Definition: pg_operator.h:900
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1123
#define OID_NAME_ICLIKE_OP
Definition: pg_operator.h:1190
#define TEXT_BTREE_FAM_OID
Definition: pg_opfamily.h:103
Pattern_Prefix_Status pattern_fixed_prefix(Const *patt, Pattern_Type ptype, Oid collation, Const **prefix, Selectivity *rest_selec)
Definition: selfuncs.c:5471
#define NULL
Definition: c.h:226
#define OID_TEXT_ICREGEXEQ_OP
Definition: pg_operator.h:912
#define OID_BPCHAR_REGEXEQ_OP
Definition: pg_operator.h:759
#define OID_TEXT_ICLIKE_OP
Definition: pg_operator.h:1195
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
#define OID_BPCHAR_ICLIKE_OP
Definition: pg_operator.h:1200
#define DatumGetPointer(X)
Definition: postgres.h:557
#define OID_INET_SUB_OP
Definition: pg_operator.h:1158
#define OID_NAME_LIKE_OP
Definition: pg_operator.h:890
#define OID_BPCHAR_ICREGEXEQ_OP
Definition: pg_operator.h:917
#define OID_NAME_REGEXEQ_OP
Definition: pg_operator.h:498
Pattern_Prefix_Status
Definition: selfuncs.h:93
#define BPCHAR_PATTERN_BTREE_FAM_OID
Definition: pg_opfamily.h:115
static List * network_prefix_quals ( Node leftop,
Oid  expr_op,
Oid  opfamily,
Datum  rightop 
)
static

Definition at line 4141 of file indxpath.c.

References BOOLOID, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, elog, ERROR, get_opfamily_member(), INETOID, InvalidOid, lappend(), list_make1, make_opclause(), make_simple_restrictinfo, makeConst(), network_scan_first(), network_scan_last(), NIL, OID_INET_SUB_OP, and OID_INET_SUBEQ_OP.

Referenced by expand_indexqual_opclause().

4142 {
4143  bool is_eq;
4144  Oid datatype;
4145  Oid opr1oid;
4146  Oid opr2oid;
4147  Datum opr1right;
4148  Datum opr2right;
4149  List *result;
4150  Expr *expr;
4151 
4152  switch (expr_op)
4153  {
4154  case OID_INET_SUB_OP:
4155  datatype = INETOID;
4156  is_eq = false;
4157  break;
4158  case OID_INET_SUBEQ_OP:
4159  datatype = INETOID;
4160  is_eq = true;
4161  break;
4162  default:
4163  elog(ERROR, "unexpected operator: %u", expr_op);
4164  return NIL;
4165  }
4166 
4167  /*
4168  * create clause "key >= network_scan_first( rightop )", or ">" if the
4169  * operator disallows equality.
4170  */
4171  if (is_eq)
4172  {
4173  opr1oid = get_opfamily_member(opfamily, datatype, datatype,
4175  if (opr1oid == InvalidOid)
4176  elog(ERROR, "no >= operator for opfamily %u", opfamily);
4177  }
4178  else
4179  {
4180  opr1oid = get_opfamily_member(opfamily, datatype, datatype,
4182  if (opr1oid == InvalidOid)
4183  elog(ERROR, "no > operator for opfamily %u", opfamily);
4184  }
4185 
4186  opr1right = network_scan_first(rightop);
4187 
4188  expr = make_opclause(opr1oid, BOOLOID, false,
4189  (Expr *) leftop,
4190  (Expr *) makeConst(datatype, -1,
4191  InvalidOid, /* not collatable */
4192  -1, opr1right,
4193  false, false),
4195  result = list_make1(make_simple_restrictinfo(expr));
4196 
4197  /* create clause "key <= network_scan_last( rightop )" */
4198 
4199  opr2oid = get_opfamily_member(opfamily, datatype, datatype,
4201  if (opr2oid == InvalidOid)
4202  elog(ERROR, "no <= operator for opfamily %u", opfamily);
4203 
4204  opr2right = network_scan_last(rightop);
4205 
4206  expr = make_opclause(opr2oid, BOOLOID, false,
4207  (Expr *) leftop,
4208  (Expr *) makeConst(datatype, -1,
4209  InvalidOid, /* not collatable */
4210  -1, opr2right,
4211  false, false),
4213  result = lappend(result, make_simple_restrictinfo(expr));
4214 
4215  return result;
4216 }
#define NIL
Definition: pg_list.h:69
#define OID_INET_SUBEQ_OP
Definition: pg_operator.h:1161
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
Datum network_scan_last(Datum in)
Definition: network.c:1095
#define INETOID
Definition: pg_type.h:440
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: clauses.c:171
unsigned int Oid
Definition: postgres_ext.h:31
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:296
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define list_make1(x1)
Definition: pg_list.h:133
#define ERROR
Definition: elog.h:43
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
List * lappend(List *list, void *datum)
Definition: list.c:128
Datum network_scan_first(Datum in)
Definition: network.c:1081
uintptr_t Datum
Definition: postgres.h:374
#define InvalidOid
Definition: postgres_ext.h:36
#define BOOLOID
Definition: pg_type.h:288
#define OID_INET_SUB_OP
Definition: pg_operator.h:1158
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
#define make_simple_restrictinfo(clause)
Definition: restrictinfo.h:21
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
static int path_usage_comparator ( const void *  a,
const void *  b 
)
static

Definition at line 1568 of file indxpath.c.

References cost_bitmap_tree_node(), and PathClauseUsage::path.

Referenced by choose_bitmap_and().

1569 {
1570  PathClauseUsage *pa = *(PathClauseUsage *const *) a;
1571  PathClauseUsage *pb = *(PathClauseUsage *const *) b;
1572  Cost acost;
1573  Cost bcost;
1574  Selectivity aselec;
1575  Selectivity bselec;
1576 
1577  cost_bitmap_tree_node(pa->path, &acost, &aselec);
1578  cost_bitmap_tree_node(pb->path, &bcost, &bselec);
1579 
1580  /*
1581  * If costs are the same, sort by selectivity.
1582  */
1583  if (acost < bcost)
1584  return -1;
1585  if (acost > bcost)
1586  return 1;
1587 
1588  if (aselec < bselec)
1589  return -1;
1590  if (aselec > bselec)
1591  return 1;
1592 
1593  return 0;
1594 }
Path * path
Definition: indxpath.c:68
double Selectivity
Definition: nodes.h:631
double Cost
Definition: nodes.h:632
void cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec)
Definition: costsize.c:940
static List * prefix_quals ( Node leftop,
Oid  opfamily,
Oid  collation,
Const prefix,
Pattern_Prefix_Status  pstatus 
)
static

Definition at line 4015 of file indxpath.c.

References Assert, BOOLOID, BPCHAR_BTREE_FAM_OID, BPCHAR_PATTERN_BTREE_FAM_OID, BPCHAROID, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTLessStrategyNumber, BYTEA_BTREE_FAM_OID, BYTEAOID, byteaout(), Const::consttype, Const::constvalue, DatumGetCString, DirectFunctionCall1, elog, ERROR, fmgr_info(), get_opcode(), get_opfamily_member(), InvalidOid, lappend(), list_make1, make_greater_string(), make_opclause(), make_simple_restrictinfo, NAME_BTREE_FAM_OID, NAMEOID, NIL, Pattern_Prefix_Exact, Pattern_Prefix_None, pfree(), string_to_const(), TEXT_BTREE_FAM_OID, TEXT_PATTERN_BTREE_FAM_OID, TEXT_SPGIST_FAM_OID, TextDatumGetCString, and TEXTOID.

Referenced by expand_indexqual_opclause().

4017 {
4018  List *result;
4019  Oid datatype;
4020  Oid oproid;
4021  Expr *expr;
4022  FmgrInfo ltproc;
4023  Const *greaterstr;
4024 
4025  Assert(pstatus != Pattern_Prefix_None);
4026 
4027  switch (opfamily)
4028  {
4029  case TEXT_BTREE_FAM_OID:
4031  case TEXT_SPGIST_FAM_OID:
4032  datatype = TEXTOID;
4033  break;
4034 
4035  case BPCHAR_BTREE_FAM_OID:
4037  datatype = BPCHAROID;
4038  break;
4039 
4040  case NAME_BTREE_FAM_OID:
4041  datatype = NAMEOID;
4042  break;
4043 
4044  case BYTEA_BTREE_FAM_OID:
4045  datatype = BYTEAOID;
4046  break;
4047 
4048  default:
4049  /* shouldn't get here */
4050  elog(ERROR, "unexpected opfamily: %u", opfamily);
4051  return NIL;
4052  }
4053 
4054  /*
4055  * If necessary, coerce the prefix constant to the right type. The given
4056  * prefix constant is either text or bytea type.
4057  */
4058  if (prefix_const->consttype != datatype)
4059  {
4060  char *prefix;
4061 
4062  switch (prefix_const->consttype)
4063  {
4064  case TEXTOID:
4065  prefix = TextDatumGetCString(prefix_const->constvalue);
4066  break;
4067  case BYTEAOID:
4069  prefix_const->constvalue));
4070  break;
4071  default:
4072  elog(ERROR, "unexpected const type: %u",
4073  prefix_const->consttype);
4074  return NIL;
4075  }
4076  prefix_const = string_to_const(prefix, datatype);
4077  pfree(prefix);
4078  }
4079 
4080  /*
4081  * If we found an exact-match pattern, generate an "=" indexqual.
4082  */
4083  if (pstatus == Pattern_Prefix_Exact)
4084  {
4085  oproid = get_opfamily_member(opfamily, datatype, datatype,
4087  if (oproid == InvalidOid)
4088  elog(ERROR, "no = operator for opfamily %u", opfamily);
4089  expr = make_opclause(oproid, BOOLOID, false,
4090  (Expr *) leftop, (Expr *) prefix_const,
4091  InvalidOid, collation);
4092  result = list_make1(make_simple_restrictinfo(expr));
4093  return result;
4094  }
4095 
4096  /*
4097  * Otherwise, we have a nonempty required prefix of the values.
4098  *
4099  * We can always say "x >= prefix".
4100  */
4101  oproid = get_opfamily_member(opfamily, datatype, datatype,
4103  if (oproid == InvalidOid)
4104  elog(ERROR, "no >= operator for opfamily %u", opfamily);
4105  expr = make_opclause(oproid, BOOLOID, false,
4106  (Expr *) leftop, (Expr *) prefix_const,
4107  InvalidOid, collation);
4108  result = list_make1(make_simple_restrictinfo(expr));
4109 
4110  /*-------
4111  * If we can create a string larger than the prefix, we can say
4112  * "x < greaterstr". NB: we rely on make_greater_string() to generate
4113  * a guaranteed-greater string, not just a probably-greater string.
4114  * In general this is only guaranteed in C locale, so we'd better be
4115  * using a C-locale index collation.
4116  *-------
4117  */
4118  oproid = get_opfamily_member(opfamily, datatype, datatype,
4120  if (oproid == InvalidOid)
4121  elog(ERROR, "no < operator for opfamily %u", opfamily);
4122  fmgr_info(get_opcode(oproid), &ltproc);
4123  greaterstr = make_greater_string(prefix_const, &ltproc, collation);
4124  if (greaterstr)
4125  {
4126  expr = make_opclause(oproid, BOOLOID, false,
4127  (Expr *) leftop, (Expr *) greaterstr,
4128  InvalidOid, collation);
4129  result = lappend(result, make_simple_restrictinfo(expr));
4130  }
4131 
4132  return result;
4133 }
Datum byteaout(PG_FUNCTION_ARGS)
Definition: varlena.c:353
#define NIL
Definition: pg_list.h:69
Definition: fmgr.h:53
#define BPCHAROID
Definition: pg_type.h:492
#define NAME_BTREE_FAM_OID
Definition: pg_opfamily.h:91
#define NAMEOID
Definition: pg_type.h:300
#define TEXTOID
Definition: pg_type.h:324
#define BPCHAR_BTREE_FAM_OID
Definition: pg_opfamily.h:68
#define TEXT_SPGIST_FAM_OID
Definition: pg_opfamily.h:153
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: clauses.c:171
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
unsigned int Oid
Definition: postgres_ext.h:31
#define list_make1(x1)
Definition: pg_list.h:133
#define BYTEA_BTREE_FAM_OID
Definition: pg_opfamily.h:71
void pfree(void *pointer)
Definition: mcxt.c:992
#define TEXT_PATTERN_BTREE_FAM_OID
Definition: pg_opfamily.h:113
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:574
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
#define TEXT_BTREE_FAM_OID
Definition: pg_opfamily.h:103
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
List * lappend(List *list, void *datum)
Definition: list.c:128
#define TextDatumGetCString(d)
Definition: builtins.h:91
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1062
Const * make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation)
Definition: selfuncs.c:5836
#define Assert(condition)
Definition: c.h:671
static Const * string_to_const(const char *str, Oid datatype)
Definition: indxpath.c:4246
#define BOOLOID
Definition: pg_type.h:288
#define BYTEAOID
Definition: pg_type.h:292
#define elog
Definition: elog.h:219
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:45
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define make_simple_restrictinfo(clause)
Definition: restrictinfo.h:21
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
#define BPCHAR_PATTERN_BTREE_FAM_OID
Definition: pg_opfamily.h:115
bool relation_has_unique_index_for ( PlannerInfo root,
RelOptInfo rel,
List restrictlist,
List exprlist,
List oprlist 
)

Definition at line 2941 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, IndexOptInfo::ncolumns, NIL, 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().

2944 {
2945  ListCell *ic;
2946 
2947  Assert(list_length(exprlist) == list_length(oprlist));
2948 
2949  /* Short-circuit if no indexes... */
2950  if (rel->indexlist == NIL)
2951  return false;
2952 
2953  /*
2954  * Examine the rel's restriction clauses for usable var = const clauses
2955  * that we can add to the restrictlist.
2956  */
2957  foreach(ic, rel->baserestrictinfo)
2958  {
2959  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(ic);
2960 
2961  /*
2962  * Note: can_join won't be set for a restriction clause, but
2963  * mergeopfamilies will be if it has a mergejoinable operator and
2964  * doesn't contain volatile functions.
2965  */
2966  if (restrictinfo->mergeopfamilies == NIL)
2967  continue; /* not mergejoinable */
2968 
2969  /*
2970  * The clause certainly doesn't refer to anything but the given rel.
2971  * If either side is pseudoconstant then we can use it.
2972  */
2973  if (bms_is_empty(restrictinfo->left_relids))
2974  {
2975  /* righthand side is inner */
2976  restrictinfo->outer_is_left = true;
2977  }
2978  else if (bms_is_empty(restrictinfo->right_relids))
2979  {
2980  /* lefthand side is inner */
2981  restrictinfo->outer_is_left = false;
2982  }
2983  else
2984  continue;
2985 
2986  /* OK, add to list */
2987  restrictlist = lappend(restrictlist, restrictinfo);
2988  }
2989 
2990  /* Short-circuit the easy case */
2991  if (restrictlist == NIL && exprlist == NIL)
2992  return false;
2993 
2994  /* Examine each index of the relation ... */
2995  foreach(ic, rel->indexlist)
2996  {
2997  IndexOptInfo *ind = (IndexOptInfo *) lfirst(ic);
2998  int c;
2999 
3000  /*
3001  * If the index is not unique, or not immediately enforced, or if it's
3002  * a partial index that doesn't match the query, it's useless here.
3003  */
3004  if (!ind->unique || !ind->immediate ||
3005  (ind->indpred != NIL && !ind->predOK))
3006  continue;
3007 
3008  /*
3009  * Try to find each index column in the lists of conditions. This is
3010  * O(N^2) or worse, but we expect all the lists to be short.
3011  */
3012  for (c = 0; c < ind->ncolumns; c++)
3013  {
3014  bool matched = false;
3015  ListCell *lc;
3016  ListCell *lc2;
3017 
3018  foreach(lc, restrictlist)
3019  {
3020  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3021  Node *rexpr;
3022 
3023  /*
3024  * The condition's equality operator must be a member of the
3025  * index opfamily, else it is not asserting the right kind of
3026  * equality behavior for this index. We check this first
3027  * since it's probably cheaper than match_index_to_operand().
3028  */
3029  if (!list_member_oid(rinfo->mergeopfamilies, ind->opfamily[c]))
3030  continue;
3031 
3032  /*
3033  * XXX at some point we may need to check collations here too.
3034  * For the moment we assume all collations reduce to the same
3035  * notion of equality.
3036  */
3037 
3038  /* OK, see if the condition operand matches the index key */
3039  if (rinfo->outer_is_left)
3040  rexpr = get_rightop(rinfo->clause);
3041  else
3042  rexpr = get_leftop(rinfo->clause);
3043 
3044  if (match_index_to_operand(rexpr, c, ind))
3045  {
3046  matched = true; /* column is unique */
3047  break;
3048  }
3049  }
3050 
3051  if (matched)
3052  continue;
3053 
3054  forboth(lc, exprlist, lc2, oprlist)
3055  {
3056  Node *expr = (Node *) lfirst(lc);
3057  Oid opr = lfirst_oid(lc2);
3058 
3059  /* See if the expression matches the index key */
3060  if (!match_index_to_operand(expr, c, ind))
3061  continue;
3062 
3063  /*
3064  * The equality operator must be a member of the index
3065  * opfamily, else it is not asserting the right kind of
3066  * equality behavior for this index. We assume the caller
3067  * determined it is an equality operator, so we don't need to
3068  * check any more tightly than this.
3069  */
3070  if (!op_in_opfamily(opr, ind->opfamily[c]))
3071  continue;
3072 
3073  /*
3074  * XXX at some point we may need to check collations here too.
3075  * For the moment we assume all collations reduce to the same
3076  * notion of equality.
3077  */
3078 
3079  matched = true; /* column is unique */
3080  break;
3081  }
3082 
3083  if (!matched)
3084  break; /* no match; this index doesn't help us */
3085  }
3086 
3087  /* Matched all columns of this index? */
3088  if (c == ind->ncolumns)
3089  return true;
3090  }
3091 
3092  return false;
3093 }
#define NIL
Definition: pg_list.h:69
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:63
bool predOK
Definition: relation.h:620
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3161
List * baserestrictinfo
Definition: relation.h:544
Definition: nodes.h:508
Relids left_relids
Definition: relation.h:1664
bool immediate
Definition: relation.h:622
unsigned int Oid
Definition: postgres_ext.h:31
List * mergeopfamilies
Definition: relation.h:1682
bool unique
Definition: relation.h:621
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
char * c
bool outer_is_left
Definition: relation.h:1692
int ncolumns
Definition: relation.h:598
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1637
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:633
List * indexlist
Definition: relation.h:527
Relids right_relids
Definition: relation.h:1665
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
Oid * opfamily
Definition: relation.h:601