Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * analyze.c
4 : * the Postgres statistics generator
5 : *
6 : * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/commands/analyze.c
12 : *
13 : *-------------------------------------------------------------------------
14 : */
15 : #include "postgres.h"
16 :
17 : #include <math.h>
18 :
19 : #include "access/detoast.h"
20 : #include "access/genam.h"
21 : #include "access/multixact.h"
22 : #include "access/relation.h"
23 : #include "access/sysattr.h"
24 : #include "access/table.h"
25 : #include "access/tableam.h"
26 : #include "access/transam.h"
27 : #include "access/tupconvert.h"
28 : #include "access/visibilitymap.h"
29 : #include "access/xact.h"
30 : #include "catalog/catalog.h"
31 : #include "catalog/index.h"
32 : #include "catalog/indexing.h"
33 : #include "catalog/pg_collation.h"
34 : #include "catalog/pg_inherits.h"
35 : #include "catalog/pg_namespace.h"
36 : #include "catalog/pg_statistic_ext.h"
37 : #include "commands/dbcommands.h"
38 : #include "commands/progress.h"
39 : #include "commands/tablecmds.h"
40 : #include "commands/vacuum.h"
41 : #include "common/pg_prng.h"
42 : #include "executor/executor.h"
43 : #include "foreign/fdwapi.h"
44 : #include "miscadmin.h"
45 : #include "nodes/nodeFuncs.h"
46 : #include "parser/parse_oper.h"
47 : #include "parser/parse_relation.h"
48 : #include "pgstat.h"
49 : #include "postmaster/autovacuum.h"
50 : #include "statistics/extended_stats_internal.h"
51 : #include "statistics/statistics.h"
52 : #include "storage/bufmgr.h"
53 : #include "storage/lmgr.h"
54 : #include "storage/proc.h"
55 : #include "storage/procarray.h"
56 : #include "utils/acl.h"
57 : #include "utils/attoptcache.h"
58 : #include "utils/builtins.h"
59 : #include "utils/datum.h"
60 : #include "utils/fmgroids.h"
61 : #include "utils/guc.h"
62 : #include "utils/lsyscache.h"
63 : #include "utils/memutils.h"
64 : #include "utils/pg_rusage.h"
65 : #include "utils/sampling.h"
66 : #include "utils/sortsupport.h"
67 : #include "utils/spccache.h"
68 : #include "utils/syscache.h"
69 : #include "utils/timestamp.h"
70 :
71 :
72 : /* Per-index data for ANALYZE */
73 : typedef struct AnlIndexData
74 : {
75 : IndexInfo *indexInfo; /* BuildIndexInfo result */
76 : double tupleFract; /* fraction of rows for partial index */
77 : VacAttrStats **vacattrstats; /* index attrs to analyze */
78 : int attr_cnt;
79 : } AnlIndexData;
80 :
81 :
82 : /* Default statistics target (GUC parameter) */
83 : int default_statistics_target = 100;
84 :
85 : /* A few variables that don't seem worth passing around as parameters */
86 : static MemoryContext anl_context = NULL;
87 : static BufferAccessStrategy vac_strategy;
88 :
89 :
90 : static void do_analyze_rel(Relation onerel,
91 : VacuumParams *params, List *va_cols,
92 : AcquireSampleRowsFunc acquirefunc, BlockNumber relpages,
93 : bool inh, bool in_outer_xact, int elevel);
94 : static void compute_index_stats(Relation onerel, double totalrows,
95 : AnlIndexData *indexdata, int nindexes,
96 : HeapTuple *rows, int numrows,
97 : MemoryContext col_context);
98 : static VacAttrStats *examine_attribute(Relation onerel, int attnum,
99 : Node *index_expr);
100 : static int acquire_sample_rows(Relation onerel, int elevel,
101 : HeapTuple *rows, int targrows,
102 : double *totalrows, double *totaldeadrows);
103 : static int compare_rows(const void *a, const void *b, void *arg);
104 : static int acquire_inherited_sample_rows(Relation onerel, int elevel,
105 : HeapTuple *rows, int targrows,
106 : double *totalrows, double *totaldeadrows);
107 : static void update_attstats(Oid relid, bool inh,
108 : int natts, VacAttrStats **vacattrstats);
109 : static Datum std_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull);
110 : static Datum ind_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull);
111 :
112 :
113 : /*
114 : * analyze_rel() -- analyze one relation
115 : *
116 : * relid identifies the relation to analyze. If relation is supplied, use
117 : * the name therein for reporting any failure to open/lock the rel; do not
118 : * use it once we've successfully opened the rel, since it might be stale.
119 : */
120 : void
1483 rhaas 121 CBC 24097 : analyze_rel(Oid relid, RangeVar *relation,
122 : VacuumParams *params, List *va_cols, bool in_outer_xact,
123 : BufferAccessStrategy bstrategy)
124 : {
125 : Relation onerel;
126 : int elevel;
4020 tgl 127 24097 : AcquireSampleRowsFunc acquirefunc = NULL;
3955 bruce 128 24097 : BlockNumber relpages = 0;
129 :
130 : /* Select logging level */
1483 rhaas 131 24097 : if (params->options & VACOPT_VERBOSE)
7708 bruce 132 UBC 0 : elevel = INFO;
133 : else
7257 bruce 134 CBC 24097 : elevel = DEBUG2;
135 :
136 : /* Set up static variables */
5793 tgl 137 24097 : vac_strategy = bstrategy;
138 :
139 : /*
140 : * Check for user-requested abort.
141 : */
8120 142 24097 : CHECK_FOR_INTERRUPTS();
143 :
144 : /*
145 : * Open the relation, getting ShareUpdateExclusiveLock to ensure that two
146 : * ANALYZEs don't run on it concurrently. (This also locks out a
147 : * concurrent VACUUM, which doesn't matter much at the moment but might
148 : * matter if we ever try to accumulate stats on dead tuples.) If the rel
149 : * has been dropped since we last saw it, we don't need to process it.
150 : *
151 : * Make sure to generate only logs for ANALYZE in this case.
152 : */
1483 rhaas 153 24097 : onerel = vacuum_open_relation(relid, relation, params->options & ~(VACOPT_VACUUM),
154 24097 : params->log_min_duration >= 0,
155 : ShareUpdateExclusiveLock);
156 :
157 : /* leave if relation could not be opened or locked */
1952 158 24097 : if (!onerel)
8179 tgl 159 337 : return;
160 :
161 : /*
162 : * Check if relation needs to be skipped based on privileges. This check
163 : * happens also when building the relation list to analyze for a manual
164 : * operation, and needs to be done additionally here as ANALYZE could
165 : * happen across multiple transactions where privileges could have changed
166 : * in-between. Make sure to generate only logs for ANALYZE in this case.
8350 bruce 167 ECB : */
132 andrew 168 GNC 24093 : if (!vacuum_is_permitted_for_relation(RelationGetRelid(onerel),
169 : onerel->rd_rel,
117 jdavis 170 24093 : params->options & VACOPT_ANALYZE))
8350 bruce 171 ECB : {
6048 tgl 172 CBC 9 : relation_close(onerel, ShareUpdateExclusiveLock);
8350 bruce 173 GIC 9 : return;
174 : }
175 :
176 : /*
177 : * Silently ignore tables that are temp tables of other backends ---
178 : * trying to analyze these is rather pointless, since their contents are
179 : * probably not up-to-date on disk. (We don't throw a warning here; it
180 : * would just lead to chatter during a database-wide ANALYZE.)
4020 tgl 181 ECB : */
4020 tgl 182 GIC 24084 : if (RELATION_IS_OTHER_TEMP(onerel))
4020 tgl 183 EUB : {
4020 tgl 184 UBC 0 : relation_close(onerel, ShareUpdateExclusiveLock);
4020 tgl 185 UIC 0 : return;
186 : }
187 :
188 : /*
189 : * We can ANALYZE any table except pg_statistic. See update_attstats
4020 tgl 190 ECB : */
4020 tgl 191 GIC 24084 : if (RelationGetRelid(onerel) == StatisticRelationId)
4020 tgl 192 ECB : {
4020 tgl 193 CBC 324 : relation_close(onerel, ShareUpdateExclusiveLock);
4020 tgl 194 GIC 324 : return;
195 : }
196 :
197 : /*
198 : * Check that it's of an analyzable relkind, and set up appropriately.
8007 tgl 199 ECB : */
3689 kgrittn 200 CBC 23760 : if (onerel->rd_rel->relkind == RELKIND_RELATION ||
2229 rhaas 201 GIC 347 : onerel->rd_rel->relkind == RELKIND_MATVIEW)
202 : {
4020 tgl 203 ECB : /* Regular table, so we'll use the regular row acquisition function */
4020 tgl 204 GIC 23413 : acquirefunc = acquire_sample_rows;
4020 tgl 205 ECB : /* Also get regular table's size */
4020 tgl 206 GIC 23413 : relpages = RelationGetNumberOfBlocks(onerel);
4020 tgl 207 ECB : }
4020 tgl 208 GIC 347 : else if (onerel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
209 : {
210 : /*
211 : * For a foreign table, call the FDW's hook function to see whether it
212 : * supports analysis.
213 : */
4020 tgl 214 ECB : FdwRoutine *fdwroutine;
4020 tgl 215 GIC 25 : bool ok = false;
4020 tgl 216 ECB :
3686 tgl 217 GIC 25 : fdwroutine = GetFdwRoutineForRelation(onerel, false);
4020 tgl 218 ECB :
4020 tgl 219 CBC 25 : if (fdwroutine->AnalyzeForeignTable != NULL)
4020 tgl 220 GIC 25 : ok = fdwroutine->AnalyzeForeignTable(onerel,
221 : &acquirefunc,
222 : &relpages);
4020 tgl 223 ECB :
4020 tgl 224 GIC 25 : if (!ok)
4020 tgl 225 EUB : {
4020 tgl 226 UIC 0 : ereport(WARNING,
227 : (errmsg("skipping \"%s\" --- cannot analyze this foreign table",
2118 tgl 228 EUB : RelationGetRelationName(onerel))));
4020 tgl 229 UBC 0 : relation_close(onerel, ShareUpdateExclusiveLock);
4020 tgl 230 UIC 0 : return;
231 : }
4020 tgl 232 ECB : }
2229 rhaas 233 GIC 322 : else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
234 : {
235 : /*
236 : * For partitioned tables, we want to do the recursive ANALYZE below.
237 : */
238 : }
239 : else
240 : {
7704 bruce 241 EUB : /* No need for a WARNING if we already complained during VACUUM */
1483 rhaas 242 UBC 0 : if (!(params->options & VACOPT_VACUUM))
7203 tgl 243 UIC 0 : ereport(WARNING,
244 : (errmsg("skipping \"%s\" --- cannot analyze non-tables or special system tables",
7203 tgl 245 EUB : RelationGetRelationName(onerel))));
6048 tgl 246 UBC 0 : relation_close(onerel, ShareUpdateExclusiveLock);
7677 tgl 247 UIC 0 : return;
248 : }
249 :
250 : /*
251 : * OK, let's do it. First, initialize progress reporting.
4849 tgl 252 ECB : */
1180 alvherre 253 GIC 23760 : pgstat_progress_start_command(PROGRESS_COMMAND_ANALYZE,
254 : RelationGetRelid(onerel));
255 :
256 : /*
257 : * Do the normal non-recursive ANALYZE. We can skip this for partitioned
258 : * tables, which don't contain any rows.
4849 tgl 259 ECB : */
2229 rhaas 260 CBC 23760 : if (onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1483 rhaas 261 GIC 23438 : do_analyze_rel(onerel, params, va_cols, acquirefunc,
262 : relpages, false, in_outer_xact, elevel);
263 :
264 : /*
265 : * If there are child tables, do recursive ANALYZE.
4849 tgl 266 ECB : */
4849 tgl 267 CBC 23739 : if (onerel->rd_rel->relhassubclass)
1483 rhaas 268 GIC 348 : do_analyze_rel(onerel, params, va_cols, acquirefunc, relpages,
269 : true, in_outer_xact, elevel);
270 :
271 : /*
272 : * Close source relation now, but keep lock so that no one deletes it
273 : * before we commit. (If someone did, they'd fail to clean up the entries
274 : * we made in pg_statistic. Also, releasing the lock before commit would
275 : * expose us to concurrent-update failures in update_attstats.)
4849 tgl 276 ECB : */
4849 tgl 277 GIC 23730 : relation_close(onerel, NoLock);
4849 tgl 278 ECB :
1180 alvherre 279 GIC 23730 : pgstat_progress_end_command();
280 : }
281 :
282 : /*
283 : * do_analyze_rel() -- analyze one relation, recursively or not
284 : *
285 : * Note that "acquirefunc" is only relevant for the non-inherited case.
286 : * For the inherited case, acquire_inherited_sample_rows() determines the
287 : * appropriate acquirefunc for each child table.
288 : */
4849 tgl 289 ECB : static void
1483 rhaas 290 GIC 23786 : do_analyze_rel(Relation onerel, VacuumParams *params,
291 : List *va_cols, AcquireSampleRowsFunc acquirefunc,
292 : BlockNumber relpages, bool inh, bool in_outer_xact,
293 : int elevel)
294 : {
295 : int attr_cnt,
296 : tcnt,
297 : i,
298 : ind;
299 : Relation *Irel;
300 : int nindexes;
301 : bool hasindex;
302 : VacAttrStats **vacattrstats;
303 : AnlIndexData *indexdata;
304 : int targrows,
305 : numrows,
306 : minrows;
307 : double totalrows,
308 : totaldeadrows;
309 : HeapTuple *rows;
4849 tgl 310 ECB : PGRUsage ru0;
4849 tgl 311 GIC 23786 : TimestampTz starttime = 0;
312 : MemoryContext caller_context;
313 : Oid save_userid;
314 : int save_sec_context;
4849 tgl 315 ECB : int save_nestlevel;
754 sfrost 316 CBC 23786 : int64 AnalyzePageHit = VacuumPageHit;
317 23786 : int64 AnalyzePageMiss = VacuumPageMiss;
318 23786 : int64 AnalyzePageDirty = VacuumPageDirty;
319 23786 : PgStat_Counter startreadtime = 0;
754 sfrost 320 GIC 23786 : PgStat_Counter startwritetime = 0;
4849 tgl 321 ECB :
4849 tgl 322 CBC 23786 : if (inh)
4849 tgl 323 GIC 348 : ereport(elevel,
324 : (errmsg("analyzing \"%s.%s\" inheritance tree",
325 : get_namespace_name(RelationGetNamespace(onerel)),
326 : RelationGetRelationName(onerel))));
4849 tgl 327 ECB : else
4849 tgl 328 GIC 23438 : ereport(elevel,
329 : (errmsg("analyzing \"%s.%s\"",
330 : get_namespace_name(RelationGetNamespace(onerel)),
331 : RelationGetRelationName(onerel))));
332 :
333 : /*
334 : * Set up a working context so that we can easily free whatever junk gets
335 : * created.
4849 tgl 336 ECB : */
4849 tgl 337 GIC 23786 : anl_context = AllocSetContextCreate(CurrentMemoryContext,
338 : "Analyze",
2416 tgl 339 ECB : ALLOCSET_DEFAULT_SIZES);
4849 tgl 340 GIC 23786 : caller_context = MemoryContextSwitchTo(anl_context);
341 :
342 : /*
343 : * Switch to the table owner's userid, so that any index functions are run
344 : * as that user. Also lock down security-restricted operations and
345 : * arrange to make GUC variable changes local to this command.
5575 tgl 346 ECB : */
4869 tgl 347 CBC 23786 : GetUserIdAndSecContext(&save_userid, &save_sec_context);
4869 tgl 348 GIC 23786 : SetUserIdAndSecContext(onerel->rd_rel->relowner,
4869 tgl 349 ECB : save_sec_context | SECURITY_RESTRICTED_OPERATION);
4869 tgl 350 GIC 23786 : save_nestlevel = NewGUCNestLevel();
351 :
5835 alvherre 352 ECB : /* measure elapsed time iff autovacuum logging requires it */
2928 alvherre 353 GIC 23786 : if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
5835 alvherre 354 ECB : {
754 sfrost 355 GIC 159 : if (track_io_timing)
754 sfrost 356 EUB : {
754 sfrost 357 UBC 0 : startreadtime = pgStatBlockReadTime;
754 sfrost 358 UIC 0 : startwritetime = pgStatBlockWriteTime;
359 : }
754 sfrost 360 ECB :
5835 alvherre 361 CBC 159 : pg_rusage_init(&ru0);
187 michael 362 GNC 159 : starttime = GetCurrentTimestamp();
363 : }
364 :
365 : /*
366 : * Determine which columns to analyze
367 : *
368 : * Note that system attributes are never analyzed, so we just reject them
369 : * at the lookup stage. We also reject duplicate column mentions. (We
370 : * could alternatively ignore duplicates, but analyzing a column twice
2026 tgl 371 ECB : * won't work; we'd end up making a conflicting update in pg_statistic.)
372 : */
2944 alvherre 373 CBC 23786 : if (va_cols != NIL)
374 : {
2026 tgl 375 GIC 47 : Bitmapset *unique_cols = NULL;
6892 neilc 376 ECB : ListCell *le;
377 :
2944 alvherre 378 CBC 47 : vacattrstats = (VacAttrStats **) palloc(list_length(va_cols) *
8007 tgl 379 ECB : sizeof(VacAttrStats *));
8007 tgl 380 GIC 47 : tcnt = 0;
2944 alvherre 381 CBC 82 : foreach(le, va_cols)
382 : {
8007 tgl 383 60 : char *col = strVal(lfirst(le));
8350 bruce 384 ECB :
7555 tgl 385 CBC 60 : i = attnameAttNum(onerel, col, false);
6226 tgl 386 GIC 60 : if (i == InvalidAttrNumber)
387 19 : ereport(ERROR,
388 : (errcode(ERRCODE_UNDEFINED_COLUMN),
2118 tgl 389 ECB : errmsg("column \"%s\" of relation \"%s\" does not exist",
390 : col, RelationGetRelationName(onerel))));
2026 tgl 391 GIC 41 : if (bms_is_member(i, unique_cols))
392 6 : ereport(ERROR,
393 : (errcode(ERRCODE_DUPLICATE_COLUMN),
2021 tgl 394 ECB : errmsg("column \"%s\" of relation \"%s\" appears more than once",
395 : col, RelationGetRelationName(onerel))));
2026 tgl 396 CBC 35 : unique_cols = bms_add_member(unique_cols, i);
2026 tgl 397 ECB :
4634 tgl 398 CBC 35 : vacattrstats[tcnt] = examine_attribute(onerel, i, NULL);
8007 tgl 399 GIC 35 : if (vacattrstats[tcnt] != NULL)
8007 tgl 400 CBC 35 : tcnt++;
401 : }
8007 tgl 402 GIC 22 : attr_cnt = tcnt;
403 : }
8007 tgl 404 ECB : else
405 : {
7555 tgl 406 CBC 23739 : attr_cnt = onerel->rd_att->natts;
6882 tgl 407 ECB : vacattrstats = (VacAttrStats **)
6882 tgl 408 CBC 23739 : palloc(attr_cnt * sizeof(VacAttrStats *));
8007 tgl 409 GIC 23739 : tcnt = 0;
7555 tgl 410 CBC 220750 : for (i = 1; i <= attr_cnt; i++)
8007 tgl 411 ECB : {
4634 tgl 412 CBC 197011 : vacattrstats[tcnt] = examine_attribute(onerel, i, NULL);
8007 tgl 413 GIC 197011 : if (vacattrstats[tcnt] != NULL)
8007 tgl 414 CBC 197005 : tcnt++;
415 : }
8350 bruce 416 GIC 23739 : attr_cnt = tcnt;
417 : }
418 :
419 : /*
420 : * Open all indexes of the relation, and see if there are any analyzable
421 : * columns in the indexes. We do not analyze index columns if there was
422 : * an explicit column list in the ANALYZE command, however.
423 : *
424 : * If we are doing a recursive scan, we don't want to touch the parent's
425 : * indexes at all. If we're processing a partitioned table, we need to
647 alvherre 426 ECB : * know if there are any indexes, but we don't want to process them.
427 : */
647 alvherre 428 CBC 23761 : if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
429 : {
332 tgl 430 313 : List *idxs = RelationGetIndexList(onerel);
647 alvherre 431 ECB :
647 alvherre 432 CBC 313 : Irel = NULL;
433 313 : nindexes = 0;
647 alvherre 434 GIC 313 : hasindex = idxs != NIL;
647 alvherre 435 CBC 313 : list_free(idxs);
436 : }
437 23448 : else if (!inh)
647 alvherre 438 ECB : {
4849 tgl 439 GIC 23422 : vac_open_indexes(onerel, AccessShareLock, &nindexes, &Irel);
647 alvherre 440 23422 : hasindex = nindexes > 0;
441 : }
4849 tgl 442 ECB : else
443 : {
4849 tgl 444 CBC 26 : Irel = NULL;
4849 tgl 445 GIC 26 : nindexes = 0;
647 alvherre 446 CBC 26 : hasindex = false;
4849 tgl 447 ECB : }
6993 tgl 448 GIC 23761 : indexdata = NULL;
647 alvherre 449 CBC 23761 : if (nindexes > 0)
6993 tgl 450 ECB : {
6993 tgl 451 GIC 20995 : indexdata = (AnlIndexData *) palloc0(nindexes * sizeof(AnlIndexData));
6993 tgl 452 CBC 61625 : for (ind = 0; ind < nindexes; ind++)
453 : {
6993 tgl 454 GIC 40631 : AnlIndexData *thisdata = &indexdata[ind];
6797 bruce 455 ECB : IndexInfo *indexInfo;
6993 tgl 456 :
6993 tgl 457 CBC 40631 : thisdata->indexInfo = indexInfo = BuildIndexInfo(Irel[ind]);
6797 bruce 458 GIC 40630 : thisdata->tupleFract = 1.0; /* fix later if partial */
2944 alvherre 459 CBC 40630 : if (indexInfo->ii_Expressions != NIL && va_cols == NIL)
460 : {
6892 neilc 461 31 : ListCell *indexpr_item = list_head(indexInfo->ii_Expressions);
6993 tgl 462 ECB :
6993 tgl 463 CBC 31 : thisdata->vacattrstats = (VacAttrStats **)
464 31 : palloc(indexInfo->ii_NumIndexAttrs * sizeof(VacAttrStats *));
6993 tgl 465 GIC 31 : tcnt = 0;
6993 tgl 466 CBC 64 : for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
467 : {
1823 teodor 468 33 : int keycol = indexInfo->ii_IndexAttrNumbers[i];
469 :
6993 tgl 470 GIC 33 : if (keycol == 0)
471 : {
472 : /* Found an index expression */
6993 tgl 473 ECB : Node *indexkey;
6993 tgl 474 EUB :
2118 tgl 475 CBC 31 : if (indexpr_item == NULL) /* shouldn't happen */
6993 tgl 476 LBC 0 : elog(ERROR, "too few entries in indexprs list");
6892 neilc 477 GIC 31 : indexkey = (Node *) lfirst(indexpr_item);
1364 tgl 478 CBC 31 : indexpr_item = lnext(indexInfo->ii_Expressions,
1364 tgl 479 ECB : indexpr_item);
6993 tgl 480 CBC 62 : thisdata->vacattrstats[tcnt] =
4634 481 31 : examine_attribute(Irel[ind], i + 1, indexkey);
6993 tgl 482 GIC 31 : if (thisdata->vacattrstats[tcnt] != NULL)
483 31 : tcnt++;
6993 tgl 484 ECB : }
485 : }
6993 tgl 486 GIC 31 : thisdata->attr_cnt = tcnt;
487 : }
488 : }
489 : }
490 :
491 : /*
492 : * Determine how many rows we need to sample, using the worst case from
493 : * all analyzable columns. We use a lower bound of 100 rows to avoid
494 : * possible overflow in Vitter's algorithm. (Note: that will also be the
3955 bruce 495 ECB : * target in the corner case where there are no analyzable columns.)
8007 tgl 496 : */
8007 tgl 497 GIC 23760 : targrows = 100;
8350 bruce 498 CBC 220787 : for (i = 0; i < attr_cnt; i++)
8350 bruce 499 ECB : {
8007 tgl 500 GIC 197027 : if (targrows < vacattrstats[i]->minrows)
8007 tgl 501 CBC 23757 : targrows = vacattrstats[i]->minrows;
502 : }
6993 503 64390 : for (ind = 0; ind < nindexes; ind++)
504 : {
505 40630 : AnlIndexData *thisdata = &indexdata[ind];
506 :
507 40661 : for (i = 0; i < thisdata->attr_cnt; i++)
6993 tgl 508 EUB : {
6993 tgl 509 GIC 31 : if (targrows < thisdata->vacattrstats[i]->minrows)
6993 tgl 510 UIC 0 : targrows = thisdata->vacattrstats[i]->minrows;
511 : }
512 : }
513 :
514 : /*
515 : * Look at extended statistics objects too, as those may define custom
516 : * statistics target. So we may need to sample more rows and then build
1307 tomas.vondra 517 ECB : * the statistics with enough detail.
518 : */
1307 tomas.vondra 519 CBC 23760 : minrows = ComputeExtStatisticsRows(onerel, attr_cnt, vacattrstats);
1307 tomas.vondra 520 EUB :
1307 tomas.vondra 521 GIC 23760 : if (targrows < minrows)
1307 tomas.vondra 522 UIC 0 : targrows = minrows;
523 :
524 : /*
8007 tgl 525 ECB : * Acquire the sample rows
526 : */
8007 tgl 527 GIC 23760 : rows = (HeapTuple *) palloc(targrows * sizeof(HeapTuple));
1180 alvherre 528 23760 : pgstat_progress_update_param(PROGRESS_ANALYZE_PHASE,
1180 alvherre 529 ECB : inh ? PROGRESS_ANALYZE_PHASE_ACQUIRE_SAMPLE_ROWS_INH :
530 : PROGRESS_ANALYZE_PHASE_ACQUIRE_SAMPLE_ROWS);
4849 tgl 531 GIC 23760 : if (inh)
4020 532 339 : numrows = acquire_inherited_sample_rows(onerel, elevel,
533 : rows, targrows,
4849 tgl 534 ECB : &totalrows, &totaldeadrows);
535 : else
4020 tgl 536 GIC 23421 : numrows = (*acquirefunc) (onerel, elevel,
537 : rows, targrows,
538 : &totalrows, &totaldeadrows);
539 :
540 : /*
541 : * Compute the statistics. Temporary results during the calculations for
542 : * each column are stored in a child context. The calc routines are
543 : * responsible to make sure that whatever they store into the VacAttrStats
6385 bruce 544 ECB : * structure is allocated in anl_context.
545 : */
8007 tgl 546 GIC 23759 : if (numrows > 0)
547 : {
548 : MemoryContext col_context,
8007 tgl 549 ECB : old_context;
550 :
1180 alvherre 551 GIC 14181 : pgstat_progress_update_param(PROGRESS_ANALYZE_PHASE,
1180 alvherre 552 ECB : PROGRESS_ANALYZE_PHASE_COMPUTE_STATS);
553 :
7603 tgl 554 GIC 14181 : col_context = AllocSetContextCreate(anl_context,
8007 tgl 555 ECB : "Analyze Column",
556 : ALLOCSET_DEFAULT_SIZES);
8007 tgl 557 CBC 14181 : old_context = MemoryContextSwitchTo(col_context);
558 :
559 153145 : for (i = 0; i < attr_cnt; i++)
560 : {
6995 tgl 561 GIC 138964 : VacAttrStats *stats = vacattrstats[i];
4054 tgl 562 ECB : AttributeOpts *aopt;
6995 563 :
6995 tgl 564 CBC 138964 : stats->rows = rows;
6995 tgl 565 GIC 138964 : stats->tupDesc = onerel->rd_att;
2040 peter_e 566 138964 : stats->compute_stats(stats,
567 : std_fetch_func,
568 : numrows,
569 : totalrows);
570 :
571 : /*
572 : * If the appropriate flavor of the n_distinct option is
4825 rhaas 573 ECB : * specified, override with the corresponding value.
574 : */
4054 tgl 575 GIC 138964 : aopt = get_attribute_options(onerel->rd_id, stats->attr->attnum);
4825 rhaas 576 138964 : if (aopt != NULL)
577 : {
4054 tgl 578 ECB : float8 n_distinct;
4790 bruce 579 :
4054 tgl 580 CBC 3 : n_distinct = inh ? aopt->n_distinct_inherited : aopt->n_distinct;
4825 rhaas 581 GIC 3 : if (n_distinct != 0.0)
582 3 : stats->stadistinct = n_distinct;
4825 rhaas 583 ECB : }
584 :
8007 tgl 585 GIC 138964 : MemoryContextResetAndDeleteChildren(col_context);
8350 bruce 586 ECB : }
6993 tgl 587 :
647 alvherre 588 GIC 14181 : if (nindexes > 0)
6993 tgl 589 11564 : compute_index_stats(onerel, totalrows,
590 : indexdata, nindexes,
591 : rows, numrows,
6993 tgl 592 ECB : col_context);
593 :
8007 tgl 594 GIC 14178 : MemoryContextSwitchTo(old_context);
595 14178 : MemoryContextDelete(col_context);
596 :
597 : /*
598 : * Emit the completed stats rows into pg_statistic, replacing any
599 : * previous statistics for the target columns. (If there are stats in
6385 bruce 600 ECB : * pg_statistic for columns we didn't process, we leave them alone.)
601 : */
4849 tgl 602 GIC 14178 : update_attstats(RelationGetRelid(onerel), inh,
4849 tgl 603 ECB : attr_cnt, vacattrstats);
604 :
6993 tgl 605 CBC 37909 : for (ind = 0; ind < nindexes; ind++)
606 : {
607 23731 : AnlIndexData *thisdata = &indexdata[ind];
608 :
4849 tgl 609 GIC 23731 : update_attstats(RelationGetRelid(Irel[ind]), false,
610 : thisdata->attr_cnt, thisdata->vacattrstats);
611 : }
2207 alvherre 612 ECB :
613 : /* Build extended statistics (if there are any). */
448 tomas.vondra 614 GIC 14178 : BuildRelationExtStatistics(onerel, inh, totalrows, numrows, rows,
615 : attr_cnt, vacattrstats);
6993 tgl 616 ECB : }
617 :
1180 alvherre 618 GIC 23756 : pgstat_progress_update_param(PROGRESS_ANALYZE_PHASE,
619 : PROGRESS_ANALYZE_PHASE_FINALIZE_ANALYZE);
620 :
621 : /*
622 : * Update pages/tuples stats in pg_class ... but not if we're doing
623 : * inherited stats.
624 : *
625 : * We assume that VACUUM hasn't set pg_class.reltuples already, even
626 : * during a VACUUM ANALYZE. Although VACUUM often updates pg_class,
627 : * exceptions exist. A "VACUUM (ANALYZE, INDEX_CLEANUP OFF)" command will
628 : * never update pg_class entries for index relations. It's also possible
629 : * that an individual index's pg_class entry won't be updated during
697 tgl 630 ECB : * VACUUM if the index AM returns NULL from its amvacuumcleanup() routine.
631 : */
4332 tgl 632 GIC 23756 : if (!inh)
633 : {
2495 rhaas 634 ECB : BlockNumber relallvisible;
635 :
2595 rhaas 636 GIC 23417 : visibilitymap_count(onerel, &relallvisible, NULL);
2595 rhaas 637 ECB :
638 : /* Update pg_class for table relation */
5263 tgl 639 GIC 23417 : vac_update_relstats(onerel,
640 : relpages,
641 : totalrows,
642 : relallvisible,
643 : hasindex,
644 : InvalidTransactionId,
645 : InvalidMultiXactId,
646 : NULL, NULL,
647 : in_outer_xact);
6117 alvherre 648 ECB :
649 : /* Same for indexes */
6993 tgl 650 CBC 64041 : for (ind = 0; ind < nindexes; ind++)
651 : {
6993 tgl 652 GIC 40624 : AnlIndexData *thisdata = &indexdata[ind];
6993 tgl 653 ECB : double totalindexrows;
654 :
6993 tgl 655 CBC 40624 : totalindexrows = ceil(thisdata->tupleFract * totalrows);
5263 tgl 656 GIC 40624 : vac_update_relstats(Irel[ind],
6993 657 40624 : RelationGetNumberOfBlocks(Irel[ind]),
658 : totalindexrows,
659 : 0,
660 : false,
661 : InvalidTransactionId,
662 : InvalidMultiXactId,
663 : NULL, NULL,
664 : in_outer_xact);
6993 tgl 665 ECB : }
666 : }
589 alvherre 667 GIC 339 : else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
668 : {
669 : /*
670 : * Partitioned tables don't have storage, so we don't set any fields
589 alvherre 671 ECB : * in their pg_class entries except for reltuples and relhasindex.
672 : */
589 alvherre 673 GIC 313 : vac_update_relstats(onerel, -1, totalrows,
674 : 0, hasindex, InvalidTransactionId,
675 : InvalidMultiXactId,
676 : NULL, NULL,
677 : in_outer_xact);
678 : }
679 :
680 : /*
681 : * Now report ANALYZE to the cumulative stats system. For regular tables,
682 : * we do it only if not doing inherited stats. For partitioned tables, we
683 : * only do it for inherited stats. (We're never called for not-inherited
684 : * stats on partitioned tables anyway.)
685 : *
686 : * Reset the changes_since_analyze counter only if we analyzed all
589 alvherre 687 ECB : * columns; otherwise, there is still work for auto-analyze to do.
688 : */
589 alvherre 689 GIC 23756 : if (!inh)
2498 tgl 690 CBC 23417 : pgstat_report_analyze(onerel, totalrows, totaldeadrows,
2498 tgl 691 ECB : (va_cols == NIL));
589 alvherre 692 GIC 339 : else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
693 313 : pgstat_report_analyze(onerel, 0, 0, (va_cols == NIL));
694 :
695 : /*
696 : * If this isn't part of VACUUM ANALYZE, let index AMs do cleanup.
697 : *
698 : * Note that most index AMs perform a no-op as a matter of policy for
699 : * amvacuumcleanup() when called in ANALYZE-only mode. The only exception
760 pg 700 ECB : * among core index AMs is GIN/ginvacuumcleanup().
701 : */
1483 rhaas 702 CBC 23756 : if (!(params->options & VACOPT_VACUUM))
703 : {
5129 tgl 704 GIC 63282 : for (ind = 0; ind < nindexes; ind++)
705 : {
706 : IndexBulkDeleteResult *stats;
5129 tgl 707 ECB : IndexVacuumInfo ivinfo;
708 :
5129 tgl 709 CBC 40024 : ivinfo.index = Irel[ind];
6 pg 710 GNC 40024 : ivinfo.heaprel = onerel;
5129 tgl 711 CBC 40024 : ivinfo.analyze_only = true;
5055 712 40024 : ivinfo.estimated_count = true;
5129 713 40024 : ivinfo.message_level = elevel;
5055 714 40024 : ivinfo.num_heap_tuples = onerel->rd_rel->reltuples;
5129 tgl 715 GIC 40024 : ivinfo.strategy = vac_strategy;
5129 tgl 716 ECB :
5129 tgl 717 GIC 40024 : stats = index_vacuum_cleanup(&ivinfo, NULL);
5129 tgl 718 ECB :
5129 tgl 719 GBC 40024 : if (stats)
5129 tgl 720 UIC 0 : pfree(stats);
721 : }
722 : }
723 :
6993 tgl 724 ECB : /* Done with indexes */
6765 tgl 725 GIC 23756 : vac_close_indexes(nindexes, Irel, NoLock);
726 :
5835 alvherre 727 ECB : /* Log the action if appropriate */
2928 alvherre 728 GIC 23756 : if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
5835 alvherre 729 ECB : {
754 sfrost 730 GIC 159 : TimestampTz endtime = GetCurrentTimestamp();
754 sfrost 731 ECB :
2928 alvherre 732 CBC 236 : if (params->log_min_duration == 0 ||
754 sfrost 733 GIC 77 : TimestampDifferenceExceeds(starttime, endtime,
734 : params->log_min_duration))
735 : {
754 sfrost 736 ECB : long delay_in_ms;
754 sfrost 737 CBC 82 : double read_rate = 0;
754 sfrost 738 GIC 82 : double write_rate = 0;
739 : StringInfoData buf;
740 :
741 : /*
742 : * Calculate the difference in the Page Hit/Miss/Dirty that
743 : * happened as part of the analyze by subtracting out the
744 : * pre-analyze values which we saved above.
754 sfrost 745 ECB : */
754 sfrost 746 CBC 82 : AnalyzePageHit = VacuumPageHit - AnalyzePageHit;
747 82 : AnalyzePageMiss = VacuumPageMiss - AnalyzePageMiss;
754 sfrost 748 GIC 82 : AnalyzePageDirty = VacuumPageDirty - AnalyzePageDirty;
749 :
750 : /*
751 : * We do not expect an analyze to take > 25 days and it simplifies
752 : * things a bit to use TimestampDifferenceMilliseconds.
754 sfrost 753 ECB : */
754 sfrost 754 GIC 82 : delay_in_ms = TimestampDifferenceMilliseconds(starttime, endtime);
755 :
756 : /*
757 : * Note that we are reporting these read/write rates in the same
758 : * manner as VACUUM does, which means that while the 'average read
759 : * rate' here actually corresponds to page misses and resulting
760 : * reads which are also picked up by track_io_timing, if enabled,
761 : * the 'average write rate' is actually talking about the rate of
762 : * pages being dirtied, not being written out, so it's typical to
763 : * have a non-zero 'avg write rate' while I/O timings only reports
764 : * reads.
765 : *
766 : * It's not clear that an ANALYZE will ever result in
767 : * FlushBuffer() being called, but we track and support reporting
768 : * on I/O write time in case that changes as it's practically free
769 : * to do so anyway.
770 : */
754 sfrost 771 ECB :
754 sfrost 772 GIC 82 : if (delay_in_ms > 0)
754 sfrost 773 ECB : {
754 sfrost 774 CBC 82 : read_rate = (double) BLCKSZ * AnalyzePageMiss / (1024 * 1024) /
775 82 : (delay_in_ms / 1000.0);
776 82 : write_rate = (double) BLCKSZ * AnalyzePageDirty / (1024 * 1024) /
754 sfrost 777 GIC 82 : (delay_in_ms / 1000.0);
778 : }
779 :
780 : /*
781 : * We split this up so we don't emit empty I/O timing values when
782 : * track_io_timing isn't enabled.
783 : */
754 sfrost 784 ECB :
754 sfrost 785 CBC 82 : initStringInfo(&buf);
754 sfrost 786 GIC 82 : appendStringInfo(&buf, _("automatic analyze of table \"%s.%s.%s\"\n"),
754 sfrost 787 ECB : get_database_name(MyDatabaseId),
754 sfrost 788 CBC 82 : get_namespace_name(RelationGetNamespace(onerel)),
789 82 : RelationGetRelationName(onerel));
754 sfrost 790 GIC 82 : if (track_io_timing)
754 sfrost 791 EUB : {
590 pg 792 UBC 0 : double read_ms = (double) (pgStatBlockReadTime - startreadtime) / 1000;
590 pg 793 UIC 0 : double write_ms = (double) (pgStatBlockWriteTime - startwritetime) / 1000;
590 pg 794 EUB :
590 pg 795 UIC 0 : appendStringInfo(&buf, _("I/O timings: read: %.3f ms, write: %.3f ms\n"),
796 : read_ms, write_ms);
754 sfrost 797 ECB : }
590 pg 798 GIC 82 : appendStringInfo(&buf, _("avg read rate: %.3f MB/s, avg write rate: %.3f MB/s\n"),
590 pg 799 ECB : read_rate, write_rate);
590 pg 800 GIC 82 : appendStringInfo(&buf, _("buffer usage: %lld hits, %lld misses, %lld dirtied\n"),
801 : (long long) AnalyzePageHit,
802 : (long long) AnalyzePageMiss,
590 pg 803 ECB : (long long) AnalyzePageDirty);
754 sfrost 804 GIC 82 : appendStringInfo(&buf, _("system usage: %s"), pg_rusage_show(&ru0));
754 sfrost 805 ECB :
5835 alvherre 806 GIC 82 : ereport(LOG,
807 : (errmsg_internal("%s", buf.data)));
754 sfrost 808 ECB :
754 sfrost 809 GIC 82 : pfree(buf.data);
810 : }
811 : }
812 :
4869 tgl 813 ECB : /* Roll back any GUC changes executed by index functions */
4869 tgl 814 GIC 23756 : AtEOXact_GUC(false, save_nestlevel);
815 :
4869 tgl 816 ECB : /* Restore userid and security context */
4869 tgl 817 GIC 23756 : SetUserIdAndSecContext(save_userid, save_sec_context);
818 :
4849 tgl 819 ECB : /* Restore current context and release memory */
4849 tgl 820 CBC 23756 : MemoryContextSwitchTo(caller_context);
821 23756 : MemoryContextDelete(anl_context);
822 23756 : anl_context = NULL;
8007 tgl 823 GIC 23756 : }
824 :
825 : /*
826 : * Compute statistics about indexes of a relation
827 : */
6993 tgl 828 ECB : static void
6993 tgl 829 GIC 11564 : compute_index_stats(Relation onerel, double totalrows,
830 : AnlIndexData *indexdata, int nindexes,
831 : HeapTuple *rows, int numrows,
832 : MemoryContext col_context)
833 : {
834 : MemoryContext ind_context,
835 : old_context;
836 : Datum values[INDEX_MAX_KEYS];
837 : bool isnull[INDEX_MAX_KEYS];
838 : int ind,
839 : i;
6993 tgl 840 ECB :
6993 tgl 841 GIC 11564 : ind_context = AllocSetContextCreate(anl_context,
842 : "Analyze Index",
2416 tgl 843 ECB : ALLOCSET_DEFAULT_SIZES);
6993 tgl 844 GIC 11564 : old_context = MemoryContextSwitchTo(ind_context);
6993 tgl 845 ECB :
6993 tgl 846 GIC 35298 : for (ind = 0; ind < nindexes; ind++)
6993 tgl 847 ECB : {
6993 tgl 848 CBC 23737 : AnlIndexData *thisdata = &indexdata[ind];
6797 bruce 849 23737 : IndexInfo *indexInfo = thisdata->indexInfo;
6993 tgl 850 GIC 23737 : int attr_cnt = thisdata->attr_cnt;
851 : TupleTableSlot *slot;
852 : EState *estate;
853 : ExprContext *econtext;
854 : ExprState *predicate;
855 : Datum *exprvals;
856 : bool *exprnulls;
857 : int numindexrows,
858 : tcnt,
859 : rowno;
860 : double totalindexrows;
861 :
6993 tgl 862 ECB : /* Ignore index if no columns to analyze and not partial */
6993 tgl 863 CBC 23737 : if (attr_cnt == 0 && indexInfo->ii_Predicate == NIL)
6993 tgl 864 GIC 23689 : continue;
865 :
866 : /*
867 : * Need an EState for evaluation of index expressions and
868 : * partial-index predicates. Create it in the per-index context to be
869 : * sure it gets cleaned up at the bottom of the loop.
6993 tgl 870 ECB : */
6993 tgl 871 CBC 48 : estate = CreateExecutorState();
6993 tgl 872 GIC 48 : econtext = GetPerTupleExprContext(estate);
6993 tgl 873 ECB : /* Need a slot to hold the current heap tuple, too */
1606 andres 874 GIC 48 : slot = MakeSingleTupleTableSlot(RelationGetDescr(onerel),
875 : &TTSOpsHeapTuple);
876 :
6993 tgl 877 ECB : /* Arrange for econtext's scan tuple to be the tuple under test */
6993 tgl 878 GIC 48 : econtext->ecxt_scantuple = slot;
879 :
6993 tgl 880 ECB : /* Set up execution state for predicate. */
2217 andres 881 GIC 48 : predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
882 :
6993 tgl 883 ECB : /* Compute and save index expression values */
6882 tgl 884 CBC 48 : exprvals = (Datum *) palloc(numrows * attr_cnt * sizeof(Datum));
885 48 : exprnulls = (bool *) palloc(numrows * attr_cnt * sizeof(bool));
6993 886 48 : numindexrows = 0;
887 48 : tcnt = 0;
6993 tgl 888 GIC 106287 : for (rowno = 0; rowno < numrows; rowno++)
6993 tgl 889 ECB : {
6993 tgl 890 GIC 106242 : HeapTuple heapTuple = rows[rowno];
6993 tgl 891 ECB :
2933 tgl 892 GIC 106242 : vacuum_delay_point();
893 :
894 : /*
895 : * Reset the per-tuple context each time, to reclaim any cruft
896 : * left behind by evaluating the predicate or index expressions.
4534 tgl 897 ECB : */
4534 tgl 898 GIC 106242 : ResetExprContext(econtext);
899 :
6993 tgl 900 ECB : /* Set up for predicate or expression evaluation */
1657 andres 901 GIC 106242 : ExecStoreHeapTuple(heapTuple, slot, false);
902 :
6993 tgl 903 ECB : /* If index is partial, check predicate */
2217 andres 904 GIC 106242 : if (predicate != NULL)
6993 tgl 905 ECB : {
2217 andres 906 CBC 29024 : if (!ExecQual(predicate, econtext))
6993 tgl 907 GIC 10666 : continue;
6993 tgl 908 ECB : }
6993 tgl 909 GIC 95576 : numindexrows++;
6993 tgl 910 ECB :
6993 tgl 911 GIC 95576 : if (attr_cnt > 0)
912 : {
913 : /*
914 : * Evaluate the index row to compute expression values. We
915 : * could do this by hand, but FormIndexDatum is convenient.
6993 tgl 916 ECB : */
6993 tgl 917 GIC 77218 : FormIndexDatum(indexInfo,
918 : slot,
919 : estate,
920 : values,
921 : isnull);
922 :
923 : /*
924 : * Save just the columns we care about. We copy the values
925 : * into ind_context from the estate's per-tuple context.
6993 tgl 926 ECB : */
6993 tgl 927 GIC 154430 : for (i = 0; i < attr_cnt; i++)
6993 tgl 928 ECB : {
6993 tgl 929 CBC 77215 : VacAttrStats *stats = thisdata->vacattrstats[i];
6797 bruce 930 GIC 77215 : int attnum = stats->attr->attnum;
6993 tgl 931 ECB :
4534 tgl 932 GIC 77215 : if (isnull[attnum - 1])
4534 tgl 933 EUB : {
4534 tgl 934 UBC 0 : exprvals[tcnt] = (Datum) 0;
4534 tgl 935 UIC 0 : exprnulls[tcnt] = true;
936 : }
937 : else
4534 tgl 938 ECB : {
4534 tgl 939 CBC 154430 : exprvals[tcnt] = datumCopy(values[attnum - 1],
940 77215 : stats->attrtype->typbyval,
941 77215 : stats->attrtype->typlen);
4534 tgl 942 GIC 77215 : exprnulls[tcnt] = false;
4534 tgl 943 ECB : }
6993 tgl 944 GIC 77215 : tcnt++;
945 : }
946 : }
947 : }
948 :
949 : /*
950 : * Having counted the number of rows that pass the predicate in the
951 : * sample, we can estimate the total number of rows in the index.
6993 tgl 952 ECB : */
6993 tgl 953 CBC 45 : thisdata->tupleFract = (double) numindexrows / (double) numrows;
6993 tgl 954 GIC 45 : totalindexrows = ceil(thisdata->tupleFract * totalrows);
955 :
956 : /*
957 : * Now we can compute the statistics for the expression columns.
6993 tgl 958 ECB : */
6993 tgl 959 GIC 45 : if (numindexrows > 0)
6993 tgl 960 ECB : {
6993 tgl 961 CBC 42 : MemoryContextSwitchTo(col_context);
6993 tgl 962 GIC 67 : for (i = 0; i < attr_cnt; i++)
6993 tgl 963 ECB : {
6993 tgl 964 GIC 25 : VacAttrStats *stats = thisdata->vacattrstats[i];
6993 tgl 965 ECB :
6993 tgl 966 CBC 25 : stats->exprvals = exprvals + i;
967 25 : stats->exprnulls = exprnulls + i;
968 25 : stats->rowstride = attr_cnt;
2040 peter_e 969 GIC 25 : stats->compute_stats(stats,
970 : ind_fetch_func,
971 : numindexrows,
972 : totalindexrows);
4825 rhaas 973 ECB :
6993 tgl 974 GIC 25 : MemoryContextResetAndDeleteChildren(col_context);
975 : }
976 : }
977 :
6993 tgl 978 ECB : /* And clean up */
6993 tgl 979 GIC 45 : MemoryContextSwitchTo(ind_context);
6993 tgl 980 ECB :
6598 tgl 981 CBC 45 : ExecDropSingleTupleTableSlot(slot);
6993 982 45 : FreeExecutorState(estate);
6993 tgl 983 GIC 45 : MemoryContextResetAndDeleteChildren(ind_context);
984 : }
6993 tgl 985 ECB :
6993 tgl 986 CBC 11561 : MemoryContextSwitchTo(old_context);
987 11561 : MemoryContextDelete(ind_context);
6993 tgl 988 GIC 11561 : }
989 :
990 : /*
991 : * examine_attribute -- pre-analysis of a single column
992 : *
993 : * Determine whether the column is analyzable; if so, create and initialize
994 : * a VacAttrStats struct for it. If not, return NULL.
995 : *
996 : * If index_expr isn't NULL, then we're trying to analyze an expression index,
997 : * and index_expr is the expression tree representing the column's data.
998 : */
8007 tgl 999 ECB : static VacAttrStats *
4634 tgl 1000 GIC 197077 : examine_attribute(Relation onerel, int attnum, Node *index_expr)
8007 tgl 1001 ECB : {
2058 andres 1002 GIC 197077 : Form_pg_attribute attr = TupleDescAttr(onerel->rd_att, attnum - 1);
1003 : HeapTuple typtuple;
1004 : VacAttrStats *stats;
1005 : int i;
1006 : bool ok;
1007 :
6996 tgl 1008 ECB : /* Never analyze dropped columns */
7555 tgl 1009 CBC 197077 : if (attr->attisdropped)
7555 tgl 1010 GIC 3 : return NULL;
1011 :
8007 tgl 1012 ECB : /* Don't analyze column if user has specified not to */
7557 tgl 1013 CBC 197074 : if (attr->attstattarget == 0)
8007 tgl 1014 GIC 3 : return NULL;
1015 :
1016 : /*
1017 : * Create the VacAttrStats struct. Note that we only have a copy of the
1018 : * fixed fields of the pg_attribute tuple.
8007 tgl 1019 ECB : */
7452 bruce 1020 CBC 197071 : stats = (VacAttrStats *) palloc0(sizeof(VacAttrStats));
5190 tgl 1021 197071 : stats->attr = (Form_pg_attribute) palloc(ATTRIBUTE_FIXED_PART_SIZE);
5190 tgl 1022 GIC 197071 : memcpy(stats->attr, attr, ATTRIBUTE_FIXED_PART_SIZE);
1023 :
1024 : /*
1025 : * When analyzing an expression index, believe the expression tree's type
1026 : * not the column datatype --- the latter might be the opckeytype storage
1027 : * type of the opclass, which is not interesting for our purposes. (Note:
1028 : * if we did anything with non-expression index columns, we'd need to
1029 : * figure out where to get the correct type info from, but for now that's
1030 : * not a problem.) It's not clear whether anyone will care about the
1031 : * typmod, but we store that too just in case.
4634 tgl 1032 ECB : */
4634 tgl 1033 GIC 197071 : if (index_expr)
4634 tgl 1034 ECB : {
4634 tgl 1035 CBC 31 : stats->attrtypid = exprType(index_expr);
4634 tgl 1036 GIC 31 : stats->attrtypmod = exprTypmod(index_expr);
1037 :
1038 : /*
1039 : * If a collation has been specified for the index column, use that in
1040 : * preference to anything else; but if not, fall back to whatever we
1041 : * can get from the expression.
1577 tgl 1042 ECB : */
1577 tgl 1043 CBC 31 : if (OidIsValid(onerel->rd_indcollation[attnum - 1]))
1577 tgl 1044 GIC 6 : stats->attrcollid = onerel->rd_indcollation[attnum - 1];
1577 tgl 1045 ECB : else
1577 tgl 1046 GIC 25 : stats->attrcollid = exprCollation(index_expr);
1047 : }
1048 : else
4634 tgl 1049 ECB : {
4634 tgl 1050 CBC 197040 : stats->attrtypid = attr->atttypid;
1051 197040 : stats->attrtypmod = attr->atttypmod;
1577 tgl 1052 GIC 197040 : stats->attrcollid = attr->attcollation;
1053 : }
4634 tgl 1054 ECB :
4233 tgl 1055 GIC 197071 : typtuple = SearchSysCacheCopy1(TYPEOID,
4233 tgl 1056 ECB : ObjectIdGetDatum(stats->attrtypid));
8007 tgl 1057 GBC 197071 : if (!HeapTupleIsValid(typtuple))
4634 tgl 1058 LBC 0 : elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
4233 tgl 1059 CBC 197071 : stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
6996 1060 197071 : stats->anl_context = anl_context;
6996 tgl 1061 GIC 197071 : stats->tupattnum = attnum;
1062 :
1063 : /*
1064 : * The fields describing the stats->stavalues[n] element types default to
1065 : * the type of the data being analyzed, but the type-specific typanalyze
1066 : * function can change them if it wants to store something else.
5395 heikki.linnakangas 1067 ECB : */
5395 heikki.linnakangas 1068 GIC 1182426 : for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
5395 heikki.linnakangas 1069 ECB : {
4634 tgl 1070 CBC 985355 : stats->statypid[i] = stats->attrtypid;
5395 heikki.linnakangas 1071 985355 : stats->statyplen[i] = stats->attrtype->typlen;
1072 985355 : stats->statypbyval[i] = stats->attrtype->typbyval;
5395 heikki.linnakangas 1073 GIC 985355 : stats->statypalign[i] = stats->attrtype->typalign;
1074 : }
1075 :
1076 : /*
1077 : * Call the type-specific typanalyze function. If none is specified, use
1078 : * std_typanalyze().
8007 tgl 1079 ECB : */
6996 tgl 1080 CBC 197071 : if (OidIsValid(stats->attrtype->typanalyze))
6996 tgl 1081 GIC 14103 : ok = DatumGetBool(OidFunctionCall1(stats->attrtype->typanalyze,
1082 : PointerGetDatum(stats)));
8007 tgl 1083 ECB : else
6996 tgl 1084 GIC 182968 : ok = std_typanalyze(stats);
6996 tgl 1085 ECB :
6996 tgl 1086 GIC 197071 : if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
8007 tgl 1087 EUB : {
4233 tgl 1088 UBC 0 : heap_freetuple(typtuple);
6996 1089 0 : pfree(stats->attr);
1090 0 : pfree(stats);
6996 tgl 1091 UIC 0 : return NULL;
1092 : }
8007 tgl 1093 ECB :
8007 tgl 1094 GIC 197071 : return stats;
1095 : }
1096 :
1097 : /*
1098 : * acquire_sample_rows -- acquire a random sample of rows from the table
1099 : *
1100 : * Selected rows are returned in the caller-allocated array rows[], which
1101 : * must have at least targrows entries.
1102 : * The actual number of rows selected is returned as the function result.
1103 : * We also estimate the total numbers of live and dead rows in the table,
1104 : * and return them into *totalrows and *totaldeadrows, respectively.
1105 : *
1106 : * The returned list of tuples is in order by physical position in the table.
1107 : * (We will rely on this later to derive correlation estimates.)
1108 : *
1109 : * As of May 2004 we use a new two-stage method: Stage one selects up
1110 : * to targrows random blocks (or all blocks, if there aren't so many).
1111 : * Stage two scans these blocks and uses the Vitter algorithm to create
1112 : * a random sample of targrows rows (or less, if there are less in the
1113 : * sample of blocks). The two stages are executed simultaneously: each
1114 : * block is processed as soon as stage one returns its number and while
1115 : * the rows are read stage two controls which ones are to be inserted
1116 : * into the sample.
1117 : *
1118 : * Although every row has an equal chance of ending up in the final
1119 : * sample, this sampling method is not perfect: not every possible
1120 : * sample has an equal chance of being selected. For large relations
1121 : * the number of different blocks represented by the sample tends to be
1122 : * too small. We can live with that for now. Improvements are welcome.
1123 : *
1124 : * An important property of this sampling method is that because we do
1125 : * look at a statistically unbiased set of blocks, we should get
1126 : * unbiased estimates of the average numbers of live and dead rows per
1127 : * block. The previous sampling method put too much credence in the row
1128 : * density near the start of the table.
1129 : */
8007 tgl 1130 ECB : static int
4020 tgl 1131 GIC 24256 : acquire_sample_rows(Relation onerel, int elevel,
1132 : HeapTuple *rows, int targrows,
1133 : double *totalrows, double *totaldeadrows)
8007 tgl 1134 ECB : {
5484 tgl 1135 CBC 24256 : int numrows = 0; /* # rows now in reservoir */
5050 bruce 1136 24256 : double samplerows = 0; /* total # rows collected */
5484 tgl 1137 24256 : double liverows = 0; /* # live rows seen */
6478 1138 24256 : double deadrows = 0; /* # dead rows seen */
6797 bruce 1139 GIC 24256 : double rowstoskip = -1; /* -1 means not set yet */
1140 : uint32 randseed; /* Seed for block sampler(s) */
1141 : BlockNumber totalblocks;
1142 : TransactionId OldestXmin;
1143 : BlockSamplerData bs;
1144 : ReservoirStateData rstate;
1145 : TupleTableSlot *slot;
1146 : TableScanDesc scan;
1180 alvherre 1147 ECB : BlockNumber nblocks;
1180 alvherre 1148 GIC 24256 : BlockNumber blksdone = 0;
754 sfrost 1149 ECB : #ifdef USE_PREFETCH
754 sfrost 1150 GIC 24256 : int prefetch_maximum = 0; /* blocks to prefetch if enabled */
1151 : BlockSamplerData prefetch_bs;
1152 : #endif
8007 tgl 1153 ECB :
4849 tgl 1154 GIC 24256 : Assert(targrows > 0);
7836 bruce 1155 ECB :
6895 tgl 1156 GIC 24256 : totalblocks = RelationGetNumberOfBlocks(onerel);
1157 :
5484 tgl 1158 ECB : /* Need a cutoff xmin for HeapTupleSatisfiesVacuum */
970 andres 1159 GIC 24256 : OldestXmin = GetOldestNonRemovableTransactionId(onerel);
1160 :
6895 tgl 1161 ECB : /* Prepare for sampling block numbers */
497 tgl 1162 CBC 24256 : randseed = pg_prng_uint32(&pg_global_prng_state);
754 sfrost 1163 GIC 24256 : nblocks = BlockSampler_Init(&bs, totalblocks, targrows, randseed);
1164 :
754 sfrost 1165 ECB : #ifdef USE_PREFETCH
590 sfrost 1166 GIC 24256 : prefetch_maximum = get_tablespace_maintenance_io_concurrency(onerel->rd_rel->reltablespace);
754 sfrost 1167 ECB : /* Create another BlockSampler, using the same seed, for prefetching */
754 sfrost 1168 CBC 24256 : if (prefetch_maximum)
754 sfrost 1169 GIC 24256 : (void) BlockSampler_Init(&prefetch_bs, totalblocks, targrows, randseed);
1170 : #endif
1171 :
1180 alvherre 1172 ECB : /* Report sampling block numbers */
1180 alvherre 1173 GIC 24256 : pgstat_progress_update_param(PROGRESS_ANALYZE_BLOCKS_TOTAL,
1174 : nblocks);
1175 :
6895 tgl 1176 ECB : /* Prepare for sampling rows */
2886 simon 1177 GIC 24256 : reservoir_init_selection_state(&rstate, targrows);
6895 tgl 1178 ECB :
1471 andres 1179 CBC 24256 : scan = table_beginscan_analyze(onerel);
1471 andres 1180 GIC 24256 : slot = table_slot_create(onerel, NULL);
1181 :
1182 : #ifdef USE_PREFETCH
1183 :
1184 : /*
1185 : * If we are doing prefetching, then go ahead and tell the kernel about
1186 : * the first set of pages we are going to want. This also moves our
1187 : * iterator out ahead of the main one being used, where we will keep it so
1188 : * that we're always pre-fetching out prefetch_maximum number of blocks
1189 : * ahead.
754 sfrost 1190 ECB : */
754 sfrost 1191 GIC 24256 : if (prefetch_maximum)
754 sfrost 1192 ECB : {
754 sfrost 1193 GIC 80832 : for (int i = 0; i < prefetch_maximum; i++)
1194 : {
1195 : BlockNumber prefetch_block;
754 sfrost 1196 ECB :
754 sfrost 1197 CBC 77493 : if (!BlockSampler_HasMore(&prefetch_bs))
754 sfrost 1198 GIC 20917 : break;
754 sfrost 1199 ECB :
754 sfrost 1200 CBC 56576 : prefetch_block = BlockSampler_Next(&prefetch_bs);
754 sfrost 1201 GIC 56576 : PrefetchBuffer(scan->rs_rd, MAIN_FORKNUM, prefetch_block);
1202 : }
1203 : }
1204 : #endif
1205 :
6895 tgl 1206 ECB : /* Outer loop over blocks to sample */
6895 tgl 1207 GIC 171614 : while (BlockSampler_HasMore(&bs))
1208 : {
754 sfrost 1209 ECB : bool block_accepted;
6895 tgl 1210 GIC 147358 : BlockNumber targblock = BlockSampler_Next(&bs);
754 sfrost 1211 ECB : #ifdef USE_PREFETCH
754 sfrost 1212 GIC 147358 : BlockNumber prefetch_targblock = InvalidBlockNumber;
1213 :
1214 : /*
1215 : * Make sure that every time the main BlockSampler is moved forward
1216 : * that our prefetch BlockSampler also gets moved forward, so that we
1217 : * always stay out ahead.
754 sfrost 1218 ECB : */
754 sfrost 1219 CBC 147358 : if (prefetch_maximum && BlockSampler_HasMore(&prefetch_bs))
754 sfrost 1220 GIC 90782 : prefetch_targblock = BlockSampler_Next(&prefetch_bs);
1221 : #endif
8007 tgl 1222 ECB :
6998 tgl 1223 GIC 147358 : vacuum_delay_point();
7763 tgl 1224 ECB :
754 sfrost 1225 GIC 147358 : block_accepted = table_scan_analyze_next_block(scan, targblock, vac_strategy);
1226 :
1227 : #ifdef USE_PREFETCH
1228 :
1229 : /*
1230 : * When pre-fetching, after we get a block, tell the kernel about the
1231 : * next one we will want, if there's any left.
1232 : *
1233 : * We want to do this even if the table_scan_analyze_next_block() call
1234 : * above decides against analyzing the block it picked.
754 sfrost 1235 ECB : */
754 sfrost 1236 CBC 147358 : if (prefetch_maximum && prefetch_targblock != InvalidBlockNumber)
754 sfrost 1237 GIC 90782 : PrefetchBuffer(scan->rs_rd, MAIN_FORKNUM, prefetch_targblock);
1238 : #endif
1239 :
1240 : /*
1241 : * Don't analyze if table_scan_analyze_next_block() indicated this
1242 : * block is unsuitable for analyzing.
754 sfrost 1243 ECB : */
754 sfrost 1244 GBC 147358 : if (!block_accepted)
1471 andres 1245 UIC 0 : continue;
5484 tgl 1246 ECB :
1471 andres 1247 GIC 10303246 : while (table_scan_analyze_next_tuple(scan, OldestXmin, &liverows, &deadrows, slot))
1248 : {
1249 : /*
1250 : * The first targrows sample rows are simply copied into the
1251 : * reservoir. Then we start replacing tuples in the sample until
1252 : * we reach the end of the relation. This algorithm is from Jeff
1253 : * Vitter's paper (see full citation in utils/misc/sampling.c). It
1254 : * works by repeatedly computing the number of tuples to skip
1255 : * before selecting a tuple, which replaces a randomly chosen
1256 : * element of the reservoir (current set of tuples). At all times
1257 : * the reservoir is a true random sample of the tuples we've
1258 : * passed over so far, so when we fall off the end of the relation
1259 : * we're done.
5484 tgl 1260 ECB : */
1471 andres 1261 CBC 10155888 : if (numrows < targrows)
1471 andres 1262 GIC 10029743 : rows[numrows++] = ExecCopySlotHeapTuple(slot);
1263 : else
1264 : {
1265 : /*
1266 : * t in Vitter's paper is the number of records already
1267 : * processed. If we need to compute a new S value, we must
1268 : * use the not-yet-incremented value of samplerows as t.
8007 tgl 1269 ECB : */
1471 andres 1270 CBC 126145 : if (rowstoskip < 0)
1471 andres 1271 GIC 57678 : rowstoskip = reservoir_get_next_S(&rstate, samplerows, targrows);
1471 andres 1272 ECB :
1471 andres 1273 GIC 126145 : if (rowstoskip <= 0)
1274 : {
1275 : /*
1276 : * Found a suitable tuple, so save it, replacing one old
1277 : * tuple at random
6895 tgl 1278 ECB : */
497 tgl 1279 GIC 57652 : int k = (int) (targrows * sampler_random_fract(&rstate.randstate));
6895 tgl 1280 ECB :
1471 andres 1281 CBC 57652 : Assert(k >= 0 && k < targrows);
1282 57652 : heap_freetuple(rows[k]);
1471 andres 1283 GIC 57652 : rows[k] = ExecCopySlotHeapTuple(slot);
1284 : }
6895 tgl 1285 ECB :
1471 andres 1286 GIC 126145 : rowstoskip -= 1;
1287 : }
6895 tgl 1288 ECB :
1471 andres 1289 GIC 10155888 : samplerows += 1;
1290 : }
1180 alvherre 1291 ECB :
1180 alvherre 1292 GIC 147358 : pgstat_progress_update_param(PROGRESS_ANALYZE_BLOCKS_DONE,
1293 : ++blksdone);
1294 : }
8350 bruce 1295 ECB :
1471 andres 1296 CBC 24256 : ExecDropSingleTupleTableSlot(slot);
1471 andres 1297 GIC 24256 : table_endscan(scan);
1298 :
1299 : /*
1300 : * If we didn't find as many tuples as we wanted then we're done. No sort
1301 : * is needed, since they're already in order.
1302 : *
1303 : * Otherwise we need to sort the collected tuples by position
1304 : * (itempointer). It's not worth worrying about corner cases where the
1305 : * tuples are already sorted.
8007 tgl 1306 ECB : */
6895 tgl 1307 CBC 24256 : if (numrows == targrows)
61 peter 1308 GNC 79 : qsort_interruptible(rows, numrows, sizeof(HeapTuple),
1309 : compare_rows, NULL);
1310 :
1311 : /*
1312 : * Estimate total numbers of live and dead rows in relation, extrapolating
1313 : * on the assumption that the average tuple density in pages we didn't
1314 : * scan is the same as in the pages we did scan. Since what we scanned is
1315 : * a random sample of the pages in the relation, this should be a good
1316 : * assumption.
8007 tgl 1317 ECB : */
6895 tgl 1318 GIC 24256 : if (bs.m > 0)
1853 tgl 1319 ECB : {
1853 tgl 1320 CBC 14708 : *totalrows = floor((liverows / bs.m) * totalblocks + 0.5);
4332 tgl 1321 GIC 14708 : *totaldeadrows = floor((deadrows / bs.m) * totalblocks + 0.5);
1322 : }
1323 : else
1853 tgl 1324 ECB : {
1853 tgl 1325 CBC 9548 : *totalrows = 0.0;
6478 tgl 1326 GIC 9548 : *totaldeadrows = 0.0;
1327 : }
1328 :
1329 : /*
1330 : * Emit some interesting relation info
7166 bruce 1331 ECB : */
7150 tgl 1332 GIC 24256 : ereport(elevel,
1333 : (errmsg("\"%s\": scanned %d of %u pages, "
1334 : "containing %.0f live rows and %.0f dead rows; "
1335 : "%d rows in sample, %.0f estimated total rows",
1336 : RelationGetRelationName(onerel),
1337 : bs.m, totalblocks,
1338 : liverows, deadrows,
1339 : numrows, *totalrows)));
7166 bruce 1340 ECB :
8007 tgl 1341 GIC 24256 : return numrows;
1342 : }
1343 :
1344 : /*
1345 : * Comparator for sorting rows[] array
1346 : */
6996 tgl 1347 ECB : static int
271 tgl 1348 GIC 1958594 : compare_rows(const void *a, const void *b, void *arg)
6996 tgl 1349 ECB : {
4228 peter_e 1350 CBC 1958594 : HeapTuple ha = *(const HeapTuple *) a;
1351 1958594 : HeapTuple hb = *(const HeapTuple *) b;
6996 tgl 1352 1958594 : BlockNumber ba = ItemPointerGetBlockNumber(&ha->t_self);
1353 1958594 : OffsetNumber oa = ItemPointerGetOffsetNumber(&ha->t_self);
1354 1958594 : BlockNumber bb = ItemPointerGetBlockNumber(&hb->t_self);
6996 tgl 1355 GIC 1958594 : OffsetNumber ob = ItemPointerGetOffsetNumber(&hb->t_self);
6996 tgl 1356 ECB :
6996 tgl 1357 CBC 1958594 : if (ba < bb)
1358 432127 : return -1;
1359 1526467 : if (ba > bb)
1360 441265 : return 1;
1361 1085202 : if (oa < ob)
1362 711024 : return -1;
1363 374178 : if (oa > ob)
6996 tgl 1364 GBC 374178 : return 1;
6996 tgl 1365 UIC 0 : return 0;
1366 : }
1367 :
1368 :
1369 : /*
1370 : * acquire_inherited_sample_rows -- acquire sample rows from inheritance tree
1371 : *
1372 : * This has the same API as acquire_sample_rows, except that rows are
1373 : * collected from all inheritance children as well as the specified table.
1374 : * We fail and return zero if there are no inheritance children, or if all
1375 : * children are foreign tables that don't support ANALYZE.
1376 : */
4849 tgl 1377 ECB : static int
4020 tgl 1378 GIC 339 : acquire_inherited_sample_rows(Relation onerel, int elevel,
1379 : HeapTuple *rows, int targrows,
1380 : double *totalrows, double *totaldeadrows)
1381 : {
1382 : List *tableOIDs;
1383 : Relation *rels;
1384 : AcquireSampleRowsFunc *acquirefuncs;
1385 : double *relblocks;
1386 : double totalblocks;
1387 : int numrows,
1388 : nrels,
1389 : i;
1390 : ListCell *lc;
1391 : bool has_child;
1392 :
9 tgl 1393 ECB : /* Initialize output parameters to zero now, in case we exit early */
9 tgl 1394 CBC 339 : *totalrows = 0;
9 tgl 1395 GIC 339 : *totaldeadrows = 0;
1396 :
1397 : /*
1398 : * Find all members of inheritance set. We only need AccessShareLock on
1399 : * the children.
1400 : */
4815 rhaas 1401 ECB : tableOIDs =
4815 rhaas 1402 GIC 339 : find_all_inheritors(RelationGetRelid(onerel), AccessShareLock, NULL);
1403 :
1404 : /*
1405 : * Check that there's at least one descendant, else fail. This could
1406 : * happen despite analyze_rel's relhassubclass check, if table once had a
1407 : * child but no longer does. In that case, we can clear the
1408 : * relhassubclass field so as not to make the same mistake again later.
1409 : * (This is safe because we hold ShareUpdateExclusiveLock.)
4849 tgl 1410 ECB : */
4849 tgl 1411 GIC 339 : if (list_length(tableOIDs) < 2)
1412 : {
4237 tgl 1413 EUB : /* CCI because we already updated the pg_class row in this command */
4237 tgl 1414 UBC 0 : CommandCounterIncrement();
1415 0 : SetRelationHasSubclass(RelationGetRelid(onerel), false);
3067 simon 1416 UIC 0 : ereport(elevel,
1417 : (errmsg("skipping analyze of \"%s.%s\" inheritance tree --- this inheritance tree contains no child tables",
1418 : get_namespace_name(RelationGetNamespace(onerel)),
3067 simon 1419 EUB : RelationGetRelationName(onerel))));
4849 tgl 1420 UIC 0 : return 0;
1421 : }
1422 :
1423 : /*
1424 : * Identify acquirefuncs to use, and count blocks in all the relations.
1425 : * The result could overflow BlockNumber, so we use double arithmetic.
4849 tgl 1426 ECB : */
4849 tgl 1427 GIC 339 : rels = (Relation *) palloc(list_length(tableOIDs) * sizeof(Relation));
2940 tgl 1428 ECB : acquirefuncs = (AcquireSampleRowsFunc *)
2940 tgl 1429 CBC 339 : palloc(list_length(tableOIDs) * sizeof(AcquireSampleRowsFunc));
4849 1430 339 : relblocks = (double *) palloc(list_length(tableOIDs) * sizeof(double));
1431 339 : totalblocks = 0;
1432 339 : nrels = 0;
2229 rhaas 1433 339 : has_child = false;
4849 tgl 1434 GIC 1625 : foreach(lc, tableOIDs)
4849 tgl 1435 ECB : {
4849 tgl 1436 GIC 1286 : Oid childOID = lfirst_oid(lc);
4849 tgl 1437 ECB : Relation childrel;
2940 tgl 1438 CBC 1286 : AcquireSampleRowsFunc acquirefunc = NULL;
2940 tgl 1439 GIC 1286 : BlockNumber relpages = 0;
1440 :
4849 tgl 1441 ECB : /* We already got the needed lock */
1539 andres 1442 GIC 1286 : childrel = table_open(childOID, NoLock);
1443 :
4849 tgl 1444 ECB : /* Ignore if temp table of another backend */
4849 tgl 1445 GIC 1286 : if (RELATION_IS_OTHER_TEMP(childrel))
1446 : {
4849 tgl 1447 EUB : /* ... but release the lock on it */
4849 tgl 1448 UBC 0 : Assert(childrel != onerel);
1539 andres 1449 LBC 0 : table_close(childrel, AccessShareLock);
4849 tgl 1450 GIC 346 : continue;
1451 : }
1452 :
2940 tgl 1453 ECB : /* Check table type (MATVIEW can't happen, but might as well allow) */
2940 tgl 1454 CBC 1286 : if (childrel->rd_rel->relkind == RELKIND_RELATION ||
2229 rhaas 1455 GIC 361 : childrel->rd_rel->relkind == RELKIND_MATVIEW)
1456 : {
2940 tgl 1457 ECB : /* Regular table, so use the regular row acquisition function */
2940 tgl 1458 CBC 925 : acquirefunc = acquire_sample_rows;
2940 tgl 1459 GIC 925 : relpages = RelationGetNumberOfBlocks(childrel);
2940 tgl 1460 ECB : }
2940 tgl 1461 GIC 361 : else if (childrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1462 : {
1463 : /*
1464 : * For a foreign table, call the FDW's hook function to see
1465 : * whether it supports analysis.
1466 : */
2940 tgl 1467 ECB : FdwRoutine *fdwroutine;
2940 tgl 1468 GIC 15 : bool ok = false;
2940 tgl 1469 ECB :
2940 tgl 1470 GIC 15 : fdwroutine = GetFdwRoutineForRelation(childrel, false);
2940 tgl 1471 ECB :
2940 tgl 1472 CBC 15 : if (fdwroutine->AnalyzeForeignTable != NULL)
2940 tgl 1473 GIC 15 : ok = fdwroutine->AnalyzeForeignTable(childrel,
1474 : &acquirefunc,
1475 : &relpages);
2940 tgl 1476 ECB :
2940 tgl 1477 GIC 15 : if (!ok)
1478 : {
2940 tgl 1479 EUB : /* ignore, but release the lock on it */
2940 tgl 1480 UBC 0 : Assert(childrel != onerel);
1539 andres 1481 0 : table_close(childrel, AccessShareLock);
2940 tgl 1482 UIC 0 : continue;
1483 : }
1484 : }
1485 : else
1486 : {
1487 : /*
1488 : * ignore, but release the lock on it. don't try to unlock the
1489 : * passed-in relation
2229 rhaas 1490 ECB : */
2224 rhaas 1491 CBC 346 : Assert(childrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
2229 1492 346 : if (childrel != onerel)
1539 andres 1493 GIC 33 : table_close(childrel, AccessShareLock);
2224 rhaas 1494 ECB : else
1539 andres 1495 CBC 313 : table_close(childrel, NoLock);
2940 tgl 1496 GIC 346 : continue;
1497 : }
1498 :
2940 tgl 1499 ECB : /* OK, we'll process this child */
2229 rhaas 1500 CBC 940 : has_child = true;
4849 tgl 1501 940 : rels[nrels] = childrel;
2940 1502 940 : acquirefuncs[nrels] = acquirefunc;
1503 940 : relblocks[nrels] = (double) relpages;
1504 940 : totalblocks += (double) relpages;
4849 tgl 1505 GIC 940 : nrels++;
1506 : }
1507 :
1508 : /*
1509 : * If we don't have at least one child table to consider, fail. If the
1510 : * relation is a partitioned table, it's not counted as a child table.
2940 tgl 1511 ECB : */
2229 rhaas 1512 GIC 339 : if (!has_child)
2940 tgl 1513 EUB : {
2940 tgl 1514 UIC 0 : ereport(elevel,
1515 : (errmsg("skipping analyze of \"%s.%s\" inheritance tree --- this inheritance tree contains no analyzable child tables",
1516 : get_namespace_name(RelationGetNamespace(onerel)),
2940 tgl 1517 EUB : RelationGetRelationName(onerel))));
2940 tgl 1518 UIC 0 : return 0;
1519 : }
1520 :
1521 : /*
1522 : * Now sample rows from each relation, proportionally to its fraction of
1523 : * the total block count. (This might be less than desirable if the child
1524 : * rels have radically different free-space percentages, but it's not
1525 : * clear that it's worth working harder.)
4849 tgl 1526 ECB : */
1180 alvherre 1527 GIC 339 : pgstat_progress_update_param(PROGRESS_ANALYZE_CHILD_TABLES_TOTAL,
1180 alvherre 1528 ECB : nrels);
4849 tgl 1529 CBC 339 : numrows = 0;
4849 tgl 1530 GIC 1279 : for (i = 0; i < nrels; i++)
4849 tgl 1531 ECB : {
4849 tgl 1532 CBC 940 : Relation childrel = rels[i];
2940 1533 940 : AcquireSampleRowsFunc acquirefunc = acquirefuncs[i];
4849 tgl 1534 GIC 940 : double childblocks = relblocks[i];
4849 tgl 1535 ECB :
1180 alvherre 1536 CBC 940 : pgstat_progress_update_param(PROGRESS_ANALYZE_CURRENT_CHILD_TABLE_RELID,
1180 alvherre 1537 GIC 940 : RelationGetRelid(childrel));
1180 alvherre 1538 ECB :
4849 tgl 1539 GIC 940 : if (childblocks > 0)
1540 : {
1541 : int childtargrows;
4849 tgl 1542 ECB :
4849 tgl 1543 GIC 875 : childtargrows = (int) rint(targrows * childblocks / totalblocks);
4849 tgl 1544 ECB : /* Make sure we don't overrun due to roundoff error */
4849 tgl 1545 CBC 875 : childtargrows = Min(childtargrows, targrows - numrows);
4849 tgl 1546 GIC 875 : if (childtargrows > 0)
1547 : {
1548 : int childrows;
1549 : double trows,
1550 : tdrows;
1551 :
4849 tgl 1552 ECB : /* Fetch a random sample of the child's rows */
2940 tgl 1553 CBC 875 : childrows = (*acquirefunc) (childrel, elevel,
2940 tgl 1554 GIC 875 : rows + numrows, childtargrows,
1555 : &trows, &tdrows);
1556 :
4849 tgl 1557 ECB : /* We may need to convert from child's rowtype to parent's */
4849 tgl 1558 CBC 875 : if (childrows > 0 &&
4849 tgl 1559 GIC 875 : !equalTupleDescs(RelationGetDescr(childrel),
1560 : RelationGetDescr(onerel)))
1561 : {
1562 : TupleConversionMap *map;
4849 tgl 1563 ECB :
4849 tgl 1564 GIC 849 : map = convert_tuples_by_name(RelationGetDescr(childrel),
1314 alvherre 1565 ECB : RelationGetDescr(onerel));
4849 tgl 1566 GIC 849 : if (map != NULL)
1567 : {
1568 : int j;
4849 tgl 1569 ECB :
4849 tgl 1570 GIC 53023 : for (j = 0; j < childrows; j++)
1571 : {
1572 : HeapTuple newtup;
4849 tgl 1573 ECB :
1650 andres 1574 CBC 52975 : newtup = execute_attr_map_tuple(rows[numrows + j], map);
4849 tgl 1575 52975 : heap_freetuple(rows[numrows + j]);
4849 tgl 1576 GIC 52975 : rows[numrows + j] = newtup;
4849 tgl 1577 ECB : }
4849 tgl 1578 GIC 48 : free_conversion_map(map);
1579 : }
1580 : }
1581 :
4849 tgl 1582 ECB : /* And add to counts */
4849 tgl 1583 CBC 875 : numrows += childrows;
1584 875 : *totalrows += trows;
4849 tgl 1585 GIC 875 : *totaldeadrows += tdrows;
1586 : }
1587 : }
1588 :
1589 : /*
1590 : * Note: we cannot release the child-table locks, since we may have
1591 : * pointers to their TOAST tables in the sampled rows.
4849 tgl 1592 ECB : */
1539 andres 1593 CBC 940 : table_close(childrel, NoLock);
1180 alvherre 1594 940 : pgstat_progress_update_param(PROGRESS_ANALYZE_CHILD_TABLES_DONE,
1180 alvherre 1595 GIC 940 : i + 1);
1596 : }
4849 tgl 1597 ECB :
4849 tgl 1598 GIC 339 : return numrows;
1599 : }
1600 :
1601 :
1602 : /*
1603 : * update_attstats() -- update attribute statistics for one relation
1604 : *
1605 : * Statistics are stored in several places: the pg_class row for the
1606 : * relation has stats about the whole relation, and there is a
1607 : * pg_statistic row for each (non-system) attribute that has ever
1608 : * been analyzed. The pg_class values are updated by VACUUM, not here.
1609 : *
1610 : * pg_statistic rows are just added or updated normally. This means
1611 : * that pg_statistic will probably contain some deleted rows at the
1612 : * completion of a vacuum cycle, unless it happens to get vacuumed last.
1613 : *
1614 : * To keep things simple, we punt for pg_statistic, and don't try
1615 : * to compute or store rows for pg_statistic itself in pg_statistic.
1616 : * This could possibly be made to work, but it's not worth the trouble.
1617 : * Note analyze_rel() has seen to it that we won't come here when
1618 : * vacuuming pg_statistic itself.
1619 : *
1620 : * Note: there would be a race condition here if two backends could
1621 : * ANALYZE the same table concurrently. Presently, we lock that out
1622 : * by taking a self-exclusive lock on the relation in analyze_rel().
1623 : */
6996 tgl 1624 ECB : static void
4849 tgl 1625 GIC 37909 : update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
1626 : {
1627 : Relation sd;
6996 tgl 1628 ECB : int attno;
144 michael 1629 GNC 37909 : CatalogIndexState indstate = NULL;
1630 :
6993 tgl 1631 CBC 37909 : if (natts <= 0)
1632 23706 : return; /* nothing to do */
1633 :
1539 andres 1634 14203 : sd = table_open(StatisticRelationId, RowExclusiveLock);
1635 :
6996 tgl 1636 153192 : for (attno = 0; attno < natts; attno++)
1637 : {
1638 138989 : VacAttrStats *stats = vacattrstats[attno];
1639 : HeapTuple stup,
1640 : oldtup;
1641 : int i,
1642 : k,
1643 : n;
1644 : Datum values[Natts_pg_statistic];
1645 : bool nulls[Natts_pg_statistic];
1646 : bool replaces[Natts_pg_statistic];
1647 :
1648 : /* Ignore attr if we weren't able to collect stats */
1649 138989 : if (!stats->stats_valid)
1650 3 : continue;
1651 :
1652 : /*
1653 : * Construct a new pg_statistic tuple
1654 : */
1655 4447552 : for (i = 0; i < Natts_pg_statistic; ++i)
1656 : {
5271 1657 4308566 : nulls[i] = false;
1658 4308566 : replaces[i] = true;
1659 : }
1660 :
4315 1661 138986 : values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(relid);
1662 138986 : values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(stats->attr->attnum);
1663 138986 : values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(inh);
1664 138986 : values[Anum_pg_statistic_stanullfrac - 1] = Float4GetDatum(stats->stanullfrac);
1665 138986 : values[Anum_pg_statistic_stawidth - 1] = Int32GetDatum(stats->stawidth);
1666 138986 : values[Anum_pg_statistic_stadistinct - 1] = Float4GetDatum(stats->stadistinct);
1667 138986 : i = Anum_pg_statistic_stakind1 - 1;
6996 1668 833916 : for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
1669 : {
2118 1670 694930 : values[i++] = Int16GetDatum(stats->stakind[k]); /* stakindN */
1671 : }
4315 1672 138986 : i = Anum_pg_statistic_staop1 - 1;
6996 1673 833916 : for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
1674 : {
1675 694930 : values[i++] = ObjectIdGetDatum(stats->staop[k]); /* staopN */
1676 : }
1577 1677 138986 : i = Anum_pg_statistic_stacoll1 - 1;
1678 833916 : for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
1679 : {
1680 694930 : values[i++] = ObjectIdGetDatum(stats->stacoll[k]); /* stacollN */
1681 : }
4315 1682 138986 : i = Anum_pg_statistic_stanumbers1 - 1;
6996 1683 833916 : for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
1684 : {
1685 694930 : int nnum = stats->numnumbers[k];
1686 :
1687 694930 : if (nnum > 0)
1688 : {
1689 217669 : Datum *numdatums = (Datum *) palloc(nnum * sizeof(Datum));
1690 : ArrayType *arry;
1691 :
1692 1871243 : for (n = 0; n < nnum; n++)
1693 1653574 : numdatums[n] = Float4GetDatum(stats->stanumbers[k][n]);
282 peter 1694 GNC 217669 : arry = construct_array_builtin(numdatums, nnum, FLOAT4OID);
6996 tgl 1695 GIC 217669 : values[i++] = PointerGetDatum(arry); /* stanumbersN */
6996 tgl 1696 ECB : }
1697 : else
1698 : {
5271 tgl 1699 GIC 477261 : nulls[i] = true;
6996 tgl 1700 CBC 477261 : values[i++] = (Datum) 0;
6996 tgl 1701 ECB : }
1702 : }
4315 tgl 1703 CBC 138986 : i = Anum_pg_statistic_stavalues1 - 1;
6996 tgl 1704 GIC 833916 : for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
1705 : {
1706 694930 : if (stats->numvalues[k] > 0)
6996 tgl 1707 ECB : {
1708 : ArrayType *arry;
1709 :
6996 tgl 1710 CBC 153974 : arry = construct_array(stats->stavalues[k],
6996 tgl 1711 ECB : stats->numvalues[k],
5395 heikki.linnakangas 1712 : stats->statypid[k],
5395 heikki.linnakangas 1713 CBC 153974 : stats->statyplen[k],
5395 heikki.linnakangas 1714 GIC 153974 : stats->statypbyval[k],
1715 153974 : stats->statypalign[k]);
6996 tgl 1716 153974 : values[i++] = PointerGetDatum(arry); /* stavaluesN */
6996 tgl 1717 ECB : }
1718 : else
1719 : {
5271 tgl 1720 GIC 540956 : nulls[i] = true;
6996 1721 540956 : values[i++] = (Datum) 0;
1722 : }
6996 tgl 1723 ECB : }
1724 :
1725 : /* Is there already a pg_statistic tuple for this attribute? */
4802 rhaas 1726 GIC 277972 : oldtup = SearchSysCache3(STATRELATTINH,
1727 : ObjectIdGetDatum(relid),
1728 138986 : Int16GetDatum(stats->attr->attnum),
4802 rhaas 1729 ECB : BoolGetDatum(inh));
6996 tgl 1730 :
1731 : /* Open index information when we know we need it */
144 michael 1732 GNC 138986 : if (indstate == NULL)
1733 14200 : indstate = CatalogOpenIndexes(sd);
1734 :
6996 tgl 1735 GIC 138986 : if (HeapTupleIsValid(oldtup))
6996 tgl 1736 ECB : {
1737 : /* Yes, replace it */
5271 tgl 1738 GIC 11585 : stup = heap_modify_tuple(oldtup,
5050 bruce 1739 ECB : RelationGetDescr(sd),
1740 : values,
1741 : nulls,
1742 : replaces);
6996 tgl 1743 GIC 11585 : ReleaseSysCache(oldtup);
144 michael 1744 GNC 11585 : CatalogTupleUpdateWithInfo(sd, &stup->t_self, stup, indstate);
6996 tgl 1745 ECB : }
1746 : else
1747 : {
1748 : /* No, insert new tuple */
5271 tgl 1749 GIC 127401 : stup = heap_form_tuple(RelationGetDescr(sd), values, nulls);
144 michael 1750 GNC 127401 : CatalogTupleInsertWithInfo(sd, stup, indstate);
6996 tgl 1751 ECB : }
1752 :
6996 tgl 1753 GIC 138986 : heap_freetuple(stup);
6996 tgl 1754 ECB : }
1755 :
144 michael 1756 GNC 14203 : if (indstate != NULL)
1757 14200 : CatalogCloseIndexes(indstate);
1539 andres 1758 GIC 14203 : table_close(sd, RowExclusiveLock);
6996 tgl 1759 ECB : }
1760 :
6995 1761 : /*
1762 : * Standard fetch function for use by compute_stats subroutines.
1763 : *
1764 : * This exists to provide some insulation between compute_stats routines
1765 : * and the actual storage of the sample data.
1766 : */
1767 : static Datum
6995 tgl 1768 GIC 119864779 : std_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull)
1769 : {
1770 119864779 : int attnum = stats->tupattnum;
6995 tgl 1771 CBC 119864779 : HeapTuple tuple = stats->rows[rownum];
6995 tgl 1772 GIC 119864779 : TupleDesc tupDesc = stats->tupDesc;
6995 tgl 1773 ECB :
6995 tgl 1774 CBC 119864779 : return heap_getattr(tuple, attnum, tupDesc, isNull);
6995 tgl 1775 ECB : }
1776 :
6993 1777 : /*
1778 : * Fetch function for analyzing index expressions.
1779 : *
1780 : * We have not bothered to construct index tuples, instead the data is
1781 : * just in Datum arrays.
1782 : */
1783 : static Datum
6993 tgl 1784 GIC 77215 : ind_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull)
1785 : {
1786 : int i;
6993 tgl 1787 ECB :
1788 : /* exprvals and exprnulls are already offset for proper column */
6993 tgl 1789 GIC 77215 : i = rownum * stats->rowstride;
1790 77215 : *isNull = stats->exprnulls[i];
1791 77215 : return stats->exprvals[i];
6993 tgl 1792 ECB : }
1793 :
6996 1794 :
1795 : /*==========================================================================
1796 : *
1797 : * Code below this point represents the "standard" type-specific statistics
1798 : * analysis algorithms. This code can be replaced on a per-data-type basis
1799 : * by setting a nonzero value in pg_type.typanalyze.
1800 : *
1801 : *==========================================================================
1802 : */
1803 :
1804 :
1805 : /*
1806 : * To avoid consuming too much memory during analysis and/or too much space
1807 : * in the resulting pg_statistic rows, we ignore varlena datums that are wider
1808 : * than WIDTH_THRESHOLD (after detoasting!). This is legitimate for MCV
1809 : * and distinct-value calculations since a wide value is unlikely to be
1810 : * duplicated at all, much less be a most-common value. For the same reason,
1811 : * ignoring wide values will not affect our estimates of histogram bin
1812 : * boundaries very much.
1813 : */
1814 : #define WIDTH_THRESHOLD 1024
1815 :
1816 : #define swapInt(a,b) do {int _tmp; _tmp=a; a=b; b=_tmp;} while(0)
1817 : #define swapDatum(a,b) do {Datum _tmp; _tmp=a; a=b; b=_tmp;} while(0)
1818 :
1819 : /*
1820 : * Extra information used by the default analysis routines
1821 : */
1822 : typedef struct
1823 : {
1824 : int count; /* # of duplicates */
1825 : int first; /* values[] index of first occurrence */
1826 : } ScalarMCVItem;
1827 :
1828 : typedef struct
1829 : {
1830 : SortSupport ssup;
1831 : int *tupnoLink;
1832 : } CompareScalarsContext;
1833 :
1834 :
1835 : static void compute_trivial_stats(VacAttrStatsP stats,
1836 : AnalyzeAttrFetchFunc fetchfunc,
1837 : int samplerows,
1838 : double totalrows);
1839 : static void compute_distinct_stats(VacAttrStatsP stats,
1840 : AnalyzeAttrFetchFunc fetchfunc,
1841 : int samplerows,
1842 : double totalrows);
1843 : static void compute_scalar_stats(VacAttrStatsP stats,
1844 : AnalyzeAttrFetchFunc fetchfunc,
1845 : int samplerows,
1846 : double totalrows);
1847 : static int compare_scalars(const void *a, const void *b, void *arg);
1848 : static int compare_mcvs(const void *a, const void *b, void *arg);
1849 : static int analyze_mcv_list(int *mcv_counts,
1850 : int num_mcv,
1851 : double stadistinct,
1852 : double stanullfrac,
1853 : int samplerows,
1854 : double totalrows);
1855 :
1856 :
1857 : /*
1858 : * std_typanalyze -- the default type-specific typanalyze function
1859 : */
1860 : bool
6996 tgl 1861 GIC 197628 : std_typanalyze(VacAttrStats *stats)
1862 : {
1863 197628 : Form_pg_attribute attr = stats->attr;
5363 tgl 1864 ECB : Oid ltopr;
1865 : Oid eqopr;
6996 1866 : StdAnalyzeData *mystats;
1867 :
1868 : /* If the attstattarget column is negative, use the default value */
1869 : /* NB: it is okay to scribble on stats->attr since it's a copy */
6996 tgl 1870 GIC 197628 : if (attr->attstattarget < 0)
1871 197337 : attr->attstattarget = default_statistics_target;
1872 :
5363 tgl 1873 ECB : /* Look for default "<" and "=" operators for column's type */
4634 tgl 1874 CBC 197628 : get_sort_group_operators(stats->attrtypid,
1875 : false, false, false,
1876 : <opr, &eqopr, NULL,
4544 tgl 1877 ECB : NULL);
1878 :
1879 : /* Save the operator info for compute_stats routines */
6996 tgl 1880 GIC 197628 : mystats = (StdAnalyzeData *) palloc(sizeof(StdAnalyzeData));
1881 197628 : mystats->eqopr = eqopr;
2755 1882 197628 : mystats->eqfunc = OidIsValid(eqopr) ? get_opcode(eqopr) : InvalidOid;
6996 tgl 1883 CBC 197628 : mystats->ltopr = ltopr;
1884 197628 : stats->extra_data = mystats;
6996 tgl 1885 ECB :
1886 : /*
1887 : * Determine which standard statistics algorithm to use
1888 : */
2755 tgl 1889 GIC 197628 : if (OidIsValid(eqopr) && OidIsValid(ltopr))
1890 : {
1891 : /* Seems to be a scalar datatype */
6996 tgl 1892 CBC 191043 : stats->compute_stats = compute_scalar_stats;
1893 : /*--------------------
1894 : * The following choice of minrows is based on the paper
6996 tgl 1895 ECB : * "Random sampling for histogram construction: how much is enough?"
1896 : * by Surajit Chaudhuri, Rajeev Motwani and Vivek Narasayya, in
1897 : * Proceedings of ACM SIGMOD International Conference on Management
1898 : * of Data, 1998, Pages 436-447. Their Corollary 1 to Theorem 5
1899 : * says that for table size n, histogram size k, maximum relative
1900 : * error in bin size f, and error probability gamma, the minimum
1901 : * random sample size is
1902 : * r = 4 * k * ln(2*n/gamma) / f^2
1903 : * Taking f = 0.5, gamma = 0.01, n = 10^6 rows, we obtain
1904 : * r = 305.82 * k
1905 : * Note that because of the log function, the dependence on n is
1906 : * quite weak; even at n = 10^12, a 300*k sample gives <= 0.66
1907 : * bin size error with probability 0.99. So there's no real need to
1908 : * scale for n, which is a good thing because we don't necessarily
1909 : * know it at this point.
1910 : *--------------------
1911 : */
6996 tgl 1912 GIC 191043 : stats->minrows = 300 * attr->attstattarget;
1913 : }
2755 1914 6585 : else if (OidIsValid(eqopr))
2755 tgl 1915 ECB : {
1916 : /* We can still recognize distinct values */
2755 tgl 1917 CBC 5858 : stats->compute_stats = compute_distinct_stats;
1918 : /* Might as well use the same minrows as above */
2755 tgl 1919 GIC 5858 : stats->minrows = 300 * attr->attstattarget;
2755 tgl 1920 ECB : }
1921 : else
6996 1922 : {
1923 : /* Can't do much but the trivial stuff */
2755 tgl 1924 GIC 727 : stats->compute_stats = compute_trivial_stats;
1925 : /* Might as well use the same minrows as above */
6996 1926 727 : stats->minrows = 300 * attr->attstattarget;
6996 tgl 1927 ECB : }
1928 :
6996 tgl 1929 CBC 197628 : return true;
1930 : }
1931 :
2755 tgl 1932 ECB :
1933 : /*
1934 : * compute_trivial_stats() -- compute very basic column statistics
1935 : *
1936 : * We use this when we cannot find a hash "=" operator for the datatype.
1937 : *
1938 : * We determine the fraction of non-null rows and the average datum width.
1939 : */
1940 : static void
2755 tgl 1941 GIC 403 : compute_trivial_stats(VacAttrStatsP stats,
1942 : AnalyzeAttrFetchFunc fetchfunc,
1943 : int samplerows,
2755 tgl 1944 ECB : double totalrows)
1945 : {
1946 : int i;
2755 tgl 1947 GIC 403 : int null_cnt = 0;
1948 403 : int nonnull_cnt = 0;
1949 403 : double total_width = 0;
2755 tgl 1950 CBC 806 : bool is_varlena = (!stats->attrtype->typbyval &&
1951 403 : stats->attrtype->typlen == -1);
1952 806 : bool is_varwidth = (!stats->attrtype->typbyval &&
1953 403 : stats->attrtype->typlen < 0);
2755 tgl 1954 ECB :
2755 tgl 1955 CBC 1285815 : for (i = 0; i < samplerows; i++)
2755 tgl 1956 ECB : {
1957 : Datum value;
1958 : bool isnull;
1959 :
2755 tgl 1960 GIC 1285412 : vacuum_delay_point();
1961 :
1962 1285412 : value = fetchfunc(stats, i, &isnull);
2755 tgl 1963 ECB :
1964 : /* Check for null/nonnull */
2755 tgl 1965 CBC 1285412 : if (isnull)
1966 : {
2755 tgl 1967 GIC 1022060 : null_cnt++;
2755 tgl 1968 CBC 1022060 : continue;
1969 : }
1970 263352 : nonnull_cnt++;
2755 tgl 1971 ECB :
1972 : /*
1973 : * If it's a variable-width field, add up widths for average width
1974 : * calculation. Note that if the value is toasted, we use the toasted
1975 : * width. We don't bother with this calculation if it's a fixed-width
1976 : * type.
1977 : */
2755 tgl 1978 GIC 263352 : if (is_varlena)
1979 : {
1980 51852 : total_width += VARSIZE_ANY(DatumGetPointer(value));
2755 tgl 1981 ECB : }
2755 tgl 1982 GIC 211500 : else if (is_varwidth)
2755 tgl 1983 ECB : {
1984 : /* must be cstring */
2755 tgl 1985 LBC 0 : total_width += strlen(DatumGetCString(value)) + 1;
1986 : }
1987 : }
2755 tgl 1988 EUB :
1989 : /* We can only compute average width if we found some non-null values. */
2755 tgl 1990 GIC 403 : if (nonnull_cnt > 0)
1991 : {
1992 75 : stats->stats_valid = true;
2755 tgl 1993 ECB : /* Do the simple null-frac and width stats */
2755 tgl 1994 GIC 75 : stats->stanullfrac = (double) null_cnt / (double) samplerows;
2755 tgl 1995 CBC 75 : if (is_varwidth)
2755 tgl 1996 GIC 28 : stats->stawidth = total_width / (double) nonnull_cnt;
2755 tgl 1997 ECB : else
2755 tgl 1998 CBC 47 : stats->stawidth = stats->attrtype->typlen;
2118 1999 75 : stats->stadistinct = 0.0; /* "unknown" */
2000 : }
2755 2001 328 : else if (null_cnt > 0)
2755 tgl 2002 ECB : {
2003 : /* We found only nulls; assume the column is entirely null */
2755 tgl 2004 CBC 328 : stats->stats_valid = true;
2755 tgl 2005 GIC 328 : stats->stanullfrac = 1.0;
2006 328 : if (is_varwidth)
2755 tgl 2007 CBC 328 : stats->stawidth = 0; /* "unknown" */
2755 tgl 2008 ECB : else
2755 tgl 2009 LBC 0 : stats->stawidth = stats->attrtype->typlen;
2118 tgl 2010 CBC 328 : stats->stadistinct = 0.0; /* "unknown" */
2011 : }
2755 tgl 2012 GBC 403 : }
2755 tgl 2013 ECB :
2014 :
2015 : /*
2016 : * compute_distinct_stats() -- compute column statistics including ndistinct
2017 : *
2018 : * We use this when we can find only an "=" operator for the datatype.
2019 : *
2020 : * We determine the fraction of non-null rows, the average width, the
2021 : * most common values, and the (estimated) number of distinct values.
2022 : *
2023 : * The most common values are determined by brute force: we keep a list
2024 : * of previously seen values, ordered by number of times seen, as we scan
2025 : * the samples. A newly seen value is inserted just after the last
2026 : * multiply-seen value, causing the bottommost (oldest) singly-seen value
2027 : * to drop off the list. The accuracy of this method, and also its cost,
2028 : * depend mainly on the length of the list we are willing to keep.
2029 : */
2030 : static void
2755 tgl 2031 GIC 4238 : compute_distinct_stats(VacAttrStatsP stats,
2032 : AnalyzeAttrFetchFunc fetchfunc,
2033 : int samplerows,
2755 tgl 2034 ECB : double totalrows)
2035 : {
2036 : int i;
8007 tgl 2037 GIC 4238 : int null_cnt = 0;
2038 4238 : int nonnull_cnt = 0;
2039 4238 : int toowide_cnt = 0;
8007 tgl 2040 CBC 4238 : double total_width = 0;
4634 2041 7170 : bool is_varlena = (!stats->attrtype->typbyval &&
2042 2932 : stats->attrtype->typlen == -1);
2043 7170 : bool is_varwidth = (!stats->attrtype->typbyval &&
2044 2932 : stats->attrtype->typlen < 0);
8007 tgl 2045 ECB : FmgrInfo f_cmpeq;
2046 : typedef struct
2047 : {
2048 : Datum value;
2049 : int count;
2050 : } TrackItem;
2051 : TrackItem *track;
2052 : int track_cnt,
2053 : track_max;
8007 tgl 2054 GIC 4238 : int num_mcv = stats->attr->attstattarget;
6996 2055 4238 : StdAnalyzeData *mystats = (StdAnalyzeData *) stats->extra_data;
2056 :
7836 bruce 2057 ECB : /*
6385 2058 : * We track up to 2*n values for an n-element MCV list; but at least 10
2059 : */
8007 tgl 2060 GIC 4238 : track_max = 2 * num_mcv;
2061 4238 : if (track_max < 10)
2062 39 : track_max = 10;
8007 tgl 2063 CBC 4238 : track = (TrackItem *) palloc(track_max * sizeof(TrackItem));
2064 4238 : track_cnt = 0;
8007 tgl 2065 ECB :
6996 tgl 2066 CBC 4238 : fmgr_info(mystats->eqfunc, &f_cmpeq);
8007 tgl 2067 ECB :
6995 tgl 2068 GIC 2778529 : for (i = 0; i < samplerows; i++)
8350 bruce 2069 ECB : {
2070 : Datum value;
8349 tgl 2071 : bool isnull;
2072 : bool match;
2073 : int firstcount1,
2074 : j;
2075 :
6998 tgl 2076 GIC 2774291 : vacuum_delay_point();
2077 :
6995 2078 2774291 : value = fetchfunc(stats, i, &isnull);
8350 bruce 2079 ECB :
2080 : /* Check for null/nonnull */
8350 bruce 2081 CBC 2774291 : if (isnull)
2082 : {
8007 tgl 2083 GIC 2336018 : null_cnt++;
8349 tgl 2084 CBC 2336018 : continue;
2085 : }
8007 2086 438273 : nonnull_cnt++;
8163 tgl 2087 ECB :
2088 : /*
7533 2089 : * If it's a variable-width field, add up widths for average width
2090 : * calculation. Note that if the value is toasted, we use the toasted
2091 : * width. We don't bother with this calculation if it's a fixed-width
2092 : * type.
2093 : */
8007 tgl 2094 GIC 438273 : if (is_varlena)
2095 : {
5847 2096 163933 : total_width += VARSIZE_ANY(DatumGetPointer(value));
7836 bruce 2097 ECB :
2098 : /*
8007 tgl 2099 : * If the value is toasted, we want to detoast it just once to
2100 : * avoid repeated detoastings and resultant excess memory usage
2101 : * during the comparisons. Also, check to see if the value is
2102 : * excessively wide, and if so don't detoast at all --- just
2103 : * ignore the value.
2104 : */
8007 tgl 2105 GIC 163933 : if (toast_raw_datum_size(value) > WIDTH_THRESHOLD)
2106 : {
8007 tgl 2107 UIC 0 : toowide_cnt++;
8007 tgl 2108 LBC 0 : continue;
2109 : }
8007 tgl 2110 GBC 163933 : value = PointerGetDatum(PG_DETOAST_DATUM(value));
8349 tgl 2111 EUB : }
7533 tgl 2112 GIC 274340 : else if (is_varwidth)
7533 tgl 2113 ECB : {
2114 : /* must be cstring */
7533 tgl 2115 LBC 0 : total_width += strlen(DatumGetCString(value)) + 1;
2116 : }
2117 :
8007 tgl 2118 EUB : /*
2119 : * See if the value matches anything we're already tracking.
2120 : */
8007 tgl 2121 GIC 438273 : match = false;
2122 438273 : firstcount1 = track_cnt;
2123 617202 : for (j = 0; j < track_cnt; j++)
8349 tgl 2124 ECB : {
4380 tgl 2125 CBC 607051 : if (DatumGetBool(FunctionCall2Coll(&f_cmpeq,
1577 tgl 2126 ECB : stats->attrcollid,
4380 tgl 2127 GIC 607051 : value, track[j].value)))
8350 bruce 2128 ECB : {
8007 tgl 2129 GIC 428122 : match = true;
8007 tgl 2130 CBC 428122 : break;
2131 : }
2132 178929 : if (j < firstcount1 && track[j].count == 1)
2133 4979 : firstcount1 = j;
2134 : }
8349 tgl 2135 ECB :
8007 tgl 2136 CBC 438273 : if (match)
2137 : {
2138 : /* Found a match */
2139 428122 : track[j].count++;
2140 : /* This value may now need to "bubble up" in the track list */
7836 bruce 2141 GIC 432965 : while (j > 0 && track[j].count > track[j - 1].count)
8350 bruce 2142 ECB : {
7836 bruce 2143 GIC 4843 : swapDatum(track[j].value, track[j - 1].value);
7836 bruce 2144 CBC 4843 : swapInt(track[j].count, track[j - 1].count);
8007 tgl 2145 GIC 4843 : j--;
8350 bruce 2146 ECB : }
8349 tgl 2147 : }
8007 2148 : else
2149 : {
2150 : /* No match. Insert at head of count-1 list */
8007 tgl 2151 GIC 10151 : if (track_cnt < track_max)
2152 10151 : track_cnt++;
7836 bruce 2153 31864 : for (j = track_cnt - 1; j > firstcount1; j--)
8007 tgl 2154 ECB : {
7836 bruce 2155 CBC 21713 : track[j].value = track[j - 1].value;
2156 21713 : track[j].count = track[j - 1].count;
2157 : }
8007 tgl 2158 10151 : if (firstcount1 < track_cnt)
8007 tgl 2159 ECB : {
8007 tgl 2160 GIC 10151 : track[firstcount1].value = value;
8007 tgl 2161 CBC 10151 : track[firstcount1].count = 1;
2162 : }
8349 tgl 2163 ECB : }
8007 2164 : }
2165 :
2166 : /* We can only compute real stats if we found some non-null values. */
8007 tgl 2167 GIC 4238 : if (nonnull_cnt > 0)
2168 : {
2169 : int nmultiple,
7836 bruce 2170 ECB : summultiple;
2171 :
8007 tgl 2172 GIC 2959 : stats->stats_valid = true;
2173 : /* Do the simple null-frac and width stats */
6995 2174 2959 : stats->stanullfrac = (double) null_cnt / (double) samplerows;
7533 tgl 2175 CBC 2959 : if (is_varwidth)
8007 tgl 2176 GIC 1653 : stats->stawidth = total_width / (double) nonnull_cnt;
8349 tgl 2177 ECB : else
8007 tgl 2178 CBC 1306 : stats->stawidth = stats->attrtype->typlen;
8349 tgl 2179 ECB :
2180 : /* Count the number of values we found multiple times */
8007 tgl 2181 CBC 2959 : summultiple = 0;
8007 tgl 2182 GIC 10519 : for (nmultiple = 0; nmultiple < track_cnt; nmultiple++)
2183 : {
8007 tgl 2184 CBC 9181 : if (track[nmultiple].count == 1)
2185 1621 : break;
8007 tgl 2186 GIC 7560 : summultiple += track[nmultiple].count;
8349 tgl 2187 ECB : }
8007 2188 :
8007 tgl 2189 CBC 2959 : if (nmultiple == 0)
2190 : {
2191 : /*
2436 tgl 2192 ECB : * If we found no repeated non-null values, assume it's a unique
2193 : * column; but be sure to discount for any nulls we found.
2194 : */
2436 tgl 2195 GIC 607 : stats->stadistinct = -1.0 * (1.0 - stats->stanullfrac);
2196 : }
8007 2197 2352 : else if (track_cnt < track_max && toowide_cnt == 0 &&
8007 tgl 2198 ECB : nmultiple == track_cnt)
2199 : {
2200 : /*
2201 : * Our track list includes every value in the sample, and every
2202 : * value appeared more than once. Assume the column has just
2203 : * these values. (This case is meant to address columns with
2204 : * small, fixed sets of possible values, such as boolean or enum
2205 : * columns. If there are any values that appear just once in the
2206 : * sample, including too-wide values, we should assume that that's
2207 : * not what we're dealing with.)
2208 : */
8007 tgl 2209 GIC 1338 : stats->stadistinct = track_cnt;
2210 : }
2211 : else
8007 tgl 2212 ECB : {
2213 : /*----------
2214 : * Estimate the number of distinct values using the estimator
2215 : * proposed by Haas and Stokes in IBM Research Report RJ 10025:
2216 : * n*d / (n - f1 + f1*n/N)
2217 : * where f1 is the number of distinct values that occurred
2218 : * exactly once in our sample of n rows (from a total of N),
2219 : * and d is the total number of distinct values in the sample.
2220 : * This is their Duj1 estimator; the other estimators they
2221 : * recommend are considerably more complex, and are numerically
2222 : * very unstable when n is much smaller than N.
2223 : *
2224 : * In this calculation, we consider only non-nulls. We used to
2225 : * include rows with null values in the n and N counts, but that
2226 : * leads to inaccurate answers in columns with many nulls, and
2227 : * it's intuitively bogus anyway considering the desired result is
2228 : * the number of distinct non-null values.
2229 : *
2230 : * We assume (not very reliably!) that all the multiply-occurring
2231 : * values are reflected in the final track[] list, and the other
2232 : * nonnull values all appeared but once. (XXX this usually
2233 : * results in a drastic overestimate of ndistinct. Can we do
2234 : * any better?)
2235 : *----------
2236 : */
7836 bruce 2237 GIC 1014 : int f1 = nonnull_cnt - summultiple;
7720 tgl 2238 1014 : int d = f1 + nmultiple;
2564 2239 1014 : double n = samplerows - null_cnt;
2564 tgl 2240 CBC 1014 : double N = totalrows * (1.0 - stats->stanullfrac);
2564 tgl 2241 ECB : double stadistinct;
7720 2242 :
2564 2243 : /* N == 0 shouldn't happen, but just in case ... */
2564 tgl 2244 GIC 1014 : if (N > 0)
2245 1014 : stadistinct = (n * d) / ((n - f1) + f1 * n / N);
2246 : else
2564 tgl 2247 LBC 0 : stadistinct = 0;
7522 bruce 2248 ECB :
2249 : /* Clamp to sane range in case of roundoff error */
2564 tgl 2250 GBC 1014 : if (stadistinct < d)
2564 tgl 2251 GIC 329 : stadistinct = d;
2252 1014 : if (stadistinct > N)
2564 tgl 2253 LBC 0 : stadistinct = N;
2564 tgl 2254 ECB : /* And round to integer */
7720 tgl 2255 CBC 1014 : stats->stadistinct = floor(stadistinct + 0.5);
8007 tgl 2256 EUB : }
2257 :
8007 tgl 2258 ECB : /*
2259 : * If we estimated the number of distinct values at more than 10% of
2260 : * the total row count (a very arbitrary limit), then assume that
2261 : * stadistinct should scale with the row count rather than be a fixed
2262 : * value.
2263 : */
8007 tgl 2264 GIC 2959 : if (stats->stadistinct > 0.1 * totalrows)
7836 bruce 2265 389 : stats->stadistinct = -(stats->stadistinct / totalrows);
2266 :
7977 tgl 2267 ECB : /*
6385 bruce 2268 : * Decide how many values are worth storing as most-common values. If
2269 : * we are able to generate a complete MCV list (all the values in the
2270 : * sample will fit, and we think these are all the ones in the table),
2271 : * then do so. Otherwise, store only those values that are
2272 : * significantly more common than the values not in the list.
2273 : *
2274 : * Note: the first of these cases is meant to address columns with
2275 : * small, fixed sets of possible values, such as boolean or enum
2276 : * columns. If we can *completely* represent the column population by
2277 : * an MCV list that will fit into the stats target, then we should do
2278 : * so and thus provide the planner with complete information. But if
2279 : * the MCV list is not complete, it's generally worth being more
2280 : * selective, and not just filling it all the way up to the stats
2281 : * target.
2282 : */
7977 tgl 2283 GIC 2959 : if (track_cnt < track_max && toowide_cnt == 0 &&
2284 2956 : stats->stadistinct > 0 &&
2285 : track_cnt <= num_mcv)
7977 tgl 2286 ECB : {
2287 : /* Track list includes all values seen, and all will fit */
7977 tgl 2288 GIC 1948 : num_mcv = track_cnt;
2289 : }
2290 : else
7977 tgl 2291 ECB : {
2292 : int *mcv_counts;
2293 :
2294 : /* Incomplete list; decide how many values are worth keeping */
7977 tgl 2295 GIC 1011 : if (num_mcv > track_cnt)
2296 981 : num_mcv = track_cnt;
2297 :
1844 dean.a.rasheed 2298 CBC 1011 : if (num_mcv > 0)
7977 tgl 2299 ECB : {
1844 dean.a.rasheed 2300 GIC 1011 : mcv_counts = (int *) palloc(num_mcv * sizeof(int));
1844 dean.a.rasheed 2301 CBC 2545 : for (i = 0; i < num_mcv; i++)
1844 dean.a.rasheed 2302 GIC 1534 : mcv_counts[i] = track[i].count;
1844 dean.a.rasheed 2303 ECB :
1844 dean.a.rasheed 2304 CBC 1011 : num_mcv = analyze_mcv_list(mcv_counts, num_mcv,
2305 1011 : stats->stadistinct,
1844 dean.a.rasheed 2306 GIC 1011 : stats->stanullfrac,
1844 dean.a.rasheed 2307 ECB : samplerows, totalrows);
7977 tgl 2308 : }
2309 : }
2310 :
2311 : /* Generate MCV slot entry */
8007 tgl 2312 GIC 2959 : if (num_mcv > 0)
2313 : {
2314 : MemoryContext old_context;
7836 bruce 2315 ECB : Datum *mcv_values;
2316 : float4 *mcv_freqs;
2317 :
2318 : /* Must copy the target values into anl_context */
6996 tgl 2319 GIC 2955 : old_context = MemoryContextSwitchTo(stats->anl_context);
8007 2320 2955 : mcv_values = (Datum *) palloc(num_mcv * sizeof(Datum));
2321 2955 : mcv_freqs = (float4 *) palloc(num_mcv * sizeof(float4));
8007 tgl 2322 CBC 12976 : for (i = 0; i < num_mcv; i++)
8007 tgl 2323 ECB : {
8007 tgl 2324 CBC 20042 : mcv_values[i] = datumCopy(track[i].value,
4634 2325 10021 : stats->attrtype->typbyval,
4634 tgl 2326 GIC 10021 : stats->attrtype->typlen);
6995 tgl 2327 CBC 10021 : mcv_freqs[i] = (double) track[i].count / (double) samplerows;
8007 tgl 2328 ECB : }
8007 tgl 2329 CBC 2955 : MemoryContextSwitchTo(old_context);
8007 tgl 2330 ECB :
8007 tgl 2331 GIC 2955 : stats->stakind[0] = STATISTIC_KIND_MCV;
6996 tgl 2332 CBC 2955 : stats->staop[0] = mystats->eqopr;
1577 tgl 2333 GIC 2955 : stats->stacoll[0] = stats->attrcollid;
8007 tgl 2334 CBC 2955 : stats->stanumbers[0] = mcv_freqs;
2335 2955 : stats->numnumbers[0] = num_mcv;
2336 2955 : stats->stavalues[0] = mcv_values;
2337 2955 : stats->numvalues[0] = num_mcv;
5050 bruce 2338 ECB :
5395 heikki.linnakangas 2339 : /*
5050 bruce 2340 : * Accept the defaults for stats->statypid and others. They have
2341 : * been set before we were called (see vacuum.h)
2342 : */
2343 : }
2344 : }
6631 tgl 2345 GIC 1279 : else if (null_cnt > 0)
2346 : {
2347 : /* We found only nulls; assume the column is entirely null */
6631 tgl 2348 CBC 1279 : stats->stats_valid = true;
6631 tgl 2349 GIC 1279 : stats->stanullfrac = 1.0;
2350 1279 : if (is_varwidth)
6385 bruce 2351 CBC 1279 : stats->stawidth = 0; /* "unknown" */
6631 tgl 2352 ECB : else
6631 tgl 2353 LBC 0 : stats->stawidth = stats->attrtype->typlen;
2118 tgl 2354 CBC 1279 : stats->stadistinct = 0.0; /* "unknown" */
2355 : }
8007 tgl 2356 EUB :
8007 tgl 2357 ECB : /* We don't need to bother cleaning up any of our temporary palloc's */
8350 bruce 2358 GIC 4238 : }
2359 :
2360 :
8350 bruce 2361 ECB : /*
2362 : * compute_scalar_stats() -- compute column statistics
2363 : *
2364 : * We use this when we can find "=" and "<" operators for the datatype.
2365 : *
2366 : * We determine the fraction of non-null rows, the average width, the
2367 : * most common values, the (estimated) number of distinct values, the
2368 : * distribution histogram, and the correlation of physical to logical order.
2369 : *
2370 : * The desired stats can be determined fairly easily after sorting the
2371 : * data values into order.
2372 : */
2373 : static void
6995 tgl 2374 GIC 134473 : compute_scalar_stats(VacAttrStatsP stats,
2375 : AnalyzeAttrFetchFunc fetchfunc,
2376 : int samplerows,
6995 tgl 2377 ECB : double totalrows)
2378 : {
2379 : int i;
8007 tgl 2380 GIC 134473 : int null_cnt = 0;
2381 134473 : int nonnull_cnt = 0;
2382 134473 : int toowide_cnt = 0;
8007 tgl 2383 CBC 134473 : double total_width = 0;
4634 2384 166233 : bool is_varlena = (!stats->attrtype->typbyval &&
2385 31760 : stats->attrtype->typlen == -1);
2386 166233 : bool is_varwidth = (!stats->attrtype->typbyval &&
2387 31760 : stats->attrtype->typlen < 0);
8007 tgl 2388 ECB : double corr_xysum;
4141 2389 : SortSupportData ssup;
8007 2390 : ScalarItem *values;
8007 tgl 2391 GIC 134473 : int values_cnt = 0;
2392 : int *tupnoLink;
2393 : ScalarMCVItem *track;
8007 tgl 2394 CBC 134473 : int track_cnt = 0;
8007 tgl 2395 GIC 134473 : int num_mcv = stats->attr->attstattarget;
7977 2396 134473 : int num_bins = stats->attr->attstattarget;
6996 tgl 2397 CBC 134473 : StdAnalyzeData *mystats = (StdAnalyzeData *) stats->extra_data;
8007 tgl 2398 ECB :
6995 tgl 2399 CBC 134473 : values = (ScalarItem *) palloc(samplerows * sizeof(ScalarItem));
2400 134473 : tupnoLink = (int *) palloc(samplerows * sizeof(int));
8007 tgl 2401 GIC 134473 : track = (ScalarMCVItem *) palloc(num_mcv * sizeof(ScalarMCVItem));
8007 tgl 2402 ECB :
4141 tgl 2403 CBC 134473 : memset(&ssup, 0, sizeof(ssup));
2404 134473 : ssup.ssup_cxt = CurrentMemoryContext;
1577 tgl 2405 GIC 134473 : ssup.ssup_collation = stats->attrcollid;
4141 tgl 2406 CBC 134473 : ssup.ssup_nulls_first = false;
2878 bruce 2407 ECB :
3002 rhaas 2408 : /*
2409 : * For now, don't perform abbreviated key conversion, because full values
2410 : * are required for MCV slot generation. Supporting that optimization
2411 : * would necessitate teaching compare_scalars() to call a tie-breaker.
2412 : */
3002 rhaas 2413 GIC 134473 : ssup.abbreviate = false;
2414 :
4141 tgl 2415 134473 : PrepareSortSupportFromOrderingOp(mystats->ltopr, &ssup);
8007 tgl 2416 ECB :
2417 : /* Initial scan to find sortable values */
6995 tgl 2418 CBC 108059039 : for (i = 0; i < samplerows; i++)
2419 : {
2420 : Datum value;
8007 tgl 2421 ECB : bool isnull;
2422 :
6998 tgl 2423 GIC 107924566 : vacuum_delay_point();
2424 :
6995 2425 107924566 : value = fetchfunc(stats, i, &isnull);
8350 bruce 2426 ECB :
2427 : /* Check for null/nonnull */
8007 tgl 2428 CBC 107924566 : if (isnull)
2429 : {
8007 tgl 2430 GIC 14078242 : null_cnt++;
8007 tgl 2431 CBC 14143383 : continue;
2432 : }
2433 93846324 : nonnull_cnt++;
8350 bruce 2434 ECB :
2435 : /*
7533 tgl 2436 : * If it's a variable-width field, add up widths for average width
2437 : * calculation. Note that if the value is toasted, we use the toasted
2438 : * width. We don't bother with this calculation if it's a fixed-width
2439 : * type.
2440 : */
8007 tgl 2441 GIC 93846324 : if (is_varlena)
2442 : {
5847 2443 8617183 : total_width += VARSIZE_ANY(DatumGetPointer(value));
7836 bruce 2444 ECB :
2445 : /*
8007 tgl 2446 : * If the value is toasted, we want to detoast it just once to
2447 : * avoid repeated detoastings and resultant excess memory usage
2448 : * during the comparisons. Also, check to see if the value is
2449 : * excessively wide, and if so don't detoast at all --- just
2450 : * ignore the value.
2451 : */
8007 tgl 2452 GIC 8617183 : if (toast_raw_datum_size(value) > WIDTH_THRESHOLD)
2453 : {
2454 65141 : toowide_cnt++;
8007 tgl 2455 CBC 65141 : continue;
2456 : }
2457 8552042 : value = PointerGetDatum(PG_DETOAST_DATUM(value));
8007 tgl 2458 ECB : }
7533 tgl 2459 GIC 85229141 : else if (is_varwidth)
7533 tgl 2460 ECB : {
2461 : /* must be cstring */
7533 tgl 2462 LBC 0 : total_width += strlen(DatumGetCString(value)) + 1;
2463 : }
2464 :
8007 tgl 2465 EUB : /* Add it to the list to be sorted */
8007 tgl 2466 GIC 93781183 : values[values_cnt].value = value;
2467 93781183 : values[values_cnt].tupno = values_cnt;
2468 93781183 : tupnoLink[values_cnt] = values_cnt;
8007 tgl 2469 CBC 93781183 : values_cnt++;
8007 tgl 2470 ECB : }
2471 :
6631 2472 : /* We can only compute real stats if we found some sortable values. */
8007 tgl 2473 GIC 134473 : if (values_cnt > 0)
2474 : {
2475 : int ndistinct, /* # distinct values in sample */
7836 bruce 2476 ECB : nmultiple, /* # that appear multiple times */
2477 : num_hist,
2478 : dups_cnt;
7836 bruce 2479 GIC 125939 : int slot_idx = 0;
2480 : CompareScalarsContext cxt;
2481 :
8007 tgl 2482 ECB : /* Sort the collected values */
4141 tgl 2483 GIC 125939 : cxt.ssup = &ssup;
6030 2484 125939 : cxt.tupnoLink = tupnoLink;
61 peter 2485 GNC 125939 : qsort_interruptible(values, values_cnt, sizeof(ScalarItem),
2486 : compare_scalars, &cxt);
8007 tgl 2487 ECB :
2488 : /*
2489 : * Now scan the values in order, find the most common ones, and also
2490 : * accumulate ordering-correlation statistics.
2491 : *
2492 : * To determine which are most common, we first have to count the
2493 : * number of duplicates of each value. The duplicates are adjacent in
2494 : * the sorted list, so a brute-force approach is to compare successive
2495 : * datum values until we find two that are not equal. However, that
2496 : * requires N-1 invocations of the datum comparison routine, which are
2497 : * completely redundant with work that was done during the sort. (The
2498 : * sort algorithm must at some point have compared each pair of items
2499 : * that are adjacent in the sorted order; otherwise it could not know
2500 : * that it's ordered the pair correctly.) We exploit this by having
2501 : * compare_scalars remember the highest tupno index that each
2502 : * ScalarItem has been found equal to. At the end of the sort, a
2503 : * ScalarItem's tupnoLink will still point to itself if and only if it
2504 : * is the last item of its group of duplicates (since the group will
2505 : * be ordered by tupno).
2506 : */
8007 tgl 2507 GIC 125939 : corr_xysum = 0;
2508 125939 : ndistinct = 0;
2509 125939 : nmultiple = 0;
8007 tgl 2510 CBC 125939 : dups_cnt = 0;
2511 93907122 : for (i = 0; i < values_cnt; i++)
8007 tgl 2512 ECB : {
8007 tgl 2513 CBC 93781183 : int tupno = values[i].tupno;
8007 tgl 2514 ECB :
7836 tgl 2515 GIC 93781183 : corr_xysum += ((double) i) * ((double) tupno);
8007 tgl 2516 CBC 93781183 : dups_cnt++;
8007 tgl 2517 GIC 93781183 : if (tupnoLink[tupno] == tupno)
8350 bruce 2518 ECB : {
8007 tgl 2519 : /* Reached end of duplicates of this value */
8007 tgl 2520 CBC 16222141 : ndistinct++;
8007 tgl 2521 GIC 16222141 : if (dups_cnt > 1)
2522 : {
8007 tgl 2523 CBC 1810101 : nmultiple++;
2524 1810101 : if (track_cnt < num_mcv ||
7836 bruce 2525 GIC 626580 : dups_cnt > track[track_cnt - 1].count)
8007 tgl 2526 ECB : {
2527 : /*
2528 : * Found a new item for the mcv list; find its
2529 : * position, bubbling down old items if needed. Loop
2530 : * invariant is that j points at an empty/ replaceable
2531 : * slot.
2532 : */
2533 : int j;
2534 :
8007 tgl 2535 GIC 1365545 : if (track_cnt < num_mcv)
2536 1183521 : track_cnt++;
7836 bruce 2537 19498446 : for (j = track_cnt - 1; j > 0; j--)
8007 tgl 2538 ECB : {
7836 bruce 2539 CBC 19350259 : if (dups_cnt <= track[j - 1].count)
8007 tgl 2540 1217358 : break;
7836 bruce 2541 GIC 18132901 : track[j].count = track[j - 1].count;
7836 bruce 2542 CBC 18132901 : track[j].first = track[j - 1].first;
8007 tgl 2543 ECB : }
8007 tgl 2544 CBC 1365545 : track[j].count = dups_cnt;
2545 1365545 : track[j].first = i + 1 - dups_cnt;
2546 : }
8007 tgl 2547 ECB : }
8007 tgl 2548 CBC 16222141 : dups_cnt = 0;
2549 : }
2550 : }
8350 bruce 2551 ECB :
8007 tgl 2552 GIC 125939 : stats->stats_valid = true;
2553 : /* Do the simple null-frac and width stats */
6995 2554 125939 : stats->stanullfrac = (double) null_cnt / (double) samplerows;
7533 tgl 2555 CBC 125939 : if (is_varwidth)
8007 tgl 2556 GIC 16382 : stats->stawidth = total_width / (double) nonnull_cnt;
8007 tgl 2557 ECB : else
8007 tgl 2558 CBC 109557 : stats->stawidth = stats->attrtype->typlen;
8350 bruce 2559 ECB :
8007 tgl 2560 GIC 125939 : if (nmultiple == 0)
8007 tgl 2561 ECB : {
2562 : /*
2436 2563 : * If we found no repeated non-null values, assume it's a unique
2564 : * column; but be sure to discount for any nulls we found.
2565 : */
2436 tgl 2566 GIC 30358 : stats->stadistinct = -1.0 * (1.0 - stats->stanullfrac);
2567 : }
8007 2568 95581 : else if (toowide_cnt == 0 && nmultiple == ndistinct)
8007 tgl 2569 ECB : {
2570 : /*
6385 bruce 2571 : * Every value in the sample appeared more than once. Assume the
2572 : * column has just these values. (This case is meant to address
2573 : * columns with small, fixed sets of possible values, such as
2574 : * boolean or enum columns. If there are any values that appear
2575 : * just once in the sample, including too-wide values, we should
2576 : * assume that that's not what we're dealing with.)
2577 : */
8007 tgl 2578 GIC 54983 : stats->stadistinct = ndistinct;
2579 : }
2580 : else
8007 tgl 2581 ECB : {
2582 : /*----------
2583 : * Estimate the number of distinct values using the estimator
2584 : * proposed by Haas and Stokes in IBM Research Report RJ 10025:
2585 : * n*d / (n - f1 + f1*n/N)
2586 : * where f1 is the number of distinct values that occurred
2587 : * exactly once in our sample of n rows (from a total of N),
2588 : * and d is the total number of distinct values in the sample.
2589 : * This is their Duj1 estimator; the other estimators they
2590 : * recommend are considerably more complex, and are numerically
2591 : * very unstable when n is much smaller than N.
2592 : *
2593 : * In this calculation, we consider only non-nulls. We used to
2594 : * include rows with null values in the n and N counts, but that
2595 : * leads to inaccurate answers in columns with many nulls, and
2596 : * it's intuitively bogus anyway considering the desired result is
2597 : * the number of distinct non-null values.
2598 : *
2599 : * Overwidth values are assumed to have been distinct.
2600 : *----------
2601 : */
7836 bruce 2602 GIC 40598 : int f1 = ndistinct - nmultiple + toowide_cnt;
7720 tgl 2603 40598 : int d = f1 + nmultiple;
2564 2604 40598 : double n = samplerows - null_cnt;
2564 tgl 2605 CBC 40598 : double N = totalrows * (1.0 - stats->stanullfrac);
2564 tgl 2606 ECB : double stadistinct;
7720 2607 :
2564 2608 : /* N == 0 shouldn't happen, but just in case ... */
2564 tgl 2609 GIC 40598 : if (N > 0)
2610 40598 : stadistinct = (n * d) / ((n - f1) + f1 * n / N);
2611 : else
2564 tgl 2612 LBC 0 : stadistinct = 0;
7522 bruce 2613 ECB :
2614 : /* Clamp to sane range in case of roundoff error */
2564 tgl 2615 GBC 40598 : if (stadistinct < d)
2564 tgl 2616 GIC 982 : stadistinct = d;
2617 40598 : if (stadistinct > N)
2564 tgl 2618 LBC 0 : stadistinct = N;
2564 tgl 2619 ECB : /* And round to integer */
7720 tgl 2620 CBC 40598 : stats->stadistinct = floor(stadistinct + 0.5);
8007 tgl 2621 EUB : }
2622 :
8007 tgl 2623 ECB : /*
2624 : * If we estimated the number of distinct values at more than 10% of
2625 : * the total row count (a very arbitrary limit), then assume that
2626 : * stadistinct should scale with the row count rather than be a fixed
2627 : * value.
2628 : */
8007 tgl 2629 GIC 125939 : if (stats->stadistinct > 0.1 * totalrows)
7836 bruce 2630 27662 : stats->stadistinct = -(stats->stadistinct / totalrows);
2631 :
7977 tgl 2632 ECB : /*
6385 bruce 2633 : * Decide how many values are worth storing as most-common values. If
2634 : * we are able to generate a complete MCV list (all the values in the
2635 : * sample will fit, and we think these are all the ones in the table),
2636 : * then do so. Otherwise, store only those values that are
2637 : * significantly more common than the values not in the list.
2638 : *
2639 : * Note: the first of these cases is meant to address columns with
2640 : * small, fixed sets of possible values, such as boolean or enum
2641 : * columns. If we can *completely* represent the column population by
2642 : * an MCV list that will fit into the stats target, then we should do
2643 : * so and thus provide the planner with complete information. But if
2644 : * the MCV list is not complete, it's generally worth being more
2645 : * selective, and not just filling it all the way up to the stats
2646 : * target.
2647 : */
7977 tgl 2648 GIC 125939 : if (track_cnt == ndistinct && toowide_cnt == 0 &&
2649 54664 : stats->stadistinct > 0 &&
2650 : track_cnt <= num_mcv)
7977 tgl 2651 ECB : {
2652 : /* Track list includes all values seen, and all will fit */
7977 tgl 2653 GIC 50810 : num_mcv = track_cnt;
2654 : }
2655 : else
7977 tgl 2656 ECB : {
2657 : int *mcv_counts;
2658 :
2659 : /* Incomplete list; decide how many values are worth keeping */
7977 tgl 2660 GIC 75129 : if (num_mcv > track_cnt)
2661 69604 : num_mcv = track_cnt;
2662 :
1844 dean.a.rasheed 2663 CBC 75129 : if (num_mcv > 0)
7977 tgl 2664 ECB : {
1844 dean.a.rasheed 2665 GIC 44771 : mcv_counts = (int *) palloc(num_mcv * sizeof(int));
1844 dean.a.rasheed 2666 CBC 1027020 : for (i = 0; i < num_mcv; i++)
1844 dean.a.rasheed 2667 GIC 982249 : mcv_counts[i] = track[i].count;
1844 dean.a.rasheed 2668 ECB :
1844 dean.a.rasheed 2669 CBC 44771 : num_mcv = analyze_mcv_list(mcv_counts, num_mcv,
2670 44771 : stats->stadistinct,
1844 dean.a.rasheed 2671 GIC 44771 : stats->stanullfrac,
1844 dean.a.rasheed 2672 ECB : samplerows, totalrows);
7977 tgl 2673 : }
2674 : }
2675 :
2676 : /* Generate MCV slot entry */
8007 tgl 2677 GIC 125939 : if (num_mcv > 0)
2678 : {
2679 : MemoryContext old_context;
7836 bruce 2680 ECB : Datum *mcv_values;
2681 : float4 *mcv_freqs;
2682 :
2683 : /* Must copy the target values into anl_context */
6996 tgl 2684 GIC 95544 : old_context = MemoryContextSwitchTo(stats->anl_context);
8007 2685 95544 : mcv_values = (Datum *) palloc(num_mcv * sizeof(Datum));
2686 95544 : mcv_freqs = (float4 *) palloc(num_mcv * sizeof(float4));
8007 tgl 2687 CBC 1278962 : for (i = 0; i < num_mcv; i++)
8007 tgl 2688 ECB : {
8007 tgl 2689 CBC 2366836 : mcv_values[i] = datumCopy(values[track[i].first].value,
4634 2690 1183418 : stats->attrtype->typbyval,
4634 tgl 2691 GIC 1183418 : stats->attrtype->typlen);
6995 tgl 2692 CBC 1183418 : mcv_freqs[i] = (double) track[i].count / (double) samplerows;
8350 bruce 2693 ECB : }
8007 tgl 2694 CBC 95544 : MemoryContextSwitchTo(old_context);
8007 tgl 2695 ECB :
8007 tgl 2696 GIC 95544 : stats->stakind[slot_idx] = STATISTIC_KIND_MCV;
6996 tgl 2697 CBC 95544 : stats->staop[slot_idx] = mystats->eqopr;
1577 tgl 2698 GIC 95544 : stats->stacoll[slot_idx] = stats->attrcollid;
8007 tgl 2699 CBC 95544 : stats->stanumbers[slot_idx] = mcv_freqs;
2700 95544 : stats->numnumbers[slot_idx] = num_mcv;
2701 95544 : stats->stavalues[slot_idx] = mcv_values;
2702 95544 : stats->numvalues[slot_idx] = num_mcv;
5050 bruce 2703 ECB :
5395 heikki.linnakangas 2704 : /*
5050 bruce 2705 : * Accept the defaults for stats->statypid and others. They have
2706 : * been set before we were called (see vacuum.h)
2707 : */
8007 tgl 2708 GIC 95544 : slot_idx++;
2709 : }
2710 :
8007 tgl 2711 ECB : /*
2712 : * Generate a histogram slot entry if there are at least two distinct
2713 : * values not accounted for in the MCV list. (This ensures the
2714 : * histogram won't collapse to empty or a singleton.)
2715 : */
8007 tgl 2716 GIC 125939 : num_hist = ndistinct - num_mcv;
7977 2717 125939 : if (num_hist > num_bins)
2718 18193 : num_hist = num_bins + 1;
8007 tgl 2719 CBC 125939 : if (num_hist >= 2)
8007 tgl 2720 ECB : {
2721 : MemoryContext old_context;
7836 bruce 2722 : Datum *hist_values;
2723 : int nvals;
2724 : int pos,
2725 : posfrac,
2726 : delta,
2727 : deltafrac;
2728 :
2729 : /* Sort the MCV items into position order to speed next loop */
61 peter 2730 GNC 53893 : qsort_interruptible(track, num_mcv, sizeof(ScalarMCVItem),
2731 : compare_mcvs, NULL);
2732 :
8350 bruce 2733 ECB : /*
2734 : * Collapse out the MCV items from the values[] array.
2735 : *
2736 : * Note we destroy the values[] array here... but we don't need it
2737 : * for anything more. We do, however, still need values_cnt.
2738 : * nvals will be the number of remaining entries in values[].
2739 : */
8007 tgl 2740 GIC 53893 : if (num_mcv > 0)
2741 : {
2742 : int src,
7836 bruce 2743 ECB : dest;
2744 : int j;
2745 :
8007 tgl 2746 GIC 33407 : src = dest = 0;
2747 33407 : j = 0; /* index of next interesting MCV item */
2748 1417706 : while (src < values_cnt)
8007 tgl 2749 ECB : {
7836 bruce 2750 : int ncopy;
8007 tgl 2751 :
8007 tgl 2752 GIC 1384299 : if (j < num_mcv)
2753 : {
7836 bruce 2754 1359624 : int first = track[j].first;
8007 tgl 2755 ECB :
8007 tgl 2756 GIC 1359624 : if (src >= first)
8007 tgl 2757 ECB : {
2758 : /* advance past this MCV item */
8007 tgl 2759 CBC 942787 : src = first + track[j].count;
8007 tgl 2760 GIC 942787 : j++;
2761 942787 : continue;
8007 tgl 2762 ECB : }
8007 tgl 2763 CBC 416837 : ncopy = first - src;
8007 tgl 2764 ECB : }
2765 : else
8007 tgl 2766 CBC 24675 : ncopy = values_cnt - src;
8007 tgl 2767 GIC 441512 : memmove(&values[dest], &values[src],
2768 : ncopy * sizeof(ScalarItem));
8007 tgl 2769 CBC 441512 : src += ncopy;
2770 441512 : dest += ncopy;
2771 : }
2772 33407 : nvals = dest;
8007 tgl 2773 ECB : }
2774 : else
8007 tgl 2775 CBC 20486 : nvals = values_cnt;
8007 tgl 2776 GIC 53893 : Assert(nvals >= num_hist);
2777 :
7603 tgl 2778 ECB : /* Must copy the target values into anl_context */
6996 tgl 2779 CBC 53893 : old_context = MemoryContextSwitchTo(stats->anl_context);
8007 tgl 2780 GIC 53893 : hist_values = (Datum *) palloc(num_hist * sizeof(Datum));
2781 :
5087 tgl 2782 ECB : /*
2783 : * The object of this loop is to copy the first and last values[]
2784 : * entries along with evenly-spaced values in between. So the
2785 : * i'th value is values[(i * (nvals - 1)) / (num_hist - 1)]. But
2786 : * computing that subscript directly risks integer overflow when
2787 : * the stats target is more than a couple thousand. Instead we
2788 : * add (nvals - 1) / (num_hist - 1) to pos at each step, tracking
2789 : * the integral and fractional parts of the sum separately.
2790 : */
5087 tgl 2791 GIC 53893 : delta = (nvals - 1) / (num_hist - 1);
2792 53893 : deltafrac = (nvals - 1) % (num_hist - 1);
2793 53893 : pos = posfrac = 0;
5087 tgl 2794 ECB :
8007 tgl 2795 CBC 2603068 : for (i = 0; i < num_hist; i++)
8007 tgl 2796 ECB : {
8007 tgl 2797 GIC 5098350 : hist_values[i] = datumCopy(values[pos].value,
4634 tgl 2798 CBC 2549175 : stats->attrtype->typbyval,
4634 tgl 2799 GIC 2549175 : stats->attrtype->typlen);
5087 tgl 2800 CBC 2549175 : pos += delta;
2801 2549175 : posfrac += deltafrac;
2802 2549175 : if (posfrac >= (num_hist - 1))
5087 tgl 2803 ECB : {
2804 : /* fractional part exceeds 1, carry to integer part */
5087 tgl 2805 CBC 848862 : pos++;
5087 tgl 2806 GIC 848862 : posfrac -= (num_hist - 1);
2807 : }
8350 bruce 2808 ECB : }
5087 tgl 2809 :
8007 tgl 2810 GIC 53893 : MemoryContextSwitchTo(old_context);
2811 :
2812 53893 : stats->stakind[slot_idx] = STATISTIC_KIND_HISTOGRAM;
6996 tgl 2813 CBC 53893 : stats->staop[slot_idx] = mystats->ltopr;
1577 tgl 2814 GIC 53893 : stats->stacoll[slot_idx] = stats->attrcollid;
8007 tgl 2815 CBC 53893 : stats->stavalues[slot_idx] = hist_values;
2816 53893 : stats->numvalues[slot_idx] = num_hist;
5050 bruce 2817 ECB :
5395 heikki.linnakangas 2818 : /*
5050 bruce 2819 : * Accept the defaults for stats->statypid and others. They have
2820 : * been set before we were called (see vacuum.h)
2821 : */
8007 tgl 2822 GIC 53893 : slot_idx++;
2823 : }
2824 :
8007 tgl 2825 ECB : /* Generate a correlation entry if there are multiple values */
8007 tgl 2826 GIC 125939 : if (values_cnt > 1)
2827 : {
2828 : MemoryContext old_context;
7836 bruce 2829 ECB : float4 *corrs;
2830 : double corr_xsum,
2831 : corr_x2sum;
2832 :
2833 : /* Must copy the target values into anl_context */
6996 tgl 2834 GIC 116030 : old_context = MemoryContextSwitchTo(stats->anl_context);
8007 2835 116030 : corrs = (float4 *) palloc(sizeof(float4));
2836 116030 : MemoryContextSwitchTo(old_context);
8007 tgl 2837 ECB :
2838 : /*----------
2839 : * Since we know the x and y value sets are both
2840 : * 0, 1, ..., values_cnt-1
2841 : * we have sum(x) = sum(y) =
2842 : * (values_cnt-1)*values_cnt / 2
2843 : * and sum(x^2) = sum(y^2) =
2844 : * (values_cnt-1)*values_cnt*(2*values_cnt-1) / 6.
2845 : *----------
2846 : */
7836 tgl 2847 GIC 116030 : corr_xsum = ((double) (values_cnt - 1)) *
2848 116030 : ((double) values_cnt) / 2.0;
2849 116030 : corr_x2sum = ((double) (values_cnt - 1)) *
7836 tgl 2850 CBC 116030 : ((double) values_cnt) * (double) (2 * values_cnt - 1) / 6.0;
7836 bruce 2851 ECB :
8007 tgl 2852 : /* And the correlation coefficient reduces to */
8007 tgl 2853 CBC 116030 : corrs[0] = (values_cnt * corr_xysum - corr_xsum * corr_xsum) /
8007 tgl 2854 GIC 116030 : (values_cnt * corr_x2sum - corr_xsum * corr_xsum);
2855 :
8007 tgl 2856 CBC 116030 : stats->stakind[slot_idx] = STATISTIC_KIND_CORRELATION;
6996 2857 116030 : stats->staop[slot_idx] = mystats->ltopr;
1577 tgl 2858 GIC 116030 : stats->stacoll[slot_idx] = stats->attrcollid;
8007 tgl 2859 CBC 116030 : stats->stanumbers[slot_idx] = corrs;
2860 116030 : stats->numnumbers[slot_idx] = 1;
2861 116030 : slot_idx++;
8350 bruce 2862 ECB : }
2863 : }
3375 tgl 2864 CBC 8534 : else if (nonnull_cnt > 0)
2865 : {
2866 : /* We found some non-null values, but they were all too wide */
2867 650 : Assert(nonnull_cnt == toowide_cnt);
3375 tgl 2868 GIC 650 : stats->stats_valid = true;
2869 : /* Do the simple null-frac and width stats */
3375 tgl 2870 CBC 650 : stats->stanullfrac = (double) null_cnt / (double) samplerows;
2871 650 : if (is_varwidth)
3375 tgl 2872 GIC 650 : stats->stawidth = total_width / (double) nonnull_cnt;
3375 tgl 2873 ECB : else
3375 tgl 2874 LBC 0 : stats->stawidth = stats->attrtype->typlen;
3375 tgl 2875 ECB : /* Assume all too-wide values are distinct, so it's a unique column */
2436 tgl 2876 GIC 650 : stats->stadistinct = -1.0 * (1.0 - stats->stanullfrac);
3375 tgl 2877 EUB : }
3375 tgl 2878 GIC 7884 : else if (null_cnt > 0)
6631 tgl 2879 ECB : {
2880 : /* We found only nulls; assume the column is entirely null */
6631 tgl 2881 CBC 7884 : stats->stats_valid = true;
6631 tgl 2882 GIC 7884 : stats->stanullfrac = 1.0;
2883 7884 : if (is_varwidth)
6385 bruce 2884 CBC 7404 : stats->stawidth = 0; /* "unknown" */
6631 tgl 2885 ECB : else
6631 tgl 2886 CBC 480 : stats->stawidth = stats->attrtype->typlen;
2118 2887 7884 : stats->stadistinct = 0.0; /* "unknown" */
2888 : }
8007 tgl 2889 ECB :
2890 : /* We don't need to bother cleaning up any of our temporary palloc's */
8350 bruce 2891 GIC 134473 : }
2892 :
2893 : /*
271 tgl 2894 ECB : * Comparator for sorting ScalarItems
2895 : *
2896 : * Aside from sorting the items, we update the tupnoLink[] array
2897 : * whenever two ScalarItems are found to contain equal datums. The array
2898 : * is indexed by tupno; for each ScalarItem, it contains the highest
2899 : * tupno that that item's datum has been found to be equal to. This allows
2900 : * us to avoid additional comparisons in compute_scalar_stats().
2901 : */
2902 : static int
6030 tgl 2903 GIC 814220939 : compare_scalars(const void *a, const void *b, void *arg)
2904 : {
4228 peter_e 2905 814220939 : Datum da = ((const ScalarItem *) a)->value;
4228 peter_e 2906 CBC 814220939 : int ta = ((const ScalarItem *) a)->tupno;
4228 peter_e 2907 GIC 814220939 : Datum db = ((const ScalarItem *) b)->value;
4228 peter_e 2908 CBC 814220939 : int tb = ((const ScalarItem *) b)->tupno;
6030 tgl 2909 814220939 : CompareScalarsContext *cxt = (CompareScalarsContext *) arg;
4141 tgl 2910 ECB : int compare;
8350 bruce 2911 :
4141 tgl 2912 CBC 814220939 : compare = ApplySortComparator(da, false, db, false, cxt->ssup);
7981 tgl 2913 GIC 814220939 : if (compare != 0)
2914 279521290 : return compare;
8350 bruce 2915 ECB :
8007 tgl 2916 : /*
6030 2917 : * The two datums are equal, so update cxt->tupnoLink[].
2918 : */
6030 tgl 2919 GIC 534699649 : if (cxt->tupnoLink[ta] < tb)
2920 82840942 : cxt->tupnoLink[ta] = tb;
2921 534699649 : if (cxt->tupnoLink[tb] < ta)
6030 tgl 2922 CBC 5398053 : cxt->tupnoLink[tb] = ta;
8007 tgl 2923 ECB :
2924 : /*
2925 : * For equal datums, sort by tupno
2926 : */
8007 tgl 2927 GIC 534699649 : return ta - tb;
2928 : }
2929 :
8007 tgl 2930 ECB : /*
2931 : * Comparator for sorting ScalarMCVItems by position
2932 : */
2933 : static int
271 tgl 2934 GIC 4942544 : compare_mcvs(const void *a, const void *b, void *arg)
2935 : {
4228 peter_e 2936 4942544 : int da = ((const ScalarMCVItem *) a)->first;
4228 peter_e 2937 CBC 4942544 : int db = ((const ScalarMCVItem *) b)->first;
2938 :
8007 tgl 2939 4942544 : return da - db;
8007 tgl 2940 ECB : }
2941 :
1844 dean.a.rasheed 2942 : /*
2943 : * Analyze the list of common values in the sample and decide how many are
2944 : * worth storing in the table's MCV list.
2945 : *
2946 : * mcv_counts is assumed to be a list of the counts of the most common values
2947 : * seen in the sample, starting with the most common. The return value is the
2948 : * number that are significantly more common than the values not in the list,
2949 : * and which are therefore deemed worth storing in the table's MCV list.
2950 : */
2951 : static int
1844 dean.a.rasheed 2952 GIC 45782 : analyze_mcv_list(int *mcv_counts,
2953 : int num_mcv,
2954 : double stadistinct,
1844 dean.a.rasheed 2955 ECB : double stanullfrac,
2956 : int samplerows,
2957 : double totalrows)
2958 : {
2959 : double ndistinct_table;
2960 : double sumcount;
2961 : int i;
2962 :
2963 : /*
2964 : * If the entire table was sampled, keep the whole list. This also
2965 : * protects us against division by zero in the code below.
2966 : */
1844 dean.a.rasheed 2967 GIC 45782 : if (samplerows == totalrows || totalrows <= 1.0)
2968 45361 : return num_mcv;
2969 :
1844 dean.a.rasheed 2970 ECB : /* Re-extract the estimated number of distinct nonnull values in table */
1844 dean.a.rasheed 2971 CBC 421 : ndistinct_table = stadistinct;
1844 dean.a.rasheed 2972 GIC 421 : if (ndistinct_table < 0)
2973 84 : ndistinct_table = -ndistinct_table * totalrows;
1844 dean.a.rasheed 2974 ECB :
2975 : /*
2976 : * Exclude the least common values from the MCV list, if they are not
2977 : * significantly more common than the estimated selectivity they would
2978 : * have if they weren't in the list. All non-MCV values are assumed to be
2979 : * equally common, after taking into account the frequencies of all the
2980 : * values in the MCV list and the number of nulls (c.f. eqsel()).
2981 : *
2982 : * Here sumcount tracks the total count of all but the last (least common)
2983 : * value in the MCV list, allowing us to determine the effect of excluding
2984 : * that value from the list.
2985 : *
2986 : * Note that we deliberately do this by removing values from the full
2987 : * list, rather than starting with an empty list and adding values,
2988 : * because the latter approach can fail to add any values if all the most
2989 : * common values have around the same frequency and make up the majority
2990 : * of the table, so that the overall average frequency of all values is
2991 : * roughly the same as that of the common values. This would lead to any
2992 : * uncommon values being significantly overestimated.
2993 : */
1844 dean.a.rasheed 2994 GIC 421 : sumcount = 0.0;
2995 865 : for (i = 0; i < num_mcv - 1; i++)
2996 444 : sumcount += mcv_counts[i];
1844 dean.a.rasheed 2997 ECB :
1844 dean.a.rasheed 2998 CBC 487 : while (num_mcv > 0)
1844 dean.a.rasheed 2999 ECB : {
3000 : double selec,
3001 : otherdistinct,
3002 : N,
3003 : n,
3004 : K,
3005 : variance,
3006 : stddev;
3007 :
3008 : /*
3009 : * Estimated selectivity the least common value would have if it
3010 : * wasn't in the MCV list (c.f. eqsel()).
3011 : */
1844 dean.a.rasheed 3012 GIC 487 : selec = 1.0 - sumcount / samplerows - stanullfrac;
3013 487 : if (selec < 0.0)
1844 dean.a.rasheed 3014 UIC 0 : selec = 0.0;
1844 dean.a.rasheed 3015 CBC 487 : if (selec > 1.0)
1844 dean.a.rasheed 3016 LBC 0 : selec = 1.0;
1844 dean.a.rasheed 3017 GBC 487 : otherdistinct = ndistinct_table - (num_mcv - 1);
1844 dean.a.rasheed 3018 CBC 487 : if (otherdistinct > 1)
1844 dean.a.rasheed 3019 GBC 487 : selec /= otherdistinct;
1844 dean.a.rasheed 3020 ECB :
3021 : /*
3022 : * If the value is kept in the MCV list, its population frequency is
3023 : * assumed to equal its sample frequency. We use the lower end of a
3024 : * textbook continuity-corrected Wald-type confidence interval to
3025 : * determine if that is significantly more common than the non-MCV
3026 : * frequency --- specifically we assume the population frequency is
3027 : * highly likely to be within around 2 standard errors of the sample
3028 : * frequency, which equates to an interval of 2 standard deviations
3029 : * either side of the sample count, plus an additional 0.5 for the
3030 : * continuity correction. Since we are sampling without replacement,
3031 : * this is a hypergeometric distribution.
3032 : *
3033 : * XXX: Empirically, this approach seems to work quite well, but it
3034 : * may be worth considering more advanced techniques for estimating
3035 : * the confidence interval of the hypergeometric distribution.
3036 : */
1844 dean.a.rasheed 3037 GIC 487 : N = totalrows;
3038 487 : n = samplerows;
3039 487 : K = N * mcv_counts[num_mcv - 1] / n;
1844 dean.a.rasheed 3040 CBC 487 : variance = n * K * (N - K) * (N - n) / (N * N * (N - 1));
3041 487 : stddev = sqrt(variance);
1844 dean.a.rasheed 3042 ECB :
1844 dean.a.rasheed 3043 CBC 487 : if (mcv_counts[num_mcv - 1] > selec * samplerows + 2 * stddev + 0.5)
1844 dean.a.rasheed 3044 ECB : {
3045 : /*
3046 : * The value is significantly more common than the non-MCV
3047 : * selectivity would suggest. Keep it, and all the other more
3048 : * common values in the list.
3049 : */
1844 dean.a.rasheed 3050 GIC 380 : break;
3051 : }
3052 : else
1844 dean.a.rasheed 3053 ECB : {
3054 : /* Discard this value and consider the next least common value */
1844 dean.a.rasheed 3055 GIC 107 : num_mcv--;
3056 107 : if (num_mcv == 0)
3057 41 : break;
1844 dean.a.rasheed 3058 CBC 66 : sumcount -= mcv_counts[num_mcv - 1];
1844 dean.a.rasheed 3059 ECB : }
3060 : }
1844 dean.a.rasheed 3061 CBC 421 : return num_mcv;
3062 : }
|