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

Go to the source code of this file.

Data Structures

struct  EstimationInfo
 
struct  VariableStatData
 
struct  GenericCosts
 

Macros

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

Typedefs

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

Functions

void examine_variable (PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
 
bool statistic_proc_security_check (VariableStatData *vardata, Oid func_oid)
 
bool get_restriction_variable (PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
 
void get_join_variables (PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo, VariableStatData *vardata1, VariableStatData *vardata2, bool *join_is_reversed)
 
double get_variable_numdistinct (VariableStatData *vardata, bool *isdefault)
 
double mcv_selectivity (VariableStatData *vardata, FmgrInfo *opproc, Oid collation, Datum constval, bool varonleft, double *sumcommonp)
 
double histogram_selectivity (VariableStatData *vardata, FmgrInfo *opproc, Oid collation, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
 
double generic_restriction_selectivity (PlannerInfo *root, Oid oproid, Oid collation, List *args, int varRelid, double default_selectivity)
 
double ineq_histogram_selectivity (PlannerInfo *root, VariableStatData *vardata, Oid opoid, FmgrInfo *opproc, bool isgt, bool iseq, Oid collation, Datum constval, Oid consttype)
 
double var_eq_const (VariableStatData *vardata, Oid oproid, Oid collation, Datum constval, bool constisnull, bool varonleft, bool negate)
 
double var_eq_non_const (VariableStatData *vardata, Oid oproid, Oid collation, Node *other, bool varonleft, bool negate)
 
Selectivity boolvarsel (PlannerInfo *root, Node *arg, int varRelid)
 
Selectivity booltestsel (PlannerInfo *root, BoolTestType booltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
Selectivity nulltestsel (PlannerInfo *root, NullTestType nulltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
Selectivity scalararraysel (PlannerInfo *root, ScalarArrayOpExpr *clause, bool is_join_clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
double estimate_array_length (PlannerInfo *root, Node *arrayexpr)
 
Selectivity rowcomparesel (PlannerInfo *root, RowCompareExpr *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
void mergejoinscansel (PlannerInfo *root, Node *clause, Oid opfamily, int strategy, bool nulls_first, Selectivity *leftstart, Selectivity *leftend, Selectivity *rightstart, Selectivity *rightend)
 
double estimate_num_groups (PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, EstimationInfo *estinfo)
 
void estimate_hash_bucket_stats (PlannerInfo *root, Node *hashkey, double nbuckets, Selectivity *mcv_freq, Selectivity *bucketsize_frac)
 
double estimate_hashagg_tablesize (PlannerInfo *root, Path *path, const AggClauseCosts *agg_costs, double dNumGroups)
 
Listget_quals_from_indexclauses (List *indexclauses)
 
Cost index_other_operands_eval_cost (PlannerInfo *root, List *indexquals)
 
Listadd_predicate_to_index_quals (IndexOptInfo *index, List *indexQuals)
 
void genericcostestimate (PlannerInfo *root, IndexPath *path, double loop_count, GenericCosts *costs)
 
Selectivity scalararraysel_containment (PlannerInfo *root, Node *leftop, Node *rightop, Oid elemtype, bool isEquality, bool useOr, int varRelid)
 

Variables

PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook
 
PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook
 

Macro Definition Documentation

◆ CLAMP_PROBABILITY

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

Definition at line 63 of file selfuncs.h.

◆ DEFAULT_EQ_SEL

#define DEFAULT_EQ_SEL   0.005

Definition at line 34 of file selfuncs.h.

◆ DEFAULT_INEQ_SEL

#define DEFAULT_INEQ_SEL   0.3333333333333333

Definition at line 37 of file selfuncs.h.

◆ DEFAULT_MATCH_SEL

#define DEFAULT_MATCH_SEL   0.005

Definition at line 46 of file selfuncs.h.

◆ DEFAULT_MATCHING_SEL

#define DEFAULT_MATCHING_SEL   0.010

Definition at line 49 of file selfuncs.h.

◆ DEFAULT_MULTIRANGE_INEQ_SEL

#define DEFAULT_MULTIRANGE_INEQ_SEL   0.005

Definition at line 43 of file selfuncs.h.

◆ DEFAULT_NOT_UNK_SEL

#define DEFAULT_NOT_UNK_SEL   (1.0 - DEFAULT_UNK_SEL)

Definition at line 56 of file selfuncs.h.

◆ DEFAULT_NUM_DISTINCT

#define DEFAULT_NUM_DISTINCT   200

Definition at line 52 of file selfuncs.h.

◆ DEFAULT_RANGE_INEQ_SEL

#define DEFAULT_RANGE_INEQ_SEL   0.005

Definition at line 40 of file selfuncs.h.

◆ DEFAULT_UNK_SEL

#define DEFAULT_UNK_SEL   0.005

Definition at line 55 of file selfuncs.h.

◆ ReleaseVariableStats

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

Definition at line 99 of file selfuncs.h.

◆ SELFLAG_USED_DEFAULT

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

Definition at line 76 of file selfuncs.h.

Typedef Documentation

◆ EstimationInfo

◆ get_index_stats_hook_type

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

Definition at line 143 of file selfuncs.h.

◆ get_relation_stats_hook_type

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

Definition at line 138 of file selfuncs.h.

◆ VariableStatData

Function Documentation

◆ add_predicate_to_index_quals()

List * add_predicate_to_index_quals ( IndexOptInfo index,
List indexQuals 
)

Definition at line 6801 of file selfuncs.c.

6802{
6803 List *predExtraQuals = NIL;
6804 ListCell *lc;
6805
6806 if (index->indpred == NIL)
6807 return indexQuals;
6808
6809 foreach(lc, index->indpred)
6810 {
6811 Node *predQual = (Node *) lfirst(lc);
6812 List *oneQual = list_make1(predQual);
6813
6814 if (!predicate_implied_by(oneQual, indexQuals, false))
6815 predExtraQuals = list_concat(predExtraQuals, oneQual);
6816 }
6817 return list_concat(predExtraQuals, indexQuals);
6818}
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
#define list_make1(x1)
Definition: pg_list.h:212
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:152
Definition: pg_list.h:54
Definition: nodes.h:129
Definition: type.h:96

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

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

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

Referenced by clause_selectivity_ext().

◆ boolvarsel()

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

Definition at line 1513 of file selfuncs.c.

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

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

Referenced by clause_selectivity_ext().

◆ estimate_array_length()

double estimate_array_length ( PlannerInfo root,
Node arrayexpr 
)

Definition at line 2140 of file selfuncs.c.

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

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

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

◆ estimate_hash_bucket_stats()

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

Definition at line 3812 of file selfuncs.c.

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

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

Referenced by final_cost_hashjoin().

◆ estimate_hashagg_tablesize()

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

Definition at line 3931 of file selfuncs.c.

3933{
3934 Size hashentrysize;
3935
3936 hashentrysize = hash_agg_entry_size(list_length(root->aggtransinfos),
3937 path->pathtarget->width,
3938 agg_costs->transitionSpace);
3939
3940 /*
3941 * Note that this disregards the effect of fill-factor and growth policy
3942 * of the hash table. That's probably ok, given that the default
3943 * fill-factor is relatively high. It'd be hard to meaningfully factor in
3944 * "double-in-size" growth policies here.
3945 */
3946 return hashentrysize * dNumGroups;
3947}
size_t Size
Definition: c.h:562
Size hash_agg_entry_size(int numTrans, Size tupleWidth, Size transitionSpace)
Definition: nodeAgg.c:1703
Size transitionSpace
Definition: pathnodes.h:62

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

Referenced by consider_groupingsets_paths().

◆ estimate_num_groups()

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

Definition at line 3430 of file selfuncs.c.

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

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

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

◆ examine_variable()

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

Definition at line 5033 of file selfuncs.c.

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

References VariableStatData::acl_ok, ACL_SELECT, ACLCHECK_OK, arg, Assert, VariableStatData::atttype, VariableStatData::atttypmod, bms_difference(), bms_free(), bms_get_singleton_member(), bms_is_empty, bms_is_member(), bms_overlap(), 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, remove_nulling_relids(), root, RTE_RELATION, RangeTblEntry::rtekind, SearchSysCache3(), statext_expressions_load(), RelOptInfo::statlist, StatisticExtInfo::statOid, VariableStatData::statsTuple, RelOptInfo::userid, VariableStatData::var, Var::varattno, Var::varno, and VariableStatData::vartype.

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

◆ generic_restriction_selectivity()

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

Definition at line 915 of file selfuncs.c.

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

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

Referenced by ltreeparentsel(), and matchingsel().

◆ genericcostestimate()

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

Definition at line 6578 of file selfuncs.c.

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

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

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

◆ get_join_variables()

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

Definition at line 4964 of file selfuncs.c.

4967{
4968 Node *left,
4969 *right;
4970
4971 if (list_length(args) != 2)
4972 elog(ERROR, "join operator should take two arguments");
4973
4974 left = (Node *) linitial(args);
4975 right = (Node *) lsecond(args);
4976
4977 examine_variable(root, left, 0, vardata1);
4978 examine_variable(root, right, 0, vardata2);
4979
4980 if (vardata1->rel &&
4981 bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
4982 *join_is_reversed = true; /* var1 is on RHS */
4983 else if (vardata2->rel &&
4984 bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
4985 *join_is_reversed = true; /* var2 is on LHS */
4986 else
4987 *join_is_reversed = false;
4988}
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
Relids relids
Definition: pathnodes.h:890
Relids syn_lefthand
Definition: pathnodes.h:2926
Relids syn_righthand
Definition: pathnodes.h:2927

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

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

◆ get_quals_from_indexclauses()

List * get_quals_from_indexclauses ( List indexclauses)

Definition at line 6494 of file selfuncs.c.

6495{
6496 List *result = NIL;
6497 ListCell *lc;
6498
6499 foreach(lc, indexclauses)
6500 {
6501 IndexClause *iclause = lfirst_node(IndexClause, lc);
6502 ListCell *lc2;
6503
6504 foreach(lc2, iclause->indexquals)
6505 {
6506 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
6507
6508 result = lappend(result, rinfo);
6509 }
6510 }
6511 return result;
6512}
#define lfirst_node(type, lc)
Definition: pg_list.h:176
List * indexquals
Definition: pathnodes.h:1790

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

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

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

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

◆ get_variable_numdistinct()

double get_variable_numdistinct ( VariableStatData vardata,
bool *  isdefault 
)

Definition at line 5798 of file selfuncs.c.

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

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

6525{
6526 Cost qual_arg_cost = 0;
6527 ListCell *lc;
6528
6529 foreach(lc, indexquals)
6530 {
6531 Expr *clause = (Expr *) lfirst(lc);
6532 Node *other_operand;
6533 QualCost index_qual_cost;
6534
6535 /*
6536 * Index quals will have RestrictInfos, indexorderbys won't. Look
6537 * through RestrictInfo if present.
6538 */
6539 if (IsA(clause, RestrictInfo))
6540 clause = ((RestrictInfo *) clause)->clause;
6541
6542 if (IsA(clause, OpExpr))
6543 {
6544 OpExpr *op = (OpExpr *) clause;
6545
6546 other_operand = (Node *) lsecond(op->args);
6547 }
6548 else if (IsA(clause, RowCompareExpr))
6549 {
6550 RowCompareExpr *rc = (RowCompareExpr *) clause;
6551
6552 other_operand = (Node *) rc->rargs;
6553 }
6554 else if (IsA(clause, ScalarArrayOpExpr))
6555 {
6556 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
6557
6558 other_operand = (Node *) lsecond(saop->args);
6559 }
6560 else if (IsA(clause, NullTest))
6561 {
6562 other_operand = NULL;
6563 }
6564 else
6565 {
6566 elog(ERROR, "unsupported indexqual type: %d",
6567 (int) nodeTag(clause));
6568 other_operand = NULL; /* keep compiler quiet */
6569 }
6570
6571 cost_qual_eval_node(&index_qual_cost, other_operand, root);
6572 qual_arg_cost += index_qual_cost.startup + index_qual_cost.per_tuple;
6573 }
6574 return qual_arg_cost;
6575}
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:4758
#define nodeTag(nodeptr)
Definition: nodes.h:133
List * args
Definition: primnodes.h:853
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47

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

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

◆ ineq_histogram_selectivity()

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

Definition at line 1042 of file selfuncs.c.

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

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

Referenced by prefix_selectivity(), and scalarineqsel().

◆ mcv_selectivity()

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

Definition at line 733 of file selfuncs.c.

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

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

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

References Assert, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, CLAMP_PROBABILITY, DEFAULT_INEQ_SEL, examine_variable(), get_leftop(), get_op_opfamily_properties(), get_opfamily_member(), get_rightop(), get_variable_range(), GETSTRUCT(), HeapTupleIsValid, is_opclause(), OidIsValid, ReleaseVariableStats, root, scalarineqsel(), and VariableStatData::statsTuple.

Referenced by cached_scansel().

◆ nulltestsel()

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

Definition at line 1699 of file selfuncs.c.

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

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

Referenced by clause_selectivity_ext(), and clauselist_selectivity_ext().

◆ rowcomparesel()

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

Definition at line 2206 of file selfuncs.c.

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

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

Referenced by clause_selectivity_ext().

◆ scalararraysel()

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

Definition at line 1817 of file selfuncs.c.

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

References generate_unaccent_rules::args, ScalarArrayOpExpr::args, ARR_ELEMTYPE, Assert, CLAMP_PROBABILITY, DatumGetArrayTypeP, DatumGetFloat8(), deconstruct_array(), ArrayExpr::elements, TypeCacheEntry::eq_opr, estimate_expression_value(), exprCollation(), exprType(), fmgr_info(), FunctionCall4Coll(), FunctionCall5Coll(), get_base_element_type(), get_negator(), get_oprjoin(), get_oprrest(), get_typlenbyval(), get_typlenbyvalalign(), i, Int16GetDatum(), Int32GetDatum(), IsA, lfirst, linitial, list_length(), list_make2, lookup_type_cache(), lsecond, makeConst(), makeNode, ObjectIdGetDatum(), OidIsValid, ScalarArrayOpExpr::opno, PointerGetDatum(), root, s1, s2, scalararraysel_containment(), strip_array_coercion(), TYPECACHE_EQ_OPR, CaseTestExpr::typeId, and ScalarArrayOpExpr::useOr.

Referenced by clause_selectivity_ext().

◆ scalararraysel_containment()

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

Definition at line 81 of file array_selfuncs.c.

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

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

Referenced by scalararraysel().

◆ statistic_proc_security_check()

bool statistic_proc_security_check ( VariableStatData vardata,
Oid  func_oid 
)

Definition at line 5769 of file selfuncs.c.

5770{
5771 if (vardata->acl_ok)
5772 return true;
5773
5774 if (!OidIsValid(func_oid))
5775 return false;
5776
5777 if (get_func_leakproof(func_oid))
5778 return true;
5779
5781 (errmsg_internal("not using statistics because function \"%s\" is not leakproof",
5782 get_func_name(func_oid))));
5783 return false;
5784}
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define DEBUG2
Definition: elog.h:29
#define ereport(elevel,...)
Definition: elog.h:149
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1864
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1635

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

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

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

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

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

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