Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * extended_stats.c
4 : : * POSTGRES extended statistics
5 : : *
6 : : * Generic code supporting statistics objects created via CREATE STATISTICS.
7 : : *
8 : : *
9 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
10 : : * Portions Copyright (c) 1994, Regents of the University of California
11 : : *
12 : : * IDENTIFICATION
13 : : * src/backend/statistics/extended_stats.c
14 : : *
15 : : *-------------------------------------------------------------------------
16 : : */
17 : : #include "postgres.h"
18 : :
19 : : #include "access/detoast.h"
20 : : #include "access/genam.h"
21 : : #include "access/htup_details.h"
22 : : #include "access/table.h"
23 : : #include "catalog/indexing.h"
24 : : #include "catalog/pg_statistic_ext.h"
25 : : #include "catalog/pg_statistic_ext_data.h"
26 : : #include "commands/defrem.h"
27 : : #include "commands/progress.h"
28 : : #include "executor/executor.h"
29 : : #include "miscadmin.h"
30 : : #include "nodes/nodeFuncs.h"
31 : : #include "optimizer/optimizer.h"
32 : : #include "parser/parsetree.h"
33 : : #include "pgstat.h"
34 : : #include "postmaster/autovacuum.h"
35 : : #include "statistics/extended_stats_internal.h"
36 : : #include "statistics/statistics.h"
37 : : #include "utils/acl.h"
38 : : #include "utils/array.h"
39 : : #include "utils/attoptcache.h"
40 : : #include "utils/builtins.h"
41 : : #include "utils/datum.h"
42 : : #include "utils/fmgroids.h"
43 : : #include "utils/lsyscache.h"
44 : : #include "utils/memutils.h"
45 : : #include "utils/rel.h"
46 : : #include "utils/selfuncs.h"
47 : : #include "utils/syscache.h"
48 : :
49 : : /*
50 : : * To avoid consuming too much memory during analysis and/or too much space
51 : : * in the resulting pg_statistic rows, we ignore varlena datums that are wider
52 : : * than WIDTH_THRESHOLD (after detoasting!). This is legitimate for MCV
53 : : * and distinct-value calculations since a wide value is unlikely to be
54 : : * duplicated at all, much less be a most-common value. For the same reason,
55 : : * ignoring wide values will not affect our estimates of histogram bin
56 : : * boundaries very much.
57 : : */
58 : : #define WIDTH_THRESHOLD 1024
59 : :
60 : : /*
61 : : * Used internally to refer to an individual statistics object, i.e.,
62 : : * a pg_statistic_ext entry.
63 : : */
64 : : typedef struct StatExtEntry
65 : : {
66 : : Oid statOid; /* OID of pg_statistic_ext entry */
67 : : char *schema; /* statistics object's schema */
68 : : char *name; /* statistics object's name */
69 : : Bitmapset *columns; /* attribute numbers covered by the object */
70 : : List *types; /* 'char' list of enabled statistics kinds */
71 : : int stattarget; /* statistics target (-1 for default) */
72 : : List *exprs; /* expressions */
73 : : } StatExtEntry;
74 : :
75 : :
76 : : static List *fetch_statentries_for_relation(Relation pg_statext, Oid relid);
77 : : static VacAttrStats **lookup_var_attr_stats(Bitmapset *attrs, List *exprs,
78 : : int nvacatts, VacAttrStats **vacatts);
79 : : static void statext_store(Oid statOid, bool inh,
80 : : MVNDistinct *ndistinct, MVDependencies *dependencies,
81 : : MCVList *mcv, Datum exprs, VacAttrStats **stats);
82 : : static int statext_compute_stattarget(int stattarget,
83 : : int nattrs, VacAttrStats **stats);
84 : :
85 : : /* Information needed to analyze a single simple expression. */
86 : : typedef struct AnlExprData
87 : : {
88 : : Node *expr; /* expression to analyze */
89 : : VacAttrStats *vacattrstat; /* statistics attrs to analyze */
90 : : } AnlExprData;
91 : :
92 : : static void compute_expr_stats(Relation onerel, AnlExprData *exprdata,
93 : : int nexprs, HeapTuple *rows, int numrows);
94 : : static Datum serialize_expr_stats(AnlExprData *exprdata, int nexprs);
95 : : static Datum expr_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull);
96 : : static AnlExprData *build_expr_data(List *exprs, int stattarget);
97 : :
98 : : static StatsBuildData *make_build_data(Relation rel, StatExtEntry *stat,
99 : : int numrows, HeapTuple *rows,
100 : : VacAttrStats **stats, int stattarget);
101 : :
102 : :
103 : : /*
104 : : * Compute requested extended stats, using the rows sampled for the plain
105 : : * (single-column) stats.
106 : : *
107 : : * This fetches a list of stats types from pg_statistic_ext, computes the
108 : : * requested stats, and serializes them back into the catalog.
109 : : */
110 : : void
1329 tomas.vondra@postgre 111 :CBC 5438 : BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows,
112 : : int numrows, HeapTuple *rows,
113 : : int natts, VacAttrStats **vacattrstats)
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. */
1625 123 [ + + ]: 5438 : if (!natts)
124 : 9 : return;
125 : :
126 : : /* the list of stats has to be allocated outside the memory context */
1446 127 : 5429 : pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
128 : 5429 : statslist = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
129 : :
130 : : /* memory context for building each statistics object */
2720 tgl@sss.pgh.pa.us 131 : 5429 : cxt = AllocSetContextCreate(CurrentMemoryContext,
132 : : "BuildRelationExtStatistics",
133 : : ALLOCSET_DEFAULT_SIZES);
3064 alvherre@alvh.no-ip. 134 : 5429 : oldcxt = MemoryContextSwitchTo(cxt);
135 : :
136 : : /* report this phase */
1625 tomas.vondra@postgre 137 [ + + ]: 5429 : if (statslist != NIL)
138 : : {
2061 alvherre@alvh.no-ip. 139 : 154 : const int index[] = {
140 : : PROGRESS_ANALYZE_PHASE,
141 : : PROGRESS_ANALYZE_EXT_STATS_TOTAL
142 : : };
143 : 308 : const int64 val[] = {
144 : : PROGRESS_ANALYZE_PHASE_COMPUTE_EXT_STATS,
1625 tomas.vondra@postgre 145 : 154 : list_length(statslist)
146 : : };
147 : :
2061 alvherre@alvh.no-ip. 148 : 154 : pgstat_progress_update_multi_param(2, index, val);
149 : : }
150 : :
151 : 5429 : ext_cnt = 0;
1625 tomas.vondra@postgre 152 [ + + + + : 5637 : foreach(lc, statslist)
+ + ]
153 : : {
3034 bruce@momjian.us 154 : 208 : StatExtEntry *stat = (StatExtEntry *) lfirst(lc);
155 : 208 : MVNDistinct *ndistinct = NULL;
3076 simon@2ndQuadrant.co 156 : 208 : MVDependencies *dependencies = NULL;
2355 tomas.vondra@postgre 157 : 208 : MCVList *mcv = NULL;
1625 158 : 208 : Datum exprstats = (Datum) 0;
159 : : VacAttrStats **stats;
160 : : ListCell *lc2;
161 : : int stattarget;
162 : : StatsBuildData *data;
163 : :
164 : : /*
165 : : * Check if we can build these stats based on the column analyzed. If
166 : : * not, report this fact (except in autovacuum) and move on.
167 : : */
242 rguo@postgresql.org 168 : 208 : stats = lookup_var_attr_stats(stat->columns, stat->exprs,
169 : : natts, vacattrstats);
2839 alvherre@alvh.no-ip. 170 [ + + ]: 208 : if (!stats)
171 : : {
551 heikki.linnakangas@i 172 [ + - ]: 6 : if (!AmAutoVacuumWorkerProcess())
2839 alvherre@alvh.no-ip. 173 [ + - ]: 6 : ereport(WARNING,
174 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
175 : : errmsg("statistics object \"%s.%s\" could not be computed for relation \"%s.%s\"",
176 : : stat->schema, stat->name,
177 : : get_namespace_name(onerel->rd_rel->relnamespace),
178 : : RelationGetRelationName(onerel)),
179 : : errtable(onerel)));
3064 180 : 6 : continue;
181 : : }
182 : :
183 : : /* compute statistics target for this statistics object */
2188 tomas.vondra@postgre 184 : 202 : stattarget = statext_compute_stattarget(stat->stattarget,
185 : 202 : 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 [ + + ]: 202 : if (stattarget == 0)
194 : 3 : continue;
195 : :
196 : : /* evaluate expressions (if the statistics object has any) */
1625 197 : 199 : data = make_build_data(onerel, stat, numrows, rows, stats, stattarget);
198 : :
199 : : /* compute statistic of each requested type */
3088 alvherre@alvh.no-ip. 200 [ + - + + : 556 : foreach(lc2, stat->types)
+ + ]
201 : : {
3034 bruce@momjian.us 202 : 357 : char t = (char) lfirst_int(lc2);
203 : :
3088 alvherre@alvh.no-ip. 204 [ + + ]: 357 : if (t == STATS_EXT_NDISTINCT)
1625 tomas.vondra@postgre 205 : 97 : ndistinct = statext_ndistinct_build(totalrows, data);
3076 simon@2ndQuadrant.co 206 [ + + ]: 260 : else if (t == STATS_EXT_DEPENDENCIES)
1625 tomas.vondra@postgre 207 : 73 : dependencies = statext_dependencies_build(data);
2355 208 [ + + ]: 187 : else if (t == STATS_EXT_MCV)
1625 209 : 109 : mcv = statext_mcv_build(data, totalrows, stattarget);
210 [ + - ]: 78 : 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 [ - + ]: 78 : if (!stat->exprs)
1625 tomas.vondra@postgre 217 [ # # ]:UBC 0 : elog(ERROR, "requested expression stats, but there are no expressions");
218 : :
1625 tomas.vondra@postgre 219 :CBC 78 : exprdata = build_expr_data(stat->exprs, stattarget);
220 : 78 : nexprs = list_length(stat->exprs);
221 : :
253 drowley@postgresql.o 222 : 78 : compute_expr_stats(onerel, exprdata, nexprs, rows, numrows);
223 : :
1625 tomas.vondra@postgre 224 : 78 : exprstats = serialize_expr_stats(exprdata, nexprs);
225 : : }
226 : : }
227 : :
228 : : /* store the statistics in the catalog */
1329 229 : 199 : statext_store(stat->statOid, inh,
230 : : ndistinct, dependencies, mcv, exprstats, stats);
231 : :
232 : : /* for reporting progress */
2061 alvherre@alvh.no-ip. 233 : 199 : pgstat_progress_update_param(PROGRESS_ANALYZE_EXT_STATS_COMPUTED,
234 : : ++ext_cnt);
235 : :
236 : : /* free the data used for building this statistics object */
1446 tomas.vondra@postgre 237 : 199 : MemoryContextReset(cxt);
238 : : }
239 : :
3064 alvherre@alvh.no-ip. 240 : 5429 : MemoryContextSwitchTo(oldcxt);
241 : 5429 : MemoryContextDelete(cxt);
242 : :
1446 tomas.vondra@postgre 243 : 5429 : list_free(statslist);
244 : :
245 : 5429 : table_close(pg_stext, RowExclusiveLock);
246 : : }
247 : :
248 : : /*
249 : : * ComputeExtStatisticsRows
250 : : * Compute number of rows required by extended statistics on a table.
251 : : *
252 : : * Computes number of rows we need to sample to build extended statistics on a
253 : : * table. This only looks at statistics we can actually build - for example
254 : : * when analyzing only some of the columns, this will skip statistics objects
255 : : * that would require additional columns.
256 : : *
257 : : * See statext_compute_stattarget for details about how we compute the
258 : : * statistics target for a statistics object (from the object target,
259 : : * attribute targets and default statistics target).
260 : : */
261 : : int
2188 262 : 8051 : ComputeExtStatisticsRows(Relation onerel,
263 : : int natts, VacAttrStats **vacattrstats)
264 : : {
265 : : Relation pg_stext;
266 : : ListCell *lc;
267 : : List *lstats;
268 : : MemoryContext cxt;
269 : : MemoryContext oldcxt;
270 : 8051 : int result = 0;
271 : :
272 : : /* If there are no columns to analyze, just return 0. */
1625 273 [ + + ]: 8051 : if (!natts)
274 : 36 : return 0;
275 : :
2188 276 : 8015 : cxt = AllocSetContextCreate(CurrentMemoryContext,
277 : : "ComputeExtStatisticsRows",
278 : : ALLOCSET_DEFAULT_SIZES);
279 : 8015 : oldcxt = MemoryContextSwitchTo(cxt);
280 : :
281 : 8015 : pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
282 : 8015 : lstats = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
283 : :
284 [ + + + + : 8223 : foreach(lc, lstats)
+ + ]
285 : : {
1941 tgl@sss.pgh.pa.us 286 : 208 : StatExtEntry *stat = (StatExtEntry *) lfirst(lc);
287 : : int stattarget;
288 : : VacAttrStats **stats;
289 : 208 : 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 : : */
242 rguo@postgresql.org 296 : 208 : stats = lookup_var_attr_stats(stat->columns, stat->exprs,
297 : : natts, vacattrstats);
298 : :
2188 tomas.vondra@postgre 299 [ + + ]: 208 : if (!stats)
300 : 6 : continue;
301 : :
302 : : /*
303 : : * Compute statistics target, based on what's set for the statistic
304 : : * object itself, and for its attributes.
305 : : */
306 : 202 : stattarget = statext_compute_stattarget(stat->stattarget,
307 : : nattrs, stats);
308 : :
309 : : /* Use the largest value for all statistics objects. */
310 [ + + ]: 202 : if (stattarget > result)
311 : 145 : result = stattarget;
312 : : }
313 : :
314 : 8015 : table_close(pg_stext, RowExclusiveLock);
315 : :
316 : 8015 : MemoryContextSwitchTo(oldcxt);
317 : 8015 : MemoryContextDelete(cxt);
318 : :
319 : : /* compute sample size based on the statistics target */
320 : 8015 : return (300 * result);
321 : : }
322 : :
323 : : /*
324 : : * statext_compute_stattarget
325 : : * compute statistics target for an extended statistic
326 : : *
327 : : * When computing target for extended statistics objects, we consider three
328 : : * places where the target may be set - the statistics object itself,
329 : : * attributes the statistics object is defined on, and then the default
330 : : * statistics target.
331 : : *
332 : : * First we look at what's set for the statistics object itself, using the
333 : : * ALTER STATISTICS ... SET STATISTICS command. If we find a valid value
334 : : * there (i.e. not -1) we're done. Otherwise we look at targets set for any
335 : : * of the attributes the statistic is defined on, and if there are columns
336 : : * with defined target, we use the maximum value. We do this mostly for
337 : : * backwards compatibility, because this is what we did before having
338 : : * statistics target for extended statistics.
339 : : *
340 : : * And finally, if we still don't have a statistics target, we use the value
341 : : * set in default_statistics_target.
342 : : */
343 : : static int
344 : 404 : statext_compute_stattarget(int stattarget, int nattrs, VacAttrStats **stats)
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 [ + + ]: 404 : if (stattarget >= 0)
353 : 6 : 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 [ + + ]: 1104 : for (i = 0; i < nattrs; i++)
361 : : {
362 : : /* keep the maximum statistics target */
796 peter@eisentraut.org 363 [ + + ]: 706 : if (stats[i]->attstattarget > stattarget)
364 : 308 : 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 : : */
2188 tomas.vondra@postgre 372 [ + + ]: 398 : if (stattarget < 0)
373 : 90 : stattarget = default_statistics_target;
374 : :
375 : : /* As this point we should have a valid statistics target. */
796 peter@eisentraut.org 376 [ + - - + ]: 398 : Assert((stattarget >= 0) && (stattarget <= MAX_STATISTICS_TARGET));
377 : :
2188 tomas.vondra@postgre 378 : 398 : return stattarget;
379 : : }
380 : :
381 : : /*
382 : : * statext_is_kind_built
383 : : * Is this stat kind built in the given pg_statistic_ext_data tuple?
384 : : */
385 : : bool
3088 alvherre@alvh.no-ip. 386 : 3972 : statext_is_kind_built(HeapTuple htup, char type)
387 : : {
388 : : AttrNumber attnum;
389 : :
390 [ + + + + : 3972 : switch (type)
- ]
391 : : {
392 : 993 : case STATS_EXT_NDISTINCT:
2277 tomas.vondra@postgre 393 : 993 : attnum = Anum_pg_statistic_ext_data_stxdndistinct;
3088 alvherre@alvh.no-ip. 394 : 993 : break;
395 : :
3076 simon@2ndQuadrant.co 396 : 993 : case STATS_EXT_DEPENDENCIES:
2277 tomas.vondra@postgre 397 : 993 : attnum = Anum_pg_statistic_ext_data_stxddependencies;
3076 simon@2ndQuadrant.co 398 : 993 : break;
399 : :
2355 tomas.vondra@postgre 400 : 993 : case STATS_EXT_MCV:
2277 401 : 993 : attnum = Anum_pg_statistic_ext_data_stxdmcv;
2355 402 : 993 : break;
403 : :
1625 404 : 993 : case STATS_EXT_EXPRESSIONS:
405 : 993 : attnum = Anum_pg_statistic_ext_data_stxdexpr;
406 : 993 : break;
407 : :
3088 alvherre@alvh.no-ip. 408 :UBC 0 : default:
409 [ # # ]: 0 : elog(ERROR, "unexpected statistics type requested: %d", type);
410 : : }
411 : :
2719 andrew@dunslane.net 412 :CBC 3972 : return !heap_attisnull(htup, attnum, NULL);
413 : : }
414 : :
415 : : /*
416 : : * Return a list (of StatExtEntry) of statistics objects for the given relation.
417 : : */
418 : : static List *
3088 alvherre@alvh.no-ip. 419 : 13444 : fetch_statentries_for_relation(Relation pg_statext, Oid relid)
420 : : {
421 : : SysScanDesc scan;
422 : : ScanKeyData skey;
423 : : HeapTuple htup;
3034 bruce@momjian.us 424 : 13444 : List *result = NIL;
425 : :
426 : : /*
427 : : * Prepare to scan pg_statistic_ext for entries having stxrelid = this
428 : : * rel.
429 : : */
3088 alvherre@alvh.no-ip. 430 : 13444 : ScanKeyInit(&skey,
431 : : Anum_pg_statistic_ext_stxrelid,
432 : : BTEqualStrategyNumber, F_OIDEQ,
433 : : ObjectIdGetDatum(relid));
434 : :
435 : 13444 : scan = systable_beginscan(pg_statext, StatisticExtRelidIndexId, true,
436 : : NULL, 1, &skey);
437 : :
438 [ + + ]: 13860 : 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;
1625 tomas.vondra@postgre 447 : 416 : List *exprs = NIL;
448 : :
3088 alvherre@alvh.no-ip. 449 : 416 : entry = palloc0(sizeof(StatExtEntry));
450 : 416 : staForm = (Form_pg_statistic_ext) GETSTRUCT(htup);
2482 andres@anarazel.de 451 : 416 : entry->statOid = staForm->oid;
3064 alvherre@alvh.no-ip. 452 : 416 : entry->schema = get_namespace_name(staForm->stxnamespace);
453 : 416 : entry->name = pstrdup(NameStr(staForm->stxname));
454 [ + + ]: 1158 : for (i = 0; i < staForm->stxkeys.dim1; i++)
455 : : {
3088 456 : 742 : entry->columns = bms_add_member(entry->columns,
3064 457 : 742 : staForm->stxkeys.values[i]);
458 : : }
459 : :
538 peter@eisentraut.org 460 : 416 : datum = SysCacheGetAttr(STATEXTOID, htup, Anum_pg_statistic_ext_stxstattarget, &isnull);
461 [ + + ]: 416 : entry->stattarget = isnull ? -1 : DatumGetInt16(datum);
462 : :
463 : : /* decode the stxkind char array into a list of chars */
896 dgustafsson@postgres 464 : 416 : datum = SysCacheGetAttrNotNull(STATEXTOID, htup,
465 : : Anum_pg_statistic_ext_stxkind);
3088 alvherre@alvh.no-ip. 466 : 416 : arr = DatumGetArrayTypeP(datum);
467 [ + - ]: 416 : if (ARR_NDIM(arr) != 1 ||
468 [ + - ]: 416 : ARR_HASNULL(arr) ||
469 [ - + ]: 416 : ARR_ELEMTYPE(arr) != CHAROID)
3064 alvherre@alvh.no-ip. 470 [ # # ]:UBC 0 : elog(ERROR, "stxkind is not a 1-D char array");
3088 alvherre@alvh.no-ip. 471 [ - + ]:CBC 416 : enabled = (char *) ARR_DATA_PTR(arr);
472 [ + + ]: 1184 : for (i = 0; i < ARR_DIMS(arr)[0]; i++)
473 : : {
3076 simon@2ndQuadrant.co 474 [ + + + + : 768 : Assert((enabled[i] == STATS_EXT_NDISTINCT) ||
+ + - + ]
475 : : (enabled[i] == STATS_EXT_DEPENDENCIES) ||
476 : : (enabled[i] == STATS_EXT_MCV) ||
477 : : (enabled[i] == STATS_EXT_EXPRESSIONS));
3088 alvherre@alvh.no-ip. 478 : 768 : entry->types = lappend_int(entry->types, (int) enabled[i]);
479 : : }
480 : :
481 : : /* decode expression (if any) */
1625 tomas.vondra@postgre 482 : 416 : datum = SysCacheGetAttr(STATEXTOID, htup,
483 : : Anum_pg_statistic_ext_stxexprs, &isnull);
484 : :
485 [ + + ]: 416 : if (!isnull)
486 : : {
487 : : char *exprsString;
488 : :
489 : 156 : exprsString = TextDatumGetCString(datum);
490 : 156 : exprs = (List *) stringToNode(exprsString);
491 : :
492 : 156 : 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 : 156 : exprs = (List *) eval_const_expressions(NULL, (Node *) exprs);
503 : :
504 : : /* May as well fix opfuncids too */
505 : 156 : fix_opfuncids((Node *) exprs);
506 : : }
507 : :
508 : 416 : entry->exprs = exprs;
509 : :
3088 alvherre@alvh.no-ip. 510 : 416 : result = lappend(result, entry);
511 : : }
512 : :
513 : 13444 : systable_endscan(scan);
514 : :
515 : 13444 : return result;
516 : : }
517 : :
518 : : /*
519 : : * examine_attribute -- pre-analysis of a single column
520 : : *
521 : : * Determine whether the column is analyzable; if so, create and initialize
522 : : * a VacAttrStats struct for it. If not, return NULL.
523 : : */
524 : : static VacAttrStats *
1625 tomas.vondra@postgre 525 : 300 : examine_attribute(Node *expr)
526 : : {
527 : : HeapTuple typtuple;
528 : : VacAttrStats *stats;
529 : : int i;
530 : : bool ok;
531 : :
532 : : /*
533 : : * Create the VacAttrStats struct.
534 : : */
535 : 300 : stats = (VacAttrStats *) palloc0(sizeof(VacAttrStats));
796 peter@eisentraut.org 536 : 300 : 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 : : */
1625 tomas.vondra@postgre 547 : 300 : stats->attrtypid = exprType(expr);
548 : 300 : stats->attrtypmod = exprTypmod(expr);
549 : 300 : stats->attrcollid = exprCollation(expr);
550 : :
551 : 300 : typtuple = SearchSysCacheCopy1(TYPEOID,
552 : : ObjectIdGetDatum(stats->attrtypid));
553 [ - + ]: 300 : if (!HeapTupleIsValid(typtuple))
1625 tomas.vondra@postgre 554 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
1625 tomas.vondra@postgre 555 :CBC 300 : 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 : 300 : stats->anl_context = NULL;
562 : 300 : stats->tupattnum = InvalidAttrNumber;
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 [ + + ]: 1800 : for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
570 : : {
571 : 1500 : stats->statypid[i] = stats->attrtypid;
572 : 1500 : stats->statyplen[i] = stats->attrtype->typlen;
573 : 1500 : stats->statypbyval[i] = stats->attrtype->typbyval;
574 : 1500 : 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 [ - + ]: 300 : if (OidIsValid(stats->attrtype->typanalyze))
1625 tomas.vondra@postgre 582 :UBC 0 : ok = DatumGetBool(OidFunctionCall1(stats->attrtype->typanalyze,
583 : : PointerGetDatum(stats)));
584 : : else
1625 tomas.vondra@postgre 585 :CBC 300 : ok = std_typanalyze(stats);
586 : :
587 [ + - + - : 300 : if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
- + ]
588 : : {
1625 tomas.vondra@postgre 589 :UBC 0 : heap_freetuple(typtuple);
590 : 0 : pfree(stats);
591 : 0 : return NULL;
592 : : }
593 : :
1625 tomas.vondra@postgre 594 :CBC 300 : return stats;
595 : : }
596 : :
597 : : /*
598 : : * examine_expression -- pre-analysis of a single expression
599 : : *
600 : : * Determine whether the expression is analyzable; if so, create and initialize
601 : : * a VacAttrStats struct for it. If not, return NULL.
602 : : */
603 : : static VacAttrStats *
604 : 300 : examine_expression(Node *expr, int stattarget)
605 : : {
606 : : HeapTuple typtuple;
607 : : VacAttrStats *stats;
608 : : int i;
609 : : bool ok;
610 : :
611 [ - + ]: 300 : Assert(expr != NULL);
612 : :
613 : : /*
614 : : * Create the VacAttrStats struct.
615 : : */
616 : 300 : 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 : : */
796 peter@eisentraut.org 623 : 300 : stats->attstattarget = stattarget;
624 : :
625 : : /*
626 : : * When analyzing an expression, believe the expression tree's type.
627 : : */
1625 tomas.vondra@postgre 628 : 300 : stats->attrtypid = exprType(expr);
629 : 300 : 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 : 300 : stats->attrcollid = exprCollation(expr);
638 : :
639 : 300 : typtuple = SearchSysCacheCopy1(TYPEOID,
640 : : ObjectIdGetDatum(stats->attrtypid));
641 [ - + ]: 300 : if (!HeapTupleIsValid(typtuple))
1625 tomas.vondra@postgre 642 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
643 : :
1625 tomas.vondra@postgre 644 :CBC 300 : stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
645 : 300 : stats->anl_context = CurrentMemoryContext; /* XXX should be using
646 : : * something else? */
647 : 300 : stats->tupattnum = InvalidAttrNumber;
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 [ + + ]: 1800 : for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
655 : : {
656 : 1500 : stats->statypid[i] = stats->attrtypid;
657 : 1500 : stats->statyplen[i] = stats->attrtype->typlen;
658 : 1500 : stats->statypbyval[i] = stats->attrtype->typbyval;
659 : 1500 : 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 [ - + ]: 300 : if (OidIsValid(stats->attrtype->typanalyze))
1625 tomas.vondra@postgre 667 :UBC 0 : ok = DatumGetBool(OidFunctionCall1(stats->attrtype->typanalyze,
668 : : PointerGetDatum(stats)));
669 : : else
1625 tomas.vondra@postgre 670 :CBC 300 : ok = std_typanalyze(stats);
671 : :
672 [ + - + - : 300 : if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
- + ]
673 : : {
1625 tomas.vondra@postgre 674 :UBC 0 : heap_freetuple(typtuple);
675 : 0 : pfree(stats);
676 : 0 : return NULL;
677 : : }
678 : :
1625 tomas.vondra@postgre 679 :CBC 300 : return stats;
680 : : }
681 : :
682 : : /*
683 : : * Using 'vacatts' of size 'nvacatts' as input data, return a newly-built
684 : : * VacAttrStats array which includes only the items corresponding to
685 : : * attributes indicated by 'attrs'. If we don't have all of the per-column
686 : : * stats available to compute the extended stats, then we return NULL to
687 : : * indicate to the caller that the stats should not be built.
688 : : */
689 : : static VacAttrStats **
242 rguo@postgresql.org 690 : 416 : lookup_var_attr_stats(Bitmapset *attrs, List *exprs,
691 : : int nvacatts, VacAttrStats **vacatts)
692 : : {
3088 alvherre@alvh.no-ip. 693 : 416 : int i = 0;
694 : 416 : int x = -1;
695 : : int natts;
696 : : VacAttrStats **stats;
697 : : ListCell *lc;
698 : :
1625 tomas.vondra@postgre 699 : 416 : natts = bms_num_members(attrs) + list_length(exprs);
700 : :
701 : 416 : stats = (VacAttrStats **) palloc(natts * sizeof(VacAttrStats *));
702 : :
703 : : /* lookup VacAttrStats info for the requested columns (same attnum) */
3088 alvherre@alvh.no-ip. 704 [ + + ]: 1140 : while ((x = bms_next_member(attrs, x)) >= 0)
705 : : {
706 : : int j;
707 : :
708 : 736 : stats[i] = NULL;
3064 709 [ + + ]: 2202 : for (j = 0; j < nvacatts; j++)
710 : : {
711 [ + + ]: 2190 : if (x == vacatts[j]->tupattnum)
712 : : {
713 : 724 : stats[i] = vacatts[j];
3088 714 : 724 : break;
715 : : }
716 : : }
717 : :
718 [ + + ]: 736 : 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 : : */
3064 725 : 12 : pfree(stats);
726 : 12 : return NULL;
727 : : }
728 : :
3088 729 : 724 : i++;
730 : : }
731 : :
732 : : /* also add info for expressions */
1625 tomas.vondra@postgre 733 [ + + + + : 704 : foreach(lc, exprs)
+ + ]
734 : : {
735 : 300 : Node *expr = (Node *) lfirst(lc);
736 : :
737 : 300 : 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 : 300 : stats[i]->tupDesc = vacatts[0]->tupDesc;
746 : :
747 : 300 : i++;
748 : : }
749 : :
3088 alvherre@alvh.no-ip. 750 : 404 : return stats;
751 : : }
752 : :
753 : : /*
754 : : * statext_store
755 : : * Serializes the statistics and stores them into the pg_statistic_ext_data
756 : : * tuple.
757 : : */
758 : : static void
1329 tomas.vondra@postgre 759 : 199 : statext_store(Oid statOid, bool inh,
760 : : MVNDistinct *ndistinct, MVDependencies *dependencies,
761 : : MCVList *mcv, Datum exprs, VacAttrStats **stats)
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 : :
1985 tgl@sss.pgh.pa.us 768 : 199 : pg_stextdata = table_open(StatisticExtDataRelationId, RowExclusiveLock);
769 : :
2838 alvherre@alvh.no-ip. 770 : 199 : memset(nulls, true, sizeof(nulls));
771 : 199 : memset(values, 0, sizeof(values));
772 : :
773 : : /* basic info */
1329 tomas.vondra@postgre 774 : 199 : values[Anum_pg_statistic_ext_data_stxoid - 1] = ObjectIdGetDatum(statOid);
775 : 199 : nulls[Anum_pg_statistic_ext_data_stxoid - 1] = false;
776 : :
777 : 199 : values[Anum_pg_statistic_ext_data_stxdinherit - 1] = BoolGetDatum(inh);
778 : 199 : 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 : : */
3088 alvherre@alvh.no-ip. 784 [ + + ]: 199 : if (ndistinct != NULL)
785 : : {
786 : 97 : bytea *data = statext_ndistinct_serialize(ndistinct);
787 : :
2277 tomas.vondra@postgre 788 : 97 : nulls[Anum_pg_statistic_ext_data_stxdndistinct - 1] = (data == NULL);
789 : 97 : values[Anum_pg_statistic_ext_data_stxdndistinct - 1] = PointerGetDatum(data);
790 : : }
791 : :
3076 simon@2ndQuadrant.co 792 [ + + ]: 199 : if (dependencies != NULL)
793 : : {
794 : 64 : bytea *data = statext_dependencies_serialize(dependencies);
795 : :
2277 tomas.vondra@postgre 796 : 64 : nulls[Anum_pg_statistic_ext_data_stxddependencies - 1] = (data == NULL);
797 : 64 : values[Anum_pg_statistic_ext_data_stxddependencies - 1] = PointerGetDatum(data);
798 : : }
2355 799 [ + + ]: 199 : if (mcv != NULL)
800 : : {
801 : 109 : bytea *data = statext_mcv_serialize(mcv, stats);
802 : :
2277 803 : 109 : nulls[Anum_pg_statistic_ext_data_stxdmcv - 1] = (data == NULL);
804 : 109 : values[Anum_pg_statistic_ext_data_stxdmcv - 1] = PointerGetDatum(data);
805 : : }
1625 806 [ + + ]: 199 : if (exprs != (Datum) 0)
807 : : {
808 : 78 : nulls[Anum_pg_statistic_ext_data_stxdexpr - 1] = false;
809 : 78 : 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 : : */
1329 816 : 199 : RemoveStatisticsDataById(statOid, inh);
817 : :
818 : : /* form and insert a new tuple */
819 : 199 : stup = heap_form_tuple(RelationGetDescr(pg_stextdata), values, nulls);
820 : 199 : CatalogTupleInsert(pg_stextdata, stup);
821 : :
3088 alvherre@alvh.no-ip. 822 : 199 : heap_freetuple(stup);
823 : :
2277 tomas.vondra@postgre 824 : 199 : table_close(pg_stextdata, RowExclusiveLock);
3088 alvherre@alvh.no-ip. 825 : 199 : }
826 : :
827 : : /* initialize multi-dimensional sort */
828 : : MultiSortSupport
829 : 703 : multi_sort_init(int ndims)
830 : : {
831 : : MultiSortSupport mss;
832 : :
833 [ - + ]: 703 : Assert(ndims >= 2);
834 : :
835 : 703 : mss = (MultiSortSupport) palloc0(offsetof(MultiSortSupportData, ssup)
2999 tgl@sss.pgh.pa.us 836 : 703 : + sizeof(SortSupportData) * ndims);
837 : :
3088 alvherre@alvh.no-ip. 838 : 703 : mss->ndims = ndims;
839 : :
840 : 703 : return mss;
841 : : }
842 : :
843 : : /*
844 : : * Prepare sort support info using the given sort operator and collation
845 : : * at the position 'sortdim'
846 : : */
847 : : void
2458 tgl@sss.pgh.pa.us 848 : 1646 : multi_sort_add_dimension(MultiSortSupport mss, int sortdim,
849 : : Oid oper, Oid collation)
850 : : {
3034 bruce@momjian.us 851 : 1646 : SortSupport ssup = &mss->ssup[sortdim];
852 : :
3088 alvherre@alvh.no-ip. 853 : 1646 : ssup->ssup_cxt = CurrentMemoryContext;
2458 tgl@sss.pgh.pa.us 854 : 1646 : ssup->ssup_collation = collation;
3088 alvherre@alvh.no-ip. 855 : 1646 : ssup->ssup_nulls_first = false;
856 : :
857 : 1646 : PrepareSortSupportFromOrderingOp(oper, ssup);
858 : 1646 : }
859 : :
860 : : /* compare all the dimensions in the selected order */
861 : : int
862 : 9845586 : multi_sort_compare(const void *a, const void *b, void *arg)
863 : : {
864 : 9845586 : MultiSortSupport mss = (MultiSortSupport) arg;
865 : 9845586 : SortItem *ia = (SortItem *) a;
866 : 9845586 : SortItem *ib = (SortItem *) b;
867 : : int i;
868 : :
869 [ + + ]: 18460163 : for (i = 0; i < mss->ndims; i++)
870 : : {
871 : : int compare;
872 : :
873 : 16125549 : compare = ApplySortComparator(ia->values[i], ia->isnull[i],
874 : 16125549 : ib->values[i], ib->isnull[i],
875 : 16125549 : &mss->ssup[i]);
876 : :
877 [ + + ]: 16125549 : if (compare != 0)
878 : 7510972 : return compare;
879 : : }
880 : :
881 : : /* equal by default */
882 : 2334614 : return 0;
883 : : }
884 : :
885 : : /* compare selected dimension */
886 : : int
887 : 736305 : multi_sort_compare_dim(int dim, const SortItem *a, const SortItem *b,
888 : : MultiSortSupport mss)
889 : : {
890 : 1472610 : return ApplySortComparator(a->values[dim], a->isnull[dim],
891 : 736305 : b->values[dim], b->isnull[dim],
892 : 736305 : &mss->ssup[dim]);
893 : : }
894 : :
895 : : int
896 : 752265 : multi_sort_compare_dims(int start, int end,
897 : : const SortItem *a, const SortItem *b,
898 : : MultiSortSupport mss)
899 : : {
900 : : int dim;
901 : :
902 [ + + ]: 1701837 : for (dim = start; dim <= end; dim++)
903 : : {
904 : 965532 : int r = ApplySortComparator(a->values[dim], a->isnull[dim],
905 : 965532 : b->values[dim], b->isnull[dim],
906 : 965532 : &mss->ssup[dim]);
907 : :
908 [ + + ]: 965532 : if (r != 0)
909 : 15960 : return r;
910 : : }
911 : :
912 : 736305 : return 0;
913 : : }
914 : :
915 : : int
2355 tomas.vondra@postgre 916 : 99765 : compare_scalars_simple(const void *a, const void *b, void *arg)
917 : : {
918 : 99765 : return compare_datums_simple(*(Datum *) a,
919 : : *(Datum *) b,
920 : : (SortSupport) arg);
921 : : }
922 : :
923 : : int
924 : 126057 : compare_datums_simple(Datum a, Datum b, SortSupport ssup)
925 : : {
926 : 126057 : return ApplySortComparator(a, false, b, false, ssup);
927 : : }
928 : :
929 : : /*
930 : : * build_attnums_array
931 : : * Transforms a bitmap into an array of AttrNumber values.
932 : : *
933 : : * This is used for extended statistics only, so all the attributes must be
934 : : * user-defined. That means offsetting by FirstLowInvalidHeapAttributeNumber
935 : : * is not necessary here (and when querying the bitmap).
936 : : */
937 : : AttrNumber *
1625 tomas.vondra@postgre 938 :UBC 0 : build_attnums_array(Bitmapset *attrs, int nexprs, int *numattrs)
939 : : {
940 : : int i,
941 : : j;
942 : : AttrNumber *attnums;
2355 943 : 0 : int num = bms_num_members(attrs);
944 : :
945 [ # # ]: 0 : if (numattrs)
946 : 0 : *numattrs = num;
947 : :
948 : : /* build attnums from the bitmapset */
949 : 0 : attnums = (AttrNumber *) palloc(sizeof(AttrNumber) * num);
950 : 0 : i = 0;
951 : 0 : j = -1;
952 [ # # ]: 0 : while ((j = bms_next_member(attrs, j)) >= 0)
953 : : {
1620 tgl@sss.pgh.pa.us 954 : 0 : 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 : : */
1625 tomas.vondra@postgre 962 [ # # ]: 0 : Assert(AttributeNumberIsValid(attnum));
963 [ # # ]: 0 : Assert(attnum <= MaxAttrNumber);
964 [ # # ]: 0 : Assert(attnum >= (-nexprs));
965 : :
966 : 0 : attnums[i++] = (AttrNumber) attnum;
967 : :
968 : : /* protect against overflows */
2355 969 [ # # ]: 0 : Assert(i <= num);
970 : : }
971 : :
972 : 0 : return attnums;
973 : : }
974 : :
975 : : /*
976 : : * build_sorted_items
977 : : * build a sorted array of SortItem with values from rows
978 : : *
979 : : * Note: All the memory is allocated in a single chunk, so that the caller
980 : : * can simply pfree the return value to release all of it.
981 : : */
982 : : SortItem *
1625 tomas.vondra@postgre 983 :CBC 423 : build_sorted_items(StatsBuildData *data, int *nitems,
984 : : MultiSortSupport mss,
985 : : int numattrs, AttrNumber *attnums)
986 : : {
987 : : int i,
988 : : j,
989 : : len,
990 : : nrows;
991 : 423 : int nvalues = data->numrows * numattrs;
992 : :
993 : : SortItem *items;
994 : : Datum *values;
995 : : bool *isnull;
996 : : char *ptr;
997 : : int *typlen;
998 : :
999 : : /* Compute the total amount of memory we need (both items and values). */
1000 : 423 : len = data->numrows * sizeof(SortItem) + nvalues * (sizeof(Datum) + sizeof(bool));
1001 : :
1002 : : /* Allocate the memory and split it into the pieces. */
2355 1003 : 423 : ptr = palloc0(len);
1004 : :
1005 : : /* items to sort */
1006 : 423 : items = (SortItem *) ptr;
1625 1007 : 423 : ptr += data->numrows * sizeof(SortItem);
1008 : :
1009 : : /* values and null flags */
2355 1010 : 423 : values = (Datum *) ptr;
1011 : 423 : ptr += nvalues * sizeof(Datum);
1012 : :
1013 : 423 : isnull = (bool *) ptr;
1014 : 423 : ptr += nvalues * sizeof(bool);
1015 : :
1016 : : /* make sure we consumed the whole buffer exactly */
1017 [ - + ]: 423 : Assert((ptr - (char *) items) == len);
1018 : :
1019 : : /* fix the pointers to Datum and bool arrays */
1625 1020 : 423 : nrows = 0;
1021 [ + + ]: 994602 : for (i = 0; i < data->numrows; i++)
1022 : : {
1023 : 994179 : items[nrows].values = &values[nrows * numattrs];
1024 : 994179 : items[nrows].isnull = &isnull[nrows * numattrs];
1025 : :
1026 : 994179 : nrows++;
1027 : : }
1028 : :
1029 : : /* build a local cache of typlen for all attributes */
1030 : 423 : typlen = (int *) palloc(sizeof(int) * data->nattnums);
1031 [ + + ]: 1560 : for (i = 0; i < data->nattnums; i++)
1032 : 1137 : typlen[i] = get_typlen(data->stats[i]->attrtypid);
1033 : :
1034 : 423 : nrows = 0;
1035 [ + + ]: 994602 : for (i = 0; i < data->numrows; i++)
1036 : : {
1037 : 994179 : bool toowide = false;
1038 : :
1039 : : /* load the values/null flags from sample rows */
2355 1040 [ + + ]: 3420825 : for (j = 0; j < numattrs; j++)
1041 : : {
1042 : : Datum value;
1043 : : bool isnull;
1044 : : int attlen;
1625 1045 : 2426646 : AttrNumber attnum = attnums[j];
1046 : :
1047 : : int idx;
1048 : :
1049 : : /* match attnum to the pre-calculated data */
1050 [ + - ]: 4783383 : for (idx = 0; idx < data->nattnums; idx++)
1051 : : {
1052 [ + + ]: 4783383 : if (attnum == data->attnums[idx])
1053 : 2426646 : break;
1054 : : }
1055 : :
1056 [ - + ]: 2426646 : Assert(idx < data->nattnums);
1057 : :
1058 : 2426646 : value = data->values[idx][i];
1059 : 2426646 : isnull = data->nulls[idx][i];
1060 : 2426646 : attlen = typlen[idx];
1061 : :
1062 : : /*
1063 : : * If this is a varlena value, check if it's too wide and if yes
1064 : : * then skip the whole item. Otherwise detoast the value.
1065 : : *
1066 : : * XXX It may happen that we've already detoasted some preceding
1067 : : * values for the current item. We don't bother to cleanup those
1068 : : * on the assumption that those are small (below WIDTH_THRESHOLD)
1069 : : * and will be discarded at the end of analyze.
1070 : : */
1071 [ + + + + ]: 2426646 : if ((!isnull) && (attlen == -1))
1072 : : {
2355 1073 [ - + ]: 740154 : if (toast_raw_datum_size(value) > WIDTH_THRESHOLD)
1074 : : {
2355 tomas.vondra@postgre 1075 :UBC 0 : toowide = true;
1076 : 0 : break;
1077 : : }
1078 : :
2355 tomas.vondra@postgre 1079 :CBC 740154 : value = PointerGetDatum(PG_DETOAST_DATUM(value));
1080 : : }
1081 : :
1625 1082 : 2426646 : items[nrows].values[j] = value;
1083 : 2426646 : items[nrows].isnull[j] = isnull;
1084 : : }
1085 : :
2355 1086 [ - + ]: 994179 : if (toowide)
2355 tomas.vondra@postgre 1087 :UBC 0 : continue;
1088 : :
1625 tomas.vondra@postgre 1089 :CBC 994179 : nrows++;
1090 : : }
1091 : :
1092 : : /* store the actual number of items (ignoring the too-wide ones) */
1093 : 423 : *nitems = nrows;
1094 : :
1095 : : /* all items were too wide */
1096 [ - + ]: 423 : if (nrows == 0)
1097 : : {
1098 : : /* everything is allocated as a single chunk */
2355 tomas.vondra@postgre 1099 :UBC 0 : pfree(items);
1100 : 0 : return NULL;
1101 : : }
1102 : :
1103 : : /* do the sort, using the multi-sort */
942 peter@eisentraut.org 1104 :CBC 423 : qsort_interruptible(items, nrows, sizeof(SortItem),
1105 : : multi_sort_compare, mss);
1106 : :
2355 tomas.vondra@postgre 1107 : 423 : return items;
1108 : : }
1109 : :
1110 : : /*
1111 : : * has_stats_of_kind
1112 : : * Check whether the list contains statistic of a given kind
1113 : : */
1114 : : bool
3076 simon@2ndQuadrant.co 1115 : 2514 : has_stats_of_kind(List *stats, char requiredkind)
1116 : : {
1117 : : ListCell *l;
1118 : :
1119 [ + - + + : 4143 : foreach(l, stats)
+ + ]
1120 : : {
1121 : 2895 : StatisticExtInfo *stat = (StatisticExtInfo *) lfirst(l);
1122 : :
1123 [ + + ]: 2895 : if (stat->kind == requiredkind)
1124 : 1266 : return true;
1125 : : }
1126 : :
1127 : 1248 : return false;
1128 : : }
1129 : :
1130 : : /*
1131 : : * stat_find_expression
1132 : : * Search for an expression in statistics object's list of expressions.
1133 : : *
1134 : : * Returns the index of the expression in the statistics object's list of
1135 : : * expressions, or -1 if not found.
1136 : : */
1137 : : static int
1625 tomas.vondra@postgre 1138 : 297 : stat_find_expression(StatisticExtInfo *stat, Node *expr)
1139 : : {
1140 : : ListCell *lc;
1141 : : int idx;
1142 : :
1143 : 297 : idx = 0;
1144 [ + + + + : 537 : foreach(lc, stat->exprs)
+ + ]
1145 : : {
1146 : 486 : Node *stat_expr = (Node *) lfirst(lc);
1147 : :
1148 [ + + ]: 486 : if (equal(stat_expr, expr))
1149 : 246 : return idx;
1150 : 240 : idx++;
1151 : : }
1152 : :
1153 : : /* Expression not found */
1154 : 51 : return -1;
1155 : : }
1156 : :
1157 : : /*
1158 : : * stat_covers_expressions
1159 : : * Test whether a statistics object covers all expressions in a list.
1160 : : *
1161 : : * Returns true if all expressions are covered. If expr_idxs is non-NULL, it
1162 : : * is populated with the indexes of the expressions found.
1163 : : */
1164 : : static bool
1165 : 1557 : stat_covers_expressions(StatisticExtInfo *stat, List *exprs,
1166 : : Bitmapset **expr_idxs)
1167 : : {
1168 : : ListCell *lc;
1169 : :
1170 [ + + + + : 1803 : foreach(lc, exprs)
+ + ]
1171 : : {
1172 : 297 : Node *expr = (Node *) lfirst(lc);
1173 : : int expr_idx;
1174 : :
1175 : 297 : expr_idx = stat_find_expression(stat, expr);
1176 [ + + ]: 297 : if (expr_idx == -1)
1177 : 51 : return false;
1178 : :
1179 [ + + ]: 246 : if (expr_idxs != NULL)
1180 : 123 : *expr_idxs = bms_add_member(*expr_idxs, expr_idx);
1181 : : }
1182 : :
1183 : : /* If we reach here, all expressions are covered */
1184 : 1506 : return true;
1185 : : }
1186 : :
1187 : : /*
1188 : : * choose_best_statistics
1189 : : * Look for and return statistics with the specified 'requiredkind' which
1190 : : * have keys that match at least two of the given attnums. Return NULL if
1191 : : * there's no match.
1192 : : *
1193 : : * The current selection criteria is very simple - we choose the statistics
1194 : : * object referencing the most attributes in covered (and still unestimated
1195 : : * clauses), breaking ties in favor of objects with fewer keys overall.
1196 : : *
1197 : : * The clause_attnums is an array of bitmaps, storing attnums for individual
1198 : : * clauses. A NULL element means the clause is either incompatible or already
1199 : : * estimated.
1200 : : *
1201 : : * XXX If multiple statistics objects tie on both criteria, then which object
1202 : : * is chosen depends on the order that they appear in the stats list. Perhaps
1203 : : * further tiebreakers are needed.
1204 : : */
1205 : : StatisticExtInfo *
1329 1206 : 675 : choose_best_statistics(List *stats, char requiredkind, bool inh,
1207 : : Bitmapset **clause_attnums, List **clause_exprs,
1208 : : int nclauses)
1209 : : {
1210 : : ListCell *lc;
3076 simon@2ndQuadrant.co 1211 : 675 : StatisticExtInfo *best_match = NULL;
1212 : 675 : int best_num_matched = 2; /* goal #1: maximize */
1213 : 675 : int best_match_keys = (STATS_MAX_DIMENSIONS + 1); /* goal #2: minimize */
1214 : :
1215 [ + - + + : 1773 : foreach(lc, stats)
+ + ]
1216 : : {
1217 : : int i;
1218 : 1098 : StatisticExtInfo *info = (StatisticExtInfo *) lfirst(lc);
1625 tomas.vondra@postgre 1219 : 1098 : Bitmapset *matched_attnums = NULL;
1220 : 1098 : Bitmapset *matched_exprs = NULL;
1221 : : int num_matched;
1222 : : int numkeys;
1223 : :
1224 : : /* skip statistics that are not of the correct type */
3076 simon@2ndQuadrant.co 1225 [ + + ]: 1098 : if (info->kind != requiredkind)
1226 : 234 : continue;
1227 : :
1228 : : /* skip statistics with mismatching inheritance flag */
1329 tomas.vondra@postgre 1229 [ + + ]: 864 : if (info->inherit != inh)
1230 : 12 : continue;
1231 : :
1232 : : /*
1233 : : * Collect attributes and expressions in remaining (unestimated)
1234 : : * clauses fully covered by this statistic object.
1235 : : *
1236 : : * We know already estimated clauses have both clause_attnums and
1237 : : * clause_exprs set to NULL. We leave the pointers NULL if already
1238 : : * estimated, or we reset them to NULL after estimating the clause.
1239 : : */
2109 1240 [ + + ]: 2898 : for (i = 0; i < nclauses; i++)
1241 : : {
1625 1242 : 2046 : Bitmapset *expr_idxs = NULL;
1243 : :
1244 : : /* ignore incompatible/estimated clauses */
1245 [ + + + + ]: 2046 : if (!clause_attnums[i] && !clause_exprs[i])
2109 1246 : 1242 : continue;
1247 : :
1248 : : /* ignore clauses that are not covered by this object */
1625 1249 [ + + ]: 1017 : if (!bms_is_subset(clause_attnums[i], info->keys) ||
1250 [ + + ]: 852 : !stat_covers_expressions(info, clause_exprs[i], &expr_idxs))
2109 1251 : 213 : continue;
1252 : :
1253 : : /* record attnums and indexes of expressions covered */
1625 1254 : 804 : matched_attnums = bms_add_members(matched_attnums, clause_attnums[i]);
1255 : 804 : matched_exprs = bms_add_members(matched_exprs, expr_idxs);
1256 : : }
1257 : :
1258 : 852 : num_matched = bms_num_members(matched_attnums) + bms_num_members(matched_exprs);
1259 : :
1260 : 852 : bms_free(matched_attnums);
1261 : 852 : bms_free(matched_exprs);
1262 : :
1263 : : /*
1264 : : * save the actual number of keys in the stats so that we can choose
1265 : : * the narrowest stats with the most matching keys.
1266 : : */
1267 : 852 : numkeys = bms_num_members(info->keys) + list_length(info->exprs);
1268 : :
1269 : : /*
1270 : : * Use this object when it increases the number of matched attributes
1271 : : * and expressions or when it matches the same number of attributes
1272 : : * and expressions but these stats have fewer keys than any previous
1273 : : * match.
1274 : : */
3076 simon@2ndQuadrant.co 1275 [ + + + + ]: 852 : if (num_matched > best_num_matched ||
1276 [ + + ]: 207 : (num_matched == best_num_matched && numkeys < best_match_keys))
1277 : : {
1278 : 303 : best_match = info;
1279 : 303 : best_num_matched = num_matched;
1280 : 303 : best_match_keys = numkeys;
1281 : : }
1282 : : }
1283 : :
1284 : 675 : return best_match;
1285 : : }
1286 : :
1287 : : /*
1288 : : * statext_is_compatible_clause_internal
1289 : : * Determines if the clause is compatible with MCV lists.
1290 : : *
1291 : : * To be compatible, the given clause must be a combination of supported
1292 : : * clauses built from Vars or sub-expressions (where a sub-expression is
1293 : : * something that exactly matches an expression found in statistics objects).
1294 : : * This function recursively examines the clause and extracts any
1295 : : * sub-expressions that will need to be matched against statistics.
1296 : : *
1297 : : * Currently, we only support the following types of clauses:
1298 : : *
1299 : : * (a) OpExprs of the form (Var/Expr op Const), or (Const op Var/Expr), where
1300 : : * the op is one of ("=", "<", ">", ">=", "<=")
1301 : : *
1302 : : * (b) (Var/Expr IS [NOT] NULL)
1303 : : *
1304 : : * (c) combinations using AND/OR/NOT
1305 : : *
1306 : : * (d) ScalarArrayOpExprs of the form (Var/Expr op ANY (Const)) or
1307 : : * (Var/Expr op ALL (Const))
1308 : : *
1309 : : * In the future, the range of supported clauses may be expanded to more
1310 : : * complex cases, for example (Var op Var).
1311 : : *
1312 : : * Arguments:
1313 : : * clause: (sub)clause to be inspected (bare clause, not a RestrictInfo)
1314 : : * relid: rel that all Vars in clause must belong to
1315 : : * *attnums: input/output parameter collecting attribute numbers of all
1316 : : * mentioned Vars. Note that we do not offset the attribute numbers,
1317 : : * so we can't cope with system columns.
1318 : : * *exprs: input/output parameter collecting primitive subclauses within
1319 : : * the clause tree
1320 : : * *leakproof: input/output parameter recording the leakproofness of the
1321 : : * clause tree. This should be true initially, and will be set to false
1322 : : * if any operator function used in an OpExpr is not leakproof.
1323 : : *
1324 : : * Returns false if there is something we definitively can't handle.
1325 : : * On true return, we can proceed to match the *exprs against statistics.
1326 : : */
1327 : : static bool
2267 dean.a.rasheed@gmail 1328 : 1731 : statext_is_compatible_clause_internal(PlannerInfo *root, Node *clause,
1329 : : Index relid, Bitmapset **attnums,
1330 : : List **exprs, bool *leakproof)
1331 : : {
1332 : : /* Look inside any binary-compatible relabeling (as in examine_variable) */
2355 tomas.vondra@postgre 1333 [ - + ]: 1731 : if (IsA(clause, RelabelType))
2355 tomas.vondra@postgre 1334 :UBC 0 : clause = (Node *) ((RelabelType *) clause)->arg;
1335 : :
1336 : : /* plain Var references (boolean Vars or recursive checks) */
2355 tomas.vondra@postgre 1337 [ + + ]:CBC 1731 : if (IsA(clause, Var))
1338 : : {
1339 : 774 : Var *var = (Var *) clause;
1340 : :
1341 : : /* Ensure var is from the correct relation */
1342 [ - + ]: 774 : if (var->varno != relid)
2355 tomas.vondra@postgre 1343 :UBC 0 : return false;
1344 : :
1345 : : /* we also better ensure the Var is from the current level */
2355 tomas.vondra@postgre 1346 [ - + ]:CBC 774 : if (var->varlevelsup > 0)
2355 tomas.vondra@postgre 1347 :UBC 0 : return false;
1348 : :
1349 : : /*
1350 : : * Also reject system attributes and whole-row Vars (we don't allow
1351 : : * stats on those).
1352 : : */
2355 tomas.vondra@postgre 1353 [ - + ]:CBC 774 : if (!AttrNumberIsForUserDefinedAttr(var->varattno))
2355 tomas.vondra@postgre 1354 :UBC 0 : return false;
1355 : :
1356 : : /* OK, record the attnum for later permissions checks. */
2355 tomas.vondra@postgre 1357 :CBC 774 : *attnums = bms_add_member(*attnums, var->varattno);
1358 : :
1359 : 774 : return true;
1360 : : }
1361 : :
1362 : : /* (Var/Expr op Const) or (Const op Var/Expr) */
1363 [ + + ]: 957 : if (is_opclause(clause))
1364 : : {
1365 : 696 : OpExpr *expr = (OpExpr *) clause;
1366 : : Node *clause_expr;
1367 : :
1368 : : /* Only expressions with two arguments are considered compatible. */
1369 [ - + ]: 696 : if (list_length(expr->args) != 2)
2355 tomas.vondra@postgre 1370 :UBC 0 : return false;
1371 : :
1372 : : /* Check if the expression has the right shape */
1625 tomas.vondra@postgre 1373 [ - + ]:CBC 696 : if (!examine_opclause_args(expr->args, &clause_expr, NULL, NULL))
2002 tomas.vondra@postgre 1374 :UBC 0 : return false;
1375 : :
1376 : : /*
1377 : : * If it's not one of the supported operators ("=", "<", ">", etc.),
1378 : : * just ignore the clause, as it's not compatible with MCV lists.
1379 : : *
1380 : : * This uses the function for estimating selectivity, not the operator
1381 : : * directly (a bit awkward, but well ...).
1382 : : */
2002 tomas.vondra@postgre 1383 [ + - ]:CBC 696 : switch (get_oprrest(expr->opno))
1384 : : {
1385 : 696 : case F_EQSEL:
1386 : : case F_NEQSEL:
1387 : : case F_SCALARLTSEL:
1388 : : case F_SCALARLESEL:
1389 : : case F_SCALARGTSEL:
1390 : : case F_SCALARGESEL:
1391 : : /* supported, will continue with inspection of the Var/Expr */
1392 : 696 : break;
1393 : :
2002 tomas.vondra@postgre 1394 :UBC 0 : default:
1395 : : /* other estimators are considered unknown/unsupported */
1396 : 0 : return false;
1397 : : }
1398 : :
1399 : : /* Check if the operator is leakproof */
26 dean.a.rasheed@gmail 1400 [ + + ]:CBC 696 : if (*leakproof)
1401 : 690 : *leakproof = get_func_leakproof(get_opcode(expr->opno));
1402 : :
1403 : : /* Check (Var op Const) or (Const op Var) clauses by recursing. */
1625 tomas.vondra@postgre 1404 [ + + ]: 696 : if (IsA(clause_expr, Var))
1405 : 576 : return statext_is_compatible_clause_internal(root, clause_expr,
1406 : : relid, attnums,
1407 : : exprs, leakproof);
1408 : :
1409 : : /* Otherwise we have (Expr op Const) or (Const op Expr). */
1410 : 120 : *exprs = lappend(*exprs, clause_expr);
1411 : 120 : return true;
1412 : : }
1413 : :
1414 : : /* Var/Expr IN Array */
2002 1415 [ + + ]: 261 : if (IsA(clause, ScalarArrayOpExpr))
1416 : : {
1941 tgl@sss.pgh.pa.us 1417 : 144 : ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) clause;
1418 : : Node *clause_expr;
1419 : : bool expronleft;
1420 : :
1421 : : /* Only expressions with two arguments are considered compatible. */
2002 tomas.vondra@postgre 1422 [ - + ]: 144 : if (list_length(expr->args) != 2)
2002 tomas.vondra@postgre 1423 :UBC 0 : return false;
1424 : :
1425 : : /* Check if the expression has the right shape (one Var, one Const) */
1128 tgl@sss.pgh.pa.us 1426 [ - + ]:CBC 144 : if (!examine_opclause_args(expr->args, &clause_expr, NULL, &expronleft))
1128 tgl@sss.pgh.pa.us 1427 :UBC 0 : return false;
1428 : :
1429 : : /* We only support Var on left, Const on right */
1128 tgl@sss.pgh.pa.us 1430 [ + + ]:CBC 144 : if (!expronleft)
2355 tomas.vondra@postgre 1431 : 3 : return false;
1432 : :
1433 : : /*
1434 : : * If it's not one of the supported operators ("=", "<", ">", etc.),
1435 : : * just ignore the clause, as it's not compatible with MCV lists.
1436 : : *
1437 : : * This uses the function for estimating selectivity, not the operator
1438 : : * directly (a bit awkward, but well ...).
1439 : : */
1440 [ + - ]: 141 : switch (get_oprrest(expr->opno))
1441 : : {
1442 : 141 : case F_EQSEL:
1443 : : case F_NEQSEL:
1444 : : case F_SCALARLTSEL:
1445 : : case F_SCALARLESEL:
1446 : : case F_SCALARGTSEL:
1447 : : case F_SCALARGESEL:
1448 : : /* supported, will continue with inspection of the Var/Expr */
1449 : 141 : break;
1450 : :
2355 tomas.vondra@postgre 1451 :UBC 0 : default:
1452 : : /* other estimators are considered unknown/unsupported */
1453 : 0 : return false;
1454 : : }
1455 : :
1456 : : /* Check if the operator is leakproof */
26 dean.a.rasheed@gmail 1457 [ + - ]:CBC 141 : if (*leakproof)
1458 : 141 : *leakproof = get_func_leakproof(get_opcode(expr->opno));
1459 : :
1460 : : /* Check Var IN Array clauses by recursing. */
1625 tomas.vondra@postgre 1461 [ + + ]: 141 : if (IsA(clause_expr, Var))
1462 : 114 : return statext_is_compatible_clause_internal(root, clause_expr,
1463 : : relid, attnums,
1464 : : exprs, leakproof);
1465 : :
1466 : : /* Otherwise we have Expr IN Array. */
1467 : 27 : *exprs = lappend(*exprs, clause_expr);
1468 : 27 : return true;
1469 : : }
1470 : :
1471 : : /* AND/OR/NOT clause */
2355 1472 [ + - + + ]: 234 : if (is_andclause(clause) ||
1473 [ + + ]: 207 : is_orclause(clause) ||
1474 : 90 : is_notclause(clause))
1475 : : {
1476 : : /*
1477 : : * AND/OR/NOT-clauses are supported if all sub-clauses are supported
1478 : : *
1479 : : * Perhaps we could improve this by handling mixed cases, when some of
1480 : : * the clauses are supported and some are not. Selectivity for the
1481 : : * supported subclauses would be computed using extended statistics,
1482 : : * and the remaining clauses would be estimated using the traditional
1483 : : * algorithm (product of selectivities).
1484 : : *
1485 : : * It however seems overly complex, and in a way we already do that
1486 : : * because if we reject the whole clause as unsupported here, it will
1487 : : * be eventually passed to clauselist_selectivity() which does exactly
1488 : : * this (split into supported/unsupported clauses etc).
1489 : : */
1490 : 42 : BoolExpr *expr = (BoolExpr *) clause;
1491 : : ListCell *lc;
1492 : :
1493 [ + - + + : 111 : foreach(lc, expr->args)
+ + ]
1494 : : {
1495 : : /*
1496 : : * If we find an incompatible clause in the arguments, treat the
1497 : : * whole clause as incompatible.
1498 : : */
2267 dean.a.rasheed@gmail 1499 [ - + ]: 69 : if (!statext_is_compatible_clause_internal(root,
1500 : 69 : (Node *) lfirst(lc),
1501 : : relid, attnums, exprs,
1502 : : leakproof))
2355 tomas.vondra@postgre 1503 :UBC 0 : return false;
1504 : : }
1505 : :
2355 tomas.vondra@postgre 1506 :CBC 42 : return true;
1507 : : }
1508 : :
1509 : : /* Var/Expr IS NULL */
1510 [ + + ]: 75 : if (IsA(clause, NullTest))
1511 : : {
1512 : 72 : NullTest *nt = (NullTest *) clause;
1513 : :
1514 : : /* Check Var IS NULL clauses by recursing. */
1625 1515 [ + + ]: 72 : if (IsA(nt->arg, Var))
26 dean.a.rasheed@gmail 1516 : 45 : return statext_is_compatible_clause_internal(root,
1517 : 45 : (Node *) (nt->arg),
1518 : : relid, attnums,
1519 : : exprs, leakproof);
1520 : :
1521 : : /* Otherwise we have Expr IS NULL. */
1625 tomas.vondra@postgre 1522 : 27 : *exprs = lappend(*exprs, nt->arg);
1523 : 27 : return true;
1524 : : }
1525 : :
1526 : : /*
1527 : : * Treat any other expressions as bare expressions to be matched against
1528 : : * expressions in statistics objects.
1529 : : */
1530 : 3 : *exprs = lappend(*exprs, clause);
1531 : 3 : return true;
1532 : : }
1533 : :
1534 : : /*
1535 : : * statext_is_compatible_clause
1536 : : * Determines if the clause is compatible with MCV lists.
1537 : : *
1538 : : * See statext_is_compatible_clause_internal, above, for the basic rules.
1539 : : * This layer deals with RestrictInfo superstructure and applies permissions
1540 : : * checks to verify that it's okay to examine all mentioned Vars.
1541 : : *
1542 : : * Arguments:
1543 : : * clause: clause to be inspected (in RestrictInfo form)
1544 : : * relid: rel that all Vars in clause must belong to
1545 : : * *attnums: input/output parameter collecting attribute numbers of all
1546 : : * mentioned Vars. Note that we do not offset the attribute numbers,
1547 : : * so we can't cope with system columns.
1548 : : * *exprs: input/output parameter collecting primitive subclauses within
1549 : : * the clause tree
1550 : : *
1551 : : * Returns false if there is something we definitively can't handle.
1552 : : * On true return, we can proceed to match the *exprs against statistics.
1553 : : */
1554 : : static bool
2267 dean.a.rasheed@gmail 1555 : 954 : statext_is_compatible_clause(PlannerInfo *root, Node *clause, Index relid,
1556 : : Bitmapset **attnums, List **exprs)
1557 : : {
1558 : : RestrictInfo *rinfo;
1559 : : int clause_relid;
1560 : : bool leakproof;
1561 : :
1562 : : /*
1563 : : * Special-case handling for bare BoolExpr AND clauses, because the
1564 : : * restrictinfo machinery doesn't build RestrictInfos on top of AND
1565 : : * clauses.
1566 : : */
1733 1567 [ + + ]: 954 : if (is_andclause(clause))
1568 : : {
1569 : 24 : BoolExpr *expr = (BoolExpr *) clause;
1570 : : ListCell *lc;
1571 : :
1572 : : /*
1573 : : * Check that each sub-clause is compatible. We expect these to be
1574 : : * RestrictInfos.
1575 : : */
1576 [ + - + + : 81 : foreach(lc, expr->args)
+ + ]
1577 : : {
1578 [ - + ]: 57 : if (!statext_is_compatible_clause(root, (Node *) lfirst(lc),
1579 : : relid, attnums, exprs))
1733 dean.a.rasheed@gmail 1580 :UBC 0 : return false;
1581 : : }
1582 : :
1733 dean.a.rasheed@gmail 1583 :CBC 24 : return true;
1584 : : }
1585 : :
1586 : : /* Otherwise it must be a RestrictInfo. */
1128 tgl@sss.pgh.pa.us 1587 [ - + ]: 930 : if (!IsA(clause, RestrictInfo))
2355 tomas.vondra@postgre 1588 :UBC 0 : return false;
1128 tgl@sss.pgh.pa.us 1589 :CBC 930 : rinfo = (RestrictInfo *) clause;
1590 : :
1591 : : /* Pseudoconstants are not really interesting here. */
2355 tomas.vondra@postgre 1592 [ + + ]: 930 : if (rinfo->pseudoconstant)
1593 : 3 : return false;
1594 : :
1595 : : /* Clauses referencing other varnos are incompatible. */
1625 1596 [ + - ]: 927 : if (!bms_get_singleton_member(rinfo->clause_relids, &clause_relid) ||
1597 [ - + ]: 927 : clause_relid != relid)
2355 tomas.vondra@postgre 1598 :UBC 0 : return false;
1599 : :
1600 : : /*
1601 : : * Check the clause, determine what attributes it references, and whether
1602 : : * it includes any non-leakproof operators.
1603 : : */
26 dean.a.rasheed@gmail 1604 :CBC 927 : leakproof = true;
2267 1605 [ + + ]: 927 : if (!statext_is_compatible_clause_internal(root, (Node *) rinfo->clause,
1606 : : relid, attnums, exprs,
1607 : : &leakproof))
1608 : 3 : return false;
1609 : :
1610 : : /*
1611 : : * If the clause includes any non-leakproof operators, check that the user
1612 : : * has permission to read all required attributes, otherwise the operators
1613 : : * might reveal values from the MCV list that the user doesn't have
1614 : : * permission to see. We require all rows to be selectable --- there must
1615 : : * be no securityQuals from security barrier views or RLS policies. See
1616 : : * similar code in examine_variable(), examine_simple_variable(), and
1617 : : * statistic_proc_security_check().
1618 : : *
1619 : : * Note that for an inheritance child, the permission checks are performed
1620 : : * on the inheritance root parent, and whole-table select privilege on the
1621 : : * parent doesn't guarantee that the user could read all columns of the
1622 : : * child. Therefore we must check all referenced columns.
1623 : : */
26 1624 [ + + ]: 924 : if (!leakproof)
1625 : : {
1609 tgl@sss.pgh.pa.us 1626 : 126 : Bitmapset *clause_attnums = NULL;
1128 1627 : 126 : int attnum = -1;
1628 : :
1629 : : /*
1630 : : * We have to check per-column privileges. *attnums has the attnums
1631 : : * for individual Vars we saw, but there may also be Vars within
1632 : : * subexpressions in *exprs. We can use pull_varattnos() to extract
1633 : : * those, but there's an impedance mismatch: attnums returned by
1634 : : * pull_varattnos() are offset by FirstLowInvalidHeapAttributeNumber,
1635 : : * while attnums within *attnums aren't. Convert *attnums to the
1636 : : * offset style so we can combine the results.
1637 : : */
1638 [ + + ]: 246 : while ((attnum = bms_next_member(*attnums, attnum)) >= 0)
1639 : : {
1640 : 120 : clause_attnums =
1641 : 120 : bms_add_member(clause_attnums,
1642 : : attnum - FirstLowInvalidHeapAttributeNumber);
1643 : : }
1644 : :
1645 : : /* Now merge attnums from *exprs into clause_attnums */
1646 [ + + ]: 126 : if (*exprs != NIL)
1647 : 24 : pull_varattnos((Node *) *exprs, relid, &clause_attnums);
1648 : :
1649 : : /* Must have permission to read all rows from these columns */
26 dean.a.rasheed@gmail 1650 [ + + ]: 126 : if (!all_rows_selectable(root, relid, clause_attnums))
1651 : 114 : return false;
1652 : : }
1653 : :
1654 : : /* If we reach here, the clause is OK */
2267 1655 : 810 : return true;
1656 : : }
1657 : :
1658 : : /*
1659 : : * statext_mcv_clauselist_selectivity
1660 : : * Estimate clauses using the best multi-column statistics.
1661 : : *
1662 : : * Applies available extended (multi-column) statistics on a table. There may
1663 : : * be multiple applicable statistics (with respect to the clauses), in which
1664 : : * case we use greedy approach. In each round we select the best statistic on
1665 : : * a table (measured by the number of attributes extracted from the clauses
1666 : : * and covered by it), and compute the selectivity for the supplied clauses.
1667 : : * We repeat this process with the remaining clauses (if any), until none of
1668 : : * the available statistics can be used.
1669 : : *
1670 : : * One of the main challenges with using MCV lists is how to extrapolate the
1671 : : * estimate to the data not covered by the MCV list. To do that, we compute
1672 : : * not only the "MCV selectivity" (selectivities for MCV items matching the
1673 : : * supplied clauses), but also the following related selectivities:
1674 : : *
1675 : : * - simple selectivity: Computed without extended statistics, i.e. as if the
1676 : : * columns/clauses were independent.
1677 : : *
1678 : : * - base selectivity: Similar to simple selectivity, but is computed using
1679 : : * the extended statistic by adding up the base frequencies (that we compute
1680 : : * and store for each MCV item) of matching MCV items.
1681 : : *
1682 : : * - total selectivity: Selectivity covered by the whole MCV list.
1683 : : *
1684 : : * These are passed to mcv_combine_selectivities() which combines them to
1685 : : * produce a selectivity estimate that makes use of both per-column statistics
1686 : : * and the multi-column MCV statistics.
1687 : : *
1688 : : * 'estimatedclauses' is an input/output parameter. We set bits for the
1689 : : * 0-based 'clauses' indexes we estimate for and also skip clause items that
1690 : : * already have a bit set.
1691 : : */
1692 : : static Selectivity
2355 tomas.vondra@postgre 1693 : 1296 : statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid,
1694 : : JoinType jointype, SpecialJoinInfo *sjinfo,
1695 : : RelOptInfo *rel, Bitmapset **estimatedclauses,
1696 : : bool is_or)
1697 : : {
1698 : : ListCell *l;
1699 : : Bitmapset **list_attnums; /* attnums extracted from the clause */
1700 : : List **list_exprs; /* expressions matched to any statistic */
1701 : : int listidx;
1738 dean.a.rasheed@gmail 1702 [ + + ]: 1296 : Selectivity sel = (is_or) ? 0.0 : 1.0;
1330 tomas.vondra@postgre 1703 [ + - ]: 1296 : RangeTblEntry *rte = planner_rt_fetch(rel->relid, root);
1704 : :
1705 : : /* check if there's any stats that might be useful for us. */
2355 1706 [ + + ]: 1296 : if (!has_stats_of_kind(rel->statlist, STATS_EXT_MCV))
1738 dean.a.rasheed@gmail 1707 : 924 : return sel;
1708 : :
2355 tomas.vondra@postgre 1709 : 372 : list_attnums = (Bitmapset **) palloc(sizeof(Bitmapset *) *
1710 : 372 : list_length(clauses));
1711 : :
1712 : : /* expressions extracted from complex expressions */
1625 1713 : 372 : list_exprs = (List **) palloc(sizeof(Node *) * list_length(clauses));
1714 : :
1715 : : /*
1716 : : * Pre-process the clauses list to extract the attnums and expressions
1717 : : * seen in each item. We need to determine if there are any clauses which
1718 : : * will be useful for selectivity estimations with extended stats. Along
1719 : : * the way we'll record all of the attnums and expressions for each clause
1720 : : * in lists which we'll reference later so we don't need to repeat the
1721 : : * same work again.
1722 : : *
1723 : : * We also skip clauses that we already estimated using different types of
1724 : : * statistics (we treat them as incompatible).
1725 : : */
2355 1726 : 372 : listidx = 0;
1727 [ + - + + : 1269 : foreach(l, clauses)
+ + ]
1728 : : {
1729 : 897 : Node *clause = (Node *) lfirst(l);
1730 : 897 : Bitmapset *attnums = NULL;
1625 1731 : 897 : List *exprs = NIL;
1732 : :
2355 1733 [ + - + + ]: 1794 : if (!bms_is_member(listidx, *estimatedclauses) &&
1625 1734 : 897 : statext_is_compatible_clause(root, clause, rel->relid, &attnums, &exprs))
1735 : : {
2355 1736 : 777 : list_attnums[listidx] = attnums;
1625 1737 : 777 : list_exprs[listidx] = exprs;
1738 : : }
1739 : : else
1740 : : {
2355 1741 : 120 : list_attnums[listidx] = NULL;
1625 1742 : 120 : list_exprs[listidx] = NIL;
1743 : : }
1744 : :
2355 1745 : 897 : listidx++;
1746 : : }
1747 : :
1748 : : /* apply as many extended statistics as possible */
1749 : : while (true)
2063 1750 : 303 : {
1751 : : StatisticExtInfo *stat;
1752 : : List *stat_clauses;
1753 : : Bitmapset *simple_clauses;
1754 : :
1755 : : /* find the best suited statistics object for these attnums */
1329 1756 : 675 : stat = choose_best_statistics(rel->statlist, STATS_EXT_MCV, rte->inh,
1757 : : list_attnums, list_exprs,
1758 : : list_length(clauses));
1759 : :
1760 : : /*
1761 : : * if no (additional) matching stats could be found then we've nothing
1762 : : * to do
1763 : : */
2063 1764 [ + + ]: 675 : if (!stat)
1765 : 372 : break;
1766 : :
1767 : : /* Ensure choose_best_statistics produced an expected stats type. */
1768 [ - + ]: 303 : Assert(stat->kind == STATS_EXT_MCV);
1769 : :
1770 : : /* now filter the clauses to be estimated using the selected MCV */
1771 : 303 : stat_clauses = NIL;
1772 : :
1773 : : /* record which clauses are simple (single column or expression) */
1738 dean.a.rasheed@gmail 1774 : 303 : simple_clauses = NULL;
1775 : :
1614 tomas.vondra@postgre 1776 : 303 : listidx = -1;
2063 1777 [ + - + + : 1056 : foreach(l, clauses)
+ + ]
1778 : : {
1779 : : /* Increment the index before we decide if to skip the clause. */
1614 1780 : 753 : listidx++;
1781 : :
1782 : : /*
1783 : : * Ignore clauses from which we did not extract any attnums or
1784 : : * expressions (this needs to be consistent with what we do in
1785 : : * choose_best_statistics).
1786 : : *
1787 : : * This also eliminates already estimated clauses - both those
1788 : : * estimated before and during applying extended statistics.
1789 : : *
1790 : : * XXX This check is needed because both bms_is_subset and
1791 : : * stat_covers_expressions return true for empty attnums and
1792 : : * expressions.
1793 : : */
1794 [ + + + + ]: 753 : if (!list_attnums[listidx] && !list_exprs[listidx])
1795 : 18 : continue;
1796 : :
1797 : : /*
1798 : : * The clause was not estimated yet, and we've extracted either
1799 : : * attnums or expressions from it. Ignore it if it's not fully
1800 : : * covered by the chosen statistics object.
1801 : : *
1802 : : * We need to check both attributes and expressions, and reject if
1803 : : * either is not covered.
1804 : : */
1805 [ + + ]: 735 : if (!bms_is_subset(list_attnums[listidx], stat->keys) ||
1806 [ + + ]: 705 : !stat_covers_expressions(stat, list_exprs[listidx], NULL))
1807 : 33 : continue;
1808 : :
1809 : : /*
1810 : : * Now we know the clause is compatible (we have either attnums or
1811 : : * expressions extracted from it), and was not estimated yet.
1812 : : */
1813 : :
1814 : : /* record simple clauses (single column or expression) */
1815 [ + + - + ]: 825 : if ((list_attnums[listidx] == NULL &&
1816 : 123 : list_length(list_exprs[listidx]) == 1) ||
1817 [ + - + + ]: 1158 : (list_exprs[listidx] == NIL &&
1818 : 579 : bms_membership(list_attnums[listidx]) == BMS_SINGLETON))
1819 : 672 : simple_clauses = bms_add_member(simple_clauses,
1820 : : list_length(stat_clauses));
1821 : :
1822 : : /* add clause to list and mark it as estimated */
1823 : 702 : stat_clauses = lappend(stat_clauses, (Node *) lfirst(l));
1824 : 702 : *estimatedclauses = bms_add_member(*estimatedclauses, listidx);
1825 : :
1826 : : /*
1827 : : * Reset the pointers, so that choose_best_statistics knows this
1828 : : * clause was estimated and does not consider it again.
1829 : : */
1830 : 702 : bms_free(list_attnums[listidx]);
1831 : 702 : list_attnums[listidx] = NULL;
1832 : :
1833 : 702 : list_free(list_exprs[listidx]);
1834 : 702 : list_exprs[listidx] = NULL;
1835 : : }
1836 : :
1738 dean.a.rasheed@gmail 1837 [ + + ]: 303 : if (is_or)
1838 : : {
1839 : 48 : bool *or_matches = NULL;
1733 1840 : 48 : Selectivity simple_or_sel = 0.0,
1841 : 48 : stat_sel = 0.0;
1842 : : MCVList *mcv_list;
1843 : :
1844 : : /* Load the MCV list stored in the statistics object */
1329 tomas.vondra@postgre 1845 : 48 : mcv_list = statext_mcv_load(stat->statOid, rte->inh);
1846 : :
1847 : : /*
1848 : : * Compute the selectivity of the ORed list of clauses covered by
1849 : : * this statistics object by estimating each in turn and combining
1850 : : * them using the formula P(A OR B) = P(A) + P(B) - P(A AND B).
1851 : : * This allows us to use the multivariate MCV stats to better
1852 : : * estimate the individual terms and their overlap.
1853 : : *
1854 : : * Each time we iterate this formula, the clause "A" above is
1855 : : * equal to all the clauses processed so far, combined with "OR".
1856 : : */
1738 dean.a.rasheed@gmail 1857 : 48 : listidx = 0;
1858 [ + - + + : 168 : foreach(l, stat_clauses)
+ + ]
1859 : : {
1860 : 120 : Node *clause = (Node *) lfirst(l);
1861 : : Selectivity simple_sel,
1862 : : overlap_simple_sel,
1863 : : mcv_sel,
1864 : : mcv_basesel,
1865 : : overlap_mcvsel,
1866 : : overlap_basesel,
1867 : : mcv_totalsel,
1868 : : clause_sel,
1869 : : overlap_sel;
1870 : :
1871 : : /*
1872 : : * "Simple" selectivity of the next clause and its overlap
1873 : : * with any of the previous clauses. These are our initial
1874 : : * estimates of P(B) and P(A AND B), assuming independence of
1875 : : * columns/clauses.
1876 : : */
1877 : 120 : simple_sel = clause_selectivity_ext(root, clause, varRelid,
1878 : : jointype, sjinfo, false);
1879 : :
1880 : 120 : overlap_simple_sel = simple_or_sel * simple_sel;
1881 : :
1882 : : /*
1883 : : * New "simple" selectivity of all clauses seen so far,
1884 : : * assuming independence.
1885 : : */
1886 : 120 : simple_or_sel += simple_sel - overlap_simple_sel;
1887 [ - + - + ]: 120 : CLAMP_PROBABILITY(simple_or_sel);
1888 : :
1889 : : /*
1890 : : * Multi-column estimate of this clause using MCV statistics,
1891 : : * along with base and total selectivities, and corresponding
1892 : : * selectivities for the overlap term P(A AND B).
1893 : : */
1894 : 120 : mcv_sel = mcv_clause_selectivity_or(root, stat, mcv_list,
1895 : : clause, &or_matches,
1896 : : &mcv_basesel,
1897 : : &overlap_mcvsel,
1898 : : &overlap_basesel,
1899 : : &mcv_totalsel);
1900 : :
1901 : : /*
1902 : : * Combine the simple and multi-column estimates.
1903 : : *
1904 : : * If this clause is a simple single-column clause, then we
1905 : : * just use the simple selectivity estimate for it, since the
1906 : : * multi-column statistics are unlikely to improve on that
1907 : : * (and in fact could make it worse). For the overlap, we
1908 : : * always make use of the multi-column statistics.
1909 : : */
1910 [ + + ]: 120 : if (bms_is_member(listidx, simple_clauses))
1911 : 96 : clause_sel = simple_sel;
1912 : : else
1913 : 24 : clause_sel = mcv_combine_selectivities(simple_sel,
1914 : : mcv_sel,
1915 : : mcv_basesel,
1916 : : mcv_totalsel);
1917 : :
1918 : 120 : overlap_sel = mcv_combine_selectivities(overlap_simple_sel,
1919 : : overlap_mcvsel,
1920 : : overlap_basesel,
1921 : : mcv_totalsel);
1922 : :
1923 : : /* Factor these into the result for this statistics object */
1733 1924 : 120 : stat_sel += clause_sel - overlap_sel;
1925 [ - + - + ]: 120 : CLAMP_PROBABILITY(stat_sel);
1926 : :
1738 1927 : 120 : listidx++;
1928 : : }
1929 : :
1930 : : /*
1931 : : * Factor the result for this statistics object into the overall
1932 : : * result. We treat the results from each separate statistics
1933 : : * object as independent of one another.
1934 : : */
1733 1935 : 48 : sel = sel + stat_sel - sel * stat_sel;
1936 : : }
1937 : : else /* Implicitly-ANDed list of clauses */
1938 : : {
1939 : : Selectivity simple_sel,
1940 : : mcv_sel,
1941 : : mcv_basesel,
1942 : : mcv_totalsel,
1943 : : stat_sel;
1944 : :
1945 : : /*
1946 : : * "Simple" selectivity, i.e. without any extended statistics,
1947 : : * essentially assuming independence of the columns/clauses.
1948 : : */
1738 1949 : 255 : simple_sel = clauselist_selectivity_ext(root, stat_clauses,
1950 : : varRelid, jointype,
1951 : : sjinfo, false);
1952 : :
1953 : : /*
1954 : : * Multi-column estimate using MCV statistics, along with base and
1955 : : * total selectivities.
1956 : : */
1957 : 255 : mcv_sel = mcv_clauselist_selectivity(root, stat, stat_clauses,
1958 : : varRelid, jointype, sjinfo,
1959 : : rel, &mcv_basesel,
1960 : : &mcv_totalsel);
1961 : :
1962 : : /* Combine the simple and multi-column estimates. */
1963 : 255 : stat_sel = mcv_combine_selectivities(simple_sel,
1964 : : mcv_sel,
1965 : : mcv_basesel,
1966 : : mcv_totalsel);
1967 : :
1968 : : /* Factor this into the overall result */
1969 : 255 : sel *= stat_sel;
1970 : : }
1971 : : }
1972 : :
2355 tomas.vondra@postgre 1973 : 372 : return sel;
1974 : : }
1975 : :
1976 : : /*
1977 : : * statext_clauselist_selectivity
1978 : : * Estimate clauses using the best multi-column statistics.
1979 : : */
1980 : : Selectivity
1981 : 1296 : statext_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid,
1982 : : JoinType jointype, SpecialJoinInfo *sjinfo,
1983 : : RelOptInfo *rel, Bitmapset **estimatedclauses,
1984 : : bool is_or)
1985 : : {
1986 : : Selectivity sel;
1987 : :
1988 : : /* First, try estimating clauses using a multivariate MCV list. */
1989 : 1296 : sel = statext_mcv_clauselist_selectivity(root, clauses, varRelid, jointype,
1990 : : sjinfo, rel, estimatedclauses, is_or);
1991 : :
1992 : : /*
1993 : : * Functional dependencies only work for clauses connected by AND, so for
1994 : : * OR clauses we're done.
1995 : : */
1738 dean.a.rasheed@gmail 1996 [ + + ]: 1296 : if (is_or)
1997 : 78 : return sel;
1998 : :
1999 : : /*
2000 : : * Then, apply functional dependencies on the remaining clauses by calling
2001 : : * dependencies_clauselist_selectivity. Pass 'estimatedclauses' so the
2002 : : * function can properly skip clauses already estimated above.
2003 : : *
2004 : : * The reasoning for applying dependencies last is that the more complex
2005 : : * stats can track more complex correlations between the attributes, and
2006 : : * so may be considered more reliable.
2007 : : *
2008 : : * For example, MCV list can give us an exact selectivity for values in
2009 : : * two columns, while functional dependencies can only provide information
2010 : : * about the overall strength of the dependency.
2011 : : */
2355 tomas.vondra@postgre 2012 : 1218 : sel *= dependencies_clauselist_selectivity(root, clauses, varRelid,
2013 : : jointype, sjinfo, rel,
2014 : : estimatedclauses);
2015 : :
2016 : 1218 : return sel;
2017 : : }
2018 : :
2019 : : /*
2020 : : * examine_opclause_args
2021 : : * Split an operator expression's arguments into Expr and Const parts.
2022 : : *
2023 : : * Attempts to match the arguments to either (Expr op Const) or (Const op
2024 : : * Expr), possibly with a RelabelType on top. When the expression matches this
2025 : : * form, returns true, otherwise returns false.
2026 : : *
2027 : : * Optionally returns pointers to the extracted Expr/Const nodes, when passed
2028 : : * non-null pointers (exprp, cstp and expronleftp). The expronleftp flag
2029 : : * specifies on which side of the operator we found the expression node.
2030 : : */
2031 : : bool
1625 2032 : 1509 : examine_opclause_args(List *args, Node **exprp, Const **cstp,
2033 : : bool *expronleftp)
2034 : : {
2035 : : Node *expr;
2036 : : Const *cst;
2037 : : bool expronleft;
2038 : : Node *leftop,
2039 : : *rightop;
2040 : :
2041 : : /* enforced by statext_is_compatible_clause_internal */
2002 2042 [ - + ]: 1509 : Assert(list_length(args) == 2);
2043 : :
2044 : 1509 : leftop = linitial(args);
2045 : 1509 : rightop = lsecond(args);
2046 : :
2047 : : /* strip RelabelType from either side of the expression */
2247 2048 [ + + ]: 1509 : if (IsA(leftop, RelabelType))
2049 : 162 : leftop = (Node *) ((RelabelType *) leftop)->arg;
2050 : :
2051 [ + + ]: 1509 : if (IsA(rightop, RelabelType))
2052 : 30 : rightop = (Node *) ((RelabelType *) rightop)->arg;
2053 : :
1625 2054 [ + + ]: 1509 : if (IsA(rightop, Const))
2055 : : {
2056 : 1428 : expr = (Node *) leftop;
2247 2057 : 1428 : cst = (Const *) rightop;
1625 2058 : 1428 : expronleft = true;
2059 : : }
2060 [ + - ]: 81 : else if (IsA(leftop, Const))
2061 : : {
2062 : 81 : expr = (Node *) rightop;
2247 2063 : 81 : cst = (Const *) leftop;
1625 2064 : 81 : expronleft = false;
2065 : : }
2066 : : else
2247 tomas.vondra@postgre 2067 :UBC 0 : return false;
2068 : :
2069 : : /* return pointers to the extracted parts if requested */
1625 tomas.vondra@postgre 2070 [ + - ]:CBC 1509 : if (exprp)
2071 : 1509 : *exprp = expr;
2072 : :
2247 2073 [ + + ]: 1509 : if (cstp)
2074 : 669 : *cstp = cst;
2075 : :
1625 2076 [ + + ]: 1509 : if (expronleftp)
2077 : 813 : *expronleftp = expronleft;
2078 : :
2247 2079 : 1509 : return true;
2080 : : }
2081 : :
2082 : :
2083 : : /*
2084 : : * Compute statistics about expressions of a relation.
2085 : : */
2086 : : static void
253 drowley@postgresql.o 2087 : 78 : compute_expr_stats(Relation onerel, AnlExprData *exprdata, int nexprs,
2088 : : HeapTuple *rows, int numrows)
2089 : : {
2090 : : MemoryContext expr_context,
2091 : : old_context;
2092 : : int ind,
2093 : : i;
2094 : :
1625 tomas.vondra@postgre 2095 : 78 : expr_context = AllocSetContextCreate(CurrentMemoryContext,
2096 : : "Analyze Expression",
2097 : : ALLOCSET_DEFAULT_SIZES);
2098 : 78 : old_context = MemoryContextSwitchTo(expr_context);
2099 : :
2100 [ + + ]: 228 : for (ind = 0; ind < nexprs; ind++)
2101 : : {
2102 : 150 : AnlExprData *thisdata = &exprdata[ind];
2103 : 150 : VacAttrStats *stats = thisdata->vacattrstat;
2104 : 150 : Node *expr = thisdata->expr;
2105 : : TupleTableSlot *slot;
2106 : : EState *estate;
2107 : : ExprContext *econtext;
2108 : : Datum *exprvals;
2109 : : bool *exprnulls;
2110 : : ExprState *exprstate;
2111 : : int tcnt;
2112 : :
2113 : : /* Are we still in the main context? */
2114 [ - + ]: 150 : Assert(CurrentMemoryContext == expr_context);
2115 : :
2116 : : /*
2117 : : * Need an EState for evaluation of expressions. Create it in the
2118 : : * per-expression context to be sure it gets cleaned up at the bottom
2119 : : * of the loop.
2120 : : */
2121 : 150 : estate = CreateExecutorState();
2122 [ - + ]: 150 : econtext = GetPerTupleExprContext(estate);
2123 : :
2124 : : /* Set up expression evaluation state */
2125 : 150 : exprstate = ExecPrepareExpr((Expr *) expr, estate);
2126 : :
2127 : : /* Need a slot to hold the current heap tuple, too */
2128 : 150 : slot = MakeSingleTupleTableSlot(RelationGetDescr(onerel),
2129 : : &TTSOpsHeapTuple);
2130 : :
2131 : : /* Arrange for econtext's scan tuple to be the tuple under test */
2132 : 150 : econtext->ecxt_scantuple = slot;
2133 : :
2134 : : /* Compute and save expression values */
2135 : 150 : exprvals = (Datum *) palloc(numrows * sizeof(Datum));
2136 : 150 : exprnulls = (bool *) palloc(numrows * sizeof(bool));
2137 : :
2138 : 150 : tcnt = 0;
2139 [ + + ]: 199965 : for (i = 0; i < numrows; i++)
2140 : : {
2141 : : Datum datum;
2142 : : bool isnull;
2143 : :
2144 : : /*
2145 : : * Reset the per-tuple context each time, to reclaim any cruft
2146 : : * left behind by evaluating the statistics expressions.
2147 : : */
2148 : 199815 : ResetExprContext(econtext);
2149 : :
2150 : : /* Set up for expression evaluation */
2151 : 199815 : ExecStoreHeapTuple(rows[i], slot, false);
2152 : :
2153 : : /*
2154 : : * Evaluate the expression. We do this in the per-tuple context so
2155 : : * as not to leak memory, and then copy the result into the
2156 : : * context created at the beginning of this function.
2157 : : */
2158 : 199815 : datum = ExecEvalExprSwitchContext(exprstate,
2159 [ + - ]: 199815 : GetPerTupleExprContext(estate),
2160 : : &isnull);
2161 [ - + ]: 199815 : if (isnull)
2162 : : {
1625 tomas.vondra@postgre 2163 :UBC 0 : exprvals[tcnt] = (Datum) 0;
2164 : 0 : exprnulls[tcnt] = true;
2165 : : }
2166 : : else
2167 : : {
2168 : : /* Make sure we copy the data into the context. */
1625 tomas.vondra@postgre 2169 [ - + ]:CBC 199815 : Assert(CurrentMemoryContext == expr_context);
2170 : :
2171 : 399630 : exprvals[tcnt] = datumCopy(datum,
2172 : 199815 : stats->attrtype->typbyval,
2173 : 199815 : stats->attrtype->typlen);
2174 : 199815 : exprnulls[tcnt] = false;
2175 : : }
2176 : :
2177 : 199815 : tcnt++;
2178 : : }
2179 : :
2180 : : /*
2181 : : * Now we can compute the statistics for the expression columns.
2182 : : *
2183 : : * XXX Unlike compute_index_stats we don't need to switch and reset
2184 : : * memory contexts here, because we're only computing stats for a
2185 : : * single expression (and not iterating over many indexes), so we just
2186 : : * do it in expr_context. Note that compute_stats copies the result
2187 : : * into stats->anl_context, so it does not disappear.
2188 : : */
2189 [ + - ]: 150 : if (tcnt > 0)
2190 : : {
2191 : : AttributeOpts *aopt =
796 peter@eisentraut.org 2192 : 150 : get_attribute_options(onerel->rd_id, stats->tupattnum);
2193 : :
1625 tomas.vondra@postgre 2194 : 150 : stats->exprvals = exprvals;
2195 : 150 : stats->exprnulls = exprnulls;
2196 : 150 : stats->rowstride = 1;
2197 : 150 : stats->compute_stats(stats,
2198 : : expr_fetch_func,
2199 : : tcnt,
2200 : : tcnt);
2201 : :
2202 : : /*
2203 : : * If the n_distinct option is specified, it overrides the above
2204 : : * computation.
2205 : : */
2206 [ - + - - ]: 150 : if (aopt != NULL && aopt->n_distinct != 0.0)
1625 tomas.vondra@postgre 2207 :UBC 0 : stats->stadistinct = aopt->n_distinct;
2208 : : }
2209 : :
2210 : : /* And clean up */
1625 tomas.vondra@postgre 2211 :CBC 150 : MemoryContextSwitchTo(expr_context);
2212 : :
2213 : 150 : ExecDropSingleTupleTableSlot(slot);
2214 : 150 : FreeExecutorState(estate);
661 nathan@postgresql.or 2215 : 150 : MemoryContextReset(expr_context);
2216 : : }
2217 : :
1625 tomas.vondra@postgre 2218 : 78 : MemoryContextSwitchTo(old_context);
2219 : 78 : MemoryContextDelete(expr_context);
2220 : 78 : }
2221 : :
2222 : :
2223 : : /*
2224 : : * Fetch function for analyzing statistics object expressions.
2225 : : *
2226 : : * We have not bothered to construct tuples from the data, instead the data
2227 : : * is just in Datum arrays.
2228 : : */
2229 : : static Datum
2230 : 199815 : expr_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull)
2231 : : {
2232 : : int i;
2233 : :
2234 : : /* exprvals and exprnulls are already offset for proper column */
2235 : 199815 : i = rownum * stats->rowstride;
2236 : 199815 : *isNull = stats->exprnulls[i];
2237 : 199815 : return stats->exprvals[i];
2238 : : }
2239 : :
2240 : : /*
2241 : : * Build analyze data for a list of expressions. As this is not tied
2242 : : * directly to a relation (table or index), we have to fake some of
2243 : : * the fields in examine_expression().
2244 : : */
2245 : : static AnlExprData *
2246 : 78 : build_expr_data(List *exprs, int stattarget)
2247 : : {
2248 : : int idx;
2249 : 78 : int nexprs = list_length(exprs);
2250 : : AnlExprData *exprdata;
2251 : : ListCell *lc;
2252 : :
2253 : 78 : exprdata = (AnlExprData *) palloc0(nexprs * sizeof(AnlExprData));
2254 : :
2255 : 78 : idx = 0;
2256 [ + - + + : 228 : foreach(lc, exprs)
+ + ]
2257 : : {
2258 : 150 : Node *expr = (Node *) lfirst(lc);
2259 : 150 : AnlExprData *thisdata = &exprdata[idx];
2260 : :
2261 : 150 : thisdata->expr = expr;
2262 : 150 : thisdata->vacattrstat = examine_expression(expr, stattarget);
2263 : 150 : idx++;
2264 : : }
2265 : :
2266 : 78 : return exprdata;
2267 : : }
2268 : :
2269 : : /* form an array of pg_statistic rows (per update_attstats) */
2270 : : static Datum
2271 : 78 : serialize_expr_stats(AnlExprData *exprdata, int nexprs)
2272 : : {
2273 : : int exprno;
2274 : : Oid typOid;
2275 : : Relation sd;
2276 : :
2277 : 78 : ArrayBuildState *astate = NULL;
2278 : :
2279 : 78 : sd = table_open(StatisticRelationId, RowExclusiveLock);
2280 : :
2281 : : /* lookup OID of composite type for pg_statistic */
2282 : 78 : typOid = get_rel_type_id(StatisticRelationId);
2283 [ - + ]: 78 : if (!OidIsValid(typOid))
1625 tomas.vondra@postgre 2284 [ # # ]:UBC 0 : ereport(ERROR,
2285 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2286 : : errmsg("relation \"%s\" does not have a composite type",
2287 : : "pg_statistic")));
2288 : :
1625 tomas.vondra@postgre 2289 [ + + ]:CBC 228 : for (exprno = 0; exprno < nexprs; exprno++)
2290 : : {
2291 : : int i,
2292 : : k;
2293 : 150 : VacAttrStats *stats = exprdata[exprno].vacattrstat;
2294 : :
2295 : : Datum values[Natts_pg_statistic];
2296 : : bool nulls[Natts_pg_statistic];
2297 : : HeapTuple stup;
2298 : :
2299 [ - + ]: 150 : if (!stats->stats_valid)
2300 : : {
1625 tomas.vondra@postgre 2301 :UBC 0 : astate = accumArrayResult(astate,
2302 : : (Datum) 0,
2303 : : true,
2304 : : typOid,
2305 : : CurrentMemoryContext);
2306 : 0 : continue;
2307 : : }
2308 : :
2309 : : /*
2310 : : * Construct a new pg_statistic tuple
2311 : : */
1625 tomas.vondra@postgre 2312 [ + + ]:CBC 4800 : for (i = 0; i < Natts_pg_statistic; ++i)
2313 : : {
2314 : 4650 : nulls[i] = false;
2315 : : }
2316 : :
2317 : 150 : values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(InvalidOid);
2318 : 150 : values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(InvalidAttrNumber);
2319 : 150 : values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(false);
2320 : 150 : values[Anum_pg_statistic_stanullfrac - 1] = Float4GetDatum(stats->stanullfrac);
2321 : 150 : values[Anum_pg_statistic_stawidth - 1] = Int32GetDatum(stats->stawidth);
2322 : 150 : values[Anum_pg_statistic_stadistinct - 1] = Float4GetDatum(stats->stadistinct);
2323 : 150 : i = Anum_pg_statistic_stakind1 - 1;
2324 [ + + ]: 900 : for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2325 : : {
2326 : 750 : values[i++] = Int16GetDatum(stats->stakind[k]); /* stakindN */
2327 : : }
2328 : 150 : i = Anum_pg_statistic_staop1 - 1;
2329 [ + + ]: 900 : for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2330 : : {
2331 : 750 : values[i++] = ObjectIdGetDatum(stats->staop[k]); /* staopN */
2332 : : }
2333 : 150 : i = Anum_pg_statistic_stacoll1 - 1;
2334 [ + + ]: 900 : for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2335 : : {
2336 : 750 : values[i++] = ObjectIdGetDatum(stats->stacoll[k]); /* stacollN */
2337 : : }
2338 : 150 : i = Anum_pg_statistic_stanumbers1 - 1;
2339 [ + + ]: 900 : for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2340 : : {
2341 : 750 : int nnum = stats->numnumbers[k];
2342 : :
2343 [ + + ]: 750 : if (nnum > 0)
2344 : : {
2345 : : int n;
2346 : 294 : Datum *numdatums = (Datum *) palloc(nnum * sizeof(Datum));
2347 : : ArrayType *arry;
2348 : :
2349 [ + + ]: 2493 : for (n = 0; n < nnum; n++)
2350 : 2199 : numdatums[n] = Float4GetDatum(stats->stanumbers[k][n]);
1163 peter@eisentraut.org 2351 : 294 : arry = construct_array_builtin(numdatums, nnum, FLOAT4OID);
1625 tomas.vondra@postgre 2352 : 294 : values[i++] = PointerGetDatum(arry); /* stanumbersN */
2353 : : }
2354 : : else
2355 : : {
2356 : 456 : nulls[i] = true;
2357 : 456 : values[i++] = (Datum) 0;
2358 : : }
2359 : : }
2360 : 150 : i = Anum_pg_statistic_stavalues1 - 1;
2361 [ + + ]: 900 : for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
2362 : : {
2363 [ + + ]: 750 : if (stats->numvalues[k] > 0)
2364 : : {
2365 : : ArrayType *arry;
2366 : :
2367 : 159 : arry = construct_array(stats->stavalues[k],
2368 : : stats->numvalues[k],
2369 : : stats->statypid[k],
2370 : 159 : stats->statyplen[k],
2371 : 159 : stats->statypbyval[k],
2372 : 159 : stats->statypalign[k]);
2373 : 159 : values[i++] = PointerGetDatum(arry); /* stavaluesN */
2374 : : }
2375 : : else
2376 : : {
2377 : 591 : nulls[i] = true;
2378 : 591 : values[i++] = (Datum) 0;
2379 : : }
2380 : : }
2381 : :
2382 : 150 : stup = heap_form_tuple(RelationGetDescr(sd), values, nulls);
2383 : :
2384 : 150 : astate = accumArrayResult(astate,
2385 : : heap_copy_tuple_as_datum(stup, RelationGetDescr(sd)),
2386 : : false,
2387 : : typOid,
2388 : : CurrentMemoryContext);
2389 : : }
2390 : :
2391 : 78 : table_close(sd, RowExclusiveLock);
2392 : :
2393 : 78 : return makeArrayResult(astate, CurrentMemoryContext);
2394 : : }
2395 : :
2396 : : /*
2397 : : * Loads pg_statistic record from expression statistics for expression
2398 : : * identified by the supplied index.
2399 : : */
2400 : : HeapTuple
1329 2401 : 822 : statext_expressions_load(Oid stxoid, bool inh, int idx)
2402 : : {
2403 : : bool isnull;
2404 : : Datum value;
2405 : : HeapTuple htup;
2406 : : ExpandedArrayHeader *eah;
2407 : : HeapTupleHeader td;
2408 : : HeapTupleData tmptup;
2409 : : HeapTuple tup;
2410 : :
2411 : 822 : htup = SearchSysCache2(STATEXTDATASTXOID,
2412 : : ObjectIdGetDatum(stxoid), BoolGetDatum(inh));
1625 2413 [ - + ]: 822 : if (!HeapTupleIsValid(htup))
1625 tomas.vondra@postgre 2414 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for statistics object %u", stxoid);
2415 : :
1625 tomas.vondra@postgre 2416 :CBC 822 : value = SysCacheGetAttr(STATEXTDATASTXOID, htup,
2417 : : Anum_pg_statistic_ext_data_stxdexpr, &isnull);
2418 [ - + ]: 822 : if (isnull)
1625 tomas.vondra@postgre 2419 [ # # ]:UBC 0 : elog(ERROR,
2420 : : "requested statistics kind \"%c\" is not yet built for statistics object %u",
2421 : : STATS_EXT_EXPRESSIONS, stxoid);
2422 : :
1625 tomas.vondra@postgre 2423 :CBC 822 : eah = DatumGetExpandedArray(value);
2424 : :
2425 : 822 : deconstruct_expanded_array(eah);
2426 : :
2427 : 822 : td = DatumGetHeapTupleHeader(eah->dvalues[idx]);
2428 : :
2429 : : /* Build a temporary HeapTuple control structure */
2430 : 822 : tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1606 2431 : 822 : ItemPointerSetInvalid(&(tmptup.t_self));
2432 : 822 : tmptup.t_tableOid = InvalidOid;
1625 2433 : 822 : tmptup.t_data = td;
2434 : :
2435 : 822 : tup = heap_copytuple(&tmptup);
2436 : :
2437 : 822 : ReleaseSysCache(htup);
2438 : :
2439 : 822 : return tup;
2440 : : }
2441 : :
2442 : : /*
2443 : : * Evaluate the expressions, so that we can use the results to build
2444 : : * all the requested statistics types. This matters especially for
2445 : : * expensive expressions, of course.
2446 : : */
2447 : : static StatsBuildData *
2448 : 199 : make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows,
2449 : : VacAttrStats **stats, int stattarget)
2450 : : {
2451 : : /* evaluated expressions */
2452 : : StatsBuildData *result;
2453 : : char *ptr;
2454 : : Size len;
2455 : :
2456 : : int i;
2457 : : int k;
2458 : : int idx;
2459 : : TupleTableSlot *slot;
2460 : : EState *estate;
2461 : : ExprContext *econtext;
2462 : 199 : List *exprstates = NIL;
2463 : 199 : int nkeys = bms_num_members(stat->columns) + list_length(stat->exprs);
2464 : : ListCell *lc;
2465 : :
2466 : : /* allocate everything as a single chunk, so we can free it easily */
2467 : 199 : len = MAXALIGN(sizeof(StatsBuildData));
2468 : 199 : len += MAXALIGN(sizeof(AttrNumber) * nkeys); /* attnums */
2469 : 199 : len += MAXALIGN(sizeof(VacAttrStats *) * nkeys); /* stats */
2470 : :
2471 : : /* values */
2472 : 199 : len += MAXALIGN(sizeof(Datum *) * nkeys);
2473 : 199 : len += nkeys * MAXALIGN(sizeof(Datum) * numrows);
2474 : :
2475 : : /* nulls */
2476 : 199 : len += MAXALIGN(sizeof(bool *) * nkeys);
2477 : 199 : len += nkeys * MAXALIGN(sizeof(bool) * numrows);
2478 : :
2479 : 199 : ptr = palloc(len);
2480 : :
2481 : : /* set the pointers */
2482 : 199 : result = (StatsBuildData *) ptr;
2483 : 199 : ptr += MAXALIGN(sizeof(StatsBuildData));
2484 : :
2485 : : /* attnums */
2486 : 199 : result->attnums = (AttrNumber *) ptr;
2487 : 199 : ptr += MAXALIGN(sizeof(AttrNumber) * nkeys);
2488 : :
2489 : : /* stats */
2490 : 199 : result->stats = (VacAttrStats **) ptr;
2491 : 199 : ptr += MAXALIGN(sizeof(VacAttrStats *) * nkeys);
2492 : :
2493 : : /* values */
2494 : 199 : result->values = (Datum **) ptr;
2495 : 199 : ptr += MAXALIGN(sizeof(Datum *) * nkeys);
2496 : :
2497 : : /* nulls */
2498 : 199 : result->nulls = (bool **) ptr;
2499 : 199 : ptr += MAXALIGN(sizeof(bool *) * nkeys);
2500 : :
2501 [ + + ]: 702 : for (i = 0; i < nkeys; i++)
2502 : : {
2503 : 503 : result->values[i] = (Datum *) ptr;
2504 : 503 : ptr += MAXALIGN(sizeof(Datum) * numrows);
2505 : :
2506 : 503 : result->nulls[i] = (bool *) ptr;
2507 : 503 : ptr += MAXALIGN(sizeof(bool) * numrows);
2508 : : }
2509 : :
2510 [ - + ]: 199 : Assert((ptr - (char *) result) == len);
2511 : :
2512 : : /* we have it allocated, so let's fill the values */
2513 : 199 : result->nattnums = nkeys;
2514 : 199 : result->numrows = numrows;
2515 : :
2516 : : /* fill the attribute info - first attributes, then expressions */
2517 : 199 : idx = 0;
2518 : 199 : k = -1;
2519 [ + + ]: 552 : while ((k = bms_next_member(stat->columns, k)) >= 0)
2520 : : {
2521 : 353 : result->attnums[idx] = k;
2522 : 353 : result->stats[idx] = stats[idx];
2523 : :
2524 : 353 : idx++;
2525 : : }
2526 : :
2527 : 199 : k = -1;
2528 [ + + + + : 349 : foreach(lc, stat->exprs)
+ + ]
2529 : : {
2530 : 150 : Node *expr = (Node *) lfirst(lc);
2531 : :
2532 : 150 : result->attnums[idx] = k;
2533 : 150 : result->stats[idx] = examine_expression(expr, stattarget);
2534 : :
2535 : 150 : idx++;
2536 : 150 : k--;
2537 : : }
2538 : :
2539 : : /* first extract values for all the regular attributes */
2540 [ + + ]: 411602 : for (i = 0; i < numrows; i++)
2541 : : {
2542 : 411403 : idx = 0;
2543 : 411403 : k = -1;
2544 [ + + ]: 1372491 : while ((k = bms_next_member(stat->columns, k)) >= 0)
2545 : : {
2546 : 1922176 : result->values[idx][i] = heap_getattr(rows[i], k,
2547 : 961088 : result->stats[idx]->tupDesc,
2548 : 961088 : &result->nulls[idx][i]);
2549 : :
2550 : 961088 : idx++;
2551 : : }
2552 : : }
2553 : :
2554 : : /* Need an EState for evaluation expressions. */
2555 : 199 : estate = CreateExecutorState();
2556 [ - + ]: 199 : econtext = GetPerTupleExprContext(estate);
2557 : :
2558 : : /* Need a slot to hold the current heap tuple, too */
2559 : 199 : slot = MakeSingleTupleTableSlot(RelationGetDescr(rel),
2560 : : &TTSOpsHeapTuple);
2561 : :
2562 : : /* Arrange for econtext's scan tuple to be the tuple under test */
2563 : 199 : econtext->ecxt_scantuple = slot;
2564 : :
2565 : : /* Set up expression evaluation state */
2566 : 199 : exprstates = ExecPrepareExprList(stat->exprs, estate);
2567 : :
2568 [ + + ]: 411602 : for (i = 0; i < numrows; i++)
2569 : : {
2570 : : /*
2571 : : * Reset the per-tuple context each time, to reclaim any cruft left
2572 : : * behind by evaluating the statistics object expressions.
2573 : : */
2574 : 411403 : ResetExprContext(econtext);
2575 : :
2576 : : /* Set up for expression evaluation */
2577 : 411403 : ExecStoreHeapTuple(rows[i], slot, false);
2578 : :
2579 : 411403 : idx = bms_num_members(stat->columns);
2580 [ + + + + : 611218 : foreach(lc, exprstates)
+ + ]
2581 : : {
2582 : : Datum datum;
2583 : : bool isnull;
2584 : 199815 : ExprState *exprstate = (ExprState *) lfirst(lc);
2585 : :
2586 : : /*
2587 : : * XXX This probably leaks memory. Maybe we should use
2588 : : * ExecEvalExprSwitchContext but then we need to copy the result
2589 : : * somewhere else.
2590 : : */
2591 : 199815 : datum = ExecEvalExpr(exprstate,
2592 [ + - ]: 199815 : GetPerTupleExprContext(estate),
2593 : : &isnull);
2594 [ - + ]: 199815 : if (isnull)
2595 : : {
1625 tomas.vondra@postgre 2596 :UBC 0 : result->values[idx][i] = (Datum) 0;
2597 : 0 : result->nulls[idx][i] = true;
2598 : : }
2599 : : else
2600 : : {
29 peter@eisentraut.org 2601 :GNC 199815 : result->values[idx][i] = datum;
1625 tomas.vondra@postgre 2602 :CBC 199815 : result->nulls[idx][i] = false;
2603 : : }
2604 : :
2605 : 199815 : idx++;
2606 : : }
2607 : : }
2608 : :
2609 : 199 : ExecDropSingleTupleTableSlot(slot);
2610 : 199 : FreeExecutorState(estate);
2611 : :
2612 : 199 : return result;
2613 : : }
|