PostgreSQL Source Code  git master
extended_stats.c File Reference
#include "postgres.h"
#include "access/detoast.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "catalog/indexing.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_statistic_ext_data.h"
#include "executor/executor.h"
#include "commands/progress.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "statistics/extended_stats_internal.h"
#include "statistics/statistics.h"
#include "utils/acl.h"
#include "utils/array.h"
#include "utils/attoptcache.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/selfuncs.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for extended_stats.c:

Go to the source code of this file.

Data Structures

struct  StatExtEntry
 
struct  AnlExprData
 

Macros

#define WIDTH_THRESHOLD   1024
 

Typedefs

typedef struct StatExtEntry StatExtEntry
 
typedef struct AnlExprData AnlExprData
 

Functions

static Listfetch_statentries_for_relation (Relation pg_statext, Oid relid)
 
static VacAttrStats ** lookup_var_attr_stats (Relation rel, Bitmapset *attrs, List *exprs, int nvacatts, VacAttrStats **vacatts)
 
static void statext_store (Oid statOid, MVNDistinct *ndistinct, MVDependencies *dependencies, MCVList *mcv, Datum exprs, VacAttrStats **stats)
 
static int statext_compute_stattarget (int stattarget, int natts, VacAttrStats **stats)
 
static void compute_expr_stats (Relation onerel, double totalrows, AnlExprData *exprdata, int nexprs, HeapTuple *rows, int numrows)
 
static Datum serialize_expr_stats (AnlExprData *exprdata, int nexprs)
 
static Datum expr_fetch_func (VacAttrStatsP stats, int rownum, bool *isNull)
 
static AnlExprDatabuild_expr_data (List *exprs, int stattarget)
 
static StatsBuildDatamake_build_data (Relation onerel, StatExtEntry *stat, int numrows, HeapTuple *rows, VacAttrStats **stats, int stattarget)
 
void BuildRelationExtStatistics (Relation onerel, double totalrows, int numrows, HeapTuple *rows, int natts, VacAttrStats **vacattrstats)
 
int ComputeExtStatisticsRows (Relation onerel, int natts, VacAttrStats **vacattrstats)
 
bool statext_is_kind_built (HeapTuple htup, char type)
 
static VacAttrStatsexamine_attribute (Node *expr)
 
static VacAttrStatsexamine_expression (Node *expr, int stattarget)
 
MultiSortSupport multi_sort_init (int ndims)
 
void multi_sort_add_dimension (MultiSortSupport mss, int sortdim, Oid oper, Oid collation)
 
int multi_sort_compare (const void *a, const void *b, void *arg)
 
int multi_sort_compare_dim (int dim, const SortItem *a, const SortItem *b, MultiSortSupport mss)
 
int multi_sort_compare_dims (int start, int end, const SortItem *a, const SortItem *b, MultiSortSupport mss)
 
int compare_scalars_simple (const void *a, const void *b, void *arg)
 
int compare_datums_simple (Datum a, Datum b, SortSupport ssup)
 
AttrNumberbuild_attnums_array (Bitmapset *attrs, int nexprs, int *numattrs)
 
SortItembuild_sorted_items (StatsBuildData *data, int *nitems, MultiSortSupport mss, int numattrs, AttrNumber *attnums)
 
bool has_stats_of_kind (List *stats, char requiredkind)
 
static int stat_find_expression (StatisticExtInfo *stat, Node *expr)
 
static bool stat_covers_expressions (StatisticExtInfo *stat, List *exprs, Bitmapset **expr_idxs)
 
StatisticExtInfochoose_best_statistics (List *stats, char requiredkind, Bitmapset **clause_attnums, List **clause_exprs, int nclauses)
 
static bool statext_is_compatible_clause_internal (PlannerInfo *root, Node *clause, Index relid, Bitmapset **attnums, List **exprs)
 
static bool statext_is_compatible_clause (PlannerInfo *root, Node *clause, Index relid, Bitmapset **attnums, List **exprs)
 
static Selectivity statext_mcv_clauselist_selectivity (PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses, bool is_or)
 
Selectivity statext_clauselist_selectivity (PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses, bool is_or)
 
bool examine_opclause_args (List *args, Node **exprp, Const **cstp, bool *expronleftp)
 
HeapTuple statext_expressions_load (Oid stxoid, int idx)
 

Macro Definition Documentation

◆ WIDTH_THRESHOLD

#define WIDTH_THRESHOLD   1024

Definition at line 59 of file extended_stats.c.

Referenced by build_sorted_items().

Typedef Documentation

◆ AnlExprData

typedef struct AnlExprData AnlExprData

◆ StatExtEntry

typedef struct StatExtEntry StatExtEntry

Function Documentation

◆ build_attnums_array()

AttrNumber* build_attnums_array ( Bitmapset attrs,
int  nexprs,
int *  numattrs 
)

Definition at line 965 of file extended_stats.c.

References Assert, attnum, AttributeNumberIsValid, bms_next_member(), bms_num_members(), i, MaxAttrNumber, and palloc().

966 {
967  int i,
968  j;
969  AttrNumber *attnums;
970  int num = bms_num_members(attrs);
971 
972  if (numattrs)
973  *numattrs = num;
974 
975  /* build attnums from the bitmapset */
976  attnums = (AttrNumber *) palloc(sizeof(AttrNumber) * num);
977  i = 0;
978  j = -1;
979  while ((j = bms_next_member(attrs, j)) >= 0)
980  {
981  int attnum = (j - nexprs);
982 
983  /*
984  * Make sure the bitmap contains only user-defined attributes. As
985  * bitmaps can't contain negative values, this can be violated in two
986  * ways. Firstly, the bitmap might contain 0 as a member, and secondly
987  * the integer value might be larger than MaxAttrNumber.
988  */
990  Assert(attnum <= MaxAttrNumber);
991  Assert(attnum >= (-nexprs));
992 
993  attnums[i++] = (AttrNumber) attnum;
994 
995  /* protect against overflows */
996  Assert(i <= num);
997  }
998 
999  return attnums;
1000 }
#define MaxAttrNumber
Definition: attnum.h:24
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
int16 attnum
Definition: pg_attribute.h:83
#define Assert(condition)
Definition: c.h:804
void * palloc(Size size)
Definition: mcxt.c:1062
int i
int16 AttrNumber
Definition: attnum.h:21

◆ build_expr_data()

static AnlExprData * build_expr_data ( List exprs,
int  stattarget 
)
static

Definition at line 2237 of file extended_stats.c.

References examine_expression(), AnlExprData::expr, idx(), lfirst, list_length(), palloc0(), and AnlExprData::vacattrstat.

Referenced by BuildRelationExtStatistics().

2238 {
2239  int idx;
2240  int nexprs = list_length(exprs);
2241  AnlExprData *exprdata;
2242  ListCell *lc;
2243 
2244  exprdata = (AnlExprData *) palloc0(nexprs * sizeof(AnlExprData));
2245 
2246  idx = 0;
2247  foreach(lc, exprs)
2248  {
2249  Node *expr = (Node *) lfirst(lc);
2250  AnlExprData *thisdata = &exprdata[idx];
2251 
2252  thisdata->expr = expr;
2253  thisdata->vacattrstat = examine_expression(expr, stattarget);
2254  idx++;
2255  }
2256 
2257  return exprdata;
2258 }
static VacAttrStats * examine_expression(Node *expr, int stattarget)
Definition: nodes.h:539
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
void * palloc0(Size size)
Definition: mcxt.c:1093
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
VacAttrStats * vacattrstat

◆ build_sorted_items()

SortItem* build_sorted_items ( StatsBuildData data,
int *  nitems,
MultiSortSupport  mss,
int  numattrs,
AttrNumber attnums 
)

Definition at line 1010 of file extended_stats.c.

References Assert, attlen, attnum, StatsBuildData::attnums, VacAttrStats::attrtypid, get_typlen(), i, idx(), multi_sort_compare(), StatsBuildData::nattnums, StatsBuildData::nulls, StatsBuildData::numrows, palloc(), palloc0(), pfree(), PG_DETOAST_DATUM, PointerGetDatum, qsort_arg(), StatsBuildData::stats, toast_raw_datum_size(), value, StatsBuildData::values, values, and WIDTH_THRESHOLD.

Referenced by dependency_degree(), and statext_mcv_build().

1013 {
1014  int i,
1015  j,
1016  len,
1017  nrows;
1018  int nvalues = data->numrows * numattrs;
1019 
1020  SortItem *items;
1021  Datum *values;
1022  bool *isnull;
1023  char *ptr;
1024  int *typlen;
1025 
1026  /* Compute the total amount of memory we need (both items and values). */
1027  len = data->numrows * sizeof(SortItem) + nvalues * (sizeof(Datum) + sizeof(bool));
1028 
1029  /* Allocate the memory and split it into the pieces. */
1030  ptr = palloc0(len);
1031 
1032  /* items to sort */
1033  items = (SortItem *) ptr;
1034  ptr += data->numrows * sizeof(SortItem);
1035 
1036  /* values and null flags */
1037  values = (Datum *) ptr;
1038  ptr += nvalues * sizeof(Datum);
1039 
1040  isnull = (bool *) ptr;
1041  ptr += nvalues * sizeof(bool);
1042 
1043  /* make sure we consumed the whole buffer exactly */
1044  Assert((ptr - (char *) items) == len);
1045 
1046  /* fix the pointers to Datum and bool arrays */
1047  nrows = 0;
1048  for (i = 0; i < data->numrows; i++)
1049  {
1050  items[nrows].values = &values[nrows * numattrs];
1051  items[nrows].isnull = &isnull[nrows * numattrs];
1052 
1053  nrows++;
1054  }
1055 
1056  /* build a local cache of typlen for all attributes */
1057  typlen = (int *) palloc(sizeof(int) * data->nattnums);
1058  for (i = 0; i < data->nattnums; i++)
1059  typlen[i] = get_typlen(data->stats[i]->attrtypid);
1060 
1061  nrows = 0;
1062  for (i = 0; i < data->numrows; i++)
1063  {
1064  bool toowide = false;
1065 
1066  /* load the values/null flags from sample rows */
1067  for (j = 0; j < numattrs; j++)
1068  {
1069  Datum value;
1070  bool isnull;
1071  int attlen;
1072  AttrNumber attnum = attnums[j];
1073 
1074  int idx;
1075 
1076  /* match attnum to the pre-calculated data */
1077  for (idx = 0; idx < data->nattnums; idx++)
1078  {
1079  if (attnum == data->attnums[idx])
1080  break;
1081  }
1082 
1083  Assert(idx < data->nattnums);
1084 
1085  value = data->values[idx][i];
1086  isnull = data->nulls[idx][i];
1087  attlen = typlen[idx];
1088 
1089  /*
1090  * If this is a varlena value, check if it's too wide and if yes
1091  * then skip the whole item. Otherwise detoast the value.
1092  *
1093  * XXX It may happen that we've already detoasted some preceding
1094  * values for the current item. We don't bother to cleanup those
1095  * on the assumption that those are small (below WIDTH_THRESHOLD)
1096  * and will be discarded at the end of analyze.
1097  */
1098  if ((!isnull) && (attlen == -1))
1099  {
1101  {
1102  toowide = true;
1103  break;
1104  }
1105 
1106  value = PointerGetDatum(PG_DETOAST_DATUM(value));
1107  }
1108 
1109  items[nrows].values[j] = value;
1110  items[nrows].isnull[j] = isnull;
1111  }
1112 
1113  if (toowide)
1114  continue;
1115 
1116  nrows++;
1117  }
1118 
1119  /* store the actual number of items (ignoring the too-wide ones) */
1120  *nitems = nrows;
1121 
1122  /* all items were too wide */
1123  if (nrows == 0)
1124  {
1125  /* everything is allocated as a single chunk */
1126  pfree(items);
1127  return NULL;
1128  }
1129 
1130  /* do the sort, using the multi-sort */
1131  qsort_arg((void *) items, nrows, sizeof(SortItem),
1132  multi_sort_compare, mss);
1133 
1134  return items;
1135 }
static struct @142 value
#define PointerGetDatum(X)
Definition: postgres.h:600
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
void pfree(void *pointer)
Definition: mcxt.c:1169
Oid attrtypid
Definition: vacuum.h:124
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:545
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
int16 attlen
Definition: pg_attribute.h:68
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
struct SortItem SortItem
#define WIDTH_THRESHOLD
VacAttrStats ** stats
int16 attnum
Definition: pg_attribute.h:83
#define Assert(condition)
Definition: c.h:804
int multi_sort_compare(const void *a, const void *b, void *arg)
static Datum values[MAXATTR]
Definition: bootstrap.c:166
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2144
void * palloc(Size size)
Definition: mcxt.c:1062
int i
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
int16 AttrNumber
Definition: attnum.h:21
unsigned char bool
Definition: c.h:391

◆ BuildRelationExtStatistics()

void BuildRelationExtStatistics ( Relation  onerel,
double  totalrows,
int  numrows,
HeapTuple rows,
int  natts,
VacAttrStats **  vacattrstats 
)

Definition at line 113 of file extended_stats.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, bms_num_members(), build_expr_data(), StatExtEntry::columns, compute_expr_stats(), CurrentMemoryContext, elog, ereport, errcode(), errmsg(), ERROR, errtable(), StatExtEntry::exprs, fetch_statentries_for_relation(), get_namespace_name(), IsAutoVacuumWorkerProcess(), lfirst, lfirst_int, list_length(), lookup_var_attr_stats(), make_build_data(), MemoryContextDelete(), MemoryContextSwitchTo(), StatExtEntry::name, NIL, pfree(), pgstat_progress_update_multi_param(), pgstat_progress_update_param(), PROGRESS_ANALYZE_EXT_STATS_COMPUTED, PROGRESS_ANALYZE_EXT_STATS_TOTAL, PROGRESS_ANALYZE_PHASE, PROGRESS_ANALYZE_PHASE_COMPUTE_EXT_STATS, RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, RowExclusiveLock, StatExtEntry::schema, serialize_expr_stats(), statext_compute_stattarget(), statext_dependencies_build(), statext_mcv_build(), statext_ndistinct_build(), statext_store(), StatExtEntry::statOid, StatExtEntry::stattarget, table_close(), table_open(), StatExtEntry::types, val, and WARNING.

Referenced by do_analyze_rel().

116 {
117  Relation pg_stext;
118  ListCell *lc;
119  List *statslist;
120  MemoryContext cxt;
121  MemoryContext oldcxt;
122  int64 ext_cnt;
123 
124  /* Do nothing if there are no columns to analyze. */
125  if (!natts)
126  return;
127 
129  "BuildRelationExtStatistics",
131  oldcxt = MemoryContextSwitchTo(cxt);
132 
133  pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
134  statslist = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
135 
136  /* report this phase */
137  if (statslist != NIL)
138  {
139  const int index[] = {
142  };
143  const int64 val[] = {
145  list_length(statslist)
146  };
147 
148  pgstat_progress_update_multi_param(2, index, val);
149  }
150 
151  ext_cnt = 0;
152  foreach(lc, statslist)
153  {
155  MVNDistinct *ndistinct = NULL;
156  MVDependencies *dependencies = NULL;
157  MCVList *mcv = NULL;
158  Datum exprstats = (Datum) 0;
159  VacAttrStats **stats;
160  ListCell *lc2;
161  int stattarget;
162  StatsBuildData *data;
163 
164  /*
165  * Check if we can build these stats based on the column analyzed. If
166  * not, report this fact (except in autovacuum) and move on.
167  */
168  stats = lookup_var_attr_stats(onerel, stat->columns, stat->exprs,
169  natts, vacattrstats);
170  if (!stats)
171  {
174  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
175  errmsg("statistics object \"%s.%s\" could not be computed for relation \"%s.%s\"",
176  stat->schema, stat->name,
177  get_namespace_name(onerel->rd_rel->relnamespace),
178  RelationGetRelationName(onerel)),
179  errtable(onerel)));
180  continue;
181  }
182 
183  /* compute statistics target for this statistics */
184  stattarget = statext_compute_stattarget(stat->stattarget,
185  bms_num_members(stat->columns),
186  stats);
187 
188  /*
189  * Don't rebuild statistics objects with statistics target set to 0
190  * (we just leave the existing values around, just like we do for
191  * regular per-column statistics).
192  */
193  if (stattarget == 0)
194  continue;
195 
196  /* evaluate expressions (if the statistics has any) */
197  data = make_build_data(onerel, stat, numrows, rows, stats, stattarget);
198 
199  /* compute statistic of each requested type */
200  foreach(lc2, stat->types)
201  {
202  char t = (char) lfirst_int(lc2);
203 
204  if (t == STATS_EXT_NDISTINCT)
205  ndistinct = statext_ndistinct_build(totalrows, data);
206  else if (t == STATS_EXT_DEPENDENCIES)
207  dependencies = statext_dependencies_build(data);
208  else if (t == STATS_EXT_MCV)
209  mcv = statext_mcv_build(data, totalrows, stattarget);
210  else if (t == STATS_EXT_EXPRESSIONS)
211  {
212  AnlExprData *exprdata;
213  int nexprs;
214 
215  /* should not happen, thanks to checks when defining stats */
216  if (!stat->exprs)
217  elog(ERROR, "requested expression stats, but there are no expressions");
218 
219  exprdata = build_expr_data(stat->exprs, stattarget);
220  nexprs = list_length(stat->exprs);
221 
222  compute_expr_stats(onerel, totalrows,
223  exprdata, nexprs,
224  rows, numrows);
225 
226  exprstats = serialize_expr_stats(exprdata, nexprs);
227  }
228  }
229 
230  /* store the statistics in the catalog */
231  statext_store(stat->statOid, ndistinct, dependencies, mcv, exprstats, stats);
232 
233  /* for reporting progress */
235  ++ext_cnt);
236 
237  /* free the build data (allocated as a single chunk) */
238  pfree(data);
239  }
240 
241  table_close(pg_stext, RowExclusiveLock);
242 
243  MemoryContextSwitchTo(oldcxt);
244  MemoryContextDelete(cxt);
245 }
#define NIL
Definition: pg_list.h:65
#define PROGRESS_ANALYZE_EXT_STATS_COMPUTED
Definition: progress.h:42
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define PROGRESS_ANALYZE_PHASE
Definition: progress.h:38
static int statext_compute_stattarget(int stattarget, int natts, VacAttrStats **stats)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
static List * fetch_statentries_for_relation(Relation pg_statext, Oid relid)
Form_pg_class rd_rel
Definition: rel.h:110
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
Bitmapset * columns
Definition: type.h:89
void pfree(void *pointer)
Definition: mcxt.c:1169
MVDependencies * statext_dependencies_build(StatsBuildData *data)
Definition: dependencies.c:355
#define ERROR
Definition: elog.h:46
#define lfirst_int(lc)
Definition: pg_list.h:170
#define PROGRESS_ANALYZE_EXT_STATS_TOTAL
Definition: progress.h:41
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define RowExclusiveLock
Definition: lockdefs.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:491
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3448
static void statext_store(Oid statOid, MVNDistinct *ndistinct, MVDependencies *dependencies, MCVList *mcv, Datum exprs, VacAttrStats **stats)
static StatsBuildData * make_build_data(Relation onerel, StatExtEntry *stat, int numrows, HeapTuple *rows, VacAttrStats **stats, int stattarget)
#define WARNING
Definition: elog.h:40
uintptr_t Datum
Definition: postgres.h:411
MVNDistinct * statext_ndistinct_build(double totalrows, StatsBuildData *data)
Definition: mvdistinct.c:89
#define ereport(elevel,...)
Definition: elog.h:157
#define PROGRESS_ANALYZE_PHASE_COMPUTE_EXT_STATS
Definition: progress.h:51
static AnlExprData * build_expr_data(List *exprs, int stattarget)
#define lfirst(lc)
Definition: pg_list.h:169
void pgstat_progress_update_param(int index, int64 val)
MCVList * statext_mcv_build(StatsBuildData *data, double totalrows, int stattarget)
Definition: mcv.c:184
static int list_length(const List *l)
Definition: pg_list.h:149
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
static void compute_expr_stats(Relation onerel, double totalrows, AnlExprData *exprdata, int nexprs, HeapTuple *rows, int numrows)
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
int errtable(Relation rel)
Definition: relcache.c:5534
#define RelationGetRelid(relation)
Definition: rel.h:457
long val
Definition: informix.c:664
static VacAttrStats ** lookup_var_attr_stats(Relation rel, Bitmapset *attrs, List *exprs, int nvacatts, VacAttrStats **vacatts)
static Datum serialize_expr_stats(AnlExprData *exprdata, int nexprs)

◆ choose_best_statistics()

StatisticExtInfo* choose_best_statistics ( List stats,
char  requiredkind,
Bitmapset **  clause_attnums,
List **  clause_exprs,
int  nclauses 
)

Definition at line 1233 of file extended_stats.c.

References bms_add_members(), bms_free(), bms_is_subset(), bms_num_members(), StatisticExtInfo::exprs, i, StatisticExtInfo::keys, StatisticExtInfo::kind, lfirst, list_length(), stat_covers_expressions(), and STATS_MAX_DIMENSIONS.

Referenced by statext_mcv_clauselist_selectivity().

1236 {
1237  ListCell *lc;
1238  StatisticExtInfo *best_match = NULL;
1239  int best_num_matched = 2; /* goal #1: maximize */
1240  int best_match_keys = (STATS_MAX_DIMENSIONS + 1); /* goal #2: minimize */
1241 
1242  foreach(lc, stats)
1243  {
1244  int i;
1245  StatisticExtInfo *info = (StatisticExtInfo *) lfirst(lc);
1246  Bitmapset *matched_attnums = NULL;
1247  Bitmapset *matched_exprs = NULL;
1248  int num_matched;
1249  int numkeys;
1250 
1251  /* skip statistics that are not of the correct type */
1252  if (info->kind != requiredkind)
1253  continue;
1254 
1255  /*
1256  * Collect attributes and expressions in remaining (unestimated)
1257  * clauses fully covered by this statistic object.
1258  *
1259  * We know already estimated clauses have both clause_attnums and
1260  * clause_exprs set to NULL. We leave the pointers NULL if already
1261  * estimated, or we reset them to NULL after estimating the clause.
1262  */
1263  for (i = 0; i < nclauses; i++)
1264  {
1265  Bitmapset *expr_idxs = NULL;
1266 
1267  /* ignore incompatible/estimated clauses */
1268  if (!clause_attnums[i] && !clause_exprs[i])
1269  continue;
1270 
1271  /* ignore clauses that are not covered by this object */
1272  if (!bms_is_subset(clause_attnums[i], info->keys) ||
1273  !stat_covers_expressions(info, clause_exprs[i], &expr_idxs))
1274  continue;
1275 
1276  /* record attnums and indexes of expressions covered */
1277  matched_attnums = bms_add_members(matched_attnums, clause_attnums[i]);
1278  matched_exprs = bms_add_members(matched_exprs, expr_idxs);
1279  }
1280 
1281  num_matched = bms_num_members(matched_attnums) + bms_num_members(matched_exprs);
1282 
1283  bms_free(matched_attnums);
1284  bms_free(matched_exprs);
1285 
1286  /*
1287  * save the actual number of keys in the stats so that we can choose
1288  * the narrowest stats with the most matching keys.
1289  */
1290  numkeys = bms_num_members(info->keys) + list_length(info->exprs);
1291 
1292  /*
1293  * Use this object when it increases the number of matched attributes
1294  * and expressions or when it matches the same number of attributes
1295  * and expressions but these stats have fewer keys than any previous
1296  * match.
1297  */
1298  if (num_matched > best_num_matched ||
1299  (num_matched == best_num_matched && numkeys < best_match_keys))
1300  {
1301  best_match = info;
1302  best_num_matched = num_matched;
1303  best_match_keys = numkeys;
1304  }
1305  }
1306 
1307  return best_match;
1308 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
static bool stat_covers_expressions(StatisticExtInfo *stat, List *exprs, Bitmapset **expr_idxs)
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
Bitmapset * keys
Definition: pathnodes.h:931
#define STATS_MAX_DIMENSIONS
Definition: statistics.h:19
int i
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793

◆ compare_datums_simple()

int compare_datums_simple ( Datum  a,
Datum  b,
SortSupport  ssup 
)

Definition at line 951 of file extended_stats.c.

References ApplySortComparator().

Referenced by compare_scalars_simple(), and statext_mcv_serialize().

952 {
953  return ApplySortComparator(a, false, b, false, ssup);
954 }
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
Definition: sortsupport.h:200

◆ compare_scalars_simple()

int compare_scalars_simple ( const void *  a,
const void *  b,
void *  arg 
)

Definition at line 943 of file extended_stats.c.

References compare_datums_simple().

Referenced by statext_mcv_serialize().

944 {
945  return compare_datums_simple(*(Datum *) a,
946  *(Datum *) b,
947  (SortSupport) arg);
948 }
int compare_datums_simple(Datum a, Datum b, SortSupport ssup)
uintptr_t Datum
Definition: postgres.h:411
void * arg

◆ compute_expr_stats()

static void compute_expr_stats ( Relation  onerel,
double  totalrows,
AnlExprData exprdata,
int  nexprs,
HeapTuple rows,
int  numrows 
)
static

Definition at line 2076 of file extended_stats.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, VacAttrStats::attr, VacAttrStats::attrtype, VacAttrStats::compute_stats, CreateExecutorState(), CurrentMemoryContext, datumCopy(), ExprContext::ecxt_scantuple, ExecDropSingleTupleTableSlot(), ExecEvalExprSwitchContext(), ExecPrepareExpr(), ExecStoreHeapTuple(), AnlExprData::expr, expr_fetch_func(), VacAttrStats::exprnulls, VacAttrStats::exprvals, FreeExecutorState(), get_attribute_options(), GetPerTupleExprContext, i, MakeSingleTupleTableSlot(), MemoryContextDelete(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), AttributeOpts::n_distinct, palloc(), RelationGetDescr, ResetExprContext, VacAttrStats::rowstride, VacAttrStats::stadistinct, TTSOpsHeapTuple, and AnlExprData::vacattrstat.

Referenced by BuildRelationExtStatistics().

2079 {
2080  MemoryContext expr_context,
2081  old_context;
2082  int ind,
2083  i;
2084 
2086  "Analyze Expression",
2088  old_context = MemoryContextSwitchTo(expr_context);
2089 
2090  for (ind = 0; ind < nexprs; ind++)
2091  {
2092  AnlExprData *thisdata = &exprdata[ind];
2093  VacAttrStats *stats = thisdata->vacattrstat;
2094  Node *expr = thisdata->expr;
2095  TupleTableSlot *slot;
2096  EState *estate;
2097  ExprContext *econtext;
2098  Datum *exprvals;
2099  bool *exprnulls;
2100  ExprState *exprstate;
2101  int tcnt;
2102 
2103  /* Are we still in the main context? */
2104  Assert(CurrentMemoryContext == expr_context);
2105 
2106  /*
2107  * Need an EState for evaluation of expressions. Create it in the
2108  * per-expression context to be sure it gets cleaned up at the bottom
2109  * of the loop.
2110  */
2111  estate = CreateExecutorState();
2112  econtext = GetPerTupleExprContext(estate);
2113 
2114  /* Set up expression evaluation state */
2115  exprstate = ExecPrepareExpr((Expr *) expr, estate);
2116 
2117  /* Need a slot to hold the current heap tuple, too */
2119  &TTSOpsHeapTuple);
2120 
2121  /* Arrange for econtext's scan tuple to be the tuple under test */
2122  econtext->ecxt_scantuple = slot;
2123 
2124  /* Compute and save expression values */
2125  exprvals = (Datum *) palloc(numrows * sizeof(Datum));
2126  exprnulls = (bool *) palloc(numrows * sizeof(bool));
2127 
2128  tcnt = 0;
2129  for (i = 0; i < numrows; i++)
2130  {
2131  Datum datum;
2132  bool isnull;
2133 
2134  /*
2135  * Reset the per-tuple context each time, to reclaim any cruft
2136  * left behind by evaluating the statistics expressions.
2137  */
2138  ResetExprContext(econtext);
2139 
2140  /* Set up for expression evaluation */
2141  ExecStoreHeapTuple(rows[i], slot, false);
2142 
2143  /*
2144  * Evaluate the expression. We do this in the per-tuple context so
2145  * as not to leak memory, and then copy the result into the
2146  * context created at the beginning of this function.
2147  */
2148  datum = ExecEvalExprSwitchContext(exprstate,
2149  GetPerTupleExprContext(estate),
2150  &isnull);
2151  if (isnull)
2152  {
2153  exprvals[tcnt] = (Datum) 0;
2154  exprnulls[tcnt] = true;
2155  }
2156  else
2157  {
2158  /* Make sure we copy the data into the context. */
2159  Assert(CurrentMemoryContext == expr_context);
2160 
2161  exprvals[tcnt] = datumCopy(datum,
2162  stats->attrtype->typbyval,
2163  stats->attrtype->typlen);
2164  exprnulls[tcnt] = false;
2165  }
2166 
2167  tcnt++;
2168  }
2169 
2170  /*
2171  * Now we can compute the statistics for the expression columns.
2172  *
2173  * XXX Unlike compute_index_stats we don't need to switch and reset
2174  * memory contexts here, because we're only computing stats for a
2175  * single expression (and not iterating over many indexes), so we just
2176  * do it in expr_context. Note that compute_stats copies the result
2177  * into stats->anl_context, so it does not disappear.
2178  */
2179  if (tcnt > 0)
2180  {
2181  AttributeOpts *aopt =
2182  get_attribute_options(stats->attr->attrelid,
2183  stats->attr->attnum);
2184 
2185  stats->exprvals = exprvals;
2186  stats->exprnulls = exprnulls;
2187  stats->rowstride = 1;
2188  stats->compute_stats(stats,
2190  tcnt,
2191  tcnt);
2192 
2193  /*
2194  * If the n_distinct option is specified, it overrides the above
2195  * computation.
2196  */
2197  if (aopt != NULL && aopt->n_distinct != 0.0)
2198  stats->stadistinct = aopt->n_distinct;
2199  }
2200 
2201  /* And clean up */
2202  MemoryContextSwitchTo(expr_context);
2203 
2205  FreeExecutorState(estate);
2207  }
2208 
2209  MemoryContextSwitchTo(old_context);
2210  MemoryContextDelete(expr_context);
2211 }
AttributeOpts * get_attribute_options(Oid attrelid, int attnum)
Definition: attoptcache.c:103
int rowstride
Definition: vacuum.h:174
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:330
#define RelationGetDescr(relation)
Definition: rel.h:483
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:539
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execExpr.c:692
float8 n_distinct
Definition: attoptcache.h:22
Form_pg_attribute attr
Definition: vacuum.h:123
void FreeExecutorState(EState *estate)
Definition: execUtils.c:186
#define GetPerTupleExprContext(estate)
Definition: executor.h:532
bool * exprnulls
Definition: vacuum.h:173
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
EState * CreateExecutorState(void)
Definition: execUtils.c:90
Datum * exprvals
Definition: vacuum.h:172
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
uintptr_t Datum
Definition: postgres.h:411
static Datum expr_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull)
#define Assert(condition)
Definition: c.h:804
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:226
VacAttrStats * vacattrstat
Form_pg_type attrtype
Definition: vacuum.h:126
void * palloc(Size size)
Definition: mcxt.c:1062
int i
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
AnalyzeAttrComputeStatsFunc compute_stats
Definition: vacuum.h:134
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1352
#define ResetExprContext(econtext)
Definition: executor.h:526
float4 stadistinct
Definition: vacuum.h:145

◆ ComputeExtStatisticsRows()

int ComputeExtStatisticsRows ( Relation  onerel,
int  natts,
VacAttrStats **  vacattrstats 
)

Definition at line 261 of file extended_stats.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, bms_num_members(), StatExtEntry::columns, CurrentMemoryContext, StatExtEntry::exprs, fetch_statentries_for_relation(), lfirst, lookup_var_attr_stats(), MemoryContextDelete(), MemoryContextSwitchTo(), RelationGetRelid, RowExclusiveLock, statext_compute_stattarget(), StatExtEntry::stattarget, table_close(), and table_open().

Referenced by do_analyze_rel().

263 {
264  Relation pg_stext;
265  ListCell *lc;
266  List *lstats;
267  MemoryContext cxt;
268  MemoryContext oldcxt;
269  int result = 0;
270 
271  /* If there are no columns to analyze, just return 0. */
272  if (!natts)
273  return 0;
274 
276  "ComputeExtStatisticsRows",
278  oldcxt = MemoryContextSwitchTo(cxt);
279 
280  pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
281  lstats = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
282 
283  foreach(lc, lstats)
284  {
286  int stattarget;
287  VacAttrStats **stats;
288  int nattrs = bms_num_members(stat->columns);
289 
290  /*
291  * Check if we can build this statistics object based on the columns
292  * analyzed. If not, ignore it (don't report anything, we'll do that
293  * during the actual build BuildRelationExtStatistics).
294  */
295  stats = lookup_var_attr_stats(onerel, stat->columns, stat->exprs,
296  natts, vacattrstats);
297 
298  if (!stats)
299  continue;
300 
301  /*
302  * Compute statistics target, based on what's set for the statistic
303  * object itself, and for its attributes.
304  */
305  stattarget = statext_compute_stattarget(stat->stattarget,
306  nattrs, stats);
307 
308  /* Use the largest value for all statistics objects. */
309  if (stattarget > result)
310  result = stattarget;
311  }
312 
313  table_close(pg_stext, RowExclusiveLock);
314 
315  MemoryContextSwitchTo(oldcxt);
316  MemoryContextDelete(cxt);
317 
318  /* compute sample size based on the statistics target */
319  return (300 * result);
320 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static int statext_compute_stattarget(int stattarget, int natts, VacAttrStats **stats)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static List * fetch_statentries_for_relation(Relation pg_statext, Oid relid)
Bitmapset * columns
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
#define RowExclusiveLock
Definition: lockdefs.h:38
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
int result
Definition: header.h:19
#define lfirst(lc)
Definition: pg_list.h:169
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:457
static VacAttrStats ** lookup_var_attr_stats(Relation rel, Bitmapset *attrs, List *exprs, int nvacatts, VacAttrStats **vacatts)

◆ examine_attribute()

static VacAttrStats* examine_attribute ( Node expr)
static

Definition at line 523 of file extended_stats.c.

References VacAttrStats::anl_context, VacAttrStats::attr, VacAttrStats::attrcollid, ATTRIBUTE_FIXED_PART_SIZE, VacAttrStats::attrtype, VacAttrStats::attrtypid, VacAttrStats::attrtypmod, VacAttrStats::compute_stats, DatumGetBool, elog, ERROR, exprCollation(), exprType(), exprTypmod(), GETSTRUCT, heap_freetuple(), HeapTupleIsValid, i, InvalidAttrNumber, VacAttrStats::minrows, ObjectIdGetDatum, OidFunctionCall1, OidIsValid, palloc0(), pfree(), PointerGetDatum, SearchSysCacheCopy1, STATISTIC_NUM_SLOTS, VacAttrStats::statypalign, VacAttrStats::statypbyval, VacAttrStats::statypid, VacAttrStats::statyplen, std_typanalyze(), VacAttrStats::tupattnum, and TYPEOID.

Referenced by lookup_var_attr_stats().

524 {
525  HeapTuple typtuple;
526  VacAttrStats *stats;
527  int i;
528  bool ok;
529 
530  /*
531  * Create the VacAttrStats struct. Note that we only have a copy of the
532  * fixed fields of the pg_attribute tuple.
533  */
534  stats = (VacAttrStats *) palloc0(sizeof(VacAttrStats));
535 
536  /* fake the attribute */
538  stats->attr->attstattarget = -1;
539 
540  /*
541  * When analyzing an expression, believe the expression tree's type not
542  * the column datatype --- the latter might be the opckeytype storage
543  * type of the opclass, which is not interesting for our purposes. (Note:
544  * if we did anything with non-expression statistics columns, we'd need to
545  * figure out where to get the correct type info from, but for now that's
546  * not a problem.) It's not clear whether anyone will care about the
547  * typmod, but we store that too just in case.
548  */
549  stats->attrtypid = exprType(expr);
550  stats->attrtypmod = exprTypmod(expr);
551  stats->attrcollid = exprCollation(expr);
552 
553  typtuple = SearchSysCacheCopy1(TYPEOID,
554  ObjectIdGetDatum(stats->attrtypid));
555  if (!HeapTupleIsValid(typtuple))
556  elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
557  stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
558 
559  /*
560  * We don't actually analyze individual attributes, so no need to set the
561  * memory context.
562  */
563  stats->anl_context = NULL;
564  stats->tupattnum = InvalidAttrNumber;
565 
566  /*
567  * The fields describing the stats->stavalues[n] element types default to
568  * the type of the data being analyzed, but the type-specific typanalyze
569  * function can change them if it wants to store something else.
570  */
571  for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
572  {
573  stats->statypid[i] = stats->attrtypid;
574  stats->statyplen[i] = stats->attrtype->typlen;
575  stats->statypbyval[i] = stats->attrtype->typbyval;
576  stats->statypalign[i] = stats->attrtype->typalign;
577  }
578 
579  /*
580  * Call the type-specific typanalyze function. If none is specified, use
581  * std_typanalyze().
582  */
583  if (OidIsValid(stats->attrtype->typanalyze))
584  ok = DatumGetBool(OidFunctionCall1(stats->attrtype->typanalyze,
585  PointerGetDatum(stats)));
586  else
587  ok = std_typanalyze(stats);
588 
589  if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
590  {
591  heap_freetuple(typtuple);
592  pfree(stats->attr);
593  pfree(stats);
594  return NULL;
595  }
596 
597  return stats;
598 }
int minrows
Definition: vacuum.h:135
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
#define PointerGetDatum(X)
Definition: postgres.h:600
int tupattnum
Definition: vacuum.h:169
bool std_typanalyze(VacAttrStats *stats)
Definition: analyze.c:1873
bool statypbyval[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:162
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
int32 attrtypmod
Definition: vacuum.h:125
#define OidIsValid(objectId)
Definition: c.h:710
char statypalign[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:163
Form_pg_attribute attr
Definition: vacuum.h:123
void pfree(void *pointer)
Definition: mcxt.c:1169
Oid attrtypid
Definition: vacuum.h:124
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:664
#define DatumGetBool(X)
Definition: postgres.h:437
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
#define ATTRIBUTE_FIXED_PART_SIZE
Definition: pg_attribute.h:195
void * palloc0(Size size)
Definition: mcxt.c:1093
Oid statypid[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:160
#define STATISTIC_NUM_SLOTS
Definition: pg_statistic.h:127
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
Oid attrcollid
Definition: vacuum.h:127
MemoryContext anl_context
Definition: vacuum.h:128
#define InvalidAttrNumber
Definition: attnum.h:23
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
Form_pg_type attrtype
Definition: vacuum.h:126
int16 statyplen[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:161
#define elog(elevel,...)
Definition: elog.h:232
int i
AnalyzeAttrComputeStatsFunc compute_stats
Definition: vacuum.h:134

◆ examine_expression()

static VacAttrStats* examine_expression ( Node expr,
int  stattarget 
)
static

Definition at line 607 of file extended_stats.c.

References VacAttrStats::anl_context, Assert, VacAttrStats::attr, VacAttrStats::attrcollid, ATTRIBUTE_FIXED_PART_SIZE, VacAttrStats::attrtype, VacAttrStats::attrtypid, VacAttrStats::attrtypmod, VacAttrStats::compute_stats, CurrentMemoryContext, DatumGetBool, elog, ERROR, exprCollation(), exprType(), exprTypmod(), GETSTRUCT, heap_freetuple(), HeapTupleIsValid, i, InvalidAttrNumber, InvalidOid, VacAttrStats::minrows, ObjectIdGetDatum, OidFunctionCall1, OidIsValid, palloc(), palloc0(), pfree(), PointerGetDatum, SearchSysCacheCopy1, STATISTIC_NUM_SLOTS, StatExtEntry::stattarget, VacAttrStats::statypalign, VacAttrStats::statypbyval, VacAttrStats::statypid, VacAttrStats::statyplen, std_typanalyze(), VacAttrStats::tupattnum, and TYPEOID.

Referenced by build_expr_data(), and make_build_data().

608 {
609  HeapTuple typtuple;
610  VacAttrStats *stats;
611  int i;
612  bool ok;
613 
614  Assert(expr != NULL);
615 
616  /*
617  * Create the VacAttrStats struct.
618  */
619  stats = (VacAttrStats *) palloc0(sizeof(VacAttrStats));
620 
621  /*
622  * When analyzing an expression, believe the expression tree's type.
623  */
624  stats->attrtypid = exprType(expr);
625  stats->attrtypmod = exprTypmod(expr);
626 
627  /*
628  * We don't allow collation to be specified in CREATE STATISTICS, so we
629  * have to use the collation specified for the expression. It's possible
630  * to specify the collation in the expression "(col COLLATE "en_US")" in
631  * which case exprCollation() does the right thing.
632  */
633  stats->attrcollid = exprCollation(expr);
634 
635  /*
636  * We don't have any pg_attribute for expressions, so let's fake something
637  * reasonable into attstattarget, which is the only thing std_typanalyze
638  * needs.
639  */
641 
642  /*
643  * We can't have statistics target specified for the expression, so we
644  * could use either the default_statistics_target, or the target computed
645  * for the extended statistics. The second option seems more reasonable.
646  */
647  stats->attr->attstattarget = stattarget;
648 
649  /* initialize some basic fields */
650  stats->attr->attrelid = InvalidOid;
651  stats->attr->attnum = InvalidAttrNumber;
652  stats->attr->atttypid = stats->attrtypid;
653 
654  typtuple = SearchSysCacheCopy1(TYPEOID,
655  ObjectIdGetDatum(stats->attrtypid));
656  if (!HeapTupleIsValid(typtuple))
657  elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
658 
659  stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
660  stats->anl_context = CurrentMemoryContext; /* XXX should be using
661  * something else? */
662  stats->tupattnum = InvalidAttrNumber;
663 
664  /*
665  * The fields describing the stats->stavalues[n] element types default to
666  * the type of the data being analyzed, but the type-specific typanalyze
667  * function can change them if it wants to store something else.
668  */
669  for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
670  {
671  stats->statypid[i] = stats->attrtypid;
672  stats->statyplen[i] = stats->attrtype->typlen;
673  stats->statypbyval[i] = stats->attrtype->typbyval;
674  stats->statypalign[i] = stats->attrtype->typalign;
675  }
676 
677  /*
678  * Call the type-specific typanalyze function. If none is specified, use
679  * std_typanalyze().
680  */
681  if (OidIsValid(stats->attrtype->typanalyze))
682  ok = DatumGetBool(OidFunctionCall1(stats->attrtype->typanalyze,
683  PointerGetDatum(stats)));
684  else
685  ok = std_typanalyze(stats);
686 
687  if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
688  {
689  heap_freetuple(typtuple);
690  pfree(stats);
691  return NULL;
692  }
693 
694  return stats;
695 }
int minrows
Definition: vacuum.h:135
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
#define PointerGetDatum(X)
Definition: postgres.h:600
int tupattnum
Definition: vacuum.h:169
bool std_typanalyze(VacAttrStats *stats)
Definition: analyze.c:1873
bool statypbyval[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:162
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
int32 attrtypmod
Definition: vacuum.h:125
#define OidIsValid(objectId)
Definition: c.h:710
char statypalign[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:163
Form_pg_attribute attr
Definition: vacuum.h:123
void pfree(void *pointer)
Definition: mcxt.c:1169
Oid attrtypid
Definition: vacuum.h:124
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:664
#define DatumGetBool(X)
Definition: postgres.h:437
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define ATTRIBUTE_FIXED_PART_SIZE
Definition: pg_attribute.h:195
void * palloc0(Size size)
Definition: mcxt.c:1093
Oid statypid[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:160
#define InvalidOid
Definition: postgres_ext.h:36
#define STATISTIC_NUM_SLOTS
Definition: pg_statistic.h:127
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
Oid attrcollid
Definition: vacuum.h:127
MemoryContext anl_context
Definition: vacuum.h:128
#define InvalidAttrNumber
Definition: attnum.h:23
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
Form_pg_type attrtype
Definition: vacuum.h:126
void * palloc(Size size)
Definition: mcxt.c:1062
int16 statyplen[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:161
#define elog(elevel,...)
Definition: elog.h:232
int i
AnalyzeAttrComputeStatsFunc compute_stats
Definition: vacuum.h:134

◆ examine_opclause_args()

bool examine_opclause_args ( List args,
Node **  exprp,
Const **  cstp,
bool expronleftp 
)

Definition at line 2021 of file extended_stats.c.

References arg, Assert, IsA, linitial, list_length(), and lsecond.

Referenced by mcv_get_match_bitmap(), and statext_is_compatible_clause_internal().

2023 {
2024  Node *expr;
2025  Const *cst;
2026  bool expronleft;
2027  Node *leftop,
2028  *rightop;
2029 
2030  /* enforced by statext_is_compatible_clause_internal */
2031  Assert(list_length(args) == 2);
2032 
2033  leftop = linitial(args);
2034  rightop = lsecond(args);
2035 
2036  /* strip RelabelType from either side of the expression */
2037  if (IsA(leftop, RelabelType))
2038  leftop = (Node *) ((RelabelType *) leftop)->arg;
2039 
2040  if (IsA(rightop, RelabelType))
2041  rightop = (Node *) ((RelabelType *) rightop)->arg;
2042 
2043  if (IsA(rightop, Const))
2044  {
2045  expr = (Node *) leftop;
2046  cst = (Const *) rightop;
2047  expronleft = true;
2048  }
2049  else if (IsA(leftop, Const))
2050  {
2051  expr = (Node *) rightop;
2052  cst = (Const *) leftop;
2053  expronleft = false;
2054  }
2055  else
2056  return false;
2057 
2058  /* return pointers to the extracted parts if requested */
2059  if (exprp)
2060  *exprp = expr;
2061 
2062  if (cstp)
2063  *cstp = cst;
2064 
2065  if (expronleftp)
2066  *expronleftp = expronleft;
2067 
2068  return true;
2069 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Definition: nodes.h:539
#define lsecond(l)
Definition: pg_list.h:179
#define linitial(l)
Definition: pg_list.h:174
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
void * arg

◆ expr_fetch_func()

static Datum expr_fetch_func ( VacAttrStatsP  stats,
int  rownum,
bool isNull 
)
static

Definition at line 2221 of file extended_stats.c.

References VacAttrStats::exprnulls, VacAttrStats::exprvals, i, and VacAttrStats::rowstride.

Referenced by compute_expr_stats().

2222 {
2223  int i;
2224 
2225  /* exprvals and exprnulls are already offset for proper column */
2226  i = rownum * stats->rowstride;
2227  *isNull = stats->exprnulls[i];
2228  return stats->exprvals[i];
2229 }
int rowstride
Definition: vacuum.h:174
bool * exprnulls
Definition: vacuum.h:173
Datum * exprvals
Definition: vacuum.h:172
int i

◆ fetch_statentries_for_relation()

static List * fetch_statentries_for_relation ( Relation  pg_statext,
Oid  relid 
)
static

Definition at line 418 of file extended_stats.c.

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert, bms_add_member(), BTEqualStrategyNumber, StatExtEntry::columns, DatumGetArrayTypeP, elog, ERROR, eval_const_expressions(), StatExtEntry::exprs, fix_opfuncids(), get_namespace_name(), GETSTRUCT, HeapTupleIsValid, i, lappend(), lappend_int(), StatExtEntry::name, NameStr, NIL, ObjectIdGetDatum, palloc0(), pfree(), pstrdup(), ScanKeyInit(), StatExtEntry::schema, STATEXTOID, StatisticExtRelidIndexId, StatExtEntry::statOid, StatExtEntry::stattarget, stringToNode(), SysCacheGetAttr(), systable_beginscan(), systable_endscan(), systable_getnext(), TextDatumGetCString, and StatExtEntry::types.

Referenced by BuildRelationExtStatistics(), and ComputeExtStatisticsRows().

419 {
420  SysScanDesc scan;
421  ScanKeyData skey;
422  HeapTuple htup;
423  List *result = NIL;
424 
425  /*
426  * Prepare to scan pg_statistic_ext for entries having stxrelid = this
427  * rel.
428  */
429  ScanKeyInit(&skey,
430  Anum_pg_statistic_ext_stxrelid,
431  BTEqualStrategyNumber, F_OIDEQ,
432  ObjectIdGetDatum(relid));
433 
434  scan = systable_beginscan(pg_statext, StatisticExtRelidIndexId, true,
435  NULL, 1, &skey);
436 
437  while (HeapTupleIsValid(htup = systable_getnext(scan)))
438  {
439  StatExtEntry *entry;
440  Datum datum;
441  bool isnull;
442  int i;
443  ArrayType *arr;
444  char *enabled;
445  Form_pg_statistic_ext staForm;
446  List *exprs = NIL;
447 
448  entry = palloc0(sizeof(StatExtEntry));
449  staForm = (Form_pg_statistic_ext) GETSTRUCT(htup);
450  entry->statOid = staForm->oid;
451  entry->schema = get_namespace_name(staForm->stxnamespace);
452  entry->name = pstrdup(NameStr(staForm->stxname));
453  entry->stattarget = staForm->stxstattarget;
454  for (i = 0; i < staForm->stxkeys.dim1; i++)
455  {
456  entry->columns = bms_add_member(entry->columns,
457  staForm->stxkeys.values[i]);
458  }
459 
460  /* decode the stxkind char array into a list of chars */
461  datum = SysCacheGetAttr(STATEXTOID, htup,
462  Anum_pg_statistic_ext_stxkind, &isnull);
463  Assert(!isnull);
464  arr = DatumGetArrayTypeP(datum);
465  if (ARR_NDIM(arr) != 1 ||
466  ARR_HASNULL(arr) ||
467  ARR_ELEMTYPE(arr) != CHAROID)
468  elog(ERROR, "stxkind is not a 1-D char array");
469  enabled = (char *) ARR_DATA_PTR(arr);
470  for (i = 0; i < ARR_DIMS(arr)[0]; i++)
471  {
472  Assert((enabled[i] == STATS_EXT_NDISTINCT) ||
473  (enabled[i] == STATS_EXT_DEPENDENCIES) ||
474  (enabled[i] == STATS_EXT_MCV) ||
475  (enabled[i] == STATS_EXT_EXPRESSIONS));
476  entry->types = lappend_int(entry->types, (int) enabled[i]);
477  }
478 
479  /* decode expression (if any) */
480  datum = SysCacheGetAttr(STATEXTOID, htup,
481  Anum_pg_statistic_ext_stxexprs, &isnull);
482 
483  if (!isnull)
484  {
485  char *exprsString;
486 
487  exprsString = TextDatumGetCString(datum);
488  exprs = (List *) stringToNode(exprsString);
489 
490  pfree(exprsString);
491 
492  /*
493  * Run the expressions through eval_const_expressions. This is not
494  * just an optimization, but is necessary, because the planner
495  * will be comparing them to similarly-processed qual clauses, and
496  * may fail to detect valid matches without this. We must not use
497  * canonicalize_qual, however, since these aren't qual
498  * expressions.
499  */
500  exprs = (List *) eval_const_expressions(NULL, (Node *) exprs);
501 
502  /* May as well fix opfuncids too */
503  fix_opfuncids((Node *) exprs);
504  }
505 
506  entry->exprs = exprs;
507 
508  result = lappend(result, entry);
509  }
510 
511  systable_endscan(scan);
512 
513  return result;
514 }
#define NIL
Definition: pg_list.h:65
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1652
char * pstrdup(const char *in)
Definition: mcxt.c:1299
Definition: nodes.h:539
void * stringToNode(const char *str)
Definition: read.c:89
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2090
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:383
Bitmapset * columns
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_DATA_PTR(a)
Definition: array.h:315
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define ARR_HASNULL(a)
Definition: array.h:284
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
#define TextDatumGetCString(d)
Definition: builtins.h:83
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
int result
Definition: header.h:19
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define ARR_NDIM(a)
Definition: array.h:283
#define elog(elevel,...)
Definition: elog.h:232
int i
#define NameStr(name)
Definition: c.h:681
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define StatisticExtRelidIndexId
Definition: pg_list.h:50
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define BTEqualStrategyNumber
Definition: stratnum.h:31
FormData_pg_statistic_ext * Form_pg_statistic_ext
#define DatumGetArrayTypeP(X)
Definition: array.h:254

◆ has_stats_of_kind()

bool has_stats_of_kind ( List stats,
char  requiredkind 
)

Definition at line 1142 of file extended_stats.c.

References StatisticExtInfo::kind, and lfirst.

Referenced by dependencies_clauselist_selectivity(), and statext_mcv_clauselist_selectivity().

1143 {
1144  ListCell *l;
1145 
1146  foreach(l, stats)
1147  {
1149 
1150  if (stat->kind == requiredkind)
1151  return true;
1152  }
1153 
1154  return false;
1155 }
#define lfirst(lc)
Definition: pg_list.h:169

◆ lookup_var_attr_stats()

static VacAttrStats ** lookup_var_attr_stats ( Relation  rel,
Bitmapset attrs,
List exprs,
int  nvacatts,
VacAttrStats **  vacatts 
)
static

Definition at line 705 of file extended_stats.c.

References Assert, bms_next_member(), bms_num_members(), examine_attribute(), i, lfirst, list_length(), palloc(), pfree(), and VacAttrStats::tupDesc.

Referenced by BuildRelationExtStatistics(), and ComputeExtStatisticsRows().

707 {
708  int i = 0;
709  int x = -1;
710  int natts;
711  VacAttrStats **stats;
712  ListCell *lc;
713 
714  natts = bms_num_members(attrs) + list_length(exprs);
715 
716  stats = (VacAttrStats **) palloc(natts * sizeof(VacAttrStats *));
717 
718  /* lookup VacAttrStats info for the requested columns (same attnum) */
719  while ((x = bms_next_member(attrs, x)) >= 0)
720  {
721  int j;
722 
723  stats[i] = NULL;
724  for (j = 0; j < nvacatts; j++)
725  {
726  if (x == vacatts[j]->tupattnum)
727  {
728  stats[i] = vacatts[j];
729  break;
730  }
731  }
732 
733  if (!stats[i])
734  {
735  /*
736  * Looks like stats were not gathered for one of the columns
737  * required. We'll be unable to build the extended stats without
738  * this column.
739  */
740  pfree(stats);
741  return NULL;
742  }
743 
744  /*
745  * Sanity check that the column is not dropped - stats should have
746  * been removed in this case.
747  */
748  Assert(!stats[i]->attr->attisdropped);
749 
750  i++;
751  }
752 
753  /* also add info for expressions */
754  foreach(lc, exprs)
755  {
756  Node *expr = (Node *) lfirst(lc);
757 
758  stats[i] = examine_attribute(expr);
759 
760  /*
761  * XXX We need tuple descriptor later, and we just grab it from
762  * stats[0]->tupDesc (see e.g. statext_mcv_build). But as coded
763  * examine_attribute does not set that, so just grab it from the first
764  * vacatts element.
765  */
766  stats[i]->tupDesc = vacatts[0]->tupDesc;
767 
768  i++;
769  }
770 
771  return stats;
772 }
static VacAttrStats * examine_attribute(Node *expr)
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Definition: nodes.h:539
TupleDesc tupDesc
Definition: vacuum.h:171
void pfree(void *pointer)
Definition: mcxt.c:1169
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
void * palloc(Size size)
Definition: mcxt.c:1062
int i

◆ make_build_data()

static StatsBuildData * make_build_data ( Relation  onerel,
StatExtEntry stat,
int  numrows,
HeapTuple rows,
VacAttrStats **  stats,
int  stattarget 
)
static

Definition at line 2440 of file extended_stats.c.

References Assert, StatsBuildData::attnums, bms_next_member(), bms_num_members(), StatExtEntry::columns, CreateExecutorState(), ExprContext::ecxt_scantuple, examine_expression(), ExecDropSingleTupleTableSlot(), ExecEvalExpr(), ExecPrepareExprList(), ExecStoreHeapTuple(), StatExtEntry::exprs, FreeExecutorState(), GetPerTupleExprContext, heap_getattr, i, idx(), lfirst, list_length(), MakeSingleTupleTableSlot(), MAXALIGN, NIL, StatsBuildData::nulls, palloc(), RelationGetDescr, ResetExprContext, StatsBuildData::stats, TTSOpsHeapTuple, and StatsBuildData::values.

Referenced by BuildRelationExtStatistics().

2442 {
2443  /* evaluated expressions */
2445  char *ptr;
2446  Size len;
2447 
2448  int i;
2449  int k;
2450  int idx;
2451  TupleTableSlot *slot;
2452  EState *estate;
2453  ExprContext *econtext;
2454  List *exprstates = NIL;
2455  int nkeys = bms_num_members(stat->columns) + list_length(stat->exprs);
2456  ListCell *lc;
2457 
2458  /* allocate everything as a single chunk, so we can free it easily */
2459  len = MAXALIGN(sizeof(StatsBuildData));
2460  len += MAXALIGN(sizeof(AttrNumber) * nkeys); /* attnums */
2461  len += MAXALIGN(sizeof(VacAttrStats *) * nkeys); /* stats */
2462 
2463  /* values */
2464  len += MAXALIGN(sizeof(Datum *) * nkeys);
2465  len += nkeys * MAXALIGN(sizeof(Datum) * numrows);
2466 
2467  /* nulls */
2468  len += MAXALIGN(sizeof(bool *) * nkeys);
2469  len += nkeys * MAXALIGN(sizeof(bool) * numrows);
2470 
2471  ptr = palloc(len);
2472 
2473  /* set the pointers */
2474  result = (StatsBuildData *) ptr;
2475  ptr += MAXALIGN(sizeof(StatsBuildData));
2476 
2477  /* attnums */
2478  result->attnums = (AttrNumber *) ptr;
2479  ptr += MAXALIGN(sizeof(AttrNumber) * nkeys);
2480 
2481  /* stats */
2482  result->stats = (VacAttrStats **) ptr;
2483  ptr += MAXALIGN(sizeof(VacAttrStats *) * nkeys);
2484 
2485  /* values */
2486  result->values = (Datum **) ptr;
2487  ptr += MAXALIGN(sizeof(Datum *) * nkeys);
2488 
2489  /* nulls */
2490  result->nulls = (bool **) ptr;
2491  ptr += MAXALIGN(sizeof(bool *) * nkeys);
2492 
2493  for (i = 0; i < nkeys; i++)
2494  {
2495  result->values[i] = (Datum *) ptr;
2496  ptr += MAXALIGN(sizeof(Datum) * numrows);
2497 
2498  result->nulls[i] = (bool *) ptr;
2499  ptr += MAXALIGN(sizeof(bool) * numrows);
2500  }
2501 
2502  Assert((ptr - (char *) result) == len);
2503 
2504  /* we have it allocated, so let's fill the values */
2505  result->nattnums = nkeys;
2506  result->numrows = numrows;
2507 
2508  /* fill the attribute info - first attributes, then expressions */
2509  idx = 0;
2510  k = -1;
2511  while ((k = bms_next_member(stat->columns, k)) >= 0)
2512  {
2513  result->attnums[idx] = k;
2514  result->stats[idx] = stats[idx];
2515 
2516  idx++;
2517  }
2518 
2519  k = -1;
2520  foreach(lc, stat->exprs)
2521  {
2522  Node *expr = (Node *) lfirst(lc);
2523 
2524  result->attnums[idx] = k;
2525  result->stats[idx] = examine_expression(expr, stattarget);
2526 
2527  idx++;
2528  k--;
2529  }
2530 
2531  /* first extract values for all the regular attributes */
2532  for (i = 0; i < numrows; i++)
2533  {
2534  idx = 0;
2535  k = -1;
2536  while ((k = bms_next_member(stat->columns, k)) >= 0)
2537  {
2538  result->values[idx][i] = heap_getattr(rows[i], k,
2539  result->stats[idx]->tupDesc,
2540  &result->nulls[idx][i]);
2541 
2542  idx++;
2543  }
2544  }
2545 
2546  /* Need an EState for evaluation expressions. */
2547  estate = CreateExecutorState();
2548  econtext = GetPerTupleExprContext(estate);
2549 
2550  /* Need a slot to hold the current heap tuple, too */
2552  &TTSOpsHeapTuple);
2553 
2554  /* Arrange for econtext's scan tuple to be the tuple under test */
2555  econtext->ecxt_scantuple = slot;
2556 
2557  /* Set up expression evaluation state */
2558  exprstates = ExecPrepareExprList(stat->exprs, estate);
2559 
2560  for (i = 0; i < numrows; i++)
2561  {
2562  /*
2563  * Reset the per-tuple context each time, to reclaim any cruft left
2564  * behind by evaluating the statistics object expressions.
2565  */
2566  ResetExprContext(econtext);
2567 
2568  /* Set up for expression evaluation */
2569  ExecStoreHeapTuple(rows[i], slot, false);
2570 
2571  idx = bms_num_members(stat->columns);
2572  foreach(lc, exprstates)
2573  {
2574  Datum datum;
2575  bool isnull;
2576  ExprState *exprstate = (ExprState *) lfirst(lc);
2577 
2578  /*
2579  * XXX This probably leaks memory. Maybe we should use
2580  * ExecEvalExprSwitchContext but then we need to copy the result
2581  * somewhere else.
2582  */
2583  datum = ExecEvalExpr(exprstate,
2584  GetPerTupleExprContext(estate),
2585  &isnull);
2586  if (isnull)
2587  {
2588  result->values[idx][i] = (Datum) 0;
2589  result->nulls[idx][i] = true;
2590  }
2591  else
2592  {
2593  result->values[idx][i] = (Datum) datum;
2594  result->nulls[idx][i] = false;
2595  }
2596 
2597  idx++;
2598  }
2599  }
2600 
2602  FreeExecutorState(estate);
2603 
2604  return result;
2605 }
static VacAttrStats * examine_expression(Node *expr, int stattarget)
#define NIL
Definition: pg_list.h:65
#define RelationGetDescr(relation)
Definition: rel.h:483
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Definition: nodes.h:539
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
TupleDesc tupDesc
Definition: vacuum.h:171
Bitmapset * columns
void FreeExecutorState(EState *estate)
Definition: execUtils.c:186
#define GetPerTupleExprContext(estate)
Definition: executor.h:532
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:315
EState * CreateExecutorState(void)
Definition: execUtils.c:90
List * ExecPrepareExprList(List *nodes, EState *estate)
Definition: execExpr.c:766
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
uintptr_t Datum
Definition: postgres.h:411
VacAttrStats ** stats
int result
Definition: header.h:19
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
size_t Size
Definition: c.h:540
static int list_length(const List *l)
Definition: pg_list.h:149
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:226
#define MAXALIGN(LEN)
Definition: c.h:757
void * palloc(Size size)
Definition: mcxt.c:1062
int i
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1352
#define ResetExprContext(econtext)
Definition: executor.h:526

◆ multi_sort_add_dimension()

void multi_sort_add_dimension ( MultiSortSupport  mss,
int  sortdim,
Oid  oper,
Oid  collation 
)

Definition at line 875 of file extended_stats.c.

References CurrentMemoryContext, PrepareSortSupportFromOrderingOp(), MultiSortSupportData::ssup, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, and SortSupportData::ssup_nulls_first.

Referenced by build_mss(), dependency_degree(), and ndistinct_for_combination().

877 {
878  SortSupport ssup = &mss->ssup[sortdim];
879 
881  ssup->ssup_collation = collation;
882  ssup->ssup_nulls_first = false;
883 
885 }
bool ssup_nulls_first
Definition: sortsupport.h:75
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Definition: sortsupport.c:135
SortSupportData ssup[FLEXIBLE_ARRAY_MEMBER]
MemoryContext ssup_cxt
Definition: sortsupport.h:66
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition: parse_oper.c:382

◆ multi_sort_compare()

int multi_sort_compare ( const void *  a,
const void *  b,
void *  arg 
)

Definition at line 889 of file extended_stats.c.

References ApplySortComparator(), compare(), i, SortItem::isnull, MultiSortSupportData::ndims, MultiSortSupportData::ssup, and SortItem::values.

Referenced by build_distinct_groups(), build_sorted_items(), count_distinct_groups(), ndistinct_for_combination(), and statext_mcv_build().

890 {
892  SortItem *ia = (SortItem *) a;
893  SortItem *ib = (SortItem *) b;
894  int i;
895 
896  for (i = 0; i < mss->ndims; i++)
897  {
898  int compare;
899 
900  compare = ApplySortComparator(ia->values[i], ia->isnull[i],
901  ib->values[i], ib->isnull[i],
902  &mss->ssup[i]);
903 
904  if (compare != 0)
905  return compare;
906  }
907 
908  /* equal by default */
909  return 0;
910 }
static int compare(const void *arg1, const void *arg2)
Definition: geqo_pool.c:145
SortSupportData ssup[FLEXIBLE_ARRAY_MEMBER]
int i
void * arg
MultiSortSupportData * MultiSortSupport
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
Definition: sortsupport.h:200

◆ multi_sort_compare_dim()

int multi_sort_compare_dim ( int  dim,
const SortItem a,
const SortItem b,
MultiSortSupport  mss 
)

Definition at line 914 of file extended_stats.c.

References ApplySortComparator(), SortItem::isnull, MultiSortSupportData::ssup, and SortItem::values.

Referenced by dependency_degree().

916 {
917  return ApplySortComparator(a->values[dim], a->isnull[dim],
918  b->values[dim], b->isnull[dim],
919  &mss->ssup[dim]);
920 }
SortSupportData ssup[FLEXIBLE_ARRAY_MEMBER]
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
Definition: sortsupport.h:200

◆ multi_sort_compare_dims()

int multi_sort_compare_dims ( int  start,
int  end,
const SortItem a,
const SortItem b,
MultiSortSupport  mss 
)

Definition at line 923 of file extended_stats.c.

References ApplySortComparator(), SortItem::isnull, MultiSortSupportData::ssup, and SortItem::values.

Referenced by dependency_degree().

926 {
927  int dim;
928 
929  for (dim = start; dim <= end; dim++)
930  {
931  int r = ApplySortComparator(a->values[dim], a->isnull[dim],
932  b->values[dim], b->isnull[dim],
933  &mss->ssup[dim]);
934 
935  if (r != 0)
936  return r;
937  }
938 
939  return 0;
940 }
SortSupportData ssup[FLEXIBLE_ARRAY_MEMBER]
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
Definition: sortsupport.h:200

◆ multi_sort_init()

MultiSortSupport multi_sort_init ( int  ndims)

Definition at line 856 of file extended_stats.c.

References Assert, MultiSortSupportData::ndims, offsetof, and palloc0().

Referenced by build_mss(), dependency_degree(), and ndistinct_for_combination().

857 {
858  MultiSortSupport mss;
859 
860  Assert(ndims >= 2);
861 
863  + sizeof(SortSupportData) * ndims);
864 
865  mss->ndims = ndims;
866 
867  return mss;
868 }
struct SortSupportData SortSupportData
void * palloc0(Size size)
Definition: mcxt.c:1093
#define Assert(condition)
Definition: c.h:804
MultiSortSupportData * MultiSortSupport
#define offsetof(type, field)
Definition: c.h:727

◆ serialize_expr_stats()

static Datum serialize_expr_stats ( AnlExprData exprdata,
int  nexprs 
)
static

Definition at line 2262 of file extended_stats.c.

References accumArrayResult(), BoolGetDatum, construct_array(), CurrentMemoryContext, ereport, errcode(), errmsg(), ERROR, Float4GetDatum(), get_rel_type_id(), heap_copy_tuple_as_datum(), heap_form_tuple(), i, Int16GetDatum, Int32GetDatum, InvalidAttrNumber, InvalidOid, makeArrayResult(), VacAttrStats::numnumbers, VacAttrStats::numvalues, ObjectIdGetDatum, OidIsValid, palloc(), PointerGetDatum, RelationGetDescr, RowExclusiveLock, VacAttrStats::stacoll, VacAttrStats::stadistinct, VacAttrStats::stakind, VacAttrStats::stanullfrac, VacAttrStats::stanumbers, VacAttrStats::staop, STATISTIC_NUM_SLOTS, VacAttrStats::stats_valid, VacAttrStats::statypalign, VacAttrStats::statypbyval, VacAttrStats::statypid, VacAttrStats::statyplen, VacAttrStats::stavalues, VacAttrStats::stawidth, table_close(), table_open(), AnlExprData::vacattrstat, and values.

Referenced by BuildRelationExtStatistics().

2263 {
2264  int exprno;
2265  Oid typOid;
2266  Relation sd;
2267 
2268  ArrayBuildState *astate = NULL;
2269 
2270  sd = table_open(StatisticRelationId, RowExclusiveLock);
2271 
2272  /* lookup OID of composite type for pg_statistic */
2273  typOid = get_rel_type_id(StatisticRelationId);
2274  if (!OidIsValid(typOid))
2275  ereport(ERROR,
2276  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2277  errmsg("relation \"pg_statistic\" does not have a composite type")));
2278 
2279  for (exprno = 0; exprno < nexprs; exprno++)
2280  {
2281  int i,
2282  k;
2283  VacAttrStats *stats = exprdata[exprno].vacattrstat;
2284 
2285  Datum values[Natts_pg_statistic];
2286  bool nulls[Natts_pg_statistic];
2287  HeapTuple stup;
2288 
2289  if (!stats->stats_valid)
2290  {
2291  astate = accumArrayResult(astate,
2292  (Datum) 0,
2293  true,
2294  typOid,
2296  continue;
2297  }
2298 
2299  /*
2300  * Construct a new pg_statistic tuple
2301  */
2302  for (i = 0; i < Natts_pg_statistic; ++i)
2303  {
2304  nulls[i] = false;
2305  }
2306 
2307  values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(InvalidOid);
2308  values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(InvalidAttrNumber);
2309  values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(false);
2310  values[Anum_pg_statistic_stanullfrac - 1] = Float4GetDatum(stats->stanullfrac);
2311  values[Anum_pg_statistic_stawidth - 1] = Int32GetDatum(stats->stawidth);
2312  values[Anum_pg_statistic_stadistinct - 1] = Float4GetDatum(stats->stadistinct);
2313  i = Anum_pg_statistic_stakind1 - 1;
2314  for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2315  {
2316  values[i++] = Int16GetDatum(stats->stakind[k]); /* stakindN */
2317  }
2318  i = Anum_pg_statistic_staop1 - 1;
2319  for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2320  {
2321  values[i++] = ObjectIdGetDatum(stats->staop[k]); /* staopN */
2322  }
2323  i = Anum_pg_statistic_stacoll1 - 1;
2324  for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2325  {
2326  values[i++] = ObjectIdGetDatum(stats->stacoll[k]); /* stacollN */
2327  }
2328  i = Anum_pg_statistic_stanumbers1 - 1;
2329  for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2330  {
2331  int nnum = stats->numnumbers[k];
2332 
2333  if (nnum > 0)
2334  {
2335  int n;
2336  Datum *numdatums = (Datum *) palloc(nnum * sizeof(Datum));
2337  ArrayType *arry;
2338 
2339  for (n = 0; n < nnum; n++)
2340  numdatums[n] = Float4GetDatum(stats->stanumbers[k][n]);
2341  /* XXX knows more than it should about type float4: */
2342  arry = construct_array(numdatums, nnum,
2343  FLOAT4OID,
2344  sizeof(float4), true, TYPALIGN_INT);
2345  values[i++] = PointerGetDatum(arry); /* stanumbersN */
2346  }
2347  else
2348  {
2349  nulls[i] = true;
2350  values[i++] = (Datum) 0;
2351  }
2352  }
2353  i = Anum_pg_statistic_stavalues1 - 1;
2354  for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2355  {
2356  if (stats->numvalues[k] > 0)
2357  {
2358  ArrayType *arry;
2359 
2360  arry = construct_array(stats->stavalues[k],
2361  stats->numvalues[k],
2362  stats->statypid[k],
2363  stats->statyplen[k],
2364  stats->statypbyval[k],
2365  stats->statypalign[k]);
2366  values[i++] = PointerGetDatum(arry); /* stavaluesN */
2367  }
2368  else
2369  {
2370  nulls[i] = true;
2371  values[i++] = (Datum) 0;
2372  }
2373  }
2374 
2375  stup = heap_form_tuple(RelationGetDescr(sd), values, nulls);
2376 
2377  astate = accumArrayResult(astate,
2379  false,
2380  typOid,
2382  }
2383 
2385 
2386  return makeArrayResult(astate, CurrentMemoryContext);
2387 }
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:725
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define RelationGetDescr(relation)
Definition: rel.h:483
#define PointerGetDatum(X)
Definition: postgres.h:600
Datum * stavalues[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:152
#define Int16GetDatum(X)
Definition: postgres.h:495
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3318
int errcode(int sqlerrcode)
Definition: elog.c:698
bool statypbyval[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:162
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
char statypalign[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:163
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
int32 stawidth
Definition: vacuum.h:144
Oid stacoll[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:148
int numnumbers[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:149
#define RowExclusiveLock
Definition: lockdefs.h:38
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5149
float4 stanullfrac
Definition: vacuum.h:143
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
Definition: heaptuple.c:984
Oid staop[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:147
bool stats_valid
Definition: vacuum.h:142
float float4
Definition: c.h:564
uintptr_t Datum
Definition: postgres.h:411
int16 stakind[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:146
Oid get_rel_type_id(Oid relid)
Definition: lsyscache.c:1950
Oid statypid[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:160
#define BoolGetDatum(X)
Definition: postgres.h:446
#define InvalidOid
Definition: postgres_ext.h:36
#define STATISTIC_NUM_SLOTS
Definition: pg_statistic.h:127
#define ereport(elevel,...)
Definition: elog.h:157
float4 * stanumbers[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:150
VacAttrStats * vacattrstat
#define InvalidAttrNumber
Definition: attnum.h:23
static Datum values[MAXATTR]
Definition: bootstrap.c:166
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5085
#define Int32GetDatum(X)
Definition: postgres.h:523
int numvalues[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:151
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
int16 statyplen[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:161
int i
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
float4 stadistinct
Definition: vacuum.h:145

◆ stat_covers_expressions()

static bool stat_covers_expressions ( StatisticExtInfo stat,
List exprs,
Bitmapset **  expr_idxs 
)
static

Definition at line 1192 of file extended_stats.c.

References bms_add_member(), lfirst, and stat_find_expression().

Referenced by choose_best_statistics(), and statext_mcv_clauselist_selectivity().

1194 {
1195  ListCell *lc;
1196 
1197  foreach(lc, exprs)
1198  {
1199  Node *expr = (Node *) lfirst(lc);
1200  int expr_idx;
1201 
1202  expr_idx = stat_find_expression(stat, expr);
1203  if (expr_idx == -1)
1204  return false;
1205 
1206  if (expr_idxs != NULL)
1207  *expr_idxs = bms_add_member(*expr_idxs, expr_idx);
1208  }
1209 
1210  /* If we reach here, all expressions are covered */
1211  return true;
1212 }
static int stat_find_expression(StatisticExtInfo *stat, Node *expr)
Definition: nodes.h:539
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736

◆ stat_find_expression()

static int stat_find_expression ( StatisticExtInfo stat,
Node expr 
)
static

Definition at line 1165 of file extended_stats.c.

References equal(), StatisticExtInfo::exprs, idx(), and lfirst.

Referenced by stat_covers_expressions().

1166 {
1167  ListCell *lc;
1168  int idx;
1169 
1170  idx = 0;
1171  foreach(lc, stat->exprs)
1172  {
1173  Node *stat_expr = (Node *) lfirst(lc);
1174 
1175  if (equal(stat_expr, expr))
1176  return idx;
1177  idx++;
1178  }
1179 
1180  /* Expression not found */
1181  return -1;
1182 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3105
Definition: nodes.h:539
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
#define lfirst(lc)
Definition: pg_list.h:169

◆ statext_clauselist_selectivity()

Selectivity statext_clauselist_selectivity ( PlannerInfo root,
List clauses,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo,
RelOptInfo rel,
Bitmapset **  estimatedclauses,
bool  is_or 
)

Definition at line 1970 of file extended_stats.c.

References dependencies_clauselist_selectivity(), and statext_mcv_clauselist_selectivity().

Referenced by clauselist_selectivity_ext(), and clauselist_selectivity_or().

1974 {
1975  Selectivity sel;
1976 
1977  /* First, try estimating clauses using a multivariate MCV list. */
1978  sel = statext_mcv_clauselist_selectivity(root, clauses, varRelid, jointype,
1979  sjinfo, rel, estimatedclauses, is_or);
1980 
1981  /*
1982  * Functional dependencies only work for clauses connected by AND, so for
1983  * OR clauses we're done.
1984  */
1985  if (is_or)
1986  return sel;
1987 
1988  /*
1989  * Then, apply functional dependencies on the remaining clauses by calling
1990  * dependencies_clauselist_selectivity. Pass 'estimatedclauses' so the
1991  * function can properly skip clauses already estimated above.
1992  *
1993  * The reasoning for applying dependencies last is that the more complex
1994  * stats can track more complex correlations between the attributes, and
1995  * so may be considered more reliable.
1996  *
1997  * For example, MCV list can give us an exact selectivity for values in
1998  * two columns, while functional dependencies can only provide information
1999  * about the overall strength of the dependency.
2000  */
2001  sel *= dependencies_clauselist_selectivity(root, clauses, varRelid,
2002  jointype, sjinfo, rel,
2003  estimatedclauses);
2004 
2005  return sel;
2006 }
Selectivity dependencies_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
double Selectivity
Definition: nodes.h:672
static Selectivity statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses, bool is_or)

◆ statext_compute_stattarget()

static int statext_compute_stattarget ( int  stattarget,
int  natts,
VacAttrStats **  stats 
)
static

Definition at line 343 of file extended_stats.c.

References Assert, VacAttrStats::attr, default_statistics_target, i, and StatExtEntry::stattarget.

Referenced by BuildRelationExtStatistics(), and ComputeExtStatisticsRows().

344 {
345  int i;
346 
347  /*
348  * If there's statistics target set for the statistics object, use it. It
349  * may be set to 0 which disables building of that statistic.
350  */
351  if (stattarget >= 0)
352  return stattarget;
353 
354  /*
355  * The target for the statistics object is set to -1, in which case we
356  * look at the maximum target set for any of the attributes the object is
357  * defined on.
358  */
359  for (i = 0; i < nattrs; i++)
360  {
361  /* keep the maximum statistics target */
362  if (stats[i]->attr->attstattarget > stattarget)
363  stattarget = stats[i]->attr->attstattarget;
364  }
365 
366  /*
367  * If the value is still negative (so neither the statistics object nor
368  * any of the columns have custom statistics target set), use the global
369  * default target.
370  */
371  if (stattarget < 0)
372  stattarget = default_statistics_target;
373 
374  /* As this point we should have a valid statistics target. */
375  Assert((stattarget >= 0) && (stattarget <= 10000));
376 
377  return stattarget;
378 }
Form_pg_attribute attr
Definition: vacuum.h:123
#define Assert(condition)
Definition: c.h:804
int i
int default_statistics_target
Definition: analyze.c:82

◆ statext_expressions_load()

HeapTuple statext_expressions_load ( Oid  stxoid,
int  idx 
)

Definition at line 2394 of file extended_stats.c.

References DatumGetExpandedArray(), DatumGetHeapTupleHeader, deconstruct_expanded_array(), ExpandedArrayHeader::dvalues, elog, ERROR, heap_copytuple(), HeapTupleHeaderGetDatumLength, HeapTupleIsValid, InvalidOid, ItemPointerSetInvalid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1(), STATEXTDATASTXOID, SysCacheGetAttr(), HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, and value.

Referenced by examine_variable().

2395 {
2396  bool isnull;
2397  Datum value;
2398  HeapTuple htup;
2399  ExpandedArrayHeader *eah;
2400  HeapTupleHeader td;
2401  HeapTupleData tmptup;
2402  HeapTuple tup;
2403 
2405  if (!HeapTupleIsValid(htup))
2406  elog(ERROR, "cache lookup failed for statistics object %u", stxoid);
2407 
2408  value = SysCacheGetAttr(STATEXTDATASTXOID, htup,
2409  Anum_pg_statistic_ext_data_stxdexpr, &isnull);
2410  if (isnull)
2411  elog(ERROR,
2412  "requested statistics kind \"%c\" is not yet built for statistics object %u",
2413  STATS_EXT_DEPENDENCIES, stxoid);
2414 
2415  eah = DatumGetExpandedArray(value);
2416 
2418 
2419  td = DatumGetHeapTupleHeader(eah->dvalues[idx]);
2420 
2421  /* Build a temporary HeapTuple control structure */
2422  tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
2423  ItemPointerSetInvalid(&(tmptup.t_self));
2424  tmptup.t_tableOid = InvalidOid;
2425  tmptup.t_data = td;
2426 
2427  tup = heap_copytuple(&tmptup);
2428 
2429  ReleaseSysCache(htup);
2430 
2431  return tup;
2432 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
void deconstruct_expanded_array(ExpandedArrayHeader *eah)
static struct @142 value
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
Datum * dvalues
Definition: array.h:139
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
HeapTupleHeader t_data
Definition: htup.h:68
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
ExpandedArrayHeader * DatumGetExpandedArray(Datum d)
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
Oid t_tableOid
Definition: htup.h:66
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
#define elog(elevel,...)
Definition: elog.h:232
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:451

◆ statext_is_compatible_clause()

static bool statext_is_compatible_clause ( PlannerInfo root,
Node clause,
Index  relid,
Bitmapset **  attnums,
List **  exprs 
)
static

Definition at line 1554 of file extended_stats.c.

References ACL_SELECT, ACLCHECK_OK, ACLMASK_ALL, BoolExpr::args, attnum, bms_add_members(), bms_get_singleton_member(), bms_is_member(), bms_next_member(), RangeTblEntry::checkAsUser, RestrictInfo::clause, RestrictInfo::clause_relids, StatExtEntry::exprs, GetUserId(), InvalidAttrNumber, is_andclause(), IsA, lfirst, NIL, pg_attribute_aclcheck(), pg_attribute_aclcheck_all(), pg_class_aclcheck(), RestrictInfo::pseudoconstant, pull_varattnos(), RangeTblEntry::relid, PlannerInfo::simple_rte_array, and statext_is_compatible_clause_internal().

Referenced by statext_mcv_clauselist_selectivity().

1556 {
1557  RangeTblEntry *rte = root->simple_rte_array[relid];
1558  RestrictInfo *rinfo = (RestrictInfo *) clause;
1559  int clause_relid;
1560  Oid userid;
1561 
1562  /*
1563  * Special-case handling for bare BoolExpr AND clauses, because the
1564  * restrictinfo machinery doesn't build RestrictInfos on top of AND
1565  * clauses.
1566  */
1567  if (is_andclause(clause))
1568  {
1569  BoolExpr *expr = (BoolExpr *) clause;
1570  ListCell *lc;
1571 
1572  /*
1573  * Check that each sub-clause is compatible. We expect these to be
1574  * RestrictInfos.
1575  */
1576  foreach(lc, expr->args)
1577  {
1578  if (!statext_is_compatible_clause(root, (Node *) lfirst(lc),
1579  relid, attnums, exprs))
1580  return false;
1581  }
1582 
1583  return true;
1584  }
1585 
1586  /* Otherwise it must be a RestrictInfo. */
1587  if (!IsA(rinfo, RestrictInfo))
1588  return false;
1589 
1590  /* Pseudoconstants are not really interesting here. */
1591  if (rinfo->pseudoconstant)
1592  return false;
1593 
1594  /* Clauses referencing other varnos are incompatible. */
1595  if (!bms_get_singleton_member(rinfo->clause_relids, &clause_relid) ||
1596  clause_relid != relid)
1597  return false;
1598 
1599  /* Check the clause and determine what attributes it references. */
1600  if (!statext_is_compatible_clause_internal(root, (Node *) rinfo->clause,
1601  relid, attnums, exprs))
1602  return false;
1603 
1604  /*
1605  * Check that the user has permission to read all required attributes. Use
1606  * checkAsUser if it's set, in case we're accessing the table via a view.
1607  */
1608  userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
1609 
1610  if (pg_class_aclcheck(rte->relid, userid, ACL_SELECT) != ACLCHECK_OK)
1611  {
1612  Bitmapset *clause_attnums = NULL;
1613 
1614  /* Don't have table privilege, must check individual columns */
1615  if (*exprs != NIL)
1616  {
1617  pull_varattnos((Node *) exprs, relid, &clause_attnums);
1618  clause_attnums = bms_add_members(clause_attnums, *attnums);
1619  }
1620  else
1621  clause_attnums = *attnums;
1622 
1623  if (bms_is_member(InvalidAttrNumber, clause_attnums))
1624  {
1625  /* Have a whole-row reference, must have access to all columns */
1626  if (pg_attribute_aclcheck_all(rte->relid, userid, ACL_SELECT,
1628  return false;
1629  }
1630  else
1631  {
1632  /* Check the columns referenced by the clause */
1633  int attnum = -1;
1634 
1635  while ((attnum = bms_next_member(clause_attnums, attnum)) >= 0)
1636  {
1637  if (pg_attribute_aclcheck(rte->relid, attnum, userid,
1638  ACL_SELECT) != ACLCHECK_OK)
1639  return false;
1640  }
1641  }
1642  }
1643 
1644  /* If we reach here, the clause is OK */
1645  return true;
1646 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
AclResult pg_attribute_aclcheck(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode)
Definition: aclchk.c:4556
Oid GetUserId(void)
Definition: miscinit.c:478
static bool statext_is_compatible_clause_internal(PlannerInfo *root, Node *clause, Index relid, Bitmapset **attnums, List **exprs)
Relids clause_relids
Definition: pathnodes.h:2063
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
bool pseudoconstant
Definition: pathnodes.h:2053
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:97
Definition: nodes.h:539
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
unsigned int Oid
Definition: postgres_ext.h:31
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:246
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:193
Expr * clause
Definition: pathnodes.h:2045
static bool statext_is_compatible_clause(PlannerInfo *root, Node *clause, Index relid, Bitmapset **attnums, List **exprs)
#define ACL_SELECT
Definition: parsenodes.h:83
int16 attnum
Definition: pg_attribute.h:83
#define lfirst(lc)
Definition: pg_list.h:169
AclResult pg_attribute_aclcheck_all(Oid table_oid, Oid roleid, AclMode mode, AclMaskHow how)
Definition: aclchk.c:4600
List * args
Definition: primnodes.h:616
#define InvalidAttrNumber
Definition: attnum.h:23
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4685
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793

◆ statext_is_compatible_clause_internal()

static bool statext_is_compatible_clause_internal ( PlannerInfo root,
Node clause,
Index  relid,
Bitmapset **  attnums,
List **  exprs 
)
static

Definition at line 1320 of file extended_stats.c.

References arg, NullTest::arg, OpExpr::args, ScalarArrayOpExpr::args, BoolExpr::args, AttrNumberIsForUserDefinedAttr, bms_add_member(), examine_opclause_args(), get_func_leakproof(), get_opcode(), get_oprrest(), is_andclause(), is_notclause(), is_opclause(), is_orclause(), IsA, lappend(), lfirst, list_length(), NIL, OpExpr::opno, ScalarArrayOpExpr::opno, RangeTblEntry::securityQuals, PlannerInfo::simple_rte_array, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by statext_is_compatible_clause().

1323 {
1324  /* Look inside any binary-compatible relabeling (as in examine_variable) */
1325  if (IsA(clause, RelabelType))
1326  clause = (Node *) ((RelabelType *) clause)->arg;
1327 
1328  /* plain Var references (boolean Vars or recursive checks) */
1329  if (IsA(clause, Var))
1330  {
1331  Var *var = (Var *) clause;
1332 
1333  /* Ensure var is from the correct relation */
1334  if (var->varno != relid)
1335  return false;
1336 
1337  /* we also better ensure the Var is from the current level */
1338  if (var->varlevelsup > 0)
1339  return false;
1340 
1341  /* Also skip system attributes (we don't allow stats on those). */
1343  return false;
1344 
1345  *attnums = bms_add_member(*attnums, var->varattno);
1346 
1347  return true;
1348  }
1349 
1350  /* (Var/Expr op Const) or (Const op Var/Expr) */
1351  if (is_opclause(clause))
1352  {
1353  RangeTblEntry *rte = root->simple_rte_array[relid];
1354  OpExpr *expr = (OpExpr *) clause;
1355  Node *clause_expr;
1356 
1357  /* Only expressions with two arguments are considered compatible. */
1358  if (list_length(expr->args) != 2)
1359  return false;
1360 
1361  /* Check if the expression has the right shape */
1362  if (!examine_opclause_args(expr->args, &clause_expr, NULL, NULL))
1363  return false;
1364 
1365  /*
1366  * If it's not one of the supported operators ("=", "<", ">", etc.),
1367  * just ignore the clause, as it's not compatible with MCV lists.
1368  *
1369  * This uses the function for estimating selectivity, not the operator
1370  * directly (a bit awkward, but well ...).
1371  */
1372  switch (get_oprrest(expr->opno))
1373  {
1374  case F_EQSEL:
1375  case F_NEQSEL:
1376  case F_SCALARLTSEL:
1377  case F_SCALARLESEL:
1378  case F_SCALARGTSEL:
1379  case F_SCALARGESEL:
1380  /* supported, will continue with inspection of the Var/Expr */
1381  break;
1382 
1383  default:
1384  /* other estimators are considered unknown/unsupported */
1385  return false;
1386  }
1387 
1388  /*
1389  * If there are any securityQuals on the RTE from security barrier
1390  * views or RLS policies, then the user may not have access to all the
1391  * table's data, and we must check that the operator is leak-proof.
1392  *
1393  * If the operator is leaky, then we must ignore this clause for the
1394  * purposes of estimating with MCV lists, otherwise the operator might
1395  * reveal values from the MCV list that the user doesn't have
1396  * permission to see.
1397  */
1398  if (rte->securityQuals != NIL &&
1400  return false;
1401 
1402  /* Check (Var op Const) or (Const op Var) clauses by recursing. */
1403  if (IsA(clause_expr, Var))
1404  return statext_is_compatible_clause_internal(root, clause_expr,
1405  relid, attnums, exprs);
1406 
1407  /* Otherwise we have (Expr op Const) or (Const op Expr). */
1408  *exprs = lappend(*exprs, clause_expr);
1409  return true;
1410  }
1411 
1412  /* Var/Expr IN Array */
1413  if (IsA(clause, ScalarArrayOpExpr))
1414  {
1415  RangeTblEntry *rte = root->simple_rte_array[relid];
1416  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) clause;
1417  Node *clause_expr;
1418 
1419  /* Only expressions with two arguments are considered compatible. */
1420  if (list_length(expr->args) != 2)
1421  return false;
1422 
1423  /* Check if the expression has the right shape (one Var, one Const) */
1424  if (!examine_opclause_args(expr->args, &clause_expr, NULL, NULL))
1425  return false;
1426 
1427  /*
1428  * If it's not one of the supported operators ("=", "<", ">", etc.),
1429  * just ignore the clause, as it's not compatible with MCV lists.
1430  *
1431  * This uses the function for estimating selectivity, not the operator
1432  * directly (a bit awkward, but well ...).
1433  */
1434  switch (get_oprrest(expr->opno))
1435  {
1436  case F_EQSEL:
1437  case F_NEQSEL:
1438  case F_SCALARLTSEL:
1439  case F_SCALARLESEL:
1440  case F_SCALARGTSEL:
1441  case F_SCALARGESEL:
1442  /* supported, will continue with inspection of the Var/Expr */
1443  break;
1444 
1445  default:
1446  /* other estimators are considered unknown/unsupported */
1447  return false;
1448  }
1449 
1450  /*
1451  * If there are any securityQuals on the RTE from security barrier
1452  * views or RLS policies, then the user may not have access to all the
1453  * table's data, and we must check that the operator is leak-proof.
1454  *
1455  * If the operator is leaky, then we must ignore this clause for the
1456  * purposes of estimating with MCV lists, otherwise the operator might
1457  * reveal values from the MCV list that the user doesn't have
1458  * permission to see.
1459  */
1460  if (rte->securityQuals != NIL &&
1462  return false;
1463 
1464  /* Check Var IN Array clauses by recursing. */
1465  if (IsA(clause_expr, Var))
1466  return statext_is_compatible_clause_internal(root, clause_expr,
1467  relid, attnums, exprs);
1468 
1469  /* Otherwise we have Expr IN Array. */
1470  *exprs = lappend(*exprs, clause_expr);
1471  return true;
1472  }
1473 
1474  /* AND/OR/NOT clause */
1475  if (is_andclause(clause) ||
1476  is_orclause(clause) ||
1477  is_notclause(clause))
1478  {
1479  /*
1480  * AND/OR/NOT-clauses are supported if all sub-clauses are supported
1481  *
1482  * Perhaps we could improve this by handling mixed cases, when some of
1483  * the clauses are supported and some are not. Selectivity for the
1484  * supported subclauses would be computed using extended statistics,
1485  * and the remaining clauses would be estimated using the traditional
1486  * algorithm (product of selectivities).
1487  *
1488  * It however seems overly complex, and in a way we already do that
1489  * because if we reject the whole clause as unsupported here, it will
1490  * be eventually passed to clauselist_selectivity() which does exactly
1491  * this (split into supported/unsupported clauses etc).
1492  */
1493  BoolExpr *expr = (BoolExpr *) clause;
1494  ListCell *lc;
1495 
1496  foreach(lc, expr->args)
1497  {
1498  /*
1499  * Had we found incompatible clause in the arguments, treat the
1500  * whole clause as incompatible.
1501  */
1503  (Node *) lfirst(lc),
1504  relid, attnums, exprs))
1505  return false;
1506  }
1507 
1508  return true;
1509  }
1510 
1511  /* Var/Expr IS NULL */
1512  if (IsA(clause, NullTest))
1513  {
1514  NullTest *nt = (NullTest *) clause;
1515 
1516  /* Check Var IS NULL clauses by recursing. */
1517  if (IsA(nt->arg, Var))
1518  return statext_is_compatible_clause_internal(root, (Node *) (nt->arg),
1519  relid, attnums, exprs);
1520 
1521  /* Otherwise we have Expr IS NULL. */
1522  *exprs = lappend(*exprs, nt->arg);
1523  return true;
1524  }
1525 
1526  /*
1527  * Treat any other expressions as bare expressions to be matched against
1528  * expressions in statistics objects.
1529  */
1530  *exprs = lappend(*exprs, clause);
1531  return true;
1532 }
#define NIL
Definition: pg_list.h:65
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1808
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Index varlevelsup
Definition: primnodes.h:196
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:106
List * securityQuals
Definition: parsenodes.h:1151
static bool statext_is_compatible_clause_internal(PlannerInfo *root, Node *clause, Index relid, Bitmapset **attnums, List **exprs)
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:97
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
Definition: attnum.h:41
Definition: nodes.h:539
AttrNumber varattno
Definition: primnodes.h:191
Definition: primnodes.h:186
Expr * arg
Definition: primnodes.h:1255
List * lappend(List *list, void *datum)
Definition: list.c:336
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:193
Index varno
Definition: primnodes.h:189
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:115
RegProcedure get_oprrest(Oid opno)
Definition: lsyscache.c:1528
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1256
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
List * args
Definition: primnodes.h:616
void * arg
Oid opno
Definition: primnodes.h:542
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:66
List * args
Definition: primnodes.h:548
bool examine_opclause_args(List *args, Node **exprp, Const **cstp, bool *expronleftp)

◆ statext_is_kind_built()

bool statext_is_kind_built ( HeapTuple  htup,
char  type 
)

Definition at line 385 of file extended_stats.c.

References attnum, elog, ERROR, and heap_attisnull().

Referenced by get_relation_statistics().

386 {
388 
389  switch (type)
390  {
391  case STATS_EXT_NDISTINCT:
392  attnum = Anum_pg_statistic_ext_data_stxdndistinct;
393  break;
394 
395  case STATS_EXT_DEPENDENCIES:
396  attnum = Anum_pg_statistic_ext_data_stxddependencies;
397  break;
398 
399  case STATS_EXT_MCV:
400  attnum = Anum_pg_statistic_ext_data_stxdmcv;
401  break;
402 
403  case STATS_EXT_EXPRESSIONS:
404  attnum = Anum_pg_statistic_ext_data_stxdexpr;
405  break;
406 
407  default:
408  elog(ERROR, "unexpected statistics type requested: %d", type);
409  }
410 
411  return !heap_attisnull(htup, attnum, NULL);
412 }
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
#define ERROR
Definition: elog.h:46
int16 attnum
Definition: pg_attribute.h:83
#define elog(elevel,...)
Definition: elog.h:232
int16 AttrNumber
Definition: attnum.h:21

◆ statext_mcv_clauselist_selectivity()

static Selectivity statext_mcv_clauselist_selectivity ( PlannerInfo root,
List clauses,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo,
RelOptInfo rel,
Bitmapset **  estimatedclauses,
bool  is_or 
)
static

Definition at line 1683 of file extended_stats.c.

References Assert, bms_add_member(), bms_free(), bms_is_member(), bms_is_subset(), bms_membership(), BMS_SINGLETON, choose_best_statistics(), CLAMP_PROBABILITY, clause_selectivity_ext(), clauselist_selectivity_ext(), StatExtEntry::exprs, has_stats_of_kind(), StatisticExtInfo::keys, StatisticExtInfo::kind, lappend(), lfirst, list_free(), list_length(), mcv_clause_selectivity_or(), mcv_clauselist_selectivity(), mcv_combine_selectivities(), NIL, palloc(), RelOptInfo::relid, stat, stat_covers_expressions(), statext_is_compatible_clause(), statext_mcv_load(), RelOptInfo::statlist, and StatisticExtInfo::statOid.

Referenced by statext_clauselist_selectivity().

1687 {
1688  ListCell *l;
1689  Bitmapset **list_attnums; /* attnums extracted from the clause */
1690  List **list_exprs; /* expressions matched to any statistic */
1691  int listidx;
1692  Selectivity sel = (is_or) ? 0.0 : 1.0;
1693 
1694  /* check if there's any stats that might be useful for us. */
1695  if (!has_stats_of_kind(rel->statlist, STATS_EXT_MCV))
1696  return sel;
1697 
1698  list_attnums = (Bitmapset **) palloc(sizeof(Bitmapset *) *
1699  list_length(clauses));
1700 
1701  /* expressions extracted from complex expressions */
1702  list_exprs = (List **) palloc(sizeof(Node *) * list_length(clauses));
1703 
1704  /*
1705  * Pre-process the clauses list to extract the attnums and expressions
1706  * seen in each item. We need to determine if there are any clauses which
1707  * will be useful for selectivity estimations with extended stats. Along
1708  * the way we'll record all of the attnums and expressions for each clause
1709  * in lists which we'll reference later so we don't need to repeat the
1710  * same work again.
1711  *
1712  * We also skip clauses that we already estimated using different types of
1713  * statistics (we treat them as incompatible).
1714  */
1715  listidx = 0;
1716  foreach(l, clauses)
1717  {
1718  Node *clause = (Node *) lfirst(l);
1719  Bitmapset *attnums = NULL;
1720  List *exprs = NIL;
1721 
1722  if (!bms_is_member(listidx, *estimatedclauses) &&
1723  statext_is_compatible_clause(root, clause, rel->relid, &attnums, &exprs))
1724  {
1725  list_attnums[listidx] = attnums;
1726  list_exprs[listidx] = exprs;
1727  }
1728  else
1729  {
1730  list_attnums[listidx] = NULL;
1731  list_exprs[listidx] = NIL;
1732  }
1733 
1734  listidx++;
1735  }
1736 
1737  /* apply as many extended statistics as possible */
1738  while (true)
1739  {
1741  List *stat_clauses;
1742  Bitmapset *simple_clauses;
1743 
1744  /* find the best suited statistics object for these attnums */
1745  stat = choose_best_statistics(rel->statlist, STATS_EXT_MCV,
1746  list_attnums, list_exprs,
1747  list_length(clauses));
1748 
1749  /*
1750  * if no (additional) matching stats could be found then we've nothing
1751  * to do
1752  */
1753  if (!stat)
1754  break;
1755 
1756  /* Ensure choose_best_statistics produced an expected stats type. */
1757  Assert(stat->kind == STATS_EXT_MCV);
1758 
1759  /* now filter the clauses to be estimated using the selected MCV */
1760  stat_clauses = NIL;
1761 
1762  /* record which clauses are simple (single column or expression) */
1763  simple_clauses = NULL;
1764 
1765  listidx = -1;
1766  foreach(l, clauses)
1767  {
1768  /* Increment the index before we decide if to skip the clause. */
1769  listidx++;
1770 
1771  /*
1772  * Ignore clauses from which we did not extract any attnums or
1773  * expressions (this needs to be consistent with what we do in
1774  * choose_best_statistics).
1775  *
1776  * This also eliminates already estimated clauses - both those
1777  * estimated before and during applying extended statistics.
1778  *
1779  * XXX This check is needed because both bms_is_subset and
1780  * stat_covers_expressions return true for empty attnums and
1781  * expressions.
1782  */
1783  if (!list_attnums[listidx] && !list_exprs[listidx])
1784  continue;
1785 
1786  /*
1787  * The clause was not estimated yet, and we've extracted either
1788  * attnums of expressions from it. Ignore it if it's not fully
1789  * covered by the chosen statistics.
1790  *
1791  * We need to check both attributes and expressions, and reject
1792  * if either is not covered.
1793  */
1794  if (!bms_is_subset(list_attnums[listidx], stat->keys) ||
1795  !stat_covers_expressions(stat, list_exprs[listidx], NULL))
1796  continue;
1797 
1798  /*
1799  * Now we know the clause is compatible (we have either atttnums
1800  * or expressions extracted from it), and was not estimated yet.
1801  */
1802 
1803  /* record simple clauses (single column or expression) */
1804  if ((list_attnums[listidx] == NULL &&
1805  list_length(list_exprs[listidx]) == 1) ||
1806  (list_exprs[listidx] == NIL &&
1807  bms_membership(list_attnums[listidx]) == BMS_SINGLETON))
1808  simple_clauses = bms_add_member(simple_clauses,
1809  list_length(stat_clauses));
1810 
1811  /* add clause to list and mark it as estimated */
1812  stat_clauses = lappend(stat_clauses, (Node *) lfirst(l));
1813  *estimatedclauses = bms_add_member(*estimatedclauses, listidx);
1814 
1815  /*
1816  * Reset the pointers, so that choose_best_statistics knows this
1817  * clause was estimated and does not consider it again.
1818  */
1819  bms_free(list_attnums[listidx]);
1820  list_attnums[listidx] = NULL;
1821 
1822  list_free(list_exprs[listidx]);
1823  list_exprs[listidx] = NULL;
1824  }
1825 
1826  if (is_or)
1827  {
1828  bool *or_matches = NULL;
1829  Selectivity simple_or_sel = 0.0,
1830  stat_sel = 0.0;
1831  MCVList *mcv_list;
1832 
1833  /* Load the MCV list stored in the statistics object */
1834  mcv_list = statext_mcv_load(stat->statOid);
1835 
1836  /*
1837  * Compute the selectivity of the ORed list of clauses covered by
1838  * this statistics object by estimating each in turn and combining
1839  * them using the formula P(A OR B) = P(A) + P(B) - P(A AND B).
1840  * This allows us to use the multivariate MCV stats to better
1841  * estimate the individual terms and their overlap.
1842  *
1843  * Each time we iterate this formula, the clause "A" above is
1844  * equal to all the clauses processed so far, combined with "OR".
1845  */
1846  listidx = 0;
1847  foreach(l, stat_clauses)
1848  {
1849  Node *clause = (Node *) lfirst(l);
1850  Selectivity simple_sel,
1851  overlap_simple_sel,
1852  mcv_sel,
1853  mcv_basesel,
1854  overlap_mcvsel,
1855  overlap_basesel,
1856  mcv_totalsel,
1857  clause_sel,
1858  overlap_sel;
1859 
1860  /*
1861  * "Simple" selectivity of the next clause and its overlap
1862  * with any of the previous clauses. These are our initial
1863  * estimates of P(B) and P(A AND B), assuming independence of
1864  * columns/clauses.
1865  */
1866  simple_sel = clause_selectivity_ext(root, clause, varRelid,
1867  jointype, sjinfo, false);
1868 
1869  overlap_simple_sel = simple_or_sel * simple_sel;
1870 
1871  /*
1872  * New "simple" selectivity of all clauses seen so far,
1873  * assuming independence.
1874  */
1875  simple_or_sel += simple_sel - overlap_simple_sel;
1876  CLAMP_PROBABILITY(simple_or_sel);
1877 
1878  /*
1879  * Multi-column estimate of this clause using MCV statistics,
1880  * along with base and total selectivities, and corresponding
1881  * selectivities for the overlap term P(A AND B).
1882  */
1883  mcv_sel = mcv_clause_selectivity_or(root, stat, mcv_list,
1884  clause, &or_matches,
1885  &mcv_basesel,
1886  &overlap_mcvsel,
1887  &overlap_basesel,
1888  &mcv_totalsel);
1889 
1890  /*
1891  * Combine the simple and multi-column estimates.
1892  *
1893  * If this clause is a simple single-column clause, then we
1894  * just use the simple selectivity estimate for it, since the
1895  * multi-column statistics are unlikely to improve on that
1896  * (and in fact could make it worse). For the overlap, we
1897  * always make use of the multi-column statistics.
1898  */
1899  if (bms_is_member(listidx, simple_clauses))
1900  clause_sel = simple_sel;
1901  else
1902  clause_sel = mcv_combine_selectivities(simple_sel,
1903  mcv_sel,
1904  mcv_basesel,
1905  mcv_totalsel);
1906 
1907  overlap_sel = mcv_combine_selectivities(overlap_simple_sel,
1908  overlap_mcvsel,
1909  overlap_basesel,
1910  mcv_totalsel);
1911 
1912  /* Factor these into the result for this statistics object */
1913  stat_sel += clause_sel - overlap_sel;
1914  CLAMP_PROBABILITY(stat_sel);
1915 
1916  listidx++;
1917  }
1918 
1919  /*
1920  * Factor the result for this statistics object into the overall
1921  * result. We treat the results from each separate statistics
1922  * object as independent of one another.
1923  */
1924  sel = sel + stat_sel - sel * stat_sel;
1925  }
1926  else /* Implicitly-ANDed list of clauses */
1927  {
1928  Selectivity simple_sel,
1929  mcv_sel,
1930  mcv_basesel,
1931  mcv_totalsel,
1932  stat_sel;
1933 
1934  /*
1935  * "Simple" selectivity, i.e. without any extended statistics,
1936  * essentially assuming independence of the columns/clauses.
1937  */
1938  simple_sel = clauselist_selectivity_ext(root, stat_clauses,
1939  varRelid, jointype,
1940  sjinfo, false);
1941 
1942  /*
1943  * Multi-column estimate using MCV statistics, along with base and
1944  * total selectivities.
1945  */
1946  mcv_sel = mcv_clauselist_selectivity(root, stat, stat_clauses,
1947  varRelid, jointype, sjinfo,
1948  rel, &mcv_basesel,
1949  &mcv_totalsel);
1950 
1951  /* Combine the simple and multi-column estimates. */
1952  stat_sel = mcv_combine_selectivities(simple_sel,
1953  mcv_sel,
1954  mcv_basesel,
1955  mcv_totalsel);
1956 
1957  /* Factor this into the overall result */
1958  sel *= stat_sel;
1959  }
1960  }
1961 
1962  return sel;
1963 }
#define NIL
Definition: pg_list.h:65
List * statlist
Definition: pathnodes.h:714
Definition: nodes.h:539
double Selectivity
Definition: nodes.h:672
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:63
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
static bool stat_covers_expressions(StatisticExtInfo *stat, List *exprs, Bitmapset **expr_idxs)
Selectivity mcv_combine_selectivities(Selectivity simple_sel, Selectivity mcv_sel, Selectivity mcv_basesel, Selectivity mcv_totalsel)
Definition: mcv.c:1988
MCVList * statext_mcv_load(Oid mvoid)
Definition: mcv.c:560
Index relid
Definition: pathnodes.h:704
List * lappend(List *list, void *datum)
Definition: list.c:336
static bool statext_is_compatible_clause(PlannerInfo *root, Node *clause, Index relid, Bitmapset **attnums, List **exprs)
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
Selectivity clause_selectivity_ext(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, bool use_extended_stats)
Definition: clausesel.c:707
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
StatisticExtInfo * choose_best_statistics(List *stats, char requiredkind, Bitmapset **clause_attnums, List **clause_exprs, int nclauses)
static int list_length(const List *l)
Definition: pg_list.h:149
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
Selectivity clauselist_selectivity_ext(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, bool use_extended_stats)
Definition: clausesel.c:119
Bitmapset * keys
Definition: pathnodes.h:931
void * palloc(Size size)
Definition: mcxt.c:1062
void list_free(List *list)
Definition: list.c:1391
Selectivity mcv_clauselist_selectivity(PlannerInfo *root, StatisticExtInfo *stat, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Selectivity *basesel, Selectivity *totalsel)
Definition: mcv.c:2030
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
bool has_stats_of_kind(List *stats, char requiredkind)
#define stat
Definition: win32_port.h:275
Selectivity mcv_clause_selectivity_or(PlannerInfo *root, StatisticExtInfo *stat, MCVList *mcv, Node *clause, bool **or_matches, Selectivity *basesel, Selectivity *overlap_mcvsel, Selectivity *overlap_basesel, Selectivity *totalsel)
Definition: mcv.c:2107

◆ statext_store()

static void statext_store ( Oid  statOid,
MVNDistinct ndistinct,
MVDependencies dependencies,
MCVList mcv,
Datum  exprs,
VacAttrStats **  stats 
)
static

Definition at line 780 of file extended_stats.c.

References CatalogTupleUpdate(), elog, ERROR, StatExtEntry::exprs, heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, ObjectIdGetDatum, PointerGetDatum, RelationGetDescr, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), statext_dependencies_serialize(), statext_mcv_serialize(), statext_ndistinct_serialize(), STATEXTDATASTXOID, HeapTupleData::t_self, table_close(), table_open(), and values.

Referenced by BuildRelationExtStatistics().

783 {
784  Relation pg_stextdata;
785  HeapTuple stup,
786  oldtup;
787  Datum values[Natts_pg_statistic_ext_data];
788  bool nulls[Natts_pg_statistic_ext_data];
789  bool replaces[Natts_pg_statistic_ext_data];
790 
791  pg_stextdata = table_open(StatisticExtDataRelationId, RowExclusiveLock);
792 
793  memset(nulls, true, sizeof(nulls));
794  memset(replaces, false, sizeof(replaces));
795  memset(values, 0, sizeof(values));
796 
797  /*
798  * Construct a new pg_statistic_ext_data tuple, replacing the calculated
799  * stats.
800  */
801  if (ndistinct != NULL)
802  {
803  bytea *data = statext_ndistinct_serialize(ndistinct);
804 
805  nulls[Anum_pg_statistic_ext_data_stxdndistinct - 1] = (data == NULL);
806  values[Anum_pg_statistic_ext_data_stxdndistinct - 1] = PointerGetDatum(data);
807  }
808 
809  if (dependencies != NULL)
810  {
811  bytea *data = statext_dependencies_serialize(dependencies);
812 
813  nulls[Anum_pg_statistic_ext_data_stxddependencies - 1] = (data == NULL);
814  values[Anum_pg_statistic_ext_data_stxddependencies - 1] = PointerGetDatum(data);
815  }
816  if (mcv != NULL)
817  {
818  bytea *data = statext_mcv_serialize(mcv, stats);
819 
820  nulls[Anum_pg_statistic_ext_data_stxdmcv - 1] = (data == NULL);
821  values[Anum_pg_statistic_ext_data_stxdmcv - 1] = PointerGetDatum(data);
822  }
823  if (exprs != (Datum) 0)
824  {
825  nulls[Anum_pg_statistic_ext_data_stxdexpr - 1] = false;
826  values[Anum_pg_statistic_ext_data_stxdexpr - 1] = exprs;
827  }
828 
829  /* always replace the value (either by bytea or NULL) */
830  replaces[Anum_pg_statistic_ext_data_stxdndistinct - 1] = true;
831  replaces[Anum_pg_statistic_ext_data_stxddependencies - 1] = true;
832  replaces[Anum_pg_statistic_ext_data_stxdmcv - 1] = true;
833  replaces[Anum_pg_statistic_ext_data_stxdexpr - 1] = true;
834 
835  /* there should already be a pg_statistic_ext_data tuple */
837  if (!HeapTupleIsValid(oldtup))
838  elog(ERROR, "cache lookup failed for statistics object %u", statOid);
839 
840  /* replace it */
841  stup = heap_modify_tuple(oldtup,
842  RelationGetDescr(pg_stextdata),
843  values,
844  nulls,
845  replaces);
846  ReleaseSysCache(oldtup);
847  CatalogTupleUpdate(pg_stextdata, &stup->t_self, stup);
848 
849  heap_freetuple(stup);
850 
851  table_close(pg_stextdata, RowExclusiveLock);
852 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define RelationGetDescr(relation)
Definition: rel.h:483
#define PointerGetDatum(X)
Definition: postgres.h:600
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
ItemPointerData t_self
Definition: htup.h:65
bytea * statext_mcv_serialize(MCVList *mcvlist, VacAttrStats **stats)
Definition: mcv.c:621
#define RowExclusiveLock
Definition: lockdefs.h:38
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
bytea * statext_ndistinct_serialize(MVNDistinct *ndistinct)
Definition: mvdistinct.c:179
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
static Datum values[MAXATTR]
Definition: bootstrap.c:166
bytea * statext_dependencies_serialize(MVDependencies *dependencies)
Definition: dependencies.c:436
#define elog(elevel,...)
Definition: elog.h:232
Definition: c.h:621
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113