PostgreSQL Source Code  git master
selfuncs.h File Reference
#include "access/htup.h"
#include "fmgr.h"
#include "nodes/pathnodes.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  EstimationInfo
 
struct  VariableStatData
 
struct  GenericCosts
 

Macros

#define DEFAULT_EQ_SEL   0.005
 
#define DEFAULT_INEQ_SEL   0.3333333333333333
 
#define DEFAULT_RANGE_INEQ_SEL   0.005
 
#define DEFAULT_MULTIRANGE_INEQ_SEL   0.005
 
#define DEFAULT_MATCH_SEL   0.005
 
#define DEFAULT_MATCHING_SEL   0.010
 
#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 SELFLAG_USED_DEFAULT
 
#define ReleaseVariableStats(vardata)
 

Typedefs

typedef struct EstimationInfo EstimationInfo
 
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)
 

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, Oid collation, Datum constval, bool varonleft, double *sumcommonp)
 
double histogram_selectivity (VariableStatData *vardata, FmgrInfo *opproc, Oid collation, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
 
double generic_restriction_selectivity (PlannerInfo *root, Oid oproid, Oid collation, List *args, int varRelid, double default_selectivity)
 
double ineq_histogram_selectivity (PlannerInfo *root, VariableStatData *vardata, Oid opoid, FmgrInfo *opproc, bool isgt, bool iseq, Oid collation, Datum constval, Oid consttype)
 
double var_eq_const (VariableStatData *vardata, Oid oproid, Oid collation, Datum constval, bool constisnull, bool varonleft, bool negate)
 
double var_eq_non_const (VariableStatData *vardata, Oid oproid, Oid collation, Node *other, bool varonleft, bool negate)
 
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)
 
double estimate_array_length (PlannerInfo *root, 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, EstimationInfo *estinfo)
 
void estimate_hash_bucket_stats (PlannerInfo *root, Node *hashkey, double nbuckets, Selectivity *mcv_freq, Selectivity *bucketsize_frac)
 
double estimate_hashagg_tablesize (PlannerInfo *root, Path *path, const AggClauseCosts *agg_costs, double dNumGroups)
 
Listget_quals_from_indexclauses (List *indexclauses)
 
Cost index_other_operands_eval_cost (PlannerInfo *root, List *indexquals)
 
Listadd_predicate_to_index_quals (IndexOptInfo *index, List *indexQuals)
 
void genericcostestimate (PlannerInfo *root, IndexPath *path, double loop_count, 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

#define CLAMP_PROBABILITY (   p)
Value:
do { \
if (p < 0.0) \
p = 0.0; \
else if (p > 1.0) \
p = 1.0; \
} while (0)

Definition at line 63 of file selfuncs.h.

◆ DEFAULT_EQ_SEL

#define DEFAULT_EQ_SEL   0.005

Definition at line 34 of file selfuncs.h.

◆ DEFAULT_INEQ_SEL

#define DEFAULT_INEQ_SEL   0.3333333333333333

Definition at line 37 of file selfuncs.h.

◆ DEFAULT_MATCH_SEL

#define DEFAULT_MATCH_SEL   0.005

Definition at line 46 of file selfuncs.h.

◆ DEFAULT_MATCHING_SEL

#define DEFAULT_MATCHING_SEL   0.010

Definition at line 49 of file selfuncs.h.

◆ DEFAULT_MULTIRANGE_INEQ_SEL

#define DEFAULT_MULTIRANGE_INEQ_SEL   0.005

Definition at line 43 of file selfuncs.h.

◆ DEFAULT_NOT_UNK_SEL

#define DEFAULT_NOT_UNK_SEL   (1.0 - DEFAULT_UNK_SEL)

Definition at line 56 of file selfuncs.h.

◆ DEFAULT_NUM_DISTINCT

#define DEFAULT_NUM_DISTINCT   200

Definition at line 52 of file selfuncs.h.

◆ DEFAULT_RANGE_INEQ_SEL

#define DEFAULT_RANGE_INEQ_SEL   0.005

Definition at line 40 of file selfuncs.h.

◆ DEFAULT_UNK_SEL

#define DEFAULT_UNK_SEL   0.005

Definition at line 55 of file selfuncs.h.

◆ ReleaseVariableStats

#define ReleaseVariableStats (   vardata)
Value:
do { \
if (HeapTupleIsValid((vardata).statsTuple)) \
(vardata).freefunc((vardata).statsTuple); \
} while(0)
#define HeapTupleIsValid(tuple)
Definition: htup.h:78

Definition at line 99 of file selfuncs.h.

◆ SELFLAG_USED_DEFAULT

#define SELFLAG_USED_DEFAULT
Value:
(1 << 0) /* Estimation fell back on one
* of the DEFAULTs as defined
* above. */

Definition at line 76 of file selfuncs.h.

Typedef Documentation

◆ EstimationInfo

◆ get_index_stats_hook_type

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

Definition at line 143 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 138 of file selfuncs.h.

◆ VariableStatData

Function Documentation

◆ add_predicate_to_index_quals()

List* add_predicate_to_index_quals ( IndexOptInfo index,
List indexQuals 
)

Definition at line 6778 of file selfuncs.c.

6779 {
6780  List *predExtraQuals = NIL;
6781  ListCell *lc;
6782 
6783  if (index->indpred == NIL)
6784  return indexQuals;
6785 
6786  foreach(lc, index->indpred)
6787  {
6788  Node *predQual = (Node *) lfirst(lc);
6789  List *oneQual = list_make1(predQual);
6790 
6791  if (!predicate_implied_by(oneQual, indexQuals, false))
6792  predExtraQuals = list_concat(predExtraQuals, oneQual);
6793  }
6794  return list_concat(predExtraQuals, indexQuals);
6795 }
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
#define list_make1(x1)
Definition: pg_list.h:212
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:152
Definition: pg_list.h:54
Definition: nodes.h:129
Definition: type.h:95

References lfirst, list_concat(), list_make1, NIL, and predicate_implied_by().

Referenced by btcostestimate(), genericcostestimate(), and gincostestimate().

◆ booltestsel()

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

Definition at line 1540 of file selfuncs.c.

1542 {
1543  VariableStatData vardata;
1544  double selec;
1545 
1546  examine_variable(root, arg, varRelid, &vardata);
1547 
1548  if (HeapTupleIsValid(vardata.statsTuple))
1549  {
1550  Form_pg_statistic stats;
1551  double freq_null;
1552  AttStatsSlot sslot;
1553 
1554  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1555  freq_null = stats->stanullfrac;
1556 
1557  if (get_attstatsslot(&sslot, vardata.statsTuple,
1558  STATISTIC_KIND_MCV, InvalidOid,
1560  && sslot.nnumbers > 0)
1561  {
1562  double freq_true;
1563  double freq_false;
1564 
1565  /*
1566  * Get first MCV frequency and derive frequency for true.
1567  */
1568  if (DatumGetBool(sslot.values[0]))
1569  freq_true = sslot.numbers[0];
1570  else
1571  freq_true = 1.0 - sslot.numbers[0] - freq_null;
1572 
1573  /*
1574  * Next derive frequency for false. Then use these as appropriate
1575  * to derive frequency for each case.
1576  */
1577  freq_false = 1.0 - freq_true - freq_null;
1578 
1579  switch (booltesttype)
1580  {
1581  case IS_UNKNOWN:
1582  /* select only NULL values */
1583  selec = freq_null;
1584  break;
1585  case IS_NOT_UNKNOWN:
1586  /* select non-NULL values */
1587  selec = 1.0 - freq_null;
1588  break;
1589  case IS_TRUE:
1590  /* select only TRUE values */
1591  selec = freq_true;
1592  break;
1593  case IS_NOT_TRUE:
1594  /* select non-TRUE values */
1595  selec = 1.0 - freq_true;
1596  break;
1597  case IS_FALSE:
1598  /* select only FALSE values */
1599  selec = freq_false;
1600  break;
1601  case IS_NOT_FALSE:
1602  /* select non-FALSE values */
1603  selec = 1.0 - freq_false;
1604  break;
1605  default:
1606  elog(ERROR, "unrecognized booltesttype: %d",
1607  (int) booltesttype);
1608  selec = 0.0; /* Keep compiler quiet */
1609  break;
1610  }
1611 
1612  free_attstatsslot(&sslot);
1613  }
1614  else
1615  {
1616  /*
1617  * No most-common-value info available. Still have null fraction
1618  * information, so use it for IS [NOT] UNKNOWN. Otherwise adjust
1619  * for null fraction and assume a 50-50 split of TRUE and FALSE.
1620  */
1621  switch (booltesttype)
1622  {
1623  case IS_UNKNOWN:
1624  /* select only NULL values */
1625  selec = freq_null;
1626  break;
1627  case IS_NOT_UNKNOWN:
1628  /* select non-NULL values */
1629  selec = 1.0 - freq_null;
1630  break;
1631  case IS_TRUE:
1632  case IS_FALSE:
1633  /* Assume we select half of the non-NULL values */
1634  selec = (1.0 - freq_null) / 2.0;
1635  break;
1636  case IS_NOT_TRUE:
1637  case IS_NOT_FALSE:
1638  /* Assume we select NULLs plus half of the non-NULLs */
1639  /* equiv. to freq_null + (1.0 - freq_null) / 2.0 */
1640  selec = (freq_null + 1.0) / 2.0;
1641  break;
1642  default:
1643  elog(ERROR, "unrecognized booltesttype: %d",
1644  (int) booltesttype);
1645  selec = 0.0; /* Keep compiler quiet */
1646  break;
1647  }
1648  }
1649  }
1650  else
1651  {
1652  /*
1653  * If we can't get variable statistics for the argument, perhaps
1654  * clause_selectivity can do something with it. We ignore the
1655  * possibility of a NULL value when using clause_selectivity, and just
1656  * assume the value is either TRUE or FALSE.
1657  */
1658  switch (booltesttype)
1659  {
1660  case IS_UNKNOWN:
1661  selec = DEFAULT_UNK_SEL;
1662  break;
1663  case IS_NOT_UNKNOWN:
1664  selec = DEFAULT_NOT_UNK_SEL;
1665  break;
1666  case IS_TRUE:
1667  case IS_NOT_FALSE:
1668  selec = (double) clause_selectivity(root, arg,
1669  varRelid,
1670  jointype, sjinfo);
1671  break;
1672  case IS_FALSE:
1673  case IS_NOT_TRUE:
1674  selec = 1.0 - (double) clause_selectivity(root, arg,
1675  varRelid,
1676  jointype, sjinfo);
1677  break;
1678  default:
1679  elog(ERROR, "unrecognized booltesttype: %d",
1680  (int) booltesttype);
1681  selec = 0.0; /* Keep compiler quiet */
1682  break;
1683  }
1684  }
1685 
1686  ReleaseVariableStats(vardata);
1687 
1688  /* result should be in range, but make sure... */
1689  CLAMP_PROBABILITY(selec);
1690 
1691  return (Selectivity) selec;
1692 }
Selectivity clause_selectivity(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:667
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3344
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:3234
#define ATTSTATSSLOT_NUMBERS
Definition: lsyscache.h:43
#define ATTSTATSSLOT_VALUES
Definition: lsyscache.h:42
double Selectivity
Definition: nodes.h:250
void * arg
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:135
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
#define InvalidOid
Definition: postgres_ext.h:36
@ IS_NOT_TRUE
Definition: primnodes.h:1976
@ IS_NOT_FALSE
Definition: primnodes.h:1976
@ IS_NOT_UNKNOWN
Definition: primnodes.h:1976
@ IS_TRUE
Definition: primnodes.h:1976
@ IS_UNKNOWN
Definition: primnodes.h:1976
@ IS_FALSE
Definition: primnodes.h:1976
tree ctl root
Definition: radixtree.h:1886
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:5023
#define DEFAULT_NOT_UNK_SEL
Definition: selfuncs.h:56
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:99
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:63
#define DEFAULT_UNK_SEL
Definition: selfuncs.h:55
Datum * values
Definition: lsyscache.h:53
float4 * numbers
Definition: lsyscache.h:56
int nnumbers
Definition: lsyscache.h:57
HeapTuple statsTuple
Definition: selfuncs.h:89

References arg, 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, root, VariableStatData::statsTuple, and AttStatsSlot::values.

Referenced by clause_selectivity_ext().

◆ boolvarsel()

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

Definition at line 1512 of file selfuncs.c.

1513 {
1514  VariableStatData vardata;
1515  double selec;
1516 
1517  examine_variable(root, arg, varRelid, &vardata);
1518  if (HeapTupleIsValid(vardata.statsTuple))
1519  {
1520  /*
1521  * A boolean variable V is equivalent to the clause V = 't', so we
1522  * compute the selectivity as if that is what we have.
1523  */
1524  selec = var_eq_const(&vardata, BooleanEqualOperator, InvalidOid,
1525  BoolGetDatum(true), false, true, false);
1526  }
1527  else
1528  {
1529  /* Otherwise, the default estimate is 0.5 */
1530  selec = 0.5;
1531  }
1532  ReleaseVariableStats(vardata);
1533  return selec;
1534 }
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
double var_eq_const(VariableStatData *vardata, Oid oproid, Oid collation, Datum constval, bool constisnull, bool varonleft, bool negate)
Definition: selfuncs.c:295

References arg, BoolGetDatum(), examine_variable(), HeapTupleIsValid, InvalidOid, ReleaseVariableStats, root, VariableStatData::statsTuple, and var_eq_const().

Referenced by clause_selectivity_ext().

◆ estimate_array_length()

double estimate_array_length ( PlannerInfo root,
Node arrayexpr 
)

Definition at line 2139 of file selfuncs.c.

2140 {
2141  /* look through any binary-compatible relabeling of arrayexpr */
2142  arrayexpr = strip_array_coercion(arrayexpr);
2143 
2144  if (arrayexpr && IsA(arrayexpr, Const))
2145  {
2146  Datum arraydatum = ((Const *) arrayexpr)->constvalue;
2147  bool arrayisnull = ((Const *) arrayexpr)->constisnull;
2148  ArrayType *arrayval;
2149 
2150  if (arrayisnull)
2151  return 0;
2152  arrayval = DatumGetArrayTypeP(arraydatum);
2153  return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
2154  }
2155  else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
2156  !((ArrayExpr *) arrayexpr)->multidims)
2157  {
2158  return list_length(((ArrayExpr *) arrayexpr)->elements);
2159  }
2160  else if (arrayexpr && root)
2161  {
2162  /* See if we can find any statistics about it */
2163  VariableStatData vardata;
2164  AttStatsSlot sslot;
2165  double nelem = 0;
2166 
2167  examine_variable(root, arrayexpr, 0, &vardata);
2168  if (HeapTupleIsValid(vardata.statsTuple))
2169  {
2170  /*
2171  * Found stats, so use the average element count, which is stored
2172  * in the last stanumbers element of the DECHIST statistics.
2173  * Actually that is the average count of *distinct* elements;
2174  * perhaps we should scale it up somewhat?
2175  */
2176  if (get_attstatsslot(&sslot, vardata.statsTuple,
2177  STATISTIC_KIND_DECHIST, InvalidOid,
2179  {
2180  if (sslot.nnumbers > 0)
2181  nelem = clamp_row_est(sslot.numbers[sslot.nnumbers - 1]);
2182  free_attstatsslot(&sslot);
2183  }
2184  }
2185  ReleaseVariableStats(vardata);
2186 
2187  if (nelem > 0)
2188  return nelem;
2189  }
2190 
2191  /* Else use a default guess --- this should match scalararraysel */
2192  return 10;
2193 }
#define ARR_NDIM(a)
Definition: array.h:290
#define DatumGetArrayTypeP(X)
Definition: array.h:261
#define ARR_DIMS(a)
Definition: array.h:294
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:57
double clamp_row_est(double nrows)
Definition: costsize.c:213
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
static int list_length(const List *l)
Definition: pg_list.h:152
uintptr_t Datum
Definition: postgres.h:64
static Node * strip_array_coercion(Node *node)
Definition: selfuncs.c:1783

References ARR_DIMS, ARR_NDIM, ArrayGetNItems(), ATTSTATSSLOT_NUMBERS, clamp_row_est(), DatumGetArrayTypeP, examine_variable(), free_attstatsslot(), get_attstatsslot(), HeapTupleIsValid, InvalidOid, IsA, list_length(), AttStatsSlot::nnumbers, AttStatsSlot::numbers, ReleaseVariableStats, root, VariableStatData::statsTuple, and strip_array_coercion().

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

◆ estimate_hash_bucket_stats()

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

Definition at line 3802 of file selfuncs.c.

3805 {
3806  VariableStatData vardata;
3807  double estfract,
3808  ndistinct,
3809  stanullfrac,
3810  avgfreq;
3811  bool isdefault;
3812  AttStatsSlot sslot;
3813 
3814  examine_variable(root, hashkey, 0, &vardata);
3815 
3816  /* Look up the frequency of the most common value, if available */
3817  *mcv_freq = 0.0;
3818 
3819  if (HeapTupleIsValid(vardata.statsTuple))
3820  {
3821  if (get_attstatsslot(&sslot, vardata.statsTuple,
3822  STATISTIC_KIND_MCV, InvalidOid,
3824  {
3825  /*
3826  * The first MCV stat is for the most common value.
3827  */
3828  if (sslot.nnumbers > 0)
3829  *mcv_freq = sslot.numbers[0];
3830  free_attstatsslot(&sslot);
3831  }
3832  }
3833 
3834  /* Get number of distinct values */
3835  ndistinct = get_variable_numdistinct(&vardata, &isdefault);
3836 
3837  /*
3838  * If ndistinct isn't real, punt. We normally return 0.1, but if the
3839  * mcv_freq is known to be even higher than that, use it instead.
3840  */
3841  if (isdefault)
3842  {
3843  *bucketsize_frac = (Selectivity) Max(0.1, *mcv_freq);
3844  ReleaseVariableStats(vardata);
3845  return;
3846  }
3847 
3848  /* Get fraction that are null */
3849  if (HeapTupleIsValid(vardata.statsTuple))
3850  {
3851  Form_pg_statistic stats;
3852 
3853  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
3854  stanullfrac = stats->stanullfrac;
3855  }
3856  else
3857  stanullfrac = 0.0;
3858 
3859  /* Compute avg freq of all distinct data values in raw relation */
3860  avgfreq = (1.0 - stanullfrac) / ndistinct;
3861 
3862  /*
3863  * Adjust ndistinct to account for restriction clauses. Observe we are
3864  * assuming that the data distribution is affected uniformly by the
3865  * restriction clauses!
3866  *
3867  * XXX Possibly better way, but much more expensive: multiply by
3868  * selectivity of rel's restriction clauses that mention the target Var.
3869  */
3870  if (vardata.rel && vardata.rel->tuples > 0)
3871  {
3872  ndistinct *= vardata.rel->rows / vardata.rel->tuples;
3873  ndistinct = clamp_row_est(ndistinct);
3874  }
3875 
3876  /*
3877  * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
3878  * number of buckets is less than the expected number of distinct values;
3879  * otherwise it is 1/ndistinct.
3880  */
3881  if (ndistinct > nbuckets)
3882  estfract = 1.0 / nbuckets;
3883  else
3884  estfract = 1.0 / ndistinct;
3885 
3886  /*
3887  * Adjust estimated bucketsize upward to account for skewed distribution.
3888  */
3889  if (avgfreq > 0.0 && *mcv_freq > avgfreq)
3890  estfract *= *mcv_freq / avgfreq;
3891 
3892  /*
3893  * Clamp bucketsize to sane range (the above adjustment could easily
3894  * produce an out-of-range result). We set the lower bound a little above
3895  * zero, since zero isn't a very sane result.
3896  */
3897  if (estfract < 1.0e-6)
3898  estfract = 1.0e-6;
3899  else if (estfract > 1.0)
3900  estfract = 1.0;
3901 
3902  *bucketsize_frac = (Selectivity) estfract;
3903 
3904  ReleaseVariableStats(vardata);
3905 }
#define Max(x, y)
Definition: c.h:989
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition: selfuncs.c:5775
Cardinality tuples
Definition: pathnodes.h:949
Cardinality rows
Definition: pathnodes.h:877
RelOptInfo * rel
Definition: selfuncs.h:88

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, root, RelOptInfo::rows, VariableStatData::statsTuple, and RelOptInfo::tuples.

Referenced by final_cost_hashjoin().

◆ estimate_hashagg_tablesize()

double estimate_hashagg_tablesize ( PlannerInfo root,
Path path,
const AggClauseCosts agg_costs,
double  dNumGroups 
)

Definition at line 3921 of file selfuncs.c.

3923 {
3924  Size hashentrysize;
3925 
3926  hashentrysize = hash_agg_entry_size(list_length(root->aggtransinfos),
3927  path->pathtarget->width,
3928  agg_costs->transitionSpace);
3929 
3930  /*
3931  * Note that this disregards the effect of fill-factor and growth policy
3932  * of the hash table. That's probably ok, given that the default
3933  * fill-factor is relatively high. It'd be hard to meaningfully factor in
3934  * "double-in-size" growth policies here.
3935  */
3936  return hashentrysize * dNumGroups;
3937 }
size_t Size
Definition: c.h:596
Size hash_agg_entry_size(int numTrans, Size tupleWidth, Size transitionSpace)
Definition: nodeAgg.c:1694
Size transitionSpace
Definition: pathnodes.h:62

References hash_agg_entry_size(), list_length(), root, and AggClauseCosts::transitionSpace.

Referenced by consider_groupingsets_paths().

◆ estimate_num_groups()

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

Definition at line 3420 of file selfuncs.c.

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

References add_unique_group_var(), Assert, clamp_row_est(), contain_volatile_functions(), estimate_multivariate_ndistinct(), examine_variable(), expression_returns_set_rows(), exprType(), EstimationInfo::flags, for_each_from, HeapTupleIsValid, i, IS_SIMPLE_REL, GroupVarInfo::isdefault, VariableStatData::isunique, lappend(), lfirst, linitial, list_member_int(), GroupVarInfo::ndistinct, NIL, pull_var_clause(), PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS, PVC_RECURSE_WINDOWFUNCS, GroupVarInfo::rel, ReleaseVariableStats, root, RelOptInfo::rows, SELFLAG_USED_DEFAULT, VariableStatData::statsTuple, and RelOptInfo::tuples.

Referenced by adjust_rowcount_for_semijoins(), build_setop_child_paths(), cost_incremental_sort(), cost_memoize_rescan(), create_final_distinct_paths(), create_partial_distinct_paths(), create_unique_path(), estimate_path_cost_size(), get_number_of_groups(), and get_windowclause_startup_tuples().

◆ examine_variable()

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

Definition at line 5023 of file selfuncs.c.

5025 {
5026  Node *basenode;
5027  Relids varnos;
5028  RelOptInfo *onerel;
5029 
5030  /* Make sure we don't return dangling pointers in vardata */
5031  MemSet(vardata, 0, sizeof(VariableStatData));
5032 
5033  /* Save the exposed type of the expression */
5034  vardata->vartype = exprType(node);
5035 
5036  /* Look inside any binary-compatible relabeling */
5037 
5038  if (IsA(node, RelabelType))
5039  basenode = (Node *) ((RelabelType *) node)->arg;
5040  else
5041  basenode = node;
5042 
5043  /* Fast path for a simple Var */
5044 
5045  if (IsA(basenode, Var) &&
5046  (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
5047  {
5048  Var *var = (Var *) basenode;
5049 
5050  /* Set up result fields other than the stats tuple */
5051  vardata->var = basenode; /* return Var without relabeling */
5052  vardata->rel = find_base_rel(root, var->varno);
5053  vardata->atttype = var->vartype;
5054  vardata->atttypmod = var->vartypmod;
5055  vardata->isunique = has_unique_index(vardata->rel, var->varattno);
5056 
5057  /* Try to locate some stats */
5058  examine_simple_variable(root, var, vardata);
5059 
5060  return;
5061  }
5062 
5063  /*
5064  * Okay, it's a more complicated expression. Determine variable
5065  * membership. Note that when varRelid isn't zero, only vars of that
5066  * relation are considered "real" vars.
5067  */
5068  varnos = pull_varnos(root, basenode);
5069 
5070  onerel = NULL;
5071 
5072  if (bms_is_empty(varnos))
5073  {
5074  /* No Vars at all ... must be pseudo-constant clause */
5075  }
5076  else
5077  {
5078  int relid;
5079 
5080  if (bms_get_singleton_member(varnos, &relid))
5081  {
5082  if (varRelid == 0 || varRelid == relid)
5083  {
5084  onerel = find_base_rel(root, relid);
5085  vardata->rel = onerel;
5086  node = basenode; /* strip any relabeling */
5087  }
5088  /* else treat it as a constant */
5089  }
5090  else
5091  {
5092  /* varnos has multiple relids */
5093  if (varRelid == 0)
5094  {
5095  /* treat it as a variable of a join relation */
5096  vardata->rel = find_join_rel(root, varnos);
5097  node = basenode; /* strip any relabeling */
5098  }
5099  else if (bms_is_member(varRelid, varnos))
5100  {
5101  /* ignore the vars belonging to other relations */
5102  vardata->rel = find_base_rel(root, varRelid);
5103  node = basenode; /* strip any relabeling */
5104  /* note: no point in expressional-index search here */
5105  }
5106  /* else treat it as a constant */
5107  }
5108  }
5109 
5110  bms_free(varnos);
5111 
5112  vardata->var = node;
5113  vardata->atttype = exprType(node);
5114  vardata->atttypmod = exprTypmod(node);
5115 
5116  if (onerel)
5117  {
5118  /*
5119  * We have an expression in vars of a single relation. Try to match
5120  * it to expressional index columns, in hopes of finding some
5121  * statistics.
5122  *
5123  * Note that we consider all index columns including INCLUDE columns,
5124  * since there could be stats for such columns. But the test for
5125  * uniqueness needs to be warier.
5126  *
5127  * XXX it's conceivable that there are multiple matches with different
5128  * index opfamilies; if so, we need to pick one that matches the
5129  * operator we are estimating for. FIXME later.
5130  */
5131  ListCell *ilist;
5132  ListCell *slist;
5133  Oid userid;
5134 
5135  /*
5136  * Determine the user ID to use for privilege checks: either
5137  * onerel->userid if it's set (e.g., in case we're accessing the table
5138  * via a view), or the current user otherwise.
5139  *
5140  * If we drill down to child relations, we keep using the same userid:
5141  * it's going to be the same anyway, due to how we set up the relation
5142  * tree (q.v. build_simple_rel).
5143  */
5144  userid = OidIsValid(onerel->userid) ? onerel->userid : GetUserId();
5145 
5146  foreach(ilist, onerel->indexlist)
5147  {
5148  IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
5149  ListCell *indexpr_item;
5150  int pos;
5151 
5152  indexpr_item = list_head(index->indexprs);
5153  if (indexpr_item == NULL)
5154  continue; /* no expressions here... */
5155 
5156  for (pos = 0; pos < index->ncolumns; pos++)
5157  {
5158  if (index->indexkeys[pos] == 0)
5159  {
5160  Node *indexkey;
5161 
5162  if (indexpr_item == NULL)
5163  elog(ERROR, "too few entries in indexprs list");
5164  indexkey = (Node *) lfirst(indexpr_item);
5165  if (indexkey && IsA(indexkey, RelabelType))
5166  indexkey = (Node *) ((RelabelType *) indexkey)->arg;
5167  if (equal(node, indexkey))
5168  {
5169  /*
5170  * Found a match ... is it a unique index? Tests here
5171  * should match has_unique_index().
5172  */
5173  if (index->unique &&
5174  index->nkeycolumns == 1 &&
5175  pos == 0 &&
5176  (index->indpred == NIL || index->predOK))
5177  vardata->isunique = true;
5178 
5179  /*
5180  * Has it got stats? We only consider stats for
5181  * non-partial indexes, since partial indexes probably
5182  * don't reflect whole-relation statistics; the above
5183  * check for uniqueness is the only info we take from
5184  * a partial index.
5185  *
5186  * An index stats hook, however, must make its own
5187  * decisions about what to do with partial indexes.
5188  */
5190  (*get_index_stats_hook) (root, index->indexoid,
5191  pos + 1, vardata))
5192  {
5193  /*
5194  * The hook took control of acquiring a stats
5195  * tuple. If it did supply a tuple, it'd better
5196  * have supplied a freefunc.
5197  */
5198  if (HeapTupleIsValid(vardata->statsTuple) &&
5199  !vardata->freefunc)
5200  elog(ERROR, "no function provided to release variable stats with");
5201  }
5202  else if (index->indpred == NIL)
5203  {
5204  vardata->statsTuple =
5205  SearchSysCache3(STATRELATTINH,
5206  ObjectIdGetDatum(index->indexoid),
5207  Int16GetDatum(pos + 1),
5208  BoolGetDatum(false));
5209  vardata->freefunc = ReleaseSysCache;
5210 
5211  if (HeapTupleIsValid(vardata->statsTuple))
5212  {
5213  /* Get index's table for permission check */
5214  RangeTblEntry *rte;
5215 
5216  rte = planner_rt_fetch(index->rel->relid, root);
5217  Assert(rte->rtekind == RTE_RELATION);
5218 
5219  /*
5220  * For simplicity, we insist on the whole
5221  * table being selectable, rather than trying
5222  * to identify which column(s) the index
5223  * depends on. Also require all rows to be
5224  * selectable --- there must be no
5225  * securityQuals from security barrier views
5226  * or RLS policies.
5227  */
5228  vardata->acl_ok =
5229  rte->securityQuals == NIL &&
5230  (pg_class_aclcheck(rte->relid, userid,
5231  ACL_SELECT) == ACLCHECK_OK);
5232 
5233  /*
5234  * If the user doesn't have permissions to
5235  * access an inheritance child relation, check
5236  * the permissions of the table actually
5237  * mentioned in the query, since most likely
5238  * the user does have that permission. Note
5239  * that whole-table select privilege on the
5240  * parent doesn't quite guarantee that the
5241  * user could read all columns of the child.
5242  * But in practice it's unlikely that any
5243  * interesting security violation could result
5244  * from allowing access to the expression
5245  * index's stats, so we allow it anyway. See
5246  * similar code in examine_simple_variable()
5247  * for additional comments.
5248  */
5249  if (!vardata->acl_ok &&
5250  root->append_rel_array != NULL)
5251  {
5252  AppendRelInfo *appinfo;
5253  Index varno = index->rel->relid;
5254 
5255  appinfo = root->append_rel_array[varno];
5256  while (appinfo &&
5257  planner_rt_fetch(appinfo->parent_relid,
5258  root)->rtekind == RTE_RELATION)
5259  {
5260  varno = appinfo->parent_relid;
5261  appinfo = root->append_rel_array[varno];
5262  }
5263  if (varno != index->rel->relid)
5264  {
5265  /* Repeat access check on this rel */
5266  rte = planner_rt_fetch(varno, root);
5267  Assert(rte->rtekind == RTE_RELATION);
5268 
5269  vardata->acl_ok =
5270  rte->securityQuals == NIL &&
5271  (pg_class_aclcheck(rte->relid,
5272  userid,
5273  ACL_SELECT) == ACLCHECK_OK);
5274  }
5275  }
5276  }
5277  else
5278  {
5279  /* suppress leakproofness checks later */
5280  vardata->acl_ok = true;
5281  }
5282  }
5283  if (vardata->statsTuple)
5284  break;
5285  }
5286  indexpr_item = lnext(index->indexprs, indexpr_item);
5287  }
5288  }
5289  if (vardata->statsTuple)
5290  break;
5291  }
5292 
5293  /*
5294  * Search extended statistics for one with a matching expression.
5295  * There might be multiple ones, so just grab the first one. In the
5296  * future, we might consider the statistics target (and pick the most
5297  * accurate statistics) and maybe some other parameters.
5298  */
5299  foreach(slist, onerel->statlist)
5300  {
5301  StatisticExtInfo *info = (StatisticExtInfo *) lfirst(slist);
5302  RangeTblEntry *rte = planner_rt_fetch(onerel->relid, root);
5303  ListCell *expr_item;
5304  int pos;
5305 
5306  /*
5307  * Stop once we've found statistics for the expression (either
5308  * from extended stats, or for an index in the preceding loop).
5309  */
5310  if (vardata->statsTuple)
5311  break;
5312 
5313  /* skip stats without per-expression stats */
5314  if (info->kind != STATS_EXT_EXPRESSIONS)
5315  continue;
5316 
5317  /* skip stats with mismatching stxdinherit value */
5318  if (info->inherit != rte->inh)
5319  continue;
5320 
5321  pos = 0;
5322  foreach(expr_item, info->exprs)
5323  {
5324  Node *expr = (Node *) lfirst(expr_item);
5325 
5326  Assert(expr);
5327 
5328  /* strip RelabelType before comparing it */
5329  if (expr && IsA(expr, RelabelType))
5330  expr = (Node *) ((RelabelType *) expr)->arg;
5331 
5332  /* found a match, see if we can extract pg_statistic row */
5333  if (equal(node, expr))
5334  {
5335  /*
5336  * XXX Not sure if we should cache the tuple somewhere.
5337  * Now we just create a new copy every time.
5338  */
5339  vardata->statsTuple =
5340  statext_expressions_load(info->statOid, rte->inh, pos);
5341 
5342  vardata->freefunc = ReleaseDummy;
5343 
5344  /*
5345  * For simplicity, we insist on the whole table being
5346  * selectable, rather than trying to identify which
5347  * column(s) the statistics object depends on. Also
5348  * require all rows to be selectable --- there must be no
5349  * securityQuals from security barrier views or RLS
5350  * policies.
5351  */
5352  vardata->acl_ok =
5353  rte->securityQuals == NIL &&
5354  (pg_class_aclcheck(rte->relid, userid,
5355  ACL_SELECT) == ACLCHECK_OK);
5356 
5357  /*
5358  * If the user doesn't have permissions to access an
5359  * inheritance child relation, check the permissions of
5360  * the table actually mentioned in the query, since most
5361  * likely the user does have that permission. Note that
5362  * whole-table select privilege on the parent doesn't
5363  * quite guarantee that the user could read all columns of
5364  * the child. But in practice it's unlikely that any
5365  * interesting security violation could result from
5366  * allowing access to the expression stats, so we allow it
5367  * anyway. See similar code in examine_simple_variable()
5368  * for additional comments.
5369  */
5370  if (!vardata->acl_ok &&
5371  root->append_rel_array != NULL)
5372  {
5373  AppendRelInfo *appinfo;
5374  Index varno = onerel->relid;
5375 
5376  appinfo = root->append_rel_array[varno];
5377  while (appinfo &&
5378  planner_rt_fetch(appinfo->parent_relid,
5379  root)->rtekind == RTE_RELATION)
5380  {
5381  varno = appinfo->parent_relid;
5382  appinfo = root->append_rel_array[varno];
5383  }
5384  if (varno != onerel->relid)
5385  {
5386  /* Repeat access check on this rel */
5387  rte = planner_rt_fetch(varno, root);
5388  Assert(rte->rtekind == RTE_RELATION);
5389 
5390  vardata->acl_ok =
5391  rte->securityQuals == NIL &&
5392  (pg_class_aclcheck(rte->relid,
5393  userid,
5394  ACL_SELECT) == ACLCHECK_OK);
5395  }
5396  }
5397 
5398  break;
5399  }
5400 
5401  pos++;
5402  }
5403  }
5404  }
5405 }
@ ACLCHECK_OK
Definition: acl.h:183
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4094
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:715
#define bms_is_empty(a)
Definition: bitmapset.h:118
unsigned int Index
Definition: c.h:605
#define MemSet(start, val, len)
Definition: c.h:1011
#define OidIsValid(objectId)
Definition: c.h:766
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
HeapTuple statext_expressions_load(Oid stxoid, bool inh, int idx)
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Oid GetUserId(void)
Definition: miscinit.c:514
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298
@ RTE_RELATION
Definition: parsenodes.h:1017
#define ACL_SELECT
Definition: parsenodes.h:77
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:570
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
bool has_unique_index(RelOptInfo *rel, AttrNumber attno)
Definition: plancat.c:2214
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:172
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
unsigned int Oid
Definition: postgres_ext.h:31
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:414
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:527
static void examine_simple_variable(PlannerInfo *root, Var *var, VariableStatData *vardata)
Definition: selfuncs.c:5417
get_index_stats_hook_type get_index_stats_hook
Definition: selfuncs.c:148
static void ReleaseDummy(HeapTuple tuple)
Definition: selfuncs.c:4982
Index parent_relid
Definition: pathnodes.h:2979
RTEKind rtekind
Definition: parsenodes.h:1047
Index relid
Definition: pathnodes.h:918
List * statlist
Definition: pathnodes.h:946
List * indexlist
Definition: pathnodes.h:944
Oid userid
Definition: pathnodes.h:966
Definition: primnodes.h:248
AttrNumber varattno
Definition: primnodes.h:260
int varno
Definition: primnodes.h:255
int32 atttypmod
Definition: selfuncs.h:94
void(* freefunc)(HeapTuple tuple)
Definition: selfuncs.h:91
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Definition: syscache.c:243
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:113

References VariableStatData::acl_ok, ACL_SELECT, ACLCHECK_OK, arg, Assert, VariableStatData::atttype, VariableStatData::atttypmod, bms_free(), bms_get_singleton_member(), bms_is_empty, bms_is_member(), BoolGetDatum(), elog, equal(), ERROR, examine_simple_variable(), StatisticExtInfo::exprs, exprType(), exprTypmod(), find_base_rel(), find_join_rel(), VariableStatData::freefunc, get_index_stats_hook, GetUserId(), has_unique_index(), HeapTupleIsValid, if(), RelOptInfo::indexlist, RangeTblEntry::inh, StatisticExtInfo::inherit, Int16GetDatum(), IsA, VariableStatData::isunique, StatisticExtInfo::kind, lfirst, list_head(), lnext(), MemSet, NIL, ObjectIdGetDatum(), OidIsValid, AppendRelInfo::parent_relid, pg_class_aclcheck(), planner_rt_fetch, pull_varnos(), VariableStatData::rel, ReleaseDummy(), ReleaseSysCache(), RangeTblEntry::relid, RelOptInfo::relid, root, RTE_RELATION, RangeTblEntry::rtekind, SearchSysCache3(), statext_expressions_load(), RelOptInfo::statlist, StatisticExtInfo::statOid, VariableStatData::statsTuple, RelOptInfo::userid, VariableStatData::var, Var::varattno, Var::varno, and VariableStatData::vartype.

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

◆ generic_restriction_selectivity()

double generic_restriction_selectivity ( PlannerInfo root,
Oid  oproid,
Oid  collation,
List args,
int  varRelid,
double  default_selectivity 
)

Definition at line 914 of file selfuncs.c.

917 {
918  double selec;
919  VariableStatData vardata;
920  Node *other;
921  bool varonleft;
922 
923  /*
924  * If expression is not variable OP something or something OP variable,
925  * then punt and return the default estimate.
926  */
927  if (!get_restriction_variable(root, args, varRelid,
928  &vardata, &other, &varonleft))
929  return default_selectivity;
930 
931  /*
932  * If the something is a NULL constant, assume operator is strict and
933  * return zero, ie, operator will never return TRUE.
934  */
935  if (IsA(other, Const) &&
936  ((Const *) other)->constisnull)
937  {
938  ReleaseVariableStats(vardata);
939  return 0.0;
940  }
941 
942  if (IsA(other, Const))
943  {
944  /* Variable is being compared to a known non-null constant */
945  Datum constval = ((Const *) other)->constvalue;
946  FmgrInfo opproc;
947  double mcvsum;
948  double mcvsel;
949  double nullfrac;
950  int hist_size;
951 
952  fmgr_info(get_opcode(oproid), &opproc);
953 
954  /*
955  * Calculate the selectivity for the column's most common values.
956  */
957  mcvsel = mcv_selectivity(&vardata, &opproc, collation,
958  constval, varonleft,
959  &mcvsum);
960 
961  /*
962  * If the histogram is large enough, see what fraction of it matches
963  * the query, and assume that's representative of the non-MCV
964  * population. Otherwise use the default selectivity for the non-MCV
965  * population.
966  */
967  selec = histogram_selectivity(&vardata, &opproc, collation,
968  constval, varonleft,
969  10, 1, &hist_size);
970  if (selec < 0)
971  {
972  /* Nope, fall back on default */
973  selec = default_selectivity;
974  }
975  else if (hist_size < 100)
976  {
977  /*
978  * For histogram sizes from 10 to 100, we combine the histogram
979  * and default selectivities, putting increasingly more trust in
980  * the histogram for larger sizes.
981  */
982  double hist_weight = hist_size / 100.0;
983 
984  selec = selec * hist_weight +
985  default_selectivity * (1.0 - hist_weight);
986  }
987 
988  /* In any case, don't believe extremely small or large estimates. */
989  if (selec < 0.0001)
990  selec = 0.0001;
991  else if (selec > 0.9999)
992  selec = 0.9999;
993 
994  /* Don't forget to account for nulls. */
995  if (HeapTupleIsValid(vardata.statsTuple))
996  nullfrac = ((Form_pg_statistic) GETSTRUCT(vardata.statsTuple))->stanullfrac;
997  else
998  nullfrac = 0.0;
999 
1000  /*
1001  * Now merge the results from the MCV and histogram calculations,
1002  * realizing that the histogram covers only the non-null values that
1003  * are not listed in MCV.
1004  */
1005  selec *= 1.0 - nullfrac - mcvsum;
1006  selec += mcvsel;
1007  }
1008  else
1009  {
1010  /* Comparison value is not constant, so we can't do anything */
1011  selec = default_selectivity;
1012  }
1013 
1014  ReleaseVariableStats(vardata);
1015 
1016  /* result should be in range, but make sure... */
1017  CLAMP_PROBABILITY(selec);
1018 
1019  return selec;
1020 }
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1285
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
Definition: selfuncs.c:4894
double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Oid collation, Datum constval, bool varonleft, double *sumcommonp)
Definition: selfuncs.c:732
double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Oid collation, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
Definition: selfuncs.c:823
Definition: fmgr.h:57

References generate_unaccent_rules::args, CLAMP_PROBABILITY, fmgr_info(), get_opcode(), get_restriction_variable(), GETSTRUCT, HeapTupleIsValid, histogram_selectivity(), IsA, mcv_selectivity(), ReleaseVariableStats, root, and VariableStatData::statsTuple.

Referenced by ltreeparentsel(), and matchingsel().

◆ genericcostestimate()

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

Definition at line 6555 of file selfuncs.c.

6559 {
6560  IndexOptInfo *index = path->indexinfo;
6561  List *indexQuals = get_quals_from_indexclauses(path->indexclauses);
6562  List *indexOrderBys = path->indexorderbys;
6563  Cost indexStartupCost;
6564  Cost indexTotalCost;
6565  Selectivity indexSelectivity;
6566  double indexCorrelation;
6567  double numIndexPages;
6568  double numIndexTuples;
6569  double spc_random_page_cost;
6570  double num_sa_scans;
6571  double num_outer_scans;
6572  double num_scans;
6573  double qual_op_cost;
6574  double qual_arg_cost;
6575  List *selectivityQuals;
6576  ListCell *l;
6577 
6578  /*
6579  * If the index is partial, AND the index predicate with the explicitly
6580  * given indexquals to produce a more accurate idea of the index
6581  * selectivity.
6582  */
6583  selectivityQuals = add_predicate_to_index_quals(index, indexQuals);
6584 
6585  /*
6586  * If caller didn't give us an estimate for ScalarArrayOpExpr index scans,
6587  * just assume that the number of index descents is the number of distinct
6588  * combinations of array elements from all of the scan's SAOP clauses.
6589  */
6590  num_sa_scans = costs->num_sa_scans;
6591  if (num_sa_scans < 1)
6592  {
6593  num_sa_scans = 1;
6594  foreach(l, indexQuals)
6595  {
6596  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
6597 
6598  if (IsA(rinfo->clause, ScalarArrayOpExpr))
6599  {
6600  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
6601  double alength = estimate_array_length(root, lsecond(saop->args));
6602 
6603  if (alength > 1)
6604  num_sa_scans *= alength;
6605  }
6606  }
6607  }
6608 
6609  /* Estimate the fraction of main-table tuples that will be visited */
6610  indexSelectivity = clauselist_selectivity(root, selectivityQuals,
6611  index->rel->relid,
6612  JOIN_INNER,
6613  NULL);
6614 
6615  /*
6616  * If caller didn't give us an estimate, estimate the number of index
6617  * tuples that will be visited. We do it in this rather peculiar-looking
6618  * way in order to get the right answer for partial indexes.
6619  */
6620  numIndexTuples = costs->numIndexTuples;
6621  if (numIndexTuples <= 0.0)
6622  {
6623  numIndexTuples = indexSelectivity * index->rel->tuples;
6624 
6625  /*
6626  * The above calculation counts all the tuples visited across all
6627  * scans induced by ScalarArrayOpExpr nodes. We want to consider the
6628  * average per-indexscan number, so adjust. This is a handy place to
6629  * round to integer, too. (If caller supplied tuple estimate, it's
6630  * responsible for handling these considerations.)
6631  */
6632  numIndexTuples = rint(numIndexTuples / num_sa_scans);
6633  }
6634 
6635  /*
6636  * We can bound the number of tuples by the index size in any case. Also,
6637  * always estimate at least one tuple is touched, even when
6638  * indexSelectivity estimate is tiny.
6639  */
6640  if (numIndexTuples > index->tuples)
6641  numIndexTuples = index->tuples;
6642  if (numIndexTuples < 1.0)
6643  numIndexTuples = 1.0;
6644 
6645  /*
6646  * Estimate the number of index pages that will be retrieved.
6647  *
6648  * We use the simplistic method of taking a pro-rata fraction of the total
6649  * number of index pages. In effect, this counts only leaf pages and not
6650  * any overhead such as index metapage or upper tree levels.
6651  *
6652  * In practice access to upper index levels is often nearly free because
6653  * those tend to stay in cache under load; moreover, the cost involved is
6654  * highly dependent on index type. We therefore ignore such costs here
6655  * and leave it to the caller to add a suitable charge if needed.
6656  */
6657  if (index->pages > 1 && index->tuples > 1)
6658  numIndexPages = ceil(numIndexTuples * index->pages / index->tuples);
6659  else
6660  numIndexPages = 1.0;
6661 
6662  /* fetch estimated page cost for tablespace containing index */
6663  get_tablespace_page_costs(index->reltablespace,
6664  &spc_random_page_cost,
6665  NULL);
6666 
6667  /*
6668  * Now compute the disk access costs.
6669  *
6670  * The above calculations are all per-index-scan. However, if we are in a
6671  * nestloop inner scan, we can expect the scan to be repeated (with
6672  * different search keys) for each row of the outer relation. Likewise,
6673  * ScalarArrayOpExpr quals result in multiple index scans. This creates
6674  * the potential for cache effects to reduce the number of disk page
6675  * fetches needed. We want to estimate the average per-scan I/O cost in
6676  * the presence of caching.
6677  *
6678  * We use the Mackert-Lohman formula (see costsize.c for details) to
6679  * estimate the total number of page fetches that occur. While this
6680  * wasn't what it was designed for, it seems a reasonable model anyway.
6681  * Note that we are counting pages not tuples anymore, so we take N = T =
6682  * index size, as if there were one "tuple" per page.
6683  */
6684  num_outer_scans = loop_count;
6685  num_scans = num_sa_scans * num_outer_scans;
6686 
6687  if (num_scans > 1)
6688  {
6689  double pages_fetched;
6690 
6691  /* total page fetches ignoring cache effects */
6692  pages_fetched = numIndexPages * num_scans;
6693 
6694  /* use Mackert and Lohman formula to adjust for cache effects */
6695  pages_fetched = index_pages_fetched(pages_fetched,
6696  index->pages,
6697  (double) index->pages,
6698  root);
6699 
6700  /*
6701  * Now compute the total disk access cost, and then report a pro-rated
6702  * share for each outer scan. (Don't pro-rate for ScalarArrayOpExpr,
6703  * since that's internal to the indexscan.)
6704  */
6705  indexTotalCost = (pages_fetched * spc_random_page_cost)
6706  / num_outer_scans;
6707  }
6708  else
6709  {
6710  /*
6711  * For a single index scan, we just charge spc_random_page_cost per
6712  * page touched.
6713  */
6714  indexTotalCost = numIndexPages * spc_random_page_cost;
6715  }
6716 
6717  /*
6718  * CPU cost: any complex expressions in the indexquals will need to be
6719  * evaluated once at the start of the scan to reduce them to runtime keys
6720  * to pass to the index AM (see nodeIndexscan.c). We model the per-tuple
6721  * CPU costs as cpu_index_tuple_cost plus one cpu_operator_cost per
6722  * indexqual operator. Because we have numIndexTuples as a per-scan
6723  * number, we have to multiply by num_sa_scans to get the correct result
6724  * for ScalarArrayOpExpr cases. Similarly add in costs for any index
6725  * ORDER BY expressions.
6726  *
6727  * Note: this neglects the possible costs of rechecking lossy operators.
6728  * Detecting that that might be needed seems more expensive than it's
6729  * worth, though, considering all the other inaccuracies here ...
6730  */
6731  qual_arg_cost = index_other_operands_eval_cost(root, indexQuals) +
6732  index_other_operands_eval_cost(root, indexOrderBys);
6733  qual_op_cost = cpu_operator_cost *
6734  (list_length(indexQuals) + list_length(indexOrderBys));
6735 
6736  indexStartupCost = qual_arg_cost;
6737  indexTotalCost += qual_arg_cost;
6738  indexTotalCost += numIndexTuples * num_sa_scans * (cpu_index_tuple_cost + qual_op_cost);
6739 
6740  /*
6741  * Generic assumption about index correlation: there isn't any.
6742  */
6743  indexCorrelation = 0.0;
6744 
6745  /*
6746  * Return everything to caller.
6747  */
6748  costs->indexStartupCost = indexStartupCost;
6749  costs->indexTotalCost = indexTotalCost;
6750  costs->indexSelectivity = indexSelectivity;
6751  costs->indexCorrelation = indexCorrelation;
6752  costs->numIndexPages = numIndexPages;
6753  costs->numIndexTuples = numIndexTuples;
6754  costs->spc_random_page_cost = spc_random_page_cost;
6755  costs->num_sa_scans = num_sa_scans;
6756 }
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:100
double cpu_operator_cost
Definition: costsize.c:134
double index_pages_fetched(double tuples_fetched, BlockNumber pages, double index_pages, PlannerInfo *root)
Definition: costsize.c:908
double cpu_index_tuple_cost
Definition: costsize.c:133
double Cost
Definition: nodes.h:251
@ JOIN_INNER
Definition: nodes.h:293
#define lsecond(l)
Definition: pg_list.h:183
List * get_quals_from_indexclauses(List *indexclauses)
Definition: selfuncs.c:6471
double estimate_array_length(PlannerInfo *root, Node *arrayexpr)
Definition: selfuncs.c:2139
List * add_predicate_to_index_quals(IndexOptInfo *index, List *indexQuals)
Definition: selfuncs.c:6778
Cost index_other_operands_eval_cost(PlannerInfo *root, List *indexquals)
Definition: selfuncs.c:6501
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition: spccache.c:182
Selectivity indexSelectivity
Definition: selfuncs.h:127
Cost indexStartupCost
Definition: selfuncs.h:125
double indexCorrelation
Definition: selfuncs.h:128
double spc_random_page_cost
Definition: selfuncs.h:133
double num_sa_scans
Definition: selfuncs.h:134
Cost indexTotalCost
Definition: selfuncs.h:126
double numIndexPages
Definition: selfuncs.h:131
double numIndexTuples
Definition: selfuncs.h:132
List * indexclauses
Definition: pathnodes.h:1721
List * indexorderbys
Definition: pathnodes.h:1722
IndexOptInfo * indexinfo
Definition: pathnodes.h:1720
Expr * clause
Definition: pathnodes.h:2574

References add_predicate_to_index_quals(), ScalarArrayOpExpr::args, RestrictInfo::clause, clauselist_selectivity(), cpu_index_tuple_cost, cpu_operator_cost, estimate_array_length(), get_quals_from_indexclauses(), get_tablespace_page_costs(), index_other_operands_eval_cost(), index_pages_fetched(), IndexPath::indexclauses, GenericCosts::indexCorrelation, IndexPath::indexinfo, IndexPath::indexorderbys, GenericCosts::indexSelectivity, GenericCosts::indexStartupCost, GenericCosts::indexTotalCost, IsA, JOIN_INNER, lfirst, list_length(), lsecond, GenericCosts::num_sa_scans, GenericCosts::numIndexPages, GenericCosts::numIndexTuples, root, and GenericCosts::spc_random_page_cost.

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

◆ get_join_variables()

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

Definition at line 4954 of file selfuncs.c.

4957 {
4958  Node *left,
4959  *right;
4960 
4961  if (list_length(args) != 2)
4962  elog(ERROR, "join operator should take two arguments");
4963 
4964  left = (Node *) linitial(args);
4965  right = (Node *) lsecond(args);
4966 
4967  examine_variable(root, left, 0, vardata1);
4968  examine_variable(root, right, 0, vardata2);
4969 
4970  if (vardata1->rel &&
4971  bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
4972  *join_is_reversed = true; /* var1 is on RHS */
4973  else if (vardata2->rel &&
4974  bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
4975  *join_is_reversed = true; /* var2 is on LHS */
4976  else
4977  *join_is_reversed = false;
4978 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
Relids relids
Definition: pathnodes.h:871
Relids syn_lefthand
Definition: pathnodes.h:2906
Relids syn_righthand
Definition: pathnodes.h:2907

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

Referenced by eqjoinsel(), neqjoinsel(), and networkjoinsel().

◆ get_quals_from_indexclauses()

List* get_quals_from_indexclauses ( List indexclauses)

Definition at line 6471 of file selfuncs.c.

6472 {
6473  List *result = NIL;
6474  ListCell *lc;
6475 
6476  foreach(lc, indexclauses)
6477  {
6478  IndexClause *iclause = lfirst_node(IndexClause, lc);
6479  ListCell *lc2;
6480 
6481  foreach(lc2, iclause->indexquals)
6482  {
6483  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
6484 
6485  result = lappend(result, rinfo);
6486  }
6487  }
6488  return result;
6489 }
#define lfirst_node(type, lc)
Definition: pg_list.h:176
List * indexquals
Definition: pathnodes.h:1769

References IndexClause::indexquals, lappend(), lfirst_node, and NIL.

Referenced by brincostestimate(), genericcostestimate(), and gincostestimate().

◆ get_restriction_variable()

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

Definition at line 4894 of file selfuncs.c.

4897 {
4898  Node *left,
4899  *right;
4900  VariableStatData rdata;
4901 
4902  /* Fail if not a binary opclause (probably shouldn't happen) */
4903  if (list_length(args) != 2)
4904  return false;
4905 
4906  left = (Node *) linitial(args);
4907  right = (Node *) lsecond(args);
4908 
4909  /*
4910  * Examine both sides. Note that when varRelid is nonzero, Vars of other
4911  * relations will be treated as pseudoconstants.
4912  */
4913  examine_variable(root, left, varRelid, vardata);
4914  examine_variable(root, right, varRelid, &rdata);
4915 
4916  /*
4917  * If one side is a variable and the other not, we win.
4918  */
4919  if (vardata->rel && rdata.rel == NULL)
4920  {
4921  *varonleft = true;
4922  *other = estimate_expression_value(root, rdata.var);
4923  /* Assume we need no ReleaseVariableStats(rdata) here */
4924  return true;
4925  }
4926 
4927  if (vardata->rel == NULL && rdata.rel)
4928  {
4929  *varonleft = false;
4930  *other = estimate_expression_value(root, vardata->var);
4931  /* Assume we need no ReleaseVariableStats(*vardata) here */
4932  *vardata = rdata;
4933  return true;
4934  }
4935 
4936  /* Oops, clause has wrong structure (probably var op var) */
4937  ReleaseVariableStats(*vardata);
4938  ReleaseVariableStats(rdata);
4939 
4940  return false;
4941 }
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2395

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

Referenced by _int_matchsel(), arraycontsel(), eqsel_internal(), generic_restriction_selectivity(), multirangesel(), networksel(), patternsel_common(), rangesel(), scalarineqsel_wrapper(), and tsmatchsel().

◆ get_variable_numdistinct()

double get_variable_numdistinct ( VariableStatData vardata,
bool isdefault 
)

Definition at line 5775 of file selfuncs.c.

5776 {
5777  double stadistinct;
5778  double stanullfrac = 0.0;
5779  double ntuples;
5780 
5781  *isdefault = false;
5782 
5783  /*
5784  * Determine the stadistinct value to use. There are cases where we can
5785  * get an estimate even without a pg_statistic entry, or can get a better
5786  * value than is in pg_statistic. Grab stanullfrac too if we can find it
5787  * (otherwise, assume no nulls, for lack of any better idea).
5788  */
5789  if (HeapTupleIsValid(vardata->statsTuple))
5790  {
5791  /* Use the pg_statistic entry */
5792  Form_pg_statistic stats;
5793 
5794  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
5795  stadistinct = stats->stadistinct;
5796  stanullfrac = stats->stanullfrac;
5797  }
5798  else if (vardata->vartype == BOOLOID)
5799  {
5800  /*
5801  * Special-case boolean columns: presumably, two distinct values.
5802  *
5803  * Are there any other datatypes we should wire in special estimates
5804  * for?
5805  */
5806  stadistinct = 2.0;
5807  }
5808  else if (vardata->rel && vardata->rel->rtekind == RTE_VALUES)
5809  {
5810  /*
5811  * If the Var represents a column of a VALUES RTE, assume it's unique.
5812  * This could of course be very wrong, but it should tend to be true
5813  * in well-written queries. We could consider examining the VALUES'
5814  * contents to get some real statistics; but that only works if the
5815  * entries are all constants, and it would be pretty expensive anyway.
5816  */
5817  stadistinct = -1.0; /* unique (and all non null) */
5818  }
5819  else
5820  {
5821  /*
5822  * We don't keep statistics for system columns, but in some cases we
5823  * can infer distinctness anyway.
5824  */
5825  if (vardata->var && IsA(vardata->var, Var))
5826  {
5827  switch (((Var *) vardata->var)->varattno)
5828  {
5830  stadistinct = -1.0; /* unique (and all non null) */
5831  break;
5833  stadistinct = 1.0; /* only 1 value */
5834  break;
5835  default:
5836  stadistinct = 0.0; /* means "unknown" */
5837  break;
5838  }
5839  }
5840  else
5841  stadistinct = 0.0; /* means "unknown" */
5842 
5843  /*
5844  * XXX consider using estimate_num_groups on expressions?
5845  */
5846  }
5847 
5848  /*
5849  * If there is a unique index or DISTINCT clause for the variable, assume
5850  * it is unique no matter what pg_statistic says; the statistics could be
5851  * out of date, or we might have found a partial unique index that proves
5852  * the var is unique for this query. However, we'd better still believe
5853  * the null-fraction statistic.
5854  */
5855  if (vardata->isunique)
5856  stadistinct = -1.0 * (1.0 - stanullfrac);
5857 
5858  /*
5859  * If we had an absolute estimate, use that.
5860  */
5861  if (stadistinct > 0.0)
5862  return clamp_row_est(stadistinct);
5863 
5864  /*
5865  * Otherwise we need to get the relation size; punt if not available.
5866  */
5867  if (vardata->rel == NULL)
5868  {
5869  *isdefault = true;
5870  return DEFAULT_NUM_DISTINCT;
5871  }
5872  ntuples = vardata->rel->tuples;
5873  if (ntuples <= 0.0)
5874  {
5875  *isdefault = true;
5876  return DEFAULT_NUM_DISTINCT;
5877  }
5878 
5879  /*
5880  * If we had a relative estimate, use that.
5881  */
5882  if (stadistinct < 0.0)
5883  return clamp_row_est(-stadistinct * ntuples);
5884 
5885  /*
5886  * With no data, estimate ndistinct = ntuples if the table is small, else
5887  * use default. We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
5888  * that the behavior isn't discontinuous.
5889  */
5890  if (ntuples < DEFAULT_NUM_DISTINCT)
5891  return clamp_row_est(ntuples);
5892 
5893  *isdefault = true;
5894  return DEFAULT_NUM_DISTINCT;
5895 }
@ RTE_VALUES
Definition: parsenodes.h:1022
#define DEFAULT_NUM_DISTINCT
Definition: selfuncs.h:52
RTEKind rtekind
Definition: pathnodes.h:922
#define TableOidAttributeNumber
Definition: sysattr.h:26
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21

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

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

◆ histogram_selectivity()

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

Definition at line 823 of file selfuncs.c.

828 {
829  double result;
830  AttStatsSlot sslot;
831 
832  /* check sanity of parameters */
833  Assert(n_skip >= 0);
834  Assert(min_hist_size > 2 * n_skip);
835 
836  if (HeapTupleIsValid(vardata->statsTuple) &&
837  statistic_proc_security_check(vardata, opproc->fn_oid) &&
838  get_attstatsslot(&sslot, vardata->statsTuple,
839  STATISTIC_KIND_HISTOGRAM, InvalidOid,
841  {
842  *hist_size = sslot.nvalues;
843  if (sslot.nvalues >= min_hist_size)
844  {
845  LOCAL_FCINFO(fcinfo, 2);
846  int nmatch = 0;
847  int i;
848 
849  /*
850  * We invoke the opproc "by hand" so that we won't fail on NULL
851  * results. Such cases won't arise for normal comparison
852  * functions, but generic_restriction_selectivity could perhaps be
853  * used with operators that can return NULL. A small side benefit
854  * is to not need to re-initialize the fcinfo struct from scratch
855  * each time.
856  */
857  InitFunctionCallInfoData(*fcinfo, opproc, 2, collation,
858  NULL, NULL);
859  fcinfo->args[0].isnull = false;
860  fcinfo->args[1].isnull = false;
861  /* be careful to apply operator right way 'round */
862  if (varonleft)
863  fcinfo->args[1].value = constval;
864  else
865  fcinfo->args[0].value = constval;
866 
867  for (i = n_skip; i < sslot.nvalues - n_skip; i++)
868  {
869  Datum fresult;
870 
871  if (varonleft)
872  fcinfo->args[0].value = sslot.values[i];
873  else
874  fcinfo->args[1].value = sslot.values[i];
875  fcinfo->isnull = false;
876  fresult = FunctionCallInvoke(fcinfo);
877  if (!fcinfo->isnull && DatumGetBool(fresult))
878  nmatch++;
879  }
880  result = ((double) nmatch) / ((double) (sslot.nvalues - 2 * n_skip));
881  }
882  else
883  result = -1;
884  free_attstatsslot(&sslot);
885  }
886  else
887  {
888  *hist_size = 0;
889  result = -1;
890  }
891 
892  return result;
893 }
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
Definition: selfuncs.c:5746
Oid fn_oid
Definition: fmgr.h:59

References Assert, ATTSTATSSLOT_VALUES, DatumGetBool(), FmgrInfo::fn_oid, free_attstatsslot(), FunctionCallInvoke, get_attstatsslot(), HeapTupleIsValid, i, InitFunctionCallInfoData, InvalidOid, LOCAL_FCINFO, AttStatsSlot::nvalues, statistic_proc_security_check(), VariableStatData::statsTuple, and AttStatsSlot::values.

Referenced by generic_restriction_selectivity(), and patternsel_common().

◆ index_other_operands_eval_cost()

Cost index_other_operands_eval_cost ( PlannerInfo root,
List indexquals 
)

Definition at line 6501 of file selfuncs.c.

6502 {
6503  Cost qual_arg_cost = 0;
6504  ListCell *lc;
6505 
6506  foreach(lc, indexquals)
6507  {
6508  Expr *clause = (Expr *) lfirst(lc);
6509  Node *other_operand;
6510  QualCost index_qual_cost;
6511 
6512  /*
6513  * Index quals will have RestrictInfos, indexorderbys won't. Look
6514  * through RestrictInfo if present.
6515  */
6516  if (IsA(clause, RestrictInfo))
6517  clause = ((RestrictInfo *) clause)->clause;
6518 
6519  if (IsA(clause, OpExpr))
6520  {
6521  OpExpr *op = (OpExpr *) clause;
6522 
6523  other_operand = (Node *) lsecond(op->args);
6524  }
6525  else if (IsA(clause, RowCompareExpr))
6526  {
6527  RowCompareExpr *rc = (RowCompareExpr *) clause;
6528 
6529  other_operand = (Node *) rc->rargs;
6530  }
6531  else if (IsA(clause, ScalarArrayOpExpr))
6532  {
6533  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
6534 
6535  other_operand = (Node *) lsecond(saop->args);
6536  }
6537  else if (IsA(clause, NullTest))
6538  {
6539  other_operand = NULL;
6540  }
6541  else
6542  {
6543  elog(ERROR, "unsupported indexqual type: %d",
6544  (int) nodeTag(clause));
6545  other_operand = NULL; /* keep compiler quiet */
6546  }
6547 
6548  cost_qual_eval_node(&index_qual_cost, other_operand, root);
6549  qual_arg_cost += index_qual_cost.startup + index_qual_cost.per_tuple;
6550  }
6551  return qual_arg_cost;
6552 }
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:4758
#define nodeTag(nodeptr)
Definition: nodes.h:133
List * args
Definition: primnodes.h:836
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47

References OpExpr::args, ScalarArrayOpExpr::args, cost_qual_eval_node(), elog, ERROR, IsA, lfirst, lsecond, nodeTag, QualCost::per_tuple, RowCompareExpr::rargs, root, and QualCost::startup.

Referenced by brincostestimate(), genericcostestimate(), and gincostestimate().

◆ ineq_histogram_selectivity()

double ineq_histogram_selectivity ( PlannerInfo root,
VariableStatData vardata,
Oid  opoid,
FmgrInfo opproc,
bool  isgt,
bool  iseq,
Oid  collation,
Datum  constval,
Oid  consttype 
)

Definition at line 1041 of file selfuncs.c.

1046 {
1047  double hist_selec;
1048  AttStatsSlot sslot;
1049 
1050  hist_selec = -1.0;
1051 
1052  /*
1053  * Someday, ANALYZE might store more than one histogram per rel/att,
1054  * corresponding to more than one possible sort ordering defined for the
1055  * column type. Right now, we know there is only one, so just grab it and
1056  * see if it matches the query.
1057  *
1058  * Note that we can't use opoid as search argument; the staop appearing in
1059  * pg_statistic will be for the relevant '<' operator, but what we have
1060  * might be some other inequality operator such as '>='. (Even if opoid
1061  * is a '<' operator, it could be cross-type.) Hence we must use
1062  * comparison_ops_are_compatible() to see if the operators match.
1063  */
1064  if (HeapTupleIsValid(vardata->statsTuple) &&
1065  statistic_proc_security_check(vardata, opproc->fn_oid) &&
1066  get_attstatsslot(&sslot, vardata->statsTuple,
1067  STATISTIC_KIND_HISTOGRAM, InvalidOid,
1069  {
1070  if (sslot.nvalues > 1 &&
1071  sslot.stacoll == collation &&
1072  comparison_ops_are_compatible(sslot.staop, opoid))
1073  {
1074  /*
1075  * Use binary search to find the desired location, namely the
1076  * right end of the histogram bin containing the comparison value,
1077  * which is the leftmost entry for which the comparison operator
1078  * succeeds (if isgt) or fails (if !isgt).
1079  *
1080  * In this loop, we pay no attention to whether the operator iseq
1081  * or not; that detail will be mopped up below. (We cannot tell,
1082  * anyway, whether the operator thinks the values are equal.)
1083  *
1084  * If the binary search accesses the first or last histogram
1085  * entry, we try to replace that endpoint with the true column min
1086  * or max as found by get_actual_variable_range(). This
1087  * ameliorates misestimates when the min or max is moving as a
1088  * result of changes since the last ANALYZE. Note that this could
1089  * result in effectively including MCVs into the histogram that
1090  * weren't there before, but we don't try to correct for that.
1091  */
1092  double histfrac;
1093  int lobound = 0; /* first possible slot to search */
1094  int hibound = sslot.nvalues; /* last+1 slot to search */
1095  bool have_end = false;
1096 
1097  /*
1098  * If there are only two histogram entries, we'll want up-to-date
1099  * values for both. (If there are more than two, we need at most
1100  * one of them to be updated, so we deal with that within the
1101  * loop.)
1102  */
1103  if (sslot.nvalues == 2)
1104  have_end = get_actual_variable_range(root,
1105  vardata,
1106  sslot.staop,
1107  collation,
1108  &sslot.values[0],
1109  &sslot.values[1]);
1110 
1111  while (lobound < hibound)
1112  {
1113  int probe = (lobound + hibound) / 2;
1114  bool ltcmp;
1115 
1116  /*
1117  * If we find ourselves about to compare to the first or last
1118  * histogram entry, first try to replace it with the actual
1119  * current min or max (unless we already did so above).
1120  */
1121  if (probe == 0 && sslot.nvalues > 2)
1122  have_end = get_actual_variable_range(root,
1123  vardata,
1124  sslot.staop,
1125  collation,
1126  &sslot.values[0],
1127  NULL);
1128  else if (probe == sslot.nvalues - 1 && sslot.nvalues > 2)
1129  have_end = get_actual_variable_range(root,
1130  vardata,
1131  sslot.staop,
1132  collation,
1133  NULL,
1134  &sslot.values[probe]);
1135 
1136  ltcmp = DatumGetBool(FunctionCall2Coll(opproc,
1137  collation,
1138  sslot.values[probe],
1139  constval));
1140  if (isgt)
1141  ltcmp = !ltcmp;
1142  if (ltcmp)
1143  lobound = probe + 1;
1144  else
1145  hibound = probe;
1146  }
1147 
1148  if (lobound <= 0)
1149  {
1150  /*
1151  * Constant is below lower histogram boundary. More
1152  * precisely, we have found that no entry in the histogram
1153  * satisfies the inequality clause (if !isgt) or they all do
1154  * (if isgt). We estimate that that's true of the entire
1155  * table, so set histfrac to 0.0 (which we'll flip to 1.0
1156  * below, if isgt).
1157  */
1158  histfrac = 0.0;
1159  }
1160  else if (lobound >= sslot.nvalues)
1161  {
1162  /*
1163  * Inverse case: constant is above upper histogram boundary.
1164  */
1165  histfrac = 1.0;
1166  }
1167  else
1168  {
1169  /* We have values[i-1] <= constant <= values[i]. */
1170  int i = lobound;
1171  double eq_selec = 0;
1172  double val,
1173  high,
1174  low;
1175  double binfrac;
1176 
1177  /*
1178  * In the cases where we'll need it below, obtain an estimate
1179  * of the selectivity of "x = constval". We use a calculation
1180  * similar to what var_eq_const() does for a non-MCV constant,
1181  * ie, estimate that all distinct non-MCV values occur equally
1182  * often. But multiplication by "1.0 - sumcommon - nullfrac"
1183  * will be done by our caller, so we shouldn't do that here.
1184  * Therefore we can't try to clamp the estimate by reference
1185  * to the least common MCV; the result would be too small.
1186  *
1187  * Note: since this is effectively assuming that constval
1188  * isn't an MCV, it's logically dubious if constval in fact is
1189  * one. But we have to apply *some* correction for equality,
1190  * and anyway we cannot tell if constval is an MCV, since we
1191  * don't have a suitable equality operator at hand.
1192  */
1193  if (i == 1 || isgt == iseq)
1194  {
1195  double otherdistinct;
1196  bool isdefault;
1197  AttStatsSlot mcvslot;
1198 
1199  /* Get estimated number of distinct values */
1200  otherdistinct = get_variable_numdistinct(vardata,
1201  &isdefault);
1202 
1203  /* Subtract off the number of known MCVs */
1204  if (get_attstatsslot(&mcvslot, vardata->statsTuple,
1205  STATISTIC_KIND_MCV, InvalidOid,
1207  {
1208  otherdistinct -= mcvslot.nnumbers;
1209  free_attstatsslot(&mcvslot);
1210  }
1211 
1212  /* If result doesn't seem sane, leave eq_selec at 0 */
1213  if (otherdistinct > 1)
1214  eq_selec = 1.0 / otherdistinct;
1215  }
1216 
1217  /*
1218  * Convert the constant and the two nearest bin boundary
1219  * values to a uniform comparison scale, and do a linear
1220  * interpolation within this bin.
1221  */
1222  if (convert_to_scalar(constval, consttype, collation,
1223  &val,
1224  sslot.values[i - 1], sslot.values[i],
1225  vardata->vartype,
1226  &low, &high))
1227  {
1228  if (high <= low)
1229  {
1230  /* cope if bin boundaries appear identical */
1231  binfrac = 0.5;
1232  }
1233  else if (val <= low)
1234  binfrac = 0.0;
1235  else if (val >= high)
1236  binfrac = 1.0;
1237  else
1238  {
1239  binfrac = (val - low) / (high - low);
1240 
1241  /*
1242  * Watch out for the possibility that we got a NaN or
1243  * Infinity from the division. This can happen
1244  * despite the previous checks, if for example "low"
1245  * is -Infinity.
1246  */
1247  if (isnan(binfrac) ||
1248  binfrac < 0.0 || binfrac > 1.0)
1249  binfrac = 0.5;
1250  }
1251  }
1252  else
1253  {
1254  /*
1255  * Ideally we'd produce an error here, on the grounds that
1256  * the given operator shouldn't have scalarXXsel
1257  * registered as its selectivity func unless we can deal
1258  * with its operand types. But currently, all manner of
1259  * stuff is invoking scalarXXsel, so give a default
1260  * estimate until that can be fixed.
1261  */
1262  binfrac = 0.5;
1263  }
1264 
1265  /*
1266  * Now, compute the overall selectivity across the values
1267  * represented by the histogram. We have i-1 full bins and
1268  * binfrac partial bin below the constant.
1269  */
1270  histfrac = (double) (i - 1) + binfrac;
1271  histfrac /= (double) (sslot.nvalues - 1);
1272 
1273  /*
1274  * At this point, histfrac is an estimate of the fraction of
1275  * the population represented by the histogram that satisfies
1276  * "x <= constval". Somewhat remarkably, this statement is
1277  * true regardless of which operator we were doing the probes
1278  * with, so long as convert_to_scalar() delivers reasonable
1279  * results. If the probe constant is equal to some histogram
1280  * entry, we would have considered the bin to the left of that
1281  * entry if probing with "<" or ">=", or the bin to the right
1282  * if probing with "<=" or ">"; but binfrac would have come
1283  * out as 1.0 in the first case and 0.0 in the second, leading
1284  * to the same histfrac in either case. For probe constants
1285  * between histogram entries, we find the same bin and get the
1286  * same estimate with any operator.
1287  *
1288  * The fact that the estimate corresponds to "x <= constval"
1289  * and not "x < constval" is because of the way that ANALYZE
1290  * constructs the histogram: each entry is, effectively, the
1291  * rightmost value in its sample bucket. So selectivity
1292  * values that are exact multiples of 1/(histogram_size-1)
1293  * should be understood as estimates including a histogram
1294  * entry plus everything to its left.
1295  *
1296  * However, that breaks down for the first histogram entry,
1297  * which necessarily is the leftmost value in its sample
1298  * bucket. That means the first histogram bin is slightly
1299  * narrower than the rest, by an amount equal to eq_selec.
1300  * Another way to say that is that we want "x <= leftmost" to
1301  * be estimated as eq_selec not zero. So, if we're dealing
1302  * with the first bin (i==1), rescale to make that true while
1303  * adjusting the rest of that bin linearly.
1304  */
1305  if (i == 1)
1306  histfrac += eq_selec * (1.0 - binfrac);
1307 
1308  /*
1309  * "x <= constval" is good if we want an estimate for "<=" or
1310  * ">", but if we are estimating for "<" or ">=", we now need
1311  * to decrease the estimate by eq_selec.
1312  */
1313  if (isgt == iseq)
1314  histfrac -= eq_selec;
1315  }
1316 
1317  /*
1318  * Now the estimate is finished for "<" and "<=" cases. If we are
1319  * estimating for ">" or ">=", flip it.
1320  */
1321  hist_selec = isgt ? (1.0 - histfrac) : histfrac;
1322 
1323  /*
1324  * The histogram boundaries are only approximate to begin with,
1325  * and may well be out of date anyway. Therefore, don't believe
1326  * extremely small or large selectivity estimates --- unless we
1327  * got actual current endpoint values from the table, in which
1328  * case just do the usual sanity clamp. Somewhat arbitrarily, we
1329  * set the cutoff for other cases at a hundredth of the histogram
1330  * resolution.
1331  */
1332  if (have_end)
1333  CLAMP_PROBABILITY(hist_selec);
1334  else
1335  {
1336  double cutoff = 0.01 / (double) (sslot.nvalues - 1);
1337 
1338  if (hist_selec < cutoff)
1339  hist_selec = cutoff;
1340  else if (hist_selec > 1.0 - cutoff)
1341  hist_selec = 1.0 - cutoff;
1342  }
1343  }
1344  else if (sslot.nvalues > 1)
1345  {
1346  /*
1347  * If we get here, we have a histogram but it's not sorted the way
1348  * we want. Do a brute-force search to see how many of the
1349  * entries satisfy the comparison condition, and take that
1350  * fraction as our estimate. (This is identical to the inner loop
1351  * of histogram_selectivity; maybe share code?)
1352  */
1353  LOCAL_FCINFO(fcinfo, 2);
1354  int nmatch = 0;
1355 
1356  InitFunctionCallInfoData(*fcinfo, opproc, 2, collation,
1357  NULL, NULL);
1358  fcinfo->args[0].isnull = false;
1359  fcinfo->args[1].isnull = false;
1360  fcinfo->args[1].value = constval;
1361  for (int i = 0; i < sslot.nvalues; i++)
1362  {
1363  Datum fresult;
1364 
1365  fcinfo->args[0].value = sslot.values[i];
1366  fcinfo->isnull = false;
1367  fresult = FunctionCallInvoke(fcinfo);
1368  if (!fcinfo->isnull && DatumGetBool(fresult))
1369  nmatch++;
1370  }
1371  hist_selec = ((double) nmatch) / ((double) sslot.nvalues);
1372 
1373  /*
1374  * As above, clamp to a hundredth of the histogram resolution.
1375  * This case is surely even less trustworthy than the normal one,
1376  * so we shouldn't believe exact 0 or 1 selectivity. (Maybe the
1377  * clamp should be more restrictive in this case?)
1378  */
1379  {
1380  double cutoff = 0.01 / (double) (sslot.nvalues - 1);
1381 
1382  if (hist_selec < cutoff)
1383  hist_selec = cutoff;
1384  else if (hist_selec > 1.0 - cutoff)
1385  hist_selec = 1.0 - cutoff;
1386  }
1387  }
1388 
1389  free_attstatsslot(&sslot);
1390  }
1391 
1392  return hist_selec;
1393 }
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1149
long val
Definition: informix.c:689
bool comparison_ops_are_compatible(Oid opno1, Oid opno2)
Definition: lsyscache.c:749
static bool convert_to_scalar(Datum value, Oid valuetypid, Oid collid, double *scaledvalue, Datum lobound, Datum hibound, Oid boundstypid, double *scaledlobound, double *scaledhibound)
Definition: selfuncs.c:4309
static bool get_actual_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop, Oid collation, Datum *min, Datum *max)
Definition: selfuncs.c:6098

References ATTSTATSSLOT_NUMBERS, ATTSTATSSLOT_VALUES, CLAMP_PROBABILITY, comparison_ops_are_compatible(), convert_to_scalar(), DatumGetBool(), FmgrInfo::fn_oid, free_attstatsslot(), FunctionCall2Coll(), FunctionCallInvoke, get_actual_variable_range(), get_attstatsslot(), get_variable_numdistinct(), HeapTupleIsValid, i, InitFunctionCallInfoData, InvalidOid, LOCAL_FCINFO, AttStatsSlot::nnumbers, AttStatsSlot::nvalues, root, AttStatsSlot::stacoll, AttStatsSlot::staop, statistic_proc_security_check(), VariableStatData::statsTuple, val, AttStatsSlot::values, and VariableStatData::vartype.

Referenced by prefix_selectivity(), and scalarineqsel().

◆ mcv_selectivity()

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

Definition at line 732 of file selfuncs.c.

735 {
736  double mcv_selec,
737  sumcommon;
738  AttStatsSlot sslot;
739  int i;
740 
741  mcv_selec = 0.0;
742  sumcommon = 0.0;
743 
744  if (HeapTupleIsValid(vardata->statsTuple) &&
745  statistic_proc_security_check(vardata, opproc->fn_oid) &&
746  get_attstatsslot(&sslot, vardata->statsTuple,
747  STATISTIC_KIND_MCV, InvalidOid,
749  {
750  LOCAL_FCINFO(fcinfo, 2);
751 
752  /*
753  * We invoke the opproc "by hand" so that we won't fail on NULL
754  * results. Such cases won't arise for normal comparison functions,
755  * but generic_restriction_selectivity could perhaps be used with
756  * operators that can return NULL. A small side benefit is to not
757  * need to re-initialize the fcinfo struct from scratch each time.
758  */
759  InitFunctionCallInfoData(*fcinfo, opproc, 2, collation,
760  NULL, NULL);
761  fcinfo->args[0].isnull = false;
762  fcinfo->args[1].isnull = false;
763  /* be careful to apply operator right way 'round */
764  if (varonleft)
765  fcinfo->args[1].value = constval;
766  else
767  fcinfo->args[0].value = constval;
768 
769  for (i = 0; i < sslot.nvalues; i++)
770  {
771  Datum fresult;
772 
773  if (varonleft)
774  fcinfo->args[0].value = sslot.values[i];
775  else
776  fcinfo->args[1].value = sslot.values[i];
777  fcinfo->isnull = false;
778  fresult = FunctionCallInvoke(fcinfo);
779  if (!fcinfo->isnull && DatumGetBool(fresult))
780  mcv_selec += sslot.numbers[i];
781  sumcommon += sslot.numbers[i];
782  }
783  free_attstatsslot(&sslot);
784  }
785 
786  *sumcommonp = sumcommon;
787  return mcv_selec;
788 }

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

Referenced by generic_restriction_selectivity(), networksel(), patternsel_common(), and scalarineqsel().

◆ 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 2955 of file selfuncs.c.

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

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, root, scalarineqsel(), and VariableStatData::statsTuple.

Referenced by cached_scansel().

◆ nulltestsel()

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

Definition at line 1698 of file selfuncs.c.

1700 {
1701  VariableStatData vardata;
1702  double selec;
1703 
1704  examine_variable(root, arg, varRelid, &vardata);
1705 
1706  if (HeapTupleIsValid(vardata.statsTuple))
1707  {
1708  Form_pg_statistic stats;
1709  double freq_null;
1710 
1711  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1712  freq_null = stats->stanullfrac;
1713 
1714  switch (nulltesttype)
1715  {
1716  case IS_NULL:
1717 
1718  /*
1719  * Use freq_null directly.
1720  */
1721  selec = freq_null;
1722  break;
1723  case IS_NOT_NULL:
1724 
1725  /*
1726  * Select not unknown (not null) values. Calculate from
1727  * freq_null.
1728  */
1729  selec = 1.0 - freq_null;
1730  break;
1731  default:
1732  elog(ERROR, "unrecognized nulltesttype: %d",
1733  (int) nulltesttype);
1734  return (Selectivity) 0; /* keep compiler quiet */
1735  }
1736  }
1737  else if (vardata.var && IsA(vardata.var, Var) &&
1738  ((Var *) vardata.var)->varattno < 0)
1739  {
1740  /*
1741  * There are no stats for system columns, but we know they are never
1742  * NULL.
1743  */
1744  selec = (nulltesttype == IS_NULL) ? 0.0 : 1.0;
1745  }
1746  else
1747  {
1748  /*
1749  * No ANALYZE stats available, so make a guess
1750  */
1751  switch (nulltesttype)
1752  {
1753  case IS_NULL:
1754  selec = DEFAULT_UNK_SEL;
1755  break;
1756  case IS_NOT_NULL:
1757  selec = DEFAULT_NOT_UNK_SEL;
1758  break;
1759  default:
1760  elog(ERROR, "unrecognized nulltesttype: %d",
1761  (int) nulltesttype);
1762  return (Selectivity) 0; /* keep compiler quiet */
1763  }
1764  }
1765 
1766  ReleaseVariableStats(vardata);
1767 
1768  /* result should be in range, but make sure... */
1769  CLAMP_PROBABILITY(selec);
1770 
1771  return (Selectivity) selec;
1772 }
@ IS_NULL
Definition: primnodes.h:1952
@ IS_NOT_NULL
Definition: primnodes.h:1952

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

Referenced by clause_selectivity_ext(), and clauselist_selectivity_ext().

◆ rowcomparesel()

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

Definition at line 2205 of file selfuncs.c.

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 base relation used.
2243  */
2244  is_join_clause = (NumRelids(root, (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. */
2260  opargs,
2261  inputcollid,
2262  varRelid);
2263  }
2264 
2265  return s1;
2266 }
int NumRelids(PlannerInfo *root, Node *clause)
Definition: clauses.c:2130
#define linitial_oid(l)
Definition: pg_list.h:180
#define list_make2(x1, x2)
Definition: pg_list.h:214
Selectivity restriction_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, int varRelid)
Definition: plancat.c:1953
Selectivity join_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: plancat.c:1992
char * s1

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

Referenced by clause_selectivity_ext().

◆ scalararraysel()

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

Definition at line 1816 of file selfuncs.c.

1822 {
1823  Oid operator = clause->opno;
1824  bool useOr = clause->useOr;
1825  bool isEquality = false;
1826  bool isInequality = false;
1827  Node *leftop;
1828  Node *rightop;
1829  Oid nominal_element_type;
1830  Oid nominal_element_collation;
1831  TypeCacheEntry *typentry;
1832  RegProcedure oprsel;
1833  FmgrInfo oprselproc;
1834  Selectivity s1;
1835  Selectivity s1disjoint;
1836 
1837  /* First, deconstruct the expression */
1838  Assert(list_length(clause->args) == 2);
1839  leftop = (Node *) linitial(clause->args);
1840  rightop = (Node *) lsecond(clause->args);
1841 
1842  /* aggressively reduce both sides to constants */
1843  leftop = estimate_expression_value(root, leftop);
1844  rightop = estimate_expression_value(root, rightop);
1845 
1846  /* get nominal (after relabeling) element type of rightop */
1847  nominal_element_type = get_base_element_type(exprType(rightop));
1848  if (!OidIsValid(nominal_element_type))
1849  return (Selectivity) 0.5; /* probably shouldn't happen */
1850  /* get nominal collation, too, for generating constants */
1851  nominal_element_collation = exprCollation(rightop);
1852 
1853  /* look through any binary-compatible relabeling of rightop */
1854  rightop = strip_array_coercion(rightop);
1855 
1856  /*
1857  * Detect whether the operator is the default equality or inequality
1858  * operator of the array element type.
1859  */
1860  typentry = lookup_type_cache(nominal_element_type, TYPECACHE_EQ_OPR);
1861  if (OidIsValid(typentry->eq_opr))
1862  {
1863  if (operator == typentry->eq_opr)
1864  isEquality = true;
1865  else if (get_negator(operator) == typentry->eq_opr)
1866  isInequality = true;
1867  }
1868 
1869  /*
1870  * If it is equality or inequality, we might be able to estimate this as a
1871  * form of array containment; for instance "const = ANY(column)" can be
1872  * treated as "ARRAY[const] <@ column". scalararraysel_containment tries
1873  * that, and returns the selectivity estimate if successful, or -1 if not.
1874  */
1875  if ((isEquality || isInequality) && !is_join_clause)
1876  {
1877  s1 = scalararraysel_containment(root, leftop, rightop,
1878  nominal_element_type,
1879  isEquality, useOr, varRelid);
1880  if (s1 >= 0.0)
1881  return s1;
1882  }
1883 
1884  /*
1885  * Look up the underlying operator's selectivity estimator. Punt if it
1886  * hasn't got one.
1887  */
1888  if (is_join_clause)
1889  oprsel = get_oprjoin(operator);
1890  else
1891  oprsel = get_oprrest(operator);
1892  if (!oprsel)
1893  return (Selectivity) 0.5;
1894  fmgr_info(oprsel, &oprselproc);
1895 
1896  /*
1897  * In the array-containment check above, we must only believe that an
1898  * operator is equality or inequality if it is the default btree equality
1899  * operator (or its negator) for the element type, since those are the
1900  * operators that array containment will use. But in what follows, we can
1901  * be a little laxer, and also believe that any operators using eqsel() or
1902  * neqsel() as selectivity estimator act like equality or inequality.
1903  */
1904  if (oprsel == F_EQSEL || oprsel == F_EQJOINSEL)
1905  isEquality = true;
1906  else if (oprsel == F_NEQSEL || oprsel == F_NEQJOINSEL)
1907  isInequality = true;
1908 
1909  /*
1910  * We consider three cases:
1911  *
1912  * 1. rightop is an Array constant: deconstruct the array, apply the
1913  * operator's selectivity function for each array element, and merge the
1914  * results in the same way that clausesel.c does for AND/OR combinations.
1915  *
1916  * 2. rightop is an ARRAY[] construct: apply the operator's selectivity
1917  * function for each element of the ARRAY[] construct, and merge.
1918  *
1919  * 3. otherwise, make a guess ...
1920  */
1921  if (rightop && IsA(rightop, Const))
1922  {
1923  Datum arraydatum = ((Const *) rightop)->constvalue;
1924  bool arrayisnull = ((Const *) rightop)->constisnull;
1925  ArrayType *arrayval;
1926  int16 elmlen;
1927  bool elmbyval;
1928  char elmalign;
1929  int num_elems;
1930  Datum *elem_values;
1931  bool *elem_nulls;
1932  int i;
1933 
1934  if (arrayisnull) /* qual can't succeed if null array */
1935  return (Selectivity) 0.0;
1936  arrayval = DatumGetArrayTypeP(arraydatum);
1938  &elmlen, &elmbyval, &elmalign);
1939  deconstruct_array(arrayval,
1940  ARR_ELEMTYPE(arrayval),
1941  elmlen, elmbyval, elmalign,
1942  &elem_values, &elem_nulls, &num_elems);
1943 
1944  /*
1945  * For generic operators, we assume the probability of success is
1946  * independent for each array element. But for "= ANY" or "<> ALL",
1947  * if the array elements are distinct (which'd typically be the case)
1948  * then the probabilities are disjoint, and we should just sum them.
1949  *
1950  * If we were being really tense we would try to confirm that the
1951  * elements are all distinct, but that would be expensive and it
1952  * doesn't seem to be worth the cycles; it would amount to penalizing
1953  * well-written queries in favor of poorly-written ones. However, we
1954  * do protect ourselves a little bit by checking whether the
1955  * disjointness assumption leads to an impossible (out of range)
1956  * probability; if so, we fall back to the normal calculation.
1957  */
1958  s1 = s1disjoint = (useOr ? 0.0 : 1.0);
1959 
1960  for (i = 0; i < num_elems; i++)
1961  {
1962  List *args;
1963  Selectivity s2;
1964 
1965  args = list_make2(leftop,
1966  makeConst(nominal_element_type,
1967  -1,
1968  nominal_element_collation,
1969  elmlen,
1970  elem_values[i],
1971  elem_nulls[i],
1972  elmbyval));
1973  if (is_join_clause)
1974  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
1975  clause->inputcollid,
1977  ObjectIdGetDatum(operator),
1979  Int16GetDatum(jointype),
1980  PointerGetDatum(sjinfo)));
1981  else
1982  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
1983  clause->inputcollid,
1985  ObjectIdGetDatum(operator),
1987  Int32GetDatum(varRelid)));
1988 
1989  if (useOr)
1990  {
1991  s1 = s1 + s2 - s1 * s2;
1992  if (isEquality)
1993  s1disjoint += s2;
1994  }
1995  else
1996  {
1997  s1 = s1 * s2;
1998  if (isInequality)
1999  s1disjoint += s2 - 1.0;
2000  }
2001  }
2002 
2003  /* accept disjoint-probability estimate if in range */
2004  if ((useOr ? isEquality : isInequality) &&
2005  s1disjoint >= 0.0 && s1disjoint <= 1.0)
2006  s1 = s1disjoint;
2007  }
2008  else if (rightop && IsA(rightop, ArrayExpr) &&
2009  !((ArrayExpr *) rightop)->multidims)
2010  {
2011  ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
2012  int16 elmlen;
2013  bool elmbyval;
2014  ListCell *l;
2015 
2016  get_typlenbyval(arrayexpr->element_typeid,
2017  &elmlen, &elmbyval);
2018 
2019  /*
2020  * We use the assumption of disjoint probabilities here too, although
2021  * the odds of equal array elements are rather higher if the elements
2022  * are not all constants (which they won't be, else constant folding
2023  * would have reduced the ArrayExpr to a Const). In this path it's
2024  * critical to have the sanity check on the s1disjoint estimate.
2025  */
2026  s1 = s1disjoint = (useOr ? 0.0 : 1.0);
2027 
2028  foreach(l, arrayexpr->elements)
2029  {
2030  Node *elem = (Node *) lfirst(l);
2031  List *args;
2032  Selectivity s2;
2033 
2034  /*
2035  * Theoretically, if elem isn't of nominal_element_type we should
2036  * insert a RelabelType, but it seems unlikely that any operator
2037  * estimation function would really care ...
2038  */
2039  args = list_make2(leftop, elem);
2040  if (is_join_clause)
2041  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
2042  clause->inputcollid,
2044  ObjectIdGetDatum(operator),
2046  Int16GetDatum(jointype),
2047  PointerGetDatum(sjinfo)));
2048  else
2049  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
2050  clause->inputcollid,
2052  ObjectIdGetDatum(operator),
2054  Int32GetDatum(varRelid)));
2055 
2056  if (useOr)
2057  {
2058  s1 = s1 + s2 - s1 * s2;
2059  if (isEquality)
2060  s1disjoint += s2;
2061  }
2062  else
2063  {
2064  s1 = s1 * s2;
2065  if (isInequality)
2066  s1disjoint += s2 - 1.0;
2067  }
2068  }
2069 
2070  /* accept disjoint-probability estimate if in range */
2071  if ((useOr ? isEquality : isInequality) &&
2072  s1disjoint >= 0.0 && s1disjoint <= 1.0)
2073  s1 = s1disjoint;
2074  }
2075  else
2076  {
2077  CaseTestExpr *dummyexpr;
2078  List *args;
2079  Selectivity s2;
2080  int i;
2081 
2082  /*
2083  * We need a dummy rightop to pass to the operator selectivity
2084  * routine. It can be pretty much anything that doesn't look like a
2085  * constant; CaseTestExpr is a convenient choice.
2086  */
2087  dummyexpr = makeNode(CaseTestExpr);
2088  dummyexpr->typeId = nominal_element_type;
2089  dummyexpr->typeMod = -1;
2090  dummyexpr->collation = clause->inputcollid;
2091  args = list_make2(leftop, dummyexpr);
2092  if (is_join_clause)
2093  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
2094  clause->inputcollid,
2096  ObjectIdGetDatum(operator),
2098  Int16GetDatum(jointype),
2099  PointerGetDatum(sjinfo)));
2100  else
2101  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
2102  clause->inputcollid,
2104  ObjectIdGetDatum(operator),
2106  Int32GetDatum(varRelid)));
2107  s1 = useOr ? 0.0 : 1.0;
2108 
2109  /*
2110  * Arbitrarily assume 10 elements in the eventual array value (see
2111  * also estimate_array_length). We don't risk an assumption of
2112  * disjoint probabilities here.
2113  */
2114  for (i = 0; i < 10; i++)
2115  {
2116  if (useOr)
2117  s1 = s1 + s2 - s1 * s2;
2118  else
2119  s1 = s1 * s2;
2120  }
2121  }
2122 
2123  /* result should be in range, but make sure... */
2125 
2126  return s1;
2127 }
#define ARR_ELEMTYPE(a)
Definition: array.h:292
Selectivity scalararraysel_containment(PlannerInfo *root, Node *leftop, Node *rightop, Oid elemtype, bool isEquality, bool useOr, int varRelid)
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3631
signed short int16
Definition: c.h:495
regproc RegProcedure
Definition: c.h:641
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1196
Datum FunctionCall5Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5)
Definition: fmgr.c:1223
RegProcedure get_oprrest(Oid opno)
Definition: lsyscache.c:1557
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2271
RegProcedure get_oprjoin(Oid opno)
Definition: lsyscache.c:1581
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2251
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2832
Oid get_negator(Oid opno)
Definition: lsyscache.c:1533
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:301
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:816
#define makeNode(_type_)
Definition: nodes.h:155
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static float8 DatumGetFloat8(Datum X)
Definition: postgres.h:494
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
char * s2
List * elements
Definition: primnodes.h:1380
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:356
#define TYPECACHE_EQ_OPR
Definition: typcache.h:137

References generate_unaccent_rules::args, ScalarArrayOpExpr::args, ARR_ELEMTYPE, Assert, CLAMP_PROBABILITY, DatumGetArrayTypeP, DatumGetFloat8(), deconstruct_array(), 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, Int16GetDatum(), Int32GetDatum(), IsA, lfirst, linitial, list_length(), list_make2, lookup_type_cache(), lsecond, makeConst(), makeNode, ObjectIdGetDatum(), OidIsValid, ScalarArrayOpExpr::opno, PointerGetDatum(), root, s1, s2, scalararraysel_containment(), strip_array_coercion(), TYPECACHE_EQ_OPR, CaseTestExpr::typeId, and ScalarArrayOpExpr::useOr.

Referenced by clause_selectivity_ext().

◆ scalararraysel_containment()

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

Definition at line 81 of file array_selfuncs.c.

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

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, OidIsValid, VariableStatData::rel, ReleaseVariableStats, root, statistic_proc_security_check(), VariableStatData::statsTuple, TYPECACHE_CMP_PROC_FINFO, and AttStatsSlot::values.

Referenced by scalararraysel().

◆ statistic_proc_security_check()

bool statistic_proc_security_check ( VariableStatData vardata,
Oid  func_oid 
)

Definition at line 5746 of file selfuncs.c.

5747 {
5748  if (vardata->acl_ok)
5749  return true;
5750 
5751  if (!OidIsValid(func_oid))
5752  return false;
5753 
5754  if (get_func_leakproof(func_oid))
5755  return true;
5756 
5757  ereport(DEBUG2,
5758  (errmsg_internal("not using statistics because function \"%s\" is not leak-proof",
5759  get_func_name(func_oid))));
5760  return false;
5761 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define DEBUG2
Definition: elog.h:29
#define ereport(elevel,...)
Definition: elog.h:149
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1608
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1837

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

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

◆ var_eq_const()

double var_eq_const ( VariableStatData vardata,
Oid  oproid,
Oid  collation,
Datum  constval,
bool  constisnull,
bool  varonleft,
bool  negate 
)

Definition at line 295 of file selfuncs.c.

298 {
299  double selec;
300  double nullfrac = 0.0;
301  bool isdefault;
302  Oid opfuncoid;
303 
304  /*
305  * If the constant is NULL, assume operator is strict and return zero, ie,
306  * operator will never return TRUE. (It's zero even for a negator op.)
307  */
308  if (constisnull)
309  return 0.0;
310 
311  /*
312  * Grab the nullfrac for use below. Note we allow use of nullfrac
313  * regardless of security check.
314  */
315  if (HeapTupleIsValid(vardata->statsTuple))
316  {
317  Form_pg_statistic stats;
318 
319  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
320  nullfrac = stats->stanullfrac;
321  }
322 
323  /*
324  * If we matched the var to a unique index or DISTINCT clause, assume
325  * there is exactly one match regardless of anything else. (This is
326  * slightly bogus, since the index or clause's equality operator might be
327  * different from ours, but it's much more likely to be right than
328  * ignoring the information.)
329  */
330  if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
331  {
332  selec = 1.0 / vardata->rel->tuples;
333  }
334  else if (HeapTupleIsValid(vardata->statsTuple) &&
336  (opfuncoid = get_opcode(oproid))))
337  {
338  AttStatsSlot sslot;
339  bool match = false;
340  int i;
341 
342  /*
343  * Is the constant "=" to any of the column's most common values?
344  * (Although the given operator may not really be "=", we will assume
345  * that seeing whether it returns TRUE is an appropriate test. If you
346  * don't like this, maybe you shouldn't be using eqsel for your
347  * operator...)
348  */
349  if (get_attstatsslot(&sslot, vardata->statsTuple,
350  STATISTIC_KIND_MCV, InvalidOid,
352  {
353  LOCAL_FCINFO(fcinfo, 2);
354  FmgrInfo eqproc;
355 
356  fmgr_info(opfuncoid, &eqproc);
357 
358  /*
359  * Save a few cycles by setting up the fcinfo struct just once.
360  * Using FunctionCallInvoke directly also avoids failure if the
361  * eqproc returns NULL, though really equality functions should
362  * never do that.
363  */
364  InitFunctionCallInfoData(*fcinfo, &eqproc, 2, collation,
365  NULL, NULL);
366  fcinfo->args[0].isnull = false;
367  fcinfo->args[1].isnull = false;
368  /* be careful to apply operator right way 'round */
369  if (varonleft)
370  fcinfo->args[1].value = constval;
371  else
372  fcinfo->args[0].value = constval;
373 
374  for (i = 0; i < sslot.nvalues; i++)
375  {
376  Datum fresult;
377 
378  if (varonleft)
379  fcinfo->args[0].value = sslot.values[i];
380  else
381  fcinfo->args[1].value = sslot.values[i];
382  fcinfo->isnull = false;
383  fresult = FunctionCallInvoke(fcinfo);
384  if (!fcinfo->isnull && DatumGetBool(fresult))
385  {
386  match = true;
387  break;
388  }
389  }
390  }
391  else
392  {
393  /* no most-common-value info available */
394  i = 0; /* keep compiler quiet */
395  }
396 
397  if (match)
398  {
399  /*
400  * Constant is "=" to this common value. We know selectivity
401  * exactly (or as exactly as ANALYZE could calculate it, anyway).
402  */
403  selec = sslot.numbers[i];
404  }
405  else
406  {
407  /*
408  * Comparison is against a constant that is neither NULL nor any
409  * of the common values. Its selectivity cannot be more than
410  * this:
411  */
412  double sumcommon = 0.0;
413  double otherdistinct;
414 
415  for (i = 0; i < sslot.nnumbers; i++)
416  sumcommon += sslot.numbers[i];
417  selec = 1.0 - sumcommon - nullfrac;
418  CLAMP_PROBABILITY(selec);
419 
420  /*
421  * and in fact it's probably a good deal less. We approximate that
422  * all the not-common values share this remaining fraction
423  * equally, so we divide by the number of other distinct values.
424  */
425  otherdistinct = get_variable_numdistinct(vardata, &isdefault) -
426  sslot.nnumbers;
427  if (otherdistinct > 1)
428  selec /= otherdistinct;
429 
430  /*
431  * Another cross-check: selectivity shouldn't be estimated as more
432  * than the least common "most common value".
433  */
434  if (sslot.nnumbers > 0 && selec > sslot.numbers[sslot.nnumbers - 1])
435  selec = sslot.numbers[sslot.nnumbers - 1];
436  }
437 
438  free_attstatsslot(&sslot);
439  }
440  else
441  {
442  /*
443  * No ANALYZE stats available, so make a guess using estimated number
444  * of distinct values and assuming they are equally common. (The guess
445  * is unlikely to be very good, but we do know a few special cases.)
446  */
447  selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
448  }
449 
450  /* now adjust if we wanted <> rather than = */
451  if (negate)
452  selec = 1.0 - selec - nullfrac;
453 
454  /* result should be in range, but make sure... */
455  CLAMP_PROBABILITY(selec);
456 
457  return selec;
458 }

References ATTSTATSSLOT_NUMBERS, ATTSTATSSLOT_VALUES, CLAMP_PROBABILITY, DatumGetBool(), fmgr_info(), free_attstatsslot(), FunctionCallInvoke, get_attstatsslot(), get_opcode(), get_variable_numdistinct(), GETSTRUCT, HeapTupleIsValid, i, InitFunctionCallInfoData, InvalidOid, VariableStatData::isunique, LOCAL_FCINFO, AttStatsSlot::nnumbers, AttStatsSlot::numbers, AttStatsSlot::nvalues, VariableStatData::rel, statistic_proc_security_check(), VariableStatData::statsTuple, RelOptInfo::tuples, and AttStatsSlot::values.

Referenced by boolvarsel(), eqsel_internal(), patternsel_common(), and prefix_selectivity().

◆ var_eq_non_const()

double var_eq_non_const ( VariableStatData vardata,
Oid  oproid,
Oid  collation,
Node other,
bool  varonleft,
bool  negate 
)

Definition at line 466 of file selfuncs.c.

469 {
470  double selec;
471  double nullfrac = 0.0;
472  bool isdefault;
473 
474  /*
475  * Grab the nullfrac for use below.
476  */
477  if (HeapTupleIsValid(vardata->statsTuple))
478  {
479  Form_pg_statistic stats;
480 
481  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
482  nullfrac = stats->stanullfrac;
483  }
484 
485  /*
486  * If we matched the var to a unique index or DISTINCT clause, assume
487  * there is exactly one match regardless of anything else. (This is
488  * slightly bogus, since the index or clause's equality operator might be
489  * different from ours, but it's much more likely to be right than
490  * ignoring the information.)
491  */
492  if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
493  {
494  selec = 1.0 / vardata->rel->tuples;
495  }
496  else if (HeapTupleIsValid(vardata->statsTuple))
497  {
498  double ndistinct;
499  AttStatsSlot sslot;
500 
501  /*
502  * Search is for a value that we do not know a priori, but we will
503  * assume it is not NULL. Estimate the selectivity as non-null
504  * fraction divided by number of distinct values, so that we get a
505  * result averaged over all possible values whether common or
506  * uncommon. (Essentially, we are assuming that the not-yet-known
507  * comparison value is equally likely to be any of the possible
508  * values, regardless of their frequency in the table. Is that a good
509  * idea?)
510  */
511  selec = 1.0 - nullfrac;
512  ndistinct = get_variable_numdistinct(vardata, &isdefault);
513  if (ndistinct > 1)
514  selec /= ndistinct;
515 
516  /*
517  * Cross-check: selectivity should never be estimated as more than the
518  * most common value's.
519  */
520  if (get_attstatsslot(&sslot, vardata->statsTuple,
521  STATISTIC_KIND_MCV, InvalidOid,
523  {
524  if (sslot.nnumbers > 0 && selec > sslot.numbers[0])
525  selec = sslot.numbers[0];
526  free_attstatsslot(&sslot);
527  }
528  }
529  else
530  {
531  /*
532  * No ANALYZE stats available, so make a guess using estimated number
533  * of distinct values and assuming they are equally common. (The guess
534  * is unlikely to be very good, but we do know a few special cases.)
535  */
536  selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
537  }
538 
539  /* now adjust if we wanted <> rather than = */
540  if (negate)
541  selec = 1.0 - selec - nullfrac;
542 
543  /* result should be in range, but make sure... */
544  CLAMP_PROBABILITY(selec);
545 
546  return selec;
547 }

References ATTSTATSSLOT_NUMBERS, CLAMP_PROBABILITY, free_attstatsslot(), get_attstatsslot(), get_variable_numdistinct(), GETSTRUCT, HeapTupleIsValid, InvalidOid, VariableStatData::isunique, AttStatsSlot::nnumbers, AttStatsSlot::numbers, VariableStatData::rel, VariableStatData::statsTuple, and RelOptInfo::tuples.

Referenced by eqsel_internal().

Variable Documentation

◆ get_index_stats_hook

PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook
extern

Definition at line 148 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
extern

Definition at line 147 of file selfuncs.c.

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