PostgreSQL Source Code git master
Data Structures | Macros | Typedefs | Functions
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 (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, 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)
 
 
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)
 
 
AttrNumberbuild_attnums_array (Bitmapset *attrs, int nexprs, int *numattrs)
 
 
bool  has_stats_of_kind (List *stats, char requiredkind)
 
 
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, bool *leakproof)
 
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 ( Bitmapsetattrs,
int  nexprs,
int *  numattrs 
)

Definition at line 938 of file extended_stats.c.

939{
940 int i,
941 j;
942 AttrNumber *attnums;
943 int num = bms_num_members(attrs);
944
945 if (numattrs)
946 *numattrs = num;
947
948 /* build attnums from the bitmapset */
949 attnums = (AttrNumber *) palloc(sizeof(AttrNumber) * num);
950 i = 0;
951 j = -1;
952 while ((j = bms_next_member(attrs, j)) >= 0)
953 {
954 int attnum = (j - nexprs);
955
956 /*
957 * Make sure the bitmap contains only user-defined attributes. As
958 * bitmaps can't contain negative values, this can be violated in two
959 * ways. Firstly, the bitmap might contain 0 as a member, and secondly
960 * the integer value might be larger than MaxAttrNumber.
961 */
964 Assert(attnum >= (-nexprs));
965
966 attnums[i++] = (AttrNumber) attnum;
967
968 /* protect against overflows */
969 Assert(i <= num);
970 }
971
972 return attnums;
973}
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
Assert(PointerIsAligned(start, uint64))
j
int j
Definition: isn.c:78
i
int i
Definition: isn.c:77
void * palloc(Size size)
Definition: mcxt.c:1365
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 ( Listexprs,
int  stattarget 
)
static

Definition at line 2247 of file extended_stats.c.

2248{
2249 int idx;
2250 int nexprs = list_length(exprs);
2251 AnlExprData *exprdata;
2252 ListCell *lc;
2253
2254 exprdata = (AnlExprData *) palloc0(nexprs * sizeof(AnlExprData));
2255
2256 idx = 0;
2257 foreach(lc, exprs)
2258 {
2259 Node *expr = (Node *) lfirst(lc);
2260 AnlExprData *thisdata = &exprdata[idx];
2261
2262 thisdata->expr = expr;
2263 thisdata->vacattrstat = examine_expression(expr, stattarget);
2264 idx++;
2265 }
2266
2267 return exprdata;
2268}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:262
static VacAttrStats * examine_expression(Node *expr, int stattarget)
void * palloc0(Size size)
Definition: mcxt.c:1395
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
VacAttrStats * vacattrstat
Definition: extended_stats.c:89
Node * expr
Definition: extended_stats.c:88
Definition: nodes.h:135
Definition: pg_list.h:46

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

Referenced by BuildRelationExtStatistics().

build_sorted_items()

SortItem * build_sorted_items ( StatsBuildDatadata,
int *  nitems,
int  numattrs,
AttrNumberattnums 
)

Definition at line 983 of file extended_stats.c.

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

References Assert(), attlen, attnum, data, get_typlen(), i, idx(), items, j, len, MAXALIGN, 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,
HeapTuplerows,
int  natts,
VacAttrStats **  vacattrstats 
)

Definition at line 111 of file extended_stats.c.

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

Definition at line 1207 of file extended_stats.c.

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

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

925{
926 return ApplySortComparator(a, false, b, false, ssup);
927}
b
int b
Definition: isn.c:74
a
int a
Definition: isn.c:73
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 916 of file extended_stats.c.

917{
918 return compare_datums_simple(*(Datum *) a,
919 *(Datum *) b,
920 (SortSupport) arg);
921}
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,
AnlExprDataexprdata,
int  nexprs,
HeapTuplerows,
int  numrows 
)
static

Definition at line 2088 of file extended_stats.c.

2090{
2091 MemoryContext expr_context,
2092 old_context;
2093 int ind,
2094 i;
2095
2097 "Analyze Expression",
2099 old_context = MemoryContextSwitchTo(expr_context);
2100
2101 for (ind = 0; ind < nexprs; ind++)
2102 {
2103 AnlExprData *thisdata = &exprdata[ind];
2104 VacAttrStats *stats = thisdata->vacattrstat;
2105 Node *expr = thisdata->expr;
2106 TupleTableSlot *slot;
2107 EState *estate;
2108 ExprContext *econtext;
2109 Datum *exprvals;
2110 bool *exprnulls;
2111 ExprState *exprstate;
2112 int tcnt;
2113
2114 /* Are we still in the main context? */
2115 Assert(CurrentMemoryContext == expr_context);
2116
2117 /*
2118 * Need an EState for evaluation of expressions. Create it in the
2119 * per-expression context to be sure it gets cleaned up at the bottom
2120 * of the loop.
2121 */
2122 estate = CreateExecutorState();
2123 econtext = GetPerTupleExprContext(estate);
2124
2125 /* Set up expression evaluation state */
2126 exprstate = ExecPrepareExpr((Expr *) expr, estate);
2127
2128 /* Need a slot to hold the current heap tuple, too */
2131
2132 /* Arrange for econtext's scan tuple to be the tuple under test */
2133 econtext->ecxt_scantuple = slot;
2134
2135 /* Compute and save expression values */
2136 exprvals = (Datum *) palloc(numrows * sizeof(Datum));
2137 exprnulls = (bool *) palloc(numrows * sizeof(bool));
2138
2139 tcnt = 0;
2140 for (i = 0; i < numrows; i++)
2141 {
2142 Datum datum;
2143 bool isnull;
2144
2145 /*
2146 * Reset the per-tuple context each time, to reclaim any cruft
2147 * left behind by evaluating the statistics expressions.
2148 */
2149 ResetExprContext(econtext);
2150
2151 /* Set up for expression evaluation */
2152 ExecStoreHeapTuple(rows[i], slot, false);
2153
2154 /*
2155 * Evaluate the expression. We do this in the per-tuple context so
2156 * as not to leak memory, and then copy the result into the
2157 * context created at the beginning of this function.
2158 */
2159 datum = ExecEvalExprSwitchContext(exprstate,
2160 GetPerTupleExprContext(estate),
2161 &isnull);
2162 if (isnull)
2163 {
2164 exprvals[tcnt] = (Datum) 0;
2165 exprnulls[tcnt] = true;
2166 }
2167 else
2168 {
2169 /* Make sure we copy the data into the context. */
2170 Assert(CurrentMemoryContext == expr_context);
2171
2172 exprvals[tcnt] = datumCopy(datum,
2173 stats->attrtype->typbyval,
2174 stats->attrtype->typlen);
2175 exprnulls[tcnt] = false;
2176 }
2177
2178 tcnt++;
2179 }
2180
2181 /*
2182 * Now we can compute the statistics for the expression columns.
2183 *
2184 * XXX Unlike compute_index_stats we don't need to switch and reset
2185 * memory contexts here, because we're only computing stats for a
2186 * single expression (and not iterating over many indexes), so we just
2187 * do it in expr_context. Note that compute_stats copies the result
2188 * into stats->anl_context, so it does not disappear.
2189 */
2190 if (tcnt > 0)
2191 {
2192 AttributeOpts *aopt =
2193 get_attribute_options(onerel->rd_id, stats->tupattnum);
2194
2195 stats->exprvals = exprvals;
2196 stats->exprnulls = exprnulls;
2197 stats->rowstride = 1;
2198 stats->compute_stats(stats,
2200 tcnt,
2201 tcnt);
2202
2203 /*
2204 * If the n_distinct option is specified, it overrides the above
2205 * computation.
2206 */
2207 if (aopt != NULL && aopt->n_distinct != 0.0)
2208 stats->stadistinct = aopt->n_distinct;
2209 }
2210
2211 /* And clean up */
2212 MemoryContextSwitchTo(expr_context);
2213
2215 FreeExecutorState(estate);
2216 MemoryContextReset(expr_context);
2217 }
2218
2219 MemoryContextSwitchTo(old_context);
2220 MemoryContextDelete(expr_context);
2221}
AttributeOpts * get_attribute_options(Oid attrelid, int attnum)
Definition: attoptcache.c:131
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execExpr.c:765
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1427
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1443
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:85
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1541
void FreeExecutorState(EState *estate)
Definition: execUtils.c:192
EState * CreateExecutorState(void)
Definition: execUtils.c:88
#define GetPerTupleExprContext(estate)
Definition: executor.h:653
#define ResetExprContext(econtext)
Definition: executor.h:647
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:433
static Datum expr_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull)
#define RelationGetDescr(relation)
Definition: rel.h:540
float8 n_distinct
Definition: attoptcache.h:22
Definition: execnodes.h:655
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:273
Definition: primnodes.h:189
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 262 of file extended_stats.c.

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

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 ( Nodeexpr )
static

Definition at line 525 of file extended_stats.c.

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

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 ( Nodeexpr,
int  stattarget 
)
static

Definition at line 604 of file extended_stats.c.

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

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 ( Listargs,
Node **  exprp,
Const **  cstp,
bool *  expronleftp 
)

Definition at line 2033 of file extended_stats.c.

2035{
2036 Node *expr;
2037 Const *cst;
2038 bool expronleft;
2039 Node *leftop,
2040 *rightop;
2041
2042 /* enforced by statext_is_compatible_clause_internal */
2043 Assert(list_length(args) == 2);
2044
2045 leftop = linitial(args);
2046 rightop = lsecond(args);
2047
2048 /* strip RelabelType from either side of the expression */
2049 if (IsA(leftop, RelabelType))
2050 leftop = (Node *) ((RelabelType *) leftop)->arg;
2051
2052 if (IsA(rightop, RelabelType))
2053 rightop = (Node *) ((RelabelType *) rightop)->arg;
2054
2055 if (IsA(rightop, Const))
2056 {
2057 expr = (Node *) leftop;
2058 cst = (Const *) rightop;
2059 expronleft = true;
2060 }
2061 else if (IsA(leftop, Const))
2062 {
2063 expr = (Node *) rightop;
2064 cst = (Const *) leftop;
2065 expronleft = false;
2066 }
2067 else
2068 return false;
2069
2070 /* return pointers to the extracted parts if requested */
2071 if (exprp)
2072 *exprp = expr;
2073
2074 if (cstp)
2075 *cstp = cst;
2076
2077 if (expronleftp)
2078 *expronleftp = expronleft;
2079
2080 return true;
2081}
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
Definition: primnodes.h:324

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

2232{
2233 int i;
2234
2235 /* exprvals and exprnulls are already offset for proper column */
2236 i = rownum * stats->rowstride;
2237 *isNull = stats->exprnulls[i];
2238 return stats->exprvals[i];
2239}

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

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

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 ( Liststats,
char  requiredkind 
)

Definition at line 1116 of file extended_stats.c.

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

References lfirst.

Referenced by dependencies_clauselist_selectivity(), and statext_mcv_clauselist_selectivity().

lookup_var_attr_stats()

static VacAttrStats ** lookup_var_attr_stats ( Bitmapsetattrs,
Listexprs,
int  nvacatts,
VacAttrStats **  vacatts 
)
static

Definition at line 690 of file extended_stats.c.

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

Definition at line 2449 of file extended_stats.c.

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

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

850{
851 SortSupport ssup = &mss->ssup[sortdim];
852
854 ssup->ssup_collation = collation;
855 ssup->ssup_nulls_first = false;
856
858}
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition: parse_oper.c:371
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Definition: sortsupport.c:134
SortSupportData ssup[FLEXIBLE_ARRAY_MEMBER]
bool ssup_nulls_first
Definition: sortsupport.h:75
Oid ssup_collation
Definition: sortsupport.h:67
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 862 of file extended_stats.c.

863{
865 SortItem *ia = (SortItem *) a;
866 SortItem *ib = (SortItem *) b;
867 int i;
868
869 for (i = 0; i < mss->ndims; i++)
870 {
871 int compare;
872
874 ib->values[i], ib->isnull[i],
875 &mss->ssup[i]);
876
877 if (compare != 0)
878 return compare;
879 }
880
881 /* equal by default */
882 return 0;
883}
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 SortItema,
const SortItemb,
)

Definition at line 887 of file extended_stats.c.

889{
890 return ApplySortComparator(a->values[dim], a->isnull[dim],
891 b->values[dim], b->isnull[dim],
892 &mss->ssup[dim]);
893}

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 SortItema,
const SortItemb,
)

Definition at line 896 of file extended_stats.c.

899{
900 int dim;
901
902 for (dim = start; dim <= end; dim++)
903 {
904 int r = ApplySortComparator(a->values[dim], a->isnull[dim],
905 b->values[dim], b->isnull[dim],
906 &mss->ssup[dim]);
907
908 if (r != 0)
909 return r;
910 }
911
912 return 0;
913}
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 829 of file extended_stats.c.

830{
832
833 Assert(ndims >= 2);
834
835 mss = (MultiSortSupport) palloc0(offsetof(MultiSortSupportData, ssup)
836 + sizeof(SortSupportData) * ndims);
837
838 mss->ndims = ndims;
839
840 return mss;
841}
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 ( AnlExprDataexprdata,
int  nexprs 
)
static

Definition at line 2272 of file extended_stats.c.

2273{
2274 int exprno;
2275 Oid typOid;
2276 Relation sd;
2277
2278 ArrayBuildState *astate = NULL;
2279
2280 sd = table_open(StatisticRelationId, RowExclusiveLock);
2281
2282 /* lookup OID of composite type for pg_statistic */
2283 typOid = get_rel_type_id(StatisticRelationId);
2284 if (!OidIsValid(typOid))
2285 ereport(ERROR,
2286 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2287 errmsg("relation \"%s\" does not have a composite type",
2288 "pg_statistic")));
2289
2290 for (exprno = 0; exprno < nexprs; exprno++)
2291 {
2292 int i,
2293 k;
2294 VacAttrStats *stats = exprdata[exprno].vacattrstat;
2295
2296 Datum values[Natts_pg_statistic];
2297 bool nulls[Natts_pg_statistic];
2298 HeapTuple stup;
2299
2300 if (!stats->stats_valid)
2301 {
2302 astate = accumArrayResult(astate,
2303 (Datum) 0,
2304 true,
2305 typOid,
2307 continue;
2308 }
2309
2310 /*
2311 * Construct a new pg_statistic tuple
2312 */
2313 for (i = 0; i < Natts_pg_statistic; ++i)
2314 {
2315 nulls[i] = false;
2316 }
2317
2318 values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(InvalidOid);
2319 values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(InvalidAttrNumber);
2320 values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(false);
2321 values[Anum_pg_statistic_stanullfrac - 1] = Float4GetDatum(stats->stanullfrac);
2322 values[Anum_pg_statistic_stawidth - 1] = Int32GetDatum(stats->stawidth);
2323 values[Anum_pg_statistic_stadistinct - 1] = Float4GetDatum(stats->stadistinct);
2324 i = Anum_pg_statistic_stakind1 - 1;
2325 for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2326 {
2327 values[i++] = Int16GetDatum(stats->stakind[k]); /* stakindN */
2328 }
2329 i = Anum_pg_statistic_staop1 - 1;
2330 for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2331 {
2332 values[i++] = ObjectIdGetDatum(stats->staop[k]); /* staopN */
2333 }
2334 i = Anum_pg_statistic_stacoll1 - 1;
2335 for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2336 {
2337 values[i++] = ObjectIdGetDatum(stats->stacoll[k]); /* stacollN */
2338 }
2339 i = Anum_pg_statistic_stanumbers1 - 1;
2340 for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2341 {
2342 int nnum = stats->numnumbers[k];
2343
2344 if (nnum > 0)
2345 {
2346 int n;
2347 Datum *numdatums = (Datum *) palloc(nnum * sizeof(Datum));
2348 ArrayType *arry;
2349
2350 for (n = 0; n < nnum; n++)
2351 numdatums[n] = Float4GetDatum(stats->stanumbers[k][n]);
2352 arry = construct_array_builtin(numdatums, nnum, FLOAT4OID);
2353 values[i++] = PointerGetDatum(arry); /* stanumbersN */
2354 }
2355 else
2356 {
2357 nulls[i] = true;
2358 values[i++] = (Datum) 0;
2359 }
2360 }
2361 i = Anum_pg_statistic_stavalues1 - 1;
2362 for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2363 {
2364 if (stats->numvalues[k] > 0)
2365 {
2366 ArrayType *arry;
2367
2368 arry = construct_array(stats->stavalues[k],
2369 stats->numvalues[k],
2370 stats->statypid[k],
2371 stats->statyplen[k],
2372 stats->statypbyval[k],
2373 stats->statypalign[k]);
2374 values[i++] = PointerGetDatum(arry); /* stavaluesN */
2375 }
2376 else
2377 {
2378 nulls[i] = true;
2379 values[i++] = (Datum) 0;
2380 }
2381 }
2382
2383 stup = heap_form_tuple(RelationGetDescr(sd), values, nulls);
2384
2385 astate = accumArrayResult(astate,
2387 false,
2388 typOid,
2390 }
2391
2393
2394 return makeArrayResult(astate, CurrentMemoryContext);
2395}
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5350
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3361
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3381
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5420
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
Definition: heaptuple.c:1081
Oid get_rel_type_id(Oid relid)
Definition: lsyscache.c:2146
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:458
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:182
static Datum BoolGetDatum(bool X)
Definition: postgres.h:112
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:222
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
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 ( StatisticExtInfostat,
Listexprs,
Bitmapset **  expr_idxs 
)
static

Definition at line 1166 of file extended_stats.c.

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

Definition at line 1139 of file extended_stats.c.

1140{
1141 ListCell *lc;
1142 int idx;
1143
1144 idx = 0;
1145 foreach(lc, stat->exprs)
1146 {
1147 Node *stat_expr = (Node *) lfirst(lc);
1148
1149 if (equal(stat_expr, expr))
1150 return idx;
1151 idx++;
1152 }
1153
1154 /* Expression not found */
1155 return -1;
1156}
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 ( PlannerInforoot,
Listclauses,
int  varRelid,
JoinType  jointype,
SpecialJoinInfosjinfo,
RelOptInforel,
Bitmapset **  estimatedclauses,
bool  is_or 
)

Definition at line 1982 of file extended_stats.c.

1986{
1987 Selectivity sel;
1988
1989 /* First, try estimating clauses using a multivariate MCV list. */
1990 sel = statext_mcv_clauselist_selectivity(root, clauses, varRelid, jointype,
1991 sjinfo, rel, estimatedclauses, is_or);
1992
1993 /*
1994 * Functional dependencies only work for clauses connected by AND, so for
1995 * OR clauses we're done.
1996 */
1997 if (is_or)
1998 return sel;
1999
2000 /*
2001 * Then, apply functional dependencies on the remaining clauses by calling
2002 * dependencies_clauselist_selectivity. Pass 'estimatedclauses' so the
2003 * function can properly skip clauses already estimated above.
2004 *
2005 * The reasoning for applying dependencies last is that the more complex
2006 * stats can track more complex correlations between the attributes, and
2007 * so may be considered more reliable.
2008 *
2009 * For example, MCV list can give us an exact selectivity for values in
2010 * two columns, while functional dependencies can only provide information
2011 * about the overall strength of the dependency.
2012 */
2013 sel *= dependencies_clauselist_selectivity(root, clauses, varRelid,
2014 jointype, sjinfo, rel,
2015 estimatedclauses);
2016
2017 return sel;
2018}
Selectivity dependencies_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
Definition: dependencies.c:1370
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:260
tree ctl root
Definition: radixtree.h:1857

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

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

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

2403{
2404 bool isnull;
2405 Datum value;
2406 HeapTuple htup;
2408 HeapTupleHeader td;
2409 HeapTupleData tmptup;
2410 HeapTuple tup;
2411
2412 htup = SearchSysCache2(STATEXTDATASTXOID,
2413 ObjectIdGetDatum(stxoid), BoolGetDatum(inh));
2414 if (!HeapTupleIsValid(htup))
2415 elog(ERROR, "cache lookup failed for statistics object %u", stxoid);
2416
2417 value = SysCacheGetAttr(STATEXTDATASTXOID, htup,
2418 Anum_pg_statistic_ext_data_stxdexpr, &isnull);
2419 if (isnull)
2420 elog(ERROR,
2421 "requested statistics kind \"%c\" is not yet built for statistics object %u",
2422 STATS_EXT_EXPRESSIONS, stxoid);
2423
2425
2427
2429
2430 /* Build a temporary HeapTuple control structure */
2432 ItemPointerSetInvalid(&(tmptup.t_self));
2433 tmptup.t_tableOid = InvalidOid;
2434 tmptup.t_data = td;
2435
2436 tup = heap_copytuple(&tmptup);
2437
2438 ReleaseSysCache(htup);
2439
2440 return tup;
2441}
void deconstruct_expanded_array(ExpandedArrayHeader *eah)
ExpandedArrayHeader * DatumGetExpandedArray(Datum d)
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:778
static uint32 HeapTupleHeaderGetDatumLength(const HeapTupleHeaderData *tup)
Definition: htup_details.h:492
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:264
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:230

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 ( PlannerInforoot,
Nodeclause,
Index  relid,
Bitmapset **  attnums,
List **  exprs 
)
static

Definition at line 1556 of file extended_stats.c.

1558{
1559 RestrictInfo *rinfo;
1560 int clause_relid;
1561 bool leakproof;
1562
1563 /*
1564 * Special-case handling for bare BoolExpr AND clauses, because the
1565 * restrictinfo machinery doesn't build RestrictInfos on top of AND
1566 * clauses.
1567 */
1568 if (is_andclause(clause))
1569 {
1570 BoolExpr *expr = (BoolExpr *) clause;
1571 ListCell *lc;
1572
1573 /*
1574 * Check that each sub-clause is compatible. We expect these to be
1575 * RestrictInfos.
1576 */
1577 foreach(lc, expr->args)
1578 {
1580 relid, attnums, exprs))
1581 return false;
1582 }
1583
1584 return true;
1585 }
1586
1587 /* Otherwise it must be a RestrictInfo. */
1588 if (!IsA(clause, RestrictInfo))
1589 return false;
1590 rinfo = (RestrictInfo *) clause;
1591
1592 /* Pseudoconstants are not really interesting here. */
1593 if (rinfo->pseudoconstant)
1594 return false;
1595
1596 /* Clauses referencing other varnos are incompatible. */
1597 if (!bms_get_singleton_member(rinfo->clause_relids, &clause_relid) ||
1598 clause_relid != relid)
1599 return false;
1600
1601 /*
1602 * Check the clause, determine what attributes it references, and whether
1603 * it includes any non-leakproof operators.
1604 */
1605 leakproof = true;
1607 relid, attnums, exprs,
1608 &leakproof))
1609 return false;
1610
1611 /*
1612 * If the clause includes any non-leakproof operators, check that the user
1613 * has permission to read all required attributes, otherwise the operators
1614 * might reveal values from the MCV list that the user doesn't have
1615 * permission to see. We require all rows to be selectable --- there must
1616 * be no securityQuals from security barrier views or RLS policies. See
1617 * similar code in examine_variable(), examine_simple_variable(), and
1618 * statistic_proc_security_check().
1619 *
1620 * Note that for an inheritance child, the permission checks are performed
1621 * on the inheritance root parent, and whole-table select privilege on the
1622 * parent doesn't guarantee that the user could read all columns of the
1623 * child. Therefore we must check all referenced columns.
1624 */
1625 if (!leakproof)
1626 {
1627 Bitmapset *clause_attnums = NULL;
1628 int attnum = -1;
1629
1630 /*
1631 * We have to check per-column privileges. *attnums has the attnums
1632 * for individual Vars we saw, but there may also be Vars within
1633 * subexpressions in *exprs. We can use pull_varattnos() to extract
1634 * those, but there's an impedance mismatch: attnums returned by
1635 * pull_varattnos() are offset by FirstLowInvalidHeapAttributeNumber,
1636 * while attnums within *attnums aren't. Convert *attnums to the
1637 * offset style so we can combine the results.
1638 */
1639 while ((attnum = bms_next_member(*attnums, attnum)) >= 0)
1640 {
1641 clause_attnums =
1642 bms_add_member(clause_attnums,
1644 }
1645
1646 /* Now merge attnums from *exprs into clause_attnums */
1647 if (*exprs != NIL)
1648 pull_varattnos((Node *) *exprs, relid, &clause_attnums);
1649
1650 /* Must have permission to read all rows from these columns */
1651 if (!all_rows_selectable(root, relid, clause_attnums))
1652 return false;
1653 }
1654
1655 /* If we reach here, the clause is OK */
1656 return true;
1657}
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, bool *leakproof)
static bool statext_is_compatible_clause(PlannerInfo *root, Node *clause, Index relid, Bitmapset **attnums, List **exprs)
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:107
bool all_rows_selectable(PlannerInfo *root, Index varno, Bitmapset *varattnos)
Definition: selfuncs.c:5911
List * args
Definition: primnodes.h:972
Expr * clause
Definition: pathnodes.h:2704
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:296

References all_rows_selectable(), BoolExpr::args, attnum, bms_add_member(), bms_get_singleton_member(), bms_next_member(), RestrictInfo::clause, FirstLowInvalidHeapAttributeNumber, is_andclause(), IsA, lfirst, NIL, pull_varattnos(), root, statext_is_compatible_clause(), and statext_is_compatible_clause_internal().

Referenced by statext_is_compatible_clause(), and statext_mcv_clauselist_selectivity().

statext_is_compatible_clause_internal()

static bool statext_is_compatible_clause_internal ( PlannerInforoot,
Nodeclause,
Index  relid,
Bitmapset **  attnums,
List **  exprs,
bool *  leakproof 
)
static

Definition at line 1329 of file extended_stats.c.

1332{
1333 /* Look inside any binary-compatible relabeling (as in examine_variable) */
1334 if (IsA(clause, RelabelType))
1335 clause = (Node *) ((RelabelType *) clause)->arg;
1336
1337 /* plain Var references (boolean Vars or recursive checks) */
1338 if (IsA(clause, Var))
1339 {
1340 Var *var = (Var *) clause;
1341
1342 /* Ensure var is from the correct relation */
1343 if (var->varno != relid)
1344 return false;
1345
1346 /* we also better ensure the Var is from the current level */
1347 if (var->varlevelsup > 0)
1348 return false;
1349
1350 /*
1351 * Also reject system attributes and whole-row Vars (we don't allow
1352 * stats on those).
1353 */
1355 return false;
1356
1357 /* OK, record the attnum for later permissions checks. */
1358 *attnums = bms_add_member(*attnums, var->varattno);
1359
1360 return true;
1361 }
1362
1363 /* (Var/Expr op Const) or (Const op Var/Expr) */
1364 if (is_opclause(clause))
1365 {
1366 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 /* Check if the operator is leakproof */
1401 if (*leakproof)
1402 *leakproof = get_func_leakproof(get_opcode(expr->opno));
1403
1404 /* Check (Var op Const) or (Const op Var) clauses by recursing. */
1405 if (IsA(clause_expr, Var))
1406 return statext_is_compatible_clause_internal(root, clause_expr,
1407 relid, attnums,
1408 exprs, leakproof);
1409
1410 /* Otherwise we have (Expr op Const) or (Const op Expr). */
1411 *exprs = lappend(*exprs, clause_expr);
1412 return true;
1413 }
1414
1415 /* Var/Expr IN Array */
1416 if (IsA(clause, ScalarArrayOpExpr))
1417 {
1418 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) clause;
1419 Node *clause_expr;
1420 bool expronleft;
1421
1422 /* Only expressions with two arguments are considered compatible. */
1423 if (list_length(expr->args) != 2)
1424 return false;
1425
1426 /* Check if the expression has the right shape (one Var, one Const) */
1427 if (!examine_opclause_args(expr->args, &clause_expr, NULL, &expronleft))
1428 return false;
1429
1430 /* We only support Var on left, Const on right */
1431 if (!expronleft)
1432 return false;
1433
1434 /*
1435 * If it's not one of the supported operators ("=", "<", ">", etc.),
1436 * just ignore the clause, as it's not compatible with MCV lists.
1437 *
1438 * This uses the function for estimating selectivity, not the operator
1439 * directly (a bit awkward, but well ...).
1440 */
1441 switch (get_oprrest(expr->opno))
1442 {
1443 case F_EQSEL:
1444 case F_NEQSEL:
1445 case F_SCALARLTSEL:
1446 case F_SCALARLESEL:
1447 case F_SCALARGTSEL:
1448 case F_SCALARGESEL:
1449 /* supported, will continue with inspection of the Var/Expr */
1450 break;
1451
1452 default:
1453 /* other estimators are considered unknown/unsupported */
1454 return false;
1455 }
1456
1457 /* Check if the operator is leakproof */
1458 if (*leakproof)
1459 *leakproof = get_func_leakproof(get_opcode(expr->opno));
1460
1461 /* Check Var IN Array clauses by recursing. */
1462 if (IsA(clause_expr, Var))
1463 return statext_is_compatible_clause_internal(root, clause_expr,
1464 relid, attnums,
1465 exprs, leakproof);
1466
1467 /* Otherwise we have Expr IN Array. */
1468 *exprs = lappend(*exprs, clause_expr);
1469 return true;
1470 }
1471
1472 /* AND/OR/NOT clause */
1473 if (is_andclause(clause) ||
1474 is_orclause(clause) ||
1475 is_notclause(clause))
1476 {
1477 /*
1478 * AND/OR/NOT-clauses are supported if all sub-clauses are supported
1479 *
1480 * Perhaps we could improve this by handling mixed cases, when some of
1481 * the clauses are supported and some are not. Selectivity for the
1482 * supported subclauses would be computed using extended statistics,
1483 * and the remaining clauses would be estimated using the traditional
1484 * algorithm (product of selectivities).
1485 *
1486 * It however seems overly complex, and in a way we already do that
1487 * because if we reject the whole clause as unsupported here, it will
1488 * be eventually passed to clauselist_selectivity() which does exactly
1489 * this (split into supported/unsupported clauses etc).
1490 */
1491 BoolExpr *expr = (BoolExpr *) clause;
1492 ListCell *lc;
1493
1494 foreach(lc, expr->args)
1495 {
1496 /*
1497 * If we find an incompatible clause in the arguments, treat the
1498 * whole clause as incompatible.
1499 */
1501 (Node *) lfirst(lc),
1502 relid, attnums, exprs,
1503 leakproof))
1504 return false;
1505 }
1506
1507 return true;
1508 }
1509
1510 /* Var/Expr IS NULL */
1511 if (IsA(clause, NullTest))
1512 {
1513 NullTest *nt = (NullTest *) clause;
1514
1515 /* Check Var IS NULL clauses by recursing. */
1516 if (IsA(nt->arg, Var))
1518 (Node *) (nt->arg),
1519 relid, attnums,
1520 exprs, leakproof);
1521
1522 /* Otherwise we have Expr IS NULL. */
1523 *exprs = lappend(*exprs, nt->arg);
1524 return true;
1525 }
1526
1527 /*
1528 * Treat any other expressions as bare expressions to be matched against
1529 * expressions in statistics objects.
1530 */
1531 *exprs = lappend(*exprs, clause);
1532 return true;
1533}
#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:1724
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1452
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:2004
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:116
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:76
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:125
Expr * arg
Definition: primnodes.h:1982
Definition: primnodes.h:846
Oid opno
Definition: primnodes.h:850
List * args
Definition: primnodes.h:868
Definition: primnodes.h:262
AttrNumber varattno
Definition: primnodes.h:274
int varno
Definition: primnodes.h:269
Index varlevelsup
Definition: primnodes.h:294

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(), OpExpr::opno, ScalarArrayOpExpr::opno, root, statext_is_compatible_clause_internal(), Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by statext_is_compatible_clause(), and statext_is_compatible_clause_internal().

statext_is_kind_built()

bool statext_is_kind_built ( HeapTuple  htup,
char  type 
)

Definition at line 386 of file extended_stats.c.

387{
389
390 switch (type)
391 {
392 case STATS_EXT_NDISTINCT:
393 attnum = Anum_pg_statistic_ext_data_stxdndistinct;
394 break;
395
396 case STATS_EXT_DEPENDENCIES:
397 attnum = Anum_pg_statistic_ext_data_stxddependencies;
398 break;
399
400 case STATS_EXT_MCV:
401 attnum = Anum_pg_statistic_ext_data_stxdmcv;
402 break;
403
404 case STATS_EXT_EXPRESSIONS:
405 attnum = Anum_pg_statistic_ext_data_stxdexpr;
406 break;
407
408 default:
409 elog(ERROR, "unexpected statistics type requested: %d", type);
410 }
411
412 return !heap_attisnull(htup, attnum, NULL);
413}
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:456
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 ( PlannerInforoot,
Listclauses,
int  varRelid,
JoinType  jointype,
SpecialJoinInfosjinfo,
RelOptInforel,
Bitmapset **  estimatedclauses,
bool  is_or 
)
static

Definition at line 1694 of file extended_stats.c.

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

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,
MVNDistinctndistinct,
MVDependenciesdependencies,
MCVListmcv,
Datum  exprs,
VacAttrStats **  stats 
)
static

Definition at line 759 of file extended_stats.c.

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

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

AltStyle によって変換されたページ (->オリジナル) /