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