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

3828 {
3829  bool var_on_left;
3830  int op_strategy;
3831  Oid op_lefttype;
3832  Oid op_righttype;
3833  int matching_cols;
3834  Oid expr_op;
3835  List *opfamilies;
3836  List *lefttypes;
3837  List *righttypes;
3838  List *new_ops;
3839  ListCell *largs_cell;
3840  ListCell *rargs_cell;
3841  ListCell *opnos_cell;
3842  ListCell *collids_cell;
3843 
3844  /* We have to figure out (again) how the first col matches */
3845  var_on_left = match_index_to_operand((Node *) linitial(clause->largs),
3846  indexcol, index);
3847  Assert(var_on_left ||
3849  indexcol, index));
3850  *var_on_left_p = var_on_left;
3851 
3852  expr_op = linitial_oid(clause->opnos);
3853  if (!var_on_left)
3854  expr_op = get_commutator(expr_op);
3855  get_op_opfamily_properties(expr_op, index->opfamily[indexcol], false,
3856  &op_strategy,
3857  &op_lefttype,
3858  &op_righttype);
3859 
3860  /* Initialize returned list of which index columns are used */
3861  *indexcolnos = list_make1_int(indexcol);
3862 
3863  /* Build lists of the opfamilies and operator datatypes in case needed */
3864  opfamilies = list_make1_oid(index->opfamily[indexcol]);
3865  lefttypes = list_make1_oid(op_lefttype);
3866  righttypes = list_make1_oid(op_righttype);
3867 
3868  /*
3869  * See how many of the remaining columns match some index column in the
3870  * same way. As in match_clause_to_indexcol(), the "other" side of any
3871  * potential index condition is OK as long as it doesn't use Vars from the
3872  * indexed relation.
3873  */
3874  matching_cols = 1;
3875  largs_cell = lnext(list_head(clause->largs));
3876  rargs_cell = lnext(list_head(clause->rargs));
3877  opnos_cell = lnext(list_head(clause->opnos));
3878  collids_cell = lnext(list_head(clause->inputcollids));
3879 
3880  while (largs_cell != NULL)
3881  {
3882  Node *varop;
3883  Node *constop;
3884  int i;
3885 
3886  expr_op = lfirst_oid(opnos_cell);
3887  if (var_on_left)
3888  {
3889  varop = (Node *) lfirst(largs_cell);
3890  constop = (Node *) lfirst(rargs_cell);
3891  }
3892  else
3893  {
3894  varop = (Node *) lfirst(rargs_cell);
3895  constop = (Node *) lfirst(largs_cell);
3896  /* indexkey is on right, so commute the operator */
3897  expr_op = get_commutator(expr_op);
3898  if (expr_op == InvalidOid)
3899  break; /* operator is not usable */
3900  }
3901  if (bms_is_member(index->rel->relid, pull_varnos(constop)))
3902  break; /* no good, Var on wrong side */
3903  if (contain_volatile_functions(constop))
3904  break; /* no good, volatile comparison value */
3905 
3906  /*
3907  * The Var side can match any column of the index.
3908  */
3909  for (i = 0; i < index->ncolumns; i++)
3910  {
3911  if (match_index_to_operand(varop, i, index) &&
3912  get_op_opfamily_strategy(expr_op,
3913  index->opfamily[i]) == op_strategy &&
3915  lfirst_oid(collids_cell)))
3916  break;
3917  }
3918  if (i >= index->ncolumns)
3919  break; /* no match found */
3920 
3921  /* Add column number to returned list */
3922  *indexcolnos = lappend_int(*indexcolnos, i);
3923 
3924  /* Add opfamily and datatypes to lists */
3925  get_op_opfamily_properties(expr_op, index->opfamily[i], false,
3926  &op_strategy,
3927  &op_lefttype,
3928  &op_righttype);
3929  opfamilies = lappend_oid(opfamilies, index->opfamily[i]);
3930  lefttypes = lappend_oid(lefttypes, op_lefttype);
3931  righttypes = lappend_oid(righttypes, op_righttype);
3932 
3933  /* This column matches, keep scanning */
3934  matching_cols++;
3935  largs_cell = lnext(largs_cell);
3936  rargs_cell = lnext(rargs_cell);
3937  opnos_cell = lnext(opnos_cell);
3938  collids_cell = lnext(collids_cell);
3939  }
3940 
3941  /* Return clause as-is if it's all usable as index quals */
3942  if (matching_cols == list_length(clause->opnos))
3943  return (Expr *) clause;
3944 
3945  /*
3946  * We have to generate a subset rowcompare (possibly just one OpExpr). The
3947  * painful part of this is changing < to <= or > to >=, so deal with that
3948  * first.
3949  */
3950  if (op_strategy == BTLessEqualStrategyNumber ||
3951  op_strategy == BTGreaterEqualStrategyNumber)
3952  {
3953  /* easy, just use the same operators */
3954  new_ops = list_truncate(list_copy(clause->opnos), matching_cols);
3955  }
3956  else
3957  {
3958  ListCell *opfamilies_cell;
3959  ListCell *lefttypes_cell;
3960  ListCell *righttypes_cell;
3961 
3962  if (op_strategy == BTLessStrategyNumber)
3963  op_strategy = BTLessEqualStrategyNumber;
3964  else if (op_strategy == BTGreaterStrategyNumber)
3965  op_strategy = BTGreaterEqualStrategyNumber;
3966  else
3967  elog(ERROR, "unexpected strategy number %d", op_strategy);
3968  new_ops = NIL;
3969  lefttypes_cell = list_head(lefttypes);
3970  righttypes_cell = list_head(righttypes);
3971  foreach(opfamilies_cell, opfamilies)
3972  {
3973  Oid opfam = lfirst_oid(opfamilies_cell);
3974  Oid lefttype = lfirst_oid(lefttypes_cell);
3975  Oid righttype = lfirst_oid(righttypes_cell);
3976 
3977  expr_op = get_opfamily_member(opfam, lefttype, righttype,
3978  op_strategy);
3979  if (!OidIsValid(expr_op)) /* should not happen */
3980  elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
3981  op_strategy, lefttype, righttype, opfam);
3982  if (!var_on_left)
3983  {
3984  expr_op = get_commutator(expr_op);
3985  if (!OidIsValid(expr_op)) /* should not happen */
3986  elog(ERROR, "could not find commutator of member %d(%u,%u) of opfamily %u",
3987  op_strategy, lefttype, righttype, opfam);
3988  }
3989  new_ops = lappend_oid(new_ops, expr_op);
3990  lefttypes_cell = lnext(lefttypes_cell);
3991  righttypes_cell = lnext(righttypes_cell);
3992  }
3993  }
3994 
3995  /* If we have more than one matching col, create a subset rowcompare */
3996  if (matching_cols > 1)
3997  {
3999 
4000  if (var_on_left)
4001  rc->rctype = (RowCompareType) op_strategy;
4002  else
4003  rc->rctype = (op_strategy == BTLessEqualStrategyNumber) ?
4005  rc->opnos = new_ops;
4007  matching_cols);
4009  matching_cols);
4010  rc->largs = list_truncate(copyObject(clause->largs),
4011  matching_cols);
4012  rc->rargs = list_truncate(copyObject(clause->rargs),
4013  matching_cols);
4014  return (Expr *) rc;
4015  }
4016  else
4017  {
4018  return make_opclause(linitial_oid(new_ops), BOOLOID, false,
4019  copyObject(linitial(clause->largs)),
4020  copyObject(linitial(clause->rargs)),
4021  InvalidOid,
4022  linitial_oid(clause->inputcollids));
4023  }
4024 }
#define NIL
Definition: pg_list.h:69
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1313
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
Oid * indexcollations
Definition: relation.h:643
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3179
RowCompareType rctype
Definition: primnodes.h:1030
List * opfamilies
Definition: primnodes.h:1032
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:509
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: clauses.c:172
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:951
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:538
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
RelOptInfo * rel
Definition: relation.h:633
#define linitial(l)
Definition: pg_list.h:111
#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:145
int ncolumns
Definition: relation.h:641
#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:553
#define list_make1_oid(x1)
Definition: pg_list.h:151
#define InvalidOid
Definition: postgres_ext.h:36
RowCompareType
Definition: primnodes.h:1016
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
#define linitial_oid(l)
Definition: pg_list.h:113
static int list_length(const List *l)
Definition: pg_list.h:89
#define BOOLOID
Definition: pg_type.h:288
Oid * opfamily
Definition: relation.h:644
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
#define copyObject(obj)
Definition: nodes.h:621
List * inputcollids
Definition: primnodes.h:1033
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
#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 2016 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().

2020 {
2021  ListCell *lc;
2022 
2023  foreach(lc, root->join_info_list)
2024  {
2025  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
2026 
2027  if (sjinfo->jointype == JOIN_SEMI &&
2028  bms_is_member(cur_relid, sjinfo->syn_lefthand) &&
2029  bms_is_member(outer_relid, sjinfo->syn_righthand))
2030  {
2031  /* Estimate number of unique-ified rows */
2032  double nraw;
2033  double nunique;
2034 
2035  nraw = approximate_joinrel_size(root, sjinfo->syn_righthand);
2036  nunique = estimate_num_groups(root,
2037  sjinfo->semi_rhs_exprs,
2038  nraw,
2039  NULL);
2040  if (rowcount > nunique)
2041  rowcount = nunique;
2042  }
2043  }
2044  return rowcount;
2045 }
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset)
Definition: selfuncs.c:3232
List * join_info_list
Definition: relation.h:250
static double approximate_joinrel_size(PlannerInfo *root, Relids relids)
Definition: indxpath.c:2059
Relids syn_lefthand
Definition: relation.h:1919
Relids syn_righthand
Definition: relation.h:1920
List * semi_rhs_exprs
Definition: relation.h:1928
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
JoinType jointype
Definition: relation.h:1921
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
static double approximate_joinrel_size ( PlannerInfo root,
Relids  relids 
)
static

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

2060 {
2061  double rowcount = 1.0;
2062  int relid;
2063 
2064  relid = -1;
2065  while ((relid = bms_next_member(relids, relid)) >= 0)
2066  {
2067  RelOptInfo *rel;
2068 
2069  /* Paranoia: ignore bogus relid indexes */
2070  if (relid >= root->simple_rel_array_size)
2071  continue;
2072  rel = root->simple_rel_array[relid];
2073  if (rel == NULL)
2074  continue;
2075  Assert(rel->relid == relid); /* sanity check on array */
2076 
2077  /* Relation could be proven empty, if so ignore */
2078  if (IS_DUMMY_REL(rel))
2079  continue;
2080 
2081  /* Otherwise, rel's rows estimate should be valid by now */
2082  Assert(rel->rows > 0);
2083 
2084  /* Accumulate product */
2085  rowcount *= rel->rows;
2086  }
2087  return rowcount;
2088 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
struct RelOptInfo ** simple_rel_array
Definition: relation.h:179
#define IS_DUMMY_REL(r)
Definition: relation.h:1187
int simple_rel_array_size
Definition: relation.h:180
Index relid
Definition: relation.h:553
double rows
Definition: relation.h:528
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static Cost bitmap_and_cost_est ( PlannerInfo root,
RelOptInfo rel,
List paths 
)
static

Definition at line 1642 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::parallel_workers, Path::param_info, Path::parent, BitmapHeapPath::path, BitmapAndPath::path, Path::pathkeys, Path::pathtarget, Path::pathtype, RelOptInfo::relid, RelOptInfo::reltarget, T_BitmapAnd, T_BitmapAndPath, T_BitmapHeapPath, T_BitmapHeapScan, Path::total_cost, and Path::type.

Referenced by choose_bitmap_and().

1643 {
1644  BitmapAndPath apath;
1645  BitmapHeapPath bpath;
1646  Relids required_outer;
1647 
1648  /* Set up a dummy BitmapAndPath */
1649  apath.path.type = T_BitmapAndPath;
1650  apath.path.pathtype = T_BitmapAnd;
1651  apath.path.parent = rel;
1652  apath.path.pathtarget = rel->reltarget;
1653  apath.path.param_info = NULL; /* not used in bitmap trees */
1654  apath.path.pathkeys = NIL;
1655  apath.bitmapquals = paths;
1656  cost_bitmap_and_node(&apath, root);
1657 
1658  /* Identify required outer rels, in case it's a parameterized scan */
1659  required_outer = get_bitmap_tree_required_outer((Path *) &apath);
1660 
1661  /* Set up a dummy BitmapHeapPath */
1662  bpath.path.type = T_BitmapHeapPath;
1663  bpath.path.pathtype = T_BitmapHeapScan;
1664  bpath.path.parent = rel;
1665  bpath.path.pathtarget = rel->reltarget;
1666  bpath.path.param_info = get_baserel_parampathinfo(root, rel,
1667  required_outer);
1668  bpath.path.pathkeys = NIL;
1669  bpath.bitmapqual = (Path *) &apath;
1670 
1671  /*
1672  * Check the cost of temporary path without considering parallelism.
1673  * Parallel bitmap heap path will be considered at later stage.
1674  */
1675  bpath.path.parallel_workers = 0;
1676 
1677  /* Now we can do cost_bitmap_heap_scan */
1678  cost_bitmap_heap_scan(&bpath.path, root, rel,
1679  bpath.path.param_info,
1680  (Path *) &apath,
1681  get_loop_count(root, rel->relid, required_outer));
1682 
1683  return bpath.path.total_cost;
1684 }
#define NIL
Definition: pg_list.h:69
PathTarget * pathtarget
Definition: relation.h:954
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:928
ParamPathInfo * get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
Definition: relnode.c:1035
NodeTag type
Definition: relation.h:949
int parallel_workers
Definition: relation.h:960
void cost_bitmap_and_node(BitmapAndPath *path, PlannerInfo *root)
Definition: costsize.c:1072
ParamPathInfo * param_info
Definition: relation.h:956
List * bitmapquals
Definition: relation.h:1074
NodeTag pathtype
Definition: relation.h:951
RelOptInfo * parent
Definition: relation.h:953
Path * bitmapqual
Definition: relation.h:1062
Index relid
Definition: relation.h:553
static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
Definition: indxpath.c:1963
Cost total_cost
Definition: relation.h:966
List * pathkeys
Definition: relation.h:968
#define NULL
Definition: c.h:229
static Relids get_bitmap_tree_required_outer(Path *bitmapqual)
Definition: indxpath.c:1749
struct PathTarget * reltarget
Definition: relation.h:536
Definition: relation.h:947
static Cost bitmap_scan_cost_est ( PlannerInfo root,
RelOptInfo rel,
Path ipath 
)
static

Definition at line 1606 of file indxpath.c.

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

Referenced by choose_bitmap_and().

1607 {
1608  BitmapHeapPath bpath;
1609  Relids required_outer;
1610 
1611  /* Identify required outer rels, in case it's a parameterized scan */
1612  required_outer = get_bitmap_tree_required_outer(ipath);
1613 
1614  /* Set up a dummy BitmapHeapPath */
1615  bpath.path.type = T_BitmapHeapPath;
1616  bpath.path.pathtype = T_BitmapHeapScan;
1617  bpath.path.parent = rel;
1618  bpath.path.pathtarget = rel->reltarget;
1619  bpath.path.param_info = get_baserel_parampathinfo(root, rel,
1620  required_outer);
1621  bpath.path.pathkeys = NIL;
1622  bpath.bitmapqual = ipath;
1623 
1624  /*
1625  * Check the cost of temporary path without considering parallelism.
1626  * Parallel bitmap heap path will be considered at later stage.
1627  */
1628  bpath.path.parallel_workers = 0;
1629  cost_bitmap_heap_scan(&bpath.path, root, rel,
1630  bpath.path.param_info,
1631  ipath,
1632  get_loop_count(root, rel->relid, required_outer));
1633 
1634  return bpath.path.total_cost;
1635 }
#define NIL
Definition: pg_list.h:69
PathTarget * pathtarget
Definition: relation.h:954
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:928
ParamPathInfo * get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
Definition: relnode.c:1035
NodeTag type
Definition: relation.h:949
int parallel_workers
Definition: relation.h:960
ParamPathInfo * param_info
Definition: relation.h:956
NodeTag pathtype
Definition: relation.h:951
RelOptInfo * parent
Definition: relation.h:953
Path * bitmapqual
Definition: relation.h:1062
Index relid
Definition: relation.h:553
static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
Definition: indxpath.c:1963
Cost total_cost
Definition: relation.h:966
List * pathkeys
Definition: relation.h:968
static Relids get_bitmap_tree_required_outer(Path *bitmapqual)
Definition: indxpath.c:1749
struct PathTarget * reltarget
Definition: relation.h:536
static bool bms_equal_any ( Relids  relids,
List relids_list 
)
static

Definition at line 708 of file indxpath.c.

References bms_equal(), and lfirst.

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

709 {
710  ListCell *lc;
711 
712  foreach(lc, relids_list)
713  {
714  if (bms_equal(relids, (Relids) lfirst(lc)))
715  return true;
716  }
717  return false;
718 }
#define lfirst(lc)
Definition: pg_list.h:106
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:131
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 859 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, result, IndexOptInfo::sortopfamily, ST_ANYSCAN, ST_BITMAPSCAN, ST_INDEXSCAN, and truncate_useless_pathkeys().

Referenced by build_paths_for_OR(), and get_index_paths().

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

Definition at line 1169 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, result, and ST_BITMAPSCAN.

Referenced by generate_bitmap_or_paths().

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

Definition at line 1864 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, RelOptInfo::reltarget, and result.

Referenced by build_index_paths().

1865 {
1866  bool result;
1867  Bitmapset *attrs_used = NULL;
1868  Bitmapset *index_canreturn_attrs = NULL;
1869  ListCell *lc;
1870  int i;
1871 
1872  /* Index-only scans must be enabled */
1873  if (!enable_indexonlyscan)
1874  return false;
1875 
1876  /*
1877  * Check that all needed attributes of the relation are available from the
1878  * index.
1879  */
1880 
1881  /*
1882  * First, identify all the attributes needed for joins or final output.
1883  * Note: we must look at rel's targetlist, not the attr_needed data,
1884  * because attr_needed isn't computed for inheritance child rels.
1885  */
1886  pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
1887 
1888  /*
1889  * Add all the attributes used by restriction clauses; but consider only
1890  * those clauses not implied by the index predicate, since ones that are
1891  * so implied don't need to be checked explicitly in the plan.
1892  *
1893  * Note: attributes used only in index quals would not be needed at
1894  * runtime either, if we are certain that the index is not lossy. However
1895  * it'd be complicated to account for that accurately, and it doesn't
1896  * matter in most cases, since we'd conclude that such attributes are
1897  * available from the index anyway.
1898  */
1899  foreach(lc, index->indrestrictinfo)
1900  {
1901  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1902 
1903  pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
1904  }
1905 
1906  /*
1907  * Construct a bitmapset of columns that the index can return back in an
1908  * index-only scan.
1909  */
1910  for (i = 0; i < index->ncolumns; i++)
1911  {
1912  int attno = index->indexkeys[i];
1913 
1914  /*
1915  * For the moment, we just ignore index expressions. It might be nice
1916  * to do something with them, later.
1917  */
1918  if (attno == 0)
1919  continue;
1920 
1921  if (index->canreturn[i])
1922  index_canreturn_attrs =
1923  bms_add_member(index_canreturn_attrs,
1925  }
1926 
1927  /* Do we have all the necessary attributes? */
1928  result = bms_is_subset(attrs_used, index_canreturn_attrs);
1929 
1930  bms_free(attrs_used);
1931  bms_free(index_canreturn_attrs);
1932 
1933  return result;
1934 }
Definition: nodes.h:509
return result
Definition: formatting.c:1632
#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:308
int ncolumns
Definition: relation.h:641
Index relid
Definition: relation.h:553
Expr * clause
Definition: relation.h:1747
List * exprs
Definition: relation.h:883
List * indrestrictinfo
Definition: relation.h:658
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
int i
int * indexkeys
Definition: relation.h:642
bool * canreturn
Definition: relation.h:649
struct PathTarget * reltarget
Definition: relation.h:536
bool enable_indexonlyscan
Definition: costsize.c:120
void check_index_predicates ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 2774 of file indxpath.c.

References PlannerInfo::all_baserels, Assert, RelOptInfo::baserestrictinfo, bms_difference(), bms_is_empty(), bms_union(), RestrictInfo::clause, contain_mutable_functions(), find_childrel_parents(), generate_join_implied_equalities(), get_plan_rowmark(), RelOptInfo::indexlist, IndexOptInfo::indpred, IndexOptInfo::indrestrictinfo, IS_SIMPLE_REL, join_clause_is_movable_to(), RelOptInfo::joininfo, lappend(), lfirst, list_concat(), list_copy(), list_make1, NIL, 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().

2775 {
2776  List *clauselist;
2777  bool have_partial;
2778  bool is_target_rel;
2779  Relids otherrels;
2780  ListCell *lc;
2781 
2782  /* Indexes are available only on base or "other" member relations. */
2783  Assert(IS_SIMPLE_REL(rel));
2784 
2785  /*
2786  * Initialize the indrestrictinfo lists to be identical to
2787  * baserestrictinfo, and check whether there are any partial indexes. If
2788  * not, this is all we need to do.
2789  */
2790  have_partial = false;
2791  foreach(lc, rel->indexlist)
2792  {
2794 
2795  index->indrestrictinfo = rel->baserestrictinfo;
2796  if (index->indpred)
2797  have_partial = true;
2798  }
2799  if (!have_partial)
2800  return;
2801 
2802  /*
2803  * Construct a list of clauses that we can assume true for the purpose of
2804  * proving the index(es) usable. Restriction clauses for the rel are
2805  * always usable, and so are any join clauses that are "movable to" this
2806  * rel. Also, we can consider any EC-derivable join clauses (which must
2807  * be "movable to" this rel, by definition).
2808  */
2809  clauselist = list_copy(rel->baserestrictinfo);
2810 
2811  /* Scan the rel's join clauses */
2812  foreach(lc, rel->joininfo)
2813  {
2814  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2815 
2816  /* Check if clause can be moved to this rel */
2817  if (!join_clause_is_movable_to(rinfo, rel))
2818  continue;
2819 
2820  clauselist = lappend(clauselist, rinfo);
2821  }
2822 
2823  /*
2824  * Add on any equivalence-derivable join clauses. Computing the correct
2825  * relid sets for generate_join_implied_equalities is slightly tricky
2826  * because the rel could be a child rel rather than a true baserel, and in
2827  * that case we must remove its parents' relid(s) from all_baserels.
2828  */
2829  if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
2830  otherrels = bms_difference(root->all_baserels,
2831  find_childrel_parents(root, rel));
2832  else
2833  otherrels = bms_difference(root->all_baserels, rel->relids);
2834 
2835  if (!bms_is_empty(otherrels))
2836  clauselist =
2837  list_concat(clauselist,
2839  bms_union(rel->relids,
2840  otherrels),
2841  otherrels,
2842  rel));
2843 
2844  /*
2845  * Normally we remove quals that are implied by a partial index's
2846  * predicate from indrestrictinfo, indicating that they need not be
2847  * checked explicitly by an indexscan plan using this index. However, if
2848  * the rel is a target relation of UPDATE/DELETE/SELECT FOR UPDATE, we
2849  * cannot remove such quals from the plan, because they need to be in the
2850  * plan so that they will be properly rechecked by EvalPlanQual testing.
2851  * Some day we might want to remove such quals from the main plan anyway
2852  * and pass them through to EvalPlanQual via a side channel; but for now,
2853  * we just don't remove implied quals at all for target relations.
2854  */
2855  is_target_rel = (rel->relid == root->parse->resultRelation ||
2856  get_plan_rowmark(root->rowMarks, rel->relid) != NULL);
2857 
2858  /*
2859  * Now try to prove each index predicate true, and compute the
2860  * indrestrictinfo lists for partial indexes. Note that we compute the
2861  * indrestrictinfo list even for non-predOK indexes; this might seem
2862  * wasteful, but we may be able to use such indexes in OR clauses, cf
2863  * generate_bitmap_or_paths().
2864  */
2865  foreach(lc, rel->indexlist)
2866  {
2867  IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
2868  ListCell *lcr;
2869 
2870  if (index->indpred == NIL)
2871  continue; /* ignore non-partial indexes here */
2872 
2873  if (!index->predOK) /* don't repeat work if already proven OK */
2874  index->predOK = predicate_implied_by(index->indpred, clauselist);
2875 
2876  /* If rel is an update target, leave indrestrictinfo as set above */
2877  if (is_target_rel)
2878  continue;
2879 
2880  /* Else compute indrestrictinfo as the non-implied quals */
2881  index->indrestrictinfo = NIL;
2882  foreach(lcr, rel->baserestrictinfo)
2883  {
2884  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcr);
2885 
2886  /* predicate_implied_by() assumes first arg is immutable */
2887  if (contain_mutable_functions((Node *) rinfo->clause) ||
2889  index->indpred))
2890  index->indrestrictinfo = lappend(index->indrestrictinfo, rinfo);
2891  }
2892  }
2893 }
#define NIL
Definition: pg_list.h:69
List * rowMarks
Definition: relation.h:256
Query * parse
Definition: relation.h:155
bool predOK
Definition: relation.h:663
RelOptKind reloptkind
Definition: relation.h:522
bool predicate_implied_by(List *predicate_list, List *restrictinfo_list)
Definition: predtest.c:128
List * baserestrictinfo
Definition: relation.h:584
int resultRelation
Definition: parsenodes.h:120
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:509
List * list_concat(List *list1, List *list2)
Definition: list.c:321
#define IS_SIMPLE_REL(rel)
Definition: relation.h:505
Definition: type.h:90
#define list_make1(x1)
Definition: pg_list.h:139
Relids all_baserels
Definition: relation.h:196
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:589
Relids relids
Definition: relation.h:525
Index relid
Definition: relation.h:553
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1747
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
List * indrestrictinfo
Definition: relation.h:658
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1000
List * indexlist
Definition: relation.h:562
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
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:878
List * indpred
Definition: relation.h:654
Definition: pg_list.h:45
static Path * choose_bitmap_and ( PlannerInfo root,
RelOptInfo rel,
List paths 
)
static

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

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

Definition at line 1702 of file indxpath.c.

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

Referenced by choose_bitmap_and().

1703 {
1705  Bitmapset *clauseids;
1706  ListCell *lc;
1707 
1708  result = (PathClauseUsage *) palloc(sizeof(PathClauseUsage));
1709  result->path = path;
1710 
1711  /* Recursively find the quals and preds used by the path */
1712  result->quals = NIL;
1713  result->preds = NIL;
1714  find_indexpath_quals(path, &result->quals, &result->preds);
1715 
1716  /* Build up a bitmapset representing the quals and preds */
1717  clauseids = NULL;
1718  foreach(lc, result->quals)
1719  {
1720  Node *node = (Node *) lfirst(lc);
1721 
1722  clauseids = bms_add_member(clauseids,
1723  find_list_position(node, clauselist));
1724  }
1725  foreach(lc, result->preds)
1726  {
1727  Node *node = (Node *) lfirst(lc);
1728 
1729  clauseids = bms_add_member(clauseids,
1730  find_list_position(node, clauselist));
1731  }
1732  result->clauseids = clauseids;
1733 
1734  return result;
1735 }
#define NIL
Definition: pg_list.h:69
static int find_list_position(Node *node, List **nodelist)
Definition: indxpath.c:1838
Path * path
Definition: indxpath.c:68
List * quals
Definition: indxpath.c:69
Definition: nodes.h:509
return result
Definition: formatting.c:1632
Bitmapset * clauseids
Definition: indxpath.c:71
static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
Definition: indxpath.c:1797
List * preds
Definition: indxpath.c:70
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
void * palloc(Size size)
Definition: mcxt.c:849
static void consider_index_join_clauses ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet rclauseset,
IndexClauseSet jclauseset,
IndexClauseSet eclauseset,
List **  bitindexpaths 
)
static

Definition at line 439 of file indxpath.c.

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

Referenced by create_index_paths().

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

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

Referenced by consider_index_join_clauses().

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

Definition at line 232 of file indxpath.c.

References add_path(), Assert, RelOptInfo::baserestrictinfo, bms_equal_any(), bms_is_subset(), choose_bitmap_and(), consider_index_join_clauses(), RelOptInfo::consider_parallel, create_bitmap_heap_path(), create_partial_bitmap_paths(), forboth, generate_bitmap_or_paths(), get_bitmap_tree_required_outer(), get_index_paths(), get_loop_count(), INDEX_MAX_KEYS, RelOptInfo::indexlist, IndexOptInfo::indpred, lappend(), RelOptInfo::lateral_relids, lfirst, list_concat(), match_eclass_clauses_to_index(), match_join_clauses_to_index(), match_restriction_clauses_to_index(), MemSet, IndexOptInfo::ncolumns, NIL, IndexClauseSet::nonempty, NULL, 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, 0);
341  add_path(rel, (Path *) bpath);
342 
343  /* create a partial bitmap heap path */
344  if (rel->consider_parallel && rel->lateral_relids == NULL)
345  create_partial_bitmap_paths(root, rel, bitmapqual);
346  }
347 
348  /*
349  * Likewise, if we found anything usable, generate BitmapHeapPaths for the
350  * most promising combinations of join bitmap index paths. Our strategy
351  * is to generate one such path for each distinct parameterization seen
352  * among the available bitmap index paths. This may look pretty
353  * expensive, but usually there won't be very many distinct
354  * parameterizations. (This logic is quite similar to that in
355  * consider_index_join_clauses, but we're working with whole paths not
356  * individual clauses.)
357  */
358  if (bitjoinpaths != NIL)
359  {
360  List *path_outer;
361  List *all_path_outers;
362  ListCell *lc;
363 
364  /*
365  * path_outer holds the parameterization of each path in bitjoinpaths
366  * (to save recalculating that several times), while all_path_outers
367  * holds all distinct parameterization sets.
368  */
369  path_outer = all_path_outers = NIL;
370  foreach(lc, bitjoinpaths)
371  {
372  Path *path = (Path *) lfirst(lc);
373  Relids required_outer;
374 
375  required_outer = get_bitmap_tree_required_outer(path);
376  path_outer = lappend(path_outer, required_outer);
377  if (!bms_equal_any(required_outer, all_path_outers))
378  all_path_outers = lappend(all_path_outers, required_outer);
379  }
380 
381  /* Now, for each distinct parameterization set ... */
382  foreach(lc, all_path_outers)
383  {
384  Relids max_outers = (Relids) lfirst(lc);
385  List *this_path_set;
386  Path *bitmapqual;
387  Relids required_outer;
388  double loop_count;
389  BitmapHeapPath *bpath;
390  ListCell *lcp;
391  ListCell *lco;
392 
393  /* Identify all the bitmap join paths needing no more than that */
394  this_path_set = NIL;
395  forboth(lcp, bitjoinpaths, lco, path_outer)
396  {
397  Path *path = (Path *) lfirst(lcp);
398  Relids p_outers = (Relids) lfirst(lco);
399 
400  if (bms_is_subset(p_outers, max_outers))
401  this_path_set = lappend(this_path_set, path);
402  }
403 
404  /*
405  * Add in restriction bitmap paths, since they can be used
406  * together with any join paths.
407  */
408  this_path_set = list_concat(this_path_set, bitindexpaths);
409 
410  /* Select best AND combination for this parameterization */
411  bitmapqual = choose_bitmap_and(root, rel, this_path_set);
412 
413  /* And push that path into the mix */
414  required_outer = get_bitmap_tree_required_outer(bitmapqual);
415  loop_count = get_loop_count(root, rel->relid, required_outer);
416  bpath = create_bitmap_heap_path(root, rel, bitmapqual,
417  required_outer, loop_count, 0);
418  add_path(rel, (Path *) bpath);
419  }
420  }
421 }
#define NIL
Definition: pg_list.h:69
bool predOK
Definition: relation.h:663
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
bool nonempty
Definition: indxpath.c:60
List * baserestrictinfo
Definition: relation.h:584
#define MemSet(start, val, len)
Definition: c.h:857
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:708
Relids lateral_relids
Definition: relation.h:550
static void match_eclass_clauses_to_index(PlannerInfo *root, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2145
static void get_index_paths(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauses, List **bitindexpaths)
Definition: indxpath.c:737
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
static Path * choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths)
Definition: indxpath.c:1372
int ncolumns
Definition: relation.h:641
Index relid
Definition: relation.h:553
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:1963
static void match_restriction_clauses_to_index(RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauseset)
Definition: indxpath.c:2101
static List * generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel, List *clauses, List *other_clauses)
Definition: indxpath.c:1265
List * indexlist
Definition: relation.h:562
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
#define INDEX_MAX_KEYS
bool consider_parallel
Definition: relation.h:533
static void match_join_clauses_to_index(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *clauseset, List **joinorclauses)
Definition: indxpath.c:2115
static Relids get_bitmap_tree_required_outer(Path *bitmapqual)
Definition: indxpath.c:1749
List * indpred
Definition: relation.h:654
void create_partial_bitmap_paths(PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual)
Definition: allpaths.c:3035
Definition: pg_list.h:45
Definition: relation.h:947
BitmapHeapPath * create_bitmap_heap_path(PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual, Relids required_outer, double loop_count, int parallel_degree)
Definition: pathnode.c:1067
static void consider_index_join_clauses(PlannerInfo *root, RelOptInfo *rel, IndexOptInfo *index, IndexClauseSet *rclauseset, IndexClauseSet *jclauseset, IndexClauseSet *eclauseset, List **bitindexpaths)
Definition: indxpath.c:439
static bool ec_member_matches_indexcol ( PlannerInfo root,
RelOptInfo rel,
EquivalenceClass ec,
EquivalenceMember em,
void *  arg 
)
static

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

2909 {
2910  IndexOptInfo *index = ((ec_member_matches_arg *) arg)->index;
2911  int indexcol = ((ec_member_matches_arg *) arg)->indexcol;
2912  Oid curFamily = index->opfamily[indexcol];
2913  Oid curCollation = index->indexcollations[indexcol];
2914 
2915  /*
2916  * If it's a btree index, we can reject it if its opfamily isn't
2917  * compatible with the EC, since no clause generated from the EC could be
2918  * used with the index. For non-btree indexes, we can't easily tell
2919  * whether clauses generated from the EC could be used with the index, so
2920  * don't check the opfamily. This might mean we return "true" for a
2921  * useless EC, so we have to recheck the results of
2922  * generate_implied_equalities_for_column; see
2923  * match_eclass_clauses_to_index.
2924  */
2925  if (index->relam == BTREE_AM_OID &&
2926  !list_member_oid(ec->ec_opfamilies, curFamily))
2927  return false;
2928 
2929  /* We insist on collation match for all index types, though */
2930  if (!IndexCollMatchesExprColl(curCollation, ec->ec_collation))
2931  return false;
2932 
2933  return match_index_to_operand((Node *) em->em_expr, indexcol, index);
2934 }
Oid * indexcollations
Definition: relation.h:643
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3179
#define BTREE_AM_OID
Definition: pg_am.h:70
Definition: nodes.h:509
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:771
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
Oid * opfamily
Definition: relation.h:644
void * arg
static bool eclass_already_used ( EquivalenceClass parent_ec,
Relids  oldrelids,
List indexjoinclauses 
)
static

Definition at line 685 of file indxpath.c.

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

Referenced by consider_index_join_outer_rels().

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

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

3613 {
3614  /* Direct match? */
3615  if (match_index_to_operand(clause, indexcol, index))
3616  {
3617  /* convert to indexkey = TRUE */
3619  (Expr *) clause,
3620  (Expr *) makeBoolConst(true, false),
3622  }
3623  /* NOT clause? */
3624  if (not_clause(clause))
3625  {
3626  Node *arg = (Node *) get_notclausearg((Expr *) clause);
3627 
3628  /* It must have matched the indexkey */
3629  Assert(match_index_to_operand(arg, indexcol, index));
3630  /* convert to indexkey = FALSE */
3632  (Expr *) arg,
3633  (Expr *) makeBoolConst(false, false),
3635  }
3636  if (clause && IsA(clause, BooleanTest))
3637  {
3638  BooleanTest *btest = (BooleanTest *) clause;
3639  Node *arg = (Node *) btest->arg;
3640 
3641  /* It must have matched the indexkey */
3642  Assert(match_index_to_operand(arg, indexcol, index));
3643  if (btest->booltesttype == IS_TRUE)
3644  {
3645  /* convert to indexkey = TRUE */
3647  (Expr *) arg,
3648  (Expr *) makeBoolConst(true, false),
3650  }
3651  if (btest->booltesttype == IS_FALSE)
3652  {
3653  /* convert to indexkey = FALSE */
3655  (Expr *) arg,
3656  (Expr *) makeBoolConst(false, false),
3658  }
3659  /* Oops */
3660  Assert(false);
3661  }
3662 
3663  return NULL;
3664 }
Expr * get_notclausearg(Expr *notclause)
Definition: clauses.c:265
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3179
Definition: nodes.h:509
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: clauses.c:172
#define BooleanEqualOperator
Definition: pg_operator.h:114
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:354
Expr * arg
Definition: primnodes.h:1202
bool not_clause(Node *clause)
Definition: clauses.c:236
BoolTestType booltesttype
Definition: primnodes.h:1203
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#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 3525 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().

3528 {
3529  List *indexquals = NIL;
3530  List *indexqualcols = NIL;
3531  ListCell *lcc,
3532  *lci;
3533 
3534  forboth(lcc, indexclauses, lci, indexclausecols)
3535  {
3536  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
3537  int indexcol = lfirst_int(lci);
3538  Expr *clause = rinfo->clause;
3539  Oid curFamily = index->opfamily[indexcol];
3540  Oid curCollation = index->indexcollations[indexcol];
3541 
3542  /* First check for boolean cases */
3543  if (IsBooleanOpfamily(curFamily))
3544  {
3545  Expr *boolqual;
3546 
3547  boolqual = expand_boolean_index_clause((Node *) clause,
3548  indexcol,
3549  index);
3550  if (boolqual)
3551  {
3552  indexquals = lappend(indexquals,
3553  make_simple_restrictinfo(boolqual));
3554  indexqualcols = lappend_int(indexqualcols, indexcol);
3555  continue;
3556  }
3557  }
3558 
3559  /*
3560  * Else it must be an opclause (usual case), ScalarArrayOp,
3561  * RowCompare, or NullTest
3562  */
3563  if (is_opclause(clause))
3564  {
3565  indexquals = list_concat(indexquals,
3567  curFamily,
3568  curCollation));
3569  /* expand_indexqual_opclause can produce multiple clauses */
3570  while (list_length(indexqualcols) < list_length(indexquals))
3571  indexqualcols = lappend_int(indexqualcols, indexcol);
3572  }
3573  else if (IsA(clause, ScalarArrayOpExpr))
3574  {
3575  /* no extra work at this time */
3576  indexquals = lappend(indexquals, rinfo);
3577  indexqualcols = lappend_int(indexqualcols, indexcol);
3578  }
3579  else if (IsA(clause, RowCompareExpr))
3580  {
3581  indexquals = lappend(indexquals,
3583  index,
3584  indexcol));
3585  indexqualcols = lappend_int(indexqualcols, indexcol);
3586  }
3587  else if (IsA(clause, NullTest))
3588  {
3589  Assert(index->amsearchnulls);
3590  indexquals = lappend(indexquals, rinfo);
3591  indexqualcols = lappend_int(indexqualcols, indexcol);
3592  }
3593  else
3594  elog(ERROR, "unsupported indexqual type: %d",
3595  (int) nodeTag(clause));
3596  }
3597 
3598  *indexquals_p = indexquals;
3599  *indexqualcols_p = indexqualcols;
3600 }
#define NIL
Definition: pg_list.h:69
#define IsBooleanOpfamily(opfamily)
Definition: indxpath.c:43
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
Oid * indexcollations
Definition: relation.h:643
Definition: nodes.h:509
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:3610
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:1747
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static RestrictInfo * expand_indexqual_rowcompare(RestrictInfo *rinfo, IndexOptInfo *index, int indexcol)
Definition: indxpath.c:3770
static int list_length(const List *l)
Definition: pg_list.h:89
#define nodeTag(nodeptr)
Definition: nodes.h:514
Oid * opfamily
Definition: relation.h:644
#define elog
Definition: elog.h:219
bool amsearchnulls
Definition: relation.h:672
static List * expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily, Oid idxcollation)
Definition: indxpath.c:3676
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 3676 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().

3677 {
3678  Expr *clause = rinfo->clause;
3679 
3680  /* we know these will succeed */
3681  Node *leftop = get_leftop(clause);
3682  Node *rightop = get_rightop(clause);
3683  Oid expr_op = ((OpExpr *) clause)->opno;
3684  Oid expr_coll = ((OpExpr *) clause)->inputcollid;
3685  Const *patt = (Const *) rightop;
3686  Const *prefix = NULL;
3687  Pattern_Prefix_Status pstatus;
3688 
3689  /*
3690  * LIKE and regex operators are not members of any btree index opfamily,
3691  * but they can be members of opfamilies for more exotic index types such
3692  * as GIN. Therefore, we should only do expansion if the operator is
3693  * actually not in the opfamily. But checking that requires a syscache
3694  * lookup, so it's best to first see if the operator is one we are
3695  * interested in.
3696  */
3697  switch (expr_op)
3698  {
3699  case OID_TEXT_LIKE_OP:
3700  case OID_BPCHAR_LIKE_OP:
3701  case OID_NAME_LIKE_OP:
3702  case OID_BYTEA_LIKE_OP:
3703  if (!op_in_opfamily(expr_op, opfamily))
3704  {
3705  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like, expr_coll,
3706  &prefix, NULL);
3707  return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
3708  }
3709  break;
3710 
3711  case OID_TEXT_ICLIKE_OP:
3712  case OID_BPCHAR_ICLIKE_OP:
3713  case OID_NAME_ICLIKE_OP:
3714  if (!op_in_opfamily(expr_op, opfamily))
3715  {
3716  /* the right-hand const is type text for all of these */
3717  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC, expr_coll,
3718  &prefix, NULL);
3719  return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
3720  }
3721  break;
3722 
3723  case OID_TEXT_REGEXEQ_OP:
3724  case OID_BPCHAR_REGEXEQ_OP:
3725  case OID_NAME_REGEXEQ_OP:
3726  if (!op_in_opfamily(expr_op, opfamily))
3727  {
3728  /* the right-hand const is type text for all of these */
3729  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex, expr_coll,
3730  &prefix, NULL);
3731  return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
3732  }
3733  break;
3734 
3735  case OID_TEXT_ICREGEXEQ_OP:
3737  case OID_NAME_ICREGEXEQ_OP:
3738  if (!op_in_opfamily(expr_op, opfamily))
3739  {
3740  /* the right-hand const is type text for all of these */
3741  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC, expr_coll,
3742  &prefix, NULL);
3743  return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
3744  }
3745  break;
3746 
3747  case OID_INET_SUB_OP:
3748  case OID_INET_SUBEQ_OP:
3749  if (!op_in_opfamily(expr_op, opfamily))
3750  {
3751  return network_prefix_quals(leftop, expr_op, opfamily,
3752  patt->constvalue);
3753  }
3754  break;
3755  }
3756 
3757  /* Default case: just make a list of the unmodified indexqual */
3758  return list_make1(rinfo);
3759 }
Datum constvalue
Definition: primnodes.h:196
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:63
#define OID_INET_SUBEQ_OP
Definition: pg_operator.h:1182
static List * network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily, Datum rightop)
Definition: indxpath.c:4159
#define OID_TEXT_REGEXEQ_OP
Definition: pg_operator.h:503
Definition: nodes.h:509
#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:1401
#define OID_NAME_ICREGEXEQ_OP
Definition: pg_operator.h:907
#define list_make1(x1)
Definition: pg_list.h:139
#define OID_BPCHAR_LIKE_OP
Definition: pg_operator.h:900
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
#define OID_NAME_ICLIKE_OP
Definition: pg_operator.h:1211
Expr * clause
Definition: relation.h:1747
static List * prefix_quals(Node *leftop, Oid opfamily, Oid collation, Const *prefix, Pattern_Prefix_Status pstatus)
Definition: indxpath.c:4033
Pattern_Prefix_Status pattern_fixed_prefix(Const *patt, Pattern_Type ptype, Oid collation, Const **prefix, Selectivity *rest_selec)
Definition: selfuncs.c:5665
#define NULL
Definition: c.h:229
#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:1216
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
#define OID_BPCHAR_ICLIKE_OP
Definition: pg_operator.h:1221
#define OID_INET_SUB_OP
Definition: pg_operator.h:1179
#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:94
static RestrictInfo * expand_indexqual_rowcompare ( RestrictInfo rinfo,
IndexOptInfo index,
int  indexcol 
)
static

Definition at line 3770 of file indxpath.c.

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

Referenced by expand_indexqual_conditions().

3773 {
3774  RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
3775  Expr *newclause;
3776  List *indexcolnos;
3777  bool var_on_left;
3778 
3779  newclause = adjust_rowcompare_for_index(clause,
3780  index,
3781  indexcol,
3782  &indexcolnos,
3783  &var_on_left);
3784 
3785  /*
3786  * If we didn't have to change the RowCompareExpr, return the original
3787  * RestrictInfo.
3788  */
3789  if (newclause == (Expr *) clause)
3790  return rinfo;
3791 
3792  /* Else we need a new RestrictInfo */
3793  return make_simple_restrictinfo(newclause);
3794 }
Expr * adjust_rowcompare_for_index(RowCompareExpr *clause, IndexOptInfo *index, int indexcol, List **indexcolnos, bool *var_on_left_p)
Definition: indxpath.c:3823
Expr * clause
Definition: relation.h:1747
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 1797 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().

1798 {
1799  if (IsA(bitmapqual, BitmapAndPath))
1800  {
1801  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1802  ListCell *l;
1803 
1804  foreach(l, apath->bitmapquals)
1805  {
1806  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1807  }
1808  }
1809  else if (IsA(bitmapqual, BitmapOrPath))
1810  {
1811  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1812  ListCell *l;
1813 
1814  foreach(l, opath->bitmapquals)
1815  {
1816  find_indexpath_quals((Path *) lfirst(l), quals, preds);
1817  }
1818  }
1819  else if (IsA(bitmapqual, IndexPath))
1820  {
1821  IndexPath *ipath = (IndexPath *) bitmapqual;
1822 
1823  *quals = list_concat(*quals, get_actual_clauses(ipath->indexclauses));
1824  *preds = list_concat(*preds, list_copy(ipath->indexinfo->indpred));
1825  }
1826  else
1827  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1828 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
IndexOptInfo * indexinfo
Definition: relation.h:1031
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:1032
List * bitmapquals
Definition: relation.h:1074
List * bitmapquals
Definition: relation.h:1087
static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
Definition: indxpath.c:1797
#define ERROR
Definition: elog.h:43
#define lfirst(lc)
Definition: pg_list.h:106
#define nodeTag(nodeptr)
Definition: nodes.h:514
#define elog
Definition: elog.h:219
List * indpred
Definition: relation.h:654
Definition: relation.h:947
static int find_list_position ( Node node,
List **  nodelist 
)
static

Definition at line 1838 of file indxpath.c.

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

Referenced by classify_index_clause_usage().

1839 {
1840  int i;
1841  ListCell *lc;
1842 
1843  i = 0;
1844  foreach(lc, *nodelist)
1845  {
1846  Node *oldnode = (Node *) lfirst(lc);
1847 
1848  if (equal(node, oldnode))
1849  return i;
1850  i++;
1851  }
1852 
1853  *nodelist = lappend(*nodelist, node);
1854 
1855  return i;
1856 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2962
Definition: nodes.h:509
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 1265 of file indxpath.c.

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

Referenced by create_index_paths().

1267 {
1268  List *result = NIL;
1269  List *all_clauses;
1270  ListCell *lc;
1271 
1272  /*
1273  * We can use both the current and other clauses as context for
1274  * build_paths_for_OR; no need to remove ORs from the lists.
1275  */
1276  all_clauses = list_concat(list_copy(clauses), other_clauses);
1277 
1278  foreach(lc, clauses)
1279  {
1280  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1281  List *pathlist;
1282  Path *bitmapqual;
1283  ListCell *j;
1284 
1285  /* Ignore RestrictInfos that aren't ORs */
1286  if (!restriction_is_or_clause(rinfo))
1287  continue;
1288 
1289  /*
1290  * We must be able to match at least one index to each of the arms of
1291  * the OR, else we can't use it.
1292  */
1293  pathlist = NIL;
1294  foreach(j, ((BoolExpr *) rinfo->orclause)->args)
1295  {
1296  Node *orarg = (Node *) lfirst(j);
1297  List *indlist;
1298 
1299  /* OR arguments should be ANDs or sub-RestrictInfos */
1300  if (and_clause(orarg))
1301  {
1302  List *andargs = ((BoolExpr *) orarg)->args;
1303 
1304  indlist = build_paths_for_OR(root, rel,
1305  andargs,
1306  all_clauses);
1307 
1308  /* Recurse in case there are sub-ORs */
1309  indlist = list_concat(indlist,
1310  generate_bitmap_or_paths(root, rel,
1311  andargs,
1312  all_clauses));
1313  }
1314  else
1315  {
1316  RestrictInfo *rinfo = castNode(RestrictInfo, orarg);
1317  List *orargs;
1318 
1320  orargs = list_make1(rinfo);
1321 
1322  indlist = build_paths_for_OR(root, rel,
1323  orargs,
1324  all_clauses);
1325  }
1326 
1327  /*
1328  * If nothing matched this arm, we can't do anything with this OR
1329  * clause.
1330  */
1331  if (indlist == NIL)
1332  {
1333  pathlist = NIL;
1334  break;
1335  }
1336 
1337  /*
1338  * OK, pick the most promising AND combination, and add it to
1339  * pathlist.
1340  */
1341  bitmapqual = choose_bitmap_and(root, rel, indlist);
1342  pathlist = lappend(pathlist, bitmapqual);
1343  }
1344 
1345  /*
1346  * If we have a match for every arm, then turn them into a
1347  * BitmapOrPath, and add to result list.
1348  */
1349  if (pathlist != NIL)
1350  {
1351  bitmapqual = (Path *) create_bitmap_or_path(root, rel, pathlist);
1352  result = lappend(result, bitmapqual);
1353  }
1354  }
1355 
1356  return result;
1357 }
#define NIL
Definition: pg_list.h:69
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
Expr * orclause
Definition: relation.h:1778
BitmapOrPath * create_bitmap_or_path(PlannerInfo *root, RelOptInfo *rel, List *bitmapquals)
Definition: pathnode.c:1136
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:509
List * list_concat(List *list1, List *list2)
Definition: list.c:321
return result
Definition: formatting.c:1632
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:293
#define list_make1(x1)
Definition: pg_list.h:139
#define lfirst_node(type, lc)
Definition: pg_list.h:109
bool and_clause(Node *clause)
Definition: clauses.c:314
static Path * choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths)
Definition: indxpath.c:1372
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:1169
static List * generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel, List *clauses, List *other_clauses)
Definition: indxpath.c:1265
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Definition: pg_list.h:45
Definition: relation.h:947
static Relids get_bitmap_tree_required_outer ( Path bitmapqual)
static

Definition at line 1749 of file indxpath.c.

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

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

1750 {
1751  Relids result = NULL;
1752  ListCell *lc;
1753 
1754  if (IsA(bitmapqual, IndexPath))
1755  {
1756  return bms_copy(PATH_REQ_OUTER(bitmapqual));
1757  }
1758  else if (IsA(bitmapqual, BitmapAndPath))
1759  {
1760  foreach(lc, ((BitmapAndPath *) bitmapqual)->bitmapquals)
1761  {
1762  result = bms_join(result,
1764  }
1765  }
1766  else if (IsA(bitmapqual, BitmapOrPath))
1767  {
1768  foreach(lc, ((BitmapOrPath *) bitmapqual)->bitmapquals)
1769  {
1770  result = bms_join(result,
1772  }
1773  }
1774  else
1775  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1776 
1777  return result;
1778 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
return result
Definition: formatting.c:1632
#define ERROR
Definition: elog.h:43
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:838
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
#define PATH_REQ_OUTER(path)
Definition: relation.h:973
#define nodeTag(nodeptr)
Definition: nodes.h:514
#define elog
Definition: elog.h:219
static Relids get_bitmap_tree_required_outer(Path *bitmapqual)
Definition: indxpath.c:1749
Definition: relation.h:947
static void get_index_paths ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet clauses,
List **  bitindexpaths 
)
static

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

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

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

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

1964 {
1965  double result;
1966  int outer_relid;
1967 
1968  /* For a non-parameterized path, just return 1.0 quickly */
1969  if (outer_relids == NULL)
1970  return 1.0;
1971 
1972  result = 0.0;
1973  outer_relid = -1;
1974  while ((outer_relid = bms_next_member(outer_relids, outer_relid)) >= 0)
1975  {
1976  RelOptInfo *outer_rel;
1977  double rowcount;
1978 
1979  /* Paranoia: ignore bogus relid indexes */
1980  if (outer_relid >= root->simple_rel_array_size)
1981  continue;
1982  outer_rel = root->simple_rel_array[outer_relid];
1983  if (outer_rel == NULL)
1984  continue;
1985  Assert(outer_rel->relid == outer_relid); /* sanity check on array */
1986 
1987  /* Other relation could be proven empty, if so ignore */
1988  if (IS_DUMMY_REL(outer_rel))
1989  continue;
1990 
1991  /* Otherwise, rel's rows estimate should be valid by now */
1992  Assert(outer_rel->rows > 0);
1993 
1994  /* Check to see if rel is on the inside of any semijoins */
1995  rowcount = adjust_rowcount_for_semijoins(root,
1996  cur_relid,
1997  outer_relid,
1998  outer_rel->rows);
1999 
2000  /* Remember smallest row count estimate among the outer rels */
2001  if (result == 0.0 || result > rowcount)
2002  result = rowcount;
2003  }
2004  /* Return 1.0 if we found no valid relations (shouldn't happen) */
2005  return (result > 0.0) ? result : 1.0;
2006 }
static double adjust_rowcount_for_semijoins(PlannerInfo *root, Index cur_relid, Index outer_relid, double rowcount)
Definition: indxpath.c:2016
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
return result
Definition: formatting.c:1632
struct RelOptInfo ** simple_rel_array
Definition: relation.h:179
#define IS_DUMMY_REL(r)
Definition: relation.h:1187
int simple_rel_array_size
Definition: relation.h:180
Index relid
Definition: relation.h:553
double rows
Definition: relation.h:528
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool indexcol_is_bool_constant_for_query ( IndexOptInfo index,
int  indexcol 
)

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

3131 {
3132  ListCell *lc;
3133 
3134  /* If the index isn't boolean, we can't possibly get a match */
3135  if (!IsBooleanOpfamily(index->opfamily[indexcol]))
3136  return false;
3137 
3138  /* Check each restriction clause for the index's rel */
3139  foreach(lc, index->rel->baserestrictinfo)
3140  {
3141  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3142 
3143  /*
3144  * As in match_clause_to_indexcol, never match pseudoconstants to
3145  * indexes. (It might be semantically okay to do so here, but the
3146  * odds of getting a match are negligible, so don't waste the cycles.)
3147  */
3148  if (rinfo->pseudoconstant)
3149  continue;
3150 
3151  /* See if we can match the clause's expression to the index column */
3152  if (match_boolean_index_clause((Node *) rinfo->clause, indexcol, index))
3153  return true;
3154  }
3155 
3156  return false;
3157 }
#define IsBooleanOpfamily(opfamily)
Definition: indxpath.c:43
List * baserestrictinfo
Definition: relation.h:584
bool pseudoconstant
Definition: relation.h:1755
Definition: nodes.h:509
RelOptInfo * rel
Definition: relation.h:633
static bool match_boolean_index_clause(Node *clause, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3305
Expr * clause
Definition: relation.h:1747
#define lfirst(lc)
Definition: pg_list.h:106
Oid * opfamily
Definition: relation.h:644
static bool is_indexable_operator ( Oid  expr_op,
Oid  opfamily,
bool  indexkey_on_left 
)
static

Definition at line 2443 of file indxpath.c.

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

Referenced by match_clause_to_indexcol().

2444 {
2445  /* Get the commuted operator if necessary */
2446  if (!indexkey_on_left)
2447  {
2448  expr_op = get_commutator(expr_op);
2449  if (expr_op == InvalidOid)
2450  return false;
2451  }
2452 
2453  /* OK if the (commuted) operator is a member of the index's opfamily */
2454  return op_in_opfamily(expr_op, opfamily);
2455 }
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:63
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1313
#define InvalidOid
Definition: postgres_ext.h:36
static bool match_boolean_index_clause ( Node clause,
int  indexcol,
IndexOptInfo index 
)
static

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

3308 {
3309  /* Direct match? */
3310  if (match_index_to_operand(clause, indexcol, index))
3311  return true;
3312  /* NOT clause? */
3313  if (not_clause(clause))
3314  {
3315  if (match_index_to_operand((Node *) get_notclausearg((Expr *) clause),
3316  indexcol, index))
3317  return true;
3318  }
3319 
3320  /*
3321  * Since we only consider clauses at top level of WHERE, we can convert
3322  * indexkey IS TRUE and indexkey IS FALSE to index searches as well. The
3323  * different meaning for NULL isn't important.
3324  */
3325  else if (clause && IsA(clause, BooleanTest))
3326  {
3327  BooleanTest *btest = (BooleanTest *) clause;
3328 
3329  if (btest->booltesttype == IS_TRUE ||
3330  btest->booltesttype == IS_FALSE)
3331  if (match_index_to_operand((Node *) btest->arg,
3332  indexcol, index))
3333  return true;
3334  }
3335  return false;
3336 }
Expr * get_notclausearg(Expr *notclause)
Definition: clauses.c:265
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3179
Definition: nodes.h:509
Expr * arg
Definition: primnodes.h:1202
bool not_clause(Node *clause)
Definition: clauses.c:236
BoolTestType booltesttype
Definition: primnodes.h:1203
static void match_clause_to_index ( IndexOptInfo index,
RestrictInfo rinfo,
IndexClauseSet clauseset 
)
static

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

2217 {
2218  int indexcol;
2219 
2220  /*
2221  * Never match pseudoconstants to indexes. (Normally a match could not
2222  * happen anyway, since a pseudoconstant clause couldn't contain a Var,
2223  * but what if someone builds an expression index on a constant? It's not
2224  * totally unreasonable to do so with a partial index, either.)
2225  */
2226  if (rinfo->pseudoconstant)
2227  return;
2228 
2229  /*
2230  * If clause can't be used as an indexqual because it must wait till after
2231  * some lower-security-level restriction clause, reject it.
2232  */
2233  if (!restriction_is_securely_promotable(rinfo, index->rel))
2234  return;
2235 
2236  /* OK, check each index column for a match */
2237  for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
2238  {
2239  if (match_clause_to_indexcol(index,
2240  indexcol,
2241  rinfo))
2242  {
2243  clauseset->indexclauses[indexcol] =
2244  list_append_unique_ptr(clauseset->indexclauses[indexcol],
2245  rinfo);
2246  clauseset->nonempty = true;
2247  return;
2248  }
2249  }
2250 }
bool nonempty
Definition: indxpath.c:60
bool pseudoconstant
Definition: relation.h:1755
RelOptInfo * rel
Definition: relation.h:633
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:2315
List * indexclauses[INDEX_MAX_KEYS]
Definition: indxpath.c:62
int ncolumns
Definition: relation.h:641
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 2315 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().

2318 {
2319  Expr *clause = rinfo->clause;
2320  Index index_relid = index->rel->relid;
2321  Oid opfamily = index->opfamily[indexcol];
2322  Oid idxcollation = index->indexcollations[indexcol];
2323  Node *leftop,
2324  *rightop;
2325  Relids left_relids;
2326  Relids right_relids;
2327  Oid expr_op;
2328  Oid expr_coll;
2329  bool plain_op;
2330 
2331  /* First check for boolean-index cases. */
2332  if (IsBooleanOpfamily(opfamily))
2333  {
2334  if (match_boolean_index_clause((Node *) clause, indexcol, index))
2335  return true;
2336  }
2337 
2338  /*
2339  * Clause must be a binary opclause, or possibly a ScalarArrayOpExpr
2340  * (which is always binary, by definition). Or it could be a
2341  * RowCompareExpr, which we pass off to match_rowcompare_to_indexcol().
2342  * Or, if the index supports it, we can handle IS NULL/NOT NULL clauses.
2343  */
2344  if (is_opclause(clause))
2345  {
2346  leftop = get_leftop(clause);
2347  rightop = get_rightop(clause);
2348  if (!leftop || !rightop)
2349  return false;
2350  left_relids = rinfo->left_relids;
2351  right_relids = rinfo->right_relids;
2352  expr_op = ((OpExpr *) clause)->opno;
2353  expr_coll = ((OpExpr *) clause)->inputcollid;
2354  plain_op = true;
2355  }
2356  else if (clause && IsA(clause, ScalarArrayOpExpr))
2357  {
2358  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
2359 
2360  /* We only accept ANY clauses, not ALL */
2361  if (!saop->useOr)
2362  return false;
2363  leftop = (Node *) linitial(saop->args);
2364  rightop = (Node *) lsecond(saop->args);
2365  left_relids = NULL; /* not actually needed */
2366  right_relids = pull_varnos(rightop);
2367  expr_op = saop->opno;
2368  expr_coll = saop->inputcollid;
2369  plain_op = false;
2370  }
2371  else if (clause && IsA(clause, RowCompareExpr))
2372  {
2373  return match_rowcompare_to_indexcol(index, indexcol,
2374  opfamily, idxcollation,
2375  (RowCompareExpr *) clause);
2376  }
2377  else if (index->amsearchnulls && IsA(clause, NullTest))
2378  {
2379  NullTest *nt = (NullTest *) clause;
2380 
2381  if (!nt->argisrow &&
2382  match_index_to_operand((Node *) nt->arg, indexcol, index))
2383  return true;
2384  return false;
2385  }
2386  else
2387  return false;
2388 
2389  /*
2390  * Check for clauses of the form: (indexkey operator constant) or
2391  * (constant operator indexkey). See above notes about const-ness.
2392  */
2393  if (match_index_to_operand(leftop, indexcol, index) &&
2394  !bms_is_member(index_relid, right_relids) &&
2395  !contain_volatile_functions(rightop))
2396  {
2397  if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2398  is_indexable_operator(expr_op, opfamily, true))
2399  return true;
2400 
2401  /*
2402  * If we didn't find a member of the index's opfamily, see whether it
2403  * is a "special" indexable operator.
2404  */
2405  if (plain_op &&
2406  match_special_index_operator(clause, opfamily,
2407  idxcollation, true))
2408  return true;
2409  return false;
2410  }
2411 
2412  if (plain_op &&
2413  match_index_to_operand(rightop, indexcol, index) &&
2414  !bms_is_member(index_relid, left_relids) &&
2415  !contain_volatile_functions(leftop))
2416  {
2417  if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2418  is_indexable_operator(expr_op, opfamily, false))
2419  return true;
2420 
2421  /*
2422  * If we didn't find a member of the index's opfamily, see whether it
2423  * is a "special" indexable operator.
2424  */
2425  if (match_special_index_operator(clause, opfamily,
2426  idxcollation, false))
2427  return true;
2428  return false;
2429  }
2430 
2431  return false;
2432 }
#define IsBooleanOpfamily(opfamily)
Definition: indxpath.c:43
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Oid * indexcollations
Definition: relation.h:643
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3179
Definition: nodes.h:509
Relids left_relids
Definition: relation.h:1774
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:951
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:2463
#define lsecond(l)
Definition: pg_list.h:116
RelOptInfo * rel
Definition: relation.h:633
#define linitial(l)
Definition: pg_list.h:111
#define is_opclause(clause)
Definition: clauses.h:20
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
static bool match_boolean_index_clause(Node *clause, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3305
Expr * arg
Definition: primnodes.h:1179
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:46
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: relation.h:553
Expr * clause
Definition: relation.h:1747
unsigned int Index
Definition: c.h:365
Relids right_relids
Definition: relation.h:1775
#define NULL
Definition: c.h:229
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
Oid * opfamily
Definition: relation.h:644
static bool is_indexable_operator(Oid expr_op, Oid opfamily, bool indexkey_on_left)
Definition: indxpath.c:2443
static bool match_special_index_operator(Expr *clause, Oid opfamily, Oid idxcollation, bool indexkey_on_left)
Definition: indxpath.c:3349
bool argisrow
Definition: primnodes.h:1181
bool amsearchnulls
Definition: relation.h:672
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
static Expr * match_clause_to_ordering_op ( IndexOptInfo index,
int  indexcol,
Expr clause,
Oid  pk_opfamily 
)
static

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

2669 {
2670  Oid opfamily = index->opfamily[indexcol];
2671  Oid idxcollation = index->indexcollations[indexcol];
2672  Node *leftop,
2673  *rightop;
2674  Oid expr_op;
2675  Oid expr_coll;
2676  Oid sortfamily;
2677  bool commuted;
2678 
2679  /*
2680  * Clause must be a binary opclause.
2681  */
2682  if (!is_opclause(clause))
2683  return NULL;
2684  leftop = get_leftop(clause);
2685  rightop = get_rightop(clause);
2686  if (!leftop || !rightop)
2687  return NULL;
2688  expr_op = ((OpExpr *) clause)->opno;
2689  expr_coll = ((OpExpr *) clause)->inputcollid;
2690 
2691  /*
2692  * We can forget the whole thing right away if wrong collation.
2693  */
2694  if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
2695  return NULL;
2696 
2697  /*
2698  * Check for clauses of the form: (indexkey operator constant) or
2699  * (constant operator indexkey).
2700  */
2701  if (match_index_to_operand(leftop, indexcol, index) &&
2702  !contain_var_clause(rightop) &&
2703  !contain_volatile_functions(rightop))
2704  {
2705  commuted = false;
2706  }
2707  else if (match_index_to_operand(rightop, indexcol, index) &&
2708  !contain_var_clause(leftop) &&
2709  !contain_volatile_functions(leftop))
2710  {
2711  /* Might match, but we need a commuted operator */
2712  expr_op = get_commutator(expr_op);
2713  if (expr_op == InvalidOid)
2714  return NULL;
2715  commuted = true;
2716  }
2717  else
2718  return NULL;
2719 
2720  /*
2721  * Is the (commuted) operator an ordering operator for the opfamily? And
2722  * if so, does it yield the right sorting semantics?
2723  */
2724  sortfamily = get_op_opfamily_sortfamily(expr_op, opfamily);
2725  if (sortfamily != pk_opfamily)
2726  return NULL;
2727 
2728  /* We have a match. Return clause or a commuted version thereof. */
2729  if (commuted)
2730  {
2731  OpExpr *newclause = makeNode(OpExpr);
2732 
2733  /* flat-copy all the fields of clause */
2734  memcpy(newclause, clause, sizeof(OpExpr));
2735 
2736  /* commute it */
2737  newclause->opno = expr_op;
2738  newclause->opfuncid = InvalidOid;
2739  newclause->args = list_make2(rightop, leftop);
2740 
2741  clause = (Expr *) newclause;
2742  }
2743 
2744  return clause;
2745 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1313
Oid * indexcollations
Definition: relation.h:643
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:3179
Definition: nodes.h:509
bool contain_var_clause(Node *node)
Definition: var.c:331
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:951
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:199
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:46
Oid opfuncid
Definition: primnodes.h:496
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
Oid * opfamily
Definition: relation.h:644
Oid opno
Definition: primnodes.h:495
List * args
Definition: primnodes.h:501
static void match_clauses_to_index ( IndexOptInfo index,
List clauses,
IndexClauseSet clauseset 
)
static

Definition at line 2183 of file indxpath.c.

References lfirst_node, and match_clause_to_index().

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

2186 {
2187  ListCell *lc;
2188 
2189  foreach(lc, clauses)
2190  {
2191  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
2192 
2193  match_clause_to_index(index, rinfo, clauseset);
2194  }
2195 }
static void match_clause_to_index(IndexOptInfo *index, RestrictInfo *rinfo, IndexClauseSet *clauseset)
Definition: indxpath.c:2214
#define lfirst_node(type, lc)
Definition: pg_list.h:109
static void match_eclass_clauses_to_index ( PlannerInfo root,
IndexOptInfo index,
IndexClauseSet clauseset 
)
static

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

2147 {
2148  int indexcol;
2149 
2150  /* No work if rel is not in any such ECs */
2151  if (!index->rel->has_eclass_joins)
2152  return;
2153 
2154  for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
2155  {
2157  List *clauses;
2158 
2159  /* Generate clauses, skipping any that join to lateral_referencers */
2160  arg.index = index;
2161  arg.indexcol = indexcol;
2163  index->rel,
2165  (void *) &arg,
2166  index->rel->lateral_referencers);
2167 
2168  /*
2169  * We have to check whether the results actually do match the index,
2170  * since for non-btree indexes the EC's equality operators might not
2171  * be in the index opclass (cf ec_member_matches_indexcol).
2172  */
2173  match_clauses_to_index(index, clauses, clauseset);
2174  }
2175 }
bool has_eclass_joins
Definition: relation.h:591
static bool ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, EquivalenceMember *em, void *arg)
Definition: indxpath.c:2906
RelOptInfo * rel
Definition: relation.h:633
int ncolumns
Definition: relation.h:641
Relids lateral_referencers
Definition: relation.h:561
IndexOptInfo * index
Definition: indxpath.c:77
static void match_clauses_to_index(IndexOptInfo *index, List *clauses, IndexClauseSet *clauseset)
Definition: indxpath.c:2183
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:2173
bool match_index_to_operand ( Node operand,
int  indexcol,
IndexOptInfo index 
)

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

3182 {
3183  int indkey;
3184 
3185  /*
3186  * Ignore any RelabelType node above the operand. This is needed to be
3187  * able to apply indexscanning in binary-compatible-operator cases. Note:
3188  * we can assume there is at most one RelabelType node;
3189  * eval_const_expressions() will have simplified if more than one.
3190  */
3191  if (operand && IsA(operand, RelabelType))
3192  operand = (Node *) ((RelabelType *) operand)->arg;
3193 
3194  indkey = index->indexkeys[indexcol];
3195  if (indkey != 0)
3196  {
3197  /*
3198  * Simple index column; operand must be a matching Var.
3199  */
3200  if (operand && IsA(operand, Var) &&
3201  index->rel->relid == ((Var *) operand)->varno &&
3202  indkey == ((Var *) operand)->varattno)
3203  return true;
3204  }
3205  else
3206  {
3207  /*
3208  * Index expression; find the correct expression. (This search could
3209  * be avoided, at the cost of complicating all the callers of this
3210  * routine; doesn't seem worth it.)
3211  */
3212  ListCell *indexpr_item;
3213  int i;
3214  Node *indexkey;
3215 
3216  indexpr_item = list_head(index->indexprs);
3217  for (i = 0; i < indexcol; i++)
3218  {
3219  if (index->indexkeys[i] == 0)
3220  {
3221  if (indexpr_item == NULL)
3222  elog(ERROR, "wrong number of index expressions");
3223  indexpr_item = lnext(indexpr_item);
3224  }
3225  }
3226  if (indexpr_item == NULL)
3227  elog(ERROR, "wrong number of index expressions");
3228  indexkey = (Node *) lfirst(indexpr_item);
3229 
3230  /*
3231  * Does it match the operand? Again, strip any relabeling.
3232  */
3233  if (indexkey && IsA(indexkey, RelabelType))
3234  indexkey = (Node *) ((RelabelType *) indexkey)->arg;
3235 
3236  if (equal(indexkey, operand))
3237  return true;
3238  }
3239 
3240  return false;
3241 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2962
Definition: nodes.h:509
Definition: primnodes.h:163
RelOptInfo * rel
Definition: relation.h:633
#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:553
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
int i
void * arg
int * indexkeys
Definition: relation.h:642
#define elog
Definition: elog.h:219
List * indexprs
Definition: relation.h:653
static void match_join_clauses_to_index ( PlannerInfo root,
RelOptInfo rel,
IndexOptInfo index,
IndexClauseSet clauseset,
List **  joinorclauses 
)
static

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

2119 {
2120  ListCell *lc;
2121 
2122  /* Scan the rel's join clauses */
2123  foreach(lc, rel->joininfo)
2124  {
2125  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2126 
2127  /* Check if clause can be moved to this rel */
2128  if (!join_clause_is_movable_to(rinfo, rel))
2129  continue;
2130 
2131  /* Potentially usable, so see if it matches the index or is an OR */
2132  if (restriction_is_or_clause(rinfo))
2133  *joinorclauses = lappend(*joinorclauses, rinfo);
2134  else
2135  match_clause_to_index(index, rinfo, clauseset);
2136  }
2137 }
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:2214
List * joininfo
Definition: relation.h:589
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 2551 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().

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

Definition at line 2101 of file indxpath.c.

References IndexOptInfo::indrestrictinfo, and match_clauses_to_index().

Referenced by create_index_paths().

2103 {
2104  /* We can ignore clauses that are implied by the index predicate */
2105  match_clauses_to_index(index, index->indrestrictinfo, clauseset);
2106 }
List * indrestrictinfo
Definition: relation.h:658
static void match_clauses_to_index(IndexOptInfo *index, List *clauses, IndexClauseSet *clauseset)
Definition: indxpath.c:2183
static bool match_rowcompare_to_indexcol ( IndexOptInfo index,
int  indexcol,
Oid  opfamily,
Oid  idxcollation,
RowCompareExpr clause 
)
static

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

2468 {
2469  Index index_relid = index->rel->relid;
2470  Node *leftop,
2471  *rightop;
2472  Oid expr_op;
2473  Oid expr_coll;
2474 
2475  /* Forget it if we're not dealing with a btree index */
2476  if (index->relam != BTREE_AM_OID)
2477  return false;
2478 
2479  /*
2480  * We could do the matching on the basis of insisting that the opfamily
2481  * shown in the RowCompareExpr be the same as the index column's opfamily,
2482  * but that could fail in the presence of reverse-sort opfamilies: it'd be
2483  * a matter of chance whether RowCompareExpr had picked the forward or
2484  * reverse-sort family. So look only at the operator, and match if it is
2485  * a member of the index's opfamily (after commutation, if the indexkey is
2486  * on the right). We'll worry later about whether any additional
2487  * operators are matchable to the index.
2488  */
2489  leftop = (Node *) linitial(clause->largs);
2490  rightop = (Node *) linitial(clause->rargs);
2491  expr_op = linitial_oid(clause->opnos);
2492  expr_coll = linitial_oid(clause->inputcollids);
2493 
2494  /* Collations must match, if relevant */
2495  if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
2496  return false;
2497 
2498  /*
2499  * These syntactic tests are the same as in match_clause_to_indexcol()
2500  */
2501  if (match_index_to_operand(leftop, indexcol, index) &&
2502  !bms_is_member(index_relid, pull_varnos(rightop)) &&
2503  !contain_volatile_functions(rightop))
2504  {
2505  /* OK, indexkey is on left */
2506  }
2507  else if (match_index_to_operand(rightop, indexcol, index) &&
2508  !bms_is_member(index_relid, pull_varnos(leftop)) &&
2509  !contain_volatile_functions(leftop))
2510  {
2511  /* indexkey is on right, so commute the operator */
2512  expr_op = get_commutator(expr_op);
2513  if (expr_op == InvalidOid)
2514  return false;
2515  }
2516  else
2517  return false;
2518 
2519  /* We're good if the operator is the right type of opfamily member */
2520  switch (get_op_opfamily_strategy(expr_op, opfamily))
2521  {
2522  case BTLessStrategyNumber:
2526  return true;
2527  }
2528 
2529  return false;
2530 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1313
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3179
#define BTREE_AM_OID
Definition: pg_am.h:70
Definition: nodes.h:509
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:951
unsigned int Oid
Definition: postgres_ext.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
RelOptInfo * rel
Definition: relation.h:633
#define linitial(l)
Definition: pg_list.h:111
#define IndexCollMatchesExprColl(idxcollation, exprcollation)
Definition: indxpath.c:46
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: relation.h:553
unsigned int Index
Definition: c.h:365
#define InvalidOid
Definition: postgres_ext.h:36
#define linitial_oid(l)
Definition: pg_list.h:113
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Definition: lsyscache.c:80
List * inputcollids
Definition: primnodes.h:1033
#define BTLessStrategyNumber
Definition: stratnum.h:29
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
#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 3349 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().

3351 {
3352  bool isIndexable = false;
3353  Node *rightop;
3354  Oid expr_op;
3355  Oid expr_coll;
3356  Const *patt;
3357  Const *prefix = NULL;
3359 
3360  /*
3361  * Currently, all known special operators require the indexkey on the
3362  * left, but this test could be pushed into the switch statement if some
3363  * are added that do not...
3364  */
3365  if (!indexkey_on_left)
3366  return false;
3367 
3368  /* we know these will succeed */
3369  rightop = get_rightop(clause);
3370  expr_op = ((OpExpr *) clause)->opno;
3371  expr_coll = ((OpExpr *) clause)->inputcollid;
3372 
3373  /* again, required for all current special ops: */
3374  if (!IsA(rightop, Const) ||
3375  ((Const *) rightop)->constisnull)
3376  return false;
3377  patt = (Const *) rightop;
3378 
3379  switch (expr_op)
3380  {
3381  case OID_TEXT_LIKE_OP:
3382  case OID_BPCHAR_LIKE_OP:
3383  case OID_NAME_LIKE_OP:
3384  /* the right-hand const is type text for all of these */
3385  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like, expr_coll,
3386  &prefix, NULL);
3387  isIndexable = (pstatus != Pattern_Prefix_None);
3388  break;
3389 
3390  case OID_BYTEA_LIKE_OP:
3391  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like, expr_coll,
3392  &prefix, NULL);
3393  isIndexable = (pstatus != Pattern_Prefix_None);
3394  break;
3395 
3396  case OID_TEXT_ICLIKE_OP:
3397  case OID_BPCHAR_ICLIKE_OP:
3398  case OID_NAME_ICLIKE_OP:
3399  /* the right-hand const is type text for all of these */
3400  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC, expr_coll,
3401  &prefix, NULL);
3402  isIndexable = (pstatus != Pattern_Prefix_None);
3403  break;
3404 
3405  case OID_TEXT_REGEXEQ_OP:
3406  case OID_BPCHAR_REGEXEQ_OP:
3407  case OID_NAME_REGEXEQ_OP:
3408  /* the right-hand const is type text for all of these */
3409  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex, expr_coll,
3410  &prefix, NULL);
3411  isIndexable = (pstatus != Pattern_Prefix_None);
3412  break;
3413 
3414  case OID_TEXT_ICREGEXEQ_OP:
3416  case OID_NAME_ICREGEXEQ_OP:
3417  /* the right-hand const is type text for all of these */
3418  pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC, expr_coll,
3419  &prefix, NULL);
3420  isIndexable = (pstatus != Pattern_Prefix_None);
3421  break;
3422 
3423  case OID_INET_SUB_OP:
3424  case OID_INET_SUBEQ_OP:
3425  isIndexable = true;
3426  break;
3427  }
3428 
3429  if (prefix)
3430  {
3431  pfree(DatumGetPointer(prefix->constvalue));
3432  pfree(prefix);
3433  }
3434 
3435  /* done if the expression doesn't look indexable */
3436  if (!isIndexable)
3437  return false;
3438 
3439  /*
3440  * Must also check that index's opfamily supports the operators we will
3441  * want to apply. (A hash index, for example, will not support ">=".)
3442  * Currently, only btree and spgist support the operators we need.
3443  *
3444  * Note: actually, in the Pattern_Prefix_Exact case, we only need "=" so a
3445  * hash index would work. Currently it doesn't seem worth checking for
3446  * that, however.
3447  *
3448  * We insist on the opfamily being the specific one we expect, else we'd
3449  * do the wrong thing if someone were to make a reverse-sort opfamily with
3450  * the same operators.
3451  *
3452  * The non-pattern opclasses will not sort the way we need in most non-C
3453  * locales. We can use such an index anyway for an exact match (simple
3454  * equality), but not for prefix-match cases. Note that here we are
3455  * looking at the index's collation, not the expression's collation --
3456  * this test is *not* dependent on the LIKE/regex operator's collation.
3457  */
3458  switch (expr_op)
3459  {
3460  case OID_TEXT_LIKE_OP:
3461  case OID_TEXT_ICLIKE_OP:
3462  case OID_TEXT_REGEXEQ_OP:
3463  case OID_TEXT_ICREGEXEQ_OP:
3464  isIndexable =
3465  (opfamily == TEXT_PATTERN_BTREE_FAM_OID) ||
3466  (opfamily == TEXT_SPGIST_FAM_OID) ||
3467  (opfamily == TEXT_BTREE_FAM_OID &&
3468  (pstatus == Pattern_Prefix_Exact ||
3469  lc_collate_is_c(idxcollation)));
3470  break;
3471 
3472  case OID_BPCHAR_LIKE_OP:
3473  case OID_BPCHAR_ICLIKE_OP:
3474  case OID_BPCHAR_REGEXEQ_OP:
3476  isIndexable =
3477  (opfamily == BPCHAR_PATTERN_BTREE_FAM_OID) ||
3478  (opfamily == BPCHAR_BTREE_FAM_OID &&
3479  (pstatus == Pattern_Prefix_Exact ||
3480  lc_collate_is_c(idxcollation)));
3481  break;
3482 
3483  case OID_NAME_LIKE_OP:
3484  case OID_NAME_ICLIKE_OP:
3485  case OID_NAME_REGEXEQ_OP:
3486  case OID_NAME_ICREGEXEQ_OP:
3487  /* name uses locale-insensitive sorting */
3488  isIndexable = (opfamily == NAME_BTREE_FAM_OID);
3489  break;
3490 
3491  case OID_BYTEA_LIKE_OP:
3492  isIndexable = (opfamily == BYTEA_BTREE_FAM_OID);
3493  break;
3494 
3495  case OID_INET_SUB_OP:
3496  case OID_INET_SUBEQ_OP:
3497  isIndexable = (opfamily == NETWORK_BTREE_FAM_OID);
3498  break;
3499  }
3500 
3501  return isIndexable;
3502 }
Datum constvalue
Definition: primnodes.h:196
#define NAME_BTREE_FAM_OID
Definition: pg_opfamily.h:93
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define OID_INET_SUBEQ_OP
Definition: pg_operator.h:1182
#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:509
#define OID_TEXT_LIKE_OP
Definition: pg_operator.h:895
#define TEXT_SPGIST_FAM_OID
Definition: pg_opfamily.h:155
unsigned int Oid
Definition: postgres_ext.h:31
#define OID_BYTEA_LIKE_OP
Definition: pg_operator.h:1401
#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:950
#define TEXT_PATTERN_BTREE_FAM_OID
Definition: pg_opfamily.h:115
#define OID_BPCHAR_LIKE_OP
Definition: pg_operator.h:900
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1128
#define OID_NAME_ICLIKE_OP
Definition: pg_operator.h:1211
#define TEXT_BTREE_FAM_OID
Definition: pg_opfamily.h:105
Pattern_Prefix_Status pattern_fixed_prefix(Const *patt, Pattern_Type ptype, Oid collation, Const **prefix, Selectivity *rest_selec)
Definition: selfuncs.c:5665
#define NULL
Definition: c.h:229
#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:1216
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
#define OID_BPCHAR_ICLIKE_OP
Definition: pg_operator.h:1221
#define DatumGetPointer(X)
Definition: postgres.h:555
#define OID_INET_SUB_OP
Definition: pg_operator.h:1179
#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:94
#define BPCHAR_PATTERN_BTREE_FAM_OID
Definition: pg_opfamily.h:117
static List * network_prefix_quals ( Node leftop,
Oid  expr_op,
Oid  opfamily,
Datum  rightop 
)
static

Definition at line 4159 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, OID_INET_SUBEQ_OP, and result.

Referenced by expand_indexqual_opclause().

4160 {
4161  bool is_eq;
4162  Oid datatype;
4163  Oid opr1oid;
4164  Oid opr2oid;
4165  Datum opr1right;
4166  Datum opr2right;
4167  List *result;
4168  Expr *expr;
4169 
4170  switch (expr_op)
4171  {
4172  case OID_INET_SUB_OP:
4173  datatype = INETOID;
4174  is_eq = false;
4175  break;
4176  case OID_INET_SUBEQ_OP:
4177  datatype = INETOID;
4178  is_eq = true;
4179  break;
4180  default:
4181  elog(ERROR, "unexpected operator: %u", expr_op);
4182  return NIL;
4183  }
4184 
4185  /*
4186  * create clause "key >= network_scan_first( rightop )", or ">" if the
4187  * operator disallows equality.
4188  */
4189  if (is_eq)
4190  {
4191  opr1oid = get_opfamily_member(opfamily, datatype, datatype,
4193  if (opr1oid == InvalidOid)
4194  elog(ERROR, "no >= operator for opfamily %u", opfamily);
4195  }
4196  else
4197  {
4198  opr1oid = get_opfamily_member(opfamily, datatype, datatype,
4200  if (opr1oid == InvalidOid)
4201  elog(ERROR, "no > operator for opfamily %u", opfamily);
4202  }
4203 
4204  opr1right = network_scan_first(rightop);
4205 
4206  expr = make_opclause(opr1oid, BOOLOID, false,
4207  (Expr *) leftop,
4208  (Expr *) makeConst(datatype, -1,
4209  InvalidOid, /* not collatable */
4210  -1, opr1right,
4211  false, false),
4213  result = list_make1(make_simple_restrictinfo(expr));
4214 
4215  /* create clause "key <= network_scan_last( rightop )" */
4216 
4217  opr2oid = get_opfamily_member(opfamily, datatype, datatype,
4219  if (opr2oid == InvalidOid)
4220  elog(ERROR, "no <= operator for opfamily %u", opfamily);
4221 
4222  opr2right = network_scan_last(rightop);
4223 
4224  expr = make_opclause(opr2oid, BOOLOID, false,
4225  (Expr *) leftop,
4226  (Expr *) makeConst(datatype, -1,
4227  InvalidOid, /* not collatable */
4228  -1, opr2right,
4229  false, false),
4231  result = lappend(result, make_simple_restrictinfo(expr));
4232 
4233  return result;
4234 }
#define NIL
Definition: pg_list.h:69
#define OID_INET_SUBEQ_OP
Definition: pg_operator.h:1182
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
Datum network_scan_last(Datum in)
Definition: network.c:1105
#define INETOID
Definition: pg_type.h:448
return result
Definition: formatting.c:1632
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: clauses.c:172
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:139
#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:1091
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
#define BOOLOID
Definition: pg_type.h:288
#define OID_INET_SUB_OP
Definition: pg_operator.h:1179
#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 1572 of file indxpath.c.

References cost_bitmap_tree_node(), and PathClauseUsage::path.

Referenced by choose_bitmap_and().

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

Definition at line 4033 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(), result, string_to_const(), TEXT_BTREE_FAM_OID, TEXT_PATTERN_BTREE_FAM_OID, TEXT_SPGIST_FAM_OID, TextDatumGetCString, and TEXTOID.

Referenced by expand_indexqual_opclause().

4035 {
4036  List *result;
4037  Oid datatype;
4038  Oid oproid;
4039  Expr *expr;
4040  FmgrInfo ltproc;
4041  Const *greaterstr;
4042 
4043  Assert(pstatus != Pattern_Prefix_None);
4044 
4045  switch (opfamily)
4046  {
4047  case TEXT_BTREE_FAM_OID:
4049  case TEXT_SPGIST_FAM_OID:
4050  datatype = TEXTOID;
4051  break;
4052 
4053  case BPCHAR_BTREE_FAM_OID:
4055  datatype = BPCHAROID;
4056  break;
4057 
4058  case NAME_BTREE_FAM_OID:
4059  datatype = NAMEOID;
4060  break;
4061 
4062  case BYTEA_BTREE_FAM_OID:
4063  datatype = BYTEAOID;
4064  break;
4065 
4066  default:
4067  /* shouldn't get here */
4068  elog(ERROR, "unexpected opfamily: %u", opfamily);
4069  return NIL;
4070  }
4071 
4072  /*
4073  * If necessary, coerce the prefix constant to the right type. The given
4074  * prefix constant is either text or bytea type.
4075  */
4076  if (prefix_const->consttype != datatype)
4077  {
4078  char *prefix;
4079 
4080  switch (prefix_const->consttype)
4081  {
4082  case TEXTOID:
4083  prefix = TextDatumGetCString(prefix_const->constvalue);
4084  break;
4085  case BYTEAOID:
4087  prefix_const->constvalue));
4088  break;
4089  default:
4090  elog(ERROR, "unexpected const type: %u",
4091  prefix_const->consttype);
4092  return NIL;
4093  }
4094  prefix_const = string_to_const(prefix, datatype);
4095  pfree(prefix);
4096  }
4097 
4098  /*
4099  * If we found an exact-match pattern, generate an "=" indexqual.
4100  */
4101  if (pstatus == Pattern_Prefix_Exact)
4102  {
4103  oproid = get_opfamily_member(opfamily, datatype, datatype,
4105  if (oproid == InvalidOid)
4106  elog(ERROR, "no = operator for opfamily %u", opfamily);
4107  expr = make_opclause(oproid, BOOLOID, false,
4108  (Expr *) leftop, (Expr *) prefix_const,
4109  InvalidOid, collation);
4110  result = list_make1(make_simple_restrictinfo(expr));
4111  return result;
4112  }
4113 
4114  /*
4115  * Otherwise, we have a nonempty required prefix of the values.
4116  *
4117  * We can always say "x >= prefix".
4118  */
4119  oproid = get_opfamily_member(opfamily, datatype, datatype,
4121  if (oproid == InvalidOid)
4122  elog(ERROR, "no >= operator for opfamily %u", opfamily);
4123  expr = make_opclause(oproid, BOOLOID, false,
4124  (Expr *) leftop, (Expr *) prefix_const,
4125  InvalidOid, collation);
4126  result = list_make1(make_simple_restrictinfo(expr));
4127 
4128  /*-------
4129  * If we can create a string larger than the prefix, we can say
4130  * "x < greaterstr". NB: we rely on make_greater_string() to generate
4131  * a guaranteed-greater string, not just a probably-greater string.
4132  * In general this is only guaranteed in C locale, so we'd better be
4133  * using a C-locale index collation.
4134  *-------
4135  */
4136  oproid = get_opfamily_member(opfamily, datatype, datatype,
4138  if (oproid == InvalidOid)
4139  elog(ERROR, "no < operator for opfamily %u", opfamily);
4140  fmgr_info(get_opcode(oproid), &ltproc);
4141  greaterstr = make_greater_string(prefix_const, &ltproc, collation);
4142  if (greaterstr)
4143  {
4144  expr = make_opclause(oproid, BOOLOID, false,
4145  (Expr *) leftop, (Expr *) greaterstr,
4146  InvalidOid, collation);
4147  result = lappend(result, make_simple_restrictinfo(expr));
4148  }
4149 
4150  return result;
4151 }
Datum byteaout(PG_FUNCTION_ARGS)
Definition: varlena.c:351
#define NIL
Definition: pg_list.h:69
Definition: fmgr.h:56
#define BPCHAROID
Definition: pg_type.h:504
#define NAME_BTREE_FAM_OID
Definition: pg_opfamily.h:93
#define NAMEOID
Definition: pg_type.h:300
#define TEXTOID
Definition: pg_type.h:324
#define BPCHAR_BTREE_FAM_OID
Definition: pg_opfamily.h:68
return result
Definition: formatting.c:1632
#define TEXT_SPGIST_FAM_OID
Definition: pg_opfamily.h:155
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: clauses.c:172
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
unsigned int Oid
Definition: postgres_ext.h:31
#define list_make1(x1)
Definition: pg_list.h:139
#define BYTEA_BTREE_FAM_OID
Definition: pg_opfamily.h:71
void pfree(void *pointer)
Definition: mcxt.c:950
#define TEXT_PATTERN_BTREE_FAM_OID
Definition: pg_opfamily.h:115
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:572
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
#define TEXT_BTREE_FAM_OID
Definition: pg_opfamily.h:105
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:92
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1094
Const * make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation)
Definition: selfuncs.c:6030
#define Assert(condition)
Definition: c.h:675
static Const * string_to_const(const char *str, Oid datatype)
Definition: indxpath.c:4264
#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:117
bool relation_has_unique_index_for ( PlannerInfo root,
RelOptInfo rel,
List restrictlist,
List exprlist,
List oprlist 
)

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

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