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)
 
int estimate_array_length (Node *arrayexpr)
 
Selectivity rowcomparesel (PlannerInfo *root, RowCompareExpr *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
void mergejoinscansel (PlannerInfo *root, Node *clause, Oid opfamily, int strategy, bool nulls_first, Selectivity *leftstart, Selectivity *leftend, Selectivity *rightstart, Selectivity *rightend)
 
double estimate_num_groups (PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, 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 140 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 135 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 6649 of file selfuncs.c.

6650 {
6651  List *predExtraQuals = NIL;
6652  ListCell *lc;
6653 
6654  if (index->indpred == NIL)
6655  return indexQuals;
6656 
6657  foreach(lc, index->indpred)
6658  {
6659  Node *predQual = (Node *) lfirst(lc);
6660  List *oneQual = list_make1(predQual);
6661 
6662  if (!predicate_implied_by(oneQual, indexQuals, false))
6663  predExtraQuals = list_concat(predExtraQuals, oneQual);
6664  }
6665  return list_concat(predExtraQuals, indexQuals);
6666 }
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
#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 1539 of file selfuncs.c.

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

Referenced by clause_selectivity_ext().

◆ boolvarsel()

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

Definition at line 1511 of file selfuncs.c.

1512 {
1513  VariableStatData vardata;
1514  double selec;
1515 
1516  examine_variable(root, arg, varRelid, &vardata);
1517  if (HeapTupleIsValid(vardata.statsTuple))
1518  {
1519  /*
1520  * A boolean variable V is equivalent to the clause V = 't', so we
1521  * compute the selectivity as if that is what we have.
1522  */
1523  selec = var_eq_const(&vardata, BooleanEqualOperator, InvalidOid,
1524  BoolGetDatum(true), false, true, false);
1525  }
1526  else
1527  {
1528  /* Otherwise, the default estimate is 0.5 */
1529  selec = 0.5;
1530  }
1531  ReleaseVariableStats(vardata);
1532  return selec;
1533 }
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:294

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

Referenced by clause_selectivity_ext().

◆ estimate_array_length()

int estimate_array_length ( Node arrayexpr)

Definition at line 2134 of file selfuncs.c.

2135 {
2136  /* look through any binary-compatible relabeling of arrayexpr */
2137  arrayexpr = strip_array_coercion(arrayexpr);
2138 
2139  if (arrayexpr && IsA(arrayexpr, Const))
2140  {
2141  Datum arraydatum = ((Const *) arrayexpr)->constvalue;
2142  bool arrayisnull = ((Const *) arrayexpr)->constisnull;
2143  ArrayType *arrayval;
2144 
2145  if (arrayisnull)
2146  return 0;
2147  arrayval = DatumGetArrayTypeP(arraydatum);
2148  return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
2149  }
2150  else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
2151  !((ArrayExpr *) arrayexpr)->multidims)
2152  {
2153  return list_length(((ArrayExpr *) arrayexpr)->elements);
2154  }
2155  else
2156  {
2157  /* default guess --- see also scalararraysel */
2158  return 10;
2159  }
2160 }
#define ARR_NDIM(a)
Definition: array.h:283
#define DatumGetArrayTypeP(X)
Definition: array.h:254
#define ARR_DIMS(a)
Definition: array.h:287
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:76
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
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:1782

References ARR_DIMS, ARR_NDIM, ArrayGetNItems(), DatumGetArrayTypeP, IsA, list_length(), 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 3768 of file selfuncs.c.

3771 {
3772  VariableStatData vardata;
3773  double estfract,
3774  ndistinct,
3775  stanullfrac,
3776  avgfreq;
3777  bool isdefault;
3778  AttStatsSlot sslot;
3779 
3780  examine_variable(root, hashkey, 0, &vardata);
3781 
3782  /* Look up the frequency of the most common value, if available */
3783  *mcv_freq = 0.0;
3784 
3785  if (HeapTupleIsValid(vardata.statsTuple))
3786  {
3787  if (get_attstatsslot(&sslot, vardata.statsTuple,
3788  STATISTIC_KIND_MCV, InvalidOid,
3790  {
3791  /*
3792  * The first MCV stat is for the most common value.
3793  */
3794  if (sslot.nnumbers > 0)
3795  *mcv_freq = sslot.numbers[0];
3796  free_attstatsslot(&sslot);
3797  }
3798  }
3799 
3800  /* Get number of distinct values */
3801  ndistinct = get_variable_numdistinct(&vardata, &isdefault);
3802 
3803  /*
3804  * If ndistinct isn't real, punt. We normally return 0.1, but if the
3805  * mcv_freq is known to be even higher than that, use it instead.
3806  */
3807  if (isdefault)
3808  {
3809  *bucketsize_frac = (Selectivity) Max(0.1, *mcv_freq);
3810  ReleaseVariableStats(vardata);
3811  return;
3812  }
3813 
3814  /* Get fraction that are null */
3815  if (HeapTupleIsValid(vardata.statsTuple))
3816  {
3817  Form_pg_statistic stats;
3818 
3819  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
3820  stanullfrac = stats->stanullfrac;
3821  }
3822  else
3823  stanullfrac = 0.0;
3824 
3825  /* Compute avg freq of all distinct data values in raw relation */
3826  avgfreq = (1.0 - stanullfrac) / ndistinct;
3827 
3828  /*
3829  * Adjust ndistinct to account for restriction clauses. Observe we are
3830  * assuming that the data distribution is affected uniformly by the
3831  * restriction clauses!
3832  *
3833  * XXX Possibly better way, but much more expensive: multiply by
3834  * selectivity of rel's restriction clauses that mention the target Var.
3835  */
3836  if (vardata.rel && vardata.rel->tuples > 0)
3837  {
3838  ndistinct *= vardata.rel->rows / vardata.rel->tuples;
3839  ndistinct = clamp_row_est(ndistinct);
3840  }
3841 
3842  /*
3843  * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
3844  * number of buckets is less than the expected number of distinct values;
3845  * otherwise it is 1/ndistinct.
3846  */
3847  if (ndistinct > nbuckets)
3848  estfract = 1.0 / nbuckets;
3849  else
3850  estfract = 1.0 / ndistinct;
3851 
3852  /*
3853  * Adjust estimated bucketsize upward to account for skewed distribution.
3854  */
3855  if (avgfreq > 0.0 && *mcv_freq > avgfreq)
3856  estfract *= *mcv_freq / avgfreq;
3857 
3858  /*
3859  * Clamp bucketsize to sane range (the above adjustment could easily
3860  * produce an out-of-range result). We set the lower bound a little above
3861  * zero, since zero isn't a very sane result.
3862  */
3863  if (estfract < 1.0e-6)
3864  estfract = 1.0e-6;
3865  else if (estfract > 1.0)
3866  estfract = 1.0;
3867 
3868  *bucketsize_frac = (Selectivity) estfract;
3869 
3870  ReleaseVariableStats(vardata);
3871 }
#define Max(x, y)
Definition: c.h:982
double clamp_row_est(double nrows)
Definition: costsize.c:202
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition: selfuncs.c:5648
Cardinality tuples
Definition: pathnodes.h:938
Cardinality rows
Definition: pathnodes.h:872
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, 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 3887 of file selfuncs.c.

3889 {
3890  Size hashentrysize;
3891 
3892  hashentrysize = hash_agg_entry_size(list_length(root->aggtransinfos),
3893  path->pathtarget->width,
3894  agg_costs->transitionSpace);
3895 
3896  /*
3897  * Note that this disregards the effect of fill-factor and growth policy
3898  * of the hash table. That's probably ok, given that the default
3899  * fill-factor is relatively high. It'd be hard to meaningfully factor in
3900  * "double-in-size" growth policies here.
3901  */
3902  return hashentrysize * dNumGroups;
3903 }
size_t Size
Definition: c.h:589
Size hash_agg_entry_size(int numTrans, Size tupleWidth, Size transitionSpace)
Definition: nodeAgg.c:1686
Size transitionSpace
Definition: pathnodes.h:62
List * aggtransinfos
Definition: pathnodes.h:512

References PlannerInfo::aggtransinfos, hash_agg_entry_size(), list_length(), 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 3386 of file selfuncs.c.

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

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

Referenced by adjust_rowcount_for_semijoins(), 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 recurse_set_operations().

◆ examine_variable()

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

Definition at line 4978 of file selfuncs.c.

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

References VariableStatData::acl_ok, ACL_SELECT, ACLCHECK_OK, arg, Assert(), VariableStatData::atttype, VariableStatData::atttypmod, BMS_EMPTY_SET, bms_free(), bms_is_member(), bms_membership(), BMS_MULTIPLE, BMS_SINGLETON, bms_singleton_member(), BoolGetDatum(), elog(), equal(), ERROR, examine_simple_variable(), 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, RTE_RELATION, RangeTblEntry::rtekind, SearchSysCache3(), RangeTblEntry::securityQuals, statext_expressions_load(), RelOptInfo::statlist, StatisticExtInfo::statOid, STATRELATTINH, VariableStatData::statsTuple, RelOptInfo::userid, VariableStatData::var, Var::varattno, Var::varno, and VariableStatData::vartype.

Referenced by booltestsel(), boolvarsel(), 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 913 of file selfuncs.c.

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

Referenced by ltreeparentsel(), and matchingsel().

◆ genericcostestimate()

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

Definition at line 6431 of file selfuncs.c.

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

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, 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 4909 of file selfuncs.c.

4912 {
4913  Node *left,
4914  *right;
4915 
4916  if (list_length(args) != 2)
4917  elog(ERROR, "join operator should take two arguments");
4918 
4919  left = (Node *) linitial(args);
4920  right = (Node *) lsecond(args);
4921 
4922  examine_variable(root, left, 0, vardata1);
4923  examine_variable(root, right, 0, vardata2);
4924 
4925  if (vardata1->rel &&
4926  bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
4927  *join_is_reversed = true; /* var1 is on RHS */
4928  else if (vardata2->rel &&
4929  bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
4930  *join_is_reversed = true; /* var2 is on LHS */
4931  else
4932  *join_is_reversed = false;
4933 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:316
Relids relids
Definition: pathnodes.h:866
Relids syn_lefthand
Definition: pathnodes.h:2833
Relids syn_righthand
Definition: pathnodes.h:2834

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

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

◆ get_quals_from_indexclauses()

List* get_quals_from_indexclauses ( List indexclauses)

Definition at line 6347 of file selfuncs.c.

6348 {
6349  List *result = NIL;
6350  ListCell *lc;
6351 
6352  foreach(lc, indexclauses)
6353  {
6354  IndexClause *iclause = lfirst_node(IndexClause, lc);
6355  ListCell *lc2;
6356 
6357  foreach(lc2, iclause->indexquals)
6358  {
6359  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
6360 
6361  result = lappend(result, rinfo);
6362  }
6363  }
6364  return result;
6365 }
#define lfirst_node(type, lc)
Definition: pg_list.h:176
List * indexquals
Definition: pathnodes.h:1737

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

4852 {
4853  Node *left,
4854  *right;
4855  VariableStatData rdata;
4856 
4857  /* Fail if not a binary opclause (probably shouldn't happen) */
4858  if (list_length(args) != 2)
4859  return false;
4860 
4861  left = (Node *) linitial(args);
4862  right = (Node *) lsecond(args);
4863 
4864  /*
4865  * Examine both sides. Note that when varRelid is nonzero, Vars of other
4866  * relations will be treated as pseudoconstants.
4867  */
4868  examine_variable(root, left, varRelid, vardata);
4869  examine_variable(root, right, varRelid, &rdata);
4870 
4871  /*
4872  * If one side is a variable and the other not, we win.
4873  */
4874  if (vardata->rel && rdata.rel == NULL)
4875  {
4876  *varonleft = true;
4877  *other = estimate_expression_value(root, rdata.var);
4878  /* Assume we need no ReleaseVariableStats(rdata) here */
4879  return true;
4880  }
4881 
4882  if (vardata->rel == NULL && rdata.rel)
4883  {
4884  *varonleft = false;
4885  *other = estimate_expression_value(root, vardata->var);
4886  /* Assume we need no ReleaseVariableStats(*vardata) here */
4887  *vardata = rdata;
4888  return true;
4889  }
4890 
4891  /* Oops, clause has wrong structure (probably var op var) */
4892  ReleaseVariableStats(*vardata);
4893  ReleaseVariableStats(rdata);
4894 
4895  return false;
4896 }
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2275

References generate_unaccent_rules::args, estimate_expression_value(), examine_variable(), linitial, list_length(), lsecond, VariableStatData::rel, ReleaseVariableStats, 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 5648 of file selfuncs.c.

5649 {
5650  double stadistinct;
5651  double stanullfrac = 0.0;
5652  double ntuples;
5653 
5654  *isdefault = false;
5655 
5656  /*
5657  * Determine the stadistinct value to use. There are cases where we can
5658  * get an estimate even without a pg_statistic entry, or can get a better
5659  * value than is in pg_statistic. Grab stanullfrac too if we can find it
5660  * (otherwise, assume no nulls, for lack of any better idea).
5661  */
5662  if (HeapTupleIsValid(vardata->statsTuple))
5663  {
5664  /* Use the pg_statistic entry */
5665  Form_pg_statistic stats;
5666 
5667  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
5668  stadistinct = stats->stadistinct;
5669  stanullfrac = stats->stanullfrac;
5670  }
5671  else if (vardata->vartype == BOOLOID)
5672  {
5673  /*
5674  * Special-case boolean columns: presumably, two distinct values.
5675  *
5676  * Are there any other datatypes we should wire in special estimates
5677  * for?
5678  */
5679  stadistinct = 2.0;
5680  }
5681  else if (vardata->rel && vardata->rel->rtekind == RTE_VALUES)
5682  {
5683  /*
5684  * If the Var represents a column of a VALUES RTE, assume it's unique.
5685  * This could of course be very wrong, but it should tend to be true
5686  * in well-written queries. We could consider examining the VALUES'
5687  * contents to get some real statistics; but that only works if the
5688  * entries are all constants, and it would be pretty expensive anyway.
5689  */
5690  stadistinct = -1.0; /* unique (and all non null) */
5691  }
5692  else
5693  {
5694  /*
5695  * We don't keep statistics for system columns, but in some cases we
5696  * can infer distinctness anyway.
5697  */
5698  if (vardata->var && IsA(vardata->var, Var))
5699  {
5700  switch (((Var *) vardata->var)->varattno)
5701  {
5703  stadistinct = -1.0; /* unique (and all non null) */
5704  break;
5706  stadistinct = 1.0; /* only 1 value */
5707  break;
5708  default:
5709  stadistinct = 0.0; /* means "unknown" */
5710  break;
5711  }
5712  }
5713  else
5714  stadistinct = 0.0; /* means "unknown" */
5715 
5716  /*
5717  * XXX consider using estimate_num_groups on expressions?
5718  */
5719  }
5720 
5721  /*
5722  * If there is a unique index or DISTINCT clause for the variable, assume
5723  * it is unique no matter what pg_statistic says; the statistics could be
5724  * out of date, or we might have found a partial unique index that proves
5725  * the var is unique for this query. However, we'd better still believe
5726  * the null-fraction statistic.
5727  */
5728  if (vardata->isunique)
5729  stadistinct = -1.0 * (1.0 - stanullfrac);
5730 
5731  /*
5732  * If we had an absolute estimate, use that.
5733  */
5734  if (stadistinct > 0.0)
5735  return clamp_row_est(stadistinct);
5736 
5737  /*
5738  * Otherwise we need to get the relation size; punt if not available.
5739  */
5740  if (vardata->rel == NULL)
5741  {
5742  *isdefault = true;
5743  return DEFAULT_NUM_DISTINCT;
5744  }
5745  ntuples = vardata->rel->tuples;
5746  if (ntuples <= 0.0)
5747  {
5748  *isdefault = true;
5749  return DEFAULT_NUM_DISTINCT;
5750  }
5751 
5752  /*
5753  * If we had a relative estimate, use that.
5754  */
5755  if (stadistinct < 0.0)
5756  return clamp_row_est(-stadistinct * ntuples);
5757 
5758  /*
5759  * With no data, estimate ndistinct = ntuples if the table is small, else
5760  * use default. We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
5761  * that the behavior isn't discontinuous.
5762  */
5763  if (ntuples < DEFAULT_NUM_DISTINCT)
5764  return clamp_row_est(ntuples);
5765 
5766  *isdefault = true;
5767  return DEFAULT_NUM_DISTINCT;
5768 }
@ RTE_VALUES
Definition: parsenodes.h:1018
#define DEFAULT_NUM_DISTINCT
Definition: selfuncs.h:52
RTEKind rtekind
Definition: pathnodes.h:917
#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 822 of file selfuncs.c.

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

6378 {
6379  Cost qual_arg_cost = 0;
6380  ListCell *lc;
6381 
6382  foreach(lc, indexquals)
6383  {
6384  Expr *clause = (Expr *) lfirst(lc);
6385  Node *other_operand;
6386  QualCost index_qual_cost;
6387 
6388  /*
6389  * Index quals will have RestrictInfos, indexorderbys won't. Look
6390  * through RestrictInfo if present.
6391  */
6392  if (IsA(clause, RestrictInfo))
6393  clause = ((RestrictInfo *) clause)->clause;
6394 
6395  if (IsA(clause, OpExpr))
6396  {
6397  OpExpr *op = (OpExpr *) clause;
6398 
6399  other_operand = (Node *) lsecond(op->args);
6400  }
6401  else if (IsA(clause, RowCompareExpr))
6402  {
6403  RowCompareExpr *rc = (RowCompareExpr *) clause;
6404 
6405  other_operand = (Node *) rc->rargs;
6406  }
6407  else if (IsA(clause, ScalarArrayOpExpr))
6408  {
6409  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
6410 
6411  other_operand = (Node *) lsecond(saop->args);
6412  }
6413  else if (IsA(clause, NullTest))
6414  {
6415  other_operand = NULL;
6416  }
6417  else
6418  {
6419  elog(ERROR, "unsupported indexqual type: %d",
6420  (int) nodeTag(clause));
6421  other_operand = NULL; /* keep compiler quiet */
6422  }
6423 
6424  cost_qual_eval_node(&index_qual_cost, other_operand, root);
6425  qual_arg_cost += index_qual_cost.startup + index_qual_cost.per_tuple;
6426  }
6427  return qual_arg_cost;
6428 }
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:4393
#define nodeTag(nodeptr)
Definition: nodes.h:133
List * args
Definition: primnodes.h:760
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, 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 1040 of file selfuncs.c.

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

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, 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 731 of file selfuncs.c.

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

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

2926 {
2927  Node *left,
2928  *right;
2929  VariableStatData leftvar,
2930  rightvar;
2931  int op_strategy;
2932  Oid op_lefttype;
2933  Oid op_righttype;
2934  Oid opno,
2935  collation,
2936  lsortop,
2937  rsortop,
2938  lstatop,
2939  rstatop,
2940  ltop,
2941  leop,
2942  revltop,
2943  revleop;
2944  bool isgt;
2945  Datum leftmin,
2946  leftmax,
2947  rightmin,
2948  rightmax;
2949  double selec;
2950 
2951  /* Set default results if we can't figure anything out. */
2952  /* XXX should default "start" fraction be a bit more than 0? */
2953  *leftstart = *rightstart = 0.0;
2954  *leftend = *rightend = 1.0;
2955 
2956  /* Deconstruct the merge clause */
2957  if (!is_opclause(clause))
2958  return; /* shouldn't happen */
2959  opno = ((OpExpr *) clause)->opno;
2960  collation = ((OpExpr *) clause)->inputcollid;
2961  left = get_leftop((Expr *) clause);
2962  right = get_rightop((Expr *) clause);
2963  if (!right)
2964  return; /* shouldn't happen */
2965 
2966  /* Look for stats for the inputs */
2967  examine_variable(root, left, 0, &leftvar);
2968  examine_variable(root, right, 0, &rightvar);
2969 
2970  /* Extract the operator's declared left/right datatypes */
2971  get_op_opfamily_properties(opno, opfamily, false,
2972  &op_strategy,
2973  &op_lefttype,
2974  &op_righttype);
2975  Assert(op_strategy == BTEqualStrategyNumber);
2976 
2977  /*
2978  * Look up the various operators we need. If we don't find them all, it
2979  * probably means the opfamily is broken, but we just fail silently.
2980  *
2981  * Note: we expect that pg_statistic histograms will be sorted by the '<'
2982  * operator, regardless of which sort direction we are considering.
2983  */
2984  switch (strategy)
2985  {
2986  case BTLessStrategyNumber:
2987  isgt = false;
2988  if (op_lefttype == op_righttype)
2989  {
2990  /* easy case */
2991  ltop = get_opfamily_member(opfamily,
2992  op_lefttype, op_righttype,
2994  leop = get_opfamily_member(opfamily,
2995  op_lefttype, op_righttype,
2997  lsortop = ltop;
2998  rsortop = ltop;
2999  lstatop = lsortop;
3000  rstatop = rsortop;
3001  revltop = ltop;
3002  revleop = leop;
3003  }
3004  else
3005  {
3006  ltop = get_opfamily_member(opfamily,
3007  op_lefttype, op_righttype,
3009  leop = get_opfamily_member(opfamily,
3010  op_lefttype, op_righttype,
3012  lsortop = get_opfamily_member(opfamily,
3013  op_lefttype, op_lefttype,
3015  rsortop = get_opfamily_member(opfamily,
3016  op_righttype, op_righttype,
3018  lstatop = lsortop;
3019  rstatop = rsortop;
3020  revltop = get_opfamily_member(opfamily,
3021  op_righttype, op_lefttype,
3023  revleop = get_opfamily_member(opfamily,
3024  op_righttype, op_lefttype,
3026  }
3027  break;
3029  /* descending-order case */
3030  isgt = true;
3031  if (op_lefttype == op_righttype)
3032  {
3033  /* easy case */
3034  ltop = get_opfamily_member(opfamily,
3035  op_lefttype, op_righttype,
3037  leop = get_opfamily_member(opfamily,
3038  op_lefttype, op_righttype,
3040  lsortop = ltop;
3041  rsortop = ltop;
3042  lstatop = get_opfamily_member(opfamily,
3043  op_lefttype, op_lefttype,
3045  rstatop = lstatop;
3046  revltop = ltop;
3047  revleop = leop;
3048  }
3049  else
3050  {
3051  ltop = get_opfamily_member(opfamily,
3052  op_lefttype, op_righttype,
3054  leop = get_opfamily_member(opfamily,
3055  op_lefttype, op_righttype,
3057  lsortop = get_opfamily_member(opfamily,
3058  op_lefttype, op_lefttype,
3060  rsortop = get_opfamily_member(opfamily,
3061  op_righttype, op_righttype,
3063  lstatop = get_opfamily_member(opfamily,
3064  op_lefttype, op_lefttype,
3066  rstatop = get_opfamily_member(opfamily,
3067  op_righttype, op_righttype,
3069  revltop = get_opfamily_member(opfamily,
3070  op_righttype, op_lefttype,
3072  revleop = get_opfamily_member(opfamily,
3073  op_righttype, op_lefttype,
3075  }
3076  break;
3077  default:
3078  goto fail; /* shouldn't get here */
3079  }
3080 
3081  if (!OidIsValid(lsortop) ||
3082  !OidIsValid(rsortop) ||
3083  !OidIsValid(lstatop) ||
3084  !OidIsValid(rstatop) ||
3085  !OidIsValid(ltop) ||
3086  !OidIsValid(leop) ||
3087  !OidIsValid(revltop) ||
3088  !OidIsValid(revleop))
3089  goto fail; /* insufficient info in catalogs */
3090 
3091  /* Try to get ranges of both inputs */
3092  if (!isgt)
3093  {
3094  if (!get_variable_range(root, &leftvar, lstatop, collation,
3095  &leftmin, &leftmax))
3096  goto fail; /* no range available from stats */
3097  if (!get_variable_range(root, &rightvar, rstatop, collation,
3098  &rightmin, &rightmax))
3099  goto fail; /* no range available from stats */
3100  }
3101  else
3102  {
3103  /* need to swap the max and min */
3104  if (!get_variable_range(root, &leftvar, lstatop, collation,
3105  &leftmax, &leftmin))
3106  goto fail; /* no range available from stats */
3107  if (!get_variable_range(root, &rightvar, rstatop, collation,
3108  &rightmax, &rightmin))
3109  goto fail; /* no range available from stats */
3110  }
3111 
3112  /*
3113  * Now, the fraction of the left variable that will be scanned is the
3114  * fraction that's <= the right-side maximum value. But only believe
3115  * non-default estimates, else stick with our 1.0.
3116  */
3117  selec = scalarineqsel(root, leop, isgt, true, collation, &leftvar,
3118  rightmax, op_righttype);
3119  if (selec != DEFAULT_INEQ_SEL)
3120  *leftend = selec;
3121 
3122  /* And similarly for the right variable. */
3123  selec = scalarineqsel(root, revleop, isgt, true, collation, &rightvar,
3124  leftmax, op_lefttype);
3125  if (selec != DEFAULT_INEQ_SEL)
3126  *rightend = selec;
3127 
3128  /*
3129  * Only one of the two "end" fractions can really be less than 1.0;
3130  * believe the smaller estimate and reset the other one to exactly 1.0. If
3131  * we get exactly equal estimates (as can easily happen with self-joins),
3132  * believe neither.
3133  */
3134  if (*leftend > *rightend)
3135  *leftend = 1.0;
3136  else if (*leftend < *rightend)
3137  *rightend = 1.0;
3138  else
3139  *leftend = *rightend = 1.0;
3140 
3141  /*
3142  * Also, the fraction of the left variable that will be scanned before the
3143  * first join pair is found is the fraction that's < the right-side
3144  * minimum value. But only believe non-default estimates, else stick with
3145  * our own default.
3146  */
3147  selec = scalarineqsel(root, ltop, isgt, false, collation, &leftvar,
3148  rightmin, op_righttype);
3149  if (selec != DEFAULT_INEQ_SEL)
3150  *leftstart = selec;
3151 
3152  /* And similarly for the right variable. */
3153  selec = scalarineqsel(root, revltop, isgt, false, collation, &rightvar,
3154  leftmin, op_lefttype);
3155  if (selec != DEFAULT_INEQ_SEL)
3156  *rightstart = selec;
3157 
3158  /*
3159  * Only one of the two "start" fractions can really be more than zero;
3160  * believe the larger estimate and reset the other one to exactly 0.0. If
3161  * we get exactly equal estimates (as can easily happen with self-joins),
3162  * believe neither.
3163  */
3164  if (*leftstart < *rightstart)
3165  *leftstart = 0.0;
3166  else if (*leftstart > *rightstart)
3167  *rightstart = 0.0;
3168  else
3169  *leftstart = *rightstart = 0.0;
3170 
3171  /*
3172  * If the sort order is nulls-first, we're going to have to skip over any
3173  * nulls too. These would not have been counted by scalarineqsel, and we
3174  * can safely add in this fraction regardless of whether we believe
3175  * scalarineqsel's results or not. But be sure to clamp the sum to 1.0!
3176  */
3177  if (nulls_first)
3178  {
3179  Form_pg_statistic stats;
3180 
3181  if (HeapTupleIsValid(leftvar.statsTuple))
3182  {
3183  stats = (Form_pg_statistic) GETSTRUCT(leftvar.statsTuple);
3184  *leftstart += stats->stanullfrac;
3185  CLAMP_PROBABILITY(*leftstart);
3186  *leftend += stats->stanullfrac;
3187  CLAMP_PROBABILITY(*leftend);
3188  }
3189  if (HeapTupleIsValid(rightvar.statsTuple))
3190  {
3191  stats = (Form_pg_statistic) GETSTRUCT(rightvar.statsTuple);
3192  *rightstart += stats->stanullfrac;
3193  CLAMP_PROBABILITY(*rightstart);
3194  *rightend += stats->stanullfrac;
3195  CLAMP_PROBABILITY(*rightend);
3196  }
3197  }
3198 
3199  /* Disbelieve start >= end, just in case that can happen */
3200  if (*leftstart >= *leftend)
3201  {
3202  *leftstart = 0.0;
3203  *leftend = 1.0;
3204  }
3205  if (*rightstart >= *rightend)
3206  {
3207  *rightstart = 0.0;
3208  *rightend = 1.0;
3209  }
3210 
3211 fail:
3212  ReleaseVariableStats(leftvar);
3213  ReleaseVariableStats(rightvar);
3214 }
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:135
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:165
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:74
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:93
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:81
static bool get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop, Oid collation, Datum *min, Datum *max)
Definition: selfuncs.c:5781
static double scalarineqsel(PlannerInfo *root, Oid operator, bool isgt, bool iseq, Oid collation, VariableStatData *vardata, Datum constval, Oid consttype)
Definition: selfuncs.c:579
#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, 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 1697 of file selfuncs.c.

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

References arg, CLAMP_PROBABILITY, DEFAULT_NOT_UNK_SEL, DEFAULT_UNK_SEL, elog(), ERROR, examine_variable(), GETSTRUCT, HeapTupleIsValid, IS_NOT_NULL, IS_NULL, IsA, ReleaseVariableStats, 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 2172 of file selfuncs.c.

2175 {
2176  Selectivity s1;
2177  Oid opno = linitial_oid(clause->opnos);
2178  Oid inputcollid = linitial_oid(clause->inputcollids);
2179  List *opargs;
2180  bool is_join_clause;
2181 
2182  /* Build equivalent arg list for single operator */
2183  opargs = list_make2(linitial(clause->largs), linitial(clause->rargs));
2184 
2185  /*
2186  * Decide if it's a join clause. This should match clausesel.c's
2187  * treat_as_join_clause(), except that we intentionally consider only the
2188  * leading columns and not the rest of the clause.
2189  */
2190  if (varRelid != 0)
2191  {
2192  /*
2193  * Caller is forcing restriction mode (eg, because we are examining an
2194  * inner indexscan qual).
2195  */
2196  is_join_clause = false;
2197  }
2198  else if (sjinfo == NULL)
2199  {
2200  /*
2201  * It must be a restriction clause, since it's being evaluated at a
2202  * scan node.
2203  */
2204  is_join_clause = false;
2205  }
2206  else
2207  {
2208  /*
2209  * Otherwise, it's a join if there's more than one base relation used.
2210  */
2211  is_join_clause = (NumRelids(root, (Node *) opargs) > 1);
2212  }
2213 
2214  if (is_join_clause)
2215  {
2216  /* Estimate selectivity for a join clause. */
2217  s1 = join_selectivity(root, opno,
2218  opargs,
2219  inputcollid,
2220  jointype,
2221  sjinfo);
2222  }
2223  else
2224  {
2225  /* Estimate selectivity for a restriction clause. */
2226  s1 = restriction_selectivity(root, opno,
2227  opargs,
2228  inputcollid,
2229  varRelid);
2230  }
2231 
2232  return s1;
2233 }
int NumRelids(PlannerInfo *root, Node *clause)
Definition: clauses.c:2010
#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:1899
Selectivity join_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: plancat.c:1938
char * s1

References join_selectivity(), RowCompareExpr::largs, linitial, linitial_oid, list_make2, NumRelids(), RowCompareExpr::rargs, restriction_selectivity(), 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 1815 of file selfuncs.c.

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

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(), 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 82 of file array_selfuncs.c.

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

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, 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 5619 of file selfuncs.c.

5620 {
5621  if (vardata->acl_ok)
5622  return true;
5623 
5624  if (!OidIsValid(func_oid))
5625  return false;
5626 
5627  if (get_func_leakproof(func_oid))
5628  return true;
5629 
5630  ereport(DEBUG2,
5631  (errmsg_internal("not using statistics because function \"%s\" is not leak-proof",
5632  get_func_name(func_oid))));
5633  return false;
5634 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
#define DEBUG2
Definition: elog.h:29
#define ereport(elevel,...)
Definition: elog.h:149
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1590
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1819

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

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

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

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

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 147 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 146 of file selfuncs.c.

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