PostgreSQL Source Code git master
Loading...
Searching...
No Matches
attribute_stats.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 * attribute_stats.c
3 *
4 * PostgreSQL relation attribute statistics manipulation.
5 *
6 * Code supporting the direct import of relation attribute statistics, similar
7 * to what is done by the ANALYZE command.
8 *
9 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
11 *
12 * IDENTIFICATION
13 * src/backend/statistics/attribute_stats.c
14 *
15 *-------------------------------------------------------------------------
16 */
17
18#include "postgres.h"
19
20#include "access/heapam.h"
21#include "catalog/indexing.h"
22#include "catalog/namespace.h"
23#include "catalog/pg_operator.h"
24#include "nodes/makefuncs.h"
27#include "utils/array.h"
28#include "utils/builtins.h"
29#include "utils/fmgroids.h"
30#include "utils/lsyscache.h"
31#include "utils/syscache.h"
32
33/*
34 * Positional argument numbers, names, and types for
35 * attribute_statistics_update() and pg_restore_attribute_stats().
36 */
37
60
61static struct StatsArgInfo attarginfo[] =
62{
63 [ATTRELSCHEMA_ARG] = {"schemaname", TEXTOID},
64 [ATTRELNAME_ARG] = {"relname", TEXTOID},
65 [ATTNAME_ARG] = {"attname", TEXTOID},
66 [ATTNUM_ARG] = {"attnum", INT2OID},
67 [INHERITED_ARG] = {"inherited", BOOLOID},
68 [NULL_FRAC_ARG] = {"null_frac", FLOAT4OID},
69 [AVG_WIDTH_ARG] = {"avg_width", INT4OID},
70 [N_DISTINCT_ARG] = {"n_distinct", FLOAT4OID},
71 [MOST_COMMON_VALS_ARG] = {"most_common_vals", TEXTOID},
72 [MOST_COMMON_FREQS_ARG] = {"most_common_freqs", FLOAT4ARRAYOID},
73 [HISTOGRAM_BOUNDS_ARG] = {"histogram_bounds", TEXTOID},
74 [CORRELATION_ARG] = {"correlation", FLOAT4OID},
75 [MOST_COMMON_ELEMS_ARG] = {"most_common_elems", TEXTOID},
76 [MOST_COMMON_ELEM_FREQS_ARG] = {"most_common_elem_freqs", FLOAT4ARRAYOID},
77 [ELEM_COUNT_HISTOGRAM_ARG] = {"elem_count_histogram", FLOAT4ARRAYOID},
78 [RANGE_LENGTH_HISTOGRAM_ARG] = {"range_length_histogram", TEXTOID},
79 [RANGE_EMPTY_FRAC_ARG] = {"range_empty_frac", FLOAT4OID},
80 [RANGE_BOUNDS_HISTOGRAM_ARG] = {"range_bounds_histogram", TEXTOID},
82};
83
84/*
85 * Positional argument numbers, names, and types for
86 * pg_clear_attribute_stats().
87 */
88
97
98static struct StatsArgInfo cleararginfo[] =
99{
100 [C_ATTRELSCHEMA_ARG] = {"relation", TEXTOID},
101 [C_ATTRELNAME_ARG] = {"relation", TEXTOID},
102 [C_ATTNAME_ARG] = {"attname", TEXTOID},
103 [C_INHERITED_ARG] = {"inherited", BOOLOID},
105};
106
109 const Datum *values, const bool *nulls, const bool *replaces);
110static bool delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit);
111
112/*
113 * Insert or Update Attribute Statistics
114 *
115 * See pg_statistic.h for an explanation of how each statistic kind is
116 * stored. Custom statistics kinds are not supported.
117 *
118 * Depending on the statistics kind, we need to derive information from the
119 * attribute for which we're storing the stats. For instance, the MCVs are
120 * stored as an anyarray, and the representation of the array needs to store
121 * the correct element type, which must be derived from the attribute.
122 *
123 * Major errors, such as the table not existing, the attribute not existing,
124 * or a permissions failure are always reported at ERROR. Other errors, such
125 * as a conversion failure on one statistic kind, are reported as a WARNING
126 * and other statistic kinds may still be updated.
127 */
128static bool
130{
131 char *nspname;
132 char *relname;
133 Oid reloid;
134 char *attname;
136 bool inherited;
138
141
143 int32 atttypmod;
144 char atttyptype;
146 Oid eq_opr = InvalidOid;
147 Oid lt_opr = InvalidOid;
148
151
153
164
166 bool nulls[Natts_pg_statistic] = {0};
167 bool replaces[Natts_pg_statistic] = {0};
168
169 bool result = true;
170
173
176
177 if (RecoveryInProgress())
180 errmsg("recovery is in progress"),
181 errhint("Statistics cannot be modified during recovery.")));
182
183 /* lock before looking up attribute */
184 reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
187
188 /* user can specify either attname or attnum, but not both */
190 {
194 errmsg("cannot specify both \"%s\" and \"%s\"", "attname", "attnum")));
196 attnum = get_attnum(reloid, attname);
197 /* note that this test covers attisdropped cases too: */
201 errmsg("column \"%s\" of relation \"%s\" does not exist",
202 attname, relname)));
203 }
204 else if (!PG_ARGISNULL(ATTNUM_ARG))
205 {
207 attname = get_attname(reloid, attnum, true);
208 /* annoyingly, get_attname doesn't check attisdropped */
209 if (attname == NULL ||
213 errmsg("column %d of relation \"%s\" does not exist",
214 attnum, relname)));
215 }
216 else
217 {
220 errmsg("must specify either \"%s\" or \"%s\"", "attname", "attnum")));
221 attname = NULL; /* keep compiler quiet */
222 attnum = 0;
223 }
224
225 if (attnum < 0)
228 errmsg("cannot modify statistics on system column \"%s\"",
229 attname)));
230
233
234 /*
235 * Check argument sanity. If some arguments are unusable, emit a WARNING
236 * and set the corresponding argument to NULL in fcinfo.
237 */
238
240 {
241 do_mcv = false;
242 result = false;
243 }
244
246 {
247 do_mcelem = false;
248 result = false;
249 }
251 {
252 do_dechist = false;
253 result = false;
254 }
255
256 if (!stats_check_arg_pair(fcinfo, attarginfo,
258 {
259 do_mcv = false;
260 result = false;
261 }
262
263 if (!stats_check_arg_pair(fcinfo, attarginfo,
266 {
267 do_mcelem = false;
268 result = false;
269 }
270
271 if (!stats_check_arg_pair(fcinfo, attarginfo,
274 {
276 result = false;
277 }
278
279 /* derive information from attribute */
280 statatt_get_type(reloid, attnum,
281 &atttypid, &atttypmod,
283 &eq_opr, &lt_opr);
284
285 /* if needed, derive element type */
286 if (do_mcelem || do_dechist)
287 {
290 {
292 (errmsg("could not determine element type of column \"%s\"", attname),
293 errdetail("Cannot set %s or %s.",
294 "STATISTIC_KIND_MCELEM", "STATISTIC_KIND_DECHIST")));
297
298 do_mcelem = false;
299 do_dechist = false;
300 result = false;
301 }
302 }
303
304 /* histogram and correlation require less-than operator */
305 if ((do_histogram || do_correlation) && !OidIsValid(lt_opr))
306 {
309 errmsg("could not determine less-than operator for column \"%s\"", attname),
310 errdetail("Cannot set %s or %s.",
311 "STATISTIC_KIND_HISTOGRAM", "STATISTIC_KIND_CORRELATION")));
312
313 do_histogram = false;
314 do_correlation = false;
315 result = false;
316 }
317
318 /* only range types can have range stats */
321 {
324 errmsg("column \"%s\" is not a range type", attname),
325 errdetail("Cannot set %s or %s.",
326 "STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM", "STATISTIC_KIND_BOUNDS_HISTOGRAM")));
327
328 do_bounds_histogram = false;
330 result = false;
331 }
332
334
336
338
339 /* initialize from existing tuple if exists */
342 else
344 replaces);
345
346 /* if specified, set to argument values */
348 {
351 }
353 {
356 }
358 {
361 }
362
363 /* STATISTIC_KIND_MCV */
364 if (do_mcv)
365 {
366 bool converted;
368 Datum stavalues = statatt_build_stavalues("most_common_vals",
371 atttypid, atttypmod,
372 &converted);
373
374 if (converted)
375 {
378 eq_opr, atttypcoll,
379 stanumbers, false, stavalues, false);
380 }
381 else
382 result = false;
383 }
384
385 /* STATISTIC_KIND_HISTOGRAM */
386 if (do_histogram)
387 {
388 Datum stavalues;
389 bool converted = false;
390
391 stavalues = statatt_build_stavalues("histogram_bounds",
394 atttypid, atttypmod,
395 &converted);
396
397 if (converted)
398 {
401 lt_opr, atttypcoll,
402 0, true, stavalues, false);
403 }
404 else
405 result = false;
406 }
407
408 /* STATISTIC_KIND_CORRELATION */
409 if (do_correlation)
410 {
413 Datum stanumbers = PointerGetDatum(arry);
414
417 lt_opr, atttypcoll,
418 stanumbers, false, 0, true);
419 }
420
421 /* STATISTIC_KIND_MCELEM */
422 if (do_mcelem)
423 {
425 bool converted = false;
426 Datum stavalues;
427
428 stavalues = statatt_build_stavalues("most_common_elems",
431 elemtypid, atttypmod,
432 &converted);
433
434 if (converted)
435 {
439 stanumbers, false, stavalues, false);
440 }
441 else
442 result = false;
443 }
444
445 /* STATISTIC_KIND_DECHIST */
446 if (do_dechist)
447 {
449
453 stanumbers, false, 0, true);
454 }
455
456 /*
457 * STATISTIC_KIND_BOUNDS_HISTOGRAM
458 *
459 * This stakind appears before STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM even
460 * though it is numerically greater, and all other stakinds appear in
461 * numerical order. We duplicate this quirk for consistency.
462 */
464 {
465 bool converted = false;
466 Datum stavalues;
467
468 stavalues = statatt_build_stavalues("range_bounds_histogram",
471 atttypid, atttypmod,
472 &converted);
473
474 if (converted)
475 {
479 0, true, stavalues, false);
480 }
481 else
482 result = false;
483 }
484
485 /* STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM */
487 {
488 /* The anyarray is always a float8[] for this stakind */
491 Datum stanumbers = PointerGetDatum(arry);
492
493 bool converted = false;
494 Datum stavalues;
495
496 stavalues = statatt_build_stavalues("range_length_histogram",
499 FLOAT8OID, 0, &converted);
500
501 if (converted)
502 {
506 stanumbers, false, stavalues, false);
507 }
508 else
509 result = false;
510 }
511
513
517
518 return result;
519}
520
521/*
522 * Upsert the pg_statistic record.
523 */
524static void
526 const Datum *values, const bool *nulls, const bool *replaces)
527{
529
531 {
533 values, nulls, replaces);
535 }
536 else
537 {
540 }
541
543
545}
546
547/*
548 * Delete pg_statistic record.
549 */
550static bool
552{
555 bool result = false;
556
557 /* Is there already a pg_statistic tuple for this attribute? */
559 ObjectIdGetDatum(reloid),
562
564 {
565 CatalogTupleDelete(sd, &oldtup->t_self);
567 result = true;
568 }
569
571
573
574 return result;
575}
576
577/*
578 * Delete statistics for the given attribute.
579 */
580Datum
582{
583 char *nspname;
584 char *relname;
585 Oid reloid;
586 char *attname;
588 bool inherited;
590
595
598
599 if (RecoveryInProgress())
602 errmsg("recovery is in progress"),
603 errhint("Statistics cannot be modified during recovery.")));
604
605 reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
608
610 attnum = get_attnum(reloid, attname);
611
612 if (attnum < 0)
615 errmsg("cannot clear statistics on system column \"%s\"",
616 attname)));
617
621 errmsg("column \"%s\" of relation \"%s\" does not exist",
622 attname, get_rel_name(reloid))));
623
625
628}
629
630/*
631 * Import statistics for a given relation attribute.
632 *
633 * Inserts or replaces a row in pg_statistic for the given relation and
634 * attribute name or number. It takes input parameters that correspond to
635 * columns in the view pg_stats.
636 *
637 * Parameters are given in a pseudo named-attribute style: they must be
638 * pairs of parameter names (as text) and values (of appropriate types).
639 * We do that, rather than using regular named-parameter notation, so
640 * that we can add or change parameters without fear of breaking
641 * carelessly-written calls.
642 *
643 * Parameters null_frac, avg_width, and n_distinct all correspond to NOT NULL
644 * columns in pg_statistic. The remaining parameters all belong to a specific
645 * stakind. Some stakinds require multiple parameters, which must be specified
646 * together (or neither specified).
647 *
648 * Parameters are only superficially validated. Omitting a parameter or
649 * passing NULL leaves the statistic unchanged.
650 *
651 * Parameters corresponding to ANYARRAY columns are instead passed in as text
652 * values, which is a valid input string for an array of the type or element
653 * type of the attribute. Any error generated by the array_in() function will
654 * in turn fail the function.
655 */
656Datum
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
int16 AttrNumber
Definition attnum.h:21
#define InvalidAttrNumber
Definition attnum.h:23
static bool delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit)
attribute_stats_argnum
@ ATTNUM_ARG
@ RANGE_LENGTH_HISTOGRAM_ARG
@ RANGE_BOUNDS_HISTOGRAM_ARG
@ ATTRELSCHEMA_ARG
@ AVG_WIDTH_ARG
@ INHERITED_ARG
@ ATTRELNAME_ARG
@ MOST_COMMON_ELEMS_ARG
@ NULL_FRAC_ARG
@ NUM_ATTRIBUTE_STATS_ARGS
@ MOST_COMMON_FREQS_ARG
@ CORRELATION_ARG
@ HISTOGRAM_BOUNDS_ARG
@ MOST_COMMON_VALS_ARG
@ RANGE_EMPTY_FRAC_ARG
@ ELEM_COUNT_HISTOGRAM_ARG
@ ATTNAME_ARG
@ N_DISTINCT_ARG
@ MOST_COMMON_ELEM_FREQS_ARG
static struct StatsArgInfo cleararginfo[]
static void upsert_pg_statistic(Relation starel, HeapTuple oldtup, const Datum *values, const bool *nulls, const bool *replaces)
static struct StatsArgInfo attarginfo[]
static bool attribute_statistics_update(FunctionCallInfo fcinfo)
clear_attribute_stats_argnum
@ C_INHERITED_ARG
@ C_NUM_ATTRIBUTE_STATS_ARGS
@ C_ATTNAME_ARG
@ C_ATTRELNAME_ARG
@ C_ATTRELSCHEMA_ARG
Datum pg_clear_attribute_stats(PG_FUNCTION_ARGS)
Datum pg_restore_attribute_stats(PG_FUNCTION_ARGS)
static Datum values[MAXATTR]
Definition bootstrap.c:155
#define TextDatumGetCString(d)
Definition builtins.h:98
int32_t int32
Definition c.h:552
#define OidIsValid(objectId)
Definition c.h:798
int errdetail(const char *fmt,...)
Definition elog.c:1216
int errhint(const char *fmt,...)
Definition elog.c:1330
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define WARNING
Definition elog.h:36
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition fmgr.c:128
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition fmgr.h:150
#define PG_ARGISNULL(n)
Definition fmgr.h:209
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define LOCAL_FCINFO(name, nargs)
Definition fmgr.h:110
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define PG_GETARG_INT16(n)
Definition fmgr.h:271
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1210
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1117
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition heaptuple.c:1346
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition htup.h:78
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition indexing.c:233
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition indexing.c:365
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
#define RowExclusiveLock
Definition lockdefs.h:38
char * get_rel_name(Oid relid)
Definition lsyscache.c:2078
AttrNumber get_attnum(Oid relid, const char *attname)
Definition lsyscache.c:934
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition lsyscache.c:903
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition makefuncs.c:473
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition namespace.c:440
NameData attname
int16 attnum
NameData relname
Definition pg_class.h:38
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Datum Int16GetDatum(int16 X)
Definition postgres.h:182
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
unsigned int Oid
static int fb(int x)
#define RelationGetDescr(relation)
Definition rel.h:540
bool statatt_get_elem_type(Oid atttypid, char atttyptype, Oid *elemtypid, Oid *elem_eq_opr)
Definition stat_utils.c:523
Datum statatt_build_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid typid, int32 typmod, bool *ok)
Definition stat_utils.c:567
bool stats_fill_fcinfo_from_arg_pairs(FunctionCallInfo pairs_fcinfo, FunctionCallInfo positional_fcinfo, struct StatsArgInfo *arginfo)
Definition stat_utils.c:348
void RangeVarCallbackForStats(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition stat_utils.c:142
void statatt_init_empty_tuple(Oid reloid, int16 attnum, bool inherited, Datum *values, bool *nulls, bool *replaces)
Definition stat_utils.c:713
bool stats_check_arg_array(FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum)
Definition stat_utils.c:70
void statatt_get_type(Oid reloid, AttrNumber attnum, Oid *atttypid, int32 *atttypmod, char *atttyptype, Oid *atttypcoll, Oid *eq_opr, Oid *lt_opr)
Definition stat_utils.c:437
void stats_check_required_arg(FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum)
Definition stat_utils.c:51
void statatt_set_slot(Datum *values, bool *nulls, bool *replaces, int16 stakind, Oid staop, Oid stacoll, Datum stanumbers, bool stanumbers_isnull, Datum stavalues, bool stavalues_isnull)
Definition stat_utils.c:635
bool stats_check_arg_pair(FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum1, int argnum2)
Definition stat_utils.c:111
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Definition syscache.c:240
bool SearchSysCacheExistsAttName(Oid relid, const char *attname)
Definition syscache.c:517
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
void CommandCounterIncrement(void)
Definition xact.c:1101
bool RecoveryInProgress(void)
Definition xlog.c:6461