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)
 
void estimate_hash_bucket_stats (PlannerInfo *root, Node *hashkey, double nbuckets, Selectivity *mcv_freq, Selectivity *bucketsize_frac)
 
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:4572
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:4572
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
List* deconstruct_indexquals ( IndexPath path)

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

6320 {
6321  List *result = NIL;
6322  IndexOptInfo *index = path->indexinfo;
6323  ListCell *lcc,
6324  *lci;
6325 
6326  forboth(lcc, path->indexquals, lci, path->indexqualcols)
6327  {
6328  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lcc);
6329  int indexcol = lfirst_int(lci);
6330  Expr *clause;
6331  Node *leftop,
6332  *rightop;
6333  IndexQualInfo *qinfo;
6334 
6335  clause = rinfo->clause;
6336 
6337  qinfo = (IndexQualInfo *) palloc(sizeof(IndexQualInfo));
6338  qinfo->rinfo = rinfo;
6339  qinfo->indexcol = indexcol;
6340 
6341  if (IsA(clause, OpExpr))
6342  {
6343  qinfo->clause_op = ((OpExpr *) clause)->opno;
6344  leftop = get_leftop(clause);
6345  rightop = get_rightop(clause);
6346  if (match_index_to_operand(leftop, indexcol, index))
6347  {
6348  qinfo->varonleft = true;
6349  qinfo->other_operand = rightop;
6350  }
6351  else
6352  {
6353  Assert(match_index_to_operand(rightop, indexcol, index));
6354  qinfo->varonleft = false;
6355  qinfo->other_operand = leftop;
6356  }
6357  }
6358  else if (IsA(clause, RowCompareExpr))
6359  {
6360  RowCompareExpr *rc = (RowCompareExpr *) clause;
6361 
6362  qinfo->clause_op = linitial_oid(rc->opnos);
6363  /* Examine only first columns to determine left/right sides */
6365  indexcol, index))
6366  {
6367  qinfo->varonleft = true;
6368  qinfo->other_operand = (Node *) rc->rargs;
6369  }
6370  else
6371  {
6373  indexcol, index));
6374  qinfo->varonleft = false;
6375  qinfo->other_operand = (Node *) rc->largs;
6376  }
6377  }
6378  else if (IsA(clause, ScalarArrayOpExpr))
6379  {
6380  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
6381 
6382  qinfo->clause_op = saop->opno;
6383  /* index column is always on the left in this case */
6385  indexcol, index));
6386  qinfo->varonleft = true;
6387  qinfo->other_operand = (Node *) lsecond(saop->args);
6388  }
6389  else if (IsA(clause, NullTest))
6390  {
6391  qinfo->clause_op = InvalidOid;
6392  Assert(match_index_to_operand((Node *) ((NullTest *) clause)->arg,
6393  indexcol, index));
6394  qinfo->varonleft = true;
6395  qinfo->other_operand = NULL;
6396  }
6397  else
6398  {
6399  elog(ERROR, "unsupported indexqual type: %d",
6400  (int) nodeTag(clause));
6401  }
6402 
6403  result = lappend(result, qinfo);
6404  }
6405  return result;
6406 }
#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:1633
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:676
#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
void estimate_hash_bucket_stats ( PlannerInfo root,
Node hashkey,
double  nbuckets,
Selectivity mcv_freq,
Selectivity bucketsize_frac 
)

Definition at line 3605 of file selfuncs.c.

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

Referenced by final_cost_hashjoin().

3608 {
3609  VariableStatData vardata;
3610  double estfract,
3611  ndistinct,
3612  stanullfrac,
3613  avgfreq;
3614  bool isdefault;
3615  AttStatsSlot sslot;
3616 
3617  examine_variable(root, hashkey, 0, &vardata);
3618 
3619  /* Look up the frequency of the most common value, if available */
3620  *mcv_freq = 0.0;
3621 
3622  if (HeapTupleIsValid(vardata.statsTuple))
3623  {
3624  if (get_attstatsslot(&sslot, vardata.statsTuple,
3627  {
3628  /*
3629  * The first MCV stat is for the most common value.
3630  */
3631  if (sslot.nnumbers > 0)
3632  *mcv_freq = sslot.numbers[0];
3633  free_attstatsslot(&sslot);
3634  }
3635  }
3636 
3637  /* Get number of distinct values */
3638  ndistinct = get_variable_numdistinct(&vardata, &isdefault);
3639 
3640  /*
3641  * If ndistinct isn't real, punt. We normally return 0.1, but if the
3642  * mcv_freq is known to be even higher than that, use it instead.
3643  */
3644  if (isdefault)
3645  {
3646  *bucketsize_frac = (Selectivity) Max(0.1, *mcv_freq);
3647  ReleaseVariableStats(vardata);
3648  return;
3649  }
3650 
3651  /* Get fraction that are null */
3652  if (HeapTupleIsValid(vardata.statsTuple))
3653  {
3654  Form_pg_statistic stats;
3655 
3656  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
3657  stanullfrac = stats->stanullfrac;
3658  }
3659  else
3660  stanullfrac = 0.0;
3661 
3662  /* Compute avg freq of all distinct data values in raw relation */
3663  avgfreq = (1.0 - stanullfrac) / ndistinct;
3664 
3665  /*
3666  * Adjust ndistinct to account for restriction clauses. Observe we are
3667  * assuming that the data distribution is affected uniformly by the
3668  * restriction clauses!
3669  *
3670  * XXX Possibly better way, but much more expensive: multiply by
3671  * selectivity of rel's restriction clauses that mention the target Var.
3672  */
3673  if (vardata.rel && vardata.rel->tuples > 0)
3674  {
3675  ndistinct *= vardata.rel->rows / vardata.rel->tuples;
3676  ndistinct = clamp_row_est(ndistinct);
3677  }
3678 
3679  /*
3680  * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
3681  * number of buckets is less than the expected number of distinct values;
3682  * otherwise it is 1/ndistinct.
3683  */
3684  if (ndistinct > nbuckets)
3685  estfract = 1.0 / nbuckets;
3686  else
3687  estfract = 1.0 / ndistinct;
3688 
3689  /*
3690  * Adjust estimated bucketsize upward to account for skewed distribution.
3691  */
3692  if (avgfreq > 0.0 && *mcv_freq > avgfreq)
3693  estfract *= *mcv_freq / avgfreq;
3694 
3695  /*
3696  * Clamp bucketsize to sane range (the above adjustment could easily
3697  * produce an out-of-range result). We set the lower bound a little above
3698  * zero, since zero isn't a very sane result.
3699  */
3700  if (estfract < 1.0e-6)
3701  estfract = 1.0e-6;
3702  else if (estfract > 1.0)
3703  estfract = 1.0;
3704 
3705  *bucketsize_frac = (Selectivity) estfract;
3706 
3707  ReleaseVariableStats(vardata);
3708 }
#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:4979
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 Max(x, y)
Definition: c.h:801
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4572
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:957
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:3729
#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:4572
#define Assert(condition)
Definition: c.h:676
#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 4572 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_bucket_stats(), estimate_num_groups(), get_join_variables(), get_restriction_variable(), mergejoinscansel(), nulltestsel(), and scalararraysel_containment().

4574 {
4575  Node *basenode;
4576  Relids varnos;
4577  RelOptInfo *onerel;
4578 
4579  /* Make sure we don't return dangling pointers in vardata */
4580  MemSet(vardata, 0, sizeof(VariableStatData));
4581 
4582  /* Save the exposed type of the expression */
4583  vardata->vartype = exprType(node);
4584 
4585  /* Look inside any binary-compatible relabeling */
4586 
4587  if (IsA(node, RelabelType))
4588  basenode = (Node *) ((RelabelType *) node)->arg;
4589  else
4590  basenode = node;
4591 
4592  /* Fast path for a simple Var */
4593 
4594  if (IsA(basenode, Var) &&
4595  (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
4596  {
4597  Var *var = (Var *) basenode;
4598 
4599  /* Set up result fields other than the stats tuple */
4600  vardata->var = basenode; /* return Var without relabeling */
4601  vardata->rel = find_base_rel(root, var->varno);
4602  vardata->atttype = var->vartype;
4603  vardata->atttypmod = var->vartypmod;
4604  vardata->isunique = has_unique_index(vardata->rel, var->varattno);
4605 
4606  /* Try to locate some stats */
4607  examine_simple_variable(root, var, vardata);
4608 
4609  return;
4610  }
4611 
4612  /*
4613  * Okay, it's a more complicated expression. Determine variable
4614  * membership. Note that when varRelid isn't zero, only vars of that
4615  * relation are considered "real" vars.
4616  */
4617  varnos = pull_varnos(basenode);
4618 
4619  onerel = NULL;
4620 
4621  switch (bms_membership(varnos))
4622  {
4623  case BMS_EMPTY_SET:
4624  /* No Vars at all ... must be pseudo-constant clause */
4625  break;
4626  case BMS_SINGLETON:
4627  if (varRelid == 0 || bms_is_member(varRelid, varnos))
4628  {
4629  onerel = find_base_rel(root,
4630  (varRelid ? varRelid : bms_singleton_member(varnos)));
4631  vardata->rel = onerel;
4632  node = basenode; /* strip any relabeling */
4633  }
4634  /* else treat it as a constant */
4635  break;
4636  case BMS_MULTIPLE:
4637  if (varRelid == 0)
4638  {
4639  /* treat it as a variable of a join relation */
4640  vardata->rel = find_join_rel(root, varnos);
4641  node = basenode; /* strip any relabeling */
4642  }
4643  else if (bms_is_member(varRelid, varnos))
4644  {
4645  /* ignore the vars belonging to other relations */
4646  vardata->rel = find_base_rel(root, varRelid);
4647  node = basenode; /* strip any relabeling */
4648  /* note: no point in expressional-index search here */
4649  }
4650  /* else treat it as a constant */
4651  break;
4652  }
4653 
4654  bms_free(varnos);
4655 
4656  vardata->var = node;
4657  vardata->atttype = exprType(node);
4658  vardata->atttypmod = exprTypmod(node);
4659 
4660  if (onerel)
4661  {
4662  /*
4663  * We have an expression in vars of a single relation. Try to match
4664  * it to expressional index columns, in hopes of finding some
4665  * statistics.
4666  *
4667  * XXX it's conceivable that there are multiple matches with different
4668  * index opfamilies; if so, we need to pick one that matches the
4669  * operator we are estimating for. FIXME later.
4670  */
4671  ListCell *ilist;
4672 
4673  foreach(ilist, onerel->indexlist)
4674  {
4675  IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
4676  ListCell *indexpr_item;
4677  int pos;
4678 
4679  indexpr_item = list_head(index->indexprs);
4680  if (indexpr_item == NULL)
4681  continue; /* no expressions here... */
4682 
4683  for (pos = 0; pos < index->ncolumns; pos++)
4684  {
4685  if (index->indexkeys[pos] == 0)
4686  {
4687  Node *indexkey;
4688 
4689  if (indexpr_item == NULL)
4690  elog(ERROR, "too few entries in indexprs list");
4691  indexkey = (Node *) lfirst(indexpr_item);
4692  if (indexkey && IsA(indexkey, RelabelType))
4693  indexkey = (Node *) ((RelabelType *) indexkey)->arg;
4694  if (equal(node, indexkey))
4695  {
4696  /*
4697  * Found a match ... is it a unique index? Tests here
4698  * should match has_unique_index().
4699  */
4700  if (index->unique &&
4701  index->ncolumns == 1 &&
4702  (index->indpred == NIL || index->predOK))
4703  vardata->isunique = true;
4704 
4705  /*
4706  * Has it got stats? We only consider stats for
4707  * non-partial indexes, since partial indexes probably
4708  * don't reflect whole-relation statistics; the above
4709  * check for uniqueness is the only info we take from
4710  * a partial index.
4711  *
4712  * An index stats hook, however, must make its own
4713  * decisions about what to do with partial indexes.
4714  */
4715  if (get_index_stats_hook &&
4716  (*get_index_stats_hook) (root, index->indexoid,
4717  pos + 1, vardata))
4718  {
4719  /*
4720  * The hook took control of acquiring a stats
4721  * tuple. If it did supply a tuple, it'd better
4722  * have supplied a freefunc.
4723  */
4724  if (HeapTupleIsValid(vardata->statsTuple) &&
4725  !vardata->freefunc)
4726  elog(ERROR, "no function provided to release variable stats with");
4727  }
4728  else if (index->indpred == NIL)
4729  {
4730  vardata->statsTuple =
4732  ObjectIdGetDatum(index->indexoid),
4733  Int16GetDatum(pos + 1),
4734  BoolGetDatum(false));
4735  vardata->freefunc = ReleaseSysCache;
4736 
4737  if (HeapTupleIsValid(vardata->statsTuple))
4738  {
4739  /* Get index's table for permission check */
4740  RangeTblEntry *rte;
4741 
4742  rte = planner_rt_fetch(index->rel->relid, root);
4743  Assert(rte->rtekind == RTE_RELATION);
4744 
4745  /*
4746  * For simplicity, we insist on the whole
4747  * table being selectable, rather than trying
4748  * to identify which column(s) the index
4749  * depends on.
4750  */
4751  vardata->acl_ok =
4753  ACL_SELECT) == ACLCHECK_OK);
4754  }
4755  else
4756  {
4757  /* suppress leakproofness checks later */
4758  vardata->acl_ok = true;
4759  }
4760  }
4761  if (vardata->statsTuple)
4762  break;
4763  }
4764  indexpr_item = lnext(indexpr_item);
4765  }
4766  }
4767  if (vardata->statsTuple)
4768  break;
4769  }
4770  }
4771 }
#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:284
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:858
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
static void examine_simple_variable(PlannerInfo *root, Var *var, VariableStatData *vardata)
Definition: selfuncs.c:4783
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:1734
#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:676
#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:944
#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 6468 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().

6473 {
6474  IndexOptInfo *index = path->indexinfo;
6475  List *indexQuals = path->indexquals;
6476  List *indexOrderBys = path->indexorderbys;
6477  Cost indexStartupCost;
6478  Cost indexTotalCost;
6479  Selectivity indexSelectivity;
6480  double indexCorrelation;
6481  double numIndexPages;
6482  double numIndexTuples;
6483  double spc_random_page_cost;
6484  double num_sa_scans;
6485  double num_outer_scans;
6486  double num_scans;
6487  double qual_op_cost;
6488  double qual_arg_cost;
6489  List *selectivityQuals;
6490  ListCell *l;
6491 
6492  /*
6493  * If the index is partial, AND the index predicate with the explicitly
6494  * given indexquals to produce a more accurate idea of the index
6495  * selectivity.
6496  */
6497  selectivityQuals = add_predicate_to_quals(index, indexQuals);
6498 
6499  /*
6500  * Check for ScalarArrayOpExpr index quals, and estimate the number of
6501  * index scans that will be performed.
6502  */
6503  num_sa_scans = 1;
6504  foreach(l, indexQuals)
6505  {
6506  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
6507 
6508  if (IsA(rinfo->clause, ScalarArrayOpExpr))
6509  {
6510  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
6511  int alength = estimate_array_length(lsecond(saop->args));
6512 
6513  if (alength > 1)
6514  num_sa_scans *= alength;
6515  }
6516  }
6517 
6518  /* Estimate the fraction of main-table tuples that will be visited */
6519  indexSelectivity = clauselist_selectivity(root, selectivityQuals,
6520  index->rel->relid,
6521  JOIN_INNER,
6522  NULL);
6523 
6524  /*
6525  * If caller didn't give us an estimate, estimate the number of index
6526  * tuples that will be visited. We do it in this rather peculiar-looking
6527  * way in order to get the right answer for partial indexes.
6528  */
6529  numIndexTuples = costs->numIndexTuples;
6530  if (numIndexTuples <= 0.0)
6531  {
6532  numIndexTuples = indexSelectivity * index->rel->tuples;
6533 
6534  /*
6535  * The above calculation counts all the tuples visited across all
6536  * scans induced by ScalarArrayOpExpr nodes. We want to consider the
6537  * average per-indexscan number, so adjust. This is a handy place to
6538  * round to integer, too. (If caller supplied tuple estimate, it's
6539  * responsible for handling these considerations.)
6540  */
6541  numIndexTuples = rint(numIndexTuples / num_sa_scans);
6542  }
6543 
6544  /*
6545  * We can bound the number of tuples by the index size in any case. Also,
6546  * always estimate at least one tuple is touched, even when
6547  * indexSelectivity estimate is tiny.
6548  */
6549  if (numIndexTuples > index->tuples)
6550  numIndexTuples = index->tuples;
6551  if (numIndexTuples < 1.0)
6552  numIndexTuples = 1.0;
6553 
6554  /*
6555  * Estimate the number of index pages that will be retrieved.
6556  *
6557  * We use the simplistic method of taking a pro-rata fraction of the total
6558  * number of index pages. In effect, this counts only leaf pages and not
6559  * any overhead such as index metapage or upper tree levels.
6560  *
6561  * In practice access to upper index levels is often nearly free because
6562  * those tend to stay in cache under load; moreover, the cost involved is
6563  * highly dependent on index type. We therefore ignore such costs here
6564  * and leave it to the caller to add a suitable charge if needed.
6565  */
6566  if (index->pages > 1 && index->tuples > 1)
6567  numIndexPages = ceil(numIndexTuples * index->pages / index->tuples);
6568  else
6569  numIndexPages = 1.0;
6570 
6571  /* fetch estimated page cost for tablespace containing index */
6573  &spc_random_page_cost,
6574  NULL);
6575 
6576  /*
6577  * Now compute the disk access costs.
6578  *
6579  * The above calculations are all per-index-scan. However, if we are in a
6580  * nestloop inner scan, we can expect the scan to be repeated (with
6581  * different search keys) for each row of the outer relation. Likewise,
6582  * ScalarArrayOpExpr quals result in multiple index scans. This creates
6583  * the potential for cache effects to reduce the number of disk page
6584  * fetches needed. We want to estimate the average per-scan I/O cost in
6585  * the presence of caching.
6586  *
6587  * We use the Mackert-Lohman formula (see costsize.c for details) to
6588  * estimate the total number of page fetches that occur. While this
6589  * wasn't what it was designed for, it seems a reasonable model anyway.
6590  * Note that we are counting pages not tuples anymore, so we take N = T =
6591  * index size, as if there were one "tuple" per page.
6592  */
6593  num_outer_scans = loop_count;
6594  num_scans = num_sa_scans * num_outer_scans;
6595 
6596  if (num_scans > 1)
6597  {
6598  double pages_fetched;
6599 
6600  /* total page fetches ignoring cache effects */
6601  pages_fetched = numIndexPages * num_scans;
6602 
6603  /* use Mackert and Lohman formula to adjust for cache effects */
6604  pages_fetched = index_pages_fetched(pages_fetched,
6605  index->pages,
6606  (double) index->pages,
6607  root);
6608 
6609  /*
6610  * Now compute the total disk access cost, and then report a pro-rated
6611  * share for each outer scan. (Don't pro-rate for ScalarArrayOpExpr,
6612  * since that's internal to the indexscan.)
6613  */
6614  indexTotalCost = (pages_fetched * spc_random_page_cost)
6615  / num_outer_scans;
6616  }
6617  else
6618  {
6619  /*
6620  * For a single index scan, we just charge spc_random_page_cost per
6621  * page touched.
6622  */
6623  indexTotalCost = numIndexPages * spc_random_page_cost;
6624  }
6625 
6626  /*
6627  * CPU cost: any complex expressions in the indexquals will need to be
6628  * evaluated once at the start of the scan to reduce them to runtime keys
6629  * to pass to the index AM (see nodeIndexscan.c). We model the per-tuple
6630  * CPU costs as cpu_index_tuple_cost plus one cpu_operator_cost per
6631  * indexqual operator. Because we have numIndexTuples as a per-scan
6632  * number, we have to multiply by num_sa_scans to get the correct result
6633  * for ScalarArrayOpExpr cases. Similarly add in costs for any index
6634  * ORDER BY expressions.
6635  *
6636  * Note: this neglects the possible costs of rechecking lossy operators.
6637  * Detecting that that might be needed seems more expensive than it's
6638  * worth, though, considering all the other inaccuracies here ...
6639  */
6640  qual_arg_cost = other_operands_eval_cost(root, qinfos) +
6641  orderby_operands_eval_cost(root, path);
6642  qual_op_cost = cpu_operator_cost *
6643  (list_length(indexQuals) + list_length(indexOrderBys));
6644 
6645  indexStartupCost = qual_arg_cost;
6646  indexTotalCost += qual_arg_cost;
6647  indexTotalCost += numIndexTuples * num_sa_scans * (cpu_index_tuple_cost + qual_op_cost);
6648 
6649  /*
6650  * Generic assumption about index correlation: there isn't any.
6651  */
6652  indexCorrelation = 0.0;
6653 
6654  /*
6655  * Return everything to caller.
6656  */
6657  costs->indexStartupCost = indexStartupCost;
6658  costs->indexTotalCost = indexTotalCost;
6659  costs->indexSelectivity = indexSelectivity;
6660  costs->indexCorrelation = indexCorrelation;
6661  costs->numIndexPages = numIndexPages;
6662  costs->numIndexTuples = numIndexTuples;
6663  costs->spc_random_page_cost = spc_random_page_cost;
6664  costs->num_sa_scans = num_sa_scans;
6665 }
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:6687
Oid reltablespace
Definition: relation.h:632
static Cost other_operands_eval_cost(PlannerInfo *root, List *qinfos)
Definition: selfuncs.c:6414
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:6439
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 4510 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().

4513 {
4514  Node *left,
4515  *right;
4516 
4517  if (list_length(args) != 2)
4518  elog(ERROR, "join operator should take two arguments");
4519 
4520  left = (Node *) linitial(args);
4521  right = (Node *) lsecond(args);
4522 
4523  examine_variable(root, left, 0, vardata1);
4524  examine_variable(root, right, 0, vardata2);
4525 
4526  if (vardata1->rel &&
4527  bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
4528  *join_is_reversed = true; /* var1 is on RHS */
4529  else if (vardata2->rel &&
4530  bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
4531  *join_is_reversed = true; /* var2 is on LHS */
4532  else
4533  *join_is_reversed = false;
4534 }
RelOptInfo * rel
Definition: selfuncs.h:70
Definition: nodes.h:509
#define lsecond(l)
Definition: pg_list.h:116
Relids syn_lefthand
Definition: relation.h:1921
Relids syn_righthand
Definition: relation.h:1922
#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:4572
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 4450 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().

4453 {
4454  Node *left,
4455  *right;
4456  VariableStatData rdata;
4457 
4458  /* Fail if not a binary opclause (probably shouldn't happen) */
4459  if (list_length(args) != 2)
4460  return false;
4461 
4462  left = (Node *) linitial(args);
4463  right = (Node *) lsecond(args);
4464 
4465  /*
4466  * Examine both sides. Note that when varRelid is nonzero, Vars of other
4467  * relations will be treated as pseudoconstants.
4468  */
4469  examine_variable(root, left, varRelid, vardata);
4470  examine_variable(root, right, varRelid, &rdata);
4471 
4472  /*
4473  * If one side is a variable and the other not, we win.
4474  */
4475  if (vardata->rel && rdata.rel == NULL)
4476  {
4477  *varonleft = true;
4478  *other = estimate_expression_value(root, rdata.var);
4479  /* Assume we need no ReleaseVariableStats(rdata) here */
4480  return true;
4481  }
4482 
4483  if (vardata->rel == NULL && rdata.rel)
4484  {
4485  *varonleft = false;
4486  *other = estimate_expression_value(root, vardata->var);
4487  /* Assume we need no ReleaseVariableStats(*vardata) here */
4488  *vardata = rdata;
4489  return true;
4490  }
4491 
4492  /* Oops, clause has wrong structure (probably var op var) */
4493  ReleaseVariableStats(*vardata);
4494  ReleaseVariableStats(rdata);
4495 
4496  return false;
4497 }
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2454
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:4572
#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 4979 of file selfuncs.c.

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

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

4980 {
4981  double stadistinct;
4982  double stanullfrac = 0.0;
4983  double ntuples;
4984 
4985  *isdefault = false;
4986 
4987  /*
4988  * Determine the stadistinct value to use. There are cases where we can
4989  * get an estimate even without a pg_statistic entry, or can get a better
4990  * value than is in pg_statistic. Grab stanullfrac too if we can find it
4991  * (otherwise, assume no nulls, for lack of any better idea).
4992  */
4993  if (HeapTupleIsValid(vardata->statsTuple))
4994  {
4995  /* Use the pg_statistic entry */
4996  Form_pg_statistic stats;
4997 
4998  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
4999  stadistinct = stats->stadistinct;
5000  stanullfrac = stats->stanullfrac;
5001  }
5002  else if (vardata->vartype == BOOLOID)
5003  {
5004  /*
5005  * Special-case boolean columns: presumably, two distinct values.
5006  *
5007  * Are there any other datatypes we should wire in special estimates
5008  * for?
5009  */
5010  stadistinct = 2.0;
5011  }
5012  else if (vardata->rel && vardata->rel->rtekind == RTE_VALUES)
5013  {
5014  /*
5015  * If the Var represents a column of a VALUES RTE, assume it's unique.
5016  * This could of course be very wrong, but it should tend to be true
5017  * in well-written queries. We could consider examining the VALUES'
5018  * contents to get some real statistics; but that only works if the
5019  * entries are all constants, and it would be pretty expensive anyway.
5020  */
5021  stadistinct = -1.0; /* unique (and all non null) */
5022  }
5023  else
5024  {
5025  /*
5026  * We don't keep statistics for system columns, but in some cases we
5027  * can infer distinctness anyway.
5028  */
5029  if (vardata->var && IsA(vardata->var, Var))
5030  {
5031  switch (((Var *) vardata->var)->varattno)
5032  {
5035  stadistinct = -1.0; /* unique (and all non null) */
5036  break;
5038  stadistinct = 1.0; /* only 1 value */
5039  break;
5040  default:
5041  stadistinct = 0.0; /* means "unknown" */
5042  break;
5043  }
5044  }
5045  else
5046  stadistinct = 0.0; /* means "unknown" */
5047 
5048  /*
5049  * XXX consider using estimate_num_groups on expressions?
5050  */
5051  }
5052 
5053  /*
5054  * If there is a unique index or DISTINCT clause for the variable, assume
5055  * it is unique no matter what pg_statistic says; the statistics could be
5056  * out of date, or we might have found a partial unique index that proves
5057  * the var is unique for this query. However, we'd better still believe
5058  * the null-fraction statistic.
5059  */
5060  if (vardata->isunique)
5061  stadistinct = -1.0 * (1.0 - stanullfrac);
5062 
5063  /*
5064  * If we had an absolute estimate, use that.
5065  */
5066  if (stadistinct > 0.0)
5067  return clamp_row_est(stadistinct);
5068 
5069  /*
5070  * Otherwise we need to get the relation size; punt if not available.
5071  */
5072  if (vardata->rel == NULL)
5073  {
5074  *isdefault = true;
5075  return DEFAULT_NUM_DISTINCT;
5076  }
5077  ntuples = vardata->rel->tuples;
5078  if (ntuples <= 0.0)
5079  {
5080  *isdefault = true;
5081  return DEFAULT_NUM_DISTINCT;
5082  }
5083 
5084  /*
5085  * If we had a relative estimate, use that.
5086  */
5087  if (stadistinct < 0.0)
5088  return clamp_row_est(-stadistinct * ntuples);
5089 
5090  /*
5091  * With no data, estimate ndistinct = ntuples if the table is small, else
5092  * use default. We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
5093  * that the behavior isn't discontinuous.
5094  */
5095  if (ntuples < DEFAULT_NUM_DISTINCT)
5096  return clamp_row_est(ntuples);
5097 
5098  *isdefault = true;
5099  return DEFAULT_NUM_DISTINCT;
5100 }
#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
RTEKind rtekind
Definition: relation.h:555
#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:4950
return result
Definition: formatting.c:1633
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:676
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 6093 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().

6094 {
6095  Oid datatype = str_const->consttype;
6096  char *workstr;
6097  int len;
6098  Datum cmpstr;
6099  text *cmptxt = NULL;
6100  mbcharacter_incrementer charinc;
6101 
6102  /*
6103  * Get a modifiable copy of the prefix string in C-string format, and set
6104  * up the string we will compare to as a Datum. In C locale this can just
6105  * be the given prefix string, otherwise we need to add a suffix. Types
6106  * NAME and BYTEA sort bytewise so they don't need a suffix either.
6107  */
6108  if (datatype == NAMEOID)
6109  {
6111  str_const->constvalue));
6112  len = strlen(workstr);
6113  cmpstr = str_const->constvalue;
6114  }
6115  else if (datatype == BYTEAOID)
6116  {
6117  bytea *bstr = DatumGetByteaPP(str_const->constvalue);
6118 
6119  len = VARSIZE_ANY_EXHDR(bstr);
6120  workstr = (char *) palloc(len);
6121  memcpy(workstr, VARDATA_ANY(bstr), len);
6122  Assert((Pointer) bstr == DatumGetPointer(str_const->constvalue));
6123  cmpstr = str_const->constvalue;
6124  }
6125  else
6126  {
6127  workstr = TextDatumGetCString(str_const->constvalue);
6128  len = strlen(workstr);
6129  if (lc_collate_is_c(collation) || len == 0)
6130  cmpstr = str_const->constvalue;
6131  else
6132  {
6133  /* If first time through, determine the suffix to use */
6134  static char suffixchar = 0;
6135  static Oid suffixcollation = 0;
6136 
6137  if (!suffixchar || suffixcollation != collation)
6138  {
6139  char *best;
6140 
6141  best = "Z";
6142  if (varstr_cmp(best, 1, "z", 1, collation) < 0)
6143  best = "z";
6144  if (varstr_cmp(best, 1, "y", 1, collation) < 0)
6145  best = "y";
6146  if (varstr_cmp(best, 1, "9", 1, collation) < 0)
6147  best = "9";
6148  suffixchar = *best;
6149  suffixcollation = collation;
6150  }
6151 
6152  /* And build the string to compare to */
6153  cmptxt = (text *) palloc(VARHDRSZ + len + 1);
6154  SET_VARSIZE(cmptxt, VARHDRSZ + len + 1);
6155  memcpy(VARDATA(cmptxt), workstr, len);
6156  *(VARDATA(cmptxt) + len) = suffixchar;
6157  cmpstr = PointerGetDatum(cmptxt);
6158  }
6159  }
6160 
6161  /* Select appropriate character-incrementer function */
6162  if (datatype == BYTEAOID)
6163  charinc = byte_increment;
6164  else
6166 
6167  /* And search ... */
6168  while (len > 0)
6169  {
6170  int charlen;
6171  unsigned char *lastchar;
6172 
6173  /* Identify the last character --- for bytea, just the last byte */
6174  if (datatype == BYTEAOID)
6175  charlen = 1;
6176  else
6177  charlen = len - pg_mbcliplen(workstr, len, len - 1);
6178  lastchar = (unsigned char *) (workstr + len - charlen);
6179 
6180  /*
6181  * Try to generate a larger string by incrementing the last character
6182  * (for BYTEA, we treat each byte as a character).
6183  *
6184  * Note: the incrementer function is expected to return true if it's
6185  * generated a valid-per-the-encoding new character, otherwise false.
6186  * The contents of the character on false return are unspecified.
6187  */
6188  while (charinc(lastchar, charlen))
6189  {
6190  Const *workstr_const;
6191 
6192  if (datatype == BYTEAOID)
6193  workstr_const = string_to_bytea_const(workstr, len);
6194  else
6195  workstr_const = string_to_const(workstr, datatype);
6196 
6197  if (DatumGetBool(FunctionCall2Coll(ltproc,
6198  collation,
6199  cmpstr,
6200  workstr_const->constvalue)))
6201  {
6202  /* Successfully made a string larger than cmpstr */
6203  if (cmptxt)
6204  pfree(cmptxt);
6205  pfree(workstr);
6206  return workstr_const;
6207  }
6208 
6209  /* No good, release unusable value and try again */
6210  pfree(DatumGetPointer(workstr_const->constvalue));
6211  pfree(workstr_const);
6212  }
6213 
6214  /*
6215  * No luck here, so truncate off the last character and try to
6216  * increment the next one.
6217  */
6218  len -= charlen;
6219  workstr[len] = '\0';
6220  }
6221 
6222  /* Failed... */
6223  if (cmptxt)
6224  pfree(cmptxt);
6225  pfree(workstr);
6226 
6227  return NULL;
6228 }
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:6299
#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:6043
static Const * string_to_const(const char *str, Oid datatype)
Definition: selfuncs.c:6256
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:676
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:4950
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:4572
#define Assert(condition)
Definition: c.h:676
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:5112
#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:4572
#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 5729 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().

5731 {
5733 
5734  switch (ptype)
5735  {
5736  case Pattern_Type_Like:
5737  result = like_fixed_prefix(patt, false, collation,
5738  prefix, rest_selec);
5739  break;
5740  case Pattern_Type_Like_IC:
5741  result = like_fixed_prefix(patt, true, collation,
5742  prefix, rest_selec);
5743  break;
5744  case Pattern_Type_Regex:
5745  result = regex_fixed_prefix(patt, false, collation,
5746  prefix, rest_selec);
5747  break;
5748  case Pattern_Type_Regex_IC:
5749  result = regex_fixed_prefix(patt, true, collation,
5750  prefix, rest_selec);
5751  break;
5752  default:
5753  elog(ERROR, "unrecognized ptype: %d", (int) ptype);
5754  result = Pattern_Prefix_None; /* keep compiler quiet */
5755  break;
5756  }
5757  return result;
5758 }
static Pattern_Prefix_Status regex_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
Definition: selfuncs.c:5661
static Pattern_Prefix_Status like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
Definition: selfuncs.c:5553
return result
Definition: formatting.c:1633
#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:1653
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:1690
List * inputcollids
Definition: primnodes.h:1034
Definition: pg_list.h:45
int NumRelids(Node *clause)
Definition: clauses.c:2238
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:2454
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:184
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:676
#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:4950
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:184
#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:4572
#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 4950 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().

4951 {
4952  if (vardata->acl_ok)
4953  return true;
4954 
4955  if (!OidIsValid(func_oid))
4956  return false;
4957 
4958  if (get_func_leakproof(func_oid))
4959  return true;
4960 
4961  ereport(DEBUG2,
4962  (errmsg_internal("not using statistics because function \"%s\" is not leak-proof",
4963  get_func_name(func_oid))));
4964  return false;
4965 }
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().