PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
selfuncs.h File Reference
#include "fmgr.h"
#include "access/htup.h"
#include "nodes/relation.h"
Include dependency graph for selfuncs.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  VariableStatData
 
struct  IndexQualInfo
 
struct  GenericCosts
 

Macros

#define DEFAULT_EQ_SEL   0.005
 
#define DEFAULT_INEQ_SEL   0.3333333333333333
 
#define DEFAULT_RANGE_INEQ_SEL   0.005
 
#define DEFAULT_MATCH_SEL   0.005
 
#define DEFAULT_NUM_DISTINCT   200
 
#define DEFAULT_UNK_SEL   0.005
 
#define DEFAULT_NOT_UNK_SEL   (1.0 - DEFAULT_UNK_SEL)
 
#define CLAMP_PROBABILITY(p)
 
#define ReleaseVariableStats(vardata)
 

Typedefs

typedef struct VariableStatData VariableStatData
 
typedef bool(* get_relation_stats_hook_type )(PlannerInfo *root, RangeTblEntry *rte, AttrNumber attnum, VariableStatData *vardata)
 
typedef bool(* get_index_stats_hook_type )(PlannerInfo *root, Oid indexOid, AttrNumber indexattnum, VariableStatData *vardata)
 

Enumerations

enum  Pattern_Type { Pattern_Type_Like, Pattern_Type_Like_IC, Pattern_Type_Regex, Pattern_Type_Regex_IC }
 
enum  Pattern_Prefix_Status { Pattern_Prefix_None, Pattern_Prefix_Partial, Pattern_Prefix_Exact }
 

Functions

void examine_variable (PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
 
bool statistic_proc_security_check (VariableStatData *vardata, Oid func_oid)
 
bool get_restriction_variable (PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
 
void get_join_variables (PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo, VariableStatData *vardata1, VariableStatData *vardata2, bool *join_is_reversed)
 
double get_variable_numdistinct (VariableStatData *vardata, bool *isdefault)
 
double mcv_selectivity (VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, double *sumcommonp)
 
double histogram_selectivity (VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
 
Pattern_Prefix_Status pattern_fixed_prefix (Const *patt, Pattern_Type ptype, Oid collation, Const **prefix, Selectivity *rest_selec)
 
Constmake_greater_string (const Const *str_const, FmgrInfo *ltproc, Oid collation)
 
Selectivity boolvarsel (PlannerInfo *root, Node *arg, int varRelid)
 
Selectivity booltestsel (PlannerInfo *root, BoolTestType booltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
Selectivity nulltestsel (PlannerInfo *root, NullTestType nulltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
Selectivity scalararraysel (PlannerInfo *root, ScalarArrayOpExpr *clause, bool is_join_clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
int estimate_array_length (Node *arrayexpr)
 
Selectivity rowcomparesel (PlannerInfo *root, RowCompareExpr *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
void mergejoinscansel (PlannerInfo *root, Node *clause, Oid opfamily, int strategy, bool nulls_first, Selectivity *leftstart, Selectivity *leftend, Selectivity *rightstart, Selectivity *rightend)
 
double estimate_num_groups (PlannerInfo *root, List *groupExprs, double input_rows, List **pgset)
 
Selectivity estimate_hash_bucketsize (PlannerInfo *root, Node *hashkey, double nbuckets)
 
Listdeconstruct_indexquals (IndexPath *path)
 
void genericcostestimate (PlannerInfo *root, IndexPath *path, double loop_count, List *qinfos, GenericCosts *costs)
 
Selectivity scalararraysel_containment (PlannerInfo *root, Node *leftop, Node *rightop, Oid elemtype, bool isEquality, bool useOr, int varRelid)
 

Variables

PGDLLIMPORT
get_relation_stats_hook_type 
get_relation_stats_hook
 
PGDLLIMPORT
get_index_stats_hook_type 
get_index_stats_hook
 

Macro Definition Documentation

#define DEFAULT_EQ_SEL   0.005

Definition at line 34 of file selfuncs.h.

Referenced by _int_matchsel(), eqsel_internal(), int_query_opr_selec(), and neqjoinsel().

#define DEFAULT_INEQ_SEL   0.3333333333333333
#define DEFAULT_MATCH_SEL   0.005

Definition at line 43 of file selfuncs.h.

Referenced by patternjoinsel(), patternsel(), and prefix_selectivity().

#define DEFAULT_NOT_UNK_SEL   (1.0 - DEFAULT_UNK_SEL)

Definition at line 50 of file selfuncs.h.

Referenced by booltestsel(), and nulltestsel().

#define DEFAULT_NUM_DISTINCT   200

Definition at line 46 of file selfuncs.h.

Referenced by get_variable_numdistinct().

#define DEFAULT_RANGE_INEQ_SEL   0.005

Definition at line 40 of file selfuncs.h.

Referenced by clauselist_selectivity(), and default_range_selectivity().

#define DEFAULT_UNK_SEL   0.005

Definition at line 49 of file selfuncs.h.

Referenced by booltestsel(), and nulltestsel().

#define ReleaseVariableStats (   vardata)

Typedef Documentation

typedef bool(* get_index_stats_hook_type)(PlannerInfo *root, Oid indexOid, AttrNumber indexattnum, VariableStatData *vardata)

Definition at line 147 of file selfuncs.h.

typedef bool(* get_relation_stats_hook_type)(PlannerInfo *root, RangeTblEntry *rte, AttrNumber attnum, VariableStatData *vardata)

Definition at line 142 of file selfuncs.h.

Enumeration Type Documentation

Enumerator
Pattern_Prefix_None 
Pattern_Prefix_Partial 
Pattern_Prefix_Exact 

Definition at line 94 of file selfuncs.h.

Enumerator
Pattern_Type_Like 
Pattern_Type_Like_IC 
Pattern_Type_Regex 
Pattern_Type_Regex_IC 

Definition at line 88 of file selfuncs.h.

Function Documentation

Selectivity booltestsel ( PlannerInfo root,
BoolTestType  booltesttype,
Node arg,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo 
)

Definition at line 1517 of file selfuncs.c.

References ATTSTATSSLOT_NUMBERS, ATTSTATSSLOT_VALUES, CLAMP_PROBABILITY, clause_selectivity(), DatumGetBool, DEFAULT_NOT_UNK_SEL, DEFAULT_UNK_SEL, elog, ERROR, examine_variable(), free_attstatsslot(), get_attstatsslot(), GETSTRUCT, HeapTupleIsValid, InvalidOid, IS_FALSE, IS_NOT_FALSE, IS_NOT_TRUE, IS_NOT_UNKNOWN, IS_TRUE, IS_UNKNOWN, AttStatsSlot::nnumbers, AttStatsSlot::numbers, ReleaseVariableStats, STATISTIC_KIND_MCV, VariableStatData::statsTuple, and AttStatsSlot::values.

Referenced by clause_selectivity().

1519 {
1520  VariableStatData vardata;
1521  double selec;
1522 
1523  examine_variable(root, arg, varRelid, &vardata);
1524 
1525  if (HeapTupleIsValid(vardata.statsTuple))
1526  {
1527  Form_pg_statistic stats;
1528  double freq_null;
1529  AttStatsSlot sslot;
1530 
1531  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1532  freq_null = stats->stanullfrac;
1533 
1534  if (get_attstatsslot(&sslot, vardata.statsTuple,
1537  && sslot.nnumbers > 0)
1538  {
1539  double freq_true;
1540  double freq_false;
1541 
1542  /*
1543  * Get first MCV frequency and derive frequency for true.
1544  */
1545  if (DatumGetBool(sslot.values[0]))
1546  freq_true = sslot.numbers[0];
1547  else
1548  freq_true = 1.0 - sslot.numbers[0] - freq_null;
1549 
1550  /*
1551  * Next derive frequency for false. Then use these as appropriate
1552  * to derive frequency for each case.
1553  */
1554  freq_false = 1.0 - freq_true - freq_null;
1555 
1556  switch (booltesttype)
1557  {
1558  case IS_UNKNOWN:
1559  /* select only NULL values */
1560  selec = freq_null;
1561  break;
1562  case IS_NOT_UNKNOWN:
1563  /* select non-NULL values */
1564  selec = 1.0 - freq_null;
1565  break;
1566  case IS_TRUE:
1567  /* select only TRUE values */
1568  selec = freq_true;
1569  break;
1570  case IS_NOT_TRUE:
1571  /* select non-TRUE values */
1572  selec = 1.0 - freq_true;
1573  break;
1574  case IS_FALSE:
1575  /* select only FALSE values */
1576  selec = freq_false;
1577  break;
1578  case IS_NOT_FALSE:
1579  /* select non-FALSE values */
1580  selec = 1.0 - freq_false;
1581  break;
1582  default:
1583  elog(ERROR, "unrecognized booltesttype: %d",
1584  (int) booltesttype);
1585  selec = 0.0; /* Keep compiler quiet */
1586  break;
1587  }
1588 
1589  free_attstatsslot(&sslot);
1590  }
1591  else
1592  {
1593  /*
1594  * No most-common-value info available. Still have null fraction
1595  * information, so use it for IS [NOT] UNKNOWN. Otherwise adjust
1596  * for null fraction and assume a 50-50 split of TRUE and FALSE.
1597  */
1598  switch (booltesttype)
1599  {
1600  case IS_UNKNOWN:
1601  /* select only NULL values */
1602  selec = freq_null;
1603  break;
1604  case IS_NOT_UNKNOWN:
1605  /* select non-NULL values */
1606  selec = 1.0 - freq_null;
1607  break;
1608  case IS_TRUE:
1609  case IS_FALSE:
1610  /* Assume we select half of the non-NULL values */
1611  selec = (1.0 - freq_null) / 2.0;
1612  break;
1613  case IS_NOT_TRUE:
1614  case IS_NOT_FALSE:
1615  /* Assume we select NULLs plus half of the non-NULLs */
1616  /* equiv. to freq_null + (1.0 - freq_null) / 2.0 */
1617  selec = (freq_null + 1.0) / 2.0;
1618  break;
1619  default:
1620  elog(ERROR, "unrecognized booltesttype: %d",
1621  (int) booltesttype);
1622  selec = 0.0; /* Keep compiler quiet */
1623  break;
1624  }
1625  }
1626  }
1627  else
1628  {
1629  /*
1630  * If we can't get variable statistics for the argument, perhaps
1631  * clause_selectivity can do something with it. We ignore the
1632  * possibility of a NULL value when using clause_selectivity, and just
1633  * assume the value is either TRUE or FALSE.
1634  */
1635  switch (booltesttype)
1636  {
1637  case IS_UNKNOWN:
1638  selec = DEFAULT_UNK_SEL;
1639  break;
1640  case IS_NOT_UNKNOWN:
1641  selec = DEFAULT_NOT_UNK_SEL;
1642  break;
1643  case IS_TRUE:
1644  case IS_NOT_FALSE:
1645  selec = (double) clause_selectivity(root, arg,
1646  varRelid,
1647  jointype, sjinfo);
1648  break;
1649  case IS_FALSE:
1650  case IS_NOT_TRUE:
1651  selec = 1.0 - (double) clause_selectivity(root, arg,
1652  varRelid,
1653  jointype, sjinfo);
1654  break;
1655  default:
1656  elog(ERROR, "unrecognized booltesttype: %d",
1657  (int) booltesttype);
1658  selec = 0.0; /* Keep compiler quiet */
1659  break;
1660  }
1661  }
1662 
1663  ReleaseVariableStats(vardata);
1664 
1665  /* result should be in range, but make sure... */
1666  CLAMP_PROBABILITY(selec);
1667 
1668  return (Selectivity) selec;
1669 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define ATTSTATSSLOT_VALUES
Definition: lsyscache.h:39
HeapTuple statsTuple
Definition: selfuncs.h:71
int nnumbers
Definition: lsyscache.h:53
double Selectivity
Definition: nodes.h:639
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
#define DEFAULT_NOT_UNK_SEL
Definition: selfuncs.h:50
#define ATTSTATSSLOT_NUMBERS
Definition: lsyscache.h:40
#define ERROR
Definition: elog.h:43
Selectivity clause_selectivity(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:572
float4 * numbers
Definition: lsyscache.h:52
#define DatumGetBool(X)
Definition: postgres.h:399
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:204
#define DEFAULT_UNK_SEL
Definition: selfuncs.h:49
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4556
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:2895
Datum * values
Definition: lsyscache.h:49
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
#define elog
Definition: elog.h:219
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3011
Selectivity boolvarsel ( PlannerInfo root,
Node arg,
int  varRelid 
)

Definition at line 1478 of file selfuncs.c.

References BooleanEqualOperator, BoolGetDatum, examine_variable(), HeapTupleIsValid, is_funcclause, ReleaseVariableStats, VariableStatData::statsTuple, and var_eq_const().

Referenced by clause_selectivity().

1479 {
1480  VariableStatData vardata;
1481  double selec;
1482 
1483  examine_variable(root, arg, varRelid, &vardata);
1484  if (HeapTupleIsValid(vardata.statsTuple))
1485  {
1486  /*
1487  * A boolean variable V is equivalent to the clause V = 't', so we
1488  * compute the selectivity as if that is what we have.
1489  */
1490  selec = var_eq_const(&vardata, BooleanEqualOperator,
1491  BoolGetDatum(true), false, true, false);
1492  }
1493  else if (is_funcclause(arg))
1494  {
1495  /*
1496  * If we have no stats and it's a function call, estimate 0.3333333.
1497  * This seems a pretty unprincipled choice, but Postgres has been
1498  * using that estimate for function calls since 1992. The hoariness
1499  * of this behavior suggests that we should not be in too much hurry
1500  * to use another value.
1501  */
1502  selec = 0.3333333;
1503  }
1504  else
1505  {
1506  /* Otherwise, the default estimate is 0.5 */
1507  selec = 0.5;
1508  }
1509  ReleaseVariableStats(vardata);
1510  return selec;
1511 }
HeapTuple statsTuple
Definition: selfuncs.h:71
#define is_funcclause(clause)
Definition: clauses.h:21
#define BooleanEqualOperator
Definition: pg_operator.h:114
static double var_eq_const(VariableStatData *vardata, Oid operator, Datum constval, bool constisnull, bool varonleft, bool negate)
Definition: selfuncs.c:296
#define BoolGetDatum(X)
Definition: postgres.h:408
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4556
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
List* deconstruct_indexquals ( IndexPath path)

Definition at line 6292 of file selfuncs.c.

References arg, ScalarArrayOpExpr::args, Assert, RestrictInfo::clause, IndexQualInfo::clause_op, elog, ERROR, forboth, get_leftop(), get_rightop(), IndexQualInfo::indexcol, IndexPath::indexinfo, IndexPath::indexqualcols, IndexPath::indexquals, InvalidOid, IsA, lappend(), RowCompareExpr::largs, lfirst_int, lfirst_node, linitial, linitial_oid, lsecond, match_index_to_operand(), NIL, nodeTag, NULL, ScalarArrayOpExpr::opno, RowCompareExpr::opnos, IndexQualInfo::other_operand, palloc(), RowCompareExpr::rargs, result, IndexQualInfo::rinfo, and IndexQualInfo::varonleft.

Referenced by blcostestimate(), brincostestimate(), btcostestimate(), gincostestimate(), gistcostestimate(), hashcostestimate(), and spgcostestimate().

6293 {
6294  List *result = NIL;
6295  IndexOptInfo *index = path->indexinfo;
6296  ListCell *lcc,
6297  *lci;
6298 
6299  forboth(lcc, path->indexquals, lci, path->indexqualcols)
6300  {
6301  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lcc);
6302  int indexcol = lfirst_int(lci);
6303  Expr *clause;
6304  Node *leftop,
6305  *rightop;
6306  IndexQualInfo *qinfo;
6307 
6308  clause = rinfo->clause;
6309 
6310  qinfo = (IndexQualInfo *) palloc(sizeof(IndexQualInfo));
6311  qinfo->rinfo = rinfo;
6312  qinfo->indexcol = indexcol;
6313 
6314  if (IsA(clause, OpExpr))
6315  {
6316  qinfo->clause_op = ((OpExpr *) clause)->opno;
6317  leftop = get_leftop(clause);
6318  rightop = get_rightop(clause);
6319  if (match_index_to_operand(leftop, indexcol, index))
6320  {
6321  qinfo->varonleft = true;
6322  qinfo->other_operand = rightop;
6323  }
6324  else
6325  {
6326  Assert(match_index_to_operand(rightop, indexcol, index));
6327  qinfo->varonleft = false;
6328  qinfo->other_operand = leftop;
6329  }
6330  }
6331  else if (IsA(clause, RowCompareExpr))
6332  {
6333  RowCompareExpr *rc = (RowCompareExpr *) clause;
6334 
6335  qinfo->clause_op = linitial_oid(rc->opnos);
6336  /* Examine only first columns to determine left/right sides */
6338  indexcol, index))
6339  {
6340  qinfo->varonleft = true;
6341  qinfo->other_operand = (Node *) rc->rargs;
6342  }
6343  else
6344  {
6346  indexcol, index));
6347  qinfo->varonleft = false;
6348  qinfo->other_operand = (Node *) rc->largs;
6349  }
6350  }
6351  else if (IsA(clause, ScalarArrayOpExpr))
6352  {
6353  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
6354 
6355  qinfo->clause_op = saop->opno;
6356  /* index column is always on the left in this case */
6358  indexcol, index));
6359  qinfo->varonleft = true;
6360  qinfo->other_operand = (Node *) lsecond(saop->args);
6361  }
6362  else if (IsA(clause, NullTest))
6363  {
6364  qinfo->clause_op = InvalidOid;
6365  Assert(match_index_to_operand((Node *) ((NullTest *) clause)->arg,
6366  indexcol, index));
6367  qinfo->varonleft = true;
6368  qinfo->other_operand = NULL;
6369  }
6370  else
6371  {
6372  elog(ERROR, "unsupported indexqual type: %d",
6373  (int) nodeTag(clause));
6374  }
6375 
6376  result = lappend(result, qinfo);
6377  }
6378  return result;
6379 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
IndexOptInfo * indexinfo
Definition: relation.h:1031
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3180
Definition: nodes.h:509
return result
Definition: formatting.c:1632
RestrictInfo * rinfo
Definition: selfuncs.h:106
#define lsecond(l)
Definition: pg_list.h:116
Definition: type.h:89
List * indexquals
Definition: relation.h:1033
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
#define lfirst_node(type, lc)
Definition: pg_list.h:109
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1747
bool varonleft
Definition: selfuncs.h:108
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define linitial_oid(l)
Definition: pg_list.h:113
#define nodeTag(nodeptr)
Definition: nodes.h:514
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
List * indexqualcols
Definition: relation.h:1034
void * palloc(Size size)
Definition: mcxt.c:849
Node * other_operand
Definition: selfuncs.h:110
void * arg
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
int estimate_array_length ( Node arrayexpr)

Definition at line 2094 of file selfuncs.c.

References ARR_DIMS, ARR_NDIM, ArrayGetNItems(), DatumGetArrayTypeP, IsA, list_length(), and strip_array_coercion().

Referenced by btcostestimate(), cost_qual_eval_walker(), cost_tidscan(), genericcostestimate(), and gincost_scalararrayopexpr().

2095 {
2096  /* look through any binary-compatible relabeling of arrayexpr */
2097  arrayexpr = strip_array_coercion(arrayexpr);
2098 
2099  if (arrayexpr && IsA(arrayexpr, Const))
2100  {
2101  Datum arraydatum = ((Const *) arrayexpr)->constvalue;
2102  bool arrayisnull = ((Const *) arrayexpr)->constisnull;
2103  ArrayType *arrayval;
2104 
2105  if (arrayisnull)
2106  return 0;
2107  arrayval = DatumGetArrayTypeP(arraydatum);
2108  return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
2109  }
2110  else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
2111  !((ArrayExpr *) arrayexpr)->multidims)
2112  {
2113  return list_length(((ArrayExpr *) arrayexpr)->elements);
2114  }
2115  else
2116  {
2117  /* default guess --- see also scalararraysel */
2118  return 10;
2119  }
2120 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
#define ARR_DIMS(a)
Definition: array.h:275
uintptr_t Datum
Definition: postgres.h:372
static int list_length(const List *l)
Definition: pg_list.h:89
#define ARR_NDIM(a)
Definition: array.h:271
static Node * strip_array_coercion(Node *node)
Definition: selfuncs.c:1751
#define DatumGetArrayTypeP(X)
Definition: array.h:242
Selectivity estimate_hash_bucketsize ( PlannerInfo root,
Node hashkey,
double  nbuckets 
)

Definition at line 3592 of file selfuncs.c.

References ATTSTATSSLOT_NUMBERS, clamp_row_est(), examine_variable(), free_attstatsslot(), get_attstatsslot(), get_variable_numdistinct(), GETSTRUCT, HeapTupleIsValid, InvalidOid, AttStatsSlot::nnumbers, AttStatsSlot::numbers, VariableStatData::rel, ReleaseVariableStats, RelOptInfo::rows, STATISTIC_KIND_MCV, VariableStatData::statsTuple, and RelOptInfo::tuples.

Referenced by final_cost_hashjoin().

3593 {
3594  VariableStatData vardata;
3595  double estfract,
3596  ndistinct,
3597  stanullfrac,
3598  mcvfreq,
3599  avgfreq;
3600  bool isdefault;
3601  AttStatsSlot sslot;
3602 
3603  examine_variable(root, hashkey, 0, &vardata);
3604 
3605  /* Get number of distinct values */
3606  ndistinct = get_variable_numdistinct(&vardata, &isdefault);
3607 
3608  /* If ndistinct isn't real, punt and return 0.1, per comments above */
3609  if (isdefault)
3610  {
3611  ReleaseVariableStats(vardata);
3612  return (Selectivity) 0.1;
3613  }
3614 
3615  /* Get fraction that are null */
3616  if (HeapTupleIsValid(vardata.statsTuple))
3617  {
3618  Form_pg_statistic stats;
3619 
3620  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
3621  stanullfrac = stats->stanullfrac;
3622  }
3623  else
3624  stanullfrac = 0.0;
3625 
3626  /* Compute avg freq of all distinct data values in raw relation */
3627  avgfreq = (1.0 - stanullfrac) / ndistinct;
3628 
3629  /*
3630  * Adjust ndistinct to account for restriction clauses. Observe we are
3631  * assuming that the data distribution is affected uniformly by the
3632  * restriction clauses!
3633  *
3634  * XXX Possibly better way, but much more expensive: multiply by
3635  * selectivity of rel's restriction clauses that mention the target Var.
3636  */
3637  if (vardata.rel && vardata.rel->tuples > 0)
3638  {
3639  ndistinct *= vardata.rel->rows / vardata.rel->tuples;
3640  ndistinct = clamp_row_est(ndistinct);
3641  }
3642 
3643  /*
3644  * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
3645  * number of buckets is less than the expected number of distinct values;
3646  * otherwise it is 1/ndistinct.
3647  */
3648  if (ndistinct > nbuckets)
3649  estfract = 1.0 / nbuckets;
3650  else
3651  estfract = 1.0 / ndistinct;
3652 
3653  /*
3654  * Look up the frequency of the most common value, if available.
3655  */
3656  mcvfreq = 0.0;
3657 
3658  if (HeapTupleIsValid(vardata.statsTuple))
3659  {
3660  if (get_attstatsslot(&sslot, vardata.statsTuple,
3663  {
3664  /*
3665  * The first MCV stat is for the most common value.
3666  */
3667  if (sslot.nnumbers > 0)
3668  mcvfreq = sslot.numbers[0];
3669  free_attstatsslot(&sslot);
3670  }
3671  }
3672 
3673  /*
3674  * Adjust estimated bucketsize upward to account for skewed distribution.
3675  */
3676  if (avgfreq > 0.0 && mcvfreq > avgfreq)
3677  estfract *= mcvfreq / avgfreq;
3678 
3679  /*
3680  * Clamp bucketsize to sane range (the above adjustment could easily
3681  * produce an out-of-range result). We set the lower bound a little above
3682  * zero, since zero isn't a very sane result.
3683  */
3684  if (estfract < 1.0e-6)
3685  estfract = 1.0e-6;
3686  else if (estfract > 1.0)
3687  estfract = 1.0;
3688 
3689  ReleaseVariableStats(vardata);
3690 
3691  return (Selectivity) estfract;
3692 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
HeapTuple statsTuple
Definition: selfuncs.h:71
int nnumbers
Definition: lsyscache.h:53
double tuples
Definition: relation.h:565
RelOptInfo * rel
Definition: selfuncs.h:70
double Selectivity
Definition: nodes.h:639
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define ATTSTATSSLOT_NUMBERS
Definition: lsyscache.h:40
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition: selfuncs.c:4963
float4 * numbers
Definition: lsyscache.h:52
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:204
double rows
Definition: relation.h:528
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4556
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:2895
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
e
Definition: preproc-init.c:82
double clamp_row_est(double nrows)
Definition: costsize.c:173
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3011
double estimate_num_groups ( PlannerInfo root,
List groupExprs,
double  input_rows,
List **  pgset 
)

Definition at line 3269 of file selfuncs.c.

References add_unique_group_var(), Assert, BOOLOID, clamp_row_est(), contain_volatile_functions(), estimate_multivariate_ndistinct(), examine_variable(), exprType(), for_each_cell, HeapTupleIsValid, i, IS_SIMPLE_REL, VariableStatData::isunique, lcons(), lfirst, linitial, list_head(), list_length(), list_member_int(), lnext, GroupVarInfo::ndistinct, NIL, pull_var_clause(), PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS, PVC_RECURSE_WINDOWFUNCS, GroupVarInfo::rel, ReleaseVariableStats, RelOptInfo::rows, VariableStatData::statsTuple, and RelOptInfo::tuples.

Referenced by adjust_rowcount_for_semijoins(), create_distinct_paths(), create_unique_path(), estimate_path_cost_size(), get_number_of_groups(), and recurse_set_operations().

3271 {
3272  List *varinfos = NIL;
3273  double numdistinct;
3274  ListCell *l;
3275  int i;
3276 
3277  /*
3278  * We don't ever want to return an estimate of zero groups, as that tends
3279  * to lead to division-by-zero and other unpleasantness. The input_rows
3280  * estimate is usually already at least 1, but clamp it just in case it
3281  * isn't.
3282  */
3283  input_rows = clamp_row_est(input_rows);
3284 
3285  /*
3286  * If no grouping columns, there's exactly one group. (This can't happen
3287  * for normal cases with GROUP BY or DISTINCT, but it is possible for
3288  * corner cases with set operations.)
3289  */
3290  if (groupExprs == NIL || (pgset && list_length(*pgset) < 1))
3291  return 1.0;
3292 
3293  /*
3294  * Count groups derived from boolean grouping expressions. For other
3295  * expressions, find the unique Vars used, treating an expression as a Var
3296  * if we can find stats for it. For each one, record the statistical
3297  * estimate of number of distinct values (total in its table, without
3298  * regard for filtering).
3299  */
3300  numdistinct = 1.0;
3301 
3302  i = 0;
3303  foreach(l, groupExprs)
3304  {
3305  Node *groupexpr = (Node *) lfirst(l);
3306  VariableStatData vardata;
3307  List *varshere;
3308  ListCell *l2;
3309 
3310  /* is expression in this grouping set? */
3311  if (pgset && !list_member_int(*pgset, i++))
3312  continue;
3313 
3314  /* Short-circuit for expressions returning boolean */
3315  if (exprType(groupexpr) == BOOLOID)
3316  {
3317  numdistinct *= 2.0;
3318  continue;
3319  }
3320 
3321  /*
3322  * If examine_variable is able to deduce anything about the GROUP BY
3323  * expression, treat it as a single variable even if it's really more
3324  * complicated.
3325  */
3326  examine_variable(root, groupexpr, 0, &vardata);
3327  if (HeapTupleIsValid(vardata.statsTuple) || vardata.isunique)
3328  {
3329  varinfos = add_unique_group_var(root, varinfos,
3330  groupexpr, &vardata);
3331  ReleaseVariableStats(vardata);
3332  continue;
3333  }
3334  ReleaseVariableStats(vardata);
3335 
3336  /*
3337  * Else pull out the component Vars. Handle PlaceHolderVars by
3338  * recursing into their arguments (effectively assuming that the
3339  * PlaceHolderVar doesn't change the number of groups, which boils
3340  * down to ignoring the possible addition of nulls to the result set).
3341  */
3342  varshere = pull_var_clause(groupexpr,
3346 
3347  /*
3348  * If we find any variable-free GROUP BY item, then either it is a
3349  * constant (and we can ignore it) or it contains a volatile function;
3350  * in the latter case we punt and assume that each input row will
3351  * yield a distinct group.
3352  */
3353  if (varshere == NIL)
3354  {
3355  if (contain_volatile_functions(groupexpr))
3356  return input_rows;
3357  continue;
3358  }
3359 
3360  /*
3361  * Else add variables to varinfos list
3362  */
3363  foreach(l2, varshere)
3364  {
3365  Node *var = (Node *) lfirst(l2);
3366 
3367  examine_variable(root, var, 0, &vardata);
3368  varinfos = add_unique_group_var(root, varinfos, var, &vardata);
3369  ReleaseVariableStats(vardata);
3370  }
3371  }
3372 
3373  /*
3374  * If now no Vars, we must have an all-constant or all-boolean GROUP BY
3375  * list.
3376  */
3377  if (varinfos == NIL)
3378  {
3379  /* Guard against out-of-range answers */
3380  if (numdistinct > input_rows)
3381  numdistinct = input_rows;
3382  return numdistinct;
3383  }
3384 
3385  /*
3386  * Group Vars by relation and estimate total numdistinct.
3387  *
3388  * For each iteration of the outer loop, we process the frontmost Var in
3389  * varinfos, plus all other Vars in the same relation. We remove these
3390  * Vars from the newvarinfos list for the next iteration. This is the
3391  * easiest way to group Vars of same rel together.
3392  */
3393  do
3394  {
3395  GroupVarInfo *varinfo1 = (GroupVarInfo *) linitial(varinfos);
3396  RelOptInfo *rel = varinfo1->rel;
3397  double reldistinct = 1;
3398  double relmaxndistinct = reldistinct;
3399  int relvarcount = 0;
3400  List *newvarinfos = NIL;
3401  List *relvarinfos = NIL;
3402 
3403  /*
3404  * Split the list of varinfos in two - one for the current rel, one
3405  * for remaining Vars on other rels.
3406  */
3407  relvarinfos = lcons(varinfo1, relvarinfos);
3408  for_each_cell(l, lnext(list_head(varinfos)))
3409  {
3410  GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
3411 
3412  if (varinfo2->rel == varinfo1->rel)
3413  {
3414  /* varinfos on current rel */
3415  relvarinfos = lcons(varinfo2, relvarinfos);
3416  }
3417  else
3418  {
3419  /* not time to process varinfo2 yet */
3420  newvarinfos = lcons(varinfo2, newvarinfos);
3421  }
3422  }
3423 
3424  /*
3425  * Get the numdistinct estimate for the Vars of this rel. We
3426  * iteratively search for multivariate n-distinct with maximum number
3427  * of vars; assuming that each var group is independent of the others,
3428  * we multiply them together. Any remaining relvarinfos after no more
3429  * multivariate matches are found are assumed independent too, so
3430  * their individual ndistinct estimates are multiplied also.
3431  *
3432  * While iterating, count how many separate numdistinct values we
3433  * apply. We apply a fudge factor below, but only if we multiplied
3434  * more than one such values.
3435  */
3436  while (relvarinfos)
3437  {
3438  double mvndistinct;
3439 
3440  if (estimate_multivariate_ndistinct(root, rel, &relvarinfos,
3441  &mvndistinct))
3442  {
3443  reldistinct *= mvndistinct;
3444  if (relmaxndistinct < mvndistinct)
3445  relmaxndistinct = mvndistinct;
3446  relvarcount++;
3447  }
3448  else
3449  {
3450  foreach(l, relvarinfos)
3451  {
3452  GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
3453 
3454  reldistinct *= varinfo2->ndistinct;
3455  if (relmaxndistinct < varinfo2->ndistinct)
3456  relmaxndistinct = varinfo2->ndistinct;
3457  relvarcount++;
3458  }
3459 
3460  /* we're done with this relation */
3461  relvarinfos = NIL;
3462  }
3463  }
3464 
3465  /*
3466  * Sanity check --- don't divide by zero if empty relation.
3467  */
3468  Assert(IS_SIMPLE_REL(rel));
3469  if (rel->tuples > 0)
3470  {
3471  /*
3472  * Clamp to size of rel, or size of rel / 10 if multiple Vars. The
3473  * fudge factor is because the Vars are probably correlated but we
3474  * don't know by how much. We should never clamp to less than the
3475  * largest ndistinct value for any of the Vars, though, since
3476  * there will surely be at least that many groups.
3477  */
3478  double clamp = rel->tuples;
3479 
3480  if (relvarcount > 1)
3481  {
3482  clamp *= 0.1;
3483  if (clamp < relmaxndistinct)
3484  {
3485  clamp = relmaxndistinct;
3486  /* for sanity in case some ndistinct is too large: */
3487  if (clamp > rel->tuples)
3488  clamp = rel->tuples;
3489  }
3490  }
3491  if (reldistinct > clamp)
3492  reldistinct = clamp;
3493 
3494  /*
3495  * Update the estimate based on the restriction selectivity,
3496  * guarding against division by zero when reldistinct is zero.
3497  * Also skip this if we know that we are returning all rows.
3498  */
3499  if (reldistinct > 0 && rel->rows < rel->tuples)
3500  {
3501  /*
3502  * Given a table containing N rows with n distinct values in a
3503  * uniform distribution, if we select p rows at random then
3504  * the expected number of distinct values selected is
3505  *
3506  * n * (1 - product((N-N/n-i)/(N-i), i=0..p-1))
3507  *
3508  * = n * (1 - (N-N/n)! / (N-N/n-p)! * (N-p)! / N!)
3509  *
3510  * See "Approximating block accesses in database
3511  * organizations", S. B. Yao, Communications of the ACM,
3512  * Volume 20 Issue 4, April 1977 Pages 260-261.
3513  *
3514  * Alternatively, re-arranging the terms from the factorials,
3515  * this may be written as
3516  *
3517  * n * (1 - product((N-p-i)/(N-i), i=0..N/n-1))
3518  *
3519  * This form of the formula is more efficient to compute in
3520  * the common case where p is larger than N/n. Additionally,
3521  * as pointed out by Dell'Era, if i << N for all terms in the
3522  * product, it can be approximated by
3523  *
3524  * n * (1 - ((N-p)/N)^(N/n))
3525  *
3526  * See "Expected distinct values when selecting from a bag
3527  * without replacement", Alberto Dell'Era,
3528  * http://www.adellera.it/investigations/distinct_balls/.
3529  *
3530  * The condition i << N is equivalent to n >> 1, so this is a
3531  * good approximation when the number of distinct values in
3532  * the table is large. It turns out that this formula also
3533  * works well even when n is small.
3534  */
3535  reldistinct *=
3536  (1 - pow((rel->tuples - rel->rows) / rel->tuples,
3537  rel->tuples / reldistinct));
3538  }
3539  reldistinct = clamp_row_est(reldistinct);
3540 
3541  /*
3542  * Update estimate of total distinct groups.
3543  */
3544  numdistinct *= reldistinct;
3545  }
3546 
3547  varinfos = newvarinfos;
3548  } while (varinfos != NIL);
3549 
3550  numdistinct = ceil(numdistinct);
3551 
3552  /* Guard against out-of-range answers */
3553  if (numdistinct > input_rows)
3554  numdistinct = input_rows;
3555  if (numdistinct < 1.0)
3556  numdistinct = 1.0;
3557 
3558  return numdistinct;
3559 }
#define NIL
Definition: pg_list.h:69
#define PVC_RECURSE_AGGREGATES
Definition: var.h:21
HeapTuple statsTuple
Definition: selfuncs.h:71
double tuples
Definition: relation.h:565
Definition: nodes.h:509
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:951
double ndistinct
Definition: selfuncs.c:3147
#define PVC_RECURSE_PLACEHOLDERS
Definition: var.h:26
#define IS_SIMPLE_REL(rel)
Definition: relation.h:505
#define linitial(l)
Definition: pg_list.h:111
bool list_member_int(const List *list, int datum)
Definition: list.c:485
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define PVC_RECURSE_WINDOWFUNCS
Definition: var.h:23
static bool estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel, List **varinfos, double *ndistinct)
Definition: selfuncs.c:3713
#define lnext(lc)
Definition: pg_list.h:105
static List * add_unique_group_var(PlannerInfo *root, List *varinfos, Node *var, VariableStatData *vardata)
Definition: selfuncs.c:3151
double rows
Definition: relation.h:528
List * lcons(void *datum, List *list)
Definition: list.c:259
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4556
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
#define for_each_cell(cell, initcell)
Definition: pg_list.h:169
#define BOOLOID
Definition: pg_type.h:288
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
int i
double clamp_row_est(double nrows)
Definition: costsize.c:173
Definition: pg_list.h:45
RelOptInfo * rel
Definition: selfuncs.c:3146
void examine_variable ( PlannerInfo root,
Node node,
int  varRelid,
VariableStatData vardata 
)

Definition at line 4556 of file selfuncs.c.

References VariableStatData::acl_ok, ACL_SELECT, ACLCHECK_OK, arg, Assert, VariableStatData::atttype, VariableStatData::atttypmod, BMS_EMPTY_SET, bms_free(), bms_is_member(), bms_membership(), BMS_MULTIPLE, BMS_SINGLETON, bms_singleton_member(), BoolGetDatum, elog, equal(), ERROR, examine_simple_variable(), exprType(), exprTypmod(), find_base_rel(), find_join_rel(), VariableStatData::freefunc, get_index_stats_hook, GetUserId(), has_unique_index(), HeapTupleIsValid, IndexOptInfo::indexkeys, RelOptInfo::indexlist, IndexOptInfo::indexoid, IndexOptInfo::indexprs, IndexOptInfo::indpred, Int16GetDatum, IsA, VariableStatData::isunique, lfirst, list_head(), lnext, MemSet, IndexOptInfo::ncolumns, NIL, NULL, ObjectIdGetDatum, pg_class_aclcheck(), planner_rt_fetch, IndexOptInfo::predOK, pull_varnos(), VariableStatData::rel, IndexOptInfo::rel, ReleaseSysCache(), RelOptInfo::relid, RangeTblEntry::relid, RTE_RELATION, RangeTblEntry::rtekind, SearchSysCache3, STATRELATTINH, VariableStatData::statsTuple, IndexOptInfo::unique, VariableStatData::var, Var::varattno, Var::varno, VariableStatData::vartype, Var::vartype, and Var::vartypmod.

Referenced by booltestsel(), boolvarsel(), estimate_hash_bucketsize(), estimate_num_groups(), get_join_variables(), get_restriction_variable(), mergejoinscansel(), nulltestsel(), and scalararraysel_containment().

4558 {
4559  Node *basenode;
4560  Relids varnos;
4561  RelOptInfo *onerel;
4562 
4563  /* Make sure we don't return dangling pointers in vardata */
4564  MemSet(vardata, 0, sizeof(VariableStatData));
4565 
4566  /* Save the exposed type of the expression */
4567  vardata->vartype = exprType(node);
4568 
4569  /* Look inside any binary-compatible relabeling */
4570 
4571  if (IsA(node, RelabelType))
4572  basenode = (Node *) ((RelabelType *) node)->arg;
4573  else
4574  basenode = node;
4575 
4576  /* Fast path for a simple Var */
4577 
4578  if (IsA(basenode, Var) &&
4579  (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
4580  {
4581  Var *var = (Var *) basenode;
4582 
4583  /* Set up result fields other than the stats tuple */
4584  vardata->var = basenode; /* return Var without relabeling */
4585  vardata->rel = find_base_rel(root, var->varno);
4586  vardata->atttype = var->vartype;
4587  vardata->atttypmod = var->vartypmod;
4588  vardata->isunique = has_unique_index(vardata->rel, var->varattno);
4589 
4590  /* Try to locate some stats */
4591  examine_simple_variable(root, var, vardata);
4592 
4593  return;
4594  }
4595 
4596  /*
4597  * Okay, it's a more complicated expression. Determine variable
4598  * membership. Note that when varRelid isn't zero, only vars of that
4599  * relation are considered "real" vars.
4600  */
4601  varnos = pull_varnos(basenode);
4602 
4603  onerel = NULL;
4604 
4605  switch (bms_membership(varnos))
4606  {
4607  case BMS_EMPTY_SET:
4608  /* No Vars at all ... must be pseudo-constant clause */
4609  break;
4610  case BMS_SINGLETON:
4611  if (varRelid == 0 || bms_is_member(varRelid, varnos))
4612  {
4613  onerel = find_base_rel(root,
4614  (varRelid ? varRelid : bms_singleton_member(varnos)));
4615  vardata->rel = onerel;
4616  node = basenode; /* strip any relabeling */
4617  }
4618  /* else treat it as a constant */
4619  break;
4620  case BMS_MULTIPLE:
4621  if (varRelid == 0)
4622  {
4623  /* treat it as a variable of a join relation */
4624  vardata->rel = find_join_rel(root, varnos);
4625  node = basenode; /* strip any relabeling */
4626  }
4627  else if (bms_is_member(varRelid, varnos))
4628  {
4629  /* ignore the vars belonging to other relations */
4630  vardata->rel = find_base_rel(root, varRelid);
4631  node = basenode; /* strip any relabeling */
4632  /* note: no point in expressional-index search here */
4633  }
4634  /* else treat it as a constant */
4635  break;
4636  }
4637 
4638  bms_free(varnos);
4639 
4640  vardata->var = node;
4641  vardata->atttype = exprType(node);
4642  vardata->atttypmod = exprTypmod(node);
4643 
4644  if (onerel)
4645  {
4646  /*
4647  * We have an expression in vars of a single relation. Try to match
4648  * it to expressional index columns, in hopes of finding some
4649  * statistics.
4650  *
4651  * XXX it's conceivable that there are multiple matches with different
4652  * index opfamilies; if so, we need to pick one that matches the
4653  * operator we are estimating for. FIXME later.
4654  */
4655  ListCell *ilist;
4656 
4657  foreach(ilist, onerel->indexlist)
4658  {
4659  IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
4660  ListCell *indexpr_item;
4661  int pos;
4662 
4663  indexpr_item = list_head(index->indexprs);
4664  if (indexpr_item == NULL)
4665  continue; /* no expressions here... */
4666 
4667  for (pos = 0; pos < index->ncolumns; pos++)
4668  {
4669  if (index->indexkeys[pos] == 0)
4670  {
4671  Node *indexkey;
4672 
4673  if (indexpr_item == NULL)
4674  elog(ERROR, "too few entries in indexprs list");
4675  indexkey = (Node *) lfirst(indexpr_item);
4676  if (indexkey && IsA(indexkey, RelabelType))
4677  indexkey = (Node *) ((RelabelType *) indexkey)->arg;
4678  if (equal(node, indexkey))
4679  {
4680  /*
4681  * Found a match ... is it a unique index? Tests here
4682  * should match has_unique_index().
4683  */
4684  if (index->unique &&
4685  index->ncolumns == 1 &&
4686  (index->indpred == NIL || index->predOK))
4687  vardata->isunique = true;
4688 
4689  /*
4690  * Has it got stats? We only consider stats for
4691  * non-partial indexes, since partial indexes probably
4692  * don't reflect whole-relation statistics; the above
4693  * check for uniqueness is the only info we take from
4694  * a partial index.
4695  *
4696  * An index stats hook, however, must make its own
4697  * decisions about what to do with partial indexes.
4698  */
4699  if (get_index_stats_hook &&
4700  (*get_index_stats_hook) (root, index->indexoid,
4701  pos + 1, vardata))
4702  {
4703  /*
4704  * The hook took control of acquiring a stats
4705  * tuple. If it did supply a tuple, it'd better
4706  * have supplied a freefunc.
4707  */
4708  if (HeapTupleIsValid(vardata->statsTuple) &&
4709  !vardata->freefunc)
4710  elog(ERROR, "no function provided to release variable stats with");
4711  }
4712  else if (index->indpred == NIL)
4713  {
4714  vardata->statsTuple =
4716  ObjectIdGetDatum(index->indexoid),
4717  Int16GetDatum(pos + 1),
4718  BoolGetDatum(false));
4719  vardata->freefunc = ReleaseSysCache;
4720 
4721  if (HeapTupleIsValid(vardata->statsTuple))
4722  {
4723  /* Get index's table for permission check */
4724  RangeTblEntry *rte;
4725 
4726  rte = planner_rt_fetch(index->rel->relid, root);
4727  Assert(rte->rtekind == RTE_RELATION);
4728 
4729  /*
4730  * For simplicity, we insist on the whole
4731  * table being selectable, rather than trying
4732  * to identify which column(s) the index
4733  * depends on.
4734  */
4735  vardata->acl_ok =
4737  ACL_SELECT) == ACLCHECK_OK);
4738  }
4739  else
4740  {
4741  /* suppress leakproofness checks later */
4742  vardata->acl_ok = true;
4743  }
4744  }
4745  if (vardata->statsTuple)
4746  break;
4747  }
4748  indexpr_item = lnext(indexpr_item);
4749  }
4750  }
4751  if (vardata->statsTuple)
4752  break;
4753  }
4754  }
4755 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
bool predOK
Definition: relation.h:664
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:308
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2962
HeapTuple statsTuple
Definition: selfuncs.h:71
Oid GetUserId(void)
Definition: miscinit.c:283
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
RelOptInfo * rel
Definition: selfuncs.h:70
#define Int16GetDatum(X)
Definition: postgres.h:457
Definition: nodes.h:509
#define MemSet(start, val, len)
Definition: c.h:857
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
static void examine_simple_variable(PlannerInfo *root, Var *var, VariableStatData *vardata)
Definition: selfuncs.c:4767
int32 atttypmod
Definition: selfuncs.h:76
bool unique
Definition: relation.h:665
Definition: type.h:89
RelOptInfo * rel
Definition: relation.h:633
#define planner_rt_fetch(rti, root)
Definition: relation.h:325
bool has_unique_index(RelOptInfo *rel, AttrNumber attno)
Definition: plancat.c:1733
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Oid vartype
Definition: primnodes.h:170
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
int ncolumns
Definition: relation.h:641
#define lnext(lc)
Definition: pg_list.h:105
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: relation.h:553
Index varno
Definition: primnodes.h:166
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:634
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define ACL_SELECT
Definition: parsenodes.h:73
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:526
List * indexlist
Definition: relation.h:562
#define BoolGetDatum(X)
Definition: postgres.h:408
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
get_index_stats_hook_type get_index_stats_hook
Definition: selfuncs.c:155
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4422
RTEKind rtekind
Definition: parsenodes.h:936
#define SearchSysCache3(cacheId, key1, key2, key3)
Definition: syscache.h:160
void * arg
int * indexkeys
Definition: relation.h:642
#define elog
Definition: elog.h:219
Oid indexoid
Definition: relation.h:631
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:243
void(* freefunc)(HeapTuple tuple)
Definition: selfuncs.h:73
List * indpred
Definition: relation.h:654
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
List * indexprs
Definition: relation.h:653
int32 vartypmod
Definition: primnodes.h:171
void genericcostestimate ( PlannerInfo root,
IndexPath path,
double  loop_count,
List qinfos,
GenericCosts costs 
)

Definition at line 6441 of file selfuncs.c.

References add_predicate_to_quals(), ScalarArrayOpExpr::args, RestrictInfo::clause, clauselist_selectivity(), cpu_index_tuple_cost, cpu_operator_cost, estimate_array_length(), get_tablespace_page_costs(), index_pages_fetched(), GenericCosts::indexCorrelation, IndexPath::indexinfo, IndexPath::indexorderbys, IndexPath::indexquals, GenericCosts::indexSelectivity, GenericCosts::indexStartupCost, GenericCosts::indexTotalCost, IsA, JOIN_INNER, lfirst, list_length(), lsecond, NULL, GenericCosts::num_sa_scans, GenericCosts::numIndexPages, GenericCosts::numIndexTuples, orderby_operands_eval_cost(), other_operands_eval_cost(), IndexOptInfo::pages, IndexOptInfo::rel, RelOptInfo::relid, IndexOptInfo::reltablespace, rint(), GenericCosts::spc_random_page_cost, RelOptInfo::tuples, and IndexOptInfo::tuples.

Referenced by blcostestimate(), btcostestimate(), gistcostestimate(), hashcostestimate(), and spgcostestimate().

6446 {
6447  IndexOptInfo *index = path->indexinfo;
6448  List *indexQuals = path->indexquals;
6449  List *indexOrderBys = path->indexorderbys;
6450  Cost indexStartupCost;
6451  Cost indexTotalCost;
6452  Selectivity indexSelectivity;
6453  double indexCorrelation;
6454  double numIndexPages;
6455  double numIndexTuples;
6456  double spc_random_page_cost;
6457  double num_sa_scans;
6458  double num_outer_scans;
6459  double num_scans;
6460  double qual_op_cost;
6461  double qual_arg_cost;
6462  List *selectivityQuals;
6463  ListCell *l;
6464 
6465  /*
6466  * If the index is partial, AND the index predicate with the explicitly
6467  * given indexquals to produce a more accurate idea of the index
6468  * selectivity.
6469  */
6470  selectivityQuals = add_predicate_to_quals(index, indexQuals);
6471 
6472  /*
6473  * Check for ScalarArrayOpExpr index quals, and estimate the number of
6474  * index scans that will be performed.
6475  */
6476  num_sa_scans = 1;
6477  foreach(l, indexQuals)
6478  {
6479  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
6480 
6481  if (IsA(rinfo->clause, ScalarArrayOpExpr))
6482  {
6483  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
6484  int alength = estimate_array_length(lsecond(saop->args));
6485 
6486  if (alength > 1)
6487  num_sa_scans *= alength;
6488  }
6489  }
6490 
6491  /* Estimate the fraction of main-table tuples that will be visited */
6492  indexSelectivity = clauselist_selectivity(root, selectivityQuals,
6493  index->rel->relid,
6494  JOIN_INNER,
6495  NULL);
6496 
6497  /*
6498  * If caller didn't give us an estimate, estimate the number of index
6499  * tuples that will be visited. We do it in this rather peculiar-looking
6500  * way in order to get the right answer for partial indexes.
6501  */
6502  numIndexTuples = costs->numIndexTuples;
6503  if (numIndexTuples <= 0.0)
6504  {
6505  numIndexTuples = indexSelectivity * index->rel->tuples;
6506 
6507  /*
6508  * The above calculation counts all the tuples visited across all
6509  * scans induced by ScalarArrayOpExpr nodes. We want to consider the
6510  * average per-indexscan number, so adjust. This is a handy place to
6511  * round to integer, too. (If caller supplied tuple estimate, it's
6512  * responsible for handling these considerations.)
6513  */
6514  numIndexTuples = rint(numIndexTuples / num_sa_scans);
6515  }
6516 
6517  /*
6518  * We can bound the number of tuples by the index size in any case. Also,
6519  * always estimate at least one tuple is touched, even when
6520  * indexSelectivity estimate is tiny.
6521  */
6522  if (numIndexTuples > index->tuples)
6523  numIndexTuples = index->tuples;
6524  if (numIndexTuples < 1.0)
6525  numIndexTuples = 1.0;
6526 
6527  /*
6528  * Estimate the number of index pages that will be retrieved.
6529  *
6530  * We use the simplistic method of taking a pro-rata fraction of the total
6531  * number of index pages. In effect, this counts only leaf pages and not
6532  * any overhead such as index metapage or upper tree levels.
6533  *
6534  * In practice access to upper index levels is often nearly free because
6535  * those tend to stay in cache under load; moreover, the cost involved is
6536  * highly dependent on index type. We therefore ignore such costs here
6537  * and leave it to the caller to add a suitable charge if needed.
6538  */
6539  if (index->pages > 1 && index->tuples > 1)
6540  numIndexPages = ceil(numIndexTuples * index->pages / index->tuples);
6541  else
6542  numIndexPages = 1.0;
6543 
6544  /* fetch estimated page cost for tablespace containing index */
6546  &spc_random_page_cost,
6547  NULL);
6548 
6549  /*
6550  * Now compute the disk access costs.
6551  *
6552  * The above calculations are all per-index-scan. However, if we are in a
6553  * nestloop inner scan, we can expect the scan to be repeated (with
6554  * different search keys) for each row of the outer relation. Likewise,
6555  * ScalarArrayOpExpr quals result in multiple index scans. This creates
6556  * the potential for cache effects to reduce the number of disk page
6557  * fetches needed. We want to estimate the average per-scan I/O cost in
6558  * the presence of caching.
6559  *
6560  * We use the Mackert-Lohman formula (see costsize.c for details) to
6561  * estimate the total number of page fetches that occur. While this
6562  * wasn't what it was designed for, it seems a reasonable model anyway.
6563  * Note that we are counting pages not tuples anymore, so we take N = T =
6564  * index size, as if there were one "tuple" per page.
6565  */
6566  num_outer_scans = loop_count;
6567  num_scans = num_sa_scans * num_outer_scans;
6568 
6569  if (num_scans > 1)
6570  {
6571  double pages_fetched;
6572 
6573  /* total page fetches ignoring cache effects */
6574  pages_fetched = numIndexPages * num_scans;
6575 
6576  /* use Mackert and Lohman formula to adjust for cache effects */
6577  pages_fetched = index_pages_fetched(pages_fetched,
6578  index->pages,
6579  (double) index->pages,
6580  root);
6581 
6582  /*
6583  * Now compute the total disk access cost, and then report a pro-rated
6584  * share for each outer scan. (Don't pro-rate for ScalarArrayOpExpr,
6585  * since that's internal to the indexscan.)
6586  */
6587  indexTotalCost = (pages_fetched * spc_random_page_cost)
6588  / num_outer_scans;
6589  }
6590  else
6591  {
6592  /*
6593  * For a single index scan, we just charge spc_random_page_cost per
6594  * page touched.
6595  */
6596  indexTotalCost = numIndexPages * spc_random_page_cost;
6597  }
6598 
6599  /*
6600  * CPU cost: any complex expressions in the indexquals will need to be
6601  * evaluated once at the start of the scan to reduce them to runtime keys
6602  * to pass to the index AM (see nodeIndexscan.c). We model the per-tuple
6603  * CPU costs as cpu_index_tuple_cost plus one cpu_operator_cost per
6604  * indexqual operator. Because we have numIndexTuples as a per-scan
6605  * number, we have to multiply by num_sa_scans to get the correct result
6606  * for ScalarArrayOpExpr cases. Similarly add in costs for any index
6607  * ORDER BY expressions.
6608  *
6609  * Note: this neglects the possible costs of rechecking lossy operators.
6610  * Detecting that that might be needed seems more expensive than it's
6611  * worth, though, considering all the other inaccuracies here ...
6612  */
6613  qual_arg_cost = other_operands_eval_cost(root, qinfos) +
6614  orderby_operands_eval_cost(root, path);
6615  qual_op_cost = cpu_operator_cost *
6616  (list_length(indexQuals) + list_length(indexOrderBys));
6617 
6618  indexStartupCost = qual_arg_cost;
6619  indexTotalCost += qual_arg_cost;
6620  indexTotalCost += numIndexTuples * num_sa_scans * (cpu_index_tuple_cost + qual_op_cost);
6621 
6622  /*
6623  * Generic assumption about index correlation: there isn't any.
6624  */
6625  indexCorrelation = 0.0;
6626 
6627  /*
6628  * Return everything to caller.
6629  */
6630  costs->indexStartupCost = indexStartupCost;
6631  costs->indexTotalCost = indexTotalCost;
6632  costs->indexSelectivity = indexSelectivity;
6633  costs->indexCorrelation = indexCorrelation;
6634  costs->numIndexPages = numIndexPages;
6635  costs->numIndexTuples = numIndexTuples;
6636  costs->spc_random_page_cost = spc_random_page_cost;
6637  costs->num_sa_scans = num_sa_scans;
6638 }
Selectivity indexSelectivity
Definition: selfuncs.h:131
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
IndexOptInfo * indexinfo
Definition: relation.h:1031
double tuples
Definition: relation.h:565
static List * add_predicate_to_quals(IndexOptInfo *index, List *indexQuals)
Definition: selfuncs.c:6660
Oid reltablespace
Definition: relation.h:632
static Cost other_operands_eval_cost(PlannerInfo *root, List *qinfos)
Definition: selfuncs.c:6387
double Selectivity
Definition: nodes.h:639
double tuples
Definition: relation.h:637
#define lsecond(l)
Definition: pg_list.h:116
static Cost orderby_operands_eval_cost(PlannerInfo *root, IndexPath *path)
Definition: selfuncs.c:6412
Definition: type.h:89
BlockNumber pages
Definition: relation.h:636
List * indexquals
Definition: relation.h:1033
int estimate_array_length(Node *arrayexpr)
Definition: selfuncs.c:2094
RelOptInfo * rel
Definition: relation.h:633
double num_sa_scans
Definition: selfuncs.h:138
double cpu_operator_cost
Definition: costsize.c:108
Cost indexTotalCost
Definition: selfuncs.h:130
double rint(double x)
Definition: rint.c:22
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition: spccache.c:182
Index relid
Definition: relation.h:553
Expr * clause
Definition: relation.h:1747
double indexCorrelation
Definition: selfuncs.h:132
List * indexorderbys
Definition: relation.h:1035
double spc_random_page_cost
Definition: selfuncs.h:137
double numIndexTuples
Definition: selfuncs.h:136
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
Cost indexStartupCost
Definition: selfuncs.h:129
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:99
Definition: pg_list.h:45
double cpu_index_tuple_cost
Definition: costsize.c:107
double index_pages_fetched(double tuples_fetched, BlockNumber pages, double index_pages, PlannerInfo *root)
Definition: costsize.c:813
double Cost
Definition: nodes.h:640
double numIndexPages
Definition: selfuncs.h:135
void get_join_variables ( PlannerInfo root,
List args,
SpecialJoinInfo sjinfo,
VariableStatData vardata1,
VariableStatData vardata2,
bool join_is_reversed 
)

Definition at line 4494 of file selfuncs.c.

References bms_is_subset(), elog, ERROR, examine_variable(), linitial, list_length(), lsecond, VariableStatData::rel, RelOptInfo::relids, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by eqjoinsel(), and networkjoinsel().

4497 {
4498  Node *left,
4499  *right;
4500 
4501  if (list_length(args) != 2)
4502  elog(ERROR, "join operator should take two arguments");
4503 
4504  left = (Node *) linitial(args);
4505  right = (Node *) lsecond(args);
4506 
4507  examine_variable(root, left, 0, vardata1);
4508  examine_variable(root, right, 0, vardata2);
4509 
4510  if (vardata1->rel &&
4511  bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
4512  *join_is_reversed = true; /* var1 is on RHS */
4513  else if (vardata2->rel &&
4514  bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
4515  *join_is_reversed = true; /* var2 is on LHS */
4516  else
4517  *join_is_reversed = false;
4518 }
RelOptInfo * rel
Definition: selfuncs.h:70
Definition: nodes.h:509
#define lsecond(l)
Definition: pg_list.h:116
Relids syn_lefthand
Definition: relation.h:1919
Relids syn_righthand
Definition: relation.h:1920
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Relids relids
Definition: relation.h:525
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4556
static int list_length(const List *l)
Definition: pg_list.h:89
#define elog
Definition: elog.h:219
bool get_restriction_variable ( PlannerInfo root,
List args,
int  varRelid,
VariableStatData vardata,
Node **  other,
bool varonleft 
)

Definition at line 4434 of file selfuncs.c.

References estimate_expression_value(), examine_variable(), linitial, list_length(), lsecond, NULL, VariableStatData::rel, ReleaseVariableStats, and VariableStatData::var.

Referenced by _int_matchsel(), arraycontsel(), eqsel_internal(), ltreeparentsel(), networksel(), patternsel(), rangesel(), scalargtsel(), scalarltsel(), and tsmatchsel().

4437 {
4438  Node *left,
4439  *right;
4440  VariableStatData rdata;
4441 
4442  /* Fail if not a binary opclause (probably shouldn't happen) */
4443  if (list_length(args) != 2)
4444  return false;
4445 
4446  left = (Node *) linitial(args);
4447  right = (Node *) lsecond(args);
4448 
4449  /*
4450  * Examine both sides. Note that when varRelid is nonzero, Vars of other
4451  * relations will be treated as pseudoconstants.
4452  */
4453  examine_variable(root, left, varRelid, vardata);
4454  examine_variable(root, right, varRelid, &rdata);
4455 
4456  /*
4457  * If one side is a variable and the other not, we win.
4458  */
4459  if (vardata->rel && rdata.rel == NULL)
4460  {
4461  *varonleft = true;
4462  *other = estimate_expression_value(root, rdata.var);
4463  /* Assume we need no ReleaseVariableStats(rdata) here */
4464  return true;
4465  }
4466 
4467  if (vardata->rel == NULL && rdata.rel)
4468  {
4469  *varonleft = false;
4470  *other = estimate_expression_value(root, vardata->var);
4471  /* Assume we need no ReleaseVariableStats(*vardata) here */
4472  *vardata = rdata;
4473  return true;
4474  }
4475 
4476  /* Oops, clause has wrong structure (probably var op var) */
4477  ReleaseVariableStats(*vardata);
4478  ReleaseVariableStats(rdata);
4479 
4480  return false;
4481 }
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2433
RelOptInfo * rel
Definition: selfuncs.h:70
Definition: nodes.h:509
#define lsecond(l)
Definition: pg_list.h:116
#define linitial(l)
Definition: pg_list.h:111
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4556
#define NULL
Definition: c.h:229
static int list_length(const List *l)
Definition: pg_list.h:89
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
double get_variable_numdistinct ( VariableStatData vardata,
bool isdefault 
)

Definition at line 4963 of file selfuncs.c.

References BOOLOID, clamp_row_est(), DEFAULT_NUM_DISTINCT, GETSTRUCT, HeapTupleIsValid, IsA, VariableStatData::isunique, NULL, ObjectIdAttributeNumber, VariableStatData::rel, SelfItemPointerAttributeNumber, VariableStatData::statsTuple, TableOidAttributeNumber, RelOptInfo::tuples, VariableStatData::var, and VariableStatData::vartype.

Referenced by add_unique_group_var(), eqjoinsel_inner(), eqjoinsel_semi(), estimate_hash_bucketsize(), var_eq_const(), and var_eq_non_const().

4964 {
4965  double stadistinct;
4966  double stanullfrac = 0.0;
4967  double ntuples;
4968 
4969  *isdefault = false;
4970 
4971  /*
4972  * Determine the stadistinct value to use. There are cases where we can
4973  * get an estimate even without a pg_statistic entry, or can get a better
4974  * value than is in pg_statistic. Grab stanullfrac too if we can find it
4975  * (otherwise, assume no nulls, for lack of any better idea).
4976  */
4977  if (HeapTupleIsValid(vardata->statsTuple))
4978  {
4979  /* Use the pg_statistic entry */
4980  Form_pg_statistic stats;
4981 
4982  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
4983  stadistinct = stats->stadistinct;
4984  stanullfrac = stats->stanullfrac;
4985  }
4986  else if (vardata->vartype == BOOLOID)
4987  {
4988  /*
4989  * Special-case boolean columns: presumably, two distinct values.
4990  *
4991  * Are there any other datatypes we should wire in special estimates
4992  * for?
4993  */
4994  stadistinct = 2.0;
4995  }
4996  else
4997  {
4998  /*
4999  * We don't keep statistics for system columns, but in some cases we
5000  * can infer distinctness anyway.
5001  */
5002  if (vardata->var && IsA(vardata->var, Var))
5003  {
5004  switch (((Var *) vardata->var)->varattno)
5005  {
5008  stadistinct = -1.0; /* unique (and all non null) */
5009  break;
5011  stadistinct = 1.0; /* only 1 value */
5012  break;
5013  default:
5014  stadistinct = 0.0; /* means "unknown" */
5015  break;
5016  }
5017  }
5018  else
5019  stadistinct = 0.0; /* means "unknown" */
5020 
5021  /*
5022  * XXX consider using estimate_num_groups on expressions?
5023  */
5024  }
5025 
5026  /*
5027  * If there is a unique index or DISTINCT clause for the variable, assume
5028  * it is unique no matter what pg_statistic says; the statistics could be
5029  * out of date, or we might have found a partial unique index that proves
5030  * the var is unique for this query. However, we'd better still believe
5031  * the null-fraction statistic.
5032  */
5033  if (vardata->isunique)
5034  stadistinct = -1.0 * (1.0 - stanullfrac);
5035 
5036  /*
5037  * If we had an absolute estimate, use that.
5038  */
5039  if (stadistinct > 0.0)
5040  return clamp_row_est(stadistinct);
5041 
5042  /*
5043  * Otherwise we need to get the relation size; punt if not available.
5044  */
5045  if (vardata->rel == NULL)
5046  {
5047  *isdefault = true;
5048  return DEFAULT_NUM_DISTINCT;
5049  }
5050  ntuples = vardata->rel->tuples;
5051  if (ntuples <= 0.0)
5052  {
5053  *isdefault = true;
5054  return DEFAULT_NUM_DISTINCT;
5055  }
5056 
5057  /*
5058  * If we had a relative estimate, use that.
5059  */
5060  if (stadistinct < 0.0)
5061  return clamp_row_est(-stadistinct * ntuples);
5062 
5063  /*
5064  * With no data, estimate ndistinct = ntuples if the table is small, else
5065  * use default. We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
5066  * that the behavior isn't discontinuous.
5067  */
5068  if (ntuples < DEFAULT_NUM_DISTINCT)
5069  return clamp_row_est(ntuples);
5070 
5071  *isdefault = true;
5072  return DEFAULT_NUM_DISTINCT;
5073 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
HeapTuple statsTuple
Definition: selfuncs.h:71
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
double tuples
Definition: relation.h:565
RelOptInfo * rel
Definition: selfuncs.h:70
Definition: primnodes.h:163
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define TableOidAttributeNumber
Definition: sysattr.h:27
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define BOOLOID
Definition: pg_type.h:288
#define DEFAULT_NUM_DISTINCT
Definition: selfuncs.h:46
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
double clamp_row_est(double nrows)
Definition: costsize.c:173
double histogram_selectivity ( VariableStatData vardata,
FmgrInfo opproc,
Datum  constval,
bool  varonleft,
int  min_hist_size,
int  n_skip,
int *  hist_size 
)

Definition at line 704 of file selfuncs.c.

References Assert, ATTSTATSSLOT_VALUES, DatumGetBool, DEFAULT_COLLATION_OID, FmgrInfo::fn_oid, free_attstatsslot(), FunctionCall2Coll(), get_attstatsslot(), HeapTupleIsValid, i, InvalidOid, AttStatsSlot::nvalues, result, STATISTIC_KIND_HISTOGRAM, statistic_proc_security_check(), VariableStatData::statsTuple, and AttStatsSlot::values.

Referenced by ltreeparentsel(), and patternsel().

708 {
709  double result;
710  AttStatsSlot sslot;
711 
712  /* check sanity of parameters */
713  Assert(n_skip >= 0);
714  Assert(min_hist_size > 2 * n_skip);
715 
716  if (HeapTupleIsValid(vardata->statsTuple) &&
717  statistic_proc_security_check(vardata, opproc->fn_oid) &&
718  get_attstatsslot(&sslot, vardata->statsTuple,
721  {
722  *hist_size = sslot.nvalues;
723  if (sslot.nvalues >= min_hist_size)
724  {
725  int nmatch = 0;
726  int i;
727 
728  for (i = n_skip; i < sslot.nvalues - n_skip; i++)
729  {
730  if (varonleft ?
733  sslot.values[i],
734  constval)) :
737  constval,
738  sslot.values[i])))
739  nmatch++;
740  }
741  result = ((double) nmatch) / ((double) (sslot.nvalues - 2 * n_skip));
742  }
743  else
744  result = -1;
745  free_attstatsslot(&sslot);
746  }
747  else
748  {
749  *hist_size = 0;
750  result = -1;
751  }
752 
753  return result;
754 }
#define ATTSTATSSLOT_VALUES
Definition: lsyscache.h:39
HeapTuple statsTuple
Definition: selfuncs.h:71
#define STATISTIC_KIND_HISTOGRAM
Definition: pg_statistic.h:222
bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
Definition: selfuncs.c:4934
return result
Definition: formatting.c:1632
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1047
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
#define DatumGetBool(X)
Definition: postgres.h:399
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:2895
#define Assert(condition)
Definition: c.h:675
Datum * values
Definition: lsyscache.h:49
int i
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3011
Const* make_greater_string ( const Const str_const,
FmgrInfo ltproc,
Oid  collation 
)

Definition at line 6066 of file selfuncs.c.

References Assert, byte_increment(), BYTEAOID, Const::consttype, Const::constvalue, DatumGetBool, DatumGetByteaPP, DatumGetCString, DatumGetPointer, DirectFunctionCall1, FunctionCall2Coll(), lc_collate_is_c(), NAMEOID, nameout(), NULL, palloc(), pfree(), pg_database_encoding_character_incrementer(), pg_mbcliplen(), PointerGetDatum, SET_VARSIZE, string_to_bytea_const(), string_to_const(), TextDatumGetCString, VARDATA, VARDATA_ANY, VARHDRSZ, VARSIZE_ANY_EXHDR, and varstr_cmp().

Referenced by prefix_quals(), and prefix_selectivity().

6067 {
6068  Oid datatype = str_const->consttype;
6069  char *workstr;
6070  int len;
6071  Datum cmpstr;
6072  text *cmptxt = NULL;
6073  mbcharacter_incrementer charinc;
6074 
6075  /*
6076  * Get a modifiable copy of the prefix string in C-string format, and set
6077  * up the string we will compare to as a Datum. In C locale this can just
6078  * be the given prefix string, otherwise we need to add a suffix. Types
6079  * NAME and BYTEA sort bytewise so they don't need a suffix either.
6080  */
6081  if (datatype == NAMEOID)
6082  {
6084  str_const->constvalue));
6085  len = strlen(workstr);
6086  cmpstr = str_const->constvalue;
6087  }
6088  else if (datatype == BYTEAOID)
6089  {
6090  bytea *bstr = DatumGetByteaPP(str_const->constvalue);
6091 
6092  len = VARSIZE_ANY_EXHDR(bstr);
6093  workstr = (char *) palloc(len);
6094  memcpy(workstr, VARDATA_ANY(bstr), len);
6095  Assert((Pointer) bstr == DatumGetPointer(str_const->constvalue));
6096  cmpstr = str_const->constvalue;
6097  }
6098  else
6099  {
6100  workstr = TextDatumGetCString(str_const->constvalue);
6101  len = strlen(workstr);
6102  if (lc_collate_is_c(collation) || len == 0)
6103  cmpstr = str_const->constvalue;
6104  else
6105  {
6106  /* If first time through, determine the suffix to use */
6107  static char suffixchar = 0;
6108  static Oid suffixcollation = 0;
6109 
6110  if (!suffixchar || suffixcollation != collation)
6111  {
6112  char *best;
6113 
6114  best = "Z";
6115  if (varstr_cmp(best, 1, "z", 1, collation) < 0)
6116  best = "z";
6117  if (varstr_cmp(best, 1, "y", 1, collation) < 0)
6118  best = "y";
6119  if (varstr_cmp(best, 1, "9", 1, collation) < 0)
6120  best = "9";
6121  suffixchar = *best;
6122  suffixcollation = collation;
6123  }
6124 
6125  /* And build the string to compare to */
6126  cmptxt = (text *) palloc(VARHDRSZ + len + 1);
6127  SET_VARSIZE(cmptxt, VARHDRSZ + len + 1);
6128  memcpy(VARDATA(cmptxt), workstr, len);
6129  *(VARDATA(cmptxt) + len) = suffixchar;
6130  cmpstr = PointerGetDatum(cmptxt);
6131  }
6132  }
6133 
6134  /* Select appropriate character-incrementer function */
6135  if (datatype == BYTEAOID)
6136  charinc = byte_increment;
6137  else
6139 
6140  /* And search ... */
6141  while (len > 0)
6142  {
6143  int charlen;
6144  unsigned char *lastchar;
6145 
6146  /* Identify the last character --- for bytea, just the last byte */
6147  if (datatype == BYTEAOID)
6148  charlen = 1;
6149  else
6150  charlen = len - pg_mbcliplen(workstr, len, len - 1);
6151  lastchar = (unsigned char *) (workstr + len - charlen);
6152 
6153  /*
6154  * Try to generate a larger string by incrementing the last character
6155  * (for BYTEA, we treat each byte as a character).
6156  *
6157  * Note: the incrementer function is expected to return true if it's
6158  * generated a valid-per-the-encoding new character, otherwise false.
6159  * The contents of the character on false return are unspecified.
6160  */
6161  while (charinc(lastchar, charlen))
6162  {
6163  Const *workstr_const;
6164 
6165  if (datatype == BYTEAOID)
6166  workstr_const = string_to_bytea_const(workstr, len);
6167  else
6168  workstr_const = string_to_const(workstr, datatype);
6169 
6170  if (DatumGetBool(FunctionCall2Coll(ltproc,
6171  collation,
6172  cmpstr,
6173  workstr_const->constvalue)))
6174  {
6175  /* Successfully made a string larger than cmpstr */
6176  if (cmptxt)
6177  pfree(cmptxt);
6178  pfree(workstr);
6179  return workstr_const;
6180  }
6181 
6182  /* No good, release unusable value and try again */
6183  pfree(DatumGetPointer(workstr_const->constvalue));
6184  pfree(workstr_const);
6185  }
6186 
6187  /*
6188  * No luck here, so truncate off the last character and try to
6189  * increment the next one.
6190  */
6191  len -= charlen;
6192  workstr[len] = '\0';
6193  }
6194 
6195  /* Failed... */
6196  if (cmptxt)
6197  pfree(cmptxt);
6198  pfree(workstr);
6199 
6200  return NULL;
6201 }
Datum constvalue
Definition: primnodes.h:196
#define NAMEOID
Definition: pg_type.h:300
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARDATA(PTR)
Definition: postgres.h:303
#define PointerGetDatum(X)
Definition: postgres.h:562
#define VARHDRSZ
Definition: c.h:445
#define DatumGetByteaPP(X)
Definition: fmgr.h:255
static Const * string_to_bytea_const(const char *str, size_t str_len)
Definition: selfuncs.c:6272
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1047
unsigned int Oid
Definition: postgres_ext.h:31
static bool byte_increment(unsigned char *ptr, int len)
Definition: selfuncs.c:6016
static Const * string_to_const(const char *str, Oid datatype)
Definition: selfuncs.c:6229
Oid consttype
Definition: primnodes.h:192
void pfree(void *pointer)
Definition: mcxt.c:950
char * Pointer
Definition: c.h:245
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1128
#define DatumGetCString(X)
Definition: postgres.h:572
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:831
#define DatumGetBool(X)
Definition: postgres.h:399
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
bool(* mbcharacter_incrementer)(unsigned char *mbstr, int len)
Definition: pg_wchar.h:358
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
#define BYTEAOID
Definition: pg_type.h:292
#define DatumGetPointer(X)
Definition: postgres.h:555
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
mbcharacter_incrementer pg_database_encoding_character_incrementer(void)
Definition: wchar.c:1842
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
Datum nameout(PG_FUNCTION_ARGS)
Definition: name.c:69
double mcv_selectivity ( VariableStatData vardata,
FmgrInfo opproc,
Datum  constval,
bool  varonleft,
double *  sumcommonp 
)

Definition at line 632 of file selfuncs.c.

References ATTSTATSSLOT_NUMBERS, ATTSTATSSLOT_VALUES, DatumGetBool, DEFAULT_COLLATION_OID, FmgrInfo::fn_oid, free_attstatsslot(), FunctionCall2Coll(), get_attstatsslot(), HeapTupleIsValid, i, InvalidOid, AttStatsSlot::numbers, AttStatsSlot::nvalues, STATISTIC_KIND_MCV, statistic_proc_security_check(), VariableStatData::statsTuple, and AttStatsSlot::values.

Referenced by ltreeparentsel(), networksel(), patternsel(), and scalarineqsel().

635 {
636  double mcv_selec,
637  sumcommon;
638  AttStatsSlot sslot;
639  int i;
640 
641  mcv_selec = 0.0;
642  sumcommon = 0.0;
643 
644  if (HeapTupleIsValid(vardata->statsTuple) &&
645  statistic_proc_security_check(vardata, opproc->fn_oid) &&
646  get_attstatsslot(&sslot, vardata->statsTuple,
649  {
650  for (i = 0; i < sslot.nvalues; i++)
651  {
652  if (varonleft ?
655  sslot.values[i],
656  constval)) :
659  constval,
660  sslot.values[i])))
661  mcv_selec += sslot.numbers[i];
662  sumcommon += sslot.numbers[i];
663  }
664  free_attstatsslot(&sslot);
665  }
666 
667  *sumcommonp = sumcommon;
668  return mcv_selec;
669 }
#define ATTSTATSSLOT_VALUES
Definition: lsyscache.h:39
HeapTuple statsTuple
Definition: selfuncs.h:71
bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
Definition: selfuncs.c:4934
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1047
#define ATTSTATSSLOT_NUMBERS
Definition: lsyscache.h:40
float4 * numbers
Definition: lsyscache.h:52
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
#define DatumGetBool(X)
Definition: postgres.h:399
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:204
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:2895
Datum * values
Definition: lsyscache.h:49
int i
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3011
void mergejoinscansel ( PlannerInfo root,
Node clause,
Oid  opfamily,
int  strategy,
bool  nulls_first,
Selectivity leftstart,
Selectivity leftend,
Selectivity rightstart,
Selectivity rightend 
)

Definition at line 2845 of file selfuncs.c.

References Assert, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, CLAMP_PROBABILITY, DEFAULT_INEQ_SEL, examine_variable(), get_leftop(), get_op_opfamily_properties(), get_opfamily_member(), get_rightop(), get_variable_range(), GETSTRUCT, HeapTupleIsValid, is_opclause, OidIsValid, ReleaseVariableStats, scalarineqsel(), and VariableStatData::statsTuple.

Referenced by cached_scansel().

2849 {
2850  Node *left,
2851  *right;
2852  VariableStatData leftvar,
2853  rightvar;
2854  int op_strategy;
2855  Oid op_lefttype;
2856  Oid op_righttype;
2857  Oid opno,
2858  lsortop,
2859  rsortop,
2860  lstatop,
2861  rstatop,
2862  ltop,
2863  leop,
2864  revltop,
2865  revleop;
2866  bool isgt;
2867  Datum leftmin,
2868  leftmax,
2869  rightmin,
2870  rightmax;
2871  double selec;
2872 
2873  /* Set default results if we can't figure anything out. */
2874  /* XXX should default "start" fraction be a bit more than 0? */
2875  *leftstart = *rightstart = 0.0;
2876  *leftend = *rightend = 1.0;
2877 
2878  /* Deconstruct the merge clause */
2879  if (!is_opclause(clause))
2880  return; /* shouldn't happen */
2881  opno = ((OpExpr *) clause)->opno;
2882  left = get_leftop((Expr *) clause);
2883  right = get_rightop((Expr *) clause);
2884  if (!right)
2885  return; /* shouldn't happen */
2886 
2887  /* Look for stats for the inputs */
2888  examine_variable(root, left, 0, &leftvar);
2889  examine_variable(root, right, 0, &rightvar);
2890 
2891  /* Extract the operator's declared left/right datatypes */
2892  get_op_opfamily_properties(opno, opfamily, false,
2893  &op_strategy,
2894  &op_lefttype,
2895  &op_righttype);
2896  Assert(op_strategy == BTEqualStrategyNumber);
2897 
2898  /*
2899  * Look up the various operators we need. If we don't find them all, it
2900  * probably means the opfamily is broken, but we just fail silently.
2901  *
2902  * Note: we expect that pg_statistic histograms will be sorted by the '<'
2903  * operator, regardless of which sort direction we are considering.
2904  */
2905  switch (strategy)
2906  {
2907  case BTLessStrategyNumber:
2908  isgt = false;
2909  if (op_lefttype == op_righttype)
2910  {
2911  /* easy case */
2912  ltop = get_opfamily_member(opfamily,
2913  op_lefttype, op_righttype,
2915  leop = get_opfamily_member(opfamily,
2916  op_lefttype, op_righttype,
2918  lsortop = ltop;
2919  rsortop = ltop;
2920  lstatop = lsortop;
2921  rstatop = rsortop;
2922  revltop = ltop;
2923  revleop = leop;
2924  }
2925  else
2926  {
2927  ltop = get_opfamily_member(opfamily,
2928  op_lefttype, op_righttype,
2930  leop = get_opfamily_member(opfamily,
2931  op_lefttype, op_righttype,
2933  lsortop = get_opfamily_member(opfamily,
2934  op_lefttype, op_lefttype,
2936  rsortop = get_opfamily_member(opfamily,
2937  op_righttype, op_righttype,
2939  lstatop = lsortop;
2940  rstatop = rsortop;
2941  revltop = get_opfamily_member(opfamily,
2942  op_righttype, op_lefttype,
2944  revleop = get_opfamily_member(opfamily,
2945  op_righttype, op_lefttype,
2947  }
2948  break;
2950  /* descending-order case */
2951  isgt = true;
2952  if (op_lefttype == op_righttype)
2953  {
2954  /* easy case */
2955  ltop = get_opfamily_member(opfamily,
2956  op_lefttype, op_righttype,
2958  leop = get_opfamily_member(opfamily,
2959  op_lefttype, op_righttype,
2961  lsortop = ltop;
2962  rsortop = ltop;
2963  lstatop = get_opfamily_member(opfamily,
2964  op_lefttype, op_lefttype,
2966  rstatop = lstatop;
2967  revltop = ltop;
2968  revleop = leop;
2969  }
2970  else
2971  {
2972  ltop = get_opfamily_member(opfamily,
2973  op_lefttype, op_righttype,
2975  leop = get_opfamily_member(opfamily,
2976  op_lefttype, op_righttype,
2978  lsortop = get_opfamily_member(opfamily,
2979  op_lefttype, op_lefttype,
2981  rsortop = get_opfamily_member(opfamily,
2982  op_righttype, op_righttype,
2984  lstatop = get_opfamily_member(opfamily,
2985  op_lefttype, op_lefttype,
2987  rstatop = get_opfamily_member(opfamily,
2988  op_righttype, op_righttype,
2990  revltop = get_opfamily_member(opfamily,
2991  op_righttype, op_lefttype,
2993  revleop = get_opfamily_member(opfamily,
2994  op_righttype, op_lefttype,
2996  }
2997  break;
2998  default:
2999  goto fail; /* shouldn't get here */
3000  }
3001 
3002  if (!OidIsValid(lsortop) ||
3003  !OidIsValid(rsortop) ||
3004  !OidIsValid(lstatop) ||
3005  !OidIsValid(rstatop) ||
3006  !OidIsValid(ltop) ||
3007  !OidIsValid(leop) ||
3008  !OidIsValid(revltop) ||
3009  !OidIsValid(revleop))
3010  goto fail; /* insufficient info in catalogs */
3011 
3012  /* Try to get ranges of both inputs */
3013  if (!isgt)
3014  {
3015  if (!get_variable_range(root, &leftvar, lstatop,
3016  &leftmin, &leftmax))
3017  goto fail; /* no range available from stats */
3018  if (!get_variable_range(root, &rightvar, rstatop,
3019  &rightmin, &rightmax))
3020  goto fail; /* no range available from stats */
3021  }
3022  else
3023  {
3024  /* need to swap the max and min */
3025  if (!get_variable_range(root, &leftvar, lstatop,
3026  &leftmax, &leftmin))
3027  goto fail; /* no range available from stats */
3028  if (!get_variable_range(root, &rightvar, rstatop,
3029  &rightmax, &rightmin))
3030  goto fail; /* no range available from stats */
3031  }
3032 
3033  /*
3034  * Now, the fraction of the left variable that will be scanned is the
3035  * fraction that's <= the right-side maximum value. But only believe
3036  * non-default estimates, else stick with our 1.0.
3037  */
3038  selec = scalarineqsel(root, leop, isgt, &leftvar,
3039  rightmax, op_righttype);
3040  if (selec != DEFAULT_INEQ_SEL)
3041  *leftend = selec;
3042 
3043  /* And similarly for the right variable. */
3044  selec = scalarineqsel(root, revleop, isgt, &rightvar,
3045  leftmax, op_lefttype);
3046  if (selec != DEFAULT_INEQ_SEL)
3047  *rightend = selec;
3048 
3049  /*
3050  * Only one of the two "end" fractions can really be less than 1.0;
3051  * believe the smaller estimate and reset the other one to exactly 1.0. If
3052  * we get exactly equal estimates (as can easily happen with self-joins),
3053  * believe neither.
3054  */
3055  if (*leftend > *rightend)
3056  *leftend = 1.0;
3057  else if (*leftend < *rightend)
3058  *rightend = 1.0;
3059  else
3060  *leftend = *rightend = 1.0;
3061 
3062  /*
3063  * Also, the fraction of the left variable that will be scanned before the
3064  * first join pair is found is the fraction that's < the right-side
3065  * minimum value. But only believe non-default estimates, else stick with
3066  * our own default.
3067  */
3068  selec = scalarineqsel(root, ltop, isgt, &leftvar,
3069  rightmin, op_righttype);
3070  if (selec != DEFAULT_INEQ_SEL)
3071  *leftstart = selec;
3072 
3073  /* And similarly for the right variable. */
3074  selec = scalarineqsel(root, revltop, isgt, &rightvar,
3075  leftmin, op_lefttype);
3076  if (selec != DEFAULT_INEQ_SEL)
3077  *rightstart = selec;
3078 
3079  /*
3080  * Only one of the two "start" fractions can really be more than zero;
3081  * believe the larger estimate and reset the other one to exactly 0.0. If
3082  * we get exactly equal estimates (as can easily happen with self-joins),
3083  * believe neither.
3084  */
3085  if (*leftstart < *rightstart)
3086  *leftstart = 0.0;
3087  else if (*leftstart > *rightstart)
3088  *rightstart = 0.0;
3089  else
3090  *leftstart = *rightstart = 0.0;
3091 
3092  /*
3093  * If the sort order is nulls-first, we're going to have to skip over any
3094  * nulls too. These would not have been counted by scalarineqsel, and we
3095  * can safely add in this fraction regardless of whether we believe
3096  * scalarineqsel's results or not. But be sure to clamp the sum to 1.0!
3097  */
3098  if (nulls_first)
3099  {
3100  Form_pg_statistic stats;
3101 
3102  if (HeapTupleIsValid(leftvar.statsTuple))
3103  {
3104  stats = (Form_pg_statistic) GETSTRUCT(leftvar.statsTuple);
3105  *leftstart += stats->stanullfrac;
3106  CLAMP_PROBABILITY(*leftstart);
3107  *leftend += stats->stanullfrac;
3108  CLAMP_PROBABILITY(*leftend);
3109  }
3110  if (HeapTupleIsValid(rightvar.statsTuple))
3111  {
3112  stats = (Form_pg_statistic) GETSTRUCT(rightvar.statsTuple);
3113  *rightstart += stats->stanullfrac;
3114  CLAMP_PROBABILITY(*rightstart);
3115  *rightend += stats->stanullfrac;
3116  CLAMP_PROBABILITY(*rightend);
3117  }
3118  }
3119 
3120  /* Disbelieve start >= end, just in case that can happen */
3121  if (*leftstart >= *leftend)
3122  {
3123  *leftstart = 0.0;
3124  *leftend = 1.0;
3125  }
3126  if (*rightstart >= *rightend)
3127  {
3128  *rightstart = 0.0;
3129  *rightend = 1.0;
3130  }
3131 
3132 fail:
3133  ReleaseVariableStats(leftvar);
3134  ReleaseVariableStats(rightvar);
3135 }
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
HeapTuple statsTuple
Definition: selfuncs.h:71
#define DEFAULT_INEQ_SEL
Definition: selfuncs.h:37
Definition: nodes.h:509
unsigned int Oid
Definition: postgres_ext.h:31
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define OidIsValid(objectId)
Definition: c.h:538
static double scalarineqsel(PlannerInfo *root, Oid operator, bool isgt, VariableStatData *vardata, Datum constval, Oid consttype)
Definition: selfuncs.c:558
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
#define is_opclause(clause)
Definition: clauses.h:20
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
uintptr_t Datum
Definition: postgres.h:372
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4556
#define Assert(condition)
Definition: c.h:675
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:133
static bool get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop, Datum *min, Datum *max)
Definition: selfuncs.c:5085
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
Selectivity nulltestsel ( PlannerInfo root,
NullTestType  nulltesttype,
Node arg,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo 
)

Definition at line 1675 of file selfuncs.c.

References CLAMP_PROBABILITY, DEFAULT_NOT_UNK_SEL, DEFAULT_UNK_SEL, elog, ERROR, examine_variable(), GETSTRUCT, HeapTupleIsValid, IS_NOT_NULL, IS_NULL, ReleaseVariableStats, and VariableStatData::statsTuple.

Referenced by clause_selectivity(), and clauselist_selectivity().

1677 {
1678  VariableStatData vardata;
1679  double selec;
1680 
1681  examine_variable(root, arg, varRelid, &vardata);
1682 
1683  if (HeapTupleIsValid(vardata.statsTuple))
1684  {
1685  Form_pg_statistic stats;
1686  double freq_null;
1687 
1688  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1689  freq_null = stats->stanullfrac;
1690 
1691  switch (nulltesttype)
1692  {
1693  case IS_NULL:
1694 
1695  /*
1696  * Use freq_null directly.
1697  */
1698  selec = freq_null;
1699  break;
1700  case IS_NOT_NULL:
1701 
1702  /*
1703  * Select not unknown (not null) values. Calculate from
1704  * freq_null.
1705  */
1706  selec = 1.0 - freq_null;
1707  break;
1708  default:
1709  elog(ERROR, "unrecognized nulltesttype: %d",
1710  (int) nulltesttype);
1711  return (Selectivity) 0; /* keep compiler quiet */
1712  }
1713  }
1714  else
1715  {
1716  /*
1717  * No ANALYZE stats available, so make a guess
1718  */
1719  switch (nulltesttype)
1720  {
1721  case IS_NULL:
1722  selec = DEFAULT_UNK_SEL;
1723  break;
1724  case IS_NOT_NULL:
1725  selec = DEFAULT_NOT_UNK_SEL;
1726  break;
1727  default:
1728  elog(ERROR, "unrecognized nulltesttype: %d",
1729  (int) nulltesttype);
1730  return (Selectivity) 0; /* keep compiler quiet */
1731  }
1732  }
1733 
1734  ReleaseVariableStats(vardata);
1735 
1736  /* result should be in range, but make sure... */
1737  CLAMP_PROBABILITY(selec);
1738 
1739  return (Selectivity) selec;
1740 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
HeapTuple statsTuple
Definition: selfuncs.h:71
double Selectivity
Definition: nodes.h:639
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
#define DEFAULT_NOT_UNK_SEL
Definition: selfuncs.h:50
#define ERROR
Definition: elog.h:43
#define DEFAULT_UNK_SEL
Definition: selfuncs.h:49
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4556
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
#define elog
Definition: elog.h:219
Pattern_Prefix_Status pattern_fixed_prefix ( Const patt,
Pattern_Type  ptype,
Oid  collation,
Const **  prefix,
Selectivity rest_selec 
)

Definition at line 5702 of file selfuncs.c.

References elog, ERROR, like_fixed_prefix(), Pattern_Prefix_None, Pattern_Type_Like, Pattern_Type_Like_IC, Pattern_Type_Regex, Pattern_Type_Regex_IC, regex_fixed_prefix(), and result.

Referenced by expand_indexqual_opclause(), match_special_index_operator(), and patternsel().

5704 {
5706 
5707  switch (ptype)
5708  {
5709  case Pattern_Type_Like:
5710  result = like_fixed_prefix(patt, false, collation,
5711  prefix, rest_selec);
5712  break;
5713  case Pattern_Type_Like_IC:
5714  result = like_fixed_prefix(patt, true, collation,
5715  prefix, rest_selec);
5716  break;
5717  case Pattern_Type_Regex:
5718  result = regex_fixed_prefix(patt, false, collation,
5719  prefix, rest_selec);
5720  break;
5721  case Pattern_Type_Regex_IC:
5722  result = regex_fixed_prefix(patt, true, collation,
5723  prefix, rest_selec);
5724  break;
5725  default:
5726  elog(ERROR, "unrecognized ptype: %d", (int) ptype);
5727  result = Pattern_Prefix_None; /* keep compiler quiet */
5728  break;
5729  }
5730  return result;
5731 }
static Pattern_Prefix_Status regex_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
Definition: selfuncs.c:5634
static Pattern_Prefix_Status like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
Definition: selfuncs.c:5526
return result
Definition: formatting.c:1632
#define ERROR
Definition: elog.h:43
#define elog
Definition: elog.h:219
Pattern_Prefix_Status
Definition: selfuncs.h:94
Selectivity rowcomparesel ( PlannerInfo root,
RowCompareExpr clause,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo 
)

Definition at line 2132 of file selfuncs.c.

References RowCompareExpr::inputcollids, join_selectivity(), RowCompareExpr::largs, linitial, linitial_oid, list_make2, NULL, NumRelids(), RowCompareExpr::opnos, RowCompareExpr::rargs, restriction_selectivity(), and s1.

Referenced by clause_selectivity().

2135 {
2136  Selectivity s1;
2137  Oid opno = linitial_oid(clause->opnos);
2138  Oid inputcollid = linitial_oid(clause->inputcollids);
2139  List *opargs;
2140  bool is_join_clause;
2141 
2142  /* Build equivalent arg list for single operator */
2143  opargs = list_make2(linitial(clause->largs), linitial(clause->rargs));
2144 
2145  /*
2146  * Decide if it's a join clause. This should match clausesel.c's
2147  * treat_as_join_clause(), except that we intentionally consider only the
2148  * leading columns and not the rest of the clause.
2149  */
2150  if (varRelid != 0)
2151  {
2152  /*
2153  * Caller is forcing restriction mode (eg, because we are examining an
2154  * inner indexscan qual).
2155  */
2156  is_join_clause = false;
2157  }
2158  else if (sjinfo == NULL)
2159  {
2160  /*
2161  * It must be a restriction clause, since it's being evaluated at a
2162  * scan node.
2163  */
2164  is_join_clause = false;
2165  }
2166  else
2167  {
2168  /*
2169  * Otherwise, it's a join if there's more than one relation used.
2170  */
2171  is_join_clause = (NumRelids((Node *) opargs) > 1);
2172  }
2173 
2174  if (is_join_clause)
2175  {
2176  /* Estimate selectivity for a join clause. */
2177  s1 = join_selectivity(root, opno,
2178  opargs,
2179  inputcollid,
2180  jointype,
2181  sjinfo);
2182  }
2183  else
2184  {
2185  /* Estimate selectivity for a restriction clause. */
2186  s1 = restriction_selectivity(root, opno,
2187  opargs,
2188  inputcollid,
2189  varRelid);
2190  }
2191 
2192  return s1;
2193 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
Selectivity restriction_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, int varRelid)
Definition: plancat.c:1652
Definition: nodes.h:509
double Selectivity
Definition: nodes.h:639
unsigned int Oid
Definition: postgres_ext.h:31
#define linitial(l)
Definition: pg_list.h:111
char * s1
#define NULL
Definition: c.h:229
#define linitial_oid(l)
Definition: pg_list.h:113
Selectivity join_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: plancat.c:1689
List * inputcollids
Definition: primnodes.h:1034
Definition: pg_list.h:45
int NumRelids(Node *clause)
Definition: clauses.c:2217
Selectivity scalararraysel ( PlannerInfo root,
ScalarArrayOpExpr clause,
bool  is_join_clause,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo 
)

Definition at line 1775 of file selfuncs.c.

References generate_unaccent_rules::args, ScalarArrayOpExpr::args, ARR_ELEMTYPE, Assert, CLAMP_PROBABILITY, CaseTestExpr::collation, DatumGetArrayTypeP, DatumGetFloat8, deconstruct_array(), ArrayExpr::element_typeid, ArrayExpr::elements, TypeCacheEntry::eq_opr, estimate_expression_value(), exprCollation(), exprType(), fmgr_info(), FunctionCall4Coll(), FunctionCall5Coll(), get_base_element_type(), get_negator(), get_oprjoin(), get_oprrest(), get_typlenbyval(), get_typlenbyvalalign(), i, ScalarArrayOpExpr::inputcollid, Int16GetDatum, Int32GetDatum, IsA, lfirst, linitial, list_length(), list_make2, lookup_type_cache(), lsecond, makeConst(), makeNode, ObjectIdGetDatum, OidIsValid, ScalarArrayOpExpr::opno, PointerGetDatum, s1, s2, scalararraysel_containment(), strip_array_coercion(), TYPECACHE_EQ_OPR, CaseTestExpr::typeId, CaseTestExpr::typeMod, and ScalarArrayOpExpr::useOr.

Referenced by clause_selectivity().

1781 {
1782  Oid operator = clause->opno;
1783  bool useOr = clause->useOr;
1784  bool isEquality = false;
1785  bool isInequality = false;
1786  Node *leftop;
1787  Node *rightop;
1788  Oid nominal_element_type;
1789  Oid nominal_element_collation;
1790  TypeCacheEntry *typentry;
1791  RegProcedure oprsel;
1792  FmgrInfo oprselproc;
1793  Selectivity s1;
1794  Selectivity s1disjoint;
1795 
1796  /* First, deconstruct the expression */
1797  Assert(list_length(clause->args) == 2);
1798  leftop = (Node *) linitial(clause->args);
1799  rightop = (Node *) lsecond(clause->args);
1800 
1801  /* aggressively reduce both sides to constants */
1802  leftop = estimate_expression_value(root, leftop);
1803  rightop = estimate_expression_value(root, rightop);
1804 
1805  /* get nominal (after relabeling) element type of rightop */
1806  nominal_element_type = get_base_element_type(exprType(rightop));
1807  if (!OidIsValid(nominal_element_type))
1808  return (Selectivity) 0.5; /* probably shouldn't happen */
1809  /* get nominal collation, too, for generating constants */
1810  nominal_element_collation = exprCollation(rightop);
1811 
1812  /* look through any binary-compatible relabeling of rightop */
1813  rightop = strip_array_coercion(rightop);
1814 
1815  /*
1816  * Detect whether the operator is the default equality or inequality
1817  * operator of the array element type.
1818  */
1819  typentry = lookup_type_cache(nominal_element_type, TYPECACHE_EQ_OPR);
1820  if (OidIsValid(typentry->eq_opr))
1821  {
1822  if (operator == typentry->eq_opr)
1823  isEquality = true;
1824  else if (get_negator(operator) == typentry->eq_opr)
1825  isInequality = true;
1826  }
1827 
1828  /*
1829  * If it is equality or inequality, we might be able to estimate this as a
1830  * form of array containment; for instance "const = ANY(column)" can be
1831  * treated as "ARRAY[const] <@ column". scalararraysel_containment tries
1832  * that, and returns the selectivity estimate if successful, or -1 if not.
1833  */
1834  if ((isEquality || isInequality) && !is_join_clause)
1835  {
1836  s1 = scalararraysel_containment(root, leftop, rightop,
1837  nominal_element_type,
1838  isEquality, useOr, varRelid);
1839  if (s1 >= 0.0)
1840  return s1;
1841  }
1842 
1843  /*
1844  * Look up the underlying operator's selectivity estimator. Punt if it
1845  * hasn't got one.
1846  */
1847  if (is_join_clause)
1848  oprsel = get_oprjoin(operator);
1849  else
1850  oprsel = get_oprrest(operator);
1851  if (!oprsel)
1852  return (Selectivity) 0.5;
1853  fmgr_info(oprsel, &oprselproc);
1854 
1855  /*
1856  * In the array-containment check above, we must only believe that an
1857  * operator is equality or inequality if it is the default btree equality
1858  * operator (or its negator) for the element type, since those are the
1859  * operators that array containment will use. But in what follows, we can
1860  * be a little laxer, and also believe that any operators using eqsel() or
1861  * neqsel() as selectivity estimator act like equality or inequality.
1862  */
1863  if (oprsel == F_EQSEL || oprsel == F_EQJOINSEL)
1864  isEquality = true;
1865  else if (oprsel == F_NEQSEL || oprsel == F_NEQJOINSEL)
1866  isInequality = true;
1867 
1868  /*
1869  * We consider three cases:
1870  *
1871  * 1. rightop is an Array constant: deconstruct the array, apply the
1872  * operator's selectivity function for each array element, and merge the
1873  * results in the same way that clausesel.c does for AND/OR combinations.
1874  *
1875  * 2. rightop is an ARRAY[] construct: apply the operator's selectivity
1876  * function for each element of the ARRAY[] construct, and merge.
1877  *
1878  * 3. otherwise, make a guess ...
1879  */
1880  if (rightop && IsA(rightop, Const))
1881  {
1882  Datum arraydatum = ((Const *) rightop)->constvalue;
1883  bool arrayisnull = ((Const *) rightop)->constisnull;
1884  ArrayType *arrayval;
1885  int16 elmlen;
1886  bool elmbyval;
1887  char elmalign;
1888  int num_elems;
1889  Datum *elem_values;
1890  bool *elem_nulls;
1891  int i;
1892 
1893  if (arrayisnull) /* qual can't succeed if null array */
1894  return (Selectivity) 0.0;
1895  arrayval = DatumGetArrayTypeP(arraydatum);
1897  &elmlen, &elmbyval, &elmalign);
1898  deconstruct_array(arrayval,
1899  ARR_ELEMTYPE(arrayval),
1900  elmlen, elmbyval, elmalign,
1901  &elem_values, &elem_nulls, &num_elems);
1902 
1903  /*
1904  * For generic operators, we assume the probability of success is
1905  * independent for each array element. But for "= ANY" or "<> ALL",
1906  * if the array elements are distinct (which'd typically be the case)
1907  * then the probabilities are disjoint, and we should just sum them.
1908  *
1909  * If we were being really tense we would try to confirm that the
1910  * elements are all distinct, but that would be expensive and it
1911  * doesn't seem to be worth the cycles; it would amount to penalizing
1912  * well-written queries in favor of poorly-written ones. However, we
1913  * do protect ourselves a little bit by checking whether the
1914  * disjointness assumption leads to an impossible (out of range)
1915  * probability; if so, we fall back to the normal calculation.
1916  */
1917  s1 = s1disjoint = (useOr ? 0.0 : 1.0);
1918 
1919  for (i = 0; i < num_elems; i++)
1920  {
1921  List *args;
1922  Selectivity s2;
1923 
1924  args = list_make2(leftop,
1925  makeConst(nominal_element_type,
1926  -1,
1927  nominal_element_collation,
1928  elmlen,
1929  elem_values[i],
1930  elem_nulls[i],
1931  elmbyval));
1932  if (is_join_clause)
1933  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
1934  clause->inputcollid,
1935  PointerGetDatum(root),
1936  ObjectIdGetDatum(operator),
1937  PointerGetDatum(args),
1938  Int16GetDatum(jointype),
1939  PointerGetDatum(sjinfo)));
1940  else
1941  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
1942  clause->inputcollid,
1943  PointerGetDatum(root),
1944  ObjectIdGetDatum(operator),
1945  PointerGetDatum(args),
1946  Int32GetDatum(varRelid)));
1947 
1948  if (useOr)
1949  {
1950  s1 = s1 + s2 - s1 * s2;
1951  if (isEquality)
1952  s1disjoint += s2;
1953  }
1954  else
1955  {
1956  s1 = s1 * s2;
1957  if (isInequality)
1958  s1disjoint += s2 - 1.0;
1959  }
1960  }
1961 
1962  /* accept disjoint-probability estimate if in range */
1963  if ((useOr ? isEquality : isInequality) &&
1964  s1disjoint >= 0.0 && s1disjoint <= 1.0)
1965  s1 = s1disjoint;
1966  }
1967  else if (rightop && IsA(rightop, ArrayExpr) &&
1968  !((ArrayExpr *) rightop)->multidims)
1969  {
1970  ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
1971  int16 elmlen;
1972  bool elmbyval;
1973  ListCell *l;
1974 
1975  get_typlenbyval(arrayexpr->element_typeid,
1976  &elmlen, &elmbyval);
1977 
1978  /*
1979  * We use the assumption of disjoint probabilities here too, although
1980  * the odds of equal array elements are rather higher if the elements
1981  * are not all constants (which they won't be, else constant folding
1982  * would have reduced the ArrayExpr to a Const). In this path it's
1983  * critical to have the sanity check on the s1disjoint estimate.
1984  */
1985  s1 = s1disjoint = (useOr ? 0.0 : 1.0);
1986 
1987  foreach(l, arrayexpr->elements)
1988  {
1989  Node *elem = (Node *) lfirst(l);
1990  List *args;
1991  Selectivity s2;
1992 
1993  /*
1994  * Theoretically, if elem isn't of nominal_element_type we should
1995  * insert a RelabelType, but it seems unlikely that any operator
1996  * estimation function would really care ...
1997  */
1998  args = list_make2(leftop, elem);
1999  if (is_join_clause)
2000  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
2001  clause->inputcollid,
2002  PointerGetDatum(root),
2003  ObjectIdGetDatum(operator),
2004  PointerGetDatum(args),
2005  Int16GetDatum(jointype),
2006  PointerGetDatum(sjinfo)));
2007  else
2008  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
2009  clause->inputcollid,
2010  PointerGetDatum(root),
2011  ObjectIdGetDatum(operator),
2012  PointerGetDatum(args),
2013  Int32GetDatum(varRelid)));
2014 
2015  if (useOr)
2016  {
2017  s1 = s1 + s2 - s1 * s2;
2018  if (isEquality)
2019  s1disjoint += s2;
2020  }
2021  else
2022  {
2023  s1 = s1 * s2;
2024  if (isInequality)
2025  s1disjoint += s2 - 1.0;
2026  }
2027  }
2028 
2029  /* accept disjoint-probability estimate if in range */
2030  if ((useOr ? isEquality : isInequality) &&
2031  s1disjoint >= 0.0 && s1disjoint <= 1.0)
2032  s1 = s1disjoint;
2033  }
2034  else
2035  {
2036  CaseTestExpr *dummyexpr;
2037  List *args;
2038  Selectivity s2;
2039  int i;
2040 
2041  /*
2042  * We need a dummy rightop to pass to the operator selectivity
2043  * routine. It can be pretty much anything that doesn't look like a
2044  * constant; CaseTestExpr is a convenient choice.
2045  */
2046  dummyexpr = makeNode(CaseTestExpr);
2047  dummyexpr->typeId = nominal_element_type;
2048  dummyexpr->typeMod = -1;
2049  dummyexpr->collation = clause->inputcollid;
2050  args = list_make2(leftop, dummyexpr);
2051  if (is_join_clause)
2052  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
2053  clause->inputcollid,
2054  PointerGetDatum(root),
2055  ObjectIdGetDatum(operator),
2056  PointerGetDatum(args),
2057  Int16GetDatum(jointype),
2058  PointerGetDatum(sjinfo)));
2059  else
2060  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
2061  clause->inputcollid,
2062  PointerGetDatum(root),
2063  ObjectIdGetDatum(operator),
2064  PointerGetDatum(args),
2065  Int32GetDatum(varRelid)));
2066  s1 = useOr ? 0.0 : 1.0;
2067 
2068  /*
2069  * Arbitrarily assume 10 elements in the eventual array value (see
2070  * also estimate_array_length). We don't risk an assumption of
2071  * disjoint probabilities here.
2072  */
2073  for (i = 0; i < 10; i++)
2074  {
2075  if (useOr)
2076  s1 = s1 + s2 - s1 * s2;
2077  else
2078  s1 = s1 * s2;
2079  }
2080  }
2081 
2082  /* result should be in range, but make sure... */
2083  CLAMP_PROBABILITY(s1);
2084 
2085  return s1;
2086 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
signed short int16
Definition: c.h:255
Definition: fmgr.h:56
RegProcedure get_oprjoin(Oid opno)
Definition: lsyscache.c:1385
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2433
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2021
#define PointerGetDatum(X)
Definition: postgres.h:562
Datum FunctionCall5Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5)
Definition: fmgr.c:1121
regproc RegProcedure
Definition: c.h:395
#define Int16GetDatum(X)
Definition: postgres.h:457
Definition: nodes.h:509
#define TYPECACHE_EQ_OPR
Definition: typcache.h:110
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1094
double Selectivity
Definition: nodes.h:639
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 OidIsValid(objectId)
Definition: c.h:538
#define lsecond(l)
Definition: pg_list.h:116
int32 typeMod
Definition: primnodes.h:937
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
#define linitial(l)
Definition: pg_list.h:111
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
char * s1
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
List * elements
Definition: primnodes.h:955
RegProcedure get_oprrest(Oid opno)
Definition: lsyscache.c:1361
char * s2
#define DatumGetFloat8(X)
Definition: postgres.h:734
uintptr_t Datum
Definition: postgres.h:372
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Selectivity scalararraysel_containment(PlannerInfo *root, Node *leftop, Node *rightop, Oid elemtype, bool isEquality, bool useOr, int varRelid)
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2001
Oid element_typeid
Definition: primnodes.h:954
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2557
#define Int32GetDatum(X)
Definition: postgres.h:485
int i
Oid get_negator(Oid opno)
Definition: lsyscache.c:1337
Definition: pg_list.h:45
#define ARR_ELEMTYPE(a)
Definition: array.h:273
static Node * strip_array_coercion(Node *node)
Definition: selfuncs.c:1751
#define DatumGetArrayTypeP(X)
Definition: array.h:242
Selectivity scalararraysel_containment ( PlannerInfo root,
Node leftop,
Node rightop,
Oid  elemtype,
bool  isEquality,
bool  useOr,
int  varRelid 
)

Definition at line 83 of file array_selfuncs.c.

References ATTSTATSSLOT_NUMBERS, ATTSTATSSLOT_VALUES, CLAMP_PROBABILITY, TypeCacheEntry::cmp_proc_finfo, examine_variable(), FmgrInfo::fn_oid, free_attstatsslot(), get_attstatsslot(), GETSTRUCT, HeapTupleIsValid, InvalidOid, IsA, lookup_type_cache(), mcelem_array_contain_overlap_selec(), mcelem_array_contained_selec(), AttStatsSlot::nnumbers, NULL, AttStatsSlot::numbers, AttStatsSlot::nvalues, OID_ARRAY_CONTAINED_OP, OID_ARRAY_CONTAINS_OP, OidIsValid, VariableStatData::rel, ReleaseVariableStats, STATISTIC_KIND_DECHIST, STATISTIC_KIND_MCELEM, statistic_proc_security_check(), VariableStatData::statsTuple, TYPECACHE_CMP_PROC_FINFO, and AttStatsSlot::values.

Referenced by scalararraysel().

87 {
88  Selectivity selec;
89  VariableStatData vardata;
90  Datum constval;
91  TypeCacheEntry *typentry;
92  FmgrInfo *cmpfunc;
93 
94  /*
95  * rightop must be a variable, else punt.
96  */
97  examine_variable(root, rightop, varRelid, &vardata);
98  if (!vardata.rel)
99  {
100  ReleaseVariableStats(vardata);
101  return -1.0;
102  }
103 
104  /*
105  * leftop must be a constant, else punt.
106  */
107  if (!IsA(leftop, Const))
108  {
109  ReleaseVariableStats(vardata);
110  return -1.0;
111  }
112  if (((Const *) leftop)->constisnull)
113  {
114  /* qual can't succeed if null on left */
115  ReleaseVariableStats(vardata);
116  return (Selectivity) 0.0;
117  }
118  constval = ((Const *) leftop)->constvalue;
119 
120  /* Get element type's default comparison function */
121  typentry = lookup_type_cache(elemtype, TYPECACHE_CMP_PROC_FINFO);
122  if (!OidIsValid(typentry->cmp_proc_finfo.fn_oid))
123  {
124  ReleaseVariableStats(vardata);
125  return -1.0;
126  }
127  cmpfunc = &typentry->cmp_proc_finfo;
128 
129  /*
130  * If the operator is <>, swap ANY/ALL, then invert the result later.
131  */
132  if (!isEquality)
133  useOr = !useOr;
134 
135  /* Get array element stats for var, if available */
136  if (HeapTupleIsValid(vardata.statsTuple) &&
137  statistic_proc_security_check(&vardata, cmpfunc->fn_oid))
138  {
139  Form_pg_statistic stats;
140  AttStatsSlot sslot;
141  AttStatsSlot hslot;
142 
143  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
144 
145  /* MCELEM will be an array of same type as element */
146  if (get_attstatsslot(&sslot, vardata.statsTuple,
149  {
150  /* For ALL case, also get histogram of distinct-element counts */
151  if (useOr ||
152  !get_attstatsslot(&hslot, vardata.statsTuple,
155  memset(&hslot, 0, sizeof(hslot));
156 
157  /*
158  * For = ANY, estimate as var @> ARRAY[const].
159  *
160  * For = ALL, estimate as var <@ ARRAY[const].
161  */
162  if (useOr)
164  sslot.nvalues,
165  sslot.numbers,
166  sslot.nnumbers,
167  &constval, 1,
169  cmpfunc);
170  else
171  selec = mcelem_array_contained_selec(sslot.values,
172  sslot.nvalues,
173  sslot.numbers,
174  sslot.nnumbers,
175  &constval, 1,
176  hslot.numbers,
177  hslot.nnumbers,
179  cmpfunc);
180 
181  free_attstatsslot(&hslot);
182  free_attstatsslot(&sslot);
183  }
184  else
185  {
186  /* No most-common-elements info, so do without */
187  if (useOr)
189  NULL, 0,
190  &constval, 1,
192  cmpfunc);
193  else
195  NULL, 0,
196  &constval, 1,
197  NULL, 0,
199  cmpfunc);
200  }
201 
202  /*
203  * MCE stats count only non-null rows, so adjust for null rows.
204  */
205  selec *= (1.0 - stats->stanullfrac);
206  }
207  else
208  {
209  /* No stats at all, so do without */
210  if (useOr)
212  NULL, 0,
213  &constval, 1,
215  cmpfunc);
216  else
218  NULL, 0,
219  &constval, 1,
220  NULL, 0,
222  cmpfunc);
223  /* we assume no nulls here, so no stanullfrac correction */
224  }
225 
226  ReleaseVariableStats(vardata);
227 
228  /*
229  * If the operator is <>, invert the results.
230  */
231  if (!isEquality)
232  selec = 1.0 - selec;
233 
234  CLAMP_PROBABILITY(selec);
235 
236  return selec;
237 }
Definition: fmgr.h:56
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define ATTSTATSSLOT_VALUES
Definition: lsyscache.h:39
HeapTuple statsTuple
Definition: selfuncs.h:71
int nnumbers
Definition: lsyscache.h:53
bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
Definition: selfuncs.c:4934
RelOptInfo * rel
Definition: selfuncs.h:70
double Selectivity
Definition: nodes.h:639
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define OidIsValid(objectId)
Definition: c.h:538
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
FmgrInfo cmp_proc_finfo
Definition: typcache.h:68
#define OID_ARRAY_CONTAINS_OP
Definition: pg_operator.h:1569
#define ATTSTATSSLOT_NUMBERS
Definition: lsyscache.h:40
#define STATISTIC_KIND_DECHIST
Definition: pg_statistic.h:270
float4 * numbers
Definition: lsyscache.h:52
#define OID_ARRAY_CONTAINED_OP
Definition: pg_operator.h:1572
uintptr_t Datum
Definition: postgres.h:372
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4556
#define NULL
Definition: c.h:229
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:2895
Datum * values
Definition: lsyscache.h:49
static Selectivity mcelem_array_contained_selec(Datum *mcelem, int nmcelem, float4 *numbers, int nnumbers, Datum *array_data, int nitems, float4 *hist, int nhist, Oid operator, FmgrInfo *cmpfunc)
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
#define STATISTIC_KIND_MCELEM
Definition: pg_statistic.h:257
#define TYPECACHE_CMP_PROC_FINFO
Definition: typcache.h:116
static Selectivity mcelem_array_contain_overlap_selec(Datum *mcelem, int nmcelem, float4 *numbers, int nnumbers, Datum *array_data, int nitems, Oid operator, FmgrInfo *cmpfunc)
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3011
bool statistic_proc_security_check ( VariableStatData vardata,
Oid  func_oid 
)

Definition at line 4934 of file selfuncs.c.

References VariableStatData::acl_ok, DEBUG2, ereport, errmsg_internal(), get_func_leakproof(), get_func_name(), and OidIsValid.

Referenced by calc_arraycontsel(), calc_hist_selectivity(), eqjoinsel_inner(), eqjoinsel_semi(), get_variable_range(), histogram_selectivity(), ineq_histogram_selectivity(), mcv_selectivity(), scalararraysel_containment(), and var_eq_const().

4935 {
4936  if (vardata->acl_ok)
4937  return true;
4938 
4939  if (!OidIsValid(func_oid))
4940  return false;
4941 
4942  if (get_func_leakproof(func_oid))
4943  return true;
4944 
4945  ereport(DEBUG2,
4946  (errmsg_internal("not using statistics because function \"%s\" is not leak-proof",
4947  get_func_name(func_oid))));
4948  return false;
4949 }
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1622
#define OidIsValid(objectId)
Definition: c.h:538
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1412
#define DEBUG2
Definition: elog.h:24
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827

Variable Documentation

PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook

Definition at line 155 of file selfuncs.c.

Referenced by brincostestimate(), btcostestimate(), and examine_variable().

PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook

Definition at line 154 of file selfuncs.c.

Referenced by brincostestimate(), btcostestimate(), and examine_simple_variable().