PostgreSQL Source Code  git master
selfuncs.h File Reference
#include "fmgr.h"
#include "access/htup.h"
#include "nodes/relation.h"
Include dependency graph for selfuncs.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  VariableStatData
 
struct  IndexQualInfo
 
struct  GenericCosts
 

Macros

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

Typedefs

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

Enumerations

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

Functions

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

Variables

PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook
 
PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook
 

Macro Definition Documentation

◆ CLAMP_PROBABILITY

◆ DEFAULT_EQ_SEL

#define DEFAULT_EQ_SEL   0.005

Definition at line 34 of file selfuncs.h.

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

◆ DEFAULT_INEQ_SEL

◆ DEFAULT_MATCH_SEL

#define DEFAULT_MATCH_SEL   0.005

Definition at line 43 of file selfuncs.h.

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

◆ DEFAULT_NOT_UNK_SEL

#define DEFAULT_NOT_UNK_SEL   (1.0 - DEFAULT_UNK_SEL)

Definition at line 50 of file selfuncs.h.

Referenced by booltestsel(), and nulltestsel().

◆ DEFAULT_NUM_DISTINCT

#define DEFAULT_NUM_DISTINCT   200

Definition at line 46 of file selfuncs.h.

Referenced by get_variable_numdistinct().

◆ DEFAULT_RANGE_INEQ_SEL

#define DEFAULT_RANGE_INEQ_SEL   0.005

Definition at line 40 of file selfuncs.h.

Referenced by clauselist_selectivity(), and default_range_selectivity().

◆ DEFAULT_UNK_SEL

#define DEFAULT_UNK_SEL   0.005

Definition at line 49 of file selfuncs.h.

Referenced by booltestsel(), and nulltestsel().

◆ ReleaseVariableStats

#define ReleaseVariableStats (   vardata)

Typedef Documentation

◆ get_index_stats_hook_type

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

Definition at line 147 of file selfuncs.h.

◆ get_relation_stats_hook_type

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

Definition at line 142 of file selfuncs.h.

◆ VariableStatData

Enumeration Type Documentation

◆ Pattern_Prefix_Status

Enumerator
Pattern_Prefix_None 
Pattern_Prefix_Partial 
Pattern_Prefix_Exact 

Definition at line 94 of file selfuncs.h.

◆ Pattern_Type

Enumerator
Pattern_Type_Like 
Pattern_Type_Like_IC 
Pattern_Type_Regex 
Pattern_Type_Regex_IC 

Definition at line 88 of file selfuncs.h.

Function Documentation

◆ booltestsel()

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

Definition at line 1581 of file selfuncs.c.

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

Referenced by clause_selectivity().

1583 {
1584  VariableStatData vardata;
1585  double selec;
1586 
1587  examine_variable(root, arg, varRelid, &vardata);
1588 
1589  if (HeapTupleIsValid(vardata.statsTuple))
1590  {
1591  Form_pg_statistic stats;
1592  double freq_null;
1593  AttStatsSlot sslot;
1594 
1595  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1596  freq_null = stats->stanullfrac;
1597 
1598  if (get_attstatsslot(&sslot, vardata.statsTuple,
1601  && sslot.nnumbers > 0)
1602  {
1603  double freq_true;
1604  double freq_false;
1605 
1606  /*
1607  * Get first MCV frequency and derive frequency for true.
1608  */
1609  if (DatumGetBool(sslot.values[0]))
1610  freq_true = sslot.numbers[0];
1611  else
1612  freq_true = 1.0 - sslot.numbers[0] - freq_null;
1613 
1614  /*
1615  * Next derive frequency for false. Then use these as appropriate
1616  * to derive frequency for each case.
1617  */
1618  freq_false = 1.0 - freq_true - freq_null;
1619 
1620  switch (booltesttype)
1621  {
1622  case IS_UNKNOWN:
1623  /* select only NULL values */
1624  selec = freq_null;
1625  break;
1626  case IS_NOT_UNKNOWN:
1627  /* select non-NULL values */
1628  selec = 1.0 - freq_null;
1629  break;
1630  case IS_TRUE:
1631  /* select only TRUE values */
1632  selec = freq_true;
1633  break;
1634  case IS_NOT_TRUE:
1635  /* select non-TRUE values */
1636  selec = 1.0 - freq_true;
1637  break;
1638  case IS_FALSE:
1639  /* select only FALSE values */
1640  selec = freq_false;
1641  break;
1642  case IS_NOT_FALSE:
1643  /* select non-FALSE values */
1644  selec = 1.0 - freq_false;
1645  break;
1646  default:
1647  elog(ERROR, "unrecognized booltesttype: %d",
1648  (int) booltesttype);
1649  selec = 0.0; /* Keep compiler quiet */
1650  break;
1651  }
1652 
1653  free_attstatsslot(&sslot);
1654  }
1655  else
1656  {
1657  /*
1658  * No most-common-value info available. Still have null fraction
1659  * information, so use it for IS [NOT] UNKNOWN. Otherwise adjust
1660  * for null fraction and assume a 50-50 split of TRUE and FALSE.
1661  */
1662  switch (booltesttype)
1663  {
1664  case IS_UNKNOWN:
1665  /* select only NULL values */
1666  selec = freq_null;
1667  break;
1668  case IS_NOT_UNKNOWN:
1669  /* select non-NULL values */
1670  selec = 1.0 - freq_null;
1671  break;
1672  case IS_TRUE:
1673  case IS_FALSE:
1674  /* Assume we select half of the non-NULL values */
1675  selec = (1.0 - freq_null) / 2.0;
1676  break;
1677  case IS_NOT_TRUE:
1678  case IS_NOT_FALSE:
1679  /* Assume we select NULLs plus half of the non-NULLs */
1680  /* equiv. to freq_null + (1.0 - freq_null) / 2.0 */
1681  selec = (freq_null + 1.0) / 2.0;
1682  break;
1683  default:
1684  elog(ERROR, "unrecognized booltesttype: %d",
1685  (int) booltesttype);
1686  selec = 0.0; /* Keep compiler quiet */
1687  break;
1688  }
1689  }
1690  }
1691  else
1692  {
1693  /*
1694  * If we can't get variable statistics for the argument, perhaps
1695  * clause_selectivity can do something with it. We ignore the
1696  * possibility of a NULL value when using clause_selectivity, and just
1697  * assume the value is either TRUE or FALSE.
1698  */
1699  switch (booltesttype)
1700  {
1701  case IS_UNKNOWN:
1702  selec = DEFAULT_UNK_SEL;
1703  break;
1704  case IS_NOT_UNKNOWN:
1705  selec = DEFAULT_NOT_UNK_SEL;
1706  break;
1707  case IS_TRUE:
1708  case IS_NOT_FALSE:
1709  selec = (double) clause_selectivity(root, arg,
1710  varRelid,
1711  jointype, sjinfo);
1712  break;
1713  case IS_FALSE:
1714  case IS_NOT_TRUE:
1715  selec = 1.0 - (double) clause_selectivity(root, arg,
1716  varRelid,
1717  jointype, sjinfo);
1718  break;
1719  default:
1720  elog(ERROR, "unrecognized booltesttype: %d",
1721  (int) booltesttype);
1722  selec = 0.0; /* Keep compiler quiet */
1723  break;
1724  }
1725  }
1726 
1727  ReleaseVariableStats(vardata);
1728 
1729  /* result should be in range, but make sure... */
1730  CLAMP_PROBABILITY(selec);
1731 
1732  return (Selectivity) selec;
1733 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
#define ATTSTATSSLOT_VALUES
Definition: lsyscache.h:39
HeapTuple statsTuple
Definition: selfuncs.h:71
int nnumbers
Definition: lsyscache.h:53
double Selectivity
Definition: nodes.h:642
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
#define DEFAULT_NOT_UNK_SEL
Definition: selfuncs.h:50
#define ATTSTATSSLOT_NUMBERS
Definition: lsyscache.h:40
#define ERROR
Definition: elog.h:43
Selectivity clause_selectivity(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:574
float4 * numbers
Definition: lsyscache.h:52
#define DatumGetBool(X)
Definition: postgres.h:399
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:204
#define DEFAULT_UNK_SEL
Definition: selfuncs.h:49
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4728
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:2928
Datum * values
Definition: lsyscache.h:49
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
#define elog
Definition: elog.h:219
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3044

◆ boolvarsel()

Selectivity boolvarsel ( PlannerInfo root,
Node arg,
int  varRelid 
)

Definition at line 1542 of file selfuncs.c.

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

Referenced by clause_selectivity().

1543 {
1544  VariableStatData vardata;
1545  double selec;
1546 
1547  examine_variable(root, arg, varRelid, &vardata);
1548  if (HeapTupleIsValid(vardata.statsTuple))
1549  {
1550  /*
1551  * A boolean variable V is equivalent to the clause V = 't', so we
1552  * compute the selectivity as if that is what we have.
1553  */
1554  selec = var_eq_const(&vardata, BooleanEqualOperator,
1555  BoolGetDatum(true), false, true, false);
1556  }
1557  else if (is_funcclause(arg))
1558  {
1559  /*
1560  * If we have no stats and it's a function call, estimate 0.3333333.
1561  * This seems a pretty unprincipled choice, but Postgres has been
1562  * using that estimate for function calls since 1992. The hoariness
1563  * of this behavior suggests that we should not be in too much hurry
1564  * to use another value.
1565  */
1566  selec = 0.3333333;
1567  }
1568  else
1569  {
1570  /* Otherwise, the default estimate is 0.5 */
1571  selec = 0.5;
1572  }
1573  ReleaseVariableStats(vardata);
1574  return selec;
1575 }
HeapTuple statsTuple
Definition: selfuncs.h:71
#define is_funcclause(clause)
Definition: clauses.h:21
#define BooleanEqualOperator
Definition: pg_operator.h:114
static double var_eq_const(VariableStatData *vardata, Oid operator, Datum constval, bool constisnull, bool varonleft, bool negate)
Definition: selfuncs.c:297
#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:4728
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81

◆ deconstruct_indexquals()

List* deconstruct_indexquals ( IndexPath path)

Definition at line 6490 of file selfuncs.c.

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

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

6491 {
6492  List *result = NIL;
6493  IndexOptInfo *index = path->indexinfo;
6494  ListCell *lcc,
6495  *lci;
6496 
6497  forboth(lcc, path->indexquals, lci, path->indexqualcols)
6498  {
6499  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lcc);
6500  int indexcol = lfirst_int(lci);
6501  Expr *clause;
6502  Node *leftop,
6503  *rightop;
6504  IndexQualInfo *qinfo;
6505 
6506  clause = rinfo->clause;
6507 
6508  qinfo = (IndexQualInfo *) palloc(sizeof(IndexQualInfo));
6509  qinfo->rinfo = rinfo;
6510  qinfo->indexcol = indexcol;
6511 
6512  if (IsA(clause, OpExpr))
6513  {
6514  qinfo->clause_op = ((OpExpr *) clause)->opno;
6515  leftop = get_leftop(clause);
6516  rightop = get_rightop(clause);
6517  if (match_index_to_operand(leftop, indexcol, index))
6518  {
6519  qinfo->varonleft = true;
6520  qinfo->other_operand = rightop;
6521  }
6522  else
6523  {
6524  Assert(match_index_to_operand(rightop, indexcol, index));
6525  qinfo->varonleft = false;
6526  qinfo->other_operand = leftop;
6527  }
6528  }
6529  else if (IsA(clause, RowCompareExpr))
6530  {
6531  RowCompareExpr *rc = (RowCompareExpr *) clause;
6532 
6533  qinfo->clause_op = linitial_oid(rc->opnos);
6534  /* Examine only first columns to determine left/right sides */
6536  indexcol, index))
6537  {
6538  qinfo->varonleft = true;
6539  qinfo->other_operand = (Node *) rc->rargs;
6540  }
6541  else
6542  {
6544  indexcol, index));
6545  qinfo->varonleft = false;
6546  qinfo->other_operand = (Node *) rc->largs;
6547  }
6548  }
6549  else if (IsA(clause, ScalarArrayOpExpr))
6550  {
6551  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
6552 
6553  qinfo->clause_op = saop->opno;
6554  /* index column is always on the left in this case */
6556  indexcol, index));
6557  qinfo->varonleft = true;
6558  qinfo->other_operand = (Node *) lsecond(saop->args);
6559  }
6560  else if (IsA(clause, NullTest))
6561  {
6562  qinfo->clause_op = InvalidOid;
6563  Assert(match_index_to_operand((Node *) ((NullTest *) clause)->arg,
6564  indexcol, index));
6565  qinfo->varonleft = true;
6566  qinfo->other_operand = NULL;
6567  }
6568  else
6569  {
6570  elog(ERROR, "unsupported indexqual type: %d",
6571  (int) nodeTag(clause));
6572  }
6573 
6574  result = lappend(result, qinfo);
6575  }
6576  return result;
6577 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
IndexOptInfo * indexinfo
Definition: relation.h:1119
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3180
Definition: nodes.h:512
RestrictInfo * rinfo
Definition: selfuncs.h:106
#define lsecond(l)
Definition: pg_list.h:116
Definition: type.h:89
List * indexquals
Definition: relation.h:1121
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
#define lfirst_node(type, lc)
Definition: pg_list.h:109
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1841
bool varonleft
Definition: selfuncs.h:108
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:680
#define linitial_oid(l)
Definition: pg_list.h:113
#define nodeTag(nodeptr)
Definition: nodes.h:517
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
List * indexqualcols
Definition: relation.h:1122
void * palloc(Size size)
Definition: mcxt.c:835
Node * other_operand
Definition: selfuncs.h:110
void * arg
#define elog
Definition: elog.h:219
Definition: pg_list.h:45

◆ estimate_array_length()

int estimate_array_length ( Node arrayexpr)

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

2168 {
2169  /* look through any binary-compatible relabeling of arrayexpr */
2170  arrayexpr = strip_array_coercion(arrayexpr);
2171 
2172  if (arrayexpr && IsA(arrayexpr, Const))
2173  {
2174  Datum arraydatum = ((Const *) arrayexpr)->constvalue;
2175  bool arrayisnull = ((Const *) arrayexpr)->constisnull;
2176  ArrayType *arrayval;
2177 
2178  if (arrayisnull)
2179  return 0;
2180  arrayval = DatumGetArrayTypeP(arraydatum);
2181  return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
2182  }
2183  else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
2184  !((ArrayExpr *) arrayexpr)->multidims)
2185  {
2186  return list_length(((ArrayExpr *) arrayexpr)->elements);
2187  }
2188  else
2189  {
2190  /* default guess --- see also scalararraysel */
2191  return 10;
2192  }
2193 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
#define ARR_DIMS(a)
Definition: array.h:279
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:275
static Node * strip_array_coercion(Node *node)
Definition: selfuncs.c:1815
#define DatumGetArrayTypeP(X)
Definition: array.h:246

◆ estimate_hash_bucket_stats()

void estimate_hash_bucket_stats ( PlannerInfo root,
Node hashkey,
double  nbuckets,
Selectivity mcv_freq,
Selectivity bucketsize_frac 
)

Definition at line 3761 of file selfuncs.c.

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

Referenced by final_cost_hashjoin().

3764 {
3765  VariableStatData vardata;
3766  double estfract,
3767  ndistinct,
3768  stanullfrac,
3769  avgfreq;
3770  bool isdefault;
3771  AttStatsSlot sslot;
3772 
3773  examine_variable(root, hashkey, 0, &vardata);
3774 
3775  /* Look up the frequency of the most common value, if available */
3776  *mcv_freq = 0.0;
3777 
3778  if (HeapTupleIsValid(vardata.statsTuple))
3779  {
3780  if (get_attstatsslot(&sslot, vardata.statsTuple,
3783  {
3784  /*
3785  * The first MCV stat is for the most common value.
3786  */
3787  if (sslot.nnumbers > 0)
3788  *mcv_freq = sslot.numbers[0];
3789  free_attstatsslot(&sslot);
3790  }
3791  }
3792 
3793  /* Get number of distinct values */
3794  ndistinct = get_variable_numdistinct(&vardata, &isdefault);
3795 
3796  /*
3797  * If ndistinct isn't real, punt. We normally return 0.1, but if the
3798  * mcv_freq is known to be even higher than that, use it instead.
3799  */
3800  if (isdefault)
3801  {
3802  *bucketsize_frac = (Selectivity) Max(0.1, *mcv_freq);
3803  ReleaseVariableStats(vardata);
3804  return;
3805  }
3806 
3807  /* Get fraction that are null */
3808  if (HeapTupleIsValid(vardata.statsTuple))
3809  {
3810  Form_pg_statistic stats;
3811 
3812  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
3813  stanullfrac = stats->stanullfrac;
3814  }
3815  else
3816  stanullfrac = 0.0;
3817 
3818  /* Compute avg freq of all distinct data values in raw relation */
3819  avgfreq = (1.0 - stanullfrac) / ndistinct;
3820 
3821  /*
3822  * Adjust ndistinct to account for restriction clauses. Observe we are
3823  * assuming that the data distribution is affected uniformly by the
3824  * restriction clauses!
3825  *
3826  * XXX Possibly better way, but much more expensive: multiply by
3827  * selectivity of rel's restriction clauses that mention the target Var.
3828  */
3829  if (vardata.rel && vardata.rel->tuples > 0)
3830  {
3831  ndistinct *= vardata.rel->rows / vardata.rel->tuples;
3832  ndistinct = clamp_row_est(ndistinct);
3833  }
3834 
3835  /*
3836  * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
3837  * number of buckets is less than the expected number of distinct values;
3838  * otherwise it is 1/ndistinct.
3839  */
3840  if (ndistinct > nbuckets)
3841  estfract = 1.0 / nbuckets;
3842  else
3843  estfract = 1.0 / ndistinct;
3844 
3845  /*
3846  * Adjust estimated bucketsize upward to account for skewed distribution.
3847  */
3848  if (avgfreq > 0.0 && *mcv_freq > avgfreq)
3849  estfract *= *mcv_freq / avgfreq;
3850 
3851  /*
3852  * Clamp bucketsize to sane range (the above adjustment could easily
3853  * produce an out-of-range result). We set the lower bound a little above
3854  * zero, since zero isn't a very sane result.
3855  */
3856  if (estfract < 1.0e-6)
3857  estfract = 1.0e-6;
3858  else if (estfract > 1.0)
3859  estfract = 1.0;
3860 
3861  *bucketsize_frac = (Selectivity) estfract;
3862 
3863  ReleaseVariableStats(vardata);
3864 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
HeapTuple statsTuple
Definition: selfuncs.h:71
int nnumbers
Definition: lsyscache.h:53
double tuples
Definition: relation.h:625
RelOptInfo * rel
Definition: selfuncs.h:70
double Selectivity
Definition: nodes.h:642
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define ATTSTATSSLOT_NUMBERS
Definition: lsyscache.h:40
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition: selfuncs.c:5135
float4 * numbers
Definition: lsyscache.h:52
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:204
double rows
Definition: relation.h:588
#define InvalidOid
Definition: postgres_ext.h:36
#define Max(x, y)
Definition: c.h:806
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4728
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:2928
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
e
Definition: preproc-init.c:82
double clamp_row_est(double nrows)
Definition: costsize.c:177
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3044

◆ estimate_num_groups()

double estimate_num_groups ( PlannerInfo root,
List groupExprs,
double  input_rows,
List **  pgset 
)

Definition at line 3398 of file selfuncs.c.

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

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

3400 {
3401  List *varinfos = NIL;
3402  double srf_multiplier = 1.0;
3403  double numdistinct;
3404  ListCell *l;
3405  int i;
3406 
3407  /*
3408  * We don't ever want to return an estimate of zero groups, as that tends
3409  * to lead to division-by-zero and other unpleasantness. The input_rows
3410  * estimate is usually already at least 1, but clamp it just in case it
3411  * isn't.
3412  */
3413  input_rows = clamp_row_est(input_rows);
3414 
3415  /*
3416  * If no grouping columns, there's exactly one group. (This can't happen
3417  * for normal cases with GROUP BY or DISTINCT, but it is possible for
3418  * corner cases with set operations.)
3419  */
3420  if (groupExprs == NIL || (pgset && list_length(*pgset) < 1))
3421  return 1.0;
3422 
3423  /*
3424  * Count groups derived from boolean grouping expressions. For other
3425  * expressions, find the unique Vars used, treating an expression as a Var
3426  * if we can find stats for it. For each one, record the statistical
3427  * estimate of number of distinct values (total in its table, without
3428  * regard for filtering).
3429  */
3430  numdistinct = 1.0;
3431 
3432  i = 0;
3433  foreach(l, groupExprs)
3434  {
3435  Node *groupexpr = (Node *) lfirst(l);
3436  double this_srf_multiplier;
3437  VariableStatData vardata;
3438  List *varshere;
3439  ListCell *l2;
3440 
3441  /* is expression in this grouping set? */
3442  if (pgset && !list_member_int(*pgset, i++))
3443  continue;
3444 
3445  /*
3446  * Set-returning functions in grouping columns are a bit problematic.
3447  * The code below will effectively ignore their SRF nature and come up
3448  * with a numdistinct estimate as though they were scalar functions.
3449  * We compensate by scaling up the end result by the largest SRF
3450  * rowcount estimate. (This will be an overestimate if the SRF
3451  * produces multiple copies of any output value, but it seems best to
3452  * assume the SRF's outputs are distinct. In any case, it's probably
3453  * pointless to worry too much about this without much better
3454  * estimates for SRF output rowcounts than we have today.)
3455  */
3456  this_srf_multiplier = expression_returns_set_rows(groupexpr);
3457  if (srf_multiplier < this_srf_multiplier)
3458  srf_multiplier = this_srf_multiplier;
3459 
3460  /* Short-circuit for expressions returning boolean */
3461  if (exprType(groupexpr) == BOOLOID)
3462  {
3463  numdistinct *= 2.0;
3464  continue;
3465  }
3466 
3467  /*
3468  * If examine_variable is able to deduce anything about the GROUP BY
3469  * expression, treat it as a single variable even if it's really more
3470  * complicated.
3471  */
3472  examine_variable(root, groupexpr, 0, &vardata);
3473  if (HeapTupleIsValid(vardata.statsTuple) || vardata.isunique)
3474  {
3475  varinfos = add_unique_group_var(root, varinfos,
3476  groupexpr, &vardata);
3477  ReleaseVariableStats(vardata);
3478  continue;
3479  }
3480  ReleaseVariableStats(vardata);
3481 
3482  /*
3483  * Else pull out the component Vars. Handle PlaceHolderVars by
3484  * recursing into their arguments (effectively assuming that the
3485  * PlaceHolderVar doesn't change the number of groups, which boils
3486  * down to ignoring the possible addition of nulls to the result set).
3487  */
3488  varshere = pull_var_clause(groupexpr,
3492 
3493  /*
3494  * If we find any variable-free GROUP BY item, then either it is a
3495  * constant (and we can ignore it) or it contains a volatile function;
3496  * in the latter case we punt and assume that each input row will
3497  * yield a distinct group.
3498  */
3499  if (varshere == NIL)
3500  {
3501  if (contain_volatile_functions(groupexpr))
3502  return input_rows;
3503  continue;
3504  }
3505 
3506  /*
3507  * Else add variables to varinfos list
3508  */
3509  foreach(l2, varshere)
3510  {
3511  Node *var = (Node *) lfirst(l2);
3512 
3513  examine_variable(root, var, 0, &vardata);
3514  varinfos = add_unique_group_var(root, varinfos, var, &vardata);
3515  ReleaseVariableStats(vardata);
3516  }
3517  }
3518 
3519  /*
3520  * If now no Vars, we must have an all-constant or all-boolean GROUP BY
3521  * list.
3522  */
3523  if (varinfos == NIL)
3524  {
3525  /* Apply SRF multiplier as we would do in the long path */
3526  numdistinct *= srf_multiplier;
3527  /* Round off */
3528  numdistinct = ceil(numdistinct);
3529  /* Guard against out-of-range answers */
3530  if (numdistinct > input_rows)
3531  numdistinct = input_rows;
3532  if (numdistinct < 1.0)
3533  numdistinct = 1.0;
3534  return numdistinct;
3535  }
3536 
3537  /*
3538  * Group Vars by relation and estimate total numdistinct.
3539  *
3540  * For each iteration of the outer loop, we process the frontmost Var in
3541  * varinfos, plus all other Vars in the same relation. We remove these
3542  * Vars from the newvarinfos list for the next iteration. This is the
3543  * easiest way to group Vars of same rel together.
3544  */
3545  do
3546  {
3547  GroupVarInfo *varinfo1 = (GroupVarInfo *) linitial(varinfos);
3548  RelOptInfo *rel = varinfo1->rel;
3549  double reldistinct = 1;
3550  double relmaxndistinct = reldistinct;
3551  int relvarcount = 0;
3552  List *newvarinfos = NIL;
3553  List *relvarinfos = NIL;
3554 
3555  /*
3556  * Split the list of varinfos in two - one for the current rel, one
3557  * for remaining Vars on other rels.
3558  */
3559  relvarinfos = lcons(varinfo1, relvarinfos);
3560  for_each_cell(l, lnext(list_head(varinfos)))
3561  {
3562  GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
3563 
3564  if (varinfo2->rel == varinfo1->rel)
3565  {
3566  /* varinfos on current rel */
3567  relvarinfos = lcons(varinfo2, relvarinfos);
3568  }
3569  else
3570  {
3571  /* not time to process varinfo2 yet */
3572  newvarinfos = lcons(varinfo2, newvarinfos);
3573  }
3574  }
3575 
3576  /*
3577  * Get the numdistinct estimate for the Vars of this rel. We
3578  * iteratively search for multivariate n-distinct with maximum number
3579  * of vars; assuming that each var group is independent of the others,
3580  * we multiply them together. Any remaining relvarinfos after no more
3581  * multivariate matches are found are assumed independent too, so
3582  * their individual ndistinct estimates are multiplied also.
3583  *
3584  * While iterating, count how many separate numdistinct values we
3585  * apply. We apply a fudge factor below, but only if we multiplied
3586  * more than one such values.
3587  */
3588  while (relvarinfos)
3589  {
3590  double mvndistinct;
3591 
3592  if (estimate_multivariate_ndistinct(root, rel, &relvarinfos,
3593  &mvndistinct))
3594  {
3595  reldistinct *= mvndistinct;
3596  if (relmaxndistinct < mvndistinct)
3597  relmaxndistinct = mvndistinct;
3598  relvarcount++;
3599  }
3600  else
3601  {
3602  foreach(l, relvarinfos)
3603  {
3604  GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
3605 
3606  reldistinct *= varinfo2->ndistinct;
3607  if (relmaxndistinct < varinfo2->ndistinct)
3608  relmaxndistinct = varinfo2->ndistinct;
3609  relvarcount++;
3610  }
3611 
3612  /* we're done with this relation */
3613  relvarinfos = NIL;
3614  }
3615  }
3616 
3617  /*
3618  * Sanity check --- don't divide by zero if empty relation.
3619  */
3620  Assert(IS_SIMPLE_REL(rel));
3621  if (rel->tuples > 0)
3622  {
3623  /*
3624  * Clamp to size of rel, or size of rel / 10 if multiple Vars. The
3625  * fudge factor is because the Vars are probably correlated but we
3626  * don't know by how much. We should never clamp to less than the
3627  * largest ndistinct value for any of the Vars, though, since
3628  * there will surely be at least that many groups.
3629  */
3630  double clamp = rel->tuples;
3631 
3632  if (relvarcount > 1)
3633  {
3634  clamp *= 0.1;
3635  if (clamp < relmaxndistinct)
3636  {
3637  clamp = relmaxndistinct;
3638  /* for sanity in case some ndistinct is too large: */
3639  if (clamp > rel->tuples)
3640  clamp = rel->tuples;
3641  }
3642  }
3643  if (reldistinct > clamp)
3644  reldistinct = clamp;
3645 
3646  /*
3647  * Update the estimate based on the restriction selectivity,
3648  * guarding against division by zero when reldistinct is zero.
3649  * Also skip this if we know that we are returning all rows.
3650  */
3651  if (reldistinct > 0 && rel->rows < rel->tuples)
3652  {
3653  /*
3654  * Given a table containing N rows with n distinct values in a
3655  * uniform distribution, if we select p rows at random then
3656  * the expected number of distinct values selected is
3657  *
3658  * n * (1 - product((N-N/n-i)/(N-i), i=0..p-1))
3659  *
3660  * = n * (1 - (N-N/n)! / (N-N/n-p)! * (N-p)! / N!)
3661  *
3662  * See "Approximating block accesses in database
3663  * organizations", S. B. Yao, Communications of the ACM,
3664  * Volume 20 Issue 4, April 1977 Pages 260-261.
3665  *
3666  * Alternatively, re-arranging the terms from the factorials,
3667  * this may be written as
3668  *
3669  * n * (1 - product((N-p-i)/(N-i), i=0..N/n-1))
3670  *
3671  * This form of the formula is more efficient to compute in
3672  * the common case where p is larger than N/n. Additionally,
3673  * as pointed out by Dell'Era, if i << N for all terms in the
3674  * product, it can be approximated by
3675  *
3676  * n * (1 - ((N-p)/N)^(N/n))
3677  *
3678  * See "Expected distinct values when selecting from a bag
3679  * without replacement", Alberto Dell'Era,
3680  * http://www.adellera.it/investigations/distinct_balls/.
3681  *
3682  * The condition i << N is equivalent to n >> 1, so this is a
3683  * good approximation when the number of distinct values in
3684  * the table is large. It turns out that this formula also
3685  * works well even when n is small.
3686  */
3687  reldistinct *=
3688  (1 - pow((rel->tuples - rel->rows) / rel->tuples,
3689  rel->tuples / reldistinct));
3690  }
3691  reldistinct = clamp_row_est(reldistinct);
3692 
3693  /*
3694  * Update estimate of total distinct groups.
3695  */
3696  numdistinct *= reldistinct;
3697  }
3698 
3699  varinfos = newvarinfos;
3700  } while (varinfos != NIL);
3701 
3702  /* Now we can account for the effects of any SRFs */
3703  numdistinct *= srf_multiplier;
3704 
3705  /* Round off */
3706  numdistinct = ceil(numdistinct);
3707 
3708  /* Guard against out-of-range answers */
3709  if (numdistinct > input_rows)
3710  numdistinct = input_rows;
3711  if (numdistinct < 1.0)
3712  numdistinct = 1.0;
3713 
3714  return numdistinct;
3715 }
#define NIL
Definition: pg_list.h:69
#define PVC_RECURSE_AGGREGATES
Definition: var.h:21
HeapTuple statsTuple
Definition: selfuncs.h:71
double expression_returns_set_rows(Node *clause)
Definition: clauses.c:802
double tuples
Definition: relation.h:625
Definition: nodes.h:512
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:957
double ndistinct
Definition: selfuncs.c:3276
#define PVC_RECURSE_PLACEHOLDERS
Definition: var.h:26
#define IS_SIMPLE_REL(rel)
Definition: relation.h:561
#define linitial(l)
Definition: pg_list.h:111
bool list_member_int(const List *list, int datum)
Definition: list.c:485
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define PVC_RECURSE_WINDOWFUNCS
Definition: var.h:23
static bool estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel, List **varinfos, double *ndistinct)
Definition: selfuncs.c:3885
#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:3280
double rows
Definition: relation.h:588
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:4728
#define Assert(condition)
Definition: c.h:680
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
#define for_each_cell(cell, initcell)
Definition: pg_list.h:169
#define BOOLOID
Definition: pg_type.h:288
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
int i
double clamp_row_est(double nrows)
Definition: costsize.c:177
Definition: pg_list.h:45
RelOptInfo * rel
Definition: selfuncs.c:3275

◆ examine_variable()

void examine_variable ( PlannerInfo root,
Node node,
int  varRelid,
VariableStatData vardata 
)

Definition at line 4728 of file selfuncs.c.

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

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

4730 {
4731  Node *basenode;
4732  Relids varnos;
4733  RelOptInfo *onerel;
4734 
4735  /* Make sure we don't return dangling pointers in vardata */
4736  MemSet(vardata, 0, sizeof(VariableStatData));
4737 
4738  /* Save the exposed type of the expression */
4739  vardata->vartype = exprType(node);
4740 
4741  /* Look inside any binary-compatible relabeling */
4742 
4743  if (IsA(node, RelabelType))
4744  basenode = (Node *) ((RelabelType *) node)->arg;
4745  else
4746  basenode = node;
4747 
4748  /* Fast path for a simple Var */
4749 
4750  if (IsA(basenode, Var) &&
4751  (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
4752  {
4753  Var *var = (Var *) basenode;
4754 
4755  /* Set up result fields other than the stats tuple */
4756  vardata->var = basenode; /* return Var without relabeling */
4757  vardata->rel = find_base_rel(root, var->varno);
4758  vardata->atttype = var->vartype;
4759  vardata->atttypmod = var->vartypmod;
4760  vardata->isunique = has_unique_index(vardata->rel, var->varattno);
4761 
4762  /* Try to locate some stats */
4763  examine_simple_variable(root, var, vardata);
4764 
4765  return;
4766  }
4767 
4768  /*
4769  * Okay, it's a more complicated expression. Determine variable
4770  * membership. Note that when varRelid isn't zero, only vars of that
4771  * relation are considered "real" vars.
4772  */
4773  varnos = pull_varnos(basenode);
4774 
4775  onerel = NULL;
4776 
4777  switch (bms_membership(varnos))
4778  {
4779  case BMS_EMPTY_SET:
4780  /* No Vars at all ... must be pseudo-constant clause */
4781  break;
4782  case BMS_SINGLETON:
4783  if (varRelid == 0 || bms_is_member(varRelid, varnos))
4784  {
4785  onerel = find_base_rel(root,
4786  (varRelid ? varRelid : bms_singleton_member(varnos)));
4787  vardata->rel = onerel;
4788  node = basenode; /* strip any relabeling */
4789  }
4790  /* else treat it as a constant */
4791  break;
4792  case BMS_MULTIPLE:
4793  if (varRelid == 0)
4794  {
4795  /* treat it as a variable of a join relation */
4796  vardata->rel = find_join_rel(root, varnos);
4797  node = basenode; /* strip any relabeling */
4798  }
4799  else if (bms_is_member(varRelid, varnos))
4800  {
4801  /* ignore the vars belonging to other relations */
4802  vardata->rel = find_base_rel(root, varRelid);
4803  node = basenode; /* strip any relabeling */
4804  /* note: no point in expressional-index search here */
4805  }
4806  /* else treat it as a constant */
4807  break;
4808  }
4809 
4810  bms_free(varnos);
4811 
4812  vardata->var = node;
4813  vardata->atttype = exprType(node);
4814  vardata->atttypmod = exprTypmod(node);
4815 
4816  if (onerel)
4817  {
4818  /*
4819  * We have an expression in vars of a single relation. Try to match
4820  * it to expressional index columns, in hopes of finding some
4821  * statistics.
4822  *
4823  * XXX it's conceivable that there are multiple matches with different
4824  * index opfamilies; if so, we need to pick one that matches the
4825  * operator we are estimating for. FIXME later.
4826  */
4827  ListCell *ilist;
4828 
4829  foreach(ilist, onerel->indexlist)
4830  {
4831  IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
4832  ListCell *indexpr_item;
4833  int pos;
4834 
4835  indexpr_item = list_head(index->indexprs);
4836  if (indexpr_item == NULL)
4837  continue; /* no expressions here... */
4838 
4839  for (pos = 0; pos < index->ncolumns; pos++)
4840  {
4841  if (index->indexkeys[pos] == 0)
4842  {
4843  Node *indexkey;
4844 
4845  if (indexpr_item == NULL)
4846  elog(ERROR, "too few entries in indexprs list");
4847  indexkey = (Node *) lfirst(indexpr_item);
4848  if (indexkey && IsA(indexkey, RelabelType))
4849  indexkey = (Node *) ((RelabelType *) indexkey)->arg;
4850  if (equal(node, indexkey))
4851  {
4852  /*
4853  * Found a match ... is it a unique index? Tests here
4854  * should match has_unique_index().
4855  */
4856  if (index->unique &&
4857  index->ncolumns == 1 &&
4858  (index->indpred == NIL || index->predOK))
4859  vardata->isunique = true;
4860 
4861  /*
4862  * Has it got stats? We only consider stats for
4863  * non-partial indexes, since partial indexes probably
4864  * don't reflect whole-relation statistics; the above
4865  * check for uniqueness is the only info we take from
4866  * a partial index.
4867  *
4868  * An index stats hook, however, must make its own
4869  * decisions about what to do with partial indexes.
4870  */
4871  if (get_index_stats_hook &&
4872  (*get_index_stats_hook) (root, index->indexoid,
4873  pos + 1, vardata))
4874  {
4875  /*
4876  * The hook took control of acquiring a stats
4877  * tuple. If it did supply a tuple, it'd better
4878  * have supplied a freefunc.
4879  */
4880  if (HeapTupleIsValid(vardata->statsTuple) &&
4881  !vardata->freefunc)
4882  elog(ERROR, "no function provided to release variable stats with");
4883  }
4884  else if (index->indpred == NIL)
4885  {
4886  vardata->statsTuple =
4888  ObjectIdGetDatum(index->indexoid),
4889  Int16GetDatum(pos + 1),
4890  BoolGetDatum(false));
4891  vardata->freefunc = ReleaseSysCache;
4892 
4893  if (HeapTupleIsValid(vardata->statsTuple))
4894  {
4895  /* Get index's table for permission check */
4896  RangeTblEntry *rte;
4897 
4898  rte = planner_rt_fetch(index->rel->relid, root);
4899  Assert(rte->rtekind == RTE_RELATION);
4900 
4901  /*
4902  * For simplicity, we insist on the whole
4903  * table being selectable, rather than trying
4904  * to identify which column(s) the index
4905  * depends on.
4906  */
4907  vardata->acl_ok =
4909  ACL_SELECT) == ACLCHECK_OK);
4910  }
4911  else
4912  {
4913  /* suppress leakproofness checks later */
4914  vardata->acl_ok = true;
4915  }
4916  }
4917  if (vardata->statsTuple)
4918  break;
4919  }
4920  indexpr_item = lnext(indexpr_item);
4921  }
4922  }
4923  if (vardata->statsTuple)
4924  break;
4925  }
4926  }
4927 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
bool predOK
Definition: relation.h:752
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:342
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2984
HeapTuple statsTuple
Definition: selfuncs.h:71
Oid GetUserId(void)
Definition: miscinit.c:284
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
RelOptInfo * rel
Definition: selfuncs.h:70
#define Int16GetDatum(X)
Definition: postgres.h:457
Definition: nodes.h:512
void(* freefunc)(HeapTuple tuple)
Definition: selfuncs.h:73
#define MemSet(start, val, len)
Definition: c.h:863
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
static void examine_simple_variable(PlannerInfo *root, Var *var, VariableStatData *vardata)
Definition: selfuncs.c:4939
int32 atttypmod
Definition: selfuncs.h:76
bool unique
Definition: relation.h:753
Definition: type.h:89
RelOptInfo * rel
Definition: relation.h:721
#define planner_rt_fetch(rti, root)
Definition: relation.h:328
bool has_unique_index(RelOptInfo *rel, AttrNumber attno)
Definition: plancat.c:1756
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Oid vartype
Definition: primnodes.h:170
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Definition: syscache.c:1134
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
int ncolumns
Definition: relation.h:729
#define lnext(lc)
Definition: pg_list.h:105
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: relation.h:613
Index varno
Definition: primnodes.h:166
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:634
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define ACL_SELECT
Definition: parsenodes.h:73
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:526
List * indexlist
Definition: relation.h:622
#define BoolGetDatum(X)
Definition: postgres.h:408
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:680
#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:156
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4480
RTEKind rtekind
Definition: parsenodes.h:951
void * arg
int * indexkeys
Definition: relation.h:730
#define elog
Definition: elog.h:219
Oid indexoid
Definition: relation.h:719
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:277
List * indpred
Definition: relation.h:742
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
List * indexprs
Definition: relation.h:741
int32 vartypmod
Definition: primnodes.h:171

◆ genericcostestimate()

void genericcostestimate ( PlannerInfo root,
IndexPath path,
double  loop_count,
List qinfos,
GenericCosts costs 
)

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

6644 {
6645  IndexOptInfo *index = path->indexinfo;
6646  List *indexQuals = path->indexquals;
6647  List *indexOrderBys = path->indexorderbys;
6648  Cost indexStartupCost;
6649  Cost indexTotalCost;
6650  Selectivity indexSelectivity;
6651  double indexCorrelation;
6652  double numIndexPages;
6653  double numIndexTuples;
6654  double spc_random_page_cost;
6655  double num_sa_scans;
6656  double num_outer_scans;
6657  double num_scans;
6658  double qual_op_cost;
6659  double qual_arg_cost;
6660  List *selectivityQuals;
6661  ListCell *l;
6662 
6663  /*
6664  * If the index is partial, AND the index predicate with the explicitly
6665  * given indexquals to produce a more accurate idea of the index
6666  * selectivity.
6667  */
6668  selectivityQuals = add_predicate_to_quals(index, indexQuals);
6669 
6670  /*
6671  * Check for ScalarArrayOpExpr index quals, and estimate the number of
6672  * index scans that will be performed.
6673  */
6674  num_sa_scans = 1;
6675  foreach(l, indexQuals)
6676  {
6677  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
6678 
6679  if (IsA(rinfo->clause, ScalarArrayOpExpr))
6680  {
6681  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
6682  int alength = estimate_array_length(lsecond(saop->args));
6683 
6684  if (alength > 1)
6685  num_sa_scans *= alength;
6686  }
6687  }
6688 
6689  /* Estimate the fraction of main-table tuples that will be visited */
6690  indexSelectivity = clauselist_selectivity(root, selectivityQuals,
6691  index->rel->relid,
6692  JOIN_INNER,
6693  NULL);
6694 
6695  /*
6696  * If caller didn't give us an estimate, estimate the number of index
6697  * tuples that will be visited. We do it in this rather peculiar-looking
6698  * way in order to get the right answer for partial indexes.
6699  */
6700  numIndexTuples = costs->numIndexTuples;
6701  if (numIndexTuples <= 0.0)
6702  {
6703  numIndexTuples = indexSelectivity * index->rel->tuples;
6704 
6705  /*
6706  * The above calculation counts all the tuples visited across all
6707  * scans induced by ScalarArrayOpExpr nodes. We want to consider the
6708  * average per-indexscan number, so adjust. This is a handy place to
6709  * round to integer, too. (If caller supplied tuple estimate, it's
6710  * responsible for handling these considerations.)
6711  */
6712  numIndexTuples = rint(numIndexTuples / num_sa_scans);
6713  }
6714 
6715  /*
6716  * We can bound the number of tuples by the index size in any case. Also,
6717  * always estimate at least one tuple is touched, even when
6718  * indexSelectivity estimate is tiny.
6719  */
6720  if (numIndexTuples > index->tuples)
6721  numIndexTuples = index->tuples;
6722  if (numIndexTuples < 1.0)
6723  numIndexTuples = 1.0;
6724 
6725  /*
6726  * Estimate the number of index pages that will be retrieved.
6727  *
6728  * We use the simplistic method of taking a pro-rata fraction of the total
6729  * number of index pages. In effect, this counts only leaf pages and not
6730  * any overhead such as index metapage or upper tree levels.
6731  *
6732  * In practice access to upper index levels is often nearly free because
6733  * those tend to stay in cache under load; moreover, the cost involved is
6734  * highly dependent on index type. We therefore ignore such costs here
6735  * and leave it to the caller to add a suitable charge if needed.
6736  */
6737  if (index->pages > 1 && index->tuples > 1)
6738  numIndexPages = ceil(numIndexTuples * index->pages / index->tuples);
6739  else
6740  numIndexPages = 1.0;
6741 
6742  /* fetch estimated page cost for tablespace containing index */
6744  &spc_random_page_cost,
6745  NULL);
6746 
6747  /*
6748  * Now compute the disk access costs.
6749  *
6750  * The above calculations are all per-index-scan. However, if we are in a
6751  * nestloop inner scan, we can expect the scan to be repeated (with
6752  * different search keys) for each row of the outer relation. Likewise,
6753  * ScalarArrayOpExpr quals result in multiple index scans. This creates
6754  * the potential for cache effects to reduce the number of disk page
6755  * fetches needed. We want to estimate the average per-scan I/O cost in
6756  * the presence of caching.
6757  *
6758  * We use the Mackert-Lohman formula (see costsize.c for details) to
6759  * estimate the total number of page fetches that occur. While this
6760  * wasn't what it was designed for, it seems a reasonable model anyway.
6761  * Note that we are counting pages not tuples anymore, so we take N = T =
6762  * index size, as if there were one "tuple" per page.
6763  */
6764  num_outer_scans = loop_count;
6765  num_scans = num_sa_scans * num_outer_scans;
6766 
6767  if (num_scans > 1)
6768  {
6769  double pages_fetched;
6770 
6771  /* total page fetches ignoring cache effects */
6772  pages_fetched = numIndexPages * num_scans;
6773 
6774  /* use Mackert and Lohman formula to adjust for cache effects */
6775  pages_fetched = index_pages_fetched(pages_fetched,
6776  index->pages,
6777  (double) index->pages,
6778  root);
6779 
6780  /*
6781  * Now compute the total disk access cost, and then report a pro-rated
6782  * share for each outer scan. (Don't pro-rate for ScalarArrayOpExpr,
6783  * since that's internal to the indexscan.)
6784  */
6785  indexTotalCost = (pages_fetched * spc_random_page_cost)
6786  / num_outer_scans;
6787  }
6788  else
6789  {
6790  /*
6791  * For a single index scan, we just charge spc_random_page_cost per
6792  * page touched.
6793  */
6794  indexTotalCost = numIndexPages * spc_random_page_cost;
6795  }
6796 
6797  /*
6798  * CPU cost: any complex expressions in the indexquals will need to be
6799  * evaluated once at the start of the scan to reduce them to runtime keys
6800  * to pass to the index AM (see nodeIndexscan.c). We model the per-tuple
6801  * CPU costs as cpu_index_tuple_cost plus one cpu_operator_cost per
6802  * indexqual operator. Because we have numIndexTuples as a per-scan
6803  * number, we have to multiply by num_sa_scans to get the correct result
6804  * for ScalarArrayOpExpr cases. Similarly add in costs for any index
6805  * ORDER BY expressions.
6806  *
6807  * Note: this neglects the possible costs of rechecking lossy operators.
6808  * Detecting that that might be needed seems more expensive than it's
6809  * worth, though, considering all the other inaccuracies here ...
6810  */
6811  qual_arg_cost = other_operands_eval_cost(root, qinfos) +
6812  orderby_operands_eval_cost(root, path);
6813  qual_op_cost = cpu_operator_cost *
6814  (list_length(indexQuals) + list_length(indexOrderBys));
6815 
6816  indexStartupCost = qual_arg_cost;
6817  indexTotalCost += qual_arg_cost;
6818  indexTotalCost += numIndexTuples * num_sa_scans * (cpu_index_tuple_cost + qual_op_cost);
6819 
6820  /*
6821  * Generic assumption about index correlation: there isn't any.
6822  */
6823  indexCorrelation = 0.0;
6824 
6825  /*
6826  * Return everything to caller.
6827  */
6828  costs->indexStartupCost = indexStartupCost;
6829  costs->indexTotalCost = indexTotalCost;
6830  costs->indexSelectivity = indexSelectivity;
6831  costs->indexCorrelation = indexCorrelation;
6832  costs->numIndexPages = numIndexPages;
6833  costs->numIndexTuples = numIndexTuples;
6834  costs->spc_random_page_cost = spc_random_page_cost;
6835  costs->num_sa_scans = num_sa_scans;
6836 }
Selectivity indexSelectivity
Definition: selfuncs.h:131
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
IndexOptInfo * indexinfo
Definition: relation.h:1119
double tuples
Definition: relation.h:625
static List * add_predicate_to_quals(IndexOptInfo *index, List *indexQuals)
Definition: selfuncs.c:6858
Oid reltablespace
Definition: relation.h:720
static Cost other_operands_eval_cost(PlannerInfo *root, List *qinfos)
Definition: selfuncs.c:6585
double Selectivity
Definition: nodes.h:642
double tuples
Definition: relation.h:725
#define lsecond(l)
Definition: pg_list.h:116
static Cost orderby_operands_eval_cost(PlannerInfo *root, IndexPath *path)
Definition: selfuncs.c:6610
Definition: type.h:89
BlockNumber pages
Definition: relation.h:724
List * indexquals
Definition: relation.h:1121
int estimate_array_length(Node *arrayexpr)
Definition: selfuncs.c:2167
RelOptInfo * rel
Definition: relation.h:721
double num_sa_scans
Definition: selfuncs.h:138
double cpu_operator_cost
Definition: costsize.c:108
Cost indexTotalCost
Definition: selfuncs.h:130
double rint(double x)
Definition: rint.c:22
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition: spccache.c:182
Index relid
Definition: relation.h:613
Expr * clause
Definition: relation.h:1841
double indexCorrelation
Definition: selfuncs.h:132
List * indexorderbys
Definition: relation.h:1123
double spc_random_page_cost
Definition: selfuncs.h:137
double numIndexTuples
Definition: selfuncs.h:136
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
Cost indexStartupCost
Definition: selfuncs.h:129
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:99
Definition: pg_list.h:45
double cpu_index_tuple_cost
Definition: costsize.c:107
double index_pages_fetched(double tuples_fetched, BlockNumber pages, double index_pages, PlannerInfo *root)
Definition: costsize.c:817
double Cost
Definition: nodes.h:643
double numIndexPages
Definition: selfuncs.h:135

◆ get_join_variables()

void get_join_variables ( PlannerInfo root,
List args,
SpecialJoinInfo sjinfo,
VariableStatData vardata1,
VariableStatData vardata2,
bool join_is_reversed 
)

Definition at line 4666 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(), neqjoinsel(), and networkjoinsel().

4669 {
4670  Node *left,
4671  *right;
4672 
4673  if (list_length(args) != 2)
4674  elog(ERROR, "join operator should take two arguments");
4675 
4676  left = (Node *) linitial(args);
4677  right = (Node *) lsecond(args);
4678 
4679  examine_variable(root, left, 0, vardata1);
4680  examine_variable(root, right, 0, vardata2);
4681 
4682  if (vardata1->rel &&
4683  bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
4684  *join_is_reversed = true; /* var1 is on RHS */
4685  else if (vardata2->rel &&
4686  bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
4687  *join_is_reversed = true; /* var2 is on LHS */
4688  else
4689  *join_is_reversed = false;
4690 }
RelOptInfo * rel
Definition: selfuncs.h:70
Definition: nodes.h:512
#define lsecond(l)
Definition: pg_list.h:116
Relids syn_lefthand
Definition: relation.h:2015
Relids syn_righthand
Definition: relation.h:2016
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Relids relids
Definition: relation.h:585
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4728
static int list_length(const List *l)
Definition: pg_list.h:89
#define elog
Definition: elog.h:219

◆ get_restriction_variable()

bool get_restriction_variable ( PlannerInfo root,
List args,
int  varRelid,
VariableStatData vardata,
Node **  other,
bool varonleft 
)

Definition at line 4606 of file selfuncs.c.

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

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

4609 {
4610  Node *left,
4611  *right;
4612  VariableStatData rdata;
4613 
4614  /* Fail if not a binary opclause (probably shouldn't happen) */
4615  if (list_length(args) != 2)
4616  return false;
4617 
4618  left = (Node *) linitial(args);
4619  right = (Node *) lsecond(args);
4620 
4621  /*
4622  * Examine both sides. Note that when varRelid is nonzero, Vars of other
4623  * relations will be treated as pseudoconstants.
4624  */
4625  examine_variable(root, left, varRelid, vardata);
4626  examine_variable(root, right, varRelid, &rdata);
4627 
4628  /*
4629  * If one side is a variable and the other not, we win.
4630  */
4631  if (vardata->rel && rdata.rel == NULL)
4632  {
4633  *varonleft = true;
4634  *other = estimate_expression_value(root, rdata.var);
4635  /* Assume we need no ReleaseVariableStats(rdata) here */
4636  return true;
4637  }
4638 
4639  if (vardata->rel == NULL && rdata.rel)
4640  {
4641  *varonleft = false;
4642  *other = estimate_expression_value(root, vardata->var);
4643  /* Assume we need no ReleaseVariableStats(*vardata) here */
4644  *vardata = rdata;
4645  return true;
4646  }
4647 
4648  /* Oops, clause has wrong structure (probably var op var) */
4649  ReleaseVariableStats(*vardata);
4650  ReleaseVariableStats(rdata);
4651 
4652  return false;
4653 }
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2492
RelOptInfo * rel
Definition: selfuncs.h:70
Definition: nodes.h:512
#define lsecond(l)
Definition: pg_list.h:116
#define linitial(l)
Definition: pg_list.h:111
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4728
static int list_length(const List *l)
Definition: pg_list.h:89
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81

◆ get_variable_numdistinct()

double get_variable_numdistinct ( VariableStatData vardata,
bool isdefault 
)

Definition at line 5135 of file selfuncs.c.

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

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

5136 {
5137  double stadistinct;
5138  double stanullfrac = 0.0;
5139  double ntuples;
5140 
5141  *isdefault = false;
5142 
5143  /*
5144  * Determine the stadistinct value to use. There are cases where we can
5145  * get an estimate even without a pg_statistic entry, or can get a better
5146  * value than is in pg_statistic. Grab stanullfrac too if we can find it
5147  * (otherwise, assume no nulls, for lack of any better idea).
5148  */
5149  if (HeapTupleIsValid(vardata->statsTuple))
5150  {
5151  /* Use the pg_statistic entry */
5152  Form_pg_statistic stats;
5153 
5154  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
5155  stadistinct = stats->stadistinct;
5156  stanullfrac = stats->stanullfrac;
5157  }
5158  else if (vardata->vartype == BOOLOID)
5159  {
5160  /*
5161  * Special-case boolean columns: presumably, two distinct values.
5162  *
5163  * Are there any other datatypes we should wire in special estimates
5164  * for?
5165  */
5166  stadistinct = 2.0;
5167  }
5168  else if (vardata->rel && vardata->rel->rtekind == RTE_VALUES)
5169  {
5170  /*
5171  * If the Var represents a column of a VALUES RTE, assume it's unique.
5172  * This could of course be very wrong, but it should tend to be true
5173  * in well-written queries. We could consider examining the VALUES'
5174  * contents to get some real statistics; but that only works if the
5175  * entries are all constants, and it would be pretty expensive anyway.
5176  */
5177  stadistinct = -1.0; /* unique (and all non null) */
5178  }
5179  else
5180  {
5181  /*
5182  * We don't keep statistics for system columns, but in some cases we
5183  * can infer distinctness anyway.
5184  */
5185  if (vardata->var && IsA(vardata->var, Var))
5186  {
5187  switch (((Var *) vardata->var)->varattno)
5188  {
5191  stadistinct = -1.0; /* unique (and all non null) */
5192  break;
5194  stadistinct = 1.0; /* only 1 value */
5195  break;
5196  default:
5197  stadistinct = 0.0; /* means "unknown" */
5198  break;
5199  }
5200  }
5201  else
5202  stadistinct = 0.0; /* means "unknown" */
5203 
5204  /*
5205  * XXX consider using estimate_num_groups on expressions?
5206  */
5207  }
5208 
5209  /*
5210  * If there is a unique index or DISTINCT clause for the variable, assume
5211  * it is unique no matter what pg_statistic says; the statistics could be
5212  * out of date, or we might have found a partial unique index that proves
5213  * the var is unique for this query. However, we'd better still believe
5214  * the null-fraction statistic.
5215  */
5216  if (vardata->isunique)
5217  stadistinct = -1.0 * (1.0 - stanullfrac);
5218 
5219  /*
5220  * If we had an absolute estimate, use that.
5221  */
5222  if (stadistinct > 0.0)
5223  return clamp_row_est(stadistinct);
5224 
5225  /*
5226  * Otherwise we need to get the relation size; punt if not available.
5227  */
5228  if (vardata->rel == NULL)
5229  {
5230  *isdefault = true;
5231  return DEFAULT_NUM_DISTINCT;
5232  }
5233  ntuples = vardata->rel->tuples;
5234  if (ntuples <= 0.0)
5235  {
5236  *isdefault = true;
5237  return DEFAULT_NUM_DISTINCT;
5238  }
5239 
5240  /*
5241  * If we had a relative estimate, use that.
5242  */
5243  if (stadistinct < 0.0)
5244  return clamp_row_est(-stadistinct * ntuples);
5245 
5246  /*
5247  * With no data, estimate ndistinct = ntuples if the table is small, else
5248  * use default. We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
5249  * that the behavior isn't discontinuous.
5250  */
5251  if (ntuples < DEFAULT_NUM_DISTINCT)
5252  return clamp_row_est(ntuples);
5253 
5254  *isdefault = true;
5255  return DEFAULT_NUM_DISTINCT;
5256 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
HeapTuple statsTuple
Definition: selfuncs.h:71
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
double tuples
Definition: relation.h:625
RelOptInfo * rel
Definition: selfuncs.h:70
Definition: primnodes.h:163
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define TableOidAttributeNumber
Definition: sysattr.h:27
RTEKind rtekind
Definition: relation.h:615
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#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:177

◆ histogram_selectivity()

double histogram_selectivity ( VariableStatData vardata,
FmgrInfo opproc,
Datum  constval,
bool  varonleft,
int  min_hist_size,
int  n_skip,
int *  hist_size 
)

Definition at line 709 of file selfuncs.c.

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

Referenced by ltreeparentsel(), and patternsel().

713 {
714  double result;
715  AttStatsSlot sslot;
716 
717  /* check sanity of parameters */
718  Assert(n_skip >= 0);
719  Assert(min_hist_size > 2 * n_skip);
720 
721  if (HeapTupleIsValid(vardata->statsTuple) &&
722  statistic_proc_security_check(vardata, opproc->fn_oid) &&
723  get_attstatsslot(&sslot, vardata->statsTuple,
726  {
727  *hist_size = sslot.nvalues;
728  if (sslot.nvalues >= min_hist_size)
729  {
730  int nmatch = 0;
731  int i;
732 
733  for (i = n_skip; i < sslot.nvalues - n_skip; i++)
734  {
735  if (varonleft ?
738  sslot.values[i],
739  constval)) :
742  constval,
743  sslot.values[i])))
744  nmatch++;
745  }
746  result = ((double) nmatch) / ((double) (sslot.nvalues - 2 * n_skip));
747  }
748  else
749  result = -1;
750  free_attstatsslot(&sslot);
751  }
752  else
753  {
754  *hist_size = 0;
755  result = -1;
756  }
757 
758  return result;
759 }
#define ATTSTATSSLOT_VALUES
Definition: lsyscache.h:39
HeapTuple statsTuple
Definition: selfuncs.h:71
#define STATISTIC_KIND_HISTOGRAM
Definition: pg_statistic.h:222
bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
Definition: selfuncs.c:5106
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1042
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
#define DatumGetBool(X)
Definition: postgres.h:399
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:2928
#define Assert(condition)
Definition: c.h:680
Datum * values
Definition: lsyscache.h:49
int i
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3044

◆ make_greater_string()

Const* make_greater_string ( const Const str_const,
FmgrInfo ltproc,
Oid  collation 
)

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

6265 {
6266  Oid datatype = str_const->consttype;
6267  char *workstr;
6268  int len;
6269  Datum cmpstr;
6270  text *cmptxt = NULL;
6271  mbcharacter_incrementer charinc;
6272 
6273  /*
6274  * Get a modifiable copy of the prefix string in C-string format, and set
6275  * up the string we will compare to as a Datum. In C locale this can just
6276  * be the given prefix string, otherwise we need to add a suffix. Types
6277  * NAME and BYTEA sort bytewise so they don't need a suffix either.
6278  */
6279  if (datatype == NAMEOID)
6280  {
6282  str_const->constvalue));
6283  len = strlen(workstr);
6284  cmpstr = str_const->constvalue;
6285  }
6286  else if (datatype == BYTEAOID)
6287  {
6288  bytea *bstr = DatumGetByteaPP(str_const->constvalue);
6289 
6290  len = VARSIZE_ANY_EXHDR(bstr);
6291  workstr = (char *) palloc(len);
6292  memcpy(workstr, VARDATA_ANY(bstr), len);
6293  Assert((Pointer) bstr == DatumGetPointer(str_const->constvalue));
6294  cmpstr = str_const->constvalue;
6295  }
6296  else
6297  {
6298  workstr = TextDatumGetCString(str_const->constvalue);
6299  len = strlen(workstr);
6300  if (lc_collate_is_c(collation) || len == 0)
6301  cmpstr = str_const->constvalue;
6302  else
6303  {
6304  /* If first time through, determine the suffix to use */
6305  static char suffixchar = 0;
6306  static Oid suffixcollation = 0;
6307 
6308  if (!suffixchar || suffixcollation != collation)
6309  {
6310  char *best;
6311 
6312  best = "Z";
6313  if (varstr_cmp(best, 1, "z", 1, collation) < 0)
6314  best = "z";
6315  if (varstr_cmp(best, 1, "y", 1, collation) < 0)
6316  best = "y";
6317  if (varstr_cmp(best, 1, "9", 1, collation) < 0)
6318  best = "9";
6319  suffixchar = *best;
6320  suffixcollation = collation;
6321  }
6322 
6323  /* And build the string to compare to */
6324  cmptxt = (text *) palloc(VARHDRSZ + len + 1);
6325  SET_VARSIZE(cmptxt, VARHDRSZ + len + 1);
6326  memcpy(VARDATA(cmptxt), workstr, len);
6327  *(VARDATA(cmptxt) + len) = suffixchar;
6328  cmpstr = PointerGetDatum(cmptxt);
6329  }
6330  }
6331 
6332  /* Select appropriate character-incrementer function */
6333  if (datatype == BYTEAOID)
6334  charinc = byte_increment;
6335  else
6337 
6338  /* And search ... */
6339  while (len > 0)
6340  {
6341  int charlen;
6342  unsigned char *lastchar;
6343 
6344  /* Identify the last character --- for bytea, just the last byte */
6345  if (datatype == BYTEAOID)
6346  charlen = 1;
6347  else
6348  charlen = len - pg_mbcliplen(workstr, len, len - 1);
6349  lastchar = (unsigned char *) (workstr + len - charlen);
6350 
6351  /*
6352  * Try to generate a larger string by incrementing the last character
6353  * (for BYTEA, we treat each byte as a character).
6354  *
6355  * Note: the incrementer function is expected to return true if it's
6356  * generated a valid-per-the-encoding new character, otherwise false.
6357  * The contents of the character on false return are unspecified.
6358  */
6359  while (charinc(lastchar, charlen))
6360  {
6361  Const *workstr_const;
6362 
6363  if (datatype == BYTEAOID)
6364  workstr_const = string_to_bytea_const(workstr, len);
6365  else
6366  workstr_const = string_to_const(workstr, datatype);
6367 
6368  if (DatumGetBool(FunctionCall2Coll(ltproc,
6369  collation,
6370  cmpstr,
6371  workstr_const->constvalue)))
6372  {
6373  /* Successfully made a string larger than cmpstr */
6374  if (cmptxt)
6375  pfree(cmptxt);
6376  pfree(workstr);
6377  return workstr_const;
6378  }
6379 
6380  /* No good, release unusable value and try again */
6381  pfree(DatumGetPointer(workstr_const->constvalue));
6382  pfree(workstr_const);
6383  }
6384 
6385  /*
6386  * No luck here, so truncate off the last character and try to
6387  * increment the next one.
6388  */
6389  len -= charlen;
6390  workstr[len] = '\0';
6391  }
6392 
6393  /* Failed... */
6394  if (cmptxt)
6395  pfree(cmptxt);
6396  pfree(workstr);
6397 
6398  return NULL;
6399 }
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:503
#define DatumGetByteaPP(X)
Definition: fmgr.h:255
static Const * string_to_bytea_const(const char *str, size_t str_len)
Definition: selfuncs.c:6470
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1042
unsigned int Oid
Definition: postgres_ext.h:31
static bool byte_increment(unsigned char *ptr, int len)
Definition: selfuncs.c:6214
static Const * string_to_const(const char *str, Oid datatype)
Definition: selfuncs.c:6427
Oid consttype
Definition: primnodes.h:192
void pfree(void *pointer)
Definition: mcxt.c:936
char * Pointer
Definition: c.h:283
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1128
#define DatumGetCString(X)
Definition: postgres.h:572
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:820
#define DatumGetBool(X)
Definition: postgres.h:399
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define Assert(condition)
Definition: c.h:680
#define BYTEAOID
Definition: pg_type.h:292
#define DatumGetPointer(X)
Definition: postgres.h:555
bool(* mbcharacter_incrementer)(unsigned char *mbstr, int len)
Definition: pg_wchar.h:369
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:835
mbcharacter_incrementer pg_database_encoding_character_incrementer(void)
Definition: wchar.c:1842
Definition: c.h:497
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
Datum nameout(PG_FUNCTION_ARGS)
Definition: name.c:69

◆ mcv_selectivity()

double mcv_selectivity ( VariableStatData vardata,
FmgrInfo opproc,
Datum  constval,
bool  varonleft,
double *  sumcommonp 
)

Definition at line 637 of file selfuncs.c.

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

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

640 {
641  double mcv_selec,
642  sumcommon;
643  AttStatsSlot sslot;
644  int i;
645 
646  mcv_selec = 0.0;
647  sumcommon = 0.0;
648 
649  if (HeapTupleIsValid(vardata->statsTuple) &&
650  statistic_proc_security_check(vardata, opproc->fn_oid) &&
651  get_attstatsslot(&sslot, vardata->statsTuple,
654  {
655  for (i = 0; i < sslot.nvalues; i++)
656  {
657  if (varonleft ?
660  sslot.values[i],
661  constval)) :
664  constval,
665  sslot.values[i])))
666  mcv_selec += sslot.numbers[i];
667  sumcommon += sslot.numbers[i];
668  }
669  free_attstatsslot(&sslot);
670  }
671 
672  *sumcommonp = sumcommon;
673  return mcv_selec;
674 }
#define ATTSTATSSLOT_VALUES
Definition: lsyscache.h:39
HeapTuple statsTuple
Definition: selfuncs.h:71
bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
Definition: selfuncs.c:5106
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1042
#define ATTSTATSSLOT_NUMBERS
Definition: lsyscache.h:40
float4 * numbers
Definition: lsyscache.h:52
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
#define DatumGetBool(X)
Definition: postgres.h:399
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:204
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:2928
Datum * values
Definition: lsyscache.h:49
int i
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3044

◆ mergejoinscansel()

void mergejoinscansel ( PlannerInfo root,
Node clause,
Oid  opfamily,
int  strategy,
bool  nulls_first,
Selectivity leftstart,
Selectivity leftend,
Selectivity rightstart,
Selectivity rightend 
)

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

2978 {
2979  Node *left,
2980  *right;
2981  VariableStatData leftvar,
2982  rightvar;
2983  int op_strategy;
2984  Oid op_lefttype;
2985  Oid op_righttype;
2986  Oid opno,
2987  lsortop,
2988  rsortop,
2989  lstatop,
2990  rstatop,
2991  ltop,
2992  leop,
2993  revltop,
2994  revleop;
2995  bool isgt;
2996  Datum leftmin,
2997  leftmax,
2998  rightmin,
2999  rightmax;
3000  double selec;
3001 
3002  /* Set default results if we can't figure anything out. */
3003  /* XXX should default "start" fraction be a bit more than 0? */
3004  *leftstart = *rightstart = 0.0;
3005  *leftend = *rightend = 1.0;
3006 
3007  /* Deconstruct the merge clause */
3008  if (!is_opclause(clause))
3009  return; /* shouldn't happen */
3010  opno = ((OpExpr *) clause)->opno;
3011  left = get_leftop((Expr *) clause);
3012  right = get_rightop((Expr *) clause);
3013  if (!right)
3014  return; /* shouldn't happen */
3015 
3016  /* Look for stats for the inputs */
3017  examine_variable(root, left, 0, &leftvar);
3018  examine_variable(root, right, 0, &rightvar);
3019 
3020  /* Extract the operator's declared left/right datatypes */
3021  get_op_opfamily_properties(opno, opfamily, false,
3022  &op_strategy,
3023  &op_lefttype,
3024  &op_righttype);
3025  Assert(op_strategy == BTEqualStrategyNumber);
3026 
3027  /*
3028  * Look up the various operators we need. If we don't find them all, it
3029  * probably means the opfamily is broken, but we just fail silently.
3030  *
3031  * Note: we expect that pg_statistic histograms will be sorted by the '<'
3032  * operator, regardless of which sort direction we are considering.
3033  */
3034  switch (strategy)
3035  {
3036  case BTLessStrategyNumber:
3037  isgt = false;
3038  if (op_lefttype == op_righttype)
3039  {
3040  /* easy case */
3041  ltop = get_opfamily_member(opfamily,
3042  op_lefttype, op_righttype,
3044  leop = get_opfamily_member(opfamily,
3045  op_lefttype, op_righttype,
3047  lsortop = ltop;
3048  rsortop = ltop;
3049  lstatop = lsortop;
3050  rstatop = rsortop;
3051  revltop = ltop;
3052  revleop = leop;
3053  }
3054  else
3055  {
3056  ltop = get_opfamily_member(opfamily,
3057  op_lefttype, op_righttype,
3059  leop = get_opfamily_member(opfamily,
3060  op_lefttype, op_righttype,
3062  lsortop = get_opfamily_member(opfamily,
3063  op_lefttype, op_lefttype,
3065  rsortop = get_opfamily_member(opfamily,
3066  op_righttype, op_righttype,
3068  lstatop = lsortop;
3069  rstatop = rsortop;
3070  revltop = get_opfamily_member(opfamily,
3071  op_righttype, op_lefttype,
3073  revleop = get_opfamily_member(opfamily,
3074  op_righttype, op_lefttype,
3076  }
3077  break;
3079  /* descending-order case */
3080  isgt = true;
3081  if (op_lefttype == op_righttype)
3082  {
3083  /* easy case */
3084  ltop = get_opfamily_member(opfamily,
3085  op_lefttype, op_righttype,
3087  leop = get_opfamily_member(opfamily,
3088  op_lefttype, op_righttype,
3090  lsortop = ltop;
3091  rsortop = ltop;
3092  lstatop = get_opfamily_member(opfamily,
3093  op_lefttype, op_lefttype,
3095  rstatop = lstatop;
3096  revltop = ltop;
3097  revleop = leop;
3098  }
3099  else
3100  {
3101  ltop = get_opfamily_member(opfamily,
3102  op_lefttype, op_righttype,
3104  leop = get_opfamily_member(opfamily,
3105  op_lefttype, op_righttype,
3107  lsortop = get_opfamily_member(opfamily,
3108  op_lefttype, op_lefttype,
3110  rsortop = get_opfamily_member(opfamily,
3111  op_righttype, op_righttype,
3113  lstatop = get_opfamily_member(opfamily,
3114  op_lefttype, op_lefttype,
3116  rstatop = get_opfamily_member(opfamily,
3117  op_righttype, op_righttype,
3119  revltop = get_opfamily_member(opfamily,
3120  op_righttype, op_lefttype,
3122  revleop = get_opfamily_member(opfamily,
3123  op_righttype, op_lefttype,
3125  }
3126  break;
3127  default:
3128  goto fail; /* shouldn't get here */
3129  }
3130 
3131  if (!OidIsValid(lsortop) ||
3132  !OidIsValid(rsortop) ||
3133  !OidIsValid(lstatop) ||
3134  !OidIsValid(rstatop) ||
3135  !OidIsValid(ltop) ||
3136  !OidIsValid(leop) ||
3137  !OidIsValid(revltop) ||
3138  !OidIsValid(revleop))
3139  goto fail; /* insufficient info in catalogs */
3140 
3141  /* Try to get ranges of both inputs */
3142  if (!isgt)
3143  {
3144  if (!get_variable_range(root, &leftvar, lstatop,
3145  &leftmin, &leftmax))
3146  goto fail; /* no range available from stats */
3147  if (!get_variable_range(root, &rightvar, rstatop,
3148  &rightmin, &rightmax))
3149  goto fail; /* no range available from stats */
3150  }
3151  else
3152  {
3153  /* need to swap the max and min */
3154  if (!get_variable_range(root, &leftvar, lstatop,
3155  &leftmax, &leftmin))
3156  goto fail; /* no range available from stats */
3157  if (!get_variable_range(root, &rightvar, rstatop,
3158  &rightmax, &rightmin))
3159  goto fail; /* no range available from stats */
3160  }
3161 
3162  /*
3163  * Now, the fraction of the left variable that will be scanned is the
3164  * fraction that's <= the right-side maximum value. But only believe
3165  * non-default estimates, else stick with our 1.0.
3166  */
3167  selec = scalarineqsel(root, leop, isgt, true, &leftvar,
3168  rightmax, op_righttype);
3169  if (selec != DEFAULT_INEQ_SEL)
3170  *leftend = selec;
3171 
3172  /* And similarly for the right variable. */
3173  selec = scalarineqsel(root, revleop, isgt, true, &rightvar,
3174  leftmax, op_lefttype);
3175  if (selec != DEFAULT_INEQ_SEL)
3176  *rightend = selec;
3177 
3178  /*
3179  * Only one of the two "end" fractions can really be less than 1.0;
3180  * believe the smaller estimate and reset the other one to exactly 1.0. If
3181  * we get exactly equal estimates (as can easily happen with self-joins),
3182  * believe neither.
3183  */
3184  if (*leftend > *rightend)
3185  *leftend = 1.0;
3186  else if (*leftend < *rightend)
3187  *rightend = 1.0;
3188  else
3189  *leftend = *rightend = 1.0;
3190 
3191  /*
3192  * Also, the fraction of the left variable that will be scanned before the
3193  * first join pair is found is the fraction that's < the right-side
3194  * minimum value. But only believe non-default estimates, else stick with
3195  * our own default.
3196  */
3197  selec = scalarineqsel(root, ltop, isgt, false, &leftvar,
3198  rightmin, op_righttype);
3199  if (selec != DEFAULT_INEQ_SEL)
3200  *leftstart = selec;
3201 
3202  /* And similarly for the right variable. */
3203  selec = scalarineqsel(root, revltop, isgt, false, &rightvar,
3204  leftmin, op_lefttype);
3205  if (selec != DEFAULT_INEQ_SEL)
3206  *rightstart = selec;
3207 
3208  /*
3209  * Only one of the two "start" fractions can really be more than zero;
3210  * believe the larger estimate and reset the other one to exactly 0.0. If
3211  * we get exactly equal estimates (as can easily happen with self-joins),
3212  * believe neither.
3213  */
3214  if (*leftstart < *rightstart)
3215  *leftstart = 0.0;
3216  else if (*leftstart > *rightstart)
3217  *rightstart = 0.0;
3218  else
3219  *leftstart = *rightstart = 0.0;
3220 
3221  /*
3222  * If the sort order is nulls-first, we're going to have to skip over any
3223  * nulls too. These would not have been counted by scalarineqsel, and we
3224  * can safely add in this fraction regardless of whether we believe
3225  * scalarineqsel's results or not. But be sure to clamp the sum to 1.0!
3226  */
3227  if (nulls_first)
3228  {
3229  Form_pg_statistic stats;
3230 
3231  if (HeapTupleIsValid(leftvar.statsTuple))
3232  {
3233  stats = (Form_pg_statistic) GETSTRUCT(leftvar.statsTuple);
3234  *leftstart += stats->stanullfrac;
3235  CLAMP_PROBABILITY(*leftstart);
3236  *leftend += stats->stanullfrac;
3237  CLAMP_PROBABILITY(*leftend);
3238  }
3239  if (HeapTupleIsValid(rightvar.statsTuple))
3240  {
3241  stats = (Form_pg_statistic) GETSTRUCT(rightvar.statsTuple);
3242  *rightstart += stats->stanullfrac;
3243  CLAMP_PROBABILITY(*rightstart);
3244  *rightend += stats->stanullfrac;
3245  CLAMP_PROBABILITY(*rightend);
3246  }
3247  }
3248 
3249  /* Disbelieve start >= end, just in case that can happen */
3250  if (*leftstart >= *leftend)
3251  {
3252  *leftstart = 0.0;
3253  *leftend = 1.0;
3254  }
3255  if (*rightstart >= *rightend)
3256  {
3257  *rightstart = 0.0;
3258  *rightend = 1.0;
3259  }
3260 
3261 fail:
3262  ReleaseVariableStats(leftvar);
3263  ReleaseVariableStats(rightvar);
3264 }
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
HeapTuple statsTuple
Definition: selfuncs.h:71
#define DEFAULT_INEQ_SEL
Definition: selfuncs.h:37
Definition: nodes.h:512
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:586
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
#define is_opclause(clause)
Definition: clauses.h:20
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
uintptr_t Datum
Definition: postgres.h:372
static double scalarineqsel(PlannerInfo *root, Oid operator, bool isgt, bool iseq, VariableStatData *vardata, Datum constval, Oid consttype)
Definition: selfuncs.c:562
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4728
#define Assert(condition)
Definition: c.h:680
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:133
static bool get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop, Datum *min, Datum *max)
Definition: selfuncs.c:5268
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32

◆ nulltestsel()

Selectivity nulltestsel ( PlannerInfo root,
NullTestType  nulltesttype,
Node arg,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo 
)

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

1741 {
1742  VariableStatData vardata;
1743  double selec;
1744 
1745  examine_variable(root, arg, varRelid, &vardata);
1746 
1747  if (HeapTupleIsValid(vardata.statsTuple))
1748  {
1749  Form_pg_statistic stats;
1750  double freq_null;
1751 
1752  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1753  freq_null = stats->stanullfrac;
1754 
1755  switch (nulltesttype)
1756  {
1757  case IS_NULL:
1758 
1759  /*
1760  * Use freq_null directly.
1761  */
1762  selec = freq_null;
1763  break;
1764  case IS_NOT_NULL:
1765 
1766  /*
1767  * Select not unknown (not null) values. Calculate from
1768  * freq_null.
1769  */
1770  selec = 1.0 - freq_null;
1771  break;
1772  default:
1773  elog(ERROR, "unrecognized nulltesttype: %d",
1774  (int) nulltesttype);
1775  return (Selectivity) 0; /* keep compiler quiet */
1776  }
1777  }
1778  else
1779  {
1780  /*
1781  * No ANALYZE stats available, so make a guess
1782  */
1783  switch (nulltesttype)
1784  {
1785  case IS_NULL:
1786  selec = DEFAULT_UNK_SEL;
1787  break;
1788  case IS_NOT_NULL:
1789  selec = DEFAULT_NOT_UNK_SEL;
1790  break;
1791  default:
1792  elog(ERROR, "unrecognized nulltesttype: %d",
1793  (int) nulltesttype);
1794  return (Selectivity) 0; /* keep compiler quiet */
1795  }
1796  }
1797 
1798  ReleaseVariableStats(vardata);
1799 
1800  /* result should be in range, but make sure... */
1801  CLAMP_PROBABILITY(selec);
1802 
1803  return (Selectivity) selec;
1804 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
HeapTuple statsTuple
Definition: selfuncs.h:71
double Selectivity
Definition: nodes.h:642
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:4728
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
#define elog
Definition: elog.h:219

◆ pattern_fixed_prefix()

Pattern_Prefix_Status pattern_fixed_prefix ( Const patt,
Pattern_Type  ptype,
Oid  collation,
Const **  prefix,
Selectivity rest_selec 
)

Definition at line 5898 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, and regex_fixed_prefix().

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

5900 {
5901  Pattern_Prefix_Status result;
5902 
5903  switch (ptype)
5904  {
5905  case Pattern_Type_Like:
5906  result = like_fixed_prefix(patt, false, collation,
5907  prefix, rest_selec);
5908  break;
5909  case Pattern_Type_Like_IC:
5910  result = like_fixed_prefix(patt, true, collation,
5911  prefix, rest_selec);
5912  break;
5913  case Pattern_Type_Regex:
5914  result = regex_fixed_prefix(patt, false, collation,
5915  prefix, rest_selec);
5916  break;
5917  case Pattern_Type_Regex_IC:
5918  result = regex_fixed_prefix(patt, true, collation,
5919  prefix, rest_selec);
5920  break;
5921  default:
5922  elog(ERROR, "unrecognized ptype: %d", (int) ptype);
5923  result = Pattern_Prefix_None; /* keep compiler quiet */
5924  break;
5925  }
5926  return result;
5927 }
static Pattern_Prefix_Status regex_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
Definition: selfuncs.c:5830
static Pattern_Prefix_Status like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
Definition: selfuncs.c:5722
#define ERROR
Definition: elog.h:43
#define elog
Definition: elog.h:219
Pattern_Prefix_Status
Definition: selfuncs.h:94

◆ rowcomparesel()

Selectivity rowcomparesel ( PlannerInfo root,
RowCompareExpr clause,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo 
)

Definition at line 2205 of file selfuncs.c.

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

Referenced by clause_selectivity().

2208 {
2209  Selectivity s1;
2210  Oid opno = linitial_oid(clause->opnos);
2211  Oid inputcollid = linitial_oid(clause->inputcollids);
2212  List *opargs;
2213  bool is_join_clause;
2214 
2215  /* Build equivalent arg list for single operator */
2216  opargs = list_make2(linitial(clause->largs), linitial(clause->rargs));
2217 
2218  /*
2219  * Decide if it's a join clause. This should match clausesel.c's
2220  * treat_as_join_clause(), except that we intentionally consider only the
2221  * leading columns and not the rest of the clause.
2222  */
2223  if (varRelid != 0)
2224  {
2225  /*
2226  * Caller is forcing restriction mode (eg, because we are examining an
2227  * inner indexscan qual).
2228  */
2229  is_join_clause = false;
2230  }
2231  else if (sjinfo == NULL)
2232  {
2233  /*
2234  * It must be a restriction clause, since it's being evaluated at a
2235  * scan node.
2236  */
2237  is_join_clause = false;
2238  }
2239  else
2240  {
2241  /*
2242  * Otherwise, it's a join if there's more than one relation used.
2243  */
2244  is_join_clause = (NumRelids((Node *) opargs) > 1);
2245  }
2246 
2247  if (is_join_clause)
2248  {
2249  /* Estimate selectivity for a join clause. */
2250  s1 = join_selectivity(root, opno,
2251  opargs,
2252  inputcollid,
2253  jointype,
2254  sjinfo);
2255  }
2256  else
2257  {
2258  /* Estimate selectivity for a restriction clause. */
2259  s1 = restriction_selectivity(root, opno,
2260  opargs,
2261  inputcollid,
2262  varRelid);
2263  }
2264 
2265  return s1;
2266 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
Selectivity restriction_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, int varRelid)
Definition: plancat.c:1675
Definition: nodes.h:512
double Selectivity
Definition: nodes.h:642
unsigned int Oid
Definition: postgres_ext.h:31
#define linitial(l)
Definition: pg_list.h:111
char * s1
#define linitial_oid(l)
Definition: pg_list.h:113
Selectivity join_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: plancat.c:1712
List * inputcollids
Definition: primnodes.h:1041
Definition: pg_list.h:45
int NumRelids(Node *clause)
Definition: clauses.c:2272

◆ scalararraysel()

Selectivity scalararraysel ( PlannerInfo root,
ScalarArrayOpExpr clause,
bool  is_join_clause,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo 
)

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

1854 {
1855  Oid operator = clause->opno;
1856  bool useOr = clause->useOr;
1857  bool isEquality = false;
1858  bool isInequality = false;
1859  Node *leftop;
1860  Node *rightop;
1861  Oid nominal_element_type;
1862  Oid nominal_element_collation;
1863  TypeCacheEntry *typentry;
1864  RegProcedure oprsel;
1865  FmgrInfo oprselproc;
1866  Selectivity s1;
1867  Selectivity s1disjoint;
1868 
1869  /* First, deconstruct the expression */
1870  Assert(list_length(clause->args) == 2);
1871  leftop = (Node *) linitial(clause->args);
1872  rightop = (Node *) lsecond(clause->args);
1873 
1874  /* aggressively reduce both sides to constants */
1875  leftop = estimate_expression_value(root, leftop);
1876  rightop = estimate_expression_value(root, rightop);
1877 
1878  /* get nominal (after relabeling) element type of rightop */
1879  nominal_element_type = get_base_element_type(exprType(rightop));
1880  if (!OidIsValid(nominal_element_type))
1881  return (Selectivity) 0.5; /* probably shouldn't happen */
1882  /* get nominal collation, too, for generating constants */
1883  nominal_element_collation = exprCollation(rightop);
1884 
1885  /* look through any binary-compatible relabeling of rightop */
1886  rightop = strip_array_coercion(rightop);
1887 
1888  /*
1889  * Detect whether the operator is the default equality or inequality
1890  * operator of the array element type.
1891  */
1892  typentry = lookup_type_cache(nominal_element_type, TYPECACHE_EQ_OPR);
1893  if (OidIsValid(typentry->eq_opr))
1894  {
1895  if (operator == typentry->eq_opr)
1896  isEquality = true;
1897  else if (get_negator(operator) == typentry->eq_opr)
1898  isInequality = true;
1899  }
1900 
1901  /*
1902  * If it is equality or inequality, we might be able to estimate this as a
1903  * form of array containment; for instance "const = ANY(column)" can be
1904  * treated as "ARRAY[const] <@ column". scalararraysel_containment tries
1905  * that, and returns the selectivity estimate if successful, or -1 if not.
1906  */
1907  if ((isEquality || isInequality) && !is_join_clause)
1908  {
1909  s1 = scalararraysel_containment(root, leftop, rightop,
1910  nominal_element_type,
1911  isEquality, useOr, varRelid);
1912  if (s1 >= 0.0)
1913  return s1;
1914  }
1915 
1916  /*
1917  * Look up the underlying operator's selectivity estimator. Punt if it
1918  * hasn't got one.
1919  */
1920  if (is_join_clause)
1921  oprsel = get_oprjoin(operator);
1922  else
1923  oprsel = get_oprrest(operator);
1924  if (!oprsel)
1925  return (Selectivity) 0.5;
1926  fmgr_info(oprsel, &oprselproc);
1927 
1928  /*
1929  * In the array-containment check above, we must only believe that an
1930  * operator is equality or inequality if it is the default btree equality
1931  * operator (or its negator) for the element type, since those are the
1932  * operators that array containment will use. But in what follows, we can
1933  * be a little laxer, and also believe that any operators using eqsel() or
1934  * neqsel() as selectivity estimator act like equality or inequality.
1935  */
1936  if (oprsel == F_EQSEL || oprsel == F_EQJOINSEL)
1937  isEquality = true;
1938  else if (oprsel == F_NEQSEL || oprsel == F_NEQJOINSEL)
1939  isInequality = true;
1940 
1941  /*
1942  * We consider three cases:
1943  *
1944  * 1. rightop is an Array constant: deconstruct the array, apply the
1945  * operator's selectivity function for each array element, and merge the
1946  * results in the same way that clausesel.c does for AND/OR combinations.
1947  *
1948  * 2. rightop is an ARRAY[] construct: apply the operator's selectivity
1949  * function for each element of the ARRAY[] construct, and merge.
1950  *
1951  * 3. otherwise, make a guess ...
1952  */
1953  if (rightop && IsA(rightop, Const))
1954  {
1955  Datum arraydatum = ((Const *) rightop)->constvalue;
1956  bool arrayisnull = ((Const *) rightop)->constisnull;
1957  ArrayType *arrayval;
1958  int16 elmlen;
1959  bool elmbyval;
1960  char elmalign;
1961  int num_elems;
1962  Datum *elem_values;
1963  bool *elem_nulls;
1964  int i;
1965 
1966  if (arrayisnull) /* qual can't succeed if null array */
1967  return (Selectivity) 0.0;
1968  arrayval = DatumGetArrayTypeP(arraydatum);
1970  &elmlen, &elmbyval, &elmalign);
1971  deconstruct_array(arrayval,
1972  ARR_ELEMTYPE(arrayval),
1973  elmlen, elmbyval, elmalign,
1974  &elem_values, &elem_nulls, &num_elems);
1975 
1976  /*
1977  * For generic operators, we assume the probability of success is
1978  * independent for each array element. But for "= ANY" or "<> ALL",
1979  * if the array elements are distinct (which'd typically be the case)
1980  * then the probabilities are disjoint, and we should just sum them.
1981  *
1982  * If we were being really tense we would try to confirm that the
1983  * elements are all distinct, but that would be expensive and it
1984  * doesn't seem to be worth the cycles; it would amount to penalizing
1985  * well-written queries in favor of poorly-written ones. However, we
1986  * do protect ourselves a little bit by checking whether the
1987  * disjointness assumption leads to an impossible (out of range)
1988  * probability; if so, we fall back to the normal calculation.
1989  */
1990  s1 = s1disjoint = (useOr ? 0.0 : 1.0);
1991 
1992  for (i = 0; i < num_elems; i++)
1993  {
1994  List *args;
1995  Selectivity s2;
1996 
1997  args = list_make2(leftop,
1998  makeConst(nominal_element_type,
1999  -1,
2000  nominal_element_collation,
2001  elmlen,
2002  elem_values[i],
2003  elem_nulls[i],
2004  elmbyval));
2005  if (is_join_clause)
2006  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
2007  clause->inputcollid,
2008  PointerGetDatum(root),
2009  ObjectIdGetDatum(operator),
2010  PointerGetDatum(args),
2011  Int16GetDatum(jointype),
2012  PointerGetDatum(sjinfo)));
2013  else
2014  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
2015  clause->inputcollid,
2016  PointerGetDatum(root),
2017  ObjectIdGetDatum(operator),
2018  PointerGetDatum(args),
2019  Int32GetDatum(varRelid)));
2020 
2021  if (useOr)
2022  {
2023  s1 = s1 + s2 - s1 * s2;
2024  if (isEquality)
2025  s1disjoint += s2;
2026  }
2027  else
2028  {
2029  s1 = s1 * s2;
2030  if (isInequality)
2031  s1disjoint += s2 - 1.0;
2032  }
2033  }
2034 
2035  /* accept disjoint-probability estimate if in range */
2036  if ((useOr ? isEquality : isInequality) &&
2037  s1disjoint >= 0.0 && s1disjoint <= 1.0)
2038  s1 = s1disjoint;
2039  }
2040  else if (rightop && IsA(rightop, ArrayExpr) &&
2041  !((ArrayExpr *) rightop)->multidims)
2042  {
2043  ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
2044  int16 elmlen;
2045  bool elmbyval;
2046  ListCell *l;
2047 
2048  get_typlenbyval(arrayexpr->element_typeid,
2049  &elmlen, &elmbyval);
2050 
2051  /*
2052  * We use the assumption of disjoint probabilities here too, although
2053  * the odds of equal array elements are rather higher if the elements
2054  * are not all constants (which they won't be, else constant folding
2055  * would have reduced the ArrayExpr to a Const). In this path it's
2056  * critical to have the sanity check on the s1disjoint estimate.
2057  */
2058  s1 = s1disjoint = (useOr ? 0.0 : 1.0);
2059 
2060  foreach(l, arrayexpr->elements)
2061  {
2062  Node *elem = (Node *) lfirst(l);
2063  List *args;
2064  Selectivity s2;
2065 
2066  /*
2067  * Theoretically, if elem isn't of nominal_element_type we should
2068  * insert a RelabelType, but it seems unlikely that any operator
2069  * estimation function would really care ...
2070  */
2071  args = list_make2(leftop, elem);
2072  if (is_join_clause)
2073  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
2074  clause->inputcollid,
2075  PointerGetDatum(root),
2076  ObjectIdGetDatum(operator),
2077  PointerGetDatum(args),
2078  Int16GetDatum(jointype),
2079  PointerGetDatum(sjinfo)));
2080  else
2081  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
2082  clause->inputcollid,
2083  PointerGetDatum(root),
2084  ObjectIdGetDatum(operator),
2085  PointerGetDatum(args),
2086  Int32GetDatum(varRelid)));
2087 
2088  if (useOr)
2089  {
2090  s1 = s1 + s2 - s1 * s2;
2091  if (isEquality)
2092  s1disjoint += s2;
2093  }
2094  else
2095  {
2096  s1 = s1 * s2;
2097  if (isInequality)
2098  s1disjoint += s2 - 1.0;
2099  }
2100  }
2101 
2102  /* accept disjoint-probability estimate if in range */
2103  if ((useOr ? isEquality : isInequality) &&
2104  s1disjoint >= 0.0 && s1disjoint <= 1.0)
2105  s1 = s1disjoint;
2106  }
2107  else
2108  {
2109  CaseTestExpr *dummyexpr;
2110  List *args;
2111  Selectivity s2;
2112  int i;
2113 
2114  /*
2115  * We need a dummy rightop to pass to the operator selectivity
2116  * routine. It can be pretty much anything that doesn't look like a
2117  * constant; CaseTestExpr is a convenient choice.
2118  */
2119  dummyexpr = makeNode(CaseTestExpr);
2120  dummyexpr->typeId = nominal_element_type;
2121  dummyexpr->typeMod = -1;
2122  dummyexpr->collation = clause->inputcollid;
2123  args = list_make2(leftop, dummyexpr);
2124  if (is_join_clause)
2125  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
2126  clause->inputcollid,
2127  PointerGetDatum(root),
2128  ObjectIdGetDatum(operator),
2129  PointerGetDatum(args),
2130  Int16GetDatum(jointype),
2131  PointerGetDatum(sjinfo)));
2132  else
2133  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
2134  clause->inputcollid,
2135  PointerGetDatum(root),
2136  ObjectIdGetDatum(operator),
2137  PointerGetDatum(args),
2138  Int32GetDatum(varRelid)));
2139  s1 = useOr ? 0.0 : 1.0;
2140 
2141  /*
2142  * Arbitrarily assume 10 elements in the eventual array value (see
2143  * also estimate_array_length). We don't risk an assumption of
2144  * disjoint probabilities here.
2145  */
2146  for (i = 0; i < 10; i++)
2147  {
2148  if (useOr)
2149  s1 = s1 + s2 - s1 * s2;
2150  else
2151  s1 = s1 * s2;
2152  }
2153  }
2154 
2155  /* result should be in range, but make sure... */
2156  CLAMP_PROBABILITY(s1);
2157 
2158  return s1;
2159 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
signed short int16
Definition: c.h:293
Definition: fmgr.h:56
RegProcedure get_oprjoin(Oid opno)
Definition: lsyscache.c:1385
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2492
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2040
#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:1116
regproc RegProcedure
Definition: c.h:453
#define Int16GetDatum(X)
Definition: postgres.h:457
Definition: nodes.h:512
#define TYPECACHE_EQ_OPR
Definition: typcache.h:124
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1089
double Selectivity
Definition: nodes.h:642
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:298
#define OidIsValid(objectId)
Definition: c.h:586
#define lsecond(l)
Definition: pg_list.h:116
int32 typeMod
Definition: primnodes.h:941
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
#define linitial(l)
Definition: pg_list.h:111
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
char * s1
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:122
List * elements
Definition: primnodes.h:959
RegProcedure get_oprrest(Oid opno)
Definition: lsyscache.c:1361
char * s2
#define DatumGetFloat8(X)
Definition: postgres.h:734
uintptr_t Datum
Definition: postgres.h:372
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:311
#define makeNode(_type_)
Definition: nodes.h:560
#define Assert(condition)
Definition: c.h:680
#define lfirst(lc)
Definition: pg_list.h:106
Selectivity scalararraysel_containment(PlannerInfo *root, Node *leftop, Node *rightop, Oid elemtype, bool isEquality, bool useOr, int varRelid)
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2020
Oid element_typeid
Definition: primnodes.h:958
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2590
#define Int32GetDatum(X)
Definition: postgres.h:485
int i
Oid get_negator(Oid opno)
Definition: lsyscache.c:1337
Definition: pg_list.h:45
#define ARR_ELEMTYPE(a)
Definition: array.h:277
static Node * strip_array_coercion(Node *node)
Definition: selfuncs.c:1815
#define DatumGetArrayTypeP(X)
Definition: array.h:246

◆ scalararraysel_containment()

Selectivity scalararraysel_containment ( PlannerInfo root,
Node leftop,
Node rightop,
Oid  elemtype,
bool  isEquality,
bool  useOr,
int  varRelid 
)

Definition at line 83 of file array_selfuncs.c.

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

Referenced by scalararraysel().

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

◆ statistic_proc_security_check()

bool statistic_proc_security_check ( VariableStatData vardata,
Oid  func_oid 
)

Definition at line 5106 of file selfuncs.c.

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

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

5107 {
5108  if (vardata->acl_ok)
5109  return true;
5110 
5111  if (!OidIsValid(func_oid))
5112  return false;
5113 
5114  if (get_func_leakproof(func_oid))
5115  return true;
5116 
5117  ereport(DEBUG2,
5118  (errmsg_internal("not using statistics because function \"%s\" is not leak-proof",
5119  get_func_name(func_oid))));
5120  return false;
5121 }
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1641
#define OidIsValid(objectId)
Definition: c.h:586
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1412
#define DEBUG2
Definition: elog.h:24
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827

Variable Documentation

◆ get_index_stats_hook

PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook

Definition at line 156 of file selfuncs.c.

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

◆ get_relation_stats_hook

PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook

Definition at line 155 of file selfuncs.c.

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