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

Variables

PGDLLIMPORT
get_relation_stats_hook_type 
get_relation_stats_hook
 
PGDLLIMPORT
get_index_stats_hook_type 
get_index_stats_hook
 

Macro Definition Documentation

#define DEFAULT_EQ_SEL   0.005

Definition at line 34 of file selfuncs.h.

Referenced by _int_matchsel(), eqsel(), int_query_opr_selec(), neqjoinsel(), and neqsel().

#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 146 of file selfuncs.h.

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

Definition at line 141 of file selfuncs.h.

Enumeration Type Documentation

Enumerator
Pattern_Prefix_None 
Pattern_Prefix_Partial 
Pattern_Prefix_Exact 

Definition at line 93 of file selfuncs.h.

Enumerator
Pattern_Type_Like 
Pattern_Type_Like_IC 
Pattern_Type_Regex 
Pattern_Type_Regex_IC 

Definition at line 87 of file selfuncs.h.

Function Documentation

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

Definition at line 1499 of file selfuncs.c.

References VariableStatData::atttype, VariableStatData::atttypmod, 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, NULL, ReleaseVariableStats, STATISTIC_KIND_MCV, VariableStatData::statsTuple, and values.

Referenced by clause_selectivity().

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

Definition at line 1460 of file selfuncs.c.

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

Referenced by clause_selectivity().

1461 {
1462  VariableStatData vardata;
1463  double selec;
1464 
1465  examine_variable(root, arg, varRelid, &vardata);
1466  if (HeapTupleIsValid(vardata.statsTuple))
1467  {
1468  /*
1469  * A boolean variable V is equivalent to the clause V = 't', so we
1470  * compute the selectivity as if that is what we have.
1471  */
1472  selec = var_eq_const(&vardata, BooleanEqualOperator,
1473  BoolGetDatum(true), false, true);
1474  }
1475  else if (is_funcclause(arg))
1476  {
1477  /*
1478  * If we have no stats and it's a function call, estimate 0.3333333.
1479  * This seems a pretty unprincipled choice, but Postgres has been
1480  * using that estimate for function calls since 1992. The hoariness
1481  * of this behavior suggests that we should not be in too much hurry
1482  * to use another value.
1483  */
1484  selec = 0.3333333;
1485  }
1486  else
1487  {
1488  /* Otherwise, the default estimate is 0.5 */
1489  selec = 0.5;
1490  }
1491  ReleaseVariableStats(vardata);
1492  return selec;
1493 }
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)
Definition: selfuncs.c:269
#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:4564
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:80
List* deconstruct_indexquals ( IndexPath path)

Definition at line 6236 of file selfuncs.c.

References arg, ScalarArrayOpExpr::args, Assert, castNode, 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, lfirst_int, 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().

6237 {
6238  List *result = NIL;
6239  IndexOptInfo *index = path->indexinfo;
6240  ListCell *lcc,
6241  *lci;
6242 
6243  forboth(lcc, path->indexquals, lci, path->indexqualcols)
6244  {
6245  RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(lcc));
6246  int indexcol = lfirst_int(lci);
6247  Expr *clause;
6248  Node *leftop,
6249  *rightop;
6250  IndexQualInfo *qinfo;
6251 
6252  clause = rinfo->clause;
6253 
6254  qinfo = (IndexQualInfo *) palloc(sizeof(IndexQualInfo));
6255  qinfo->rinfo = rinfo;
6256  qinfo->indexcol = indexcol;
6257 
6258  if (IsA(clause, OpExpr))
6259  {
6260  qinfo->clause_op = ((OpExpr *) clause)->opno;
6261  leftop = get_leftop(clause);
6262  rightop = get_rightop(clause);
6263  if (match_index_to_operand(leftop, indexcol, index))
6264  {
6265  qinfo->varonleft = true;
6266  qinfo->other_operand = rightop;
6267  }
6268  else
6269  {
6270  Assert(match_index_to_operand(rightop, indexcol, index));
6271  qinfo->varonleft = false;
6272  qinfo->other_operand = leftop;
6273  }
6274  }
6275  else if (IsA(clause, RowCompareExpr))
6276  {
6277  RowCompareExpr *rc = (RowCompareExpr *) clause;
6278 
6279  qinfo->clause_op = linitial_oid(rc->opnos);
6280  /* Examine only first columns to determine left/right sides */
6282  indexcol, index))
6283  {
6284  qinfo->varonleft = true;
6285  qinfo->other_operand = (Node *) rc->rargs;
6286  }
6287  else
6288  {
6290  indexcol, index));
6291  qinfo->varonleft = false;
6292  qinfo->other_operand = (Node *) rc->largs;
6293  }
6294  }
6295  else if (IsA(clause, ScalarArrayOpExpr))
6296  {
6297  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
6298 
6299  qinfo->clause_op = saop->opno;
6300  /* index column is always on the left in this case */
6302  indexcol, index));
6303  qinfo->varonleft = true;
6304  qinfo->other_operand = (Node *) lsecond(saop->args);
6305  }
6306  else if (IsA(clause, NullTest))
6307  {
6308  qinfo->clause_op = InvalidOid;
6309  Assert(match_index_to_operand((Node *) ((NullTest *) clause)->arg,
6310  indexcol, index));
6311  qinfo->varonleft = true;
6312  qinfo->other_operand = NULL;
6313  }
6314  else
6315  {
6316  elog(ERROR, "unsupported indexqual type: %d",
6317  (int) nodeTag(clause));
6318  }
6319 
6320  result = lappend(result, qinfo);
6321  }
6322  return result;
6323 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
IndexOptInfo * indexinfo
Definition: relation.h:995
#define castNode(_type_, nodeptr)
Definition: nodes.h:575
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3176
Definition: nodes.h:506
return result
Definition: formatting.c:1618
RestrictInfo * rinfo
Definition: selfuncs.h:105
#define lsecond(l)
Definition: pg_list.h:114
Definition: type.h:90
List * indexquals
Definition: relation.h:997
#define linitial(l)
Definition: pg_list.h:110
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1699
bool varonleft
Definition: selfuncs.h:107
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
#define linitial_oid(l)
Definition: pg_list.h:112
#define nodeTag(nodeptr)
Definition: nodes.h:511
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
List * indexqualcols
Definition: relation.h:998
void * palloc(Size size)
Definition: mcxt.c:849
Node * other_operand
Definition: selfuncs.h:109
void * arg
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
int estimate_array_length ( Node arrayexpr)

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

2084 {
2085  /* look through any binary-compatible relabeling of arrayexpr */
2086  arrayexpr = strip_array_coercion(arrayexpr);
2087 
2088  if (arrayexpr && IsA(arrayexpr, Const))
2089  {
2090  Datum arraydatum = ((Const *) arrayexpr)->constvalue;
2091  bool arrayisnull = ((Const *) arrayexpr)->constisnull;
2092  ArrayType *arrayval;
2093 
2094  if (arrayisnull)
2095  return 0;
2096  arrayval = DatumGetArrayTypeP(arraydatum);
2097  return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
2098  }
2099  else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
2100  !((ArrayExpr *) arrayexpr)->multidims)
2101  {
2102  return list_length(((ArrayExpr *) arrayexpr)->elements);
2103  }
2104  else
2105  {
2106  /* default guess --- see also scalararraysel */
2107  return 10;
2108  }
2109 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
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:1740
#define DatumGetArrayTypeP(X)
Definition: array.h:242
Selectivity estimate_hash_bucketsize ( PlannerInfo root,
Node hashkey,
double  nbuckets 
)

Definition at line 3600 of file selfuncs.c.

References VariableStatData::atttype, VariableStatData::atttypmod, clamp_row_est(), examine_variable(), free_attstatsslot(), get_attstatsslot(), get_variable_numdistinct(), GETSTRUCT, HeapTupleIsValid, InvalidOid, NULL, VariableStatData::rel, ReleaseVariableStats, RelOptInfo::rows, STATISTIC_KIND_MCV, VariableStatData::statsTuple, and RelOptInfo::tuples.

Referenced by final_cost_hashjoin().

3601 {
3602  VariableStatData vardata;
3603  double estfract,
3604  ndistinct,
3605  stanullfrac,
3606  mcvfreq,
3607  avgfreq;
3608  bool isdefault;
3609  float4 *numbers;
3610  int nnumbers;
3611 
3612  examine_variable(root, hashkey, 0, &vardata);
3613 
3614  /* Get number of distinct values */
3615  ndistinct = get_variable_numdistinct(&vardata, &isdefault);
3616 
3617  /* If ndistinct isn't real, punt and return 0.1, per comments above */
3618  if (isdefault)
3619  {
3620  ReleaseVariableStats(vardata);
3621  return (Selectivity) 0.1;
3622  }
3623 
3624  /* Get fraction that are null */
3625  if (HeapTupleIsValid(vardata.statsTuple))
3626  {
3627  Form_pg_statistic stats;
3628 
3629  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
3630  stanullfrac = stats->stanullfrac;
3631  }
3632  else
3633  stanullfrac = 0.0;
3634 
3635  /* Compute avg freq of all distinct data values in raw relation */
3636  avgfreq = (1.0 - stanullfrac) / ndistinct;
3637 
3638  /*
3639  * Adjust ndistinct to account for restriction clauses. Observe we are
3640  * assuming that the data distribution is affected uniformly by the
3641  * restriction clauses!
3642  *
3643  * XXX Possibly better way, but much more expensive: multiply by
3644  * selectivity of rel's restriction clauses that mention the target Var.
3645  */
3646  if (vardata.rel && vardata.rel->tuples > 0)
3647  {
3648  ndistinct *= vardata.rel->rows / vardata.rel->tuples;
3649  ndistinct = clamp_row_est(ndistinct);
3650  }
3651 
3652  /*
3653  * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
3654  * number of buckets is less than the expected number of distinct values;
3655  * otherwise it is 1/ndistinct.
3656  */
3657  if (ndistinct > nbuckets)
3658  estfract = 1.0 / nbuckets;
3659  else
3660  estfract = 1.0 / ndistinct;
3661 
3662  /*
3663  * Look up the frequency of the most common value, if available.
3664  */
3665  mcvfreq = 0.0;
3666 
3667  if (HeapTupleIsValid(vardata.statsTuple))
3668  {
3669  if (get_attstatsslot(vardata.statsTuple,
3670  vardata.atttype, vardata.atttypmod,
3672  NULL,
3673  NULL, NULL,
3674  &numbers, &nnumbers))
3675  {
3676  /*
3677  * The first MCV stat is for the most common value.
3678  */
3679  if (nnumbers > 0)
3680  mcvfreq = numbers[0];
3681  free_attstatsslot(vardata.atttype, NULL, 0,
3682  numbers, nnumbers);
3683  }
3684  }
3685 
3686  /*
3687  * Adjust estimated bucketsize upward to account for skewed distribution.
3688  */
3689  if (avgfreq > 0.0 && mcvfreq > avgfreq)
3690  estfract *= mcvfreq / avgfreq;
3691 
3692  /*
3693  * Clamp bucketsize to sane range (the above adjustment could easily
3694  * produce an out-of-range result). We set the lower bound a little above
3695  * zero, since zero isn't a very sane result.
3696  */
3697  if (estfract < 1.0e-6)
3698  estfract = 1.0e-6;
3699  else if (estfract > 1.0)
3700  estfract = 1.0;
3701 
3702  ReleaseVariableStats(vardata);
3703 
3704  return (Selectivity) estfract;
3705 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
HeapTuple statsTuple
Definition: selfuncs.h:71
double tuples
Definition: relation.h:534
RelOptInfo * rel
Definition: selfuncs.h:70
double Selectivity
Definition: nodes.h:635
bool get_attstatsslot(HeapTuple statstuple, Oid atttype, int32 atttypmod, int reqkind, Oid reqop, Oid *actualop, Datum **values, int *nvalues, float4 **numbers, int *nnumbers)
Definition: lsyscache.c:2854
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
int32 atttypmod
Definition: selfuncs.h:76
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition: selfuncs.c:4908
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:204
float float4
Definition: c.h:380
double rows
Definition: relation.h:497
#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:4564
#define NULL
Definition: c.h:229
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:80
e
Definition: preproc-init.c:82
double clamp_row_est(double nrows)
Definition: costsize.c:173
void free_attstatsslot(Oid atttype, Datum *values, int nvalues, float4 *numbers, int nnumbers)
Definition: lsyscache.c:2978
double estimate_num_groups ( PlannerInfo root,
List groupExprs,
double  input_rows,
List **  pgset 
)

Definition at line 3277 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, 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, RELOPT_BASEREL, RelOptInfo::reloptkind, 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().

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

Definition at line 4564 of file selfuncs.c.

References arg, 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, 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, IndexOptInfo::predOK, pull_varnos(), VariableStatData::rel, ReleaseSysCache(), SearchSysCache3, STATRELATTINH, VariableStatData::statsTuple, IndexOptInfo::unique, VariableStatData::var, Var::varattno, Var::varno, VariableStatData::vartype, Var::vartype, and Var::vartypmod.

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

4566 {
4567  Node *basenode;
4568  Relids varnos;
4569  RelOptInfo *onerel;
4570 
4571  /* Make sure we don't return dangling pointers in vardata */
4572  MemSet(vardata, 0, sizeof(VariableStatData));
4573 
4574  /* Save the exposed type of the expression */
4575  vardata->vartype = exprType(node);
4576 
4577  /* Look inside any binary-compatible relabeling */
4578 
4579  if (IsA(node, RelabelType))
4580  basenode = (Node *) ((RelabelType *) node)->arg;
4581  else
4582  basenode = node;
4583 
4584  /* Fast path for a simple Var */
4585 
4586  if (IsA(basenode, Var) &&
4587  (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
4588  {
4589  Var *var = (Var *) basenode;
4590 
4591  /* Set up result fields other than the stats tuple */
4592  vardata->var = basenode; /* return Var without relabeling */
4593  vardata->rel = find_base_rel(root, var->varno);
4594  vardata->atttype = var->vartype;
4595  vardata->atttypmod = var->vartypmod;
4596  vardata->isunique = has_unique_index(vardata->rel, var->varattno);
4597 
4598  /* Try to locate some stats */
4599  examine_simple_variable(root, var, vardata);
4600 
4601  return;
4602  }
4603 
4604  /*
4605  * Okay, it's a more complicated expression. Determine variable
4606  * membership. Note that when varRelid isn't zero, only vars of that
4607  * relation are considered "real" vars.
4608  */
4609  varnos = pull_varnos(basenode);
4610 
4611  onerel = NULL;
4612 
4613  switch (bms_membership(varnos))
4614  {
4615  case BMS_EMPTY_SET:
4616  /* No Vars at all ... must be pseudo-constant clause */
4617  break;
4618  case BMS_SINGLETON:
4619  if (varRelid == 0 || bms_is_member(varRelid, varnos))
4620  {
4621  onerel = find_base_rel(root,
4622  (varRelid ? varRelid : bms_singleton_member(varnos)));
4623  vardata->rel = onerel;
4624  node = basenode; /* strip any relabeling */
4625  }
4626  /* else treat it as a constant */
4627  break;
4628  case BMS_MULTIPLE:
4629  if (varRelid == 0)
4630  {
4631  /* treat it as a variable of a join relation */
4632  vardata->rel = find_join_rel(root, varnos);
4633  node = basenode; /* strip any relabeling */
4634  }
4635  else if (bms_is_member(varRelid, varnos))
4636  {
4637  /* ignore the vars belonging to other relations */
4638  vardata->rel = find_base_rel(root, varRelid);
4639  node = basenode; /* strip any relabeling */
4640  /* note: no point in expressional-index search here */
4641  }
4642  /* else treat it as a constant */
4643  break;
4644  }
4645 
4646  bms_free(varnos);
4647 
4648  vardata->var = node;
4649  vardata->atttype = exprType(node);
4650  vardata->atttypmod = exprTypmod(node);
4651 
4652  if (onerel)
4653  {
4654  /*
4655  * We have an expression in vars of a single relation. Try to match
4656  * it to expressional index columns, in hopes of finding some
4657  * statistics.
4658  *
4659  * XXX it's conceivable that there are multiple matches with different
4660  * index opfamilies; if so, we need to pick one that matches the
4661  * operator we are estimating for. FIXME later.
4662  */
4663  ListCell *ilist;
4664 
4665  foreach(ilist, onerel->indexlist)
4666  {
4667  IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
4668  ListCell *indexpr_item;
4669  int pos;
4670 
4671  indexpr_item = list_head(index->indexprs);
4672  if (indexpr_item == NULL)
4673  continue; /* no expressions here... */
4674 
4675  for (pos = 0; pos < index->ncolumns; pos++)
4676  {
4677  if (index->indexkeys[pos] == 0)
4678  {
4679  Node *indexkey;
4680 
4681  if (indexpr_item == NULL)
4682  elog(ERROR, "too few entries in indexprs list");
4683  indexkey = (Node *) lfirst(indexpr_item);
4684  if (indexkey && IsA(indexkey, RelabelType))
4685  indexkey = (Node *) ((RelabelType *) indexkey)->arg;
4686  if (equal(node, indexkey))
4687  {
4688  /*
4689  * Found a match ... is it a unique index? Tests here
4690  * should match has_unique_index().
4691  */
4692  if (index->unique &&
4693  index->ncolumns == 1 &&
4694  (index->indpred == NIL || index->predOK))
4695  vardata->isunique = true;
4696 
4697  /*
4698  * Has it got stats? We only consider stats for
4699  * non-partial indexes, since partial indexes probably
4700  * don't reflect whole-relation statistics; the above
4701  * check for uniqueness is the only info we take from
4702  * a partial index.
4703  *
4704  * An index stats hook, however, must make its own
4705  * decisions about what to do with partial indexes.
4706  */
4707  if (get_index_stats_hook &&
4708  (*get_index_stats_hook) (root, index->indexoid,
4709  pos + 1, vardata))
4710  {
4711  /*
4712  * The hook took control of acquiring a stats
4713  * tuple. If it did supply a tuple, it'd better
4714  * have supplied a freefunc.
4715  */
4716  if (HeapTupleIsValid(vardata->statsTuple) &&
4717  !vardata->freefunc)
4718  elog(ERROR, "no function provided to release variable stats with");
4719  }
4720  else if (index->indpred == NIL)
4721  {
4722  vardata->statsTuple =
4724  ObjectIdGetDatum(index->indexoid),
4725  Int16GetDatum(pos + 1),
4726  BoolGetDatum(false));
4727  vardata->freefunc = ReleaseSysCache;
4728  }
4729  if (vardata->statsTuple)
4730  break;
4731  }
4732  indexpr_item = lnext(indexpr_item);
4733  }
4734  }
4735  if (vardata->statsTuple)
4736  break;
4737  }
4738  }
4739 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
bool predOK
Definition: relation.h:625
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:288
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2946
HeapTuple statsTuple
Definition: selfuncs.h:71
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
RelOptInfo * rel
Definition: selfuncs.h:70
#define Int16GetDatum(X)
Definition: postgres.h:457
Definition: nodes.h:506
#define MemSet(start, val, len)
Definition: c.h:857
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
static void examine_simple_variable(PlannerInfo *root, Var *var, VariableStatData *vardata)
Definition: selfuncs.c:4751
int32 atttypmod
Definition: selfuncs.h:76
bool unique
Definition: relation.h:626
Definition: type.h:90
bool has_unique_index(RelOptInfo *rel, AttrNumber attno)
Definition: plancat.c:1716
#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:603
#define lnext(lc)
Definition: pg_list.h:105
Relids pull_varnos(Node *node)
Definition: var.c:95
Index varno
Definition: primnodes.h:166
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:634
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:526
List * indexlist
Definition: relation.h:531
#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 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:152
#define SearchSysCache3(cacheId, key1, key2, key3)
Definition: syscache.h:156
void * arg
int * indexkeys
Definition: relation.h:604
#define elog
Definition: elog.h:219
Oid indexoid
Definition: relation.h:593
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:223
void(* freefunc)(HeapTuple tuple)
Definition: selfuncs.h:73
List * indpred
Definition: relation.h:616
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
List * indexprs
Definition: relation.h:615
int32 vartypmod
Definition: primnodes.h:171
void genericcostestimate ( PlannerInfo root,
IndexPath path,
double  loop_count,
List qinfos,
GenericCosts costs 
)

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

6390 {
6391  IndexOptInfo *index = path->indexinfo;
6392  List *indexQuals = path->indexquals;
6393  List *indexOrderBys = path->indexorderbys;
6394  Cost indexStartupCost;
6395  Cost indexTotalCost;
6396  Selectivity indexSelectivity;
6397  double indexCorrelation;
6398  double numIndexPages;
6399  double numIndexTuples;
6400  double spc_random_page_cost;
6401  double num_sa_scans;
6402  double num_outer_scans;
6403  double num_scans;
6404  double qual_op_cost;
6405  double qual_arg_cost;
6406  List *selectivityQuals;
6407  ListCell *l;
6408 
6409  /*
6410  * If the index is partial, AND the index predicate with the explicitly
6411  * given indexquals to produce a more accurate idea of the index
6412  * selectivity.
6413  */
6414  selectivityQuals = add_predicate_to_quals(index, indexQuals);
6415 
6416  /*
6417  * Check for ScalarArrayOpExpr index quals, and estimate the number of
6418  * index scans that will be performed.
6419  */
6420  num_sa_scans = 1;
6421  foreach(l, indexQuals)
6422  {
6423  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
6424 
6425  if (IsA(rinfo->clause, ScalarArrayOpExpr))
6426  {
6427  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
6428  int alength = estimate_array_length(lsecond(saop->args));
6429 
6430  if (alength > 1)
6431  num_sa_scans *= alength;
6432  }
6433  }
6434 
6435  /* Estimate the fraction of main-table tuples that will be visited */
6436  indexSelectivity = clauselist_selectivity(root, selectivityQuals,
6437  index->rel->relid,
6438  JOIN_INNER,
6439  NULL);
6440 
6441  /*
6442  * If caller didn't give us an estimate, estimate the number of index
6443  * tuples that will be visited. We do it in this rather peculiar-looking
6444  * way in order to get the right answer for partial indexes.
6445  */
6446  numIndexTuples = costs->numIndexTuples;
6447  if (numIndexTuples <= 0.0)
6448  {
6449  numIndexTuples = indexSelectivity * index->rel->tuples;
6450 
6451  /*
6452  * The above calculation counts all the tuples visited across all
6453  * scans induced by ScalarArrayOpExpr nodes. We want to consider the
6454  * average per-indexscan number, so adjust. This is a handy place to
6455  * round to integer, too. (If caller supplied tuple estimate, it's
6456  * responsible for handling these considerations.)
6457  */
6458  numIndexTuples = rint(numIndexTuples / num_sa_scans);
6459  }
6460 
6461  /*
6462  * We can bound the number of tuples by the index size in any case. Also,
6463  * always estimate at least one tuple is touched, even when
6464  * indexSelectivity estimate is tiny.
6465  */
6466  if (numIndexTuples > index->tuples)
6467  numIndexTuples = index->tuples;
6468  if (numIndexTuples < 1.0)
6469  numIndexTuples = 1.0;
6470 
6471  /*
6472  * Estimate the number of index pages that will be retrieved.
6473  *
6474  * We use the simplistic method of taking a pro-rata fraction of the total
6475  * number of index pages. In effect, this counts only leaf pages and not
6476  * any overhead such as index metapage or upper tree levels.
6477  *
6478  * In practice access to upper index levels is often nearly free because
6479  * those tend to stay in cache under load; moreover, the cost involved is
6480  * highly dependent on index type. We therefore ignore such costs here
6481  * and leave it to the caller to add a suitable charge if needed.
6482  */
6483  if (index->pages > 1 && index->tuples > 1)
6484  numIndexPages = ceil(numIndexTuples * index->pages / index->tuples);
6485  else
6486  numIndexPages = 1.0;
6487 
6488  /* fetch estimated page cost for tablespace containing index */
6490  &spc_random_page_cost,
6491  NULL);
6492 
6493  /*
6494  * Now compute the disk access costs.
6495  *
6496  * The above calculations are all per-index-scan. However, if we are in a
6497  * nestloop inner scan, we can expect the scan to be repeated (with
6498  * different search keys) for each row of the outer relation. Likewise,
6499  * ScalarArrayOpExpr quals result in multiple index scans. This creates
6500  * the potential for cache effects to reduce the number of disk page
6501  * fetches needed. We want to estimate the average per-scan I/O cost in
6502  * the presence of caching.
6503  *
6504  * We use the Mackert-Lohman formula (see costsize.c for details) to
6505  * estimate the total number of page fetches that occur. While this
6506  * wasn't what it was designed for, it seems a reasonable model anyway.
6507  * Note that we are counting pages not tuples anymore, so we take N = T =
6508  * index size, as if there were one "tuple" per page.
6509  */
6510  num_outer_scans = loop_count;
6511  num_scans = num_sa_scans * num_outer_scans;
6512 
6513  if (num_scans > 1)
6514  {
6515  double pages_fetched;
6516 
6517  /* total page fetches ignoring cache effects */
6518  pages_fetched = numIndexPages * num_scans;
6519 
6520  /* use Mackert and Lohman formula to adjust for cache effects */
6521  pages_fetched = index_pages_fetched(pages_fetched,
6522  index->pages,
6523  (double) index->pages,
6524  root);
6525 
6526  /*
6527  * Now compute the total disk access cost, and then report a pro-rated
6528  * share for each outer scan. (Don't pro-rate for ScalarArrayOpExpr,
6529  * since that's internal to the indexscan.)
6530  */
6531  indexTotalCost = (pages_fetched * spc_random_page_cost)
6532  / num_outer_scans;
6533  }
6534  else
6535  {
6536  /*
6537  * For a single index scan, we just charge spc_random_page_cost per
6538  * page touched.
6539  */
6540  indexTotalCost = numIndexPages * spc_random_page_cost;
6541  }
6542 
6543  /*
6544  * CPU cost: any complex expressions in the indexquals will need to be
6545  * evaluated once at the start of the scan to reduce them to runtime keys
6546  * to pass to the index AM (see nodeIndexscan.c). We model the per-tuple
6547  * CPU costs as cpu_index_tuple_cost plus one cpu_operator_cost per
6548  * indexqual operator. Because we have numIndexTuples as a per-scan
6549  * number, we have to multiply by num_sa_scans to get the correct result
6550  * for ScalarArrayOpExpr cases. Similarly add in costs for any index
6551  * ORDER BY expressions.
6552  *
6553  * Note: this neglects the possible costs of rechecking lossy operators.
6554  * Detecting that that might be needed seems more expensive than it's
6555  * worth, though, considering all the other inaccuracies here ...
6556  */
6557  qual_arg_cost = other_operands_eval_cost(root, qinfos) +
6558  orderby_operands_eval_cost(root, path);
6559  qual_op_cost = cpu_operator_cost *
6560  (list_length(indexQuals) + list_length(indexOrderBys));
6561 
6562  indexStartupCost = qual_arg_cost;
6563  indexTotalCost += qual_arg_cost;
6564  indexTotalCost += numIndexTuples * num_sa_scans * (cpu_index_tuple_cost + qual_op_cost);
6565 
6566  /*
6567  * Generic assumption about index correlation: there isn't any.
6568  */
6569  indexCorrelation = 0.0;
6570 
6571  /*
6572  * Return everything to caller.
6573  */
6574  costs->indexStartupCost = indexStartupCost;
6575  costs->indexTotalCost = indexTotalCost;
6576  costs->indexSelectivity = indexSelectivity;
6577  costs->indexCorrelation = indexCorrelation;
6578  costs->numIndexPages = numIndexPages;
6579  costs->numIndexTuples = numIndexTuples;
6580  costs->spc_random_page_cost = spc_random_page_cost;
6581  costs->num_sa_scans = num_sa_scans;
6582 }
Selectivity indexSelectivity
Definition: selfuncs.h:130
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
IndexOptInfo * indexinfo
Definition: relation.h:995
double tuples
Definition: relation.h:534
static List * add_predicate_to_quals(IndexOptInfo *index, List *indexQuals)
Definition: selfuncs.c:6604
Oid reltablespace
Definition: relation.h:594
static Cost other_operands_eval_cost(PlannerInfo *root, List *qinfos)
Definition: selfuncs.c:6331
double Selectivity
Definition: nodes.h:635
double tuples
Definition: relation.h:599
#define lsecond(l)
Definition: pg_list.h:114
static Cost orderby_operands_eval_cost(PlannerInfo *root, IndexPath *path)
Definition: selfuncs.c:6356
Definition: type.h:90
BlockNumber pages
Definition: relation.h:598
List * indexquals
Definition: relation.h:997
int estimate_array_length(Node *arrayexpr)
Definition: selfuncs.c:2083
RelOptInfo * rel
Definition: relation.h:595
double num_sa_scans
Definition: selfuncs.h:137
double cpu_operator_cost
Definition: costsize.c:108
Cost indexTotalCost
Definition: selfuncs.h:129
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:522
Expr * clause
Definition: relation.h:1699
double indexCorrelation
Definition: selfuncs.h:131
List * indexorderbys
Definition: relation.h:999
double spc_random_page_cost
Definition: selfuncs.h:136
double numIndexTuples
Definition: selfuncs.h:135
#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:128
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:92
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:636
double numIndexPages
Definition: selfuncs.h:134
void get_join_variables ( PlannerInfo root,
List args,
SpecialJoinInfo sjinfo,
VariableStatData vardata1,
VariableStatData vardata2,
bool join_is_reversed 
)

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

4508 {
4509  Node *left,
4510  *right;
4511 
4512  if (list_length(args) != 2)
4513  elog(ERROR, "join operator should take two arguments");
4514 
4515  left = (Node *) linitial(args);
4516  right = (Node *) lsecond(args);
4517 
4518  examine_variable(root, left, 0, vardata1);
4519  examine_variable(root, right, 0, vardata2);
4520 
4521  if (vardata1->rel &&
4522  bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
4523  *join_is_reversed = true; /* var1 is on RHS */
4524  else if (vardata2->rel &&
4525  bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
4526  *join_is_reversed = true; /* var2 is on LHS */
4527  else
4528  *join_is_reversed = false;
4529 }
RelOptInfo * rel
Definition: selfuncs.h:70
Definition: nodes.h:506
#define lsecond(l)
Definition: pg_list.h:114
Relids syn_lefthand
Definition: relation.h:1871
Relids syn_righthand
Definition: relation.h:1872
#define linitial(l)
Definition: pg_list.h:110
#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:494
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4564
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 4445 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(), ltreeparentsel(), networksel(), patternsel(), rangesel(), scalargtsel(), scalarltsel(), and tsmatchsel().

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

Definition at line 4908 of file selfuncs.c.

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

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

4909 {
4910  double stadistinct;
4911  double stanullfrac = 0.0;
4912  double ntuples;
4913 
4914  *isdefault = false;
4915 
4916  /*
4917  * Determine the stadistinct value to use. There are cases where we can
4918  * get an estimate even without a pg_statistic entry, or can get a better
4919  * value than is in pg_statistic. Grab stanullfrac too if we can find it
4920  * (otherwise, assume no nulls, for lack of any better idea).
4921  */
4922  if (HeapTupleIsValid(vardata->statsTuple))
4923  {
4924  /* Use the pg_statistic entry */
4925  Form_pg_statistic stats;
4926 
4927  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
4928  stadistinct = stats->stadistinct;
4929  stanullfrac = stats->stanullfrac;
4930  }
4931  else if (vardata->vartype == BOOLOID)
4932  {
4933  /*
4934  * Special-case boolean columns: presumably, two distinct values.
4935  *
4936  * Are there any other datatypes we should wire in special estimates
4937  * for?
4938  */
4939  stadistinct = 2.0;
4940  }
4941  else
4942  {
4943  /*
4944  * We don't keep statistics for system columns, but in some cases we
4945  * can infer distinctness anyway.
4946  */
4947  if (vardata->var && IsA(vardata->var, Var))
4948  {
4949  switch (((Var *) vardata->var)->varattno)
4950  {
4953  stadistinct = -1.0; /* unique (and all non null) */
4954  break;
4956  stadistinct = 1.0; /* only 1 value */
4957  break;
4958  default:
4959  stadistinct = 0.0; /* means "unknown" */
4960  break;
4961  }
4962  }
4963  else
4964  stadistinct = 0.0; /* means "unknown" */
4965 
4966  /*
4967  * XXX consider using estimate_num_groups on expressions?
4968  */
4969  }
4970 
4971  /*
4972  * If there is a unique index or DISTINCT clause for the variable, assume
4973  * it is unique no matter what pg_statistic says; the statistics could be
4974  * out of date, or we might have found a partial unique index that proves
4975  * the var is unique for this query. However, we'd better still believe
4976  * the null-fraction statistic.
4977  */
4978  if (vardata->isunique)
4979  stadistinct = -1.0 * (1.0 - stanullfrac);
4980 
4981  /*
4982  * If we had an absolute estimate, use that.
4983  */
4984  if (stadistinct > 0.0)
4985  return clamp_row_est(stadistinct);
4986 
4987  /*
4988  * Otherwise we need to get the relation size; punt if not available.
4989  */
4990  if (vardata->rel == NULL)
4991  {
4992  *isdefault = true;
4993  return DEFAULT_NUM_DISTINCT;
4994  }
4995  ntuples = vardata->rel->tuples;
4996  if (ntuples <= 0.0)
4997  {
4998  *isdefault = true;
4999  return DEFAULT_NUM_DISTINCT;
5000  }
5001 
5002  /*
5003  * If we had a relative estimate, use that.
5004  */
5005  if (stadistinct < 0.0)
5006  return clamp_row_est(-stadistinct * ntuples);
5007 
5008  /*
5009  * With no data, estimate ndistinct = ntuples if the table is small, else
5010  * use default. We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
5011  * that the behavior isn't discontinuous.
5012  */
5013  if (ntuples < DEFAULT_NUM_DISTINCT)
5014  return clamp_row_est(ntuples);
5015 
5016  *isdefault = true;
5017  return DEFAULT_NUM_DISTINCT;
5018 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
#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:534
RelOptInfo * rel
Definition: selfuncs.h:70
Definition: primnodes.h:163
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define TableOidAttributeNumber
Definition: sysattr.h:27
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define BOOLOID
Definition: pg_type.h:288
#define DEFAULT_NUM_DISTINCT
Definition: selfuncs.h:46
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
double clamp_row_est(double nrows)
Definition: costsize.c:173
double histogram_selectivity ( VariableStatData vardata,
FmgrInfo opproc,
Datum  constval,
bool  varonleft,
int  min_hist_size,
int  n_skip,
int *  hist_size 
)

Definition at line 689 of file selfuncs.c.

References Assert, VariableStatData::atttype, VariableStatData::atttypmod, DatumGetBool, DEFAULT_COLLATION_OID, free_attstatsslot(), FunctionCall2Coll(), get_attstatsslot(), HeapTupleIsValid, i, InvalidOid, NULL, result, STATISTIC_KIND_HISTOGRAM, VariableStatData::statsTuple, and values.

Referenced by ltreeparentsel(), and patternsel().

693 {
694  double result;
695  Datum *values;
696  int nvalues;
697 
698  /* check sanity of parameters */
699  Assert(n_skip >= 0);
700  Assert(min_hist_size > 2 * n_skip);
701 
702  if (HeapTupleIsValid(vardata->statsTuple) &&
703  get_attstatsslot(vardata->statsTuple,
704  vardata->atttype, vardata->atttypmod,
706  NULL,
707  &values, &nvalues,
708  NULL, NULL))
709  {
710  *hist_size = nvalues;
711  if (nvalues >= min_hist_size)
712  {
713  int nmatch = 0;
714  int i;
715 
716  for (i = n_skip; i < nvalues - n_skip; i++)
717  {
718  if (varonleft ?
721  values[i],
722  constval)) :
725  constval,
726  values[i])))
727  nmatch++;
728  }
729  result = ((double) nmatch) / ((double) (nvalues - 2 * n_skip));
730  }
731  else
732  result = -1;
733  free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
734  }
735  else
736  {
737  *hist_size = 0;
738  result = -1;
739  }
740 
741  return result;
742 }
HeapTuple statsTuple
Definition: selfuncs.h:71
#define STATISTIC_KIND_HISTOGRAM
Definition: pg_statistic.h:222
return result
Definition: formatting.c:1618
bool get_attstatsslot(HeapTuple statstuple, Oid atttype, int32 atttypmod, int reqkind, Oid reqop, Oid *actualop, Datum **values, int *nvalues, float4 **numbers, int *nnumbers)
Definition: lsyscache.c:2854
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
int32 atttypmod
Definition: selfuncs.h:76
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
#define DatumGetBool(X)
Definition: postgres.h:399
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int i
void free_attstatsslot(Oid atttype, Datum *values, int nvalues, float4 *numbers, int nnumbers)
Definition: lsyscache.c:2978
Const* make_greater_string ( const Const str_const,
FmgrInfo ltproc,
Oid  collation 
)

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

6011 {
6012  Oid datatype = str_const->consttype;
6013  char *workstr;
6014  int len;
6015  Datum cmpstr;
6016  text *cmptxt = NULL;
6017  mbcharacter_incrementer charinc;
6018 
6019  /*
6020  * Get a modifiable copy of the prefix string in C-string format, and set
6021  * up the string we will compare to as a Datum. In C locale this can just
6022  * be the given prefix string, otherwise we need to add a suffix. Types
6023  * NAME and BYTEA sort bytewise so they don't need a suffix either.
6024  */
6025  if (datatype == NAMEOID)
6026  {
6028  str_const->constvalue));
6029  len = strlen(workstr);
6030  cmpstr = str_const->constvalue;
6031  }
6032  else if (datatype == BYTEAOID)
6033  {
6034  bytea *bstr = DatumGetByteaPP(str_const->constvalue);
6035 
6036  len = VARSIZE_ANY_EXHDR(bstr);
6037  workstr = (char *) palloc(len);
6038  memcpy(workstr, VARDATA_ANY(bstr), len);
6039  Assert((Pointer) bstr == DatumGetPointer(str_const->constvalue));
6040  cmpstr = str_const->constvalue;
6041  }
6042  else
6043  {
6044  workstr = TextDatumGetCString(str_const->constvalue);
6045  len = strlen(workstr);
6046  if (lc_collate_is_c(collation) || len == 0)
6047  cmpstr = str_const->constvalue;
6048  else
6049  {
6050  /* If first time through, determine the suffix to use */
6051  static char suffixchar = 0;
6052  static Oid suffixcollation = 0;
6053 
6054  if (!suffixchar || suffixcollation != collation)
6055  {
6056  char *best;
6057 
6058  best = "Z";
6059  if (varstr_cmp(best, 1, "z", 1, collation) < 0)
6060  best = "z";
6061  if (varstr_cmp(best, 1, "y", 1, collation) < 0)
6062  best = "y";
6063  if (varstr_cmp(best, 1, "9", 1, collation) < 0)
6064  best = "9";
6065  suffixchar = *best;
6066  suffixcollation = collation;
6067  }
6068 
6069  /* And build the string to compare to */
6070  cmptxt = (text *) palloc(VARHDRSZ + len + 1);
6071  SET_VARSIZE(cmptxt, VARHDRSZ + len + 1);
6072  memcpy(VARDATA(cmptxt), workstr, len);
6073  *(VARDATA(cmptxt) + len) = suffixchar;
6074  cmpstr = PointerGetDatum(cmptxt);
6075  }
6076  }
6077 
6078  /* Select appropriate character-incrementer function */
6079  if (datatype == BYTEAOID)
6080  charinc = byte_increment;
6081  else
6083 
6084  /* And search ... */
6085  while (len > 0)
6086  {
6087  int charlen;
6088  unsigned char *lastchar;
6089 
6090  /* Identify the last character --- for bytea, just the last byte */
6091  if (datatype == BYTEAOID)
6092  charlen = 1;
6093  else
6094  charlen = len - pg_mbcliplen(workstr, len, len - 1);
6095  lastchar = (unsigned char *) (workstr + len - charlen);
6096 
6097  /*
6098  * Try to generate a larger string by incrementing the last character
6099  * (for BYTEA, we treat each byte as a character).
6100  *
6101  * Note: the incrementer function is expected to return true if it's
6102  * generated a valid-per-the-encoding new character, otherwise false.
6103  * The contents of the character on false return are unspecified.
6104  */
6105  while (charinc(lastchar, charlen))
6106  {
6107  Const *workstr_const;
6108 
6109  if (datatype == BYTEAOID)
6110  workstr_const = string_to_bytea_const(workstr, len);
6111  else
6112  workstr_const = string_to_const(workstr, datatype);
6113 
6114  if (DatumGetBool(FunctionCall2Coll(ltproc,
6115  collation,
6116  cmpstr,
6117  workstr_const->constvalue)))
6118  {
6119  /* Successfully made a string larger than cmpstr */
6120  if (cmptxt)
6121  pfree(cmptxt);
6122  pfree(workstr);
6123  return workstr_const;
6124  }
6125 
6126  /* No good, release unusable value and try again */
6127  pfree(DatumGetPointer(workstr_const->constvalue));
6128  pfree(workstr_const);
6129  }
6130 
6131  /*
6132  * No luck here, so truncate off the last character and try to
6133  * increment the next one.
6134  */
6135  len -= charlen;
6136  workstr[len] = '\0';
6137  }
6138 
6139  /* Failed... */
6140  if (cmptxt)
6141  pfree(cmptxt);
6142  pfree(workstr);
6143 
6144  return NULL;
6145 }
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:6216
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
unsigned int Oid
Definition: postgres_ext.h:31
static bool byte_increment(unsigned char *ptr, int len)
Definition: selfuncs.c:5960
static Const * string_to_const(const char *str, Oid datatype)
Definition: selfuncs.c:6173
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:356
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
#define BYTEAOID
Definition: pg_type.h:292
#define DatumGetPointer(X)
Definition: postgres.h:555
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
mbcharacter_incrementer pg_database_encoding_character_incrementer(void)
Definition: wchar.c:1842
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
Datum nameout(PG_FUNCTION_ARGS)
Definition: name.c:69
double mcv_selectivity ( VariableStatData vardata,
FmgrInfo opproc,
Datum  constval,
bool  varonleft,
double *  sumcommonp 
)

Definition at line 611 of file selfuncs.c.

References VariableStatData::atttype, VariableStatData::atttypmod, DatumGetBool, DEFAULT_COLLATION_OID, free_attstatsslot(), FunctionCall2Coll(), get_attstatsslot(), HeapTupleIsValid, i, InvalidOid, NULL, STATISTIC_KIND_MCV, VariableStatData::statsTuple, and values.

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

614 {
615  double mcv_selec,
616  sumcommon;
617  Datum *values;
618  int nvalues;
619  float4 *numbers;
620  int nnumbers;
621  int i;
622 
623  mcv_selec = 0.0;
624  sumcommon = 0.0;
625 
626  if (HeapTupleIsValid(vardata->statsTuple) &&
627  get_attstatsslot(vardata->statsTuple,
628  vardata->atttype, vardata->atttypmod,
630  NULL,
631  &values, &nvalues,
632  &numbers, &nnumbers))
633  {
634  for (i = 0; i < nvalues; i++)
635  {
636  if (varonleft ?
639  values[i],
640  constval)) :
643  constval,
644  values[i])))
645  mcv_selec += numbers[i];
646  sumcommon += numbers[i];
647  }
648  free_attstatsslot(vardata->atttype, values, nvalues,
649  numbers, nnumbers);
650  }
651 
652  *sumcommonp = sumcommon;
653  return mcv_selec;
654 }
HeapTuple statsTuple
Definition: selfuncs.h:71
bool get_attstatsslot(HeapTuple statstuple, Oid atttype, int32 atttypmod, int reqkind, Oid reqop, Oid *actualop, Datum **values, int *nvalues, float4 **numbers, int *nnumbers)
Definition: lsyscache.c:2854
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
int32 atttypmod
Definition: selfuncs.h:76
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
#define DatumGetBool(X)
Definition: postgres.h:399
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:204
float float4
Definition: c.h:380
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int i
void free_attstatsslot(Oid atttype, Datum *values, int nvalues, float4 *numbers, int nnumbers)
Definition: lsyscache.c:2978
void mergejoinscansel ( PlannerInfo root,
Node clause,
Oid  opfamily,
int  strategy,
bool  nulls_first,
Selectivity leftstart,
Selectivity leftend,
Selectivity rightstart,
Selectivity rightend 
)

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

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

1666 {
1667  VariableStatData vardata;
1668  double selec;
1669 
1670  examine_variable(root, arg, varRelid, &vardata);
1671 
1672  if (HeapTupleIsValid(vardata.statsTuple))
1673  {
1674  Form_pg_statistic stats;
1675  double freq_null;
1676 
1677  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1678  freq_null = stats->stanullfrac;
1679 
1680  switch (nulltesttype)
1681  {
1682  case IS_NULL:
1683 
1684  /*
1685  * Use freq_null directly.
1686  */
1687  selec = freq_null;
1688  break;
1689  case IS_NOT_NULL:
1690 
1691  /*
1692  * Select not unknown (not null) values. Calculate from
1693  * freq_null.
1694  */
1695  selec = 1.0 - freq_null;
1696  break;
1697  default:
1698  elog(ERROR, "unrecognized nulltesttype: %d",
1699  (int) nulltesttype);
1700  return (Selectivity) 0; /* keep compiler quiet */
1701  }
1702  }
1703  else
1704  {
1705  /*
1706  * No ANALYZE stats available, so make a guess
1707  */
1708  switch (nulltesttype)
1709  {
1710  case IS_NULL:
1711  selec = DEFAULT_UNK_SEL;
1712  break;
1713  case IS_NOT_NULL:
1714  selec = DEFAULT_NOT_UNK_SEL;
1715  break;
1716  default:
1717  elog(ERROR, "unrecognized nulltesttype: %d",
1718  (int) nulltesttype);
1719  return (Selectivity) 0; /* keep compiler quiet */
1720  }
1721  }
1722 
1723  ReleaseVariableStats(vardata);
1724 
1725  /* result should be in range, but make sure... */
1726  CLAMP_PROBABILITY(selec);
1727 
1728  return (Selectivity) selec;
1729 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
HeapTuple statsTuple
Definition: selfuncs.h:71
double Selectivity
Definition: nodes.h:635
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:4564
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:80
#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 5645 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().

5647 {
5649 
5650  switch (ptype)
5651  {
5652  case Pattern_Type_Like:
5653  result = like_fixed_prefix(patt, false, collation,
5654  prefix, rest_selec);
5655  break;
5656  case Pattern_Type_Like_IC:
5657  result = like_fixed_prefix(patt, true, collation,
5658  prefix, rest_selec);
5659  break;
5660  case Pattern_Type_Regex:
5661  result = regex_fixed_prefix(patt, false, collation,
5662  prefix, rest_selec);
5663  break;
5664  case Pattern_Type_Regex_IC:
5665  result = regex_fixed_prefix(patt, true, collation,
5666  prefix, rest_selec);
5667  break;
5668  default:
5669  elog(ERROR, "unrecognized ptype: %d", (int) ptype);
5670  result = Pattern_Prefix_None; /* keep compiler quiet */
5671  break;
5672  }
5673  return result;
5674 }
static Pattern_Prefix_Status regex_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
Definition: selfuncs.c:5577
static Pattern_Prefix_Status like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
Definition: selfuncs.c:5469
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define elog
Definition: elog.h:219
Pattern_Prefix_Status
Definition: selfuncs.h:93
Selectivity rowcomparesel ( PlannerInfo root,
RowCompareExpr clause,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo 
)

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

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

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

1770 {
1771  Oid operator = clause->opno;
1772  bool useOr = clause->useOr;
1773  bool isEquality = false;
1774  bool isInequality = false;
1775  Node *leftop;
1776  Node *rightop;
1777  Oid nominal_element_type;
1778  Oid nominal_element_collation;
1779  TypeCacheEntry *typentry;
1780  RegProcedure oprsel;
1781  FmgrInfo oprselproc;
1782  Selectivity s1;
1783  Selectivity s1disjoint;
1784 
1785  /* First, deconstruct the expression */
1786  Assert(list_length(clause->args) == 2);
1787  leftop = (Node *) linitial(clause->args);
1788  rightop = (Node *) lsecond(clause->args);
1789 
1790  /* aggressively reduce both sides to constants */
1791  leftop = estimate_expression_value(root, leftop);
1792  rightop = estimate_expression_value(root, rightop);
1793 
1794  /* get nominal (after relabeling) element type of rightop */
1795  nominal_element_type = get_base_element_type(exprType(rightop));
1796  if (!OidIsValid(nominal_element_type))
1797  return (Selectivity) 0.5; /* probably shouldn't happen */
1798  /* get nominal collation, too, for generating constants */
1799  nominal_element_collation = exprCollation(rightop);
1800 
1801  /* look through any binary-compatible relabeling of rightop */
1802  rightop = strip_array_coercion(rightop);
1803 
1804  /*
1805  * Detect whether the operator is the default equality or inequality
1806  * operator of the array element type.
1807  */
1808  typentry = lookup_type_cache(nominal_element_type, TYPECACHE_EQ_OPR);
1809  if (OidIsValid(typentry->eq_opr))
1810  {
1811  if (operator == typentry->eq_opr)
1812  isEquality = true;
1813  else if (get_negator(operator) == typentry->eq_opr)
1814  isInequality = true;
1815  }
1816 
1817  /*
1818  * If it is equality or inequality, we might be able to estimate this as a
1819  * form of array containment; for instance "const = ANY(column)" can be
1820  * treated as "ARRAY[const] <@ column". scalararraysel_containment tries
1821  * that, and returns the selectivity estimate if successful, or -1 if not.
1822  */
1823  if ((isEquality || isInequality) && !is_join_clause)
1824  {
1825  s1 = scalararraysel_containment(root, leftop, rightop,
1826  nominal_element_type,
1827  isEquality, useOr, varRelid);
1828  if (s1 >= 0.0)
1829  return s1;
1830  }
1831 
1832  /*
1833  * Look up the underlying operator's selectivity estimator. Punt if it
1834  * hasn't got one.
1835  */
1836  if (is_join_clause)
1837  oprsel = get_oprjoin(operator);
1838  else
1839  oprsel = get_oprrest(operator);
1840  if (!oprsel)
1841  return (Selectivity) 0.5;
1842  fmgr_info(oprsel, &oprselproc);
1843 
1844  /*
1845  * In the array-containment check above, we must only believe that an
1846  * operator is equality or inequality if it is the default btree equality
1847  * operator (or its negator) for the element type, since those are the
1848  * operators that array containment will use. But in what follows, we can
1849  * be a little laxer, and also believe that any operators using eqsel() or
1850  * neqsel() as selectivity estimator act like equality or inequality.
1851  */
1852  if (oprsel == F_EQSEL || oprsel == F_EQJOINSEL)
1853  isEquality = true;
1854  else if (oprsel == F_NEQSEL || oprsel == F_NEQJOINSEL)
1855  isInequality = true;
1856 
1857  /*
1858  * We consider three cases:
1859  *
1860  * 1. rightop is an Array constant: deconstruct the array, apply the
1861  * operator's selectivity function for each array element, and merge the
1862  * results in the same way that clausesel.c does for AND/OR combinations.
1863  *
1864  * 2. rightop is an ARRAY[] construct: apply the operator's selectivity
1865  * function for each element of the ARRAY[] construct, and merge.
1866  *
1867  * 3. otherwise, make a guess ...
1868  */
1869  if (rightop && IsA(rightop, Const))
1870  {
1871  Datum arraydatum = ((Const *) rightop)->constvalue;
1872  bool arrayisnull = ((Const *) rightop)->constisnull;
1873  ArrayType *arrayval;
1874  int16 elmlen;
1875  bool elmbyval;
1876  char elmalign;
1877  int num_elems;
1878  Datum *elem_values;
1879  bool *elem_nulls;
1880  int i;
1881 
1882  if (arrayisnull) /* qual can't succeed if null array */
1883  return (Selectivity) 0.0;
1884  arrayval = DatumGetArrayTypeP(arraydatum);
1886  &elmlen, &elmbyval, &elmalign);
1887  deconstruct_array(arrayval,
1888  ARR_ELEMTYPE(arrayval),
1889  elmlen, elmbyval, elmalign,
1890  &elem_values, &elem_nulls, &num_elems);
1891 
1892  /*
1893  * For generic operators, we assume the probability of success is
1894  * independent for each array element. But for "= ANY" or "<> ALL",
1895  * if the array elements are distinct (which'd typically be the case)
1896  * then the probabilities are disjoint, and we should just sum them.
1897  *
1898  * If we were being really tense we would try to confirm that the
1899  * elements are all distinct, but that would be expensive and it
1900  * doesn't seem to be worth the cycles; it would amount to penalizing
1901  * well-written queries in favor of poorly-written ones. However, we
1902  * do protect ourselves a little bit by checking whether the
1903  * disjointness assumption leads to an impossible (out of range)
1904  * probability; if so, we fall back to the normal calculation.
1905  */
1906  s1 = s1disjoint = (useOr ? 0.0 : 1.0);
1907 
1908  for (i = 0; i < num_elems; i++)
1909  {
1910  List *args;
1911  Selectivity s2;
1912 
1913  args = list_make2(leftop,
1914  makeConst(nominal_element_type,
1915  -1,
1916  nominal_element_collation,
1917  elmlen,
1918  elem_values[i],
1919  elem_nulls[i],
1920  elmbyval));
1921  if (is_join_clause)
1922  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
1923  clause->inputcollid,
1924  PointerGetDatum(root),
1925  ObjectIdGetDatum(operator),
1926  PointerGetDatum(args),
1927  Int16GetDatum(jointype),
1928  PointerGetDatum(sjinfo)));
1929  else
1930  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
1931  clause->inputcollid,
1932  PointerGetDatum(root),
1933  ObjectIdGetDatum(operator),
1934  PointerGetDatum(args),
1935  Int32GetDatum(varRelid)));
1936 
1937  if (useOr)
1938  {
1939  s1 = s1 + s2 - s1 * s2;
1940  if (isEquality)
1941  s1disjoint += s2;
1942  }
1943  else
1944  {
1945  s1 = s1 * s2;
1946  if (isInequality)
1947  s1disjoint += s2 - 1.0;
1948  }
1949  }
1950 
1951  /* accept disjoint-probability estimate if in range */
1952  if ((useOr ? isEquality : isInequality) &&
1953  s1disjoint >= 0.0 && s1disjoint <= 1.0)
1954  s1 = s1disjoint;
1955  }
1956  else if (rightop && IsA(rightop, ArrayExpr) &&
1957  !((ArrayExpr *) rightop)->multidims)
1958  {
1959  ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
1960  int16 elmlen;
1961  bool elmbyval;
1962  ListCell *l;
1963 
1964  get_typlenbyval(arrayexpr->element_typeid,
1965  &elmlen, &elmbyval);
1966 
1967  /*
1968  * We use the assumption of disjoint probabilities here too, although
1969  * the odds of equal array elements are rather higher if the elements
1970  * are not all constants (which they won't be, else constant folding
1971  * would have reduced the ArrayExpr to a Const). In this path it's
1972  * critical to have the sanity check on the s1disjoint estimate.
1973  */
1974  s1 = s1disjoint = (useOr ? 0.0 : 1.0);
1975 
1976  foreach(l, arrayexpr->elements)
1977  {
1978  Node *elem = (Node *) lfirst(l);
1979  List *args;
1980  Selectivity s2;
1981 
1982  /*
1983  * Theoretically, if elem isn't of nominal_element_type we should
1984  * insert a RelabelType, but it seems unlikely that any operator
1985  * estimation function would really care ...
1986  */
1987  args = list_make2(leftop, elem);
1988  if (is_join_clause)
1989  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
1990  clause->inputcollid,
1991  PointerGetDatum(root),
1992  ObjectIdGetDatum(operator),
1993  PointerGetDatum(args),
1994  Int16GetDatum(jointype),
1995  PointerGetDatum(sjinfo)));
1996  else
1997  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
1998  clause->inputcollid,
1999  PointerGetDatum(root),
2000  ObjectIdGetDatum(operator),
2001  PointerGetDatum(args),
2002  Int32GetDatum(varRelid)));
2003 
2004  if (useOr)
2005  {
2006  s1 = s1 + s2 - s1 * s2;
2007  if (isEquality)
2008  s1disjoint += s2;
2009  }
2010  else
2011  {
2012  s1 = s1 * s2;
2013  if (isInequality)
2014  s1disjoint += s2 - 1.0;
2015  }
2016  }
2017 
2018  /* accept disjoint-probability estimate if in range */
2019  if ((useOr ? isEquality : isInequality) &&
2020  s1disjoint >= 0.0 && s1disjoint <= 1.0)
2021  s1 = s1disjoint;
2022  }
2023  else
2024  {
2025  CaseTestExpr *dummyexpr;
2026  List *args;
2027  Selectivity s2;
2028  int i;
2029 
2030  /*
2031  * We need a dummy rightop to pass to the operator selectivity
2032  * routine. It can be pretty much anything that doesn't look like a
2033  * constant; CaseTestExpr is a convenient choice.
2034  */
2035  dummyexpr = makeNode(CaseTestExpr);
2036  dummyexpr->typeId = nominal_element_type;
2037  dummyexpr->typeMod = -1;
2038  dummyexpr->collation = clause->inputcollid;
2039  args = list_make2(leftop, dummyexpr);
2040  if (is_join_clause)
2041  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
2042  clause->inputcollid,
2043  PointerGetDatum(root),
2044  ObjectIdGetDatum(operator),
2045  PointerGetDatum(args),
2046  Int16GetDatum(jointype),
2047  PointerGetDatum(sjinfo)));
2048  else
2049  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
2050  clause->inputcollid,
2051  PointerGetDatum(root),
2052  ObjectIdGetDatum(operator),
2053  PointerGetDatum(args),
2054  Int32GetDatum(varRelid)));
2055  s1 = useOr ? 0.0 : 1.0;
2056 
2057  /*
2058  * Arbitrarily assume 10 elements in the eventual array value (see
2059  * also estimate_array_length). We don't risk an assumption of
2060  * disjoint probabilities here.
2061  */
2062  for (i = 0; i < 10; i++)
2063  {
2064  if (useOr)
2065  s1 = s1 + s2 - s1 * s2;
2066  else
2067  s1 = s1 * s2;
2068  }
2069  }
2070 
2071  /* result should be in range, but make sure... */
2072  CLAMP_PROBABILITY(s1);
2073 
2074  return s1;
2075 }
#define list_make2(x1, x2)
Definition: pg_list.h:134
signed short int16
Definition: c.h:255
Definition: fmgr.h:56
RegProcedure get_oprjoin(Oid opno)
Definition: lsyscache.c:1353
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2399
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:1989
#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:1430
regproc RegProcedure
Definition: c.h:395
#define Int16GetDatum(X)
Definition: postgres.h:457
Definition: nodes.h:506
#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:1403
double Selectivity
Definition: nodes.h:635
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:114
int32 typeMod
Definition: primnodes.h:935
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
#define linitial(l)
Definition: pg_list.h:110
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
char * s1
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
List * elements
Definition: primnodes.h:953
RegProcedure get_oprrest(Oid opno)
Definition: lsyscache.c:1329
char * s2
#define DatumGetFloat8(X)
Definition: postgres.h:734
uintptr_t Datum
Definition: postgres.h:372
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define makeNode(_type_)
Definition: nodes.h:554
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Selectivity scalararraysel_containment(PlannerInfo *root, Node *leftop, Node *rightop, Oid elemtype, bool isEquality, bool useOr, int varRelid)
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:1969
Oid element_typeid
Definition: primnodes.h:952
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:2525
#define Int32GetDatum(X)
Definition: postgres.h:485
int i
Oid get_negator(Oid opno)
Definition: lsyscache.c:1305
Definition: pg_list.h:45
#define ARR_ELEMTYPE(a)
Definition: array.h:273
static Node * strip_array_coercion(Node *node)
Definition: selfuncs.c:1740
#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 VariableStatData::atttypmod, 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(), NULL, OID_ARRAY_CONTAINED_OP, OID_ARRAY_CONTAINS_OP, OidIsValid, VariableStatData::rel, ReleaseVariableStats, STATISTIC_KIND_DECHIST, STATISTIC_KIND_MCELEM, VariableStatData::statsTuple, TYPECACHE_CMP_PROC_FINFO, and 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  {
138  Form_pg_statistic stats;
139  Datum *values;
140  int nvalues;
141  float4 *numbers;
142  int nnumbers;
143  float4 *hist;
144  int nhist;
145 
146  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
147 
148  /* MCELEM will be an array of same type as element */
149  if (get_attstatsslot(vardata.statsTuple,
150  elemtype, vardata.atttypmod,
152  NULL,
153  &values, &nvalues,
154  &numbers, &nnumbers))
155  {
156  /* For ALL case, also get histogram of distinct-element counts */
157  if (useOr ||
158  !get_attstatsslot(vardata.statsTuple,
159  elemtype, vardata.atttypmod,
161  NULL,
162  NULL, NULL,
163  &hist, &nhist))
164  {
165  hist = NULL;
166  nhist = 0;
167  }
168 
169  /*
170  * For = ANY, estimate as var @> ARRAY[const].
171  *
172  * For = ALL, estimate as var <@ ARRAY[const].
173  */
174  if (useOr)
175  selec = mcelem_array_contain_overlap_selec(values, nvalues,
176  numbers, nnumbers,
177  &constval, 1,
179  cmpfunc);
180  else
181  selec = mcelem_array_contained_selec(values, nvalues,
182  numbers, nnumbers,
183  &constval, 1,
184  hist, nhist,
186  cmpfunc);
187 
188  if (hist)
189  free_attstatsslot(elemtype, NULL, 0, hist, nhist);
190  free_attstatsslot(elemtype, values, nvalues, numbers, nnumbers);
191  }
192  else
193  {
194  /* No most-common-elements info, so do without */
195  if (useOr)
197  NULL, 0,
198  &constval, 1,
200  cmpfunc);
201  else
203  NULL, 0,
204  &constval, 1,
205  NULL, 0,
207  cmpfunc);
208  }
209 
210  /*
211  * MCE stats count only non-null rows, so adjust for null rows.
212  */
213  selec *= (1.0 - stats->stanullfrac);
214  }
215  else
216  {
217  /* No stats at all, so do without */
218  if (useOr)
220  NULL, 0,
221  &constval, 1,
223  cmpfunc);
224  else
226  NULL, 0,
227  &constval, 1,
228  NULL, 0,
230  cmpfunc);
231  /* we assume no nulls here, so no stanullfrac correction */
232  }
233 
234  ReleaseVariableStats(vardata);
235 
236  /*
237  * If the operator is <>, invert the results.
238  */
239  if (!isEquality)
240  selec = 1.0 - selec;
241 
242  CLAMP_PROBABILITY(selec);
243 
244  return selec;
245 }
Definition: fmgr.h:56
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
HeapTuple statsTuple
Definition: selfuncs.h:71
RelOptInfo * rel
Definition: selfuncs.h:70
double Selectivity
Definition: nodes.h:635
bool get_attstatsslot(HeapTuple statstuple, Oid atttype, int32 atttypmod, int reqkind, Oid reqop, Oid *actualop, Datum **values, int *nvalues, float4 **numbers, int *nnumbers)
Definition: lsyscache.c:2854
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define OidIsValid(objectId)
Definition: c.h:538
int32 atttypmod
Definition: selfuncs.h:76
#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 STATISTIC_KIND_DECHIST
Definition: pg_statistic.h:270
#define OID_ARRAY_CONTAINED_OP
Definition: pg_operator.h:1572
float float4
Definition: c.h:380
uintptr_t Datum
Definition: postgres.h:372
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4564
#define NULL
Definition: c.h:229
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)
static Datum values[MAXATTR]
Definition: bootstrap.c:162
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:80
#define STATISTIC_KIND_MCELEM
Definition: pg_statistic.h:257
#define TYPECACHE_CMP_PROC_FINFO
Definition: typcache.h:116
void free_attstatsslot(Oid atttype, Datum *values, int nvalues, float4 *numbers, int nnumbers)
Definition: lsyscache.c:2978
static Selectivity mcelem_array_contain_overlap_selec(Datum *mcelem, int nmcelem, float4 *numbers, int nnumbers, Datum *array_data, int nitems, Oid operator, FmgrInfo *cmpfunc)

Variable Documentation

PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook

Definition at line 152 of file selfuncs.c.

Referenced by btcostestimate(), and examine_variable().

PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook

Definition at line 151 of file selfuncs.c.

Referenced by btcostestimate(), and examine_simple_variable().