1 /*-------------------------------------------------------------------------
4 * the postgres statistics generator
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/commands/analyze.c,v 1.46 2002/09/04 20:31:14 momjian Exp $
13 *-------------------------------------------------------------------------
19 #include "access/heapam.h"
20 #include "access/tuptoaster.h"
21 #include "catalog/catalog.h"
22 #include "catalog/catname.h"
23 #include "catalog/indexing.h"
24 #include "catalog/pg_operator.h"
25 #include "catalog/pg_statistic.h"
26 #include "catalog/pg_type.h"
27 #include "commands/vacuum.h"
28 #include "miscadmin.h"
29 #include "parser/parse_oper.h"
30 #include "parser/parse_relation.h"
31 #include "utils/acl.h"
32 #include "utils/builtins.h"
33 #include "utils/datum.h"
34 #include "utils/fmgroids.h"
35 #include "utils/lsyscache.h"
36 #include "utils/syscache.h"
37 #include "utils/tuplesort.h"
41 * Analysis algorithms supported
45 ALG_MINIMAL = 1, /* Compute only most-common-values */
46 ALG_SCALAR /* Compute MCV, histogram, sort
51 * To avoid consuming too much memory during analysis and/or too much space
52 * in the resulting pg_statistic rows, we ignore varlena datums that are wider
53 * than WIDTH_THRESHOLD (after detoasting!). This is legitimate for MCV
54 * and distinct-value calculations since a wide value is unlikely to be
55 * duplicated at all, much less be a most-common value. For the same reason,
56 * ignoring wide values will not affect our estimates of histogram bin
57 * boundaries very much.
59 #define WIDTH_THRESHOLD 1024
62 * We build one of these structs for each attribute (column) that is to be
63 * analyzed. The struct and subsidiary data are in anl_context,
64 * so they live until the end of the ANALYZE operation.
68 /* These fields are set up by examine_attribute */
69 int attnum; /* attribute number */
70 AlgCode algcode; /* Which algorithm to use for this column */
71 int minrows; /* Minimum # of rows wanted for stats */
72 Form_pg_attribute attr; /* copy of pg_attribute row for column */
73 Form_pg_type attrtype; /* copy of pg_type row for column */
74 Oid eqopr; /* '=' operator for datatype, if any */
75 Oid eqfunc; /* and associated function */
76 Oid ltopr; /* '<' operator for datatype, if any */
79 * These fields are filled in by the actual statistics-gathering
83 float4 stanullfrac; /* fraction of entries that are NULL */
84 int4 stawidth; /* average width */
85 float4 stadistinct; /* # distinct values */
86 int2 stakind[STATISTIC_NUM_SLOTS];
87 Oid staop[STATISTIC_NUM_SLOTS];
88 int numnumbers[STATISTIC_NUM_SLOTS];
89 float4 *stanumbers[STATISTIC_NUM_SLOTS];
90 int numvalues[STATISTIC_NUM_SLOTS];
91 Datum *stavalues[STATISTIC_NUM_SLOTS];
97 Datum value; /* a data value */
98 int tupno; /* position index for tuple it came from */
103 int count; /* # of duplicates */
104 int first; /* values[] index of first occurrence */
108 #define swapInt(a,b) do {int _tmp; _tmp=a; a=b; b=_tmp;} while(0)
109 #define swapDatum(a,b) do {Datum _tmp; _tmp=a; a=b; b=_tmp;} while(0)
112 /* Default statistics target (GUC parameter) */
113 int default_statistics_target = 10;
116 static int elevel = -1;
118 static MemoryContext anl_context = NULL;
120 /* context information for compare_scalars() */
121 static FmgrInfo *datumCmpFn;
122 static SortFunctionKind datumCmpFnKind;
123 static int *datumCmpTupnoLink;
126 static VacAttrStats *examine_attribute(Relation onerel, int attnum);
127 static int acquire_sample_rows(Relation onerel, HeapTuple *rows,
128 int targrows, double *totalrows);
129 static double random_fract(void);
130 static double init_selection_state(int n);
131 static double select_next_random_record(double t, int n, double *stateptr);
132 static int compare_rows(const void *a, const void *b);
133 static int compare_scalars(const void *a, const void *b);
134 static int compare_mcvs(const void *a, const void *b);
135 static void compute_minimal_stats(VacAttrStats *stats,
136 TupleDesc tupDesc, double totalrows,
137 HeapTuple *rows, int numrows);
138 static void compute_scalar_stats(VacAttrStats *stats,
139 TupleDesc tupDesc, double totalrows,
140 HeapTuple *rows, int numrows);
141 static void update_attstats(Oid relid, int natts, VacAttrStats **vacattrstats);
145 * analyze_rel() -- analyze one relation
148 analyze_rel(Oid relid, VacuumStmt *vacstmt)
154 VacAttrStats **vacattrstats;
160 if (vacstmt->verbose)
166 * Use the current context for storing analysis info. vacuum.c
167 * ensures that this context will be cleared when I return, thus
168 * releasing the memory allocated here.
170 anl_context = CurrentMemoryContext;
173 * Check for user-requested abort. Note we want this to be inside a
174 * transaction, so xact.c doesn't issue useless WARNING.
176 CHECK_FOR_INTERRUPTS();
179 * Race condition -- if the pg_class tuple has gone away since the
180 * last time we saw it, we don't need to process it.
182 if (!SearchSysCacheExists(RELOID,
183 ObjectIdGetDatum(relid),
188 * Open the class, getting only a read lock on it, and check
189 * permissions. Permissions check should match vacuum's check!
191 onerel = relation_open(relid, AccessShareLock);
193 if (!(pg_class_ownercheck(RelationGetRelid(onerel), GetUserId()) ||
194 (is_dbadmin(MyDatabaseId) && !onerel->rd_rel->relisshared)))
196 /* No need for a WARNING if we already complained during VACUUM */
197 if (!vacstmt->vacuum)
198 elog(WARNING, "Skipping \"%s\" --- only table or database owner can ANALYZE it",
199 RelationGetRelationName(onerel));
200 relation_close(onerel, AccessShareLock);
205 * Check that it's a plain table; we used to do this in getrels() but
206 * seems safer to check after we've locked the relation.
208 if (onerel->rd_rel->relkind != RELKIND_RELATION)
210 /* No need for a WARNING if we already complained during VACUUM */
211 if (!vacstmt->vacuum)
212 elog(WARNING, "Skipping \"%s\" --- can not process indexes, views or special system tables",
213 RelationGetRelationName(onerel));
214 relation_close(onerel, AccessShareLock);
219 * We can ANALYZE any table except pg_statistic. See update_attstats
221 if (IsSystemNamespace(RelationGetNamespace(onerel)) &&
222 strcmp(RelationGetRelationName(onerel), StatisticRelationName) == 0)
224 relation_close(onerel, AccessShareLock);
228 elog(elevel, "Analyzing %s.%s",
229 get_namespace_name(RelationGetNamespace(onerel)),
230 RelationGetRelationName(onerel));
233 * Determine which columns to analyze
235 * Note that system attributes are never analyzed.
237 if (vacstmt->va_cols != NIL)
241 vacattrstats = (VacAttrStats **) palloc(length(vacstmt->va_cols) *
242 sizeof(VacAttrStats *));
244 foreach(le, vacstmt->va_cols)
246 char *col = strVal(lfirst(le));
248 i = attnameAttNum(onerel, col, false);
249 vacattrstats[tcnt] = examine_attribute(onerel, i);
250 if (vacattrstats[tcnt] != NULL)
257 attr_cnt = onerel->rd_att->natts;
258 vacattrstats = (VacAttrStats **) palloc(attr_cnt *
259 sizeof(VacAttrStats *));
261 for (i = 1; i <= attr_cnt; i++)
263 vacattrstats[tcnt] = examine_attribute(onerel, i);
264 if (vacattrstats[tcnt] != NULL)
271 * Quit if no analyzable columns
275 relation_close(onerel, AccessShareLock);
280 * Determine how many rows we need to sample, using the worst case
281 * from all analyzable columns. We use a lower bound of 100 rows to
282 * avoid possible overflow in Vitter's algorithm.
285 for (i = 0; i < attr_cnt; i++)
287 if (targrows < vacattrstats[i]->minrows)
288 targrows = vacattrstats[i]->minrows;
292 * Acquire the sample rows
294 rows = (HeapTuple *) palloc(targrows * sizeof(HeapTuple));
295 numrows = acquire_sample_rows(onerel, rows, targrows, &totalrows);
298 * If we are running a standalone ANALYZE, update pages/tuples stats
299 * in pg_class. We have the accurate page count from heap_beginscan,
300 * but only an approximate number of tuples; therefore, if we are part
301 * of VACUUM ANALYZE do *not* overwrite the accurate count already
302 * inserted by VACUUM.
304 if (!vacstmt->vacuum)
305 vac_update_relstats(RelationGetRelid(onerel),
308 RelationGetForm(onerel)->relhasindex);
311 * Compute the statistics. Temporary results during the calculations
312 * for each column are stored in a child context. The calc routines
313 * are responsible to make sure that whatever they store into the
314 * VacAttrStats structure is allocated in anl_context.
318 MemoryContext col_context,
321 col_context = AllocSetContextCreate(anl_context,
323 ALLOCSET_DEFAULT_MINSIZE,
324 ALLOCSET_DEFAULT_INITSIZE,
325 ALLOCSET_DEFAULT_MAXSIZE);
326 old_context = MemoryContextSwitchTo(col_context);
327 for (i = 0; i < attr_cnt; i++)
329 switch (vacattrstats[i]->algcode)
332 compute_minimal_stats(vacattrstats[i],
333 onerel->rd_att, totalrows,
337 compute_scalar_stats(vacattrstats[i],
338 onerel->rd_att, totalrows,
342 MemoryContextResetAndDeleteChildren(col_context);
344 MemoryContextSwitchTo(old_context);
345 MemoryContextDelete(col_context);
348 * Emit the completed stats rows into pg_statistic, replacing any
349 * previous statistics for the target columns. (If there are
350 * stats in pg_statistic for columns we didn't process, we leave
353 update_attstats(relid, attr_cnt, vacattrstats);
357 * Close source relation now, but keep lock so that no one deletes it
358 * before we commit. (If someone did, they'd fail to clean up the
359 * entries we made in pg_statistic.)
361 relation_close(onerel, NoLock);
365 * examine_attribute -- pre-analysis of a single column
367 * Determine whether the column is analyzable; if so, create and initialize
368 * a VacAttrStats struct for it. If not, return NULL.
370 static VacAttrStats *
371 examine_attribute(Relation onerel, int attnum)
373 Form_pg_attribute attr = onerel->rd_att->attrs[attnum - 1];
374 Operator func_operator;
377 Oid eqopr = InvalidOid;
378 Oid eqfunc = InvalidOid;
379 Oid ltopr = InvalidOid;
382 /* Don't analyze dropped columns */
383 if (attr->attisdropped)
386 /* Don't analyze column if user has specified not to */
387 if (attr->attstattarget == 0)
390 /* If column has no "=" operator, we can't do much of anything */
391 func_operator = compatible_oper(makeList1(makeString("=")),
395 if (func_operator != NULL)
397 oprrest = ((Form_pg_operator) GETSTRUCT(func_operator))->oprrest;
398 if (oprrest == F_EQSEL)
400 eqopr = oprid(func_operator);
401 eqfunc = oprfuncid(func_operator);
403 ReleaseSysCache(func_operator);
405 if (!OidIsValid(eqfunc))
409 * If we have "=" then we're at least able to do the minimal
410 * algorithm, so start filling in a VacAttrStats struct.
412 stats = (VacAttrStats *) palloc(sizeof(VacAttrStats));
413 MemSet(stats, 0, sizeof(VacAttrStats));
414 stats->attnum = attnum;
415 stats->attr = (Form_pg_attribute) palloc(ATTRIBUTE_TUPLE_SIZE);
416 memcpy(stats->attr, attr, ATTRIBUTE_TUPLE_SIZE);
417 typtuple = SearchSysCache(TYPEOID,
418 ObjectIdGetDatum(attr->atttypid),
420 if (!HeapTupleIsValid(typtuple))
421 elog(ERROR, "cache lookup of type %u failed", attr->atttypid);
422 stats->attrtype = (Form_pg_type) palloc(sizeof(FormData_pg_type));
423 memcpy(stats->attrtype, GETSTRUCT(typtuple), sizeof(FormData_pg_type));
424 ReleaseSysCache(typtuple);
425 stats->eqopr = eqopr;
426 stats->eqfunc = eqfunc;
428 /* If the attstattarget column is negative, use the default value */
429 if (stats->attr->attstattarget < 0)
430 stats->attr->attstattarget = default_statistics_target;
432 /* Is there a "<" operator with suitable semantics? */
433 func_operator = compatible_oper(makeList1(makeString("<")),
437 if (func_operator != NULL)
439 oprrest = ((Form_pg_operator) GETSTRUCT(func_operator))->oprrest;
440 if (oprrest == F_SCALARLTSEL)
441 ltopr = oprid(func_operator);
442 ReleaseSysCache(func_operator);
444 stats->ltopr = ltopr;
447 * Determine the algorithm to use (this will get more complicated
450 if (OidIsValid(ltopr))
452 /* Seems to be a scalar datatype */
453 stats->algcode = ALG_SCALAR;
454 /*--------------------
455 * The following choice of minrows is based on the paper
456 * "Random sampling for histogram construction: how much is enough?"
457 * by Surajit Chaudhuri, Rajeev Motwani and Vivek Narasayya, in
458 * Proceedings of ACM SIGMOD International Conference on Management
459 * of Data, 1998, Pages 436-447. Their Corollary 1 to Theorem 5
460 * says that for table size n, histogram size k, maximum relative
461 * error in bin size f, and error probability gamma, the minimum
462 * random sample size is
463 * r = 4 * k * ln(2*n/gamma) / f^2
464 * Taking f = 0.5, gamma = 0.01, n = 1 million rows, we obtain
466 * Note that because of the log function, the dependence on n is
467 * quite weak; even at n = 1 billion, a 300*k sample gives <= 0.59
468 * bin size error with probability 0.99. So there's no real need to
469 * scale for n, which is a good thing because we don't necessarily
470 * know it at this point.
471 *--------------------
473 stats->minrows = 300 * stats->attr->attstattarget;
477 /* Can't do much but the minimal stuff */
478 stats->algcode = ALG_MINIMAL;
479 /* Might as well use the same minrows as above */
480 stats->minrows = 300 * stats->attr->attstattarget;
487 * acquire_sample_rows -- acquire a random sample of rows from the table
489 * Up to targrows rows are collected (if there are fewer than that many
490 * rows in the table, all rows are collected). When the table is larger
491 * than targrows, a truly random sample is collected: every row has an
492 * equal chance of ending up in the final sample.
494 * We also estimate the total number of rows in the table, and return that
497 * The returned list of tuples is in order by physical position in the table.
498 * (We will rely on this later to derive correlation estimates.)
501 acquire_sample_rows(Relation onerel, HeapTuple *rows, int targrows,
507 ItemPointer lasttuple;
508 BlockNumber lastblock,
510 OffsetNumber lastoffset;
512 double tuplesperpage;
516 Assert(targrows > 1);
519 * Do a simple linear scan until we reach the target number of rows.
521 scan = heap_beginscan(onerel, SnapshotNow, 0, NULL);
522 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
524 rows[numrows++] = heap_copytuple(tuple);
525 if (numrows >= targrows)
527 CHECK_FOR_INTERRUPTS();
532 * If we ran out of tuples then we're done, no matter how few we
533 * collected. No sort is needed, since they're already in order.
535 if (!HeapTupleIsValid(tuple))
537 *totalrows = (double) numrows;
542 * Otherwise, start replacing tuples in the sample until we reach the
543 * end of the relation. This algorithm is from Jeff Vitter's paper
544 * (see full citation below). It works by repeatedly computing the
545 * number of the next tuple we want to fetch, which will replace a
546 * randomly chosen element of the reservoir (current set of tuples).
547 * At all times the reservoir is a true random sample of the tuples
548 * we've passed over so far, so when we fall off the end of the
549 * relation we're done.
551 * A slight difficulty is that since we don't want to fetch tuples or
552 * even pages that we skip over, it's not possible to fetch *exactly*
553 * the N'th tuple at each step --- we don't know how many valid tuples
554 * are on the skipped pages. We handle this by assuming that the
555 * average number of valid tuples/page on the pages already scanned
556 * over holds good for the rest of the relation as well; this lets us
557 * estimate which page the next tuple should be on and its position in
558 * the page. Then we fetch the first valid tuple at or after that
559 * position, being careful not to use the same tuple twice. This
560 * approach should still give a good random sample, although it's not
563 lasttuple = &(rows[numrows - 1]->t_self);
564 lastblock = ItemPointerGetBlockNumber(lasttuple);
565 lastoffset = ItemPointerGetOffsetNumber(lasttuple);
568 * If possible, estimate tuples/page using only completely-scanned
571 for (numest = numrows; numest > 0; numest--)
573 if (ItemPointerGetBlockNumber(&(rows[numest - 1]->t_self)) != lastblock)
578 numest = numrows; /* don't have a full page? */
579 estblock = lastblock + 1;
582 estblock = lastblock;
583 tuplesperpage = (double) numest / (double) estblock;
585 t = (double) numrows; /* t is the # of records processed so far */
586 rstate = init_selection_state(targrows);
590 BlockNumber targblock;
593 OffsetNumber targoffset,
596 CHECK_FOR_INTERRUPTS();
598 t = select_next_random_record(t, targrows, &rstate);
599 /* Try to read the t'th record in the table */
600 targpos = t / tuplesperpage;
601 targblock = (BlockNumber) targpos;
602 targoffset = ((int) ((targpos - targblock) * tuplesperpage)) +
604 /* Make sure we are past the last selected record */
605 if (targblock <= lastblock)
607 targblock = lastblock;
608 if (targoffset <= lastoffset)
609 targoffset = lastoffset + 1;
611 /* Loop to find first valid record at or after given position */
615 * Have we fallen off the end of the relation? (We rely on
616 * heap_beginscan to have updated rd_nblocks.)
618 if (targblock >= onerel->rd_nblocks)
622 * We must maintain a pin on the target page's buffer to ensure
623 * that the maxoffset value stays good (else concurrent VACUUM
624 * might delete tuples out from under us). Hence, pin the page
625 * until we are done looking at it. We don't maintain a lock on
626 * the page, so tuples could get added to it, but we ignore such
629 targbuffer = ReadBuffer(onerel, targblock);
630 if (!BufferIsValid(targbuffer))
631 elog(ERROR, "acquire_sample_rows: ReadBuffer(%s,%u) failed",
632 RelationGetRelationName(onerel), targblock);
633 LockBuffer(targbuffer, BUFFER_LOCK_SHARE);
634 targpage = BufferGetPage(targbuffer);
635 maxoffset = PageGetMaxOffsetNumber(targpage);
636 LockBuffer(targbuffer, BUFFER_LOCK_UNLOCK);
640 HeapTupleData targtuple;
643 if (targoffset > maxoffset)
645 /* Fell off end of this page, try next */
646 ReleaseBuffer(targbuffer);
648 targoffset = FirstOffsetNumber;
651 ItemPointerSet(&targtuple.t_self, targblock, targoffset);
652 if (heap_fetch(onerel, SnapshotNow, &targtuple, &tupbuffer,
656 * Found a suitable tuple, so save it, replacing one old
659 int k = (int) (targrows * random_fract());
661 Assert(k >= 0 && k < targrows);
662 heap_freetuple(rows[k]);
663 rows[k] = heap_copytuple(&targtuple);
664 /* this releases the second pin acquired by heap_fetch: */
665 ReleaseBuffer(tupbuffer);
666 /* this releases the initial pin: */
667 ReleaseBuffer(targbuffer);
668 lastblock = targblock;
669 lastoffset = targoffset;
672 /* this tuple is dead, so advance to next one on same page */
678 * Now we need to sort the collected tuples by position (itempointer).
680 qsort((void *) rows, numrows, sizeof(HeapTuple), compare_rows);
683 * Estimate total number of valid rows in relation.
685 *totalrows = floor((double) onerel->rd_nblocks * tuplesperpage + 0.5);
690 /* Select a random value R uniformly distributed in 0 < R < 1 */
696 /* random() can produce endpoint values, try again if so */
700 } while (!(z > 0 && z < MAX_RANDOM_VALUE));
701 return (double) z / (double) MAX_RANDOM_VALUE;
705 * These two routines embody Algorithm Z from "Random sampling with a
706 * reservoir" by Jeffrey S. Vitter, in ACM Trans. Math. Softw. 11, 1
707 * (Mar. 1985), Pages 37-57. While Vitter describes his algorithm in terms
708 * of the count S of records to skip before processing another record,
709 * it is convenient to work primarily with t, the index (counting from 1)
710 * of the last record processed and next record to process. The only extra
711 * state needed between calls is W, a random state variable.
713 * Note: the original algorithm defines t, S, numer, and denom as integers.
714 * Here we express them as doubles to avoid overflow if the number of rows
715 * in the table exceeds INT_MAX. The algorithm should work as long as the
716 * row count does not become so large that it is not represented accurately
717 * in a double (on IEEE-math machines this would be around 2^52 rows).
719 * init_selection_state computes the initial W value.
721 * Given that we've already processed t records (t >= n),
722 * select_next_random_record determines the number of the next record to
726 init_selection_state(int n)
728 /* Initial value of W (for use when Algorithm Z is first applied) */
729 return exp(-log(random_fract()) / n);
733 select_next_random_record(double t, int n, double *stateptr)
735 /* The magic constant here is T from Vitter's paper */
738 /* Process records using Algorithm X until t is large enough */
742 V = random_fract(); /* Generate V */
744 quot = (t - (double) n) / t;
745 /* Find min S satisfying (4.1) */
749 quot *= (t - (double) n) / t;
754 /* Now apply Algorithm Z */
755 double W = *stateptr;
756 double term = t - (double) n + 1;
771 /* Generate U and X */
774 S = floor(X); /* S is tentatively set to floor(X) */
775 /* Test if U <= h(S)/cg(X) in the manner of (6.3) */
776 tmp = (t + 1) / term;
777 lhs = exp(log(((U * tmp * tmp) * (term + S)) / (t + X)) / n);
778 rhs = (((t + X) / (term + S)) * term) / t;
784 /* Test if U <= f(S)/cg(X) */
785 y = (((U * (t + 1)) / term) * (t + S + 1)) / (t + X);
789 numer_lim = term + S;
793 denom = t - (double) n + S;
796 for (numer = t + S; numer >= numer_lim; numer -= 1)
801 W = exp(-log(random_fract()) / n); /* Generate W in advance */
802 if (exp(log(y) / n) <= (t + X) / t)
812 * qsort comparator for sorting rows[] array
815 compare_rows(const void *a, const void *b)
817 HeapTuple ha = *(HeapTuple *) a;
818 HeapTuple hb = *(HeapTuple *) b;
819 BlockNumber ba = ItemPointerGetBlockNumber(&ha->t_self);
820 OffsetNumber oa = ItemPointerGetOffsetNumber(&ha->t_self);
821 BlockNumber bb = ItemPointerGetBlockNumber(&hb->t_self);
822 OffsetNumber ob = ItemPointerGetOffsetNumber(&hb->t_self);
837 * compute_minimal_stats() -- compute minimal column statistics
839 * We use this when we can find only an "=" operator for the datatype.
841 * We determine the fraction of non-null rows, the average width, the
842 * most common values, and the (estimated) number of distinct values.
844 * The most common values are determined by brute force: we keep a list
845 * of previously seen values, ordered by number of times seen, as we scan
846 * the samples. A newly seen value is inserted just after the last
847 * multiply-seen value, causing the bottommost (oldest) singly-seen value
848 * to drop off the list. The accuracy of this method, and also its cost,
849 * depend mainly on the length of the list we are willing to keep.
852 compute_minimal_stats(VacAttrStats *stats,
853 TupleDesc tupDesc, double totalrows,
854 HeapTuple *rows, int numrows)
860 double total_width = 0;
861 bool is_varlena = (!stats->attr->attbyval &&
862 stats->attr->attlen == -1);
863 bool is_varwidth = (!stats->attr->attbyval &&
864 stats->attr->attlen < 0);
874 int num_mcv = stats->attr->attstattarget;
877 * We track up to 2*n values for an n-element MCV list; but at least
880 track_max = 2 * num_mcv;
883 track = (TrackItem *) palloc(track_max * sizeof(TrackItem));
886 fmgr_info(stats->eqfunc, &f_cmpeq);
888 for (i = 0; i < numrows; i++)
890 HeapTuple tuple = rows[i];
897 CHECK_FOR_INTERRUPTS();
899 value = heap_getattr(tuple, stats->attnum, tupDesc, &isnull);
901 /* Check for null/nonnull */
910 * If it's a variable-width field, add up widths for average width
911 * calculation. Note that if the value is toasted, we use the
912 * toasted width. We don't bother with this calculation if it's a
917 total_width += VARSIZE(DatumGetPointer(value));
920 * If the value is toasted, we want to detoast it just once to
921 * avoid repeated detoastings and resultant excess memory
922 * usage during the comparisons. Also, check to see if the
923 * value is excessively wide, and if so don't detoast at all
924 * --- just ignore the value.
926 if (toast_raw_datum_size(value) > WIDTH_THRESHOLD)
931 value = PointerGetDatum(PG_DETOAST_DATUM(value));
933 else if (is_varwidth)
935 /* must be cstring */
936 total_width += strlen(DatumGetCString(value)) + 1;
940 * See if the value matches anything we're already tracking.
943 firstcount1 = track_cnt;
944 for (j = 0; j < track_cnt; j++)
946 if (DatumGetBool(FunctionCall2(&f_cmpeq, value, track[j].value)))
951 if (j < firstcount1 && track[j].count == 1)
959 /* This value may now need to "bubble up" in the track list */
960 while (j > 0 && track[j].count > track[j - 1].count)
962 swapDatum(track[j].value, track[j - 1].value);
963 swapInt(track[j].count, track[j - 1].count);
969 /* No match. Insert at head of count-1 list */
970 if (track_cnt < track_max)
972 for (j = track_cnt - 1; j > firstcount1; j--)
974 track[j].value = track[j - 1].value;
975 track[j].count = track[j - 1].count;
977 if (firstcount1 < track_cnt)
979 track[firstcount1].value = value;
980 track[firstcount1].count = 1;
985 /* We can only compute valid stats if we found some non-null values. */
991 stats->stats_valid = true;
992 /* Do the simple null-frac and width stats */
993 stats->stanullfrac = (double) null_cnt / (double) numrows;
995 stats->stawidth = total_width / (double) nonnull_cnt;
997 stats->stawidth = stats->attrtype->typlen;
999 /* Count the number of values we found multiple times */
1001 for (nmultiple = 0; nmultiple < track_cnt; nmultiple++)
1003 if (track[nmultiple].count == 1)
1005 summultiple += track[nmultiple].count;
1010 /* If we found no repeated values, assume it's a unique column */
1011 stats->stadistinct = -1.0;
1013 else if (track_cnt < track_max && toowide_cnt == 0 &&
1014 nmultiple == track_cnt)
1017 * Our track list includes every value in the sample, and
1018 * every value appeared more than once. Assume the column has
1019 * just these values.
1021 stats->stadistinct = track_cnt;
1026 * Estimate the number of distinct values using the estimator
1027 * proposed by Haas and Stokes in IBM Research Report RJ 10025:
1028 * n*d / (n - f1 + f1*n/N)
1029 * where f1 is the number of distinct values that occurred
1030 * exactly once in our sample of n rows (from a total of N),
1031 * and d is the total number of distinct values in the sample.
1032 * This is their Duj1 estimator; the other estimators they
1033 * recommend are considerably more complex, and are numerically
1034 * very unstable when n is much smaller than N.
1036 * We assume (not very reliably!) that all the multiply-occurring
1037 * values are reflected in the final track[] list, and the other
1038 * nonnull values all appeared but once. (XXX this usually
1039 * results in a drastic overestimate of ndistinct. Can we do
1043 int f1 = nonnull_cnt - summultiple;
1044 int d = f1 + nmultiple;
1049 numer = (double) numrows *(double) d;
1051 denom = (double) (numrows - f1) +
1052 (double) f1 *(double) numrows / totalrows;
1054 stadistinct = numer / denom;
1055 /* Clamp to sane range in case of roundoff error */
1056 if (stadistinct < (double) d)
1057 stadistinct = (double) d;
1058 if (stadistinct > totalrows)
1059 stadistinct = totalrows;
1060 stats->stadistinct = floor(stadistinct + 0.5);
1064 * If we estimated the number of distinct values at more than 10%
1065 * of the total row count (a very arbitrary limit), then assume
1066 * that stadistinct should scale with the row count rather than be
1069 if (stats->stadistinct > 0.1 * totalrows)
1070 stats->stadistinct = -(stats->stadistinct / totalrows);
1073 * Decide how many values are worth storing as most-common values.
1074 * If we are able to generate a complete MCV list (all the values
1075 * in the sample will fit, and we think these are all the ones in
1076 * the table), then do so. Otherwise, store only those values
1077 * that are significantly more common than the (estimated)
1078 * average. We set the threshold rather arbitrarily at 25% more
1079 * than average, with at least 2 instances in the sample.
1081 if (track_cnt < track_max && toowide_cnt == 0 &&
1082 stats->stadistinct > 0 &&
1083 track_cnt <= num_mcv)
1085 /* Track list includes all values seen, and all will fit */
1086 num_mcv = track_cnt;
1090 double ndistinct = stats->stadistinct;
1095 ndistinct = -ndistinct * totalrows;
1096 /* estimate # of occurrences in sample of a typical value */
1097 avgcount = (double) numrows / ndistinct;
1098 /* set minimum threshold count to store a value */
1099 mincount = avgcount * 1.25;
1102 if (num_mcv > track_cnt)
1103 num_mcv = track_cnt;
1104 for (i = 0; i < num_mcv; i++)
1106 if (track[i].count < mincount)
1114 /* Generate MCV slot entry */
1117 MemoryContext old_context;
1121 /* Must copy the target values into anl_context */
1122 old_context = MemoryContextSwitchTo(anl_context);
1123 mcv_values = (Datum *) palloc(num_mcv * sizeof(Datum));
1124 mcv_freqs = (float4 *) palloc(num_mcv * sizeof(float4));
1125 for (i = 0; i < num_mcv; i++)
1127 mcv_values[i] = datumCopy(track[i].value,
1128 stats->attr->attbyval,
1129 stats->attr->attlen);
1130 mcv_freqs[i] = (double) track[i].count / (double) numrows;
1132 MemoryContextSwitchTo(old_context);
1134 stats->stakind[0] = STATISTIC_KIND_MCV;
1135 stats->staop[0] = stats->eqopr;
1136 stats->stanumbers[0] = mcv_freqs;
1137 stats->numnumbers[0] = num_mcv;
1138 stats->stavalues[0] = mcv_values;
1139 stats->numvalues[0] = num_mcv;
1143 /* We don't need to bother cleaning up any of our temporary palloc's */
1148 * compute_scalar_stats() -- compute column statistics
1150 * We use this when we can find "=" and "<" operators for the datatype.
1152 * We determine the fraction of non-null rows, the average width, the
1153 * most common values, the (estimated) number of distinct values, the
1154 * distribution histogram, and the correlation of physical to logical order.
1156 * The desired stats can be determined fairly easily after sorting the
1157 * data values into order.
1160 compute_scalar_stats(VacAttrStats *stats,
1161 TupleDesc tupDesc, double totalrows,
1162 HeapTuple *rows, int numrows)
1166 int nonnull_cnt = 0;
1167 int toowide_cnt = 0;
1168 double total_width = 0;
1169 bool is_varlena = (!stats->attr->attbyval &&
1170 stats->attr->attlen == -1);
1171 bool is_varwidth = (!stats->attr->attbyval &&
1172 stats->attr->attlen < 0);
1175 SortFunctionKind cmpFnKind;
1180 ScalarMCVItem *track;
1182 int num_mcv = stats->attr->attstattarget;
1183 int num_bins = stats->attr->attstattarget;
1185 values = (ScalarItem *) palloc(numrows * sizeof(ScalarItem));
1186 tupnoLink = (int *) palloc(numrows * sizeof(int));
1187 track = (ScalarMCVItem *) palloc(num_mcv * sizeof(ScalarMCVItem));
1189 SelectSortFunction(stats->ltopr, &cmpFn, &cmpFnKind);
1190 fmgr_info(cmpFn, &f_cmpfn);
1192 /* Initial scan to find sortable values */
1193 for (i = 0; i < numrows; i++)
1195 HeapTuple tuple = rows[i];
1199 CHECK_FOR_INTERRUPTS();
1201 value = heap_getattr(tuple, stats->attnum, tupDesc, &isnull);
1203 /* Check for null/nonnull */
1212 * If it's a variable-width field, add up widths for average width
1213 * calculation. Note that if the value is toasted, we use the
1214 * toasted width. We don't bother with this calculation if it's a
1219 total_width += VARSIZE(DatumGetPointer(value));
1222 * If the value is toasted, we want to detoast it just once to
1223 * avoid repeated detoastings and resultant excess memory
1224 * usage during the comparisons. Also, check to see if the
1225 * value is excessively wide, and if so don't detoast at all
1226 * --- just ignore the value.
1228 if (toast_raw_datum_size(value) > WIDTH_THRESHOLD)
1233 value = PointerGetDatum(PG_DETOAST_DATUM(value));
1235 else if (is_varwidth)
1237 /* must be cstring */
1238 total_width += strlen(DatumGetCString(value)) + 1;
1241 /* Add it to the list to be sorted */
1242 values[values_cnt].value = value;
1243 values[values_cnt].tupno = values_cnt;
1244 tupnoLink[values_cnt] = values_cnt;
1248 /* We can only compute valid stats if we found some sortable values. */
1251 int ndistinct, /* # distinct values in sample */
1252 nmultiple, /* # that appear multiple times */
1257 /* Sort the collected values */
1258 datumCmpFn = &f_cmpfn;
1259 datumCmpFnKind = cmpFnKind;
1260 datumCmpTupnoLink = tupnoLink;
1261 qsort((void *) values, values_cnt,
1262 sizeof(ScalarItem), compare_scalars);
1265 * Now scan the values in order, find the most common ones, and
1266 * also accumulate ordering-correlation statistics.
1268 * To determine which are most common, we first have to count the
1269 * number of duplicates of each value. The duplicates are
1270 * adjacent in the sorted list, so a brute-force approach is to
1271 * compare successive datum values until we find two that are not
1272 * equal. However, that requires N-1 invocations of the datum
1273 * comparison routine, which are completely redundant with work
1274 * that was done during the sort. (The sort algorithm must at
1275 * some point have compared each pair of items that are adjacent
1276 * in the sorted order; otherwise it could not know that it's
1277 * ordered the pair correctly.) We exploit this by having
1278 * compare_scalars remember the highest tupno index that each
1279 * ScalarItem has been found equal to. At the end of the sort, a
1280 * ScalarItem's tupnoLink will still point to itself if and only
1281 * if it is the last item of its group of duplicates (since the
1282 * group will be ordered by tupno).
1288 for (i = 0; i < values_cnt; i++)
1290 int tupno = values[i].tupno;
1292 corr_xysum += ((double) i) * ((double) tupno);
1294 if (tupnoLink[tupno] == tupno)
1296 /* Reached end of duplicates of this value */
1301 if (track_cnt < num_mcv ||
1302 dups_cnt > track[track_cnt - 1].count)
1305 * Found a new item for the mcv list; find its
1306 * position, bubbling down old items if needed.
1307 * Loop invariant is that j points at an empty/
1312 if (track_cnt < num_mcv)
1314 for (j = track_cnt - 1; j > 0; j--)
1316 if (dups_cnt <= track[j - 1].count)
1318 track[j].count = track[j - 1].count;
1319 track[j].first = track[j - 1].first;
1321 track[j].count = dups_cnt;
1322 track[j].first = i + 1 - dups_cnt;
1329 stats->stats_valid = true;
1330 /* Do the simple null-frac and width stats */
1331 stats->stanullfrac = (double) null_cnt / (double) numrows;
1333 stats->stawidth = total_width / (double) nonnull_cnt;
1335 stats->stawidth = stats->attrtype->typlen;
1339 /* If we found no repeated values, assume it's a unique column */
1340 stats->stadistinct = -1.0;
1342 else if (toowide_cnt == 0 && nmultiple == ndistinct)
1345 * Every value in the sample appeared more than once. Assume
1346 * the column has just these values.
1348 stats->stadistinct = ndistinct;
1353 * Estimate the number of distinct values using the estimator
1354 * proposed by Haas and Stokes in IBM Research Report RJ 10025:
1355 * n*d / (n - f1 + f1*n/N)
1356 * where f1 is the number of distinct values that occurred
1357 * exactly once in our sample of n rows (from a total of N),
1358 * and d is the total number of distinct values in the sample.
1359 * This is their Duj1 estimator; the other estimators they
1360 * recommend are considerably more complex, and are numerically
1361 * very unstable when n is much smaller than N.
1363 * Overwidth values are assumed to have been distinct.
1366 int f1 = ndistinct - nmultiple + toowide_cnt;
1367 int d = f1 + nmultiple;
1372 numer = (double) numrows *(double) d;
1374 denom = (double) (numrows - f1) +
1375 (double) f1 *(double) numrows / totalrows;
1377 stadistinct = numer / denom;
1378 /* Clamp to sane range in case of roundoff error */
1379 if (stadistinct < (double) d)
1380 stadistinct = (double) d;
1381 if (stadistinct > totalrows)
1382 stadistinct = totalrows;
1383 stats->stadistinct = floor(stadistinct + 0.5);
1387 * If we estimated the number of distinct values at more than 10%
1388 * of the total row count (a very arbitrary limit), then assume
1389 * that stadistinct should scale with the row count rather than be
1392 if (stats->stadistinct > 0.1 * totalrows)
1393 stats->stadistinct = -(stats->stadistinct / totalrows);
1396 * Decide how many values are worth storing as most-common values.
1397 * If we are able to generate a complete MCV list (all the values
1398 * in the sample will fit, and we think these are all the ones in
1399 * the table), then do so. Otherwise, store only those values
1400 * that are significantly more common than the (estimated)
1401 * average. We set the threshold rather arbitrarily at 25% more
1402 * than average, with at least 2 instances in the sample. Also,
1403 * we won't suppress values that have a frequency of at least 1/K
1404 * where K is the intended number of histogram bins; such values
1405 * might otherwise cause us to emit duplicate histogram bin
1408 if (track_cnt == ndistinct && toowide_cnt == 0 &&
1409 stats->stadistinct > 0 &&
1410 track_cnt <= num_mcv)
1412 /* Track list includes all values seen, and all will fit */
1413 num_mcv = track_cnt;
1417 double ndistinct = stats->stadistinct;
1423 ndistinct = -ndistinct * totalrows;
1424 /* estimate # of occurrences in sample of a typical value */
1425 avgcount = (double) numrows / ndistinct;
1426 /* set minimum threshold count to store a value */
1427 mincount = avgcount * 1.25;
1430 /* don't let threshold exceed 1/K, however */
1431 maxmincount = (double) numrows / (double) num_bins;
1432 if (mincount > maxmincount)
1433 mincount = maxmincount;
1434 if (num_mcv > track_cnt)
1435 num_mcv = track_cnt;
1436 for (i = 0; i < num_mcv; i++)
1438 if (track[i].count < mincount)
1446 /* Generate MCV slot entry */
1449 MemoryContext old_context;
1453 /* Must copy the target values into anl_context */
1454 old_context = MemoryContextSwitchTo(anl_context);
1455 mcv_values = (Datum *) palloc(num_mcv * sizeof(Datum));
1456 mcv_freqs = (float4 *) palloc(num_mcv * sizeof(float4));
1457 for (i = 0; i < num_mcv; i++)
1459 mcv_values[i] = datumCopy(values[track[i].first].value,
1460 stats->attr->attbyval,
1461 stats->attr->attlen);
1462 mcv_freqs[i] = (double) track[i].count / (double) numrows;
1464 MemoryContextSwitchTo(old_context);
1466 stats->stakind[slot_idx] = STATISTIC_KIND_MCV;
1467 stats->staop[slot_idx] = stats->eqopr;
1468 stats->stanumbers[slot_idx] = mcv_freqs;
1469 stats->numnumbers[slot_idx] = num_mcv;
1470 stats->stavalues[slot_idx] = mcv_values;
1471 stats->numvalues[slot_idx] = num_mcv;
1476 * Generate a histogram slot entry if there are at least two
1477 * distinct values not accounted for in the MCV list. (This
1478 * ensures the histogram won't collapse to empty or a singleton.)
1480 num_hist = ndistinct - num_mcv;
1481 if (num_hist > num_bins)
1482 num_hist = num_bins + 1;
1485 MemoryContext old_context;
1489 /* Sort the MCV items into position order to speed next loop */
1490 qsort((void *) track, num_mcv,
1491 sizeof(ScalarMCVItem), compare_mcvs);
1494 * Collapse out the MCV items from the values[] array.
1496 * Note we destroy the values[] array here... but we don't need
1497 * it for anything more. We do, however, still need
1498 * values_cnt. nvals will be the number of remaining entries
1508 j = 0; /* index of next interesting MCV item */
1509 while (src < values_cnt)
1515 int first = track[j].first;
1519 /* advance past this MCV item */
1520 src = first + track[j].count;
1524 ncopy = first - src;
1527 ncopy = values_cnt - src;
1528 memmove(&values[dest], &values[src],
1529 ncopy * sizeof(ScalarItem));
1537 Assert(nvals >= num_hist);
1539 /* Must copy the target values into anl_context */
1540 old_context = MemoryContextSwitchTo(anl_context);
1541 hist_values = (Datum *) palloc(num_hist * sizeof(Datum));
1542 for (i = 0; i < num_hist; i++)
1546 pos = (i * (nvals - 1)) / (num_hist - 1);
1547 hist_values[i] = datumCopy(values[pos].value,
1548 stats->attr->attbyval,
1549 stats->attr->attlen);
1551 MemoryContextSwitchTo(old_context);
1553 stats->stakind[slot_idx] = STATISTIC_KIND_HISTOGRAM;
1554 stats->staop[slot_idx] = stats->ltopr;
1555 stats->stavalues[slot_idx] = hist_values;
1556 stats->numvalues[slot_idx] = num_hist;
1560 /* Generate a correlation entry if there are multiple values */
1563 MemoryContext old_context;
1568 /* Must copy the target values into anl_context */
1569 old_context = MemoryContextSwitchTo(anl_context);
1570 corrs = (float4 *) palloc(sizeof(float4));
1571 MemoryContextSwitchTo(old_context);
1574 * Since we know the x and y value sets are both
1575 * 0, 1, ..., values_cnt-1
1576 * we have sum(x) = sum(y) =
1577 * (values_cnt-1)*values_cnt / 2
1578 * and sum(x^2) = sum(y^2) =
1579 * (values_cnt-1)*values_cnt*(2*values_cnt-1) / 6.
1582 corr_xsum = ((double) (values_cnt - 1)) *
1583 ((double) values_cnt) / 2.0;
1584 corr_x2sum = ((double) (values_cnt - 1)) *
1585 ((double) values_cnt) * (double) (2 * values_cnt - 1) / 6.0;
1587 /* And the correlation coefficient reduces to */
1588 corrs[0] = (values_cnt * corr_xysum - corr_xsum * corr_xsum) /
1589 (values_cnt * corr_x2sum - corr_xsum * corr_xsum);
1591 stats->stakind[slot_idx] = STATISTIC_KIND_CORRELATION;
1592 stats->staop[slot_idx] = stats->ltopr;
1593 stats->stanumbers[slot_idx] = corrs;
1594 stats->numnumbers[slot_idx] = 1;
1599 /* We don't need to bother cleaning up any of our temporary palloc's */
1603 * qsort comparator for sorting ScalarItems
1605 * Aside from sorting the items, we update the datumCmpTupnoLink[] array
1606 * whenever two ScalarItems are found to contain equal datums. The array
1607 * is indexed by tupno; for each ScalarItem, it contains the highest
1608 * tupno that that item's datum has been found to be equal to. This allows
1609 * us to avoid additional comparisons in compute_scalar_stats().
1612 compare_scalars(const void *a, const void *b)
1614 Datum da = ((ScalarItem *) a)->value;
1615 int ta = ((ScalarItem *) a)->tupno;
1616 Datum db = ((ScalarItem *) b)->value;
1617 int tb = ((ScalarItem *) b)->tupno;
1620 compare = ApplySortFunction(datumCmpFn, datumCmpFnKind,
1621 da, false, db, false);
1626 * The two datums are equal, so update datumCmpTupnoLink[].
1628 if (datumCmpTupnoLink[ta] < tb)
1629 datumCmpTupnoLink[ta] = tb;
1630 if (datumCmpTupnoLink[tb] < ta)
1631 datumCmpTupnoLink[tb] = ta;
1634 * For equal datums, sort by tupno
1640 * qsort comparator for sorting ScalarMCVItems by position
1643 compare_mcvs(const void *a, const void *b)
1645 int da = ((ScalarMCVItem *) a)->first;
1646 int db = ((ScalarMCVItem *) b)->first;
1653 * update_attstats() -- update attribute statistics for one relation
1655 * Statistics are stored in several places: the pg_class row for the
1656 * relation has stats about the whole relation, and there is a
1657 * pg_statistic row for each (non-system) attribute that has ever
1658 * been analyzed. The pg_class values are updated by VACUUM, not here.
1660 * pg_statistic rows are just added or updated normally. This means
1661 * that pg_statistic will probably contain some deleted rows at the
1662 * completion of a vacuum cycle, unless it happens to get vacuumed last.
1664 * To keep things simple, we punt for pg_statistic, and don't try
1665 * to compute or store rows for pg_statistic itself in pg_statistic.
1666 * This could possibly be made to work, but it's not worth the trouble.
1667 * Note analyze_rel() has seen to it that we won't come here when
1668 * vacuuming pg_statistic itself.
1670 * Note: if two backends concurrently try to analyze the same relation,
1671 * the second one is likely to fail here with a "tuple concurrently
1672 * updated" error. This is slightly annoying, but no real harm is done.
1673 * We could prevent the problem by using a stronger lock on the
1674 * relation for ANALYZE (ie, ShareUpdateExclusiveLock instead
1675 * of AccessShareLock); but that cure seems worse than the disease,
1676 * especially now that ANALYZE doesn't start a new transaction
1677 * for each relation. The lock could be held for a long time...
1680 update_attstats(Oid relid, int natts, VacAttrStats **vacattrstats)
1685 sd = heap_openr(StatisticRelationName, RowExclusiveLock);
1687 for (attno = 0; attno < natts; attno++)
1689 VacAttrStats *stats = vacattrstats[attno];
1690 FmgrInfo out_function;
1696 Datum values[Natts_pg_statistic];
1697 char nulls[Natts_pg_statistic];
1698 char replaces[Natts_pg_statistic];
1700 /* Ignore attr if we weren't able to collect stats */
1701 if (!stats->stats_valid)
1704 fmgr_info(stats->attrtype->typoutput, &out_function);
1707 * Construct a new pg_statistic tuple
1709 for (i = 0; i < Natts_pg_statistic; ++i)
1716 values[i++] = ObjectIdGetDatum(relid); /* starelid */
1717 values[i++] = Int16GetDatum(stats->attnum); /* staattnum */
1718 values[i++] = Float4GetDatum(stats->stanullfrac); /* stanullfrac */
1719 values[i++] = Int32GetDatum(stats->stawidth); /* stawidth */
1720 values[i++] = Float4GetDatum(stats->stadistinct); /* stadistinct */
1721 for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
1723 values[i++] = Int16GetDatum(stats->stakind[k]); /* stakindN */
1725 for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
1727 values[i++] = ObjectIdGetDatum(stats->staop[k]); /* staopN */
1729 for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
1731 int nnum = stats->numnumbers[k];
1735 Datum *numdatums = (Datum *) palloc(nnum * sizeof(Datum));
1738 for (n = 0; n < nnum; n++)
1739 numdatums[n] = Float4GetDatum(stats->stanumbers[k][n]);
1740 /* XXX knows more than it should about type float4: */
1741 arry = construct_array(numdatums, nnum,
1743 sizeof(float4), false, 'i');
1744 values[i++] = PointerGetDatum(arry); /* stanumbersN */
1749 values[i++] = (Datum) 0;
1752 for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
1754 int ntxt = stats->numvalues[k];
1758 Datum *txtdatums = (Datum *) palloc(ntxt * sizeof(Datum));
1761 for (n = 0; n < ntxt; n++)
1764 * Convert data values to a text string to be inserted
1765 * into the text array.
1770 FunctionCall3(&out_function,
1771 stats->stavalues[k][n],
1772 ObjectIdGetDatum(stats->attrtype->typelem),
1773 Int32GetDatum(stats->attr->atttypmod));
1774 txtdatums[n] = DirectFunctionCall1(textin, stringdatum);
1775 pfree(DatumGetPointer(stringdatum));
1777 /* XXX knows more than it should about type text: */
1778 arry = construct_array(txtdatums, ntxt,
1781 values[i++] = PointerGetDatum(arry); /* stavaluesN */
1786 values[i++] = (Datum) 0;
1790 /* Is there already a pg_statistic tuple for this attribute? */
1791 oldtup = SearchSysCache(STATRELATT,
1792 ObjectIdGetDatum(relid),
1793 Int16GetDatum(stats->attnum),
1796 if (HeapTupleIsValid(oldtup))
1798 /* Yes, replace it */
1799 stup = heap_modifytuple(oldtup,
1804 ReleaseSysCache(oldtup);
1805 simple_heap_update(sd, &stup->t_self, stup);
1809 /* No, insert new tuple */
1810 stup = heap_formtuple(sd->rd_att, values, nulls);
1811 simple_heap_insert(sd, stup);
1814 /* update indexes too */
1815 CatalogUpdateIndexes(sd, stup);
1817 heap_freetuple(stup);
1820 heap_close(sd, RowExclusiveLock);