PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
selfuncs.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <float.h>
#include <math.h>
#include "access/gin.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "catalog/index.h"
#include "catalog/pg_am.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_opfamily.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "mb/pg_wchar.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/plancat.h"
#include "optimizer/predtest.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/var.h"
#include "parser/parse_clause.h"
#include "parser/parse_coerce.h"
#include "parser/parsetree.h"
#include "statistics/statistics.h"
#include "utils/builtins.h"
#include "utils/bytea.h"
#include "utils/date.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/index_selfuncs.h"
#include "utils/lsyscache.h"
#include "utils/nabstime.h"
#include "utils/pg_locale.h"
#include "utils/rel.h"
#include "utils/selfuncs.h"
#include "utils/spccache.h"
#include "utils/syscache.h"
#include "utils/timestamp.h"
#include "utils/tqual.h"
#include "utils/typcache.h"
#include "utils/varlena.h"
Include dependency graph for selfuncs.c:

Go to the source code of this file.

Data Structures

struct  GroupVarInfo
 
struct  GinQualCounts
 

Macros

#define FIXED_CHAR_SEL   0.20 /* about 1/5 */
 
#define CHAR_RANGE_SEL   0.25
 
#define ANY_CHAR_SEL   0.9 /* not 1, since it won't match end-of-string */
 
#define FULL_WILDCARD_SEL   5.0
 
#define PARTIAL_WILDCARD_SEL   2.0
 

Functions

static double var_eq_const (VariableStatData *vardata, Oid operator, Datum constval, bool constisnull, bool varonleft)
 
static double var_eq_non_const (VariableStatData *vardata, Oid operator, Node *other, bool varonleft)
 
static double ineq_histogram_selectivity (PlannerInfo *root, VariableStatData *vardata, FmgrInfo *opproc, bool isgt, Datum constval, Oid consttype)
 
static double eqjoinsel_inner (Oid operator, VariableStatData *vardata1, VariableStatData *vardata2)
 
static double eqjoinsel_semi (Oid operator, VariableStatData *vardata1, VariableStatData *vardata2, RelOptInfo *inner_rel)
 
static bool estimate_multivariate_ndistinct (PlannerInfo *root, RelOptInfo *rel, List **varinfos, double *ndistinct)
 
static bool convert_to_scalar (Datum value, Oid valuetypid, double *scaledvalue, Datum lobound, Datum hibound, Oid boundstypid, double *scaledlobound, double *scaledhibound)
 
static double convert_numeric_to_scalar (Datum value, Oid typid)
 
static void convert_string_to_scalar (char *value, double *scaledvalue, char *lobound, double *scaledlobound, char *hibound, double *scaledhibound)
 
static void convert_bytea_to_scalar (Datum value, double *scaledvalue, Datum lobound, double *scaledlobound, Datum hibound, double *scaledhibound)
 
static double convert_one_string_to_scalar (char *value, int rangelo, int rangehi)
 
static double convert_one_bytea_to_scalar (unsigned char *value, int valuelen, int rangelo, int rangehi)
 
static char * convert_string_datum (Datum value, Oid typid)
 
static double convert_timevalue_to_scalar (Datum value, Oid typid)
 
static void examine_simple_variable (PlannerInfo *root, Var *var, VariableStatData *vardata)
 
static bool get_variable_range (PlannerInfo *root, VariableStatData *vardata, Oid sortop, Datum *min, Datum *max)
 
static bool get_actual_variable_range (PlannerInfo *root, VariableStatData *vardata, Oid sortop, Datum *min, Datum *max)
 
static RelOptInfofind_join_input_rel (PlannerInfo *root, Relids relids)
 
static Selectivity prefix_selectivity (PlannerInfo *root, VariableStatData *vardata, Oid vartype, Oid opfamily, Const *prefixcon)
 
static Selectivity like_selectivity (const char *patt, int pattlen, bool case_insensitive)
 
static Selectivity regex_selectivity (const char *patt, int pattlen, bool case_insensitive, int fixed_prefix_len)
 
static Datum string_to_datum (const char *str, Oid datatype)
 
static Conststring_to_const (const char *str, Oid datatype)
 
static Conststring_to_bytea_const (const char *str, size_t str_len)
 
static Listadd_predicate_to_quals (IndexOptInfo *index, List *indexQuals)
 
Datum eqsel (PG_FUNCTION_ARGS)
 
Datum neqsel (PG_FUNCTION_ARGS)
 
static double scalarineqsel (PlannerInfo *root, Oid operator, bool isgt, VariableStatData *vardata, Datum constval, Oid consttype)
 
double mcv_selectivity (VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, double *sumcommonp)
 
double histogram_selectivity (VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
 
Datum scalarltsel (PG_FUNCTION_ARGS)
 
Datum scalargtsel (PG_FUNCTION_ARGS)
 
static double patternsel (PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate)
 
Datum regexeqsel (PG_FUNCTION_ARGS)
 
Datum icregexeqsel (PG_FUNCTION_ARGS)
 
Datum likesel (PG_FUNCTION_ARGS)
 
Datum iclikesel (PG_FUNCTION_ARGS)
 
Datum regexnesel (PG_FUNCTION_ARGS)
 
Datum icregexnesel (PG_FUNCTION_ARGS)
 
Datum nlikesel (PG_FUNCTION_ARGS)
 
Datum icnlikesel (PG_FUNCTION_ARGS)
 
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)
 
static Nodestrip_array_coercion (Node *node)
 
Selectivity scalararraysel (PlannerInfo *root, ScalarArrayOpExpr *clause, bool is_join_clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
int estimate_array_length (Node *arrayexpr)
 
Selectivity rowcomparesel (PlannerInfo *root, RowCompareExpr *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
Datum eqjoinsel (PG_FUNCTION_ARGS)
 
Datum neqjoinsel (PG_FUNCTION_ARGS)
 
Datum scalarltjoinsel (PG_FUNCTION_ARGS)
 
Datum scalargtjoinsel (PG_FUNCTION_ARGS)
 
static double patternjoinsel (PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate)
 
Datum regexeqjoinsel (PG_FUNCTION_ARGS)
 
Datum icregexeqjoinsel (PG_FUNCTION_ARGS)
 
Datum likejoinsel (PG_FUNCTION_ARGS)
 
Datum iclikejoinsel (PG_FUNCTION_ARGS)
 
Datum regexnejoinsel (PG_FUNCTION_ARGS)
 
Datum icregexnejoinsel (PG_FUNCTION_ARGS)
 
Datum nlikejoinsel (PG_FUNCTION_ARGS)
 
Datum icnlikejoinsel (PG_FUNCTION_ARGS)
 
void mergejoinscansel (PlannerInfo *root, Node *clause, Oid opfamily, int strategy, bool nulls_first, Selectivity *leftstart, Selectivity *leftend, Selectivity *rightstart, Selectivity *rightend)
 
static Listadd_unique_group_var (PlannerInfo *root, List *varinfos, Node *var, VariableStatData *vardata)
 
double estimate_num_groups (PlannerInfo *root, List *groupExprs, double input_rows, List **pgset)
 
Selectivity estimate_hash_bucketsize (PlannerInfo *root, Node *hashkey, double nbuckets)
 
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)
 
void examine_variable (PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
 
double get_variable_numdistinct (VariableStatData *vardata, bool *isdefault)
 
static int pattern_char_isalpha (char c, bool is_multibyte, pg_locale_t locale, bool locale_is_c)
 
static Pattern_Prefix_Status like_fixed_prefix (Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
 
static Pattern_Prefix_Status regex_fixed_prefix (Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
 
Pattern_Prefix_Status pattern_fixed_prefix (Const *patt, Pattern_Type ptype, Oid collation, Const **prefix, Selectivity *rest_selec)
 
static Selectivity regex_selectivity_sub (const char *patt, int pattlen, bool case_insensitive)
 
static bool byte_increment (unsigned char *ptr, int len)
 
Constmake_greater_string (const Const *str_const, FmgrInfo *ltproc, Oid collation)
 
Listdeconstruct_indexquals (IndexPath *path)
 
static Cost other_operands_eval_cost (PlannerInfo *root, List *qinfos)
 
static Cost orderby_operands_eval_cost (PlannerInfo *root, IndexPath *path)
 
void genericcostestimate (PlannerInfo *root, IndexPath *path, double loop_count, List *qinfos, GenericCosts *costs)
 
void btcostestimate (PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 
void hashcostestimate (PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 
void gistcostestimate (PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 
void spgcostestimate (PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 
static bool gincost_pattern (IndexOptInfo *index, int indexcol, Oid clause_op, Datum query, GinQualCounts *counts)
 
static bool gincost_opexpr (PlannerInfo *root, IndexOptInfo *index, IndexQualInfo *qinfo, GinQualCounts *counts)
 
static bool gincost_scalararrayopexpr (PlannerInfo *root, IndexOptInfo *index, IndexQualInfo *qinfo, double numIndexEntries, GinQualCounts *counts)
 
void gincostestimate (PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 
void brincostestimate (PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 

Variables

get_relation_stats_hook_type get_relation_stats_hook = NULL
 
get_index_stats_hook_type get_index_stats_hook = NULL
 

Macro Definition Documentation

#define ANY_CHAR_SEL   0.9 /* not 1, since it won't match end-of-string */

Definition at line 5787 of file selfuncs.c.

Referenced by like_selectivity(), and regex_selectivity_sub().

#define CHAR_RANGE_SEL   0.25

Definition at line 5786 of file selfuncs.c.

Referenced by regex_selectivity_sub().

#define FIXED_CHAR_SEL   0.20 /* about 1/5 */

Definition at line 5785 of file selfuncs.c.

Referenced by like_selectivity(), regex_selectivity(), and regex_selectivity_sub().

#define FULL_WILDCARD_SEL   5.0

Definition at line 5788 of file selfuncs.c.

Referenced by like_selectivity(), and regex_selectivity().

#define PARTIAL_WILDCARD_SEL   2.0

Definition at line 5789 of file selfuncs.c.

Referenced by regex_selectivity_sub().

Function Documentation

static List * add_predicate_to_quals ( IndexOptInfo index,
List indexQuals 
)
static

Definition at line 6600 of file selfuncs.c.

References IndexOptInfo::indpred, lfirst, list_concat(), list_make1, NIL, and predicate_implied_by().

Referenced by btcostestimate(), and genericcostestimate().

6601 {
6602  List *predExtraQuals = NIL;
6603  ListCell *lc;
6604 
6605  if (index->indpred == NIL)
6606  return indexQuals;
6607 
6608  foreach(lc, index->indpred)
6609  {
6610  Node *predQual = (Node *) lfirst(lc);
6611  List *oneQual = list_make1(predQual);
6612 
6613  if (!predicate_implied_by(oneQual, indexQuals))
6614  predExtraQuals = list_concat(predExtraQuals, oneQual);
6615  }
6616  /* list_concat avoids modifying the passed-in indexQuals list */
6617  return list_concat(predExtraQuals, indexQuals);
6618 }
#define NIL
Definition: pg_list.h:69
bool predicate_implied_by(List *predicate_list, List *restrictinfo_list)
Definition: predtest.c:128
Definition: nodes.h:504
List * list_concat(List *list1, List *list2)
Definition: list.c:321
#define list_make1(x1)
Definition: pg_list.h:133
#define lfirst(lc)
Definition: pg_list.h:106
List * indpred
Definition: relation.h:616
Definition: pg_list.h:45
static List* add_unique_group_var ( PlannerInfo root,
List varinfos,
Node var,
VariableStatData vardata 
)
static

Definition at line 3159 of file selfuncs.c.

References equal(), exprs_known_equal(), get_variable_numdistinct(), lappend(), lfirst, list_delete_ptr(), list_head(), lnext, GroupVarInfo::ndistinct, palloc(), VariableStatData::rel, GroupVarInfo::rel, and GroupVarInfo::var.

Referenced by estimate_num_groups().

3161 {
3162  GroupVarInfo *varinfo;
3163  double ndistinct;
3164  bool isdefault;
3165  ListCell *lc;
3166 
3167  ndistinct = get_variable_numdistinct(vardata, &isdefault);
3168 
3169  /* cannot use foreach here because of possible list_delete */
3170  lc = list_head(varinfos);
3171  while (lc)
3172  {
3173  varinfo = (GroupVarInfo *) lfirst(lc);
3174 
3175  /* must advance lc before list_delete possibly pfree's it */
3176  lc = lnext(lc);
3177 
3178  /* Drop exact duplicates */
3179  if (equal(var, varinfo->var))
3180  return varinfos;
3181 
3182  /*
3183  * Drop known-equal vars, but only if they belong to different
3184  * relations (see comments for estimate_num_groups)
3185  */
3186  if (vardata->rel != varinfo->rel &&
3187  exprs_known_equal(root, var, varinfo->var))
3188  {
3189  if (varinfo->ndistinct <= ndistinct)
3190  {
3191  /* Keep older item, forget new one */
3192  return varinfos;
3193  }
3194  else
3195  {
3196  /* Delete the older item */
3197  varinfos = list_delete_ptr(varinfos, varinfo);
3198  }
3199  }
3200  }
3201 
3202  varinfo = (GroupVarInfo *) palloc(sizeof(GroupVarInfo));
3203 
3204  varinfo->var = var;
3205  varinfo->rel = vardata->rel;
3206  varinfo->ndistinct = ndistinct;
3207  varinfos = lappend(varinfos, varinfo);
3208  return varinfos;
3209 }
bool exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2)
Definition: equivclass.c:1943
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2946
RelOptInfo * rel
Definition: selfuncs.h:70
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
double ndistinct
Definition: selfuncs.c:3155
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition: selfuncs.c:4904
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
Node * var
Definition: selfuncs.c:3153
#define lnext(lc)
Definition: pg_list.h:105
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
void * palloc(Size size)
Definition: mcxt.c:849
RelOptInfo * rel
Definition: selfuncs.c:3154
Selectivity booltestsel ( PlannerInfo root,
BoolTestType  booltesttype,
Node arg,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo 
)

Definition at line 1499 of file selfuncs.c.

References VariableStatData::atttype, VariableStatData::atttypmod, CLAMP_PROBABILITY, clause_selectivity(), DatumGetBool, DEFAULT_NOT_UNK_SEL, DEFAULT_UNK_SEL, elog, ERROR, examine_variable(), free_attstatsslot(), get_attstatsslot(), GETSTRUCT, HeapTupleIsValid, InvalidOid, IS_FALSE, IS_NOT_FALSE, IS_NOT_TRUE, IS_NOT_UNKNOWN, IS_TRUE, IS_UNKNOWN, NULL, ReleaseVariableStats, STATISTIC_KIND_MCV, VariableStatData::statsTuple, and values.

Referenced by clause_selectivity().

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

Definition at line 1460 of file selfuncs.c.

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

Referenced by clause_selectivity().

1461 {
1462  VariableStatData vardata;
1463  double selec;
1464 
1465  examine_variable(root, arg, varRelid, &vardata);
1466  if (HeapTupleIsValid(vardata.statsTuple))
1467  {
1468  /*
1469  * A boolean variable V is equivalent to the clause V = 't', so we
1470  * compute the selectivity as if that is what we have.
1471  */
1472  selec = var_eq_const(&vardata, BooleanEqualOperator,
1473  BoolGetDatum(true), false, true);
1474  }
1475  else if (is_funcclause(arg))
1476  {
1477  /*
1478  * If we have no stats and it's a function call, estimate 0.3333333.
1479  * This seems a pretty unprincipled choice, but Postgres has been
1480  * using that estimate for function calls since 1992. The hoariness
1481  * of this behavior suggests that we should not be in too much hurry
1482  * to use another value.
1483  */
1484  selec = 0.3333333;
1485  }
1486  else
1487  {
1488  /* Otherwise, the default estimate is 0.5 */
1489  selec = 0.5;
1490  }
1491  ReleaseVariableStats(vardata);
1492  return selec;
1493 }
HeapTuple statsTuple
Definition: selfuncs.h:71
#define is_funcclause(clause)
Definition: clauses.h:21
#define BooleanEqualOperator
Definition: pg_operator.h:114
static double var_eq_const(VariableStatData *vardata, Oid operator, Datum constval, bool constisnull, bool varonleft)
Definition: selfuncs.c:269
#define BoolGetDatum(X)
Definition: postgres.h:408
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4560
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:80
void brincostestimate ( PlannerInfo root,
IndexPath path,
double  loop_count,
Cost indexStartupCost,
Cost indexTotalCost,
Selectivity indexSelectivity,
double *  indexCorrelation,
double *  indexPages 
)

Definition at line 7706 of file selfuncs.c.

References clauselist_selectivity(), cpu_index_tuple_cost, cpu_operator_cost, deconstruct_indexquals(), get_tablespace_page_costs(), IndexPath::indexinfo, IndexPath::indexorderbys, IndexPath::indexquals, JOIN_INNER, list_length(), NULL, orderby_operands_eval_cost(), other_operands_eval_cost(), IndexOptInfo::pages, IndexOptInfo::rel, RelOptInfo::relid, IndexOptInfo::reltablespace, and IndexOptInfo::tuples.

Referenced by brinhandler().

7710 {
7711  IndexOptInfo *index = path->indexinfo;
7712  List *indexQuals = path->indexquals;
7713  List *indexOrderBys = path->indexorderbys;
7714  double numPages = index->pages;
7715  double numTuples = index->tuples;
7716  List *qinfos;
7717  Cost spc_seq_page_cost;
7718  Cost spc_random_page_cost;
7719  double qual_op_cost;
7720  double qual_arg_cost;
7721 
7722  /* Do preliminary analysis of indexquals */
7723  qinfos = deconstruct_indexquals(path);
7724 
7725  /* fetch estimated page cost for tablespace containing index */
7727  &spc_random_page_cost,
7728  &spc_seq_page_cost);
7729 
7730  /*
7731  * BRIN indexes are always read in full; use that as startup cost.
7732  *
7733  * XXX maybe only include revmap pages here?
7734  */
7735  *indexStartupCost = spc_seq_page_cost * numPages * loop_count;
7736 
7737  /*
7738  * To read a BRIN index there might be a bit of back and forth over
7739  * regular pages, as revmap might point to them out of sequential order;
7740  * calculate this as reading the whole index in random order.
7741  */
7742  *indexTotalCost = spc_random_page_cost * numPages * loop_count;
7743 
7744  *indexSelectivity =
7745  clauselist_selectivity(root, indexQuals,
7746  path->indexinfo->rel->relid,
7747  JOIN_INNER, NULL);
7748  *indexCorrelation = 1;
7749 
7750  /*
7751  * Add on index qual eval costs, much as in genericcostestimate.
7752  */
7753  qual_arg_cost = other_operands_eval_cost(root, qinfos) +
7754  orderby_operands_eval_cost(root, path);
7755  qual_op_cost = cpu_operator_cost *
7756  (list_length(indexQuals) + list_length(indexOrderBys));
7757 
7758  *indexStartupCost += qual_arg_cost;
7759  *indexTotalCost += qual_arg_cost;
7760  *indexTotalCost += (numTuples * *indexSelectivity) * (cpu_index_tuple_cost + qual_op_cost);
7761  *indexPages = index->pages;
7762 
7763  /* XXX what about pages_per_range? */
7764 }
IndexOptInfo * indexinfo
Definition: relation.h:995
Oid reltablespace
Definition: relation.h:594
static Cost other_operands_eval_cost(PlannerInfo *root, List *qinfos)
Definition: selfuncs.c:6327
double tuples
Definition: relation.h:599
List * deconstruct_indexquals(IndexPath *path)
Definition: selfuncs.c:6232
static Cost orderby_operands_eval_cost(PlannerInfo *root, IndexPath *path)
Definition: selfuncs.c:6352
Definition: type.h:90
BlockNumber pages
Definition: relation.h:598
List * indexquals
Definition: relation.h:997
RelOptInfo * rel
Definition: relation.h:595
double cpu_operator_cost
Definition: costsize.c:108
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition: spccache.c:182
Index relid
Definition: relation.h:522
List * indexorderbys
Definition: relation.h:999
#define NULL
Definition: c.h:229
static int list_length(const List *l)
Definition: pg_list.h:89
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:92
Definition: pg_list.h:45
double cpu_index_tuple_cost
Definition: costsize.c:107
double Cost
Definition: nodes.h:628
void btcostestimate ( PlannerInfo root,
IndexPath path,
double  loop_count,
Cost indexStartupCost,
Cost indexTotalCost,
Selectivity indexSelectivity,
double *  indexCorrelation,
double *  indexPages 
)

Definition at line 6622 of file selfuncs.c.

References add_predicate_to_quals(), Assert, BoolGetDatum, BTEqualStrategyNumber, BTLessStrategyNumber, RestrictInfo::clause, IndexQualInfo::clause_op, clauselist_selectivity(), cpu_operator_cost, deconstruct_indexquals(), elog, ERROR, estimate_array_length(), free_attstatsslot(), VariableStatData::freefunc, genericcostestimate(), get_attstatsslot(), get_index_stats_hook, get_op_opfamily_strategy(), get_opfamily_member(), get_relation_stats_hook, HeapTupleIsValid, IndexQualInfo::indexcol, GenericCosts::indexCorrelation, IndexPath::indexinfo, IndexOptInfo::indexkeys, IndexOptInfo::indexoid, GenericCosts::indexSelectivity, GenericCosts::indexStartupCost, GenericCosts::indexTotalCost, RangeTblEntry::inh, Int16GetDatum, InvalidOid, IS_NULL, IsA, JOIN_INNER, lappend(), lfirst, MemSet, IndexOptInfo::ncolumns, NIL, NULL, NullTest::nulltesttype, GenericCosts::num_sa_scans, GenericCosts::numIndexPages, GenericCosts::numIndexTuples, ObjectIdGetDatum, OidIsValid, IndexOptInfo::opcintype, IndexOptInfo::opfamily, IndexQualInfo::other_operand, planner_rt_fetch, IndexOptInfo::rel, ReleaseSysCache(), ReleaseVariableStats, RelOptInfo::relid, RangeTblEntry::relid, IndexOptInfo::reverse_sort, IndexQualInfo::rinfo, rint(), RTE_RELATION, RangeTblEntry::rtekind, SearchSysCache3, STATISTIC_KIND_CORRELATION, STATRELATTINH, VariableStatData::statsTuple, IndexOptInfo::tree_height, RelOptInfo::tuples, IndexOptInfo::tuples, and IndexOptInfo::unique.

Referenced by bthandler().

6626 {
6627  IndexOptInfo *index = path->indexinfo;
6628  List *qinfos;
6629  GenericCosts costs;
6630  Oid relid;
6631  AttrNumber colnum;
6632  VariableStatData vardata;
6633  double numIndexTuples;
6634  Cost descentCost;
6635  List *indexBoundQuals;
6636  int indexcol;
6637  bool eqQualHere;
6638  bool found_saop;
6639  bool found_is_null_op;
6640  double num_sa_scans;
6641  ListCell *lc;
6642 
6643  /* Do preliminary analysis of indexquals */
6644  qinfos = deconstruct_indexquals(path);
6645 
6646  /*
6647  * For a btree scan, only leading '=' quals plus inequality quals for the
6648  * immediately next attribute contribute to index selectivity (these are
6649  * the "boundary quals" that determine the starting and stopping points of
6650  * the index scan). Additional quals can suppress visits to the heap, so
6651  * it's OK to count them in indexSelectivity, but they should not count
6652  * for estimating numIndexTuples. So we must examine the given indexquals
6653  * to find out which ones count as boundary quals. We rely on the
6654  * knowledge that they are given in index column order.
6655  *
6656  * For a RowCompareExpr, we consider only the first column, just as
6657  * rowcomparesel() does.
6658  *
6659  * If there's a ScalarArrayOpExpr in the quals, we'll actually perform N
6660  * index scans not one, but the ScalarArrayOpExpr's operator can be
6661  * considered to act the same as it normally does.
6662  */
6663  indexBoundQuals = NIL;
6664  indexcol = 0;
6665  eqQualHere = false;
6666  found_saop = false;
6667  found_is_null_op = false;
6668  num_sa_scans = 1;
6669  foreach(lc, qinfos)
6670  {
6671  IndexQualInfo *qinfo = (IndexQualInfo *) lfirst(lc);
6672  RestrictInfo *rinfo = qinfo->rinfo;
6673  Expr *clause = rinfo->clause;
6674  Oid clause_op;
6675  int op_strategy;
6676 
6677  if (indexcol != qinfo->indexcol)
6678  {
6679  /* Beginning of a new column's quals */
6680  if (!eqQualHere)
6681  break; /* done if no '=' qual for indexcol */
6682  eqQualHere = false;
6683  indexcol++;
6684  if (indexcol != qinfo->indexcol)
6685  break; /* no quals at all for indexcol */
6686  }
6687 
6688  if (IsA(clause, ScalarArrayOpExpr))
6689  {
6690  int alength = estimate_array_length(qinfo->other_operand);
6691 
6692  found_saop = true;
6693  /* count up number of SA scans induced by indexBoundQuals only */
6694  if (alength > 1)
6695  num_sa_scans *= alength;
6696  }
6697  else if (IsA(clause, NullTest))
6698  {
6699  NullTest *nt = (NullTest *) clause;
6700 
6701  if (nt->nulltesttype == IS_NULL)
6702  {
6703  found_is_null_op = true;
6704  /* IS NULL is like = for selectivity determination purposes */
6705  eqQualHere = true;
6706  }
6707  }
6708 
6709  /*
6710  * We would need to commute the clause_op if not varonleft, except
6711  * that we only care if it's equality or not, so that refinement is
6712  * unnecessary.
6713  */
6714  clause_op = qinfo->clause_op;
6715 
6716  /* check for equality operator */
6717  if (OidIsValid(clause_op))
6718  {
6719  op_strategy = get_op_opfamily_strategy(clause_op,
6720  index->opfamily[indexcol]);
6721  Assert(op_strategy != 0); /* not a member of opfamily?? */
6722  if (op_strategy == BTEqualStrategyNumber)
6723  eqQualHere = true;
6724  }
6725 
6726  indexBoundQuals = lappend(indexBoundQuals, rinfo);
6727  }
6728 
6729  /*
6730  * If index is unique and we found an '=' clause for each column, we can
6731  * just assume numIndexTuples = 1 and skip the expensive
6732  * clauselist_selectivity calculations. However, a ScalarArrayOp or
6733  * NullTest invalidates that theory, even though it sets eqQualHere.
6734  */
6735  if (index->unique &&
6736  indexcol == index->ncolumns - 1 &&
6737  eqQualHere &&
6738  !found_saop &&
6739  !found_is_null_op)
6740  numIndexTuples = 1.0;
6741  else
6742  {
6743  List *selectivityQuals;
6744  Selectivity btreeSelectivity;
6745 
6746  /*
6747  * If the index is partial, AND the index predicate with the
6748  * index-bound quals to produce a more accurate idea of the number of
6749  * rows covered by the bound conditions.
6750  */
6751  selectivityQuals = add_predicate_to_quals(index, indexBoundQuals);
6752 
6753  btreeSelectivity = clauselist_selectivity(root, selectivityQuals,
6754  index->rel->relid,
6755  JOIN_INNER,
6756  NULL);
6757  numIndexTuples = btreeSelectivity * index->rel->tuples;
6758 
6759  /*
6760  * As in genericcostestimate(), we have to adjust for any
6761  * ScalarArrayOpExpr quals included in indexBoundQuals, and then round
6762  * to integer.
6763  */
6764  numIndexTuples = rint(numIndexTuples / num_sa_scans);
6765  }
6766 
6767  /*
6768  * Now do generic index cost estimation.
6769  */
6770  MemSet(&costs, 0, sizeof(costs));
6771  costs.numIndexTuples = numIndexTuples;
6772 
6773  genericcostestimate(root, path, loop_count, qinfos, &costs);
6774 
6775  /*
6776  * Add a CPU-cost component to represent the costs of initial btree
6777  * descent. We don't charge any I/O cost for touching upper btree levels,
6778  * since they tend to stay in cache, but we still have to do about log2(N)
6779  * comparisons to descend a btree of N leaf tuples. We charge one
6780  * cpu_operator_cost per comparison.
6781  *
6782  * If there are ScalarArrayOpExprs, charge this once per SA scan. The
6783  * ones after the first one are not startup cost so far as the overall
6784  * plan is concerned, so add them only to "total" cost.
6785  */
6786  if (index->tuples > 1) /* avoid computing log(0) */
6787  {
6788  descentCost = ceil(log(index->tuples) / log(2.0)) * cpu_operator_cost;
6789  costs.indexStartupCost += descentCost;
6790  costs.indexTotalCost += costs.num_sa_scans * descentCost;
6791  }
6792 
6793  /*
6794  * Even though we're not charging I/O cost for touching upper btree pages,
6795  * it's still reasonable to charge some CPU cost per page descended
6796  * through. Moreover, if we had no such charge at all, bloated indexes
6797  * would appear to have the same search cost as unbloated ones, at least
6798  * in cases where only a single leaf page is expected to be visited. This
6799  * cost is somewhat arbitrarily set at 50x cpu_operator_cost per page
6800  * touched. The number of such pages is btree tree height plus one (ie,
6801  * we charge for the leaf page too). As above, charge once per SA scan.
6802  */
6803  descentCost = (index->tree_height + 1) * 50.0 * cpu_operator_cost;
6804  costs.indexStartupCost += descentCost;
6805  costs.indexTotalCost += costs.num_sa_scans * descentCost;
6806 
6807  /*
6808  * If we can get an estimate of the first column's ordering correlation C
6809  * from pg_statistic, estimate the index correlation as C for a
6810  * single-column index, or C * 0.75 for multiple columns. (The idea here
6811  * is that multiple columns dilute the importance of the first column's
6812  * ordering, but don't negate it entirely. Before 8.0 we divided the
6813  * correlation by the number of columns, but that seems too strong.)
6814  */
6815  MemSet(&vardata, 0, sizeof(vardata));
6816 
6817  if (index->indexkeys[0] != 0)
6818  {
6819  /* Simple variable --- look to stats for the underlying table */
6820  RangeTblEntry *rte = planner_rt_fetch(index->rel->relid, root);
6821 
6822  Assert(rte->rtekind == RTE_RELATION);
6823  relid = rte->relid;
6824  Assert(relid != InvalidOid);
6825  colnum = index->indexkeys[0];
6826 
6828  (*get_relation_stats_hook) (root, rte, colnum, &vardata))
6829  {
6830  /*
6831  * The hook took control of acquiring a stats tuple. If it did
6832  * supply a tuple, it'd better have supplied a freefunc.
6833  */
6834  if (HeapTupleIsValid(vardata.statsTuple) &&
6835  !vardata.freefunc)
6836  elog(ERROR, "no function provided to release variable stats with");
6837  }
6838  else
6839  {
6841  ObjectIdGetDatum(relid),
6842  Int16GetDatum(colnum),
6843  BoolGetDatum(rte->inh));
6844  vardata.freefunc = ReleaseSysCache;
6845  }
6846  }
6847  else
6848  {
6849  /* Expression --- maybe there are stats for the index itself */
6850  relid = index->indexoid;
6851  colnum = 1;
6852 
6853  if (get_index_stats_hook &&
6854  (*get_index_stats_hook) (root, relid, colnum, &vardata))
6855  {
6856  /*
6857  * The hook took control of acquiring a stats tuple. If it did
6858  * supply a tuple, it'd better have supplied a freefunc.
6859  */
6860  if (HeapTupleIsValid(vardata.statsTuple) &&
6861  !vardata.freefunc)
6862  elog(ERROR, "no function provided to release variable stats with");
6863  }
6864  else
6865  {
6867  ObjectIdGetDatum(relid),
6868  Int16GetDatum(colnum),
6869  BoolGetDatum(false));
6870  vardata.freefunc = ReleaseSysCache;
6871  }
6872  }
6873 
6874  if (HeapTupleIsValid(vardata.statsTuple))
6875  {
6876  Oid sortop;
6877  float4 *numbers;
6878  int nnumbers;
6879 
6880  sortop = get_opfamily_member(index->opfamily[0],
6881  index->opcintype[0],
6882  index->opcintype[0],
6884  if (OidIsValid(sortop) &&
6887  sortop,
6888  NULL,
6889  NULL, NULL,
6890  &numbers, &nnumbers))
6891  {
6892  double varCorrelation;
6893 
6894  Assert(nnumbers == 1);
6895  varCorrelation = numbers[0];
6896 
6897  if (index->reverse_sort[0])
6898  varCorrelation = -varCorrelation;
6899 
6900  if (index->ncolumns > 1)
6901  costs.indexCorrelation = varCorrelation * 0.75;
6902  else
6903  costs.indexCorrelation = varCorrelation;
6904 
6905  free_attstatsslot(InvalidOid, NULL, 0, numbers, nnumbers);
6906  }
6907  }
6908 
6909  ReleaseVariableStats(vardata);
6910 
6911  *indexStartupCost = costs.indexStartupCost;
6912  *indexTotalCost = costs.indexTotalCost;
6913  *indexSelectivity = costs.indexSelectivity;
6914  *indexCorrelation = costs.indexCorrelation;
6915  *indexPages = costs.numIndexPages;
6916 }
Selectivity indexSelectivity
Definition: selfuncs.h:130
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:555
IndexOptInfo * indexinfo
Definition: relation.h:995
HeapTuple statsTuple
Definition: selfuncs.h:71
double tuples
Definition: relation.h:534
static List * add_predicate_to_quals(IndexOptInfo *index, List *indexQuals)
Definition: selfuncs.c:6600
#define Int16GetDatum(X)
Definition: postgres.h:457
#define MemSet(start, val, len)
Definition: c.h:857
double Selectivity
Definition: nodes.h:627
bool get_attstatsslot(HeapTuple statstuple, Oid atttype, int32 atttypmod, int reqkind, Oid reqop, Oid *actualop, Datum **values, int *nvalues, float4 **numbers, int *nnumbers)
Definition: lsyscache.c:2854
double tuples
Definition: relation.h:599
unsigned int Oid
Definition: postgres_ext.h:31
int tree_height
Definition: relation.h:600
#define OidIsValid(objectId)
Definition: c.h:538
RestrictInfo * rinfo
Definition: selfuncs.h:105
List * deconstruct_indexquals(IndexPath *path)
Definition: selfuncs.c:6232
bool unique
Definition: relation.h:626
Definition: type.h:90
int estimate_array_length(Node *arrayexpr)
Definition: selfuncs.c:2083
RelOptInfo * rel
Definition: relation.h:595
#define planner_rt_fetch(rti, root)
Definition: relation.h:324
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
double num_sa_scans
Definition: selfuncs.h:137
#define STATISTIC_KIND_CORRELATION
Definition: pg_statistic.h:233
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
double cpu_operator_cost
Definition: costsize.c:108
Cost indexTotalCost
Definition: selfuncs.h:129
get_relation_stats_hook_type get_relation_stats_hook
Definition: selfuncs.c:151
double rint(double x)
Definition: rint.c:22
int ncolumns
Definition: relation.h:603
Index relid
Definition: relation.h:522
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1679
float float4
Definition: c.h:380
double indexCorrelation
Definition: selfuncs.h:131
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
NullTestType nulltesttype
Definition: primnodes.h:1179
#define BoolGetDatum(X)
Definition: postgres.h:408
#define InvalidOid
Definition: postgres_ext.h:36
double numIndexTuples
Definition: selfuncs.h:135
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
get_index_stats_hook_type get_index_stats_hook
Definition: selfuncs.c:152
Oid * opcintype
Definition: relation.h:607
Cost indexStartupCost
Definition: selfuncs.h:128
Oid * opfamily
Definition: relation.h:606
RTEKind rtekind
Definition: parsenodes.h:916
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Definition: lsyscache.c:80
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:80
Node * other_operand
Definition: selfuncs.h:109
#define SearchSysCache3(cacheId, key1, key2, key3)
Definition: syscache.h:156
int * indexkeys
Definition: relation.h:604
#define elog
Definition: elog.h:219
Oid indexoid
Definition: relation.h:593
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:92
void(* freefunc)(HeapTuple tuple)
Definition: selfuncs.h:73
bool * reverse_sort
Definition: relation.h:609
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
void free_attstatsslot(Oid atttype, Datum *values, int nvalues, float4 *numbers, int nnumbers)
Definition: lsyscache.c:2978
#define BTEqualStrategyNumber
Definition: stratnum.h:31
double Cost
Definition: nodes.h:628
void genericcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, List *qinfos, GenericCosts *costs)
Definition: selfuncs.c:6381
double numIndexPages
Definition: selfuncs.h:134
static bool byte_increment ( unsigned char *  ptr,
int  len 
)
static

Definition at line 5956 of file selfuncs.c.

Referenced by make_greater_string().

5957 {
5958  if (*ptr >= 255)
5959  return false;
5960  (*ptr)++;
5961  return true;
5962 }
static void convert_bytea_to_scalar ( Datum  value,
double *  scaledvalue,
Datum  lobound,
double *  scaledlobound,
Datum  hibound,
double *  scaledhibound 
)
static

Definition at line 4277 of file selfuncs.c.

References convert_one_bytea_to_scalar(), DatumGetPointer, i, Min, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by convert_to_scalar().

4283 {
4284  int rangelo,
4285  rangehi,
4286  valuelen = VARSIZE(DatumGetPointer(value)) - VARHDRSZ,
4287  loboundlen = VARSIZE(DatumGetPointer(lobound)) - VARHDRSZ,
4288  hiboundlen = VARSIZE(DatumGetPointer(hibound)) - VARHDRSZ,
4289  i,
4290  minlen;
4291  unsigned char *valstr = (unsigned char *) VARDATA(DatumGetPointer(value)),
4292  *lostr = (unsigned char *) VARDATA(DatumGetPointer(lobound)),
4293  *histr = (unsigned char *) VARDATA(DatumGetPointer(hibound));
4294 
4295  /*
4296  * Assume bytea data is uniformly distributed across all byte values.
4297  */
4298  rangelo = 0;
4299  rangehi = 255;
4300 
4301  /*
4302  * Now strip any common prefix of the three strings.
4303  */
4304  minlen = Min(Min(valuelen, loboundlen), hiboundlen);
4305  for (i = 0; i < minlen; i++)
4306  {
4307  if (*lostr != *histr || *lostr != *valstr)
4308  break;
4309  lostr++, histr++, valstr++;
4310  loboundlen--, hiboundlen--, valuelen--;
4311  }
4312 
4313  /*
4314  * Now we can do the conversions.
4315  */
4316  *scaledvalue = convert_one_bytea_to_scalar(valstr, valuelen, rangelo, rangehi);
4317  *scaledlobound = convert_one_bytea_to_scalar(lostr, loboundlen, rangelo, rangehi);
4318  *scaledhibound = convert_one_bytea_to_scalar(histr, hiboundlen, rangelo, rangehi);
4319 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:445
#define Min(x, y)
Definition: c.h:806
static double convert_one_bytea_to_scalar(unsigned char *value, int valuelen, int rangelo, int rangehi)
Definition: selfuncs.c:4322
static struct @114 value
#define DatumGetPointer(X)
Definition: postgres.h:555
int i
static double convert_numeric_to_scalar ( Datum  value,
Oid  typid 
)
static

Definition at line 3985 of file selfuncs.c.

References BOOLOID, DatumGetBool, DatumGetFloat4, DatumGetFloat8, DatumGetInt16, DatumGetInt32, DatumGetInt64, DatumGetObjectId, DirectFunctionCall1, elog, ERROR, FLOAT4OID, FLOAT8OID, INT2OID, INT4OID, INT8OID, numeric_float8_no_overflow(), NUMERICOID, OIDOID, REGCLASSOID, REGCONFIGOID, REGDICTIONARYOID, REGNAMESPACEOID, REGOPERATOROID, REGOPEROID, REGPROCEDUREOID, REGPROCOID, REGROLEOID, and REGTYPEOID.

Referenced by convert_to_scalar().

3986 {
3987  switch (typid)
3988  {
3989  case BOOLOID:
3990  return (double) DatumGetBool(value);
3991  case INT2OID:
3992  return (double) DatumGetInt16(value);
3993  case INT4OID:
3994  return (double) DatumGetInt32(value);
3995  case INT8OID:
3996  return (double) DatumGetInt64(value);
3997  case FLOAT4OID:
3998  return (double) DatumGetFloat4(value);
3999  case FLOAT8OID:
4000  return (double) DatumGetFloat8(value);
4001  case NUMERICOID:
4002  /* Note: out-of-range values will be clamped to +-HUGE_VAL */
4003  return (double)
4005  value));
4006  case OIDOID:
4007  case REGPROCOID:
4008  case REGPROCEDUREOID:
4009  case REGOPEROID:
4010  case REGOPERATOROID:
4011  case REGCLASSOID:
4012  case REGTYPEOID:
4013  case REGCONFIGOID:
4014  case REGDICTIONARYOID:
4015  case REGROLEOID:
4016  case REGNAMESPACEOID:
4017  /* we can treat OIDs as integers... */
4018  return (double) DatumGetObjectId(value);
4019  }
4020 
4021  /*
4022  * Can't get here unless someone tries to use scalarltsel/scalargtsel on
4023  * an operator with one numeric and one non-numeric operand.
4024  */
4025  elog(ERROR, "unsupported type: %u", typid);
4026  return 0;
4027 }
#define REGCLASSOID
Definition: pg_type.h:573
#define DatumGetInt32(X)
Definition: postgres.h:478
#define REGROLEOID
Definition: pg_type.h:581
#define OIDOID
Definition: pg_type.h:328
#define NUMERICOID
Definition: pg_type.h:550
#define DatumGetObjectId(X)
Definition: postgres.h:506
#define INT4OID
Definition: pg_type.h:316
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
#define REGTYPEOID
Definition: pg_type.h:577
#define REGOPEROID
Definition: pg_type.h:565
static struct @114 value
#define ERROR
Definition: elog.h:43
#define DatumGetInt64(X)
Definition: postgres.h:613
#define INT2OID
Definition: pg_type.h:308
#define DatumGetInt16(X)
Definition: postgres.h:450
#define DatumGetBool(X)
Definition: postgres.h:399
#define REGDICTIONARYOID
Definition: pg_type.h:623
#define FLOAT4OID
Definition: pg_type.h:412
#define DatumGetFloat8(X)
Definition: postgres.h:734
#define INT8OID
Definition: pg_type.h:304
#define DatumGetFloat4(X)
Definition: postgres.h:686
Datum numeric_float8_no_overflow(PG_FUNCTION_ARGS)
Definition: numeric.c:3118
#define FLOAT8OID
Definition: pg_type.h:415
#define BOOLOID
Definition: pg_type.h:288
#define REGCONFIGOID
Definition: pg_type.h:620
#define elog
Definition: elog.h:219
#define REGPROCEDUREOID
Definition: pg_type.h:561
#define REGNAMESPACEOID
Definition: pg_type.h:585
#define REGOPERATOROID
Definition: pg_type.h:569
#define REGPROCOID
Definition: pg_type.h:320
static double convert_one_bytea_to_scalar ( unsigned char *  value,
int  valuelen,
int  rangelo,
int  rangehi 
)
static

Definition at line 4322 of file selfuncs.c.

Referenced by convert_bytea_to_scalar().

4324 {
4325  double num,
4326  denom,
4327  base;
4328 
4329  if (valuelen <= 0)
4330  return 0.0; /* empty string has scalar value 0 */
4331 
4332  /*
4333  * Since base is 256, need not consider more than about 10 chars (even
4334  * this many seems like overkill)
4335  */
4336  if (valuelen > 10)
4337  valuelen = 10;
4338 
4339  /* Convert initial characters to fraction */
4340  base = rangehi - rangelo + 1;
4341  num = 0.0;
4342  denom = base;
4343  while (valuelen-- > 0)
4344  {
4345  int ch = *value++;
4346 
4347  if (ch < rangelo)
4348  ch = rangelo - 1;
4349  else if (ch > rangehi)
4350  ch = rangehi + 1;
4351  num += ((double) (ch - rangelo)) / denom;
4352  denom *= base;
4353  }
4354 
4355  return num;
4356 }
static struct @114 value
static double convert_one_string_to_scalar ( char *  value,
int  rangelo,
int  rangehi 
)
static

Definition at line 4130 of file selfuncs.c.

Referenced by convert_string_to_scalar().

4131 {
4132  int slen = strlen(value);
4133  double num,
4134  denom,
4135  base;
4136 
4137  if (slen <= 0)
4138  return 0.0; /* empty string has scalar value 0 */
4139 
4140  /*
4141  * There seems little point in considering more than a dozen bytes from
4142  * the string. Since base is at least 10, that will give us nominal
4143  * resolution of at least 12 decimal digits, which is surely far more
4144  * precision than this estimation technique has got anyway (especially in
4145  * non-C locales). Also, even with the maximum possible base of 256, this
4146  * ensures denom cannot grow larger than 256^13 = 2.03e31, which will not
4147  * overflow on any known machine.
4148  */
4149  if (slen > 12)
4150  slen = 12;
4151 
4152  /* Convert initial characters to fraction */
4153  base = rangehi - rangelo + 1;
4154  num = 0.0;
4155  denom = base;
4156  while (slen-- > 0)
4157  {
4158  int ch = (unsigned char) *value++;
4159 
4160  if (ch < rangelo)
4161  ch = rangelo - 1;
4162  else if (ch > rangehi)
4163  ch = rangehi + 1;
4164  num += ((double) (ch - rangelo)) / denom;
4165  denom *= base;
4166  }
4167 
4168  return num;
4169 }
static struct @114 value
static char * convert_string_datum ( Datum  value,
Oid  typid 
)
static

Definition at line 4178 of file selfuncs.c.

References Assert, BPCHAROID, CHAROID, DatumGetChar, DatumGetPointer, DEFAULT_COLLATION_OID, elog, ERROR, lc_collate_is_c(), NAMEOID, NameStr, NULL, palloc(), pfree(), PG_USED_FOR_ASSERTS_ONLY, pstrdup(), TextDatumGetCString, TEXTOID, val, and VARCHAROID.

Referenced by convert_to_scalar().

4179 {
4180  char *val;
4181 
4182  switch (typid)
4183  {
4184  case CHAROID:
4185  val = (char *) palloc(2);
4186  val[0] = DatumGetChar(value);
4187  val[1] = '\0';
4188  break;
4189  case BPCHAROID:
4190  case VARCHAROID:
4191  case TEXTOID:
4192  val = TextDatumGetCString(value);
4193  break;
4194  case NAMEOID:
4195  {
4197 
4198  val = pstrdup(NameStr(*nm));
4199  break;
4200  }
4201  default:
4202 
4203  /*
4204  * Can't get here unless someone tries to use scalarltsel on an
4205  * operator with one string and one non-string operand.
4206  */
4207  elog(ERROR, "unsupported type: %u", typid);
4208  return NULL;
4209  }
4210 
4212  {
4213  char *xfrmstr;
4214  size_t xfrmlen;
4215  size_t xfrmlen2 PG_USED_FOR_ASSERTS_ONLY;
4216 
4217  /*
4218  * XXX: We could guess at a suitable output buffer size and only call
4219  * strxfrm twice if our guess is too small.
4220  *
4221  * XXX: strxfrm doesn't support UTF-8 encoding on Win32, it can return
4222  * bogus data or set an error. This is not really a problem unless it
4223  * crashes since it will only give an estimation error and nothing
4224  * fatal.
4225  */
4226 #if _MSC_VER == 1400 /* VS.Net 2005 */
4227 
4228  /*
4229  *
4230  * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?
4231  * FeedbackID=99694 */
4232  {
4233  char x[1];
4234 
4235  xfrmlen = strxfrm(x, val, 0);
4236  }
4237 #else
4238  xfrmlen = strxfrm(NULL, val, 0);
4239 #endif
4240 #ifdef WIN32
4241 
4242  /*
4243  * On Windows, strxfrm returns INT_MAX when an error occurs. Instead
4244  * of trying to allocate this much memory (and fail), just return the
4245  * original string unmodified as if we were in the C locale.
4246  */
4247  if (xfrmlen == INT_MAX)
4248  return val;
4249 #endif
4250  xfrmstr = (char *) palloc(xfrmlen + 1);
4251  xfrmlen2 = strxfrm(xfrmstr, val, xfrmlen + 1);
4252 
4253  /*
4254  * Some systems (e.g., glibc) can return a smaller value from the
4255  * second call than the first; thus the Assert must be <= not ==.
4256  */
4257  Assert(xfrmlen2 <= xfrmlen);
4258  pfree(val);
4259  val = xfrmstr;
4260  }
4261 
4262  return val;
4263 }
#define BPCHAROID
Definition: pg_type.h:500
#define NAMEOID
Definition: pg_type.h:300
#define TEXTOID
Definition: pg_type.h:324
char * pstrdup(const char *in)
Definition: mcxt.c:1077
static struct @114 value
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1128
Definition: c.h:493
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
#define VARCHAROID
Definition: pg_type.h:503
#define TextDatumGetCString(d)
Definition: builtins.h:92
#define DatumGetChar(X)
Definition: postgres.h:415
#define CHAROID
Definition: pg_type.h:296
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define DatumGetPointer(X)
Definition: postgres.h:555
void * palloc(Size size)
Definition: mcxt.c:849
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:990
long val
Definition: informix.c:689
static void convert_string_to_scalar ( char *  value,
double *  scaledvalue,
char *  lobound,
double *  scaledlobound,
char *  hibound,
double *  scaledhibound 
)
static

Definition at line 4050 of file selfuncs.c.

References convert_one_string_to_scalar().

Referenced by convert_to_scalar().

4056 {
4057  int rangelo,
4058  rangehi;
4059  char *sptr;
4060 
4061  rangelo = rangehi = (unsigned char) hibound[0];
4062  for (sptr = lobound; *sptr; sptr++)
4063  {
4064  if (rangelo > (unsigned char) *sptr)
4065  rangelo = (unsigned char) *sptr;
4066  if (rangehi < (unsigned char) *sptr)
4067  rangehi = (unsigned char) *sptr;
4068  }
4069  for (sptr = hibound; *sptr; sptr++)
4070  {
4071  if (rangelo > (unsigned char) *sptr)
4072  rangelo = (unsigned char) *sptr;
4073  if (rangehi < (unsigned char) *sptr)
4074  rangehi = (unsigned char) *sptr;
4075  }
4076  /* If range includes any upper-case ASCII chars, make it include all */
4077  if (rangelo <= 'Z' && rangehi >= 'A')
4078  {
4079  if (rangelo > 'A')
4080  rangelo = 'A';
4081  if (rangehi < 'Z')
4082  rangehi = 'Z';
4083  }
4084  /* Ditto lower-case */
4085  if (rangelo <= 'z' && rangehi >= 'a')
4086  {
4087  if (rangelo > 'a')
4088  rangelo = 'a';
4089  if (rangehi < 'z')
4090  rangehi = 'z';
4091  }
4092  /* Ditto digits */
4093  if (rangelo <= '9' && rangehi >= '0')
4094  {
4095  if (rangelo > '0')
4096  rangelo = '0';
4097  if (rangehi < '9')
4098  rangehi = '9';
4099  }
4100 
4101  /*
4102  * If range includes less than 10 chars, assume we have not got enough
4103  * data, and make it include regular ASCII set.
4104  */
4105  if (rangehi - rangelo < 9)
4106  {
4107  rangelo = ' ';
4108  rangehi = 127;
4109  }
4110 
4111  /*
4112  * Now strip any common prefix of the three strings.
4113  */
4114  while (*lobound)
4115  {
4116  if (*lobound != *hibound || *lobound != *value)
4117  break;
4118  lobound++, hibound++, value++;
4119  }
4120 
4121  /*
4122  * Now we can do the conversions.
4123  */
4124  *scaledvalue = convert_one_string_to_scalar(value, rangelo, rangehi);
4125  *scaledlobound = convert_one_string_to_scalar(lobound, rangelo, rangehi);
4126  *scaledhibound = convert_one_string_to_scalar(hibound, rangelo, rangehi);
4127 }
static struct @114 value
static double convert_one_string_to_scalar(char *value, int rangelo, int rangehi)
Definition: selfuncs.c:4130
static double convert_timevalue_to_scalar ( Datum  value,
Oid  typid 
)
static

Definition at line 4362 of file selfuncs.c.

References abstime_timestamp(), ABSTIMEOID, TimeIntervalData::data, date2timestamp_no_overflow(), DATEOID, DatumGetDateADT, DatumGetIntervalP, DatumGetRelativeTime, DatumGetTimeADT, DatumGetTimeInterval, DatumGetTimestamp, DatumGetTimestampTz, DatumGetTimeTzADTP, Interval::day, DAYS_PER_YEAR, DirectFunctionCall1, elog, ERROR, INTERVALOID, Interval::month, MONTHS_PER_YEAR, RELTIMEOID, TimeIntervalData::status, TimeTzADT::time, Interval::time, TIMEOID, TIMESTAMPOID, TIMESTAMPTZOID, TIMETZOID, TINTERVALOID, USECS_PER_DAY, and TimeTzADT::zone.

Referenced by convert_to_scalar().

4363 {
4364  switch (typid)
4365  {
4366  case TIMESTAMPOID:
4367  return DatumGetTimestamp(value);
4368  case TIMESTAMPTZOID:
4369  return DatumGetTimestampTz(value);
4370  case ABSTIMEOID:
4372  value));
4373  case DATEOID:
4375  case INTERVALOID:
4376  {
4378 
4379  /*
4380  * Convert the month part of Interval to days using assumed
4381  * average month length of 365.25/12.0 days. Not too
4382  * accurate, but plenty good enough for our purposes.
4383  */
4384  return interval->time + interval->day * (double) USECS_PER_DAY +
4385  interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * USECS_PER_DAY);
4386  }
4387  case RELTIMEOID:
4388  return (DatumGetRelativeTime(value) * 1000000.0);
4389  case TINTERVALOID:
4390  {
4392 
4393  if (tinterval->status != 0)
4394  return ((tinterval->data[1] - tinterval->data[0]) * 1000000.0);
4395  return 0; /* for lack of a better idea */
4396  }
4397  case TIMEOID:
4398  return DatumGetTimeADT(value);
4399  case TIMETZOID:
4400  {
4401  TimeTzADT *timetz = DatumGetTimeTzADTP(value);
4402 
4403  /* use GMT-equivalent time */
4404  return (double) (timetz->time + (timetz->zone * 1000000.0));
4405  }
4406  }
4407 
4408  /*
4409  * Can't get here unless someone tries to use scalarltsel/scalargtsel on
4410  * an operator with one timevalue and one non-timevalue operand.
4411  */
4412  elog(ERROR, "unsupported type: %u", typid);
4413  return 0;
4414 }
#define TIMESTAMPTZOID
Definition: pg_type.h:521
#define TIMEOID
Definition: pg_type.h:510
#define DATEOID
Definition: pg_type.h:507
#define DatumGetDateADT(X)
Definition: date.h:52
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
TimeADT time
Definition: date.h:28
#define DatumGetTimeTzADTP(X)
Definition: date.h:54
#define TINTERVALOID
Definition: pg_type.h:424
double date2timestamp_no_overflow(DateADT dateVal)
Definition: date.c:658
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
int32 day
Definition: timestamp.h:47
static struct @114 value
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define DAYS_PER_YEAR
Definition: timestamp.h:68
#define TIMESTAMPOID
Definition: pg_type.h:515
#define ERROR
Definition: elog.h:43
int32 zone
Definition: date.h:29
#define DatumGetRelativeTime(X)
Definition: nabstime.h:51
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28
#define INTERVALOID
Definition: pg_type.h:525
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
Datum abstime_timestamp(PG_FUNCTION_ARGS)
Definition: nabstime.c:467
#define DatumGetTimeADT(X)
Definition: date.h:53
AbsoluteTime data[2]
Definition: nabstime.h:42
#define TIMETZOID
Definition: pg_type.h:532
#define DatumGetTimeInterval(X)
Definition: nabstime.h:52
#define elog
Definition: elog.h:219
#define ABSTIMEOID
Definition: pg_type.h:418
Definition: date.h:26
#define DatumGetTimestamp(X)
Definition: timestamp.h:27
#define RELTIMEOID
Definition: pg_type.h:421
static bool convert_to_scalar ( Datum  value,
Oid  valuetypid,
double *  scaledvalue,
Datum  lobound,
Datum  hibound,
Oid  boundstypid,
double *  scaledlobound,
double *  scaledhibound 
)
static

Definition at line 3867 of file selfuncs.c.

References ABSTIMEOID, BOOLOID, BPCHAROID, BYTEAOID, CHAROID, CIDROID, convert_bytea_to_scalar(), convert_network_to_scalar(), convert_numeric_to_scalar(), convert_string_datum(), convert_string_to_scalar(), convert_timevalue_to_scalar(), DATEOID, FLOAT4OID, FLOAT8OID, INETOID, INT2OID, INT4OID, INT8OID, INTERVALOID, MACADDR8OID, MACADDROID, NAMEOID, NUMERICOID, OIDOID, pfree(), REGCLASSOID, REGCONFIGOID, REGDICTIONARYOID, REGNAMESPACEOID, REGOPERATOROID, REGOPEROID, REGPROCEDUREOID, REGPROCOID, REGROLEOID, REGTYPEOID, RELTIMEOID, TEXTOID, TIMEOID, TIMESTAMPOID, TIMESTAMPTZOID, TIMETZOID, TINTERVALOID, and VARCHAROID.

Referenced by ineq_histogram_selectivity().

3870 {
3871  /*
3872  * Both the valuetypid and the boundstypid should exactly match the
3873  * declared input type(s) of the operator we are invoked for, so we just
3874  * error out if either is not recognized.
3875  *
3876  * XXX The histogram we are interpolating between points of could belong
3877  * to a column that's only binary-compatible with the declared type. In
3878  * essence we are assuming that the semantics of binary-compatible types
3879  * are enough alike that we can use a histogram generated with one type's
3880  * operators to estimate selectivity for the other's. This is outright
3881  * wrong in some cases --- in particular signed versus unsigned
3882  * interpretation could trip us up. But it's useful enough in the
3883  * majority of cases that we do it anyway. Should think about more
3884  * rigorous ways to do it.
3885  */
3886  switch (valuetypid)
3887  {
3888  /*
3889  * Built-in numeric types
3890  */
3891  case BOOLOID:
3892  case INT2OID:
3893  case INT4OID:
3894  case INT8OID:
3895  case FLOAT4OID:
3896  case FLOAT8OID:
3897  case NUMERICOID:
3898  case OIDOID:
3899  case REGPROCOID:
3900  case REGPROCEDUREOID:
3901  case REGOPEROID:
3902  case REGOPERATOROID:
3903  case REGCLASSOID:
3904  case REGTYPEOID:
3905  case REGCONFIGOID:
3906  case REGDICTIONARYOID:
3907  case REGROLEOID:
3908  case REGNAMESPACEOID:
3909  *scaledvalue = convert_numeric_to_scalar(value, valuetypid);
3910  *scaledlobound = convert_numeric_to_scalar(lobound, boundstypid);
3911  *scaledhibound = convert_numeric_to_scalar(hibound, boundstypid);
3912  return true;
3913 
3914  /*
3915  * Built-in string types
3916  */
3917  case CHAROID:
3918  case BPCHAROID:
3919  case VARCHAROID:
3920  case TEXTOID:
3921  case NAMEOID:
3922  {
3923  char *valstr = convert_string_datum(value, valuetypid);
3924  char *lostr = convert_string_datum(lobound, boundstypid);
3925  char *histr = convert_string_datum(hibound, boundstypid);
3926 
3927  convert_string_to_scalar(valstr, scaledvalue,
3928  lostr, scaledlobound,
3929  histr, scaledhibound);
3930  pfree(valstr);
3931  pfree(lostr);
3932  pfree(histr);
3933  return true;
3934  }
3935 
3936  /*
3937  * Built-in bytea type
3938  */
3939  case BYTEAOID:
3940  {
3941  convert_bytea_to_scalar(value, scaledvalue,
3942  lobound, scaledlobound,
3943  hibound, scaledhibound);
3944  return true;
3945  }
3946 
3947  /*
3948  * Built-in time types
3949  */
3950  case TIMESTAMPOID:
3951  case TIMESTAMPTZOID:
3952  case ABSTIMEOID:
3953  case DATEOID:
3954  case INTERVALOID:
3955  case RELTIMEOID:
3956  case TINTERVALOID:
3957  case TIMEOID:
3958  case TIMETZOID:
3959  *scaledvalue = convert_timevalue_to_scalar(value, valuetypid);
3960  *scaledlobound = convert_timevalue_to_scalar(lobound, boundstypid);
3961  *scaledhibound = convert_timevalue_to_scalar(hibound, boundstypid);
3962  return true;
3963 
3964  /*
3965  * Built-in network types
3966  */
3967  case INETOID:
3968  case CIDROID:
3969  case MACADDROID:
3970  case MACADDR8OID:
3971  *scaledvalue = convert_network_to_scalar(value, valuetypid);
3972  *scaledlobound = convert_network_to_scalar(lobound, boundstypid);
3973  *scaledhibound = convert_network_to_scalar(hibound, boundstypid);
3974  return true;
3975  }
3976  /* Don't know how to convert */
3977  *scaledvalue = *scaledlobound = *scaledhibound = 0;
3978  return false;
3979 }
#define CIDROID
Definition: pg_type.h:447
#define TIMESTAMPTZOID
Definition: pg_type.h:521
#define TIMEOID
Definition: pg_type.h:510
#define REGCLASSOID
Definition: pg_type.h:573
#define BPCHAROID
Definition: pg_type.h:500
#define DATEOID
Definition: pg_type.h:507
#define NAMEOID
Definition: pg_type.h:300
#define REGROLEOID
Definition: pg_type.h:581
#define OIDOID
Definition: pg_type.h:328
#define TEXTOID
Definition: pg_type.h:324
#define INETOID
Definition: pg_type.h:444
#define NUMERICOID
Definition: pg_type.h:550
static double convert_numeric_to_scalar(Datum value, Oid typid)
Definition: selfuncs.c:3985
#define INT4OID
Definition: pg_type.h:316
#define TINTERVALOID
Definition: pg_type.h:424
#define REGTYPEOID
Definition: pg_type.h:577
#define REGOPEROID
Definition: pg_type.h:565
static struct @114 value
void pfree(void *pointer)
Definition: mcxt.c:950
#define TIMESTAMPOID
Definition: pg_type.h:515
double convert_network_to_scalar(Datum value, Oid typid)
Definition: network.c:897
#define INT2OID
Definition: pg_type.h:308
#define INTERVALOID
Definition: pg_type.h:525
#define REGDICTIONARYOID
Definition: pg_type.h:623
#define VARCHAROID
Definition: pg_type.h:503
#define FLOAT4OID
Definition: pg_type.h:412
#define CHAROID
Definition: pg_type.h:296
#define INT8OID
Definition: pg_type.h:304
static char * convert_string_datum(Datum value, Oid typid)
Definition: selfuncs.c:4178
#define TIMETZOID
Definition: pg_type.h:532
#define FLOAT8OID
Definition: pg_type.h:415
#define BOOLOID
Definition: pg_type.h:288
static double convert_timevalue_to_scalar(Datum value, Oid typid)
Definition: selfuncs.c:4362
#define BYTEAOID
Definition: pg_type.h:292
#define REGCONFIGOID
Definition: pg_type.h:620
#define MACADDR8OID
Definition: pg_type.h:450
static void convert_bytea_to_scalar(Datum value, double *scaledvalue, Datum lobound, double *scaledlobound, Datum hibound, double *scaledhibound)
Definition: selfuncs.c:4277
#define MACADDROID
Definition: pg_type.h:441
#define REGPROCEDUREOID
Definition: pg_type.h:561
#define ABSTIMEOID
Definition: pg_type.h:418
#define REGNAMESPACEOID
Definition: pg_type.h:585
static void convert_string_to_scalar(char *value, double *scaledvalue, char *lobound, double *scaledlobound, char *hibound, double *scaledhibound)
Definition: selfuncs.c:4050
#define REGOPERATOROID
Definition: pg_type.h:569
#define REGPROCOID
Definition: pg_type.h:320
#define RELTIMEOID
Definition: pg_type.h:421
List* deconstruct_indexquals ( IndexPath path)

Definition at line 6232 of file selfuncs.c.

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

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

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

Definition at line 2188 of file selfuncs.c.

References generate_unaccent_rules::args, CLAMP_PROBABILITY, elog, eqjoinsel_inner(), eqjoinsel_semi(), ERROR, find_join_input_rel(), get_commutator(), get_join_variables(), JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_SEMI, SpecialJoinInfo::jointype, SpecialJoinInfo::min_righthand, PG_GETARG_INT16, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_FLOAT8, and ReleaseVariableStats.

Referenced by neqjoinsel().

2189 {
2190  PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2191  Oid operator = PG_GETARG_OID(1);
2192  List *args = (List *) PG_GETARG_POINTER(2);
2193 
2194 #ifdef NOT_USED
2195  JoinType jointype = (JoinType) PG_GETARG_INT16(3);
2196 #endif
2198  double selec;
2199  VariableStatData vardata1;
2200  VariableStatData vardata2;
2201  bool join_is_reversed;
2202  RelOptInfo *inner_rel;
2203 
2204  get_join_variables(root, args, sjinfo,
2205  &vardata1, &vardata2, &join_is_reversed);
2206 
2207  switch (sjinfo->jointype)
2208  {
2209  case JOIN_INNER:
2210  case JOIN_LEFT:
2211  case JOIN_FULL:
2212  selec = eqjoinsel_inner(operator, &vardata1, &vardata2);
2213  break;
2214  case JOIN_SEMI:
2215  case JOIN_ANTI:
2216 
2217  /*
2218  * Look up the join's inner relation. min_righthand is sufficient
2219  * information because neither SEMI nor ANTI joins permit any
2220  * reassociation into or out of their RHS, so the righthand will
2221  * always be exactly that set of rels.
2222  */
2223  inner_rel = find_join_input_rel(root, sjinfo->min_righthand);
2224 
2225  if (!join_is_reversed)
2226  selec = eqjoinsel_semi(operator, &vardata1, &vardata2,
2227  inner_rel);
2228  else
2229  selec = eqjoinsel_semi(get_commutator(operator),
2230  &vardata2, &vardata1,
2231  inner_rel);
2232  break;
2233  default:
2234  /* other values not expected here */
2235  elog(ERROR, "unrecognized join type: %d",
2236  (int) sjinfo->jointype);
2237  selec = 0; /* keep compiler quiet */
2238  break;
2239  }
2240 
2241  ReleaseVariableStats(vardata1);
2242  ReleaseVariableStats(vardata2);
2243 
2244  CLAMP_PROBABILITY(selec);
2245 
2246  PG_RETURN_FLOAT8((float8) selec);
2247 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1281
Relids min_righthand
Definition: relation.h:1850
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
JoinType
Definition: nodes.h:661
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
void get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo, VariableStatData *vardata1, VariableStatData *vardata2, bool *join_is_reversed)
Definition: selfuncs.c:4501
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
static double eqjoinsel_semi(Oid operator, VariableStatData *vardata1, VariableStatData *vardata2, RelOptInfo *inner_rel)
Definition: selfuncs.c:2482
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
static RelOptInfo * find_join_input_rel(PlannerInfo *root, Relids relids)
Definition: selfuncs.c:5384
JoinType jointype
Definition: relation.h:1853
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:80
static double eqjoinsel_inner(Oid operator, VariableStatData *vardata1, VariableStatData *vardata2)
Definition: selfuncs.c:2256
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
static double eqjoinsel_inner ( Oid  operator,
VariableStatData vardata1,
VariableStatData vardata2 
)
static

Definition at line 2256 of file selfuncs.c.

References VariableStatData::atttype, VariableStatData::atttypmod, CLAMP_PROBABILITY, DatumGetBool, DEFAULT_COLLATION_OID, fmgr_info(), free_attstatsslot(), FunctionCall2Coll(), get_attstatsslot(), get_opcode(), get_variable_numdistinct(), GETSTRUCT, HeapTupleIsValid, i, InvalidOid, NULL, palloc0(), pfree(), STATISTIC_KIND_MCV, and VariableStatData::statsTuple.

Referenced by eqjoinsel().

2258 {
2259  double selec;
2260  double nd1;
2261  double nd2;
2262  bool isdefault1;
2263  bool isdefault2;
2264  Form_pg_statistic stats1 = NULL;
2265  Form_pg_statistic stats2 = NULL;
2266  bool have_mcvs1 = false;
2267  Datum *values1 = NULL;
2268  int nvalues1 = 0;
2269  float4 *numbers1 = NULL;
2270  int nnumbers1 = 0;
2271  bool have_mcvs2 = false;
2272  Datum *values2 = NULL;
2273  int nvalues2 = 0;
2274  float4 *numbers2 = NULL;
2275  int nnumbers2 = 0;
2276 
2277  nd1 = get_variable_numdistinct(vardata1, &isdefault1);
2278  nd2 = get_variable_numdistinct(vardata2, &isdefault2);
2279 
2280  if (HeapTupleIsValid(vardata1->statsTuple))
2281  {
2282  stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
2283  have_mcvs1 = get_attstatsslot(vardata1->statsTuple,
2284  vardata1->atttype,
2285  vardata1->atttypmod,
2287  InvalidOid,
2288  NULL,
2289  &values1, &nvalues1,
2290  &numbers1, &nnumbers1);
2291  }
2292 
2293  if (HeapTupleIsValid(vardata2->statsTuple))
2294  {
2295  stats2 = (Form_pg_statistic) GETSTRUCT(vardata2->statsTuple);
2296  have_mcvs2 = get_attstatsslot(vardata2->statsTuple,
2297  vardata2->atttype,
2298  vardata2->atttypmod,
2300  InvalidOid,
2301  NULL,
2302  &values2, &nvalues2,
2303  &numbers2, &nnumbers2);
2304  }
2305 
2306  if (have_mcvs1 && have_mcvs2)
2307  {
2308  /*
2309  * We have most-common-value lists for both relations. Run through
2310  * the lists to see which MCVs actually join to each other with the
2311  * given operator. This allows us to determine the exact join
2312  * selectivity for the portion of the relations represented by the MCV
2313  * lists. We still have to estimate for the remaining population, but
2314  * in a skewed distribution this gives us a big leg up in accuracy.
2315  * For motivation see the analysis in Y. Ioannidis and S.
2316  * Christodoulakis, "On the propagation of errors in the size of join
2317  * results", Technical Report 1018, Computer Science Dept., University
2318  * of Wisconsin, Madison, March 1991 (available from ftp.cs.wisc.edu).
2319  */
2320  FmgrInfo eqproc;
2321  bool *hasmatch1;
2322  bool *hasmatch2;
2323  double nullfrac1 = stats1->stanullfrac;
2324  double nullfrac2 = stats2->stanullfrac;
2325  double matchprodfreq,
2326  matchfreq1,
2327  matchfreq2,
2328  unmatchfreq1,
2329  unmatchfreq2,
2330  otherfreq1,
2331  otherfreq2,
2332  totalsel1,
2333  totalsel2;
2334  int i,
2335  nmatches;
2336 
2337  fmgr_info(get_opcode(operator), &eqproc);
2338  hasmatch1 = (bool *) palloc0(nvalues1 * sizeof(bool));
2339  hasmatch2 = (bool *) palloc0(nvalues2 * sizeof(bool));
2340 
2341  /*
2342  * Note we assume that each MCV will match at most one member of the
2343  * other MCV list. If the operator isn't really equality, there could
2344  * be multiple matches --- but we don't look for them, both for speed
2345  * and because the math wouldn't add up...
2346  */
2347  matchprodfreq = 0.0;
2348  nmatches = 0;
2349  for (i = 0; i < nvalues1; i++)
2350  {
2351  int j;
2352 
2353  for (j = 0; j < nvalues2; j++)
2354  {
2355  if (hasmatch2[j])
2356  continue;
2357  if (DatumGetBool(FunctionCall2Coll(&eqproc,
2359  values1[i],
2360  values2[j])))
2361  {
2362  hasmatch1[i] = hasmatch2[j] = true;
2363  matchprodfreq += numbers1[i] * numbers2[j];
2364  nmatches++;
2365  break;
2366  }
2367  }
2368  }
2369  CLAMP_PROBABILITY(matchprodfreq);
2370  /* Sum up frequencies of matched and unmatched MCVs */
2371  matchfreq1 = unmatchfreq1 = 0.0;
2372  for (i = 0; i < nvalues1; i++)
2373  {
2374  if (hasmatch1[i])
2375  matchfreq1 += numbers1[i];
2376  else
2377  unmatchfreq1 += numbers1[i];
2378  }
2379  CLAMP_PROBABILITY(matchfreq1);
2380  CLAMP_PROBABILITY(unmatchfreq1);
2381  matchfreq2 = unmatchfreq2 = 0.0;
2382  for (i = 0; i < nvalues2; i++)
2383  {
2384  if (hasmatch2[i])
2385  matchfreq2 += numbers2[i];
2386  else
2387  unmatchfreq2 += numbers2[i];
2388  }
2389  CLAMP_PROBABILITY(matchfreq2);
2390  CLAMP_PROBABILITY(unmatchfreq2);
2391  pfree(hasmatch1);
2392  pfree(hasmatch2);
2393 
2394  /*
2395  * Compute total frequency of non-null values that are not in the MCV
2396  * lists.
2397  */
2398  otherfreq1 = 1.0 - nullfrac1 - matchfreq1 - unmatchfreq1;
2399  otherfreq2 = 1.0 - nullfrac2 - matchfreq2 - unmatchfreq2;
2400  CLAMP_PROBABILITY(otherfreq1);
2401  CLAMP_PROBABILITY(otherfreq2);
2402 
2403  /*
2404  * We can estimate the total selectivity from the point of view of
2405  * relation 1 as: the known selectivity for matched MCVs, plus
2406  * unmatched MCVs that are assumed to match against random members of
2407  * relation 2's non-MCV population, plus non-MCV values that are
2408  * assumed to match against random members of relation 2's unmatched
2409  * MCVs plus non-MCV values.
2410  */
2411  totalsel1 = matchprodfreq;
2412  if (nd2 > nvalues2)
2413  totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - nvalues2);
2414  if (nd2 > nmatches)
2415  totalsel1 += otherfreq1 * (otherfreq2 + unmatchfreq2) /
2416  (nd2 - nmatches);
2417  /* Same estimate from the point of view of relation 2. */
2418  totalsel2 = matchprodfreq;
2419  if (nd1 > nvalues1)
2420  totalsel2 += unmatchfreq2 * otherfreq1 / (nd1 - nvalues1);
2421  if (nd1 > nmatches)
2422  totalsel2 += otherfreq2 * (otherfreq1 + unmatchfreq1) /
2423  (nd1 - nmatches);
2424 
2425  /*
2426  * Use the smaller of the two estimates. This can be justified in
2427  * essentially the same terms as given below for the no-stats case: to
2428  * a first approximation, we are estimating from the point of view of
2429  * the relation with smaller nd.
2430  */
2431  selec = (totalsel1 < totalsel2) ? totalsel1 : totalsel2;
2432  }
2433  else
2434  {
2435  /*
2436  * We do not have MCV lists for both sides. Estimate the join
2437  * selectivity as MIN(1/nd1,1/nd2)*(1-nullfrac1)*(1-nullfrac2). This
2438  * is plausible if we assume that the join operator is strict and the
2439  * non-null values are about equally distributed: a given non-null
2440  * tuple of rel1 will join to either zero or N2*(1-nullfrac2)/nd2 rows
2441  * of rel2, so total join rows are at most
2442  * N1*(1-nullfrac1)*N2*(1-nullfrac2)/nd2 giving a join selectivity of
2443  * not more than (1-nullfrac1)*(1-nullfrac2)/nd2. By the same logic it
2444  * is not more than (1-nullfrac1)*(1-nullfrac2)/nd1, so the expression
2445  * with MIN() is an upper bound. Using the MIN() means we estimate
2446  * from the point of view of the relation with smaller nd (since the
2447  * larger nd is determining the MIN). It is reasonable to assume that
2448  * most tuples in this rel will have join partners, so the bound is
2449  * probably reasonably tight and should be taken as-is.
2450  *
2451  * XXX Can we be smarter if we have an MCV list for just one side? It
2452  * seems that if we assume equal distribution for the other side, we
2453  * end up with the same answer anyway.
2454  */
2455  double nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
2456  double nullfrac2 = stats2 ? stats2->stanullfrac : 0.0;
2457 
2458  selec = (1.0 - nullfrac1) * (1.0 - nullfrac2);
2459  if (nd1 > nd2)
2460  selec /= nd1;
2461  else
2462  selec /= nd2;
2463  }
2464 
2465  if (have_mcvs1)
2466  free_attstatsslot(vardata1->atttype, values1, nvalues1,
2467  numbers1, nnumbers1);
2468  if (have_mcvs2)
2469  free_attstatsslot(vardata2->atttype, values2, nvalues2,
2470  numbers2, nnumbers2);
2471 
2472  return selec;
2473 }
Definition: fmgr.h:56
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
HeapTuple statsTuple
Definition: selfuncs.h:71
bool get_attstatsslot(HeapTuple statstuple, Oid atttype, int32 atttypmod, int reqkind, Oid reqop, Oid *actualop, Datum **values, int *nvalues, float4 **numbers, int *nnumbers)
Definition: lsyscache.c:2854
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
int32 atttypmod
Definition: selfuncs.h:76
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
void pfree(void *pointer)
Definition: mcxt.c:950
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition: selfuncs.c:4904
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
#define DatumGetBool(X)
Definition: postgres.h:399
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:204
float float4
Definition: c.h:380
void * palloc0(Size size)
Definition: mcxt.c:878
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1062
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
int i
void free_attstatsslot(Oid atttype, Datum *values, int nvalues, float4 *numbers, int nnumbers)
Definition: lsyscache.c:2978
static double eqjoinsel_semi ( Oid  operator,
VariableStatData vardata1,
VariableStatData vardata2,
RelOptInfo inner_rel 
)
static

Definition at line 2482 of file selfuncs.c.

References VariableStatData::atttype, VariableStatData::atttypmod, CLAMP_PROBABILITY, DatumGetBool, DEFAULT_COLLATION_OID, fmgr_info(), free_attstatsslot(), FunctionCall2Coll(), get_attstatsslot(), get_opcode(), get_variable_numdistinct(), GETSTRUCT, HeapTupleIsValid, i, InvalidOid, Min, NULL, OidIsValid, palloc0(), pfree(), VariableStatData::rel, RelOptInfo::rows, STATISTIC_KIND_MCV, and VariableStatData::statsTuple.

Referenced by eqjoinsel().

2485 {
2486  double selec;
2487  double nd1;
2488  double nd2;
2489  bool isdefault1;
2490  bool isdefault2;
2491  Form_pg_statistic stats1 = NULL;
2492  bool have_mcvs1 = false;
2493  Datum *values1 = NULL;
2494  int nvalues1 = 0;
2495  float4 *numbers1 = NULL;
2496  int nnumbers1 = 0;
2497  bool have_mcvs2 = false;
2498  Datum *values2 = NULL;
2499  int nvalues2 = 0;
2500  float4 *numbers2 = NULL;
2501  int nnumbers2 = 0;
2502 
2503  nd1 = get_variable_numdistinct(vardata1, &isdefault1);
2504  nd2 = get_variable_numdistinct(vardata2, &isdefault2);
2505 
2506  /*
2507  * We clamp nd2 to be not more than what we estimate the inner relation's
2508  * size to be. This is intuitively somewhat reasonable since obviously
2509  * there can't be more than that many distinct values coming from the
2510  * inner rel. The reason for the asymmetry (ie, that we don't clamp nd1
2511  * likewise) is that this is the only pathway by which restriction clauses
2512  * applied to the inner rel will affect the join result size estimate,
2513  * since set_joinrel_size_estimates will multiply SEMI/ANTI selectivity by
2514  * only the outer rel's size. If we clamped nd1 we'd be double-counting
2515  * the selectivity of outer-rel restrictions.
2516  *
2517  * We can apply this clamping both with respect to the base relation from
2518  * which the join variable comes (if there is just one), and to the
2519  * immediate inner input relation of the current join.
2520  *
2521  * If we clamp, we can treat nd2 as being a non-default estimate; it's not
2522  * great, maybe, but it didn't come out of nowhere either. This is most
2523  * helpful when the inner relation is empty and consequently has no stats.
2524  */
2525  if (vardata2->rel)
2526  {
2527  if (nd2 >= vardata2->rel->rows)
2528  {
2529  nd2 = vardata2->rel->rows;
2530  isdefault2 = false;
2531  }
2532  }
2533  if (nd2 >= inner_rel->rows)
2534  {
2535  nd2 = inner_rel->rows;
2536  isdefault2 = false;
2537  }
2538 
2539  if (HeapTupleIsValid(vardata1->statsTuple))
2540  {
2541  stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
2542  have_mcvs1 = get_attstatsslot(vardata1->statsTuple,
2543  vardata1->atttype,
2544  vardata1->atttypmod,
2546  InvalidOid,
2547  NULL,
2548  &values1, &nvalues1,
2549  &numbers1, &nnumbers1);
2550  }
2551 
2552  if (HeapTupleIsValid(vardata2->statsTuple))
2553  {
2554  have_mcvs2 = get_attstatsslot(vardata2->statsTuple,
2555  vardata2->atttype,
2556  vardata2->atttypmod,
2558  InvalidOid,
2559  NULL,
2560  &values2, &nvalues2,
2561  &numbers2, &nnumbers2);
2562  }
2563 
2564  if (have_mcvs1 && have_mcvs2 && OidIsValid(operator))
2565  {
2566  /*
2567  * We have most-common-value lists for both relations. Run through
2568  * the lists to see which MCVs actually join to each other with the
2569  * given operator. This allows us to determine the exact join
2570  * selectivity for the portion of the relations represented by the MCV
2571  * lists. We still have to estimate for the remaining population, but
2572  * in a skewed distribution this gives us a big leg up in accuracy.
2573  */
2574  FmgrInfo eqproc;
2575  bool *hasmatch1;
2576  bool *hasmatch2;
2577  double nullfrac1 = stats1->stanullfrac;
2578  double matchfreq1,
2579  uncertainfrac,
2580  uncertain;
2581  int i,
2582  nmatches,
2583  clamped_nvalues2;
2584 
2585  /*
2586  * The clamping above could have resulted in nd2 being less than
2587  * nvalues2; in which case, we assume that precisely the nd2 most
2588  * common values in the relation will appear in the join input, and so
2589  * compare to only the first nd2 members of the MCV list. Of course
2590  * this is frequently wrong, but it's the best bet we can make.
2591  */
2592  clamped_nvalues2 = Min(nvalues2, nd2);
2593 
2594  fmgr_info(get_opcode(operator), &eqproc);
2595  hasmatch1 = (bool *) palloc0(nvalues1 * sizeof(bool));
2596  hasmatch2 = (bool *) palloc0(clamped_nvalues2 * sizeof(bool));
2597 
2598  /*
2599  * Note we assume that each MCV will match at most one member of the
2600  * other MCV list. If the operator isn't really equality, there could
2601  * be multiple matches --- but we don't look for them, both for speed
2602  * and because the math wouldn't add up...
2603  */
2604  nmatches = 0;
2605  for (i = 0; i < nvalues1; i++)
2606  {
2607  int j;
2608 
2609  for (j = 0; j < clamped_nvalues2; j++)
2610  {
2611  if (hasmatch2[j])
2612  continue;
2613  if (DatumGetBool(FunctionCall2Coll(&eqproc,
2615  values1[i],
2616  values2[j])))
2617  {
2618  hasmatch1[i] = hasmatch2[j] = true;
2619  nmatches++;
2620  break;
2621  }
2622  }
2623  }
2624  /* Sum up frequencies of matched MCVs */
2625  matchfreq1 = 0.0;
2626  for (i = 0; i < nvalues1; i++)
2627  {
2628  if (hasmatch1[i])
2629  matchfreq1 += numbers1[i];
2630  }
2631  CLAMP_PROBABILITY(matchfreq1);
2632  pfree(hasmatch1);
2633  pfree(hasmatch2);
2634 
2635  /*
2636  * Now we need to estimate the fraction of relation 1 that has at
2637  * least one join partner. We know for certain that the matched MCVs
2638  * do, so that gives us a lower bound, but we're really in the dark
2639  * about everything else. Our crude approach is: if nd1 <= nd2 then
2640  * assume all non-null rel1 rows have join partners, else assume for
2641  * the uncertain rows that a fraction nd2/nd1 have join partners. We
2642  * can discount the known-matched MCVs from the distinct-values counts
2643  * before doing the division.
2644  *
2645  * Crude as the above is, it's completely useless if we don't have
2646  * reliable ndistinct values for both sides. Hence, if either nd1 or
2647  * nd2 is default, punt and assume half of the uncertain rows have
2648  * join partners.
2649  */
2650  if (!isdefault1 && !isdefault2)
2651  {
2652  nd1 -= nmatches;
2653  nd2 -= nmatches;
2654  if (nd1 <= nd2 || nd2 < 0)
2655  uncertainfrac = 1.0;
2656  else
2657  uncertainfrac = nd2 / nd1;
2658  }
2659  else
2660  uncertainfrac = 0.5;
2661  uncertain = 1.0 - matchfreq1 - nullfrac1;
2662  CLAMP_PROBABILITY(uncertain);
2663  selec = matchfreq1 + uncertainfrac * uncertain;
2664  }
2665  else
2666  {
2667  /*
2668  * Without MCV lists for both sides, we can only use the heuristic
2669  * about nd1 vs nd2.
2670  */
2671  double nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
2672 
2673  if (!isdefault1 && !isdefault2)
2674  {
2675  if (nd1 <= nd2 || nd2 < 0)
2676  selec = 1.0 - nullfrac1;
2677  else
2678  selec = (nd2 / nd1) * (1.0 - nullfrac1);
2679  }
2680  else
2681  selec = 0.5 * (1.0 - nullfrac1);
2682  }
2683 
2684  if (have_mcvs1)
2685  free_attstatsslot(vardata1->atttype, values1, nvalues1,
2686  numbers1, nnumbers1);
2687  if (have_mcvs2)
2688  free_attstatsslot(vardata2->atttype, values2, nvalues2,
2689  numbers2, nnumbers2);
2690 
2691  return selec;
2692 }
Definition: fmgr.h:56
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
HeapTuple statsTuple
Definition: selfuncs.h:71
RelOptInfo * rel
Definition: selfuncs.h:70
#define Min(x, y)
Definition: c.h:806
bool get_attstatsslot(HeapTuple statstuple, Oid atttype, int32 atttypmod, int reqkind, Oid reqop, Oid *actualop, Datum **values, int *nvalues, float4 **numbers, int *nnumbers)
Definition: lsyscache.c:2854
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define OidIsValid(objectId)
Definition: c.h:538
int32 atttypmod
Definition: selfuncs.h:76
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
void pfree(void *pointer)
Definition: mcxt.c:950
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition: selfuncs.c:4904
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
#define DatumGetBool(X)
Definition: postgres.h:399
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:204
float float4
Definition: c.h:380
void * palloc0(Size size)
Definition: mcxt.c:878
uintptr_t Datum
Definition: postgres.h:372
double rows
Definition: relation.h:497
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1062
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
int i
void free_attstatsslot(Oid atttype, Datum *values, int nvalues, float4 *numbers, int nnumbers)
Definition: lsyscache.c:2978
Datum eqsel ( PG_FUNCTION_ARGS  )

Definition at line 225 of file selfuncs.c.

References generate_unaccent_rules::args, DEFAULT_EQ_SEL, get_restriction_variable(), IsA, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_FLOAT8, ReleaseVariableStats, var_eq_const(), and var_eq_non_const().

Referenced by neqsel().

226 {
228  Oid operator = PG_GETARG_OID(1);
229  List *args = (List *) PG_GETARG_POINTER(2);
230  int varRelid = PG_GETARG_INT32(3);
231  VariableStatData vardata;
232  Node *other;
233  bool varonleft;
234  double selec;
235 
236  /*
237  * If expression is not variable = something or something = variable, then
238  * punt and return a default estimate.
239  */
240  if (!get_restriction_variable(root, args, varRelid,
241  &vardata, &other, &varonleft))
243 
244  /*
245  * We can do a lot better if the something is a constant. (Note: the
246  * Const might result from estimation rather than being a simple constant
247  * in the query.)
248  */
249  if (IsA(other, Const))
250  selec = var_eq_const(&vardata, operator,
251  ((Const *) other)->constvalue,
252  ((Const *) other)->constisnull,
253  varonleft);
254  else
255  selec = var_eq_non_const(&vardata, operator, other,
256  varonleft);
257 
258  ReleaseVariableStats(vardata);
259 
260  PG_RETURN_FLOAT8((float8) selec);
261 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define IsA(nodeptr, _type_)
Definition: nodes.h:555
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
Definition: selfuncs.c:4441
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
Definition: nodes.h:504
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
double float8
Definition: c.h:381
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
static double var_eq_const(VariableStatData *vardata, Oid operator, Datum constval, bool constisnull, bool varonleft)
Definition: selfuncs.c:269
#define DEFAULT_EQ_SEL
Definition: selfuncs.h:34
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:80
static double var_eq_non_const(VariableStatData *vardata, Oid operator, Node *other, bool varonleft)
Definition: selfuncs.c:411
Definition: pg_list.h:45
int estimate_array_length ( Node arrayexpr)

Definition at line 2083 of file selfuncs.c.

References ARR_DIMS, ARR_NDIM, ArrayGetNItems(), DatumGetArrayTypeP, IsA, list_length(), and strip_array_coercion().

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

2084 {
2085  /* look through any binary-compatible relabeling of arrayexpr */
2086  arrayexpr = strip_array_coercion(arrayexpr);
2087 
2088  if (arrayexpr && IsA(arrayexpr, Const))
2089  {
2090  Datum arraydatum = ((Const *) arrayexpr)->constvalue;
2091  bool arrayisnull = ((Const *) arrayexpr)->constisnull;
2092  ArrayType *arrayval;
2093 
2094  if (arrayisnull)
2095  return 0;
2096  arrayval = DatumGetArrayTypeP(arraydatum);
2097  return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
2098  }
2099  else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
2100  !((ArrayExpr *) arrayexpr)->multidims)
2101  {
2102  return list_length(((ArrayExpr *) arrayexpr)->elements);
2103  }
2104  else
2105  {
2106  /* default guess --- see also scalararraysel */
2107  return 10;
2108  }
2109 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:555
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
#define ARR_DIMS(a)
Definition: array.h:275
uintptr_t Datum
Definition: postgres.h:372
static int list_length(const List *l)
Definition: pg_list.h:89
#define ARR_NDIM(a)
Definition: array.h:271
static Node * strip_array_coercion(Node *node)
Definition: selfuncs.c:1740
#define DatumGetArrayTypeP(X)
Definition: array.h:242
Selectivity estimate_hash_bucketsize ( PlannerInfo root,
Node hashkey,
double  nbuckets 
)

Definition at line 3596 of file selfuncs.c.

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

Referenced by final_cost_hashjoin().

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

Definition at line 3722 of file selfuncs.c.

References Assert, MVNDistinctItem::attrs, bms_add_member(), BMS_EQUAL, bms_intersect(), bms_is_member(), bms_num_members(), bms_subset_compare(), elog, ERROR, i, InvalidOid, IsA, MVNDistinct::items, StatisticExtInfo::keys, StatisticExtInfo::kind, lappend(), lfirst, MVNDistinctItem::ndistinct, NIL, MVNDistinct::nitems, NULL, GroupVarInfo::rel, statext_ndistinct_load(), RelOptInfo::statlist, StatisticExtInfo::statOid, STATS_EXT_NDISTINCT, and GroupVarInfo::var.

Referenced by estimate_num_groups().

3724 {
3725  ListCell *lc;
3726  Bitmapset *attnums = NULL;
3727  int nmatches;
3728  Oid statOid = InvalidOid;
3729  MVNDistinct *stats;
3730  Bitmapset *matched = NULL;
3731 
3732  /* bail out immediately if the table has no extended statistics */
3733  if (!rel->statlist)
3734  return false;
3735 
3736  /* Determine the attnums we're looking for */
3737  foreach(lc, *varinfos)
3738  {
3739  GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc);
3740 
3741  Assert(varinfo->rel == rel);
3742 
3743  if (IsA(varinfo->var, Var))
3744  {
3745  attnums = bms_add_member(attnums,
3746  ((Var *) varinfo->var)->varattno);
3747  }
3748  }
3749 
3750  /* look for the ndistinct statistics matching the most vars */
3751  nmatches = 1; /* we require at least two matches */
3752  foreach(lc, rel->statlist)
3753  {
3754  StatisticExtInfo *info = (StatisticExtInfo *) lfirst(lc);
3755  Bitmapset *shared;
3756 
3757  /* skip statistics of other kinds */
3758  if (info->kind != STATS_EXT_NDISTINCT)
3759  continue;
3760 
3761  /* compute attnums shared by the vars and the statistic */
3762  shared = bms_intersect(info->keys, attnums);
3763 
3764  /*
3765  * Does this statistics matches more columns than the currently
3766  * best statistic? If so, use this one instead.
3767  *
3768  * XXX This should break ties using name of the statistic, or
3769  * something like that, to make the outcome stable.
3770  */
3771  if (bms_num_members(shared) > nmatches)
3772  {
3773  statOid = info->statOid;
3774  nmatches = bms_num_members(shared);
3775  matched = shared;
3776  }
3777  }
3778 
3779  /* No match? */
3780  if (statOid == InvalidOid)
3781  return false;
3782  Assert(nmatches > 1 && matched != NULL);
3783 
3784  stats = statext_ndistinct_load(statOid);
3785 
3786  /*
3787  * If we have a match, search it for the specific item that matches (there
3788  * must be one), and construct the output values.
3789  */
3790  if (stats)
3791  {
3792  int i;
3793  List *newlist = NIL;
3794  MVNDistinctItem *item = NULL;
3795 
3796  /* Find the specific item that exactly matches the combination */
3797  for (i = 0; i < stats->nitems; i++)
3798  {
3799  MVNDistinctItem *tmpitem = &stats->items[i];
3800 
3801  if (bms_subset_compare(tmpitem->attrs, matched) == BMS_EQUAL)
3802  {
3803  item = tmpitem;
3804  break;
3805  }
3806  }
3807 
3808  /* make sure we found an item */
3809  if (!item)
3810  elog(ERROR, "corrupt MVNDistinct entry");
3811 
3812  /* Form the output varinfo list, keeping only unmatched ones */
3813  foreach(lc, *varinfos)
3814  {
3815  GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc);
3816  AttrNumber attnum;
3817 
3818  if (!IsA(varinfo->var, Var))
3819  {
3820  newlist = lappend(newlist, varinfo);
3821  continue;
3822  }
3823 
3824  attnum = ((Var *) varinfo->var)->varattno;
3825  if (!bms_is_member(attnum, matched))
3826  newlist = lappend(newlist, varinfo);
3827  }
3828 
3829  *varinfos = newlist;
3830  *ndistinct = item->ndistinct;
3831  return true;
3832  }
3833 
3834  return false;
3835 }
#define NIL
Definition: pg_list.h:69
#define STATS_EXT_NDISTINCT
#define IsA(nodeptr, _type_)
Definition: nodes.h:555
List * statlist
Definition: relation.h:532
MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:37
double ndistinct
Definition: statistics.h:27
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
#define ERROR
Definition: elog.h:43
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:575
Node * var
Definition: selfuncs.c:3153
uint32 nitems
Definition: statistics.h:36
List * lappend(List *list, void *datum)
Definition: list.c:128
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
#define InvalidOid
Definition: postgres_ext.h:36
BMS_Comparison bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:344
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * attrs
Definition: statistics.h:28
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
Bitmapset * keys
Definition: relation.h:688
int i
#define elog
Definition: elog.h:219
MVNDistinct * statext_ndistinct_load(Oid mvoid)
Definition: mvdistinct.c:126
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
int16 AttrNumber
Definition: attnum.h:21
RelOptInfo * rel
Definition: selfuncs.c:3154
double estimate_num_groups ( PlannerInfo root,
List groupExprs,
double  input_rows,
List **  pgset 
)

Definition at line 3277 of file selfuncs.c.

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

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

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

Definition at line 4747 of file selfuncs.c.

References Alias::aliasname, Assert, BoolGetDatum, Query::distinctClause, elog, RangeTblEntry::eref, ERROR, TargetEntry::expr, find_base_rel(), VariableStatData::freefunc, get_relation_stats_hook, get_tle_by_resno(), Query::groupClause, HeapTupleIsValid, RangeTblEntry::inh, Int16GetDatum, InvalidAttrNumber, InvalidOid, IsA, VariableStatData::isunique, list_length(), NULL, ObjectIdGetDatum, PlannerInfo::parse, ReleaseSysCache(), RangeTblEntry::relid, TargetEntry::resjunk, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, SearchSysCache3, RangeTblEntry::security_barrier, Query::setOperations, PlannerInfo::simple_rte_array, STATRELATTINH, VariableStatData::statsTuple, RangeTblEntry::subquery, RelOptInfo::subroot, targetIsInSortList(), Query::targetList, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by examine_variable().

4749 {
4750  RangeTblEntry *rte = root->simple_rte_array[var->varno];
4751 
4752  Assert(IsA(rte, RangeTblEntry));
4753 
4755  (*get_relation_stats_hook) (root, rte, var->varattno, vardata))
4756  {
4757  /*
4758  * The hook took control of acquiring a stats tuple. If it did supply
4759  * a tuple, it'd better have supplied a freefunc.
4760  */
4761  if (HeapTupleIsValid(vardata->statsTuple) &&
4762  !vardata->freefunc)
4763  elog(ERROR, "no function provided to release variable stats with");
4764  }
4765  else if (rte->rtekind == RTE_RELATION)
4766  {
4767  /*
4768  * Plain table or parent of an inheritance appendrel, so look up the
4769  * column in pg_statistic
4770  */
4772  ObjectIdGetDatum(rte->relid),
4773  Int16GetDatum(var->varattno),
4774  BoolGetDatum(rte->inh));
4775  vardata->freefunc = ReleaseSysCache;
4776  }
4777  else if (rte->rtekind == RTE_SUBQUERY && !rte->inh)
4778  {
4779  /*
4780  * Plain subquery (not one that was converted to an appendrel).
4781  */
4782  Query *subquery = rte->subquery;
4783  RelOptInfo *rel;
4784  TargetEntry *ste;
4785 
4786  /*
4787  * Punt if it's a whole-row var rather than a plain column reference.
4788  */
4789  if (var->varattno == InvalidAttrNumber)
4790  return;
4791 
4792  /*
4793  * Punt if subquery uses set operations or GROUP BY, as these will
4794  * mash underlying columns' stats beyond recognition. (Set ops are
4795  * particularly nasty; if we forged ahead, we would return stats
4796  * relevant to only the leftmost subselect...) DISTINCT is also
4797  * problematic, but we check that later because there is a possibility
4798  * of learning something even with it.
4799  */
4800  if (subquery->setOperations ||
4801  subquery->groupClause)
4802  return;
4803 
4804  /*
4805  * OK, fetch RelOptInfo for subquery. Note that we don't change the
4806  * rel returned in vardata, since caller expects it to be a rel of the
4807  * caller's query level. Because we might already be recursing, we
4808  * can't use that rel pointer either, but have to look up the Var's
4809  * rel afresh.
4810  */
4811  rel = find_base_rel(root, var->varno);
4812 
4813  /* If the subquery hasn't been planned yet, we have to punt */
4814  if (rel->subroot == NULL)
4815  return;
4816  Assert(IsA(rel->subroot, PlannerInfo));
4817 
4818  /*
4819  * Switch our attention to the subquery as mangled by the planner. It
4820  * was okay to look at the pre-planning version for the tests above,
4821  * but now we need a Var that will refer to the subroot's live
4822  * RelOptInfos. For instance, if any subquery pullup happened during
4823  * planning, Vars in the targetlist might have gotten replaced, and we
4824  * need to see the replacement expressions.
4825  */
4826  subquery = rel->subroot->parse;
4827  Assert(IsA(subquery, Query));
4828 
4829  /* Get the subquery output expression referenced by the upper Var */
4830  ste = get_tle_by_resno(subquery->targetList, var->varattno);
4831  if (ste == NULL || ste->resjunk)
4832  elog(ERROR, "subquery %s does not have attribute %d",
4833  rte->eref->aliasname, var->varattno);
4834  var = (Var *) ste->expr;
4835 
4836  /*
4837  * If subquery uses DISTINCT, we can't make use of any stats for the
4838  * variable ... but, if it's the only DISTINCT column, we are entitled
4839  * to consider it unique. We do the test this way so that it works
4840  * for cases involving DISTINCT ON.
4841  */
4842  if (subquery->distinctClause)
4843  {
4844  if (list_length(subquery->distinctClause) == 1 &&
4845  targetIsInSortList(ste, InvalidOid, subquery->distinctClause))
4846  vardata->isunique = true;
4847  /* cannot go further */
4848  return;
4849  }
4850 
4851  /*
4852  * If the sub-query originated from a view with the security_barrier
4853  * attribute, we must not look at the variable's statistics, though it
4854  * seems all right to notice the existence of a DISTINCT clause. So
4855  * stop here.
4856  *
4857  * This is probably a harsher restriction than necessary; it's
4858  * certainly OK for the selectivity estimator (which is a C function,
4859  * and therefore omnipotent anyway) to look at the statistics. But
4860  * many selectivity estimators will happily *invoke the operator
4861  * function* to try to work out a good estimate - and that's not OK.
4862  * So for now, don't dig down for stats.
4863  */
4864  if (rte->security_barrier)
4865  return;
4866 
4867  /* Can only handle a simple Var of subquery's query level */
4868  if (var && IsA(var, Var) &&
4869  var->varlevelsup == 0)
4870  {
4871  /*
4872  * OK, recurse into the subquery. Note that the original setting
4873  * of vardata->isunique (which will surely be false) is left
4874  * unchanged in this situation. That's what we want, since even
4875  * if the underlying column is unique, the subquery may have
4876  * joined to other tables in a way that creates duplicates.
4877  */
4878  examine_simple_variable(rel->subroot, var, vardata);
4879  }
4880  }
4881  else
4882  {
4883  /*
4884  * Otherwise, the Var comes from a FUNCTION, VALUES, or CTE RTE. (We
4885  * won't see RTE_JOIN here because join alias Vars have already been
4886  * flattened.) There's not much we can do with function outputs, but
4887  * maybe someday try to be smarter about VALUES and/or CTEs.
4888  */
4889  }
4890 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:555
Query * parse
Definition: relation.h:154
Index varlevelsup
Definition: primnodes.h:173
HeapTuple statsTuple
Definition: selfuncs.h:71
#define Int16GetDatum(X)
Definition: postgres.h:457
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
static void examine_simple_variable(PlannerInfo *root, Var *var, VariableStatData *vardata)
Definition: selfuncs.c:4747
List * targetList
Definition: parsenodes.h:131
PlannerInfo * subroot
Definition: relation.h:536
bool resjunk
Definition: primnodes.h:1359
List * distinctClause
Definition: parsenodes.h:145
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
get_relation_stats_hook_type get_relation_stats_hook
Definition: selfuncs.c:151
RangeTblEntry ** simple_rte_array
Definition: relation.h:187
Index varno
Definition: primnodes.h:166
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
bool security_barrier
Definition: parsenodes.h:935
#define BoolGetDatum(X)
Definition: postgres.h:408
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
char * aliasname
Definition: primnodes.h:42
Expr * expr
Definition: primnodes.h:1352
static int list_length(const List *l)
Definition: pg_list.h:89
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:916
Node * setOperations
Definition: parsenodes.h:154
Query * subquery
Definition: parsenodes.h:934
List * groupClause
Definition: parsenodes.h:137
#define SearchSysCache3(cacheId, key1, key2, key3)
Definition: syscache.h:156
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1000
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:223
void(* freefunc)(HeapTuple tuple)
Definition: selfuncs.h:73
void examine_variable ( PlannerInfo root,
Node node,
int  varRelid,
VariableStatData vardata 
)

Definition at line 4560 of file selfuncs.c.

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

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

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

Definition at line 5384 of file selfuncs.c.

References BMS_EMPTY_SET, bms_membership(), BMS_MULTIPLE, BMS_SINGLETON, bms_singleton_member(), elog, ERROR, find_base_rel(), find_join_rel(), and NULL.

Referenced by eqjoinsel().

5385 {
5386  RelOptInfo *rel = NULL;
5387 
5388  switch (bms_membership(relids))
5389  {
5390  case BMS_EMPTY_SET:
5391  /* should not happen */
5392  break;
5393  case BMS_SINGLETON:
5394  rel = find_base_rel(root, bms_singleton_member(relids));
5395  break;
5396  case BMS_MULTIPLE:
5397  rel = find_join_rel(root, relids);
5398  break;
5399  }
5400 
5401  if (rel == NULL)
5402  elog(ERROR, "could not find RelOptInfo for given relids");
5403 
5404  return rel;
5405 }
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:288
#define ERROR
Definition: elog.h:43
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:604
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:496
#define NULL
Definition: c.h:229
#define elog
Definition: elog.h:219
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:223
void genericcostestimate ( PlannerInfo root,
IndexPath path,
double  loop_count,
List qinfos,
GenericCosts costs 
)

Definition at line 6381 of file selfuncs.c.

References add_predicate_to_quals(), ScalarArrayOpExpr::args, RestrictInfo::clause, clauselist_selectivity(), cpu_index_tuple_cost, cpu_operator_cost, estimate_array_length(), get_tablespace_page_costs(), index_pages_fetched(), GenericCosts::indexCorrelation, IndexPath::indexinfo, IndexPath::indexorderbys, IndexPath::indexquals, GenericCosts::indexSelectivity, GenericCosts::indexStartupCost, GenericCosts::indexTotalCost, IsA, JOIN_INNER, lfirst, list_length(), lsecond, NULL, GenericCosts::num_sa_scans, GenericCosts::numIndexPages, GenericCosts::numIndexTuples, orderby_operands_eval_cost(), other_operands_eval_cost(), IndexOptInfo::pages, IndexOptInfo::rel, RelOptInfo::relid, IndexOptInfo::reltablespace, rint(), GenericCosts::spc_random_page_cost, RelOptInfo::tuples, and IndexOptInfo::tuples.

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

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

Definition at line 5158 of file selfuncs.c.

References AccessShareLock, Assert, VariableStatData::atttype, BackwardScanDirection, BTGreaterStrategyNumber, BTLessStrategyNumber, BTREE_AM_OID, BuildIndexInfo(), CreateExecutorState(), datumCopy(), ExprContext::ecxt_per_tuple_memory, ExprContext::ecxt_scantuple, elog, ERROR, ExecDropSingleTupleTableSlot(), ExecStoreTuple(), FormIndexDatum(), ForwardScanDirection, FreeExecutorState(), get_op_opfamily_strategy(), get_typlenbyval(), GetPerTupleExprContext, heap_close, heap_open(), IndexOptInfo::hypothetical, index_beginscan(), index_close(), index_endscan(), index_getnext(), INDEX_MAX_KEYS, index_open(), index_rescan(), RelOptInfo::indexlist, IndexOptInfo::indexoid, IndexOptInfo::indpred, InitDirtySnapshot, InvalidBuffer, InvalidOid, InvalidStrategy, lfirst, MakeSingleTupleTableSlot(), match_index_to_operand(), MemoryContextSwitchTo(), NIL, NoLock, NULL, VariableStatData::rel, IndexOptInfo::relam, RelationGetDescr, RelationGetRelationName, RelOptInfo::relid, RangeTblEntry::relid, IndexOptInfo::reverse_sort, RTE_RELATION, RangeTblEntry::rtekind, ScanKeyEntryInitialize(), PlannerInfo::simple_rte_array, SK_ISNULL, SK_SEARCHNOTNULL, IndexOptInfo::sortopfamily, values, and VariableStatData::var.

Referenced by get_variable_range(), and ineq_histogram_selectivity().

5161 {
5162  bool have_data = false;
5163  RelOptInfo *rel = vardata->rel;
5164  RangeTblEntry *rte;
5165  ListCell *lc;
5166 
5167  /* No hope if no relation or it doesn't have indexes */
5168  if (rel == NULL || rel->indexlist == NIL)
5169  return false;
5170  /* If it has indexes it must be a plain relation */
5171  rte = root->simple_rte_array[rel->relid];
5172  Assert(rte->rtekind == RTE_RELATION);
5173 
5174  /* Search through the indexes to see if any match our problem */
5175  foreach(lc, rel->indexlist)
5176  {
5178  ScanDirection indexscandir;
5179 
5180  /* Ignore non-btree indexes */
5181  if (index->relam != BTREE_AM_OID)
5182  continue;
5183 
5184  /*
5185  * Ignore partial indexes --- we only want stats that cover the entire
5186  * relation.
5187  */
5188  if (index->indpred != NIL)
5189  continue;
5190 
5191  /*
5192  * The index list might include hypothetical indexes inserted by a
5193  * get_relation_info hook --- don't try to access them.
5194  */
5195  if (index->hypothetical)
5196  continue;
5197 
5198  /*
5199  * The first index column must match the desired variable and sort
5200  * operator --- but we can use a descending-order index.
5201  */
5202  if (!match_index_to_operand(vardata->var, 0, index))
5203  continue;
5204  switch (get_op_opfamily_strategy(sortop, index->sortopfamily[0]))
5205  {
5206  case BTLessStrategyNumber:
5207  if (index->reverse_sort[0])
5208  indexscandir = BackwardScanDirection;
5209  else
5210  indexscandir = ForwardScanDirection;
5211  break;
5213  if (index->reverse_sort[0])
5214  indexscandir = ForwardScanDirection;
5215  else
5216  indexscandir = BackwardScanDirection;
5217  break;
5218  default:
5219  /* index doesn't match the sortop */
5220  continue;
5221  }
5222 
5223  /*
5224  * Found a suitable index to extract data from. We'll need an EState
5225  * and a bunch of other infrastructure.
5226  */
5227  {
5228  EState *estate;
5229  ExprContext *econtext;
5230  MemoryContext tmpcontext;
5231  MemoryContext oldcontext;
5232  Relation heapRel;
5233  Relation indexRel;
5234  IndexInfo *indexInfo;
5235  TupleTableSlot *slot;
5236  int16 typLen;
5237  bool typByVal;
5238  ScanKeyData scankeys[1];
5239  IndexScanDesc index_scan;
5240  HeapTuple tup;
5242  bool isnull[INDEX_MAX_KEYS];
5243  SnapshotData SnapshotDirty;
5244 
5245  estate = CreateExecutorState();
5246  econtext = GetPerTupleExprContext(estate);
5247  /* Make sure any cruft is generated in the econtext's memory */
5248  tmpcontext = econtext->ecxt_per_tuple_memory;
5249  oldcontext = MemoryContextSwitchTo(tmpcontext);
5250 
5251  /*
5252  * Open the table and index so we can read from them. We should
5253  * already have at least AccessShareLock on the table, but not
5254  * necessarily on the index.
5255  */
5256  heapRel = heap_open(rte->relid, NoLock);
5257  indexRel = index_open(index->indexoid, AccessShareLock);
5258 
5259  /* extract index key information from the index's pg_index info */
5260  indexInfo = BuildIndexInfo(indexRel);
5261 
5262  /* some other stuff */
5263  slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRel));
5264  econtext->ecxt_scantuple = slot;
5265  get_typlenbyval(vardata->atttype, &typLen, &typByVal);
5266  InitDirtySnapshot(SnapshotDirty);
5267 
5268  /* set up an IS NOT NULL scan key so that we ignore nulls */
5269  ScanKeyEntryInitialize(&scankeys[0],
5271  1, /* index col to scan */
5272  InvalidStrategy, /* no strategy */
5273  InvalidOid, /* no strategy subtype */
5274  InvalidOid, /* no collation */
5275  InvalidOid, /* no reg proc for this */
5276  (Datum) 0); /* constant */
5277 
5278  have_data = true;
5279 
5280  /* If min is requested ... */
5281  if (min)
5282  {
5283  /*
5284  * In principle, we should scan the index with our current
5285  * active snapshot, which is the best approximation we've got
5286  * to what the query will see when executed. But that won't
5287  * be exact if a new snap is taken before running the query,
5288  * and it can be very expensive if a lot of uncommitted rows
5289  * exist at the end of the index (because we'll laboriously
5290  * fetch each one and reject it). What seems like a good
5291  * compromise is to use SnapshotDirty. That will accept
5292  * uncommitted rows, and thus avoid fetching multiple heap
5293  * tuples in this scenario. On the other hand, it will reject
5294  * known-dead rows, and thus not give a bogus answer when the
5295  * extreme value has been deleted; that case motivates not
5296  * using SnapshotAny here.
5297  */
5298  index_scan = index_beginscan(heapRel, indexRel, &SnapshotDirty,
5299  1, 0);
5300  index_rescan(index_scan, scankeys, 1, NULL, 0);
5301 
5302  /* Fetch first tuple in sortop's direction */
5303  if ((tup = index_getnext(index_scan,
5304  indexscandir)) != NULL)
5305  {
5306  /* Extract the index column values from the heap tuple */
5307  ExecStoreTuple(tup, slot, InvalidBuffer, false);
5308  FormIndexDatum(indexInfo, slot, estate,
5309  values, isnull);
5310 
5311  /* Shouldn't have got a null, but be careful */
5312  if (isnull[0])
5313  elog(ERROR, "found unexpected null value in index \"%s\"",
5314  RelationGetRelationName(indexRel));
5315 
5316  /* Copy the index column value out to caller's context */
5317  MemoryContextSwitchTo(oldcontext);
5318  *min = datumCopy(values[0], typByVal, typLen);
5319  MemoryContextSwitchTo(tmpcontext);
5320  }
5321  else
5322  have_data = false;
5323 
5324  index_endscan(index_scan);
5325  }
5326 
5327  /* If max is requested, and we didn't find the index is empty */
5328  if (max && have_data)
5329  {
5330  index_scan = index_beginscan(heapRel, indexRel, &SnapshotDirty,
5331  1, 0);
5332  index_rescan(index_scan, scankeys, 1, NULL, 0);
5333 
5334  /* Fetch first tuple in reverse direction */
5335  if ((tup = index_getnext(index_scan,
5336  -indexscandir)) != NULL)
5337  {
5338  /* Extract the index column values from the heap tuple */
5339  ExecStoreTuple(tup, slot, InvalidBuffer, false);
5340  FormIndexDatum(indexInfo, slot, estate,
5341  values, isnull);
5342 
5343  /* Shouldn't have got a null, but be careful */
5344  if (isnull[0])
5345  elog(ERROR, "found unexpected null value in index \"%s\"",
5346  RelationGetRelationName(indexRel));
5347 
5348  /* Copy the index column value out to caller's context */
5349  MemoryContextSwitchTo(oldcontext);
5350  *max = datumCopy(values[0], typByVal, typLen);
5351  MemoryContextSwitchTo(tmpcontext);
5352  }
5353  else
5354  have_data = false;
5355 
5356  index_endscan(index_scan);
5357  }
5358 
5359  /* Clean everything up */
5361 
5362  index_close(indexRel, AccessShareLock);
5363  heap_close(heapRel, NoLock);
5364 
5365  MemoryContextSwitchTo(oldcontext);
5366  FreeExecutorState(estate);
5367 
5368  /* And we're done */
5369  break;
5370  }
5371  }
5372 
5373  return have_data;
5374 }
signed short int16
Definition: c.h:255
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
Definition: index.c:1764
#define InvalidStrategy
Definition: stratnum.h:24
#define NIL
Definition: pg_list.h:69
TupleTableSlot * ExecStoreTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer, bool shouldFree)
Definition: execTuples.c:320
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define RelationGetDescr(relation)
Definition: rel.h:429
bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index)
Definition: indxpath.c:3176
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:202
RelOptInfo * rel
Definition: selfuncs.h:70
#define BTREE_AM_OID
Definition: pg_am.h:70
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
#define InvalidBuffer
Definition: buf.h:25
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:310
Oid * sortopfamily
Definition: relation.h:608
bool hypothetical
Definition: relation.h:628
#define heap_close(r, l)
Definition: heapam.h:97
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:1639
Definition: type.h:90
void FreeExecutorState(EState *estate)
Definition: execUtils.c:173
#define GetPerTupleExprContext(estate)
Definition: executor.h:456
#define ERROR
Definition: elog.h:43
#define InitDirtySnapshot(snapshotdata)
Definition: tqual.h:100
#define NoLock
Definition: lockdefs.h:34
void ScanKeyEntryInitialize(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, RegProcedure procedure, Datum argument)
Definition: scankey.c:32
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:216
ScanDirection
Definition: sdir.h:22
#define RelationGetRelationName(relation)
Definition: rel.h:437
#define SK_SEARCHNOTNULL
Definition: skey.h:122
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc)
Definition: execTuples.c:199
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:340
#define SK_ISNULL
Definition: skey.h:115
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:128
EState * CreateExecutorState(void)
Definition: execUtils.c:77
Index relid
Definition: relation.h:522
RangeTblEntry ** simple_rte_array
Definition: relation.h:187
uintptr_t Datum
Definition: postgres.h:372
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
List * indexlist
Definition: relation.h:531
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
#define INDEX_MAX_KEYS
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:1969
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:196
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:176
RTEKind rtekind
Definition: parsenodes.h:916
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Definition: lsyscache.c:80
#define elog
Definition: elog.h:219
Oid indexoid
Definition: relation.h:593
bool * reverse_sort
Definition: relation.h:609
#define BTLessStrategyNumber
Definition: stratnum.h:29
List * indpred
Definition: relation.h:616
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:151
HeapTuple index_getnext(IndexScanDesc scan, ScanDirection direction)
Definition: indexam.c:659
IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys)
Definition: indexam.c:221
void get_join_variables ( PlannerInfo root,
List args,
SpecialJoinInfo sjinfo,
VariableStatData vardata1,
VariableStatData vardata2,
bool join_is_reversed 
)

Definition at line 4501 of file selfuncs.c.

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

Referenced by eqjoinsel(), and networkjoinsel().

4504 {
4505  Node *left,
4506  *right;
4507 
4508  if (list_length(args) != 2)
4509  elog(ERROR, "join operator should take two arguments");
4510 
4511  left = (Node *) linitial(args);
4512  right = (Node *) lsecond(args);
4513 
4514  examine_variable(root, left, 0, vardata1);
4515  examine_variable(root, right, 0, vardata2);
4516 
4517  if (vardata1->rel &&
4518  bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
4519  *join_is_reversed = true; /* var1 is on RHS */
4520  else if (vardata2->rel &&
4521  bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
4522  *join_is_reversed = true; /* var2 is on LHS */
4523  else
4524  *join_is_reversed = false;
4525 }
RelOptInfo * rel
Definition: selfuncs.h:70
Definition: nodes.h:504
#define lsecond(l)
Definition: pg_list.h:114
Relids syn_lefthand
Definition: relation.h:1851
Relids syn_righthand
Definition: relation.h:1852
#define linitial(l)
Definition: pg_list.h:110
#define ERROR
Definition: elog.h:43
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:307
Relids relids
Definition: relation.h:494
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4560
static int list_length(const List *l)
Definition: pg_list.h:89
#define elog
Definition: elog.h:219
bool get_restriction_variable ( PlannerInfo root,
List args,
int  varRelid,
VariableStatData vardata,
Node **  other,
bool varonleft 
)

Definition at line 4441 of file selfuncs.c.

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

Referenced by _int_matchsel(), arraycontsel(), eqsel(), ltreeparentsel(), networksel(), patternsel(), rangesel(), scalargtsel(), scalarltsel(), and tsmatchsel().

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

Definition at line 4904 of file selfuncs.c.

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

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

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

Definition at line 5026 of file selfuncs.c.

References VariableStatData::atttype, VariableStatData::atttypmod, datumCopy(), DatumGetBool, DEFAULT_COLLATION_OID, fmgr_info(), free_attstatsslot(), FunctionCall2Coll(), get_actual_variable_range(), get_attstatsslot(), get_opcode(), get_typlenbyval(), HeapTupleIsValid, i, InvalidOid, NULL, STATISTIC_KIND_HISTOGRAM, STATISTIC_KIND_MCV, VariableStatData::statsTuple, and values.

Referenced by mergejoinscansel().

5028 {
5029  Datum tmin = 0;
5030  Datum tmax = 0;
5031  bool have_data = false;
5032  int16 typLen;
5033  bool typByVal;
5034  Datum *values;
5035  int nvalues;
5036  int i;
5037 
5038  /*
5039  * XXX It's very tempting to try to use the actual column min and max, if
5040  * we can get them relatively-cheaply with an index probe. However, since
5041  * this function is called many times during join planning, that could
5042  * have unpleasant effects on planning speed. Need more investigation
5043  * before enabling this.
5044  */
5045 #ifdef NOT_USED
5046  if (get_actual_variable_range(root, vardata, sortop, min, max))
5047  return true;
5048 #endif
5049 
5050  if (!HeapTupleIsValid(vardata->statsTuple))
5051  {
5052  /* no stats available, so default result */
5053  return false;
5054  }
5055 
5056  get_typlenbyval(vardata->atttype, &typLen, &typByVal);
5057 
5058  /*
5059  * If there is a histogram, grab the first and last values.
5060  *
5061  * If there is a histogram that is sorted with some other operator than
5062  * the one we want, fail --- this suggests that there is data we can't
5063  * use.
5064  */
5065  if (get_attstatsslot(vardata->statsTuple,
5066  vardata->atttype, vardata->atttypmod,
5067  STATISTIC_KIND_HISTOGRAM, sortop,
5068  NULL,
5069  &values, &nvalues,
5070  NULL, NULL))
5071  {
5072  if (nvalues > 0)
5073  {
5074  tmin = datumCopy(values[0], typByVal, typLen);
5075  tmax = datumCopy(values[nvalues - 1], typByVal, typLen);
5076  have_data = true;
5077  }
5078  free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
5079  }
5080  else if (get_attstatsslot(vardata->statsTuple,
5081  vardata->atttype, vardata->atttypmod,
5083  NULL,
5084  &values, &nvalues,
5085  NULL, NULL))
5086  {
5087  free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
5088  return false;
5089  }
5090 
5091  /*
5092  * If we have most-common-values info, look for extreme MCVs. This is
5093  * needed even if we also have a histogram, since the histogram excludes
5094  * the MCVs. However, usually the MCVs will not be the extreme values, so
5095  * avoid unnecessary data copying.
5096  */
5097  if (get_attstatsslot(vardata->statsTuple,
5098  vardata->atttype, vardata->atttypmod,
5100  NULL,
5101  &values, &nvalues,
5102  NULL, NULL))
5103  {
5104  bool tmin_is_mcv = false;
5105  bool tmax_is_mcv = false;
5106  FmgrInfo opproc;
5107 
5108  fmgr_info(get_opcode(sortop), &opproc);
5109 
5110  for (i = 0; i < nvalues; i++)
5111  {
5112  if (!have_data)
5113  {
5114  tmin = tmax = values[i];
5115  tmin_is_mcv = tmax_is_mcv = have_data = true;
5116  continue;
5117  }
5118  if (DatumGetBool(FunctionCall2Coll(&opproc,
5120  values[i], tmin)))
5121  {
5122  tmin = values[i];
5123  tmin_is_mcv = true;
5124  }
5125  if (DatumGetBool(