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/defrem.h"
#include "commands/progress.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/optimizer.h"
#include "parser/parsetree.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, bool inh, MVNDistinct *ndistinct, MVDependencies *dependencies, MCVList *mcv, Datum exprs, VacAttrStats **stats)
 
static int statext_compute_stattarget (int stattarget, int nattrs, 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 rel, StatExtEntry *stat, int numrows, HeapTuple *rows, VacAttrStats **stats, int stattarget)
 
void BuildRelationExtStatistics (Relation onerel, bool inh, 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, bool inh, 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, bool inh, int idx)
 

Macro Definition Documentation

◆ WIDTH_THRESHOLD

#define WIDTH_THRESHOLD   1024

Definition at line 61 of file extended_stats.c.

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 942 of file extended_stats.c.

943 {
944  int i,
945  j;
946  AttrNumber *attnums;
947  int num = bms_num_members(attrs);
948 
949  if (numattrs)
950  *numattrs = num;
951 
952  /* build attnums from the bitmapset */
953  attnums = (AttrNumber *) palloc(sizeof(AttrNumber) * num);
954  i = 0;
955  j = -1;
956  while ((j = bms_next_member(attrs, j)) >= 0)
957  {
958  int attnum = (j - nexprs);
959 
960  /*
961  * Make sure the bitmap contains only user-defined attributes. As
962  * bitmaps can't contain negative values, this can be violated in two
963  * ways. Firstly, the bitmap might contain 0 as a member, and secondly
964  * the integer value might be larger than MaxAttrNumber.
965  */
968  Assert(attnum >= (-nexprs));
969 
970  attnums[i++] = (AttrNumber) attnum;
971 
972  /* protect against overflows */
973  Assert(i <= num);
974  }
975 
976  return attnums;
977 }
int16 AttrNumber
Definition: attnum.h:21
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
#define MaxAttrNumber
Definition: attnum.h:24
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1106
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:685
int j
Definition: isn.c:74
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc(Size size)
Definition: mcxt.c:1226
int16 attnum
Definition: pg_attribute.h:74

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

◆ build_expr_data()

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

Definition at line 2271 of file extended_stats.c.

2272 {
2273  int idx;
2274  int nexprs = list_length(exprs);
2275  AnlExprData *exprdata;
2276  ListCell *lc;
2277 
2278  exprdata = (AnlExprData *) palloc0(nexprs * sizeof(AnlExprData));
2279 
2280  idx = 0;
2281  foreach(lc, exprs)
2282  {
2283  Node *expr = (Node *) lfirst(lc);
2284  AnlExprData *thisdata = &exprdata[idx];
2285 
2286  thisdata->expr = expr;
2287  thisdata->vacattrstat = examine_expression(expr, stattarget);
2288  idx++;
2289  }
2290 
2291  return exprdata;
2292 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
static VacAttrStats * examine_expression(Node *expr, int stattarget)
void * palloc0(Size size)
Definition: mcxt.c:1257
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
VacAttrStats * vacattrstat
Definition: nodes.h:129

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

Referenced by BuildRelationExtStatistics().

◆ build_sorted_items()

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

Definition at line 987 of file extended_stats.c.

990 {
991  int i,
992  j,
993  len,
994  nrows;
995  int nvalues = data->numrows * numattrs;
996 
997  SortItem *items;
998  Datum *values;
999  bool *isnull;
1000  char *ptr;
1001  int *typlen;
1002 
1003  /* Compute the total amount of memory we need (both items and values). */
1004  len = data->numrows * sizeof(SortItem) + nvalues * (sizeof(Datum) + sizeof(bool));
1005 
1006  /* Allocate the memory and split it into the pieces. */
1007  ptr = palloc0(len);
1008 
1009  /* items to sort */
1010  items = (SortItem *) ptr;
1011  ptr += data->numrows * sizeof(SortItem);
1012 
1013  /* values and null flags */
1014  values = (Datum *) ptr;
1015  ptr += nvalues * sizeof(Datum);
1016 
1017  isnull = (bool *) ptr;
1018  ptr += nvalues * sizeof(bool);
1019 
1020  /* make sure we consumed the whole buffer exactly */
1021  Assert((ptr - (char *) items) == len);
1022 
1023  /* fix the pointers to Datum and bool arrays */
1024  nrows = 0;
1025  for (i = 0; i < data->numrows; i++)
1026  {
1027  items[nrows].values = &values[nrows * numattrs];
1028  items[nrows].isnull = &isnull[nrows * numattrs];
1029 
1030  nrows++;
1031  }
1032 
1033  /* build a local cache of typlen for all attributes */
1034  typlen = (int *) palloc(sizeof(int) * data->nattnums);
1035  for (i = 0; i < data->nattnums; i++)
1036  typlen[i] = get_typlen(data->stats[i]->attrtypid);
1037 
1038  nrows = 0;
1039  for (i = 0; i < data->numrows; i++)
1040  {
1041  bool toowide = false;
1042 
1043  /* load the values/null flags from sample rows */
1044  for (j = 0; j < numattrs; j++)
1045  {
1046  Datum value;
1047  bool isnull;
1048  int attlen;
1049  AttrNumber attnum = attnums[j];
1050 
1051  int idx;
1052 
1053  /* match attnum to the pre-calculated data */
1054  for (idx = 0; idx < data->nattnums; idx++)
1055  {
1056  if (attnum == data->attnums[idx])
1057  break;
1058  }
1059 
1060  Assert(idx < data->nattnums);
1061 
1062  value = data->values[idx][i];
1063  isnull = data->nulls[idx][i];
1064  attlen = typlen[idx];
1065 
1066  /*
1067  * If this is a varlena value, check if it's too wide and if yes
1068  * then skip the whole item. Otherwise detoast the value.
1069  *
1070  * XXX It may happen that we've already detoasted some preceding
1071  * values for the current item. We don't bother to cleanup those
1072  * on the assumption that those are small (below WIDTH_THRESHOLD)
1073  * and will be discarded at the end of analyze.
1074  */
1075  if ((!isnull) && (attlen == -1))
1076  {
1078  {
1079  toowide = true;
1080  break;
1081  }
1082 
1084  }
1085 
1086  items[nrows].values[j] = value;
1087  items[nrows].isnull[j] = isnull;
1088  }
1089 
1090  if (toowide)
1091  continue;
1092 
1093  nrows++;
1094  }
1095 
1096  /* store the actual number of items (ignoring the too-wide ones) */
1097  *nitems = nrows;
1098 
1099  /* all items were too wide */
1100  if (nrows == 0)
1101  {
1102  /* everything is allocated as a single chunk */
1103  pfree(items);
1104  return NULL;
1105  }
1106 
1107  /* do the sort, using the multi-sort */
1108  qsort_interruptible(items, nrows, sizeof(SortItem),
1109  multi_sort_compare, mss);
1110 
1111  return items;
1112 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
unsigned char bool
Definition: c.h:445
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:545
#define WIDTH_THRESHOLD
int multi_sort_compare(const void *a, const void *b, void *arg)
struct SortItem SortItem
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
#define nitems(x)
Definition: indent.h:31
static struct @148 value
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2179
void pfree(void *pointer)
Definition: mcxt.c:1456
int16 attlen
Definition: pg_attribute.h:59
const void size_t len
const void * data
void qsort_interruptible(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64

References Assert(), attlen, attnum, data, get_typlen(), i, idx(), SortItem::isnull, j, len, multi_sort_compare(), nitems, palloc(), palloc0(), pfree(), PG_DETOAST_DATUM, PointerGetDatum(), qsort_interruptible(), toast_raw_datum_size(), value, values, SortItem::values, and WIDTH_THRESHOLD.

Referenced by dependency_degree(), and statext_mcv_build().

◆ BuildRelationExtStatistics()

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

Definition at line 115 of file extended_stats.c.

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

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, bms_num_members(), build_expr_data(), compute_expr_stats(), CurrentMemoryContext, data, elog(), ereport, errcode(), errmsg(), ERROR, errtable(), fetch_statentries_for_relation(), get_namespace_name(), IsAutoVacuumWorkerProcess(), lfirst, lfirst_int, list_free(), list_length(), lookup_var_attr_stats(), make_build_data(), MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), NIL, 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, serialize_expr_stats(), statext_compute_stattarget(), statext_dependencies_build(), statext_mcv_build(), statext_ndistinct_build(), statext_store(), table_close(), table_open(), val, and WARNING.

Referenced by do_analyze_rel().

◆ choose_best_statistics()

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

Definition at line 1210 of file extended_stats.c.

1213 {
1214  ListCell *lc;
1215  StatisticExtInfo *best_match = NULL;
1216  int best_num_matched = 2; /* goal #1: maximize */
1217  int best_match_keys = (STATS_MAX_DIMENSIONS + 1); /* goal #2: minimize */
1218 
1219  foreach(lc, stats)
1220  {
1221  int i;
1222  StatisticExtInfo *info = (StatisticExtInfo *) lfirst(lc);
1223  Bitmapset *matched_attnums = NULL;
1224  Bitmapset *matched_exprs = NULL;
1225  int num_matched;
1226  int numkeys;
1227 
1228  /* skip statistics that are not of the correct type */
1229  if (info->kind != requiredkind)
1230  continue;
1231 
1232  /* skip statistics with mismatching inheritance flag */
1233  if (info->inherit != inh)
1234  continue;
1235 
1236  /*
1237  * Collect attributes and expressions in remaining (unestimated)
1238  * clauses fully covered by this statistic object.
1239  *
1240  * We know already estimated clauses have both clause_attnums and
1241  * clause_exprs set to NULL. We leave the pointers NULL if already
1242  * estimated, or we reset them to NULL after estimating the clause.
1243  */
1244  for (i = 0; i < nclauses; i++)
1245  {
1246  Bitmapset *expr_idxs = NULL;
1247 
1248  /* ignore incompatible/estimated clauses */
1249  if (!clause_attnums[i] && !clause_exprs[i])
1250  continue;
1251 
1252  /* ignore clauses that are not covered by this object */
1253  if (!bms_is_subset(clause_attnums[i], info->keys) ||
1254  !stat_covers_expressions(info, clause_exprs[i], &expr_idxs))
1255  continue;
1256 
1257  /* record attnums and indexes of expressions covered */
1258  matched_attnums = bms_add_members(matched_attnums, clause_attnums[i]);
1259  matched_exprs = bms_add_members(matched_exprs, expr_idxs);
1260  }
1261 
1262  num_matched = bms_num_members(matched_attnums) + bms_num_members(matched_exprs);
1263 
1264  bms_free(matched_attnums);
1265  bms_free(matched_exprs);
1266 
1267  /*
1268  * save the actual number of keys in the stats so that we can choose
1269  * the narrowest stats with the most matching keys.
1270  */
1271  numkeys = bms_num_members(info->keys) + list_length(info->exprs);
1272 
1273  /*
1274  * Use this object when it increases the number of matched attributes
1275  * and expressions or when it matches the same number of attributes
1276  * and expressions but these stats have fewer keys than any previous
1277  * match.
1278  */
1279  if (num_matched > best_num_matched ||
1280  (num_matched == best_num_matched && numkeys < best_match_keys))
1281  {
1282  best_match = info;
1283  best_num_matched = num_matched;
1284  best_match_keys = numkeys;
1285  }
1286  }
1287 
1288  return best_match;
1289 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:363
void bms_free(Bitmapset *a)
Definition: bitmapset.c:194
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:835
static bool stat_covers_expressions(StatisticExtInfo *stat, List *exprs, Bitmapset **expr_idxs)
#define STATS_MAX_DIMENSIONS
Definition: statistics.h:19
Bitmapset * keys
Definition: pathnodes.h:1268

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

Referenced by statext_mcv_clauselist_selectivity().

◆ compare_datums_simple()

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

Definition at line 928 of file extended_stats.c.

929 {
930  return ApplySortComparator(a, false, b, false, ssup);
931 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
Definition: sortsupport.h:200

References a, ApplySortComparator(), and b.

Referenced by compare_scalars_simple(), and statext_mcv_serialize().

◆ compare_scalars_simple()

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

Definition at line 920 of file extended_stats.c.

921 {
922  return compare_datums_simple(*(Datum *) a,
923  *(Datum *) b,
924  (SortSupport) arg);
925 }
int compare_datums_simple(Datum a, Datum b, SortSupport ssup)
void * arg

References a, arg, b, and compare_datums_simple().

Referenced by statext_mcv_serialize().

◆ compute_expr_stats()

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

Definition at line 2111 of file extended_stats.c.

2114 {
2115  MemoryContext expr_context,
2116  old_context;
2117  int ind,
2118  i;
2119 
2121  "Analyze Expression",
2123  old_context = MemoryContextSwitchTo(expr_context);
2124 
2125  for (ind = 0; ind < nexprs; ind++)
2126  {
2127  AnlExprData *thisdata = &exprdata[ind];
2128  VacAttrStats *stats = thisdata->vacattrstat;
2129  Node *expr = thisdata->expr;
2130  TupleTableSlot *slot;
2131  EState *estate;
2132  ExprContext *econtext;
2133  Datum *exprvals;
2134  bool *exprnulls;
2135  ExprState *exprstate;
2136  int tcnt;
2137 
2138  /* Are we still in the main context? */
2139  Assert(CurrentMemoryContext == expr_context);
2140 
2141  /*
2142  * Need an EState for evaluation of expressions. Create it in the
2143  * per-expression context to be sure it gets cleaned up at the bottom
2144  * of the loop.
2145  */
2146  estate = CreateExecutorState();
2147  econtext = GetPerTupleExprContext(estate);
2148 
2149  /* Set up expression evaluation state */
2150  exprstate = ExecPrepareExpr((Expr *) expr, estate);
2151 
2152  /* Need a slot to hold the current heap tuple, too */
2154  &TTSOpsHeapTuple);
2155 
2156  /* Arrange for econtext's scan tuple to be the tuple under test */
2157  econtext->ecxt_scantuple = slot;
2158 
2159  /* Compute and save expression values */
2160  exprvals = (Datum *) palloc(numrows * sizeof(Datum));
2161  exprnulls = (bool *) palloc(numrows * sizeof(bool));
2162 
2163  tcnt = 0;
2164  for (i = 0; i < numrows; i++)
2165  {
2166  Datum datum;
2167  bool isnull;
2168 
2169  /*
2170  * Reset the per-tuple context each time, to reclaim any cruft
2171  * left behind by evaluating the statistics expressions.
2172  */
2173  ResetExprContext(econtext);
2174 
2175  /* Set up for expression evaluation */
2176  ExecStoreHeapTuple(rows[i], slot, false);
2177 
2178  /*
2179  * Evaluate the expression. We do this in the per-tuple context so
2180  * as not to leak memory, and then copy the result into the
2181  * context created at the beginning of this function.
2182  */
2183  datum = ExecEvalExprSwitchContext(exprstate,
2184  GetPerTupleExprContext(estate),
2185  &isnull);
2186  if (isnull)
2187  {
2188  exprvals[tcnt] = (Datum) 0;
2189  exprnulls[tcnt] = true;
2190  }
2191  else
2192  {
2193  /* Make sure we copy the data into the context. */
2194  Assert(CurrentMemoryContext == expr_context);
2195 
2196  exprvals[tcnt] = datumCopy(datum,
2197  stats->attrtype->typbyval,
2198  stats->attrtype->typlen);
2199  exprnulls[tcnt] = false;
2200  }
2201 
2202  tcnt++;
2203  }
2204 
2205  /*
2206  * Now we can compute the statistics for the expression columns.
2207  *
2208  * XXX Unlike compute_index_stats we don't need to switch and reset
2209  * memory contexts here, because we're only computing stats for a
2210  * single expression (and not iterating over many indexes), so we just
2211  * do it in expr_context. Note that compute_stats copies the result
2212  * into stats->anl_context, so it does not disappear.
2213  */
2214  if (tcnt > 0)
2215  {
2216  AttributeOpts *aopt =
2217  get_attribute_options(onerel->rd_id, stats->tupattnum);
2218 
2219  stats->exprvals = exprvals;
2220  stats->exprnulls = exprnulls;
2221  stats->rowstride = 1;
2222  stats->compute_stats(stats,
2224  tcnt,
2225  tcnt);
2226 
2227  /*
2228  * If the n_distinct option is specified, it overrides the above
2229  * computation.
2230  */
2231  if (aopt != NULL && aopt->n_distinct != 0.0)
2232  stats->stadistinct = aopt->n_distinct;
2233  }
2234 
2235  /* And clean up */
2236  MemoryContextSwitchTo(expr_context);
2237 
2239  FreeExecutorState(estate);
2241  }
2242 
2243  MemoryContextSwitchTo(old_context);
2244  MemoryContextDelete(expr_context);
2245 }
AttributeOpts * get_attribute_options(Oid attrelid, int attnum)
Definition: attoptcache.c:104
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execExpr.c:736
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1255
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1353
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1239
EState * CreateExecutorState(void)
Definition: execUtils.c:93
void FreeExecutorState(EState *estate)
Definition: execUtils.c:194
#define GetPerTupleExprContext(estate)
Definition: executor.h:549
#define ResetExprContext(econtext)
Definition: executor.h:543
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:347
static Datum expr_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull)
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:70
#define RelationGetDescr(relation)
Definition: rel.h:530
float8 n_distinct
Definition: attoptcache.h:22
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:249
Oid rd_id
Definition: rel.h:113
Form_pg_type attrtype
Definition: vacuum.h:127
int tupattnum
Definition: vacuum.h:170
int rowstride
Definition: vacuum.h:175
bool * exprnulls
Definition: vacuum.h:174
Datum * exprvals
Definition: vacuum.h:173
float4 stadistinct
Definition: vacuum.h:146
AnalyzeAttrComputeStatsFunc compute_stats
Definition: vacuum.h:135

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), 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(), RelationData::rd_id, RelationGetDescr, ResetExprContext, VacAttrStats::rowstride, VacAttrStats::stadistinct, TTSOpsHeapTuple, VacAttrStats::tupattnum, and AnlExprData::vacattrstat.

Referenced by BuildRelationExtStatistics().

◆ ComputeExtStatisticsRows()

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

Definition at line 268 of file extended_stats.c.

270 {
271  Relation pg_stext;
272  ListCell *lc;
273  List *lstats;
274  MemoryContext cxt;
275  MemoryContext oldcxt;
276  int result = 0;
277 
278  /* If there are no columns to analyze, just return 0. */
279  if (!natts)
280  return 0;
281 
283  "ComputeExtStatisticsRows",
285  oldcxt = MemoryContextSwitchTo(cxt);
286 
287  pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
288  lstats = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
289 
290  foreach(lc, lstats)
291  {
293  int stattarget;
294  VacAttrStats **stats;
295  int nattrs = bms_num_members(stat->columns);
296 
297  /*
298  * Check if we can build this statistics object based on the columns
299  * analyzed. If not, ignore it (don't report anything, we'll do that
300  * during the actual build BuildRelationExtStatistics).
301  */
302  stats = lookup_var_attr_stats(onerel, stat->columns, stat->exprs,
303  natts, vacattrstats);
304 
305  if (!stats)
306  continue;
307 
308  /*
309  * Compute statistics target, based on what's set for the statistic
310  * object itself, and for its attributes.
311  */
312  stattarget = statext_compute_stattarget(stat->stattarget,
313  nattrs, stats);
314 
315  /* Use the largest value for all statistics objects. */
316  if (stattarget > result)
317  result = stattarget;
318  }
319 
320  table_close(pg_stext, RowExclusiveLock);
321 
322  MemoryContextSwitchTo(oldcxt);
323  MemoryContextDelete(cxt);
324 
325  /* compute sample size based on the statistics target */
326  return (300 * result);
327 }

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

Referenced by do_analyze_rel().

◆ examine_attribute()

static VacAttrStats* examine_attribute ( Node expr)
static

Definition at line 529 of file extended_stats.c.

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

References VacAttrStats::anl_context, VacAttrStats::attrcollid, VacAttrStats::attrtype, VacAttrStats::attrtypid, VacAttrStats::attrtypmod, VacAttrStats::attstattarget, 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().

◆ examine_expression()

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

Definition at line 608 of file extended_stats.c.

609 {
610  HeapTuple typtuple;
611  VacAttrStats *stats;
612  int i;
613  bool ok;
614 
615  Assert(expr != NULL);
616 
617  /*
618  * Create the VacAttrStats struct.
619  */
620  stats = (VacAttrStats *) palloc0(sizeof(VacAttrStats));
621 
622  /*
623  * We can't have statistics target specified for the expression, so we
624  * could use either the default_statistics_target, or the target computed
625  * for the extended statistics. The second option seems more reasonable.
626  */
627  stats->attstattarget = stattarget;
628 
629  /*
630  * When analyzing an expression, believe the expression tree's type.
631  */
632  stats->attrtypid = exprType(expr);
633  stats->attrtypmod = exprTypmod(expr);
634 
635  /*
636  * We don't allow collation to be specified in CREATE STATISTICS, so we
637  * have to use the collation specified for the expression. It's possible
638  * to specify the collation in the expression "(col COLLATE "en_US")" in
639  * which case exprCollation() does the right thing.
640  */
641  stats->attrcollid = exprCollation(expr);
642 
643  typtuple = SearchSysCacheCopy1(TYPEOID,
644  ObjectIdGetDatum(stats->attrtypid));
645  if (!HeapTupleIsValid(typtuple))
646  elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
647 
648  stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
649  stats->anl_context = CurrentMemoryContext; /* XXX should be using
650  * something else? */
651  stats->tupattnum = InvalidAttrNumber;
652 
653  /*
654  * The fields describing the stats->stavalues[n] element types default to
655  * the type of the data being analyzed, but the type-specific typanalyze
656  * function can change them if it wants to store something else.
657  */
658  for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
659  {
660  stats->statypid[i] = stats->attrtypid;
661  stats->statyplen[i] = stats->attrtype->typlen;
662  stats->statypbyval[i] = stats->attrtype->typbyval;
663  stats->statypalign[i] = stats->attrtype->typalign;
664  }
665 
666  /*
667  * Call the type-specific typanalyze function. If none is specified, use
668  * std_typanalyze().
669  */
670  if (OidIsValid(stats->attrtype->typanalyze))
671  ok = DatumGetBool(OidFunctionCall1(stats->attrtype->typanalyze,
672  PointerGetDatum(stats)));
673  else
674  ok = std_typanalyze(stats);
675 
676  if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
677  {
678  heap_freetuple(typtuple);
679  pfree(stats);
680  return NULL;
681  }
682 
683  return stats;
684 }

References VacAttrStats::anl_context, Assert(), VacAttrStats::attrcollid, VacAttrStats::attrtype, VacAttrStats::attrtypid, VacAttrStats::attrtypmod, VacAttrStats::attstattarget, VacAttrStats::compute_stats, CurrentMemoryContext, 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 build_expr_data(), and make_build_data().

◆ examine_opclause_args()

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

Definition at line 2056 of file extended_stats.c.

2058 {
2059  Node *expr;
2060  Const *cst;
2061  bool expronleft;
2062  Node *leftop,
2063  *rightop;
2064 
2065  /* enforced by statext_is_compatible_clause_internal */
2066  Assert(list_length(args) == 2);
2067 
2068  leftop = linitial(args);
2069  rightop = lsecond(args);
2070 
2071  /* strip RelabelType from either side of the expression */
2072  if (IsA(leftop, RelabelType))
2073  leftop = (Node *) ((RelabelType *) leftop)->arg;
2074 
2075  if (IsA(rightop, RelabelType))
2076  rightop = (Node *) ((RelabelType *) rightop)->arg;
2077 
2078  if (IsA(rightop, Const))
2079  {
2080  expr = (Node *) leftop;
2081  cst = (Const *) rightop;
2082  expronleft = true;
2083  }
2084  else if (IsA(leftop, Const))
2085  {
2086  expr = (Node *) rightop;
2087  cst = (Const *) leftop;
2088  expronleft = false;
2089  }
2090  else
2091  return false;
2092 
2093  /* return pointers to the extracted parts if requested */
2094  if (exprp)
2095  *exprp = expr;
2096 
2097  if (cstp)
2098  *cstp = cst;
2099 
2100  if (expronleftp)
2101  *expronleftp = expronleft;
2102 
2103  return true;
2104 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183

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

Referenced by mcv_get_match_bitmap(), and statext_is_compatible_clause_internal().

◆ expr_fetch_func()

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

Definition at line 2255 of file extended_stats.c.

2256 {
2257  int i;
2258 
2259  /* exprvals and exprnulls are already offset for proper column */
2260  i = rownum * stats->rowstride;
2261  *isNull = stats->exprnulls[i];
2262  return stats->exprvals[i];
2263 }

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

Referenced by compute_expr_stats().

◆ fetch_statentries_for_relation()

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

Definition at line 425 of file extended_stats.c.

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

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, StatExtEntry::statOid, StatExtEntry::stattarget, stringToNode(), SysCacheGetAttr(), SysCacheGetAttrNotNull(), systable_beginscan(), systable_endscan(), systable_getnext(), TextDatumGetCString, and StatExtEntry::types.

Referenced by BuildRelationExtStatistics(), and ComputeExtStatisticsRows().

◆ has_stats_of_kind()

bool has_stats_of_kind ( List stats,
char  requiredkind 
)

Definition at line 1119 of file extended_stats.c.

1120 {
1121  ListCell *l;
1122 
1123  foreach(l, stats)
1124  {
1126 
1127  if (stat->kind == requiredkind)
1128  return true;
1129  }
1130 
1131  return false;
1132 }

References lfirst.

Referenced by dependencies_clauselist_selectivity(), and statext_mcv_clauselist_selectivity().

◆ lookup_var_attr_stats()

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

Definition at line 694 of file extended_stats.c.

696 {
697  int i = 0;
698  int x = -1;
699  int natts;
700  VacAttrStats **stats;
701  ListCell *lc;
702 
703  natts = bms_num_members(attrs) + list_length(exprs);
704 
705  stats = (VacAttrStats **) palloc(natts * sizeof(VacAttrStats *));
706 
707  /* lookup VacAttrStats info for the requested columns (same attnum) */
708  while ((x = bms_next_member(attrs, x)) >= 0)
709  {
710  int j;
711 
712  stats[i] = NULL;
713  for (j = 0; j < nvacatts; j++)
714  {
715  if (x == vacatts[j]->tupattnum)
716  {
717  stats[i] = vacatts[j];
718  break;
719  }
720  }
721 
722  if (!stats[i])
723  {
724  /*
725  * Looks like stats were not gathered for one of the columns
726  * required. We'll be unable to build the extended stats without
727  * this column.
728  */
729  pfree(stats);
730  return NULL;
731  }
732 
733  i++;
734  }
735 
736  /* also add info for expressions */
737  foreach(lc, exprs)
738  {
739  Node *expr = (Node *) lfirst(lc);
740 
741  stats[i] = examine_attribute(expr);
742 
743  /*
744  * XXX We need tuple descriptor later, and we just grab it from
745  * stats[0]->tupDesc (see e.g. statext_mcv_build). But as coded
746  * examine_attribute does not set that, so just grab it from the first
747  * vacatts element.
748  */
749  stats[i]->tupDesc = vacatts[0]->tupDesc;
750 
751  i++;
752  }
753 
754  return stats;
755 }
static VacAttrStats * examine_attribute(Node *expr)
int x
Definition: isn.c:71
TupleDesc tupDesc
Definition: vacuum.h:172

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

Referenced by BuildRelationExtStatistics(), and ComputeExtStatisticsRows().

◆ make_build_data()

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

Definition at line 2473 of file extended_stats.c.

2475 {
2476  /* evaluated expressions */
2477  StatsBuildData *result;
2478  char *ptr;
2479  Size len;
2480 
2481  int i;
2482  int k;
2483  int idx;
2484  TupleTableSlot *slot;
2485  EState *estate;
2486  ExprContext *econtext;
2487  List *exprstates = NIL;
2488  int nkeys = bms_num_members(stat->columns) + list_length(stat->exprs);
2489  ListCell *lc;
2490 
2491  /* allocate everything as a single chunk, so we can free it easily */
2492  len = MAXALIGN(sizeof(StatsBuildData));
2493  len += MAXALIGN(sizeof(AttrNumber) * nkeys); /* attnums */
2494  len += MAXALIGN(sizeof(VacAttrStats *) * nkeys); /* stats */
2495 
2496  /* values */
2497  len += MAXALIGN(sizeof(Datum *) * nkeys);
2498  len += nkeys * MAXALIGN(sizeof(Datum) * numrows);
2499 
2500  /* nulls */
2501  len += MAXALIGN(sizeof(bool *) * nkeys);
2502  len += nkeys * MAXALIGN(sizeof(bool) * numrows);
2503 
2504  ptr = palloc(len);
2505 
2506  /* set the pointers */
2507  result = (StatsBuildData *) ptr;
2508  ptr += MAXALIGN(sizeof(StatsBuildData));
2509 
2510  /* attnums */
2511  result->attnums = (AttrNumber *) ptr;
2512  ptr += MAXALIGN(sizeof(AttrNumber) * nkeys);
2513 
2514  /* stats */
2515  result->stats = (VacAttrStats **) ptr;
2516  ptr += MAXALIGN(sizeof(VacAttrStats *) * nkeys);
2517 
2518  /* values */
2519  result->values = (Datum **) ptr;
2520  ptr += MAXALIGN(sizeof(Datum *) * nkeys);
2521 
2522  /* nulls */
2523  result->nulls = (bool **) ptr;
2524  ptr += MAXALIGN(sizeof(bool *) * nkeys);
2525 
2526  for (i = 0; i < nkeys; i++)
2527  {
2528  result->values[i] = (Datum *) ptr;
2529  ptr += MAXALIGN(sizeof(Datum) * numrows);
2530 
2531  result->nulls[i] = (bool *) ptr;
2532  ptr += MAXALIGN(sizeof(bool) * numrows);
2533  }
2534 
2535  Assert((ptr - (char *) result) == len);
2536 
2537  /* we have it allocated, so let's fill the values */
2538  result->nattnums = nkeys;
2539  result->numrows = numrows;
2540 
2541  /* fill the attribute info - first attributes, then expressions */
2542  idx = 0;
2543  k = -1;
2544  while ((k = bms_next_member(stat->columns, k)) >= 0)
2545  {
2546  result->attnums[idx] = k;
2547  result->stats[idx] = stats[idx];
2548 
2549  idx++;
2550  }
2551 
2552  k = -1;
2553  foreach(lc, stat->exprs)
2554  {
2555  Node *expr = (Node *) lfirst(lc);
2556 
2557  result->attnums[idx] = k;
2558  result->stats[idx] = examine_expression(expr, stattarget);
2559 
2560  idx++;
2561  k--;
2562  }
2563 
2564  /* first extract values for all the regular attributes */
2565  for (i = 0; i < numrows; i++)
2566  {
2567  idx = 0;
2568  k = -1;
2569  while ((k = bms_next_member(stat->columns, k)) >= 0)
2570  {
2571  result->values[idx][i] = heap_getattr(rows[i], k,
2572  result->stats[idx]->tupDesc,
2573  &result->nulls[idx][i]);
2574 
2575  idx++;
2576  }
2577  }
2578 
2579  /* Need an EState for evaluation expressions. */
2580  estate = CreateExecutorState();
2581  econtext = GetPerTupleExprContext(estate);
2582 
2583  /* Need a slot to hold the current heap tuple, too */
2585  &TTSOpsHeapTuple);
2586 
2587  /* Arrange for econtext's scan tuple to be the tuple under test */
2588  econtext->ecxt_scantuple = slot;
2589 
2590  /* Set up expression evaluation state */
2591  exprstates = ExecPrepareExprList(stat->exprs, estate);
2592 
2593  for (i = 0; i < numrows; i++)
2594  {
2595  /*
2596  * Reset the per-tuple context each time, to reclaim any cruft left
2597  * behind by evaluating the statistics object expressions.
2598  */
2599  ResetExprContext(econtext);
2600 
2601  /* Set up for expression evaluation */
2602  ExecStoreHeapTuple(rows[i], slot, false);
2603 
2604  idx = bms_num_members(stat->columns);
2605  foreach(lc, exprstates)
2606  {
2607  Datum datum;
2608  bool isnull;
2609  ExprState *exprstate = (ExprState *) lfirst(lc);
2610 
2611  /*
2612  * XXX This probably leaks memory. Maybe we should use
2613  * ExecEvalExprSwitchContext but then we need to copy the result
2614  * somewhere else.
2615  */
2616  datum = ExecEvalExpr(exprstate,
2617  GetPerTupleExprContext(estate),
2618  &isnull);
2619  if (isnull)
2620  {
2621  result->values[idx][i] = (Datum) 0;
2622  result->nulls[idx][i] = true;
2623  }
2624  else
2625  {
2626  result->values[idx][i] = (Datum) datum;
2627  result->nulls[idx][i] = false;
2628  }
2629 
2630  idx++;
2631  }
2632  }
2633 
2635  FreeExecutorState(estate);
2636 
2637  return result;
2638 }
#define MAXALIGN(LEN)
Definition: c.h:800
size_t Size
Definition: c.h:594
List * ExecPrepareExprList(List *nodes, EState *estate)
Definition: execExpr.c:810
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:332
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:792
VacAttrStats ** stats

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

Referenced by BuildRelationExtStatistics().

◆ multi_sort_add_dimension()

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

Definition at line 852 of file extended_stats.c.

854 {
855  SortSupport ssup = &mss->ssup[sortdim];
856 
858  ssup->ssup_collation = collation;
859  ssup->ssup_nulls_first = false;
860 
862 }
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition: parse_oper.c:382
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Definition: sortsupport.c:135
SortSupportData ssup[FLEXIBLE_ARRAY_MEMBER]
bool ssup_nulls_first
Definition: sortsupport.h:75
MemoryContext ssup_cxt
Definition: sortsupport.h:66

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

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

◆ multi_sort_compare()

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

Definition at line 866 of file extended_stats.c.

867 {
869  SortItem *ia = (SortItem *) a;
870  SortItem *ib = (SortItem *) b;
871  int i;
872 
873  for (i = 0; i < mss->ndims; i++)
874  {
875  int compare;
876 
878  ib->values[i], ib->isnull[i],
879  &mss->ssup[i]);
880 
881  if (compare != 0)
882  return compare;
883  }
884 
885  /* equal by default */
886  return 0;
887 }
MultiSortSupportData * MultiSortSupport
static int compare(const void *arg1, const void *arg2)
Definition: geqo_pool.c:145

References a, ApplySortComparator(), arg, b, 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().

◆ multi_sort_compare_dim()

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

Definition at line 891 of file extended_stats.c.

893 {
894  return ApplySortComparator(a->values[dim], a->isnull[dim],
895  b->values[dim], b->isnull[dim],
896  &mss->ssup[dim]);
897 }

References a, ApplySortComparator(), b, and MultiSortSupportData::ssup.

Referenced by dependency_degree().

◆ multi_sort_compare_dims()

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

Definition at line 900 of file extended_stats.c.

903 {
904  int dim;
905 
906  for (dim = start; dim <= end; dim++)
907  {
908  int r = ApplySortComparator(a->values[dim], a->isnull[dim],
909  b->values[dim], b->isnull[dim],
910  &mss->ssup[dim]);
911 
912  if (r != 0)
913  return r;
914  }
915 
916  return 0;
917 }

References a, ApplySortComparator(), b, and MultiSortSupportData::ssup.

Referenced by dependency_degree().

◆ multi_sort_init()

MultiSortSupport multi_sort_init ( int  ndims)

Definition at line 833 of file extended_stats.c.

834 {
835  MultiSortSupport mss;
836 
837  Assert(ndims >= 2);
838 
839  mss = (MultiSortSupport) palloc0(offsetof(MultiSortSupportData, ssup)
840  + sizeof(SortSupportData) * ndims);
841 
842  mss->ndims = ndims;
843 
844  return mss;
845 }
struct SortSupportData SortSupportData

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

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

◆ serialize_expr_stats()

static Datum serialize_expr_stats ( AnlExprData exprdata,
int  nexprs 
)
static

Definition at line 2296 of file extended_stats.c.

2297 {
2298  int exprno;
2299  Oid typOid;
2300  Relation sd;
2301 
2302  ArrayBuildState *astate = NULL;
2303 
2304  sd = table_open(StatisticRelationId, RowExclusiveLock);
2305 
2306  /* lookup OID of composite type for pg_statistic */
2307  typOid = get_rel_type_id(StatisticRelationId);
2308  if (!OidIsValid(typOid))
2309  ereport(ERROR,
2310  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2311  errmsg("relation \"%s\" does not have a composite type",
2312  "pg_statistic")));
2313 
2314  for (exprno = 0; exprno < nexprs; exprno++)
2315  {
2316  int i,
2317  k;
2318  VacAttrStats *stats = exprdata[exprno].vacattrstat;
2319 
2320  Datum values[Natts_pg_statistic];
2321  bool nulls[Natts_pg_statistic];
2322  HeapTuple stup;
2323 
2324  if (!stats->stats_valid)
2325  {
2326  astate = accumArrayResult(astate,
2327  (Datum) 0,
2328  true,
2329  typOid,
2331  continue;
2332  }
2333 
2334  /*
2335  * Construct a new pg_statistic tuple
2336  */
2337  for (i = 0; i < Natts_pg_statistic; ++i)
2338  {
2339  nulls[i] = false;
2340  }
2341 
2342  values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(InvalidOid);
2343  values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(InvalidAttrNumber);
2344  values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(false);
2345  values[Anum_pg_statistic_stanullfrac - 1] = Float4GetDatum(stats->stanullfrac);
2346  values[Anum_pg_statistic_stawidth - 1] = Int32GetDatum(stats->stawidth);
2347  values[Anum_pg_statistic_stadistinct - 1] = Float4GetDatum(stats->stadistinct);
2348  i = Anum_pg_statistic_stakind1 - 1;
2349  for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2350  {
2351  values[i++] = Int16GetDatum(stats->stakind[k]); /* stakindN */
2352  }
2353  i = Anum_pg_statistic_staop1 - 1;
2354  for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2355  {
2356  values[i++] = ObjectIdGetDatum(stats->staop[k]); /* staopN */
2357  }
2358  i = Anum_pg_statistic_stacoll1 - 1;
2359  for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2360  {
2361  values[i++] = ObjectIdGetDatum(stats->stacoll[k]); /* stacollN */
2362  }
2363  i = Anum_pg_statistic_stanumbers1 - 1;
2364  for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2365  {
2366  int nnum = stats->numnumbers[k];
2367 
2368  if (nnum > 0)
2369  {
2370  int n;
2371  Datum *numdatums = (Datum *) palloc(nnum * sizeof(Datum));
2372  ArrayType *arry;
2373 
2374  for (n = 0; n < nnum; n++)
2375  numdatums[n] = Float4GetDatum(stats->stanumbers[k][n]);
2376  arry = construct_array_builtin(numdatums, nnum, FLOAT4OID);
2377  values[i++] = PointerGetDatum(arry); /* stanumbersN */
2378  }
2379  else
2380  {
2381  nulls[i] = true;
2382  values[i++] = (Datum) 0;
2383  }
2384  }
2385  i = Anum_pg_statistic_stavalues1 - 1;
2386  for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2387  {
2388  if (stats->numvalues[k] > 0)
2389  {
2390  ArrayType *arry;
2391 
2392  arry = construct_array(stats->stavalues[k],
2393  stats->numvalues[k],
2394  stats->statypid[k],
2395  stats->statyplen[k],
2396  stats->statypbyval[k],
2397  stats->statypalign[k]);
2398  values[i++] = PointerGetDatum(arry); /* stavaluesN */
2399  }
2400  else
2401  {
2402  nulls[i] = true;
2403  values[i++] = (Datum) 0;
2404  }
2405  }
2406 
2407  stup = heap_form_tuple(RelationGetDescr(sd), values, nulls);
2408 
2409  astate = accumArrayResult(astate,
2411  false,
2412  typOid,
2414  }
2415 
2417 
2418  return makeArrayResult(astate, CurrentMemoryContext);
2419 }
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5297
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3340
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3320
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5367
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1108
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
Definition: heaptuple.c:1072
Oid get_rel_type_id(Oid relid)
Definition: lsyscache.c:1983
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:475
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:172
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
bool stats_valid
Definition: vacuum.h:143
float4 stanullfrac
Definition: vacuum.h:144
int16 stakind[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:147
Oid staop[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:148
Oid stacoll[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:149
float4 * stanumbers[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:151
int32 stawidth
Definition: vacuum.h:145
int numvalues[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:152
Datum * stavalues[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:153
int numnumbers[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:150

References accumArrayResult(), BoolGetDatum(), construct_array(), construct_array_builtin(), 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().

◆ stat_covers_expressions()

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

Definition at line 1169 of file extended_stats.c.

1171 {
1172  ListCell *lc;
1173 
1174  foreach(lc, exprs)
1175  {
1176  Node *expr = (Node *) lfirst(lc);
1177  int expr_idx;
1178 
1179  expr_idx = stat_find_expression(stat, expr);
1180  if (expr_idx == -1)
1181  return false;
1182 
1183  if (expr_idxs != NULL)
1184  *expr_idxs = bms_add_member(*expr_idxs, expr_idx);
1185  }
1186 
1187  /* If we reach here, all expressions are covered */
1188  return true;
1189 }
static int stat_find_expression(StatisticExtInfo *stat, Node *expr)

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

Referenced by choose_best_statistics(), and statext_mcv_clauselist_selectivity().

◆ stat_find_expression()

static int stat_find_expression ( StatisticExtInfo stat,
Node expr 
)
static

Definition at line 1142 of file extended_stats.c.

1143 {
1144  ListCell *lc;
1145  int idx;
1146 
1147  idx = 0;
1148  foreach(lc, stat->exprs)
1149  {
1150  Node *stat_expr = (Node *) lfirst(lc);
1151 
1152  if (equal(stat_expr, expr))
1153  return idx;
1154  idx++;
1155  }
1156 
1157  /* Expression not found */
1158  return -1;
1159 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223

References equal(), idx(), and lfirst.

Referenced by stat_covers_expressions().

◆ 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 2005 of file extended_stats.c.

2009 {
2010  Selectivity sel;
2011 
2012  /* First, try estimating clauses using a multivariate MCV list. */
2013  sel = statext_mcv_clauselist_selectivity(root, clauses, varRelid, jointype,
2014  sjinfo, rel, estimatedclauses, is_or);
2015 
2016  /*
2017  * Functional dependencies only work for clauses connected by AND, so for
2018  * OR clauses we're done.
2019  */
2020  if (is_or)
2021  return sel;
2022 
2023  /*
2024  * Then, apply functional dependencies on the remaining clauses by calling
2025  * dependencies_clauselist_selectivity. Pass 'estimatedclauses' so the
2026  * function can properly skip clauses already estimated above.
2027  *
2028  * The reasoning for applying dependencies last is that the more complex
2029  * stats can track more complex correlations between the attributes, and
2030  * so may be considered more reliable.
2031  *
2032  * For example, MCV list can give us an exact selectivity for values in
2033  * two columns, while functional dependencies can only provide information
2034  * about the overall strength of the dependency.
2035  */
2036  sel *= dependencies_clauselist_selectivity(root, clauses, varRelid,
2037  jointype, sjinfo, rel,
2038  estimatedclauses);
2039 
2040  return sel;
2041 }
Selectivity dependencies_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
static Selectivity statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses, bool is_or)
double Selectivity
Definition: nodes.h:261

References dependencies_clauselist_selectivity(), and statext_mcv_clauselist_selectivity().

Referenced by clauselist_selectivity_ext(), and clauselist_selectivity_or().

◆ statext_compute_stattarget()

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

Definition at line 350 of file extended_stats.c.

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

References Assert(), VacAttrStats::attstattarget, default_statistics_target, i, and MAX_STATISTICS_TARGET.

Referenced by BuildRelationExtStatistics(), and ComputeExtStatisticsRows().

◆ statext_expressions_load()

HeapTuple statext_expressions_load ( Oid  stxoid,
bool  inh,
int  idx 
)

Definition at line 2426 of file extended_stats.c.

2427 {
2428  bool isnull;
2429  Datum value;
2430  HeapTuple htup;
2431  ExpandedArrayHeader *eah;
2432  HeapTupleHeader td;
2433  HeapTupleData tmptup;
2434  HeapTuple tup;
2435 
2437  ObjectIdGetDatum(stxoid), BoolGetDatum(inh));
2438  if (!HeapTupleIsValid(htup))
2439  elog(ERROR, "cache lookup failed for statistics object %u", stxoid);
2440 
2442  Anum_pg_statistic_ext_data_stxdexpr, &isnull);
2443  if (isnull)
2444  elog(ERROR,
2445  "requested statistics kind \"%c\" is not yet built for statistics object %u",
2446  STATS_EXT_DEPENDENCIES, stxoid);
2447 
2449 
2451 
2452  td = DatumGetHeapTupleHeader(eah->dvalues[idx]);
2453 
2454  /* Build a temporary HeapTuple control structure */
2455  tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
2456  ItemPointerSetInvalid(&(tmptup.t_self));
2457  tmptup.t_tableOid = InvalidOid;
2458  tmptup.t_data = td;
2459 
2460  tup = heap_copytuple(&tmptup);
2461 
2462  ReleaseSysCache(htup);
2463 
2464  return tup;
2465 }
ExpandedArrayHeader * DatumGetExpandedArray(Datum d)
void deconstruct_expanded_array(ExpandedArrayHeader *eah)
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:768
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:450
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition: itemptr.h:184
Datum * dvalues
Definition: array.h:139
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
Oid t_tableOid
Definition: htup.h:66
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:868
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:831
@ STATEXTDATASTXOID
Definition: syscache.h:94

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

Referenced by examine_variable().

◆ statext_is_compatible_clause()

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

Definition at line 1573 of file extended_stats.c.

1575 {
1576  RangeTblEntry *rte = root->simple_rte_array[relid];
1577  RelOptInfo *rel = root->simple_rel_array[relid];
1578  RestrictInfo *rinfo;
1579  int clause_relid;
1580  Oid userid;
1581 
1582  /*
1583  * Special-case handling for bare BoolExpr AND clauses, because the
1584  * restrictinfo machinery doesn't build RestrictInfos on top of AND
1585  * clauses.
1586  */
1587  if (is_andclause(clause))
1588  {
1589  BoolExpr *expr = (BoolExpr *) clause;
1590  ListCell *lc;
1591 
1592  /*
1593  * Check that each sub-clause is compatible. We expect these to be
1594  * RestrictInfos.
1595  */
1596  foreach(lc, expr->args)
1597  {
1598  if (!statext_is_compatible_clause(root, (Node *) lfirst(lc),
1599  relid, attnums, exprs))
1600  return false;
1601  }
1602 
1603  return true;
1604  }
1605 
1606  /* Otherwise it must be a RestrictInfo. */
1607  if (!IsA(clause, RestrictInfo))
1608  return false;
1609  rinfo = (RestrictInfo *) clause;
1610 
1611  /* Pseudoconstants are not really interesting here. */
1612  if (rinfo->pseudoconstant)
1613  return false;
1614 
1615  /* Clauses referencing other varnos are incompatible. */
1616  if (!bms_get_singleton_member(rinfo->clause_relids, &clause_relid) ||
1617  clause_relid != relid)
1618  return false;
1619 
1620  /* Check the clause and determine what attributes it references. */
1621  if (!statext_is_compatible_clause_internal(root, (Node *) rinfo->clause,
1622  relid, attnums, exprs))
1623  return false;
1624 
1625  /*
1626  * Check that the user has permission to read all required attributes.
1627  */
1628  userid = OidIsValid(rel->userid) ? rel->userid : GetUserId();
1629 
1630  /* Table-level SELECT privilege is sufficient for all columns */
1631  if (pg_class_aclcheck(rte->relid, userid, ACL_SELECT) != ACLCHECK_OK)
1632  {
1633  Bitmapset *clause_attnums = NULL;
1634  int attnum = -1;
1635 
1636  /*
1637  * We have to check per-column privileges. *attnums has the attnums
1638  * for individual Vars we saw, but there may also be Vars within
1639  * subexpressions in *exprs. We can use pull_varattnos() to extract
1640  * those, but there's an impedance mismatch: attnums returned by
1641  * pull_varattnos() are offset by FirstLowInvalidHeapAttributeNumber,
1642  * while attnums within *attnums aren't. Convert *attnums to the
1643  * offset style so we can combine the results.
1644  */
1645  while ((attnum = bms_next_member(*attnums, attnum)) >= 0)
1646  {
1647  clause_attnums =
1648  bms_add_member(clause_attnums,
1650  }
1651 
1652  /* Now merge attnums from *exprs into clause_attnums */
1653  if (*exprs != NIL)
1654  pull_varattnos((Node *) *exprs, relid, &clause_attnums);
1655 
1656  attnum = -1;
1657  while ((attnum = bms_next_member(clause_attnums, attnum)) >= 0)
1658  {
1659  /* Undo the offset */
1661 
1662  if (attno == InvalidAttrNumber)
1663  {
1664  /* Whole-row reference, so must have access to all columns */
1665  if (pg_attribute_aclcheck_all(rte->relid, userid, ACL_SELECT,
1667  return false;
1668  }
1669  else
1670  {
1671  if (pg_attribute_aclcheck(rte->relid, attno, userid,
1672  ACL_SELECT) != ACLCHECK_OK)
1673  return false;
1674  }
1675  }
1676  }
1677 
1678  /* If we reach here, the clause is OK */
1679  return true;
1680 }
@ ACLCHECK_OK
Definition: acl.h:182
@ ACLMASK_ALL
Definition: acl.h:175
AclResult pg_attribute_aclcheck_all(Oid table_oid, Oid roleid, AclMode mode, AclMaskHow how)
Definition: aclchk.c:3823
AclResult pg_attribute_aclcheck(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode)
Definition: aclchk.c:3779
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:3908
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:652
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)
Oid GetUserId(void)
Definition: miscinit.c:509
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:105
#define ACL_SELECT
Definition: parsenodes.h:84
List * args
Definition: primnodes.h:867
Oid userid
Definition: pathnodes.h:945
Expr * clause
Definition: pathnodes.h:2529
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:291

References ACL_SELECT, ACLCHECK_OK, ACLMASK_ALL, BoolExpr::args, attnum, bms_add_member(), bms_get_singleton_member(), bms_next_member(), RestrictInfo::clause, FirstLowInvalidHeapAttributeNumber, GetUserId(), InvalidAttrNumber, is_andclause(), IsA, lfirst, NIL, OidIsValid, pg_attribute_aclcheck(), pg_attribute_aclcheck_all(), pg_class_aclcheck(), pull_varattnos(), RangeTblEntry::relid, statext_is_compatible_clause_internal(), and RelOptInfo::userid.

Referenced by statext_mcv_clauselist_selectivity().

◆ 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 1329 of file extended_stats.c.

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

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, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by statext_is_compatible_clause().

◆ statext_is_kind_built()

bool statext_is_kind_built ( HeapTuple  htup,
char  type 
)

Definition at line 392 of file extended_stats.c.

393 {
395 
396  switch (type)
397  {
398  case STATS_EXT_NDISTINCT:
399  attnum = Anum_pg_statistic_ext_data_stxdndistinct;
400  break;
401 
402  case STATS_EXT_DEPENDENCIES:
403  attnum = Anum_pg_statistic_ext_data_stxddependencies;
404  break;
405 
406  case STATS_EXT_MCV:
407  attnum = Anum_pg_statistic_ext_data_stxdmcv;
408  break;
409 
410  case STATS_EXT_EXPRESSIONS:
411  attnum = Anum_pg_statistic_ext_data_stxdexpr;
412  break;
413 
414  default:
415  elog(ERROR, "unexpected statistics type requested: %d", type);
416  }
417 
418  return !heap_attisnull(htup, attnum, NULL);
419 }
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:447
const char * type

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

Referenced by get_relation_statistics_worker().

◆ 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 1717 of file extended_stats.c.

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

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(), has_stats_of_kind(), RangeTblEntry::inh, lappend(), lfirst, list_free(), list_length(), mcv_clause_selectivity_or(), mcv_clauselist_selectivity(), mcv_combine_selectivities(), NIL, palloc(), planner_rt_fetch, RelOptInfo::relid, stat, stat_covers_expressions(), statext_is_compatible_clause(), statext_mcv_load(), and RelOptInfo::statlist.

Referenced by statext_clauselist_selectivity().

◆ statext_store()

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

Definition at line 763 of file extended_stats.c.

766 {
767  Relation pg_stextdata;
768  HeapTuple stup;
769  Datum values[Natts_pg_statistic_ext_data];
770  bool nulls[Natts_pg_statistic_ext_data];
771 
772  pg_stextdata = table_open(StatisticExtDataRelationId, RowExclusiveLock);
773 
774  memset(nulls, true, sizeof(nulls));
775  memset(values, 0, sizeof(values));
776 
777  /* basic info */
778  values[Anum_pg_statistic_ext_data_stxoid - 1] = ObjectIdGetDatum(statOid);
779  nulls[Anum_pg_statistic_ext_data_stxoid - 1] = false;
780 
781  values[Anum_pg_statistic_ext_data_stxdinherit - 1] = BoolGetDatum(inh);
782  nulls[Anum_pg_statistic_ext_data_stxdinherit - 1] = false;
783 
784  /*
785  * Construct a new pg_statistic_ext_data tuple, replacing the calculated
786  * stats.
787  */
788  if (ndistinct != NULL)
789  {
790  bytea *data = statext_ndistinct_serialize(ndistinct);
791 
792  nulls[Anum_pg_statistic_ext_data_stxdndistinct - 1] = (data == NULL);
793  values[Anum_pg_statistic_ext_data_stxdndistinct - 1] = PointerGetDatum(data);
794  }
795 
796  if (dependencies != NULL)
797  {
798  bytea *data = statext_dependencies_serialize(dependencies);
799 
800  nulls[Anum_pg_statistic_ext_data_stxddependencies - 1] = (data == NULL);
801  values[Anum_pg_statistic_ext_data_stxddependencies - 1] = PointerGetDatum(data);
802  }
803  if (mcv != NULL)
804  {
805  bytea *data = statext_mcv_serialize(mcv, stats);
806 
807  nulls[Anum_pg_statistic_ext_data_stxdmcv - 1] = (data == NULL);
808  values[Anum_pg_statistic_ext_data_stxdmcv - 1] = PointerGetDatum(data);
809  }
810  if (exprs != (Datum) 0)
811  {
812  nulls[Anum_pg_statistic_ext_data_stxdexpr - 1] = false;
813  values[Anum_pg_statistic_ext_data_stxdexpr - 1] = exprs;
814  }
815 
816  /*
817  * Delete the old tuple if it exists, and insert a new one. It's easier
818  * than trying to update or insert, based on various conditions.
819  */
820  RemoveStatisticsDataById(statOid, inh);
821 
822  /* form and insert a new tuple */
823  stup = heap_form_tuple(RelationGetDescr(pg_stextdata), values, nulls);
824  CatalogTupleInsert(pg_stextdata, stup);
825 
826  heap_freetuple(stup);
827 
828  table_close(pg_stextdata, RowExclusiveLock);
829 }
bytea * statext_dependencies_serialize(MVDependencies *dependencies)
Definition: dependencies.c:446
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
bytea * statext_mcv_serialize(MCVList *mcvlist, VacAttrStats **stats)
Definition: mcv.c:625
bytea * statext_ndistinct_serialize(MVNDistinct *ndistinct)
Definition: mvdistinct.c:180
void RemoveStatisticsDataById(Oid statsOid, bool inh)
Definition: statscmds.c:709
Definition: c.h:676

References BoolGetDatum(), CatalogTupleInsert(), data, heap_form_tuple(), heap_freetuple(), ObjectIdGetDatum(), PointerGetDatum(), RelationGetDescr, RemoveStatisticsDataById(), RowExclusiveLock, statext_dependencies_serialize(), statext_mcv_serialize(), statext_ndistinct_serialize(), table_close(), table_open(), and values.

Referenced by BuildRelationExtStatistics().