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_statistic_ext.h"
#include "catalog/pg_statistic_ext_data.h"
#include "commands/defrem.h"
#include "commands/progress.h"
#include "executor/executor.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.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 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 58 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 941 of file extended_stats.c.

942 {
943  int i,
944  j;
945  AttrNumber *attnums;
946  int num = bms_num_members(attrs);
947 
948  if (numattrs)
949  *numattrs = num;
950 
951  /* build attnums from the bitmapset */
952  attnums = (AttrNumber *) palloc(sizeof(AttrNumber) * num);
953  i = 0;
954  j = -1;
955  while ((j = bms_next_member(attrs, j)) >= 0)
956  {
957  int attnum = (j - nexprs);
958 
959  /*
960  * Make sure the bitmap contains only user-defined attributes. As
961  * bitmaps can't contain negative values, this can be violated in two
962  * ways. Firstly, the bitmap might contain 0 as a member, and secondly
963  * the integer value might be larger than MaxAttrNumber.
964  */
967  Assert(attnum >= (-nexprs));
968 
969  attnums[i++] = (AttrNumber) attnum;
970 
971  /* protect against overflows */
972  Assert(i <= num);
973  }
974 
975  return attnums;
976 }
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:1306
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
#define Assert(condition)
Definition: c.h:858
int j
Definition: isn.c:74
int i
Definition: isn.c:73
void * palloc(Size size)
Definition: mcxt.c:1316
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 2270 of file extended_stats.c.

2271 {
2272  int idx;
2273  int nexprs = list_length(exprs);
2274  AnlExprData *exprdata;
2275  ListCell *lc;
2276 
2277  exprdata = (AnlExprData *) palloc0(nexprs * sizeof(AnlExprData));
2278 
2279  idx = 0;
2280  foreach(lc, exprs)
2281  {
2282  Node *expr = (Node *) lfirst(lc);
2283  AnlExprData *thisdata = &exprdata[idx];
2284 
2285  thisdata->expr = expr;
2286  thisdata->vacattrstat = examine_expression(expr, stattarget);
2287  idx++;
2288  }
2289 
2290  return exprdata;
2291 }
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:1346
#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 986 of file extended_stats.c.

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

References Assert, attlen, attnum, data, get_typlen(), i, idx(), items, j, len, multi_sort_compare(), nitems, palloc(), palloc0(), pfree(), PG_DETOAST_DATUM, PointerGetDatum(), qsort_interruptible(), toast_raw_datum_size(), value, 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 112 of file extended_stats.c.

115 {
116  Relation pg_stext;
117  ListCell *lc;
118  List *statslist;
119  MemoryContext cxt;
120  MemoryContext oldcxt;
121  int64 ext_cnt;
122 
123  /* Do nothing if there are no columns to analyze. */
124  if (!natts)
125  return;
126 
127  /* the list of stats has to be allocated outside the memory context */
128  pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
129  statslist = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
130 
131  /* memory context for building each statistics object */
133  "BuildRelationExtStatistics",
135  oldcxt = MemoryContextSwitchTo(cxt);
136 
137  /* report this phase */
138  if (statslist != NIL)
139  {
140  const int index[] = {
143  };
144  const int64 val[] = {
146  list_length(statslist)
147  };
148 
150  }
151 
152  ext_cnt = 0;
153  foreach(lc, statslist)
154  {
156  MVNDistinct *ndistinct = NULL;
157  MVDependencies *dependencies = NULL;
158  MCVList *mcv = NULL;
159  Datum exprstats = (Datum) 0;
160  VacAttrStats **stats;
161  ListCell *lc2;
162  int stattarget;
164 
165  /*
166  * Check if we can build these stats based on the column analyzed. If
167  * not, report this fact (except in autovacuum) and move on.
168  */
169  stats = lookup_var_attr_stats(onerel, stat->columns, stat->exprs,
170  natts, vacattrstats);
171  if (!stats)
172  {
175  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
176  errmsg("statistics object \"%s.%s\" could not be computed for relation \"%s.%s\"",
177  stat->schema, stat->name,
178  get_namespace_name(onerel->rd_rel->relnamespace),
179  RelationGetRelationName(onerel)),
180  errtable(onerel)));
181  continue;
182  }
183 
184  /* compute statistics target for this statistics object */
185  stattarget = statext_compute_stattarget(stat->stattarget,
186  bms_num_members(stat->columns),
187  stats);
188 
189  /*
190  * Don't rebuild statistics objects with statistics target set to 0
191  * (we just leave the existing values around, just like we do for
192  * regular per-column statistics).
193  */
194  if (stattarget == 0)
195  continue;
196 
197  /* evaluate expressions (if the statistics object has any) */
198  data = make_build_data(onerel, stat, numrows, rows, stats, stattarget);
199 
200  /* compute statistic of each requested type */
201  foreach(lc2, stat->types)
202  {
203  char t = (char) lfirst_int(lc2);
204 
205  if (t == STATS_EXT_NDISTINCT)
206  ndistinct = statext_ndistinct_build(totalrows, data);
207  else if (t == STATS_EXT_DEPENDENCIES)
208  dependencies = statext_dependencies_build(data);
209  else if (t == STATS_EXT_MCV)
210  mcv = statext_mcv_build(data, totalrows, stattarget);
211  else if (t == STATS_EXT_EXPRESSIONS)
212  {
213  AnlExprData *exprdata;
214  int nexprs;
215 
216  /* should not happen, thanks to checks when defining stats */
217  if (!stat->exprs)
218  elog(ERROR, "requested expression stats, but there are no expressions");
219 
220  exprdata = build_expr_data(stat->exprs, stattarget);
221  nexprs = list_length(stat->exprs);
222 
223  compute_expr_stats(onerel, totalrows,
224  exprdata, nexprs,
225  rows, numrows);
226 
227  exprstats = serialize_expr_stats(exprdata, nexprs);
228  }
229  }
230 
231  /* store the statistics in the catalog */
232  statext_store(stat->statOid, inh,
233  ndistinct, dependencies, mcv, exprstats, stats);
234 
235  /* for reporting progress */
237  ++ext_cnt);
238 
239  /* free the data used for building this statistics object */
240  MemoryContextReset(cxt);
241  }
242 
243  MemoryContextSwitchTo(oldcxt);
244  MemoryContextDelete(cxt);
245 
246  list_free(statslist);
247 
248  table_close(pg_stext, RowExclusiveLock);
249 }
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:348
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#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:670
void list_free(List *list)
Definition: list.c:1546
#define RowExclusiveLock
Definition: lockdefs.h:38
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3366
MCVList * statext_mcv_build(StatsBuildData *data, double totalrows, int stattarget)
Definition: mcv.c:180
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:383
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define AmAutoVacuumWorkerProcess()
Definition: miscadmin.h:375
MVNDistinct * statext_ndistinct_build(double totalrows, StatsBuildData *data)
Definition: mvdistinct.c:88
#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
MemoryContextSwitchTo(old_ctx)
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationGetRelationName(relation)
Definition: rel.h:539
int errtable(Relation rel)
Definition: relcache.c:5945
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, AmAutoVacuumWorkerProcess, bms_num_members(), build_expr_data(), compute_expr_stats(), CurrentMemoryContext, data, elog, ereport, errcode(), errmsg(), ERROR, errtable(), fetch_statentries_for_relation(), get_namespace_name(), 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 1209 of file extended_stats.c.

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

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

928 {
929  return ApplySortComparator(a, false, b, false, ssup);
930 }
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 919 of file extended_stats.c.

920 {
921  return compare_datums_simple(*(Datum *) a,
922  *(Datum *) b,
923  (SortSupport) arg);
924 }
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 2110 of file extended_stats.c.

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

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(), MemoryContextReset(), 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 265 of file extended_stats.c.

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

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

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

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(), and VacAttrStats::tupattnum.

Referenced by lookup_var_attr_stats().

◆ examine_expression()

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

Definition at line 607 of file extended_stats.c.

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

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(), and VacAttrStats::tupattnum.

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

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

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

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

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

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert, bms_add_member(), BTEqualStrategyNumber, StatExtEntry::columns, DatumGetArrayTypeP, DatumGetInt16(), 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, 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 1118 of file extended_stats.c.

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

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

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

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

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

853 {
854  SortSupport ssup = &mss->ssup[sortdim];
855 
857  ssup->ssup_collation = collation;
858  ssup->ssup_nulls_first = false;
859 
861 }
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition: parse_oper.c:370
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Definition: sortsupport.c:134
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 865 of file extended_stats.c.

866 {
868  SortItem *ia = (SortItem *) a;
869  SortItem *ib = (SortItem *) b;
870  int i;
871 
872  for (i = 0; i < mss->ndims; i++)
873  {
874  int compare;
875 
877  ib->values[i], ib->isnull[i],
878  &mss->ssup[i]);
879 
880  if (compare != 0)
881  return compare;
882  }
883 
884  /* equal by default */
885  return 0;
886 }
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 890 of file extended_stats.c.

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

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

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

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

Referenced by dependency_degree().

◆ multi_sort_init()

MultiSortSupport multi_sort_init ( int  ndims)

Definition at line 832 of file extended_stats.c.

833 {
834  MultiSortSupport mss;
835 
836  Assert(ndims >= 2);
837 
838  mss = (MultiSortSupport) palloc0(offsetof(MultiSortSupportData, ssup)
839  + sizeof(SortSupportData) * ndims);
840 
841  mss->ndims = ndims;
842 
843  return mss;
844 }
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 2295 of file extended_stats.c.

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

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

1170 {
1171  ListCell *lc;
1172 
1173  foreach(lc, exprs)
1174  {
1175  Node *expr = (Node *) lfirst(lc);
1176  int expr_idx;
1177 
1178  expr_idx = stat_find_expression(stat, expr);
1179  if (expr_idx == -1)
1180  return false;
1181 
1182  if (expr_idxs != NULL)
1183  *expr_idxs = bms_add_member(*expr_idxs, expr_idx);
1184  }
1185 
1186  /* If we reach here, all expressions are covered */
1187  return true;
1188 }
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 1141 of file extended_stats.c.

1142 {
1143  ListCell *lc;
1144  int idx;
1145 
1146  idx = 0;
1147  foreach(lc, stat->exprs)
1148  {
1149  Node *stat_expr = (Node *) lfirst(lc);
1150 
1151  if (equal(stat_expr, expr))
1152  return idx;
1153  idx++;
1154  }
1155 
1156  /* Expression not found */
1157  return -1;
1158 }
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 2004 of file extended_stats.c.

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

References dependencies_clauselist_selectivity(), root, 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 347 of file extended_stats.c.

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

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

2426 {
2427  bool isnull;
2428  Datum value;
2429  HeapTuple htup;
2430  ExpandedArrayHeader *eah;
2431  HeapTupleHeader td;
2432  HeapTupleData tmptup;
2433  HeapTuple tup;
2434 
2435  htup = SearchSysCache2(STATEXTDATASTXOID,
2436  ObjectIdGetDatum(stxoid), BoolGetDatum(inh));
2437  if (!HeapTupleIsValid(htup))
2438  elog(ERROR, "cache lookup failed for statistics object %u", stxoid);
2439 
2440  value = SysCacheGetAttr(STATEXTDATASTXOID, htup,
2441  Anum_pg_statistic_ext_data_stxdexpr, &isnull);
2442  if (isnull)
2443  elog(ERROR,
2444  "requested statistics kind \"%c\" is not yet built for statistics object %u",
2445  STATS_EXT_EXPRESSIONS, stxoid);
2446 
2448 
2450 
2451  td = DatumGetHeapTupleHeader(eah->dvalues[idx]);
2452 
2453  /* Build a temporary HeapTuple control structure */
2454  tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
2455  ItemPointerSetInvalid(&(tmptup.t_self));
2456  tmptup.t_tableOid = InvalidOid;
2457  tmptup.t_data = td;
2458 
2459  tup = heap_copytuple(&tmptup);
2460 
2461  ReleaseSysCache(htup);
2462 
2463  return tup;
2464 }
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:776
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:450
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition: itemptr.h:184
Datum * dvalues
Definition: array.h:146
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:266
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:229

References BoolGetDatum(), DatumGetExpandedArray(), DatumGetHeapTupleHeader, deconstruct_expanded_array(), ExpandedArrayHeader::dvalues, elog, ERROR, heap_copytuple(), HeapTupleHeaderGetDatumLength, HeapTupleIsValid, idx(), InvalidOid, ItemPointerSetInvalid(), ObjectIdGetDatum(), ReleaseSysCache(), SearchSysCache2(), 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 1572 of file extended_stats.c.

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

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

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, root, 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 389 of file extended_stats.c.

390 {
392 
393  switch (type)
394  {
395  case STATS_EXT_NDISTINCT:
396  attnum = Anum_pg_statistic_ext_data_stxdndistinct;
397  break;
398 
399  case STATS_EXT_DEPENDENCIES:
400  attnum = Anum_pg_statistic_ext_data_stxddependencies;
401  break;
402 
403  case STATS_EXT_MCV:
404  attnum = Anum_pg_statistic_ext_data_stxdmcv;
405  break;
406 
407  case STATS_EXT_EXPRESSIONS:
408  attnum = Anum_pg_statistic_ext_data_stxdexpr;
409  break;
410 
411  default:
412  elog(ERROR, "unexpected statistics type requested: %d", type);
413  }
414 
415  return !heap_attisnull(htup, attnum, NULL);
416 }
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:455
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 1716 of file extended_stats.c.

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

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

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().