Line data Source code
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-2025, 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_collation.h"
24 : #include "catalog/pg_operator.h"
25 : #include "nodes/makefuncs.h"
26 : #include "nodes/nodeFuncs.h"
27 : #include "statistics/statistics.h"
28 : #include "statistics/stat_utils.h"
29 : #include "utils/array.h"
30 : #include "utils/builtins.h"
31 : #include "utils/fmgroids.h"
32 : #include "utils/lsyscache.h"
33 : #include "utils/syscache.h"
34 :
35 : #define DEFAULT_NULL_FRAC Float4GetDatum(0.0)
36 : #define DEFAULT_AVG_WIDTH Int32GetDatum(0) /* unknown */
37 : #define DEFAULT_N_DISTINCT Float4GetDatum(0.0) /* unknown */
38 :
39 : /*
40 : * Positional argument numbers, names, and types for
41 : * attribute_statistics_update() and pg_restore_attribute_stats().
42 : */
43 :
44 : enum attribute_stats_argnum
45 : {
46 : ATTRELSCHEMA_ARG = 0,
47 : ATTRELNAME_ARG,
48 : ATTNAME_ARG,
49 : ATTNUM_ARG,
50 : INHERITED_ARG,
51 : NULL_FRAC_ARG,
52 : AVG_WIDTH_ARG,
53 : N_DISTINCT_ARG,
54 : MOST_COMMON_VALS_ARG,
55 : MOST_COMMON_FREQS_ARG,
56 : HISTOGRAM_BOUNDS_ARG,
57 : CORRELATION_ARG,
58 : MOST_COMMON_ELEMS_ARG,
59 : MOST_COMMON_ELEM_FREQS_ARG,
60 : ELEM_COUNT_HISTOGRAM_ARG,
61 : RANGE_LENGTH_HISTOGRAM_ARG,
62 : RANGE_EMPTY_FRAC_ARG,
63 : RANGE_BOUNDS_HISTOGRAM_ARG,
64 : NUM_ATTRIBUTE_STATS_ARGS
65 : };
66 :
67 : static struct StatsArgInfo attarginfo[] =
68 : {
69 : [ATTRELSCHEMA_ARG] = {"schemaname", TEXTOID},
70 : [ATTRELNAME_ARG] = {"relname", TEXTOID},
71 : [ATTNAME_ARG] = {"attname", TEXTOID},
72 : [ATTNUM_ARG] = {"attnum", INT2OID},
73 : [INHERITED_ARG] = {"inherited", BOOLOID},
74 : [NULL_FRAC_ARG] = {"null_frac", FLOAT4OID},
75 : [AVG_WIDTH_ARG] = {"avg_width", INT4OID},
76 : [N_DISTINCT_ARG] = {"n_distinct", FLOAT4OID},
77 : [MOST_COMMON_VALS_ARG] = {"most_common_vals", TEXTOID},
78 : [MOST_COMMON_FREQS_ARG] = {"most_common_freqs", FLOAT4ARRAYOID},
79 : [HISTOGRAM_BOUNDS_ARG] = {"histogram_bounds", TEXTOID},
80 : [CORRELATION_ARG] = {"correlation", FLOAT4OID},
81 : [MOST_COMMON_ELEMS_ARG] = {"most_common_elems", TEXTOID},
82 : [MOST_COMMON_ELEM_FREQS_ARG] = {"most_common_elem_freqs", FLOAT4ARRAYOID},
83 : [ELEM_COUNT_HISTOGRAM_ARG] = {"elem_count_histogram", FLOAT4ARRAYOID},
84 : [RANGE_LENGTH_HISTOGRAM_ARG] = {"range_length_histogram", TEXTOID},
85 : [RANGE_EMPTY_FRAC_ARG] = {"range_empty_frac", FLOAT4OID},
86 : [RANGE_BOUNDS_HISTOGRAM_ARG] = {"range_bounds_histogram", TEXTOID},
87 : [NUM_ATTRIBUTE_STATS_ARGS] = {0}
88 : };
89 :
90 : /*
91 : * Positional argument numbers, names, and types for
92 : * pg_clear_attribute_stats().
93 : */
94 :
95 : enum clear_attribute_stats_argnum
96 : {
97 : C_ATTRELSCHEMA_ARG = 0,
98 : C_ATTRELNAME_ARG,
99 : C_ATTNAME_ARG,
100 : C_INHERITED_ARG,
101 : C_NUM_ATTRIBUTE_STATS_ARGS
102 : };
103 :
104 : static struct StatsArgInfo cleararginfo[] =
105 : {
106 : [C_ATTRELSCHEMA_ARG] = {"relation", TEXTOID},
107 : [C_ATTRELNAME_ARG] = {"relation", TEXTOID},
108 : [C_ATTNAME_ARG] = {"attname", TEXTOID},
109 : [C_INHERITED_ARG] = {"inherited", BOOLOID},
110 : [C_NUM_ATTRIBUTE_STATS_ARGS] = {0}
111 : };
112 :
113 : static bool attribute_statistics_update(FunctionCallInfo fcinfo);
114 : static Node *get_attr_expr(Relation rel, int attnum);
115 : static void get_attr_stat_type(Oid reloid, AttrNumber attnum,
116 : Oid *atttypid, int32 *atttypmod,
117 : char *atttyptype, Oid *atttypcoll,
118 : Oid *eq_opr, Oid *lt_opr);
119 : static bool get_elem_stat_type(Oid atttypid, char atttyptype,
120 : Oid *elemtypid, Oid *elem_eq_opr);
121 : static Datum text_to_stavalues(const char *staname, FmgrInfo *array_in, Datum d,
122 : Oid typid, int32 typmod, bool *ok);
123 : static void set_stats_slot(Datum *values, bool *nulls, bool *replaces,
124 : int16 stakind, Oid staop, Oid stacoll,
125 : Datum stanumbers, bool stanumbers_isnull,
126 : Datum stavalues, bool stavalues_isnull);
127 : static void upsert_pg_statistic(Relation starel, HeapTuple oldtup,
128 : const Datum *values, const bool *nulls, const bool *replaces);
129 : static bool delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit);
130 : static void init_empty_stats_tuple(Oid reloid, int16 attnum, bool inherited,
131 : Datum *values, bool *nulls, bool *replaces);
132 :
133 : /*
134 : * Insert or Update Attribute Statistics
135 : *
136 : * See pg_statistic.h for an explanation of how each statistic kind is
137 : * stored. Custom statistics kinds are not supported.
138 : *
139 : * Depending on the statistics kind, we need to derive information from the
140 : * attribute for which we're storing the stats. For instance, the MCVs are
141 : * stored as an anyarray, and the representation of the array needs to store
142 : * the correct element type, which must be derived from the attribute.
143 : *
144 : * Major errors, such as the table not existing, the attribute not existing,
145 : * or a permissions failure are always reported at ERROR. Other errors, such
146 : * as a conversion failure on one statistic kind, are reported as a WARNING
147 : * and other statistic kinds may still be updated.
148 : */
149 : static bool
150 1436 : attribute_statistics_update(FunctionCallInfo fcinfo)
151 : {
152 : char *nspname;
153 : char *relname;
154 : Oid reloid;
155 : char *attname;
156 : AttrNumber attnum;
157 : bool inherited;
158 1436 : Oid locked_table = InvalidOid;
159 :
160 : Relation starel;
161 : HeapTuple statup;
162 :
163 1436 : Oid atttypid = InvalidOid;
164 : int32 atttypmod;
165 : char atttyptype;
166 1436 : Oid atttypcoll = InvalidOid;
167 1436 : Oid eq_opr = InvalidOid;
168 1436 : Oid lt_opr = InvalidOid;
169 :
170 1436 : Oid elemtypid = InvalidOid;
171 1436 : Oid elem_eq_opr = InvalidOid;
172 :
173 : FmgrInfo array_in_fn;
174 :
175 2080 : bool do_mcv = !PG_ARGISNULL(MOST_COMMON_FREQS_ARG) &&
176 644 : !PG_ARGISNULL(MOST_COMMON_VALS_ARG);
177 1436 : bool do_histogram = !PG_ARGISNULL(HISTOGRAM_BOUNDS_ARG);
178 1436 : bool do_correlation = !PG_ARGISNULL(CORRELATION_ARG);
179 1482 : bool do_mcelem = !PG_ARGISNULL(MOST_COMMON_ELEMS_ARG) &&
180 46 : !PG_ARGISNULL(MOST_COMMON_ELEM_FREQS_ARG);
181 1436 : bool do_dechist = !PG_ARGISNULL(ELEM_COUNT_HISTOGRAM_ARG);
182 1436 : bool do_bounds_histogram = !PG_ARGISNULL(RANGE_BOUNDS_HISTOGRAM_ARG);
183 1466 : bool do_range_length_histogram = !PG_ARGISNULL(RANGE_LENGTH_HISTOGRAM_ARG) &&
184 30 : !PG_ARGISNULL(RANGE_EMPTY_FRAC_ARG);
185 :
186 1436 : Datum values[Natts_pg_statistic] = {0};
187 1436 : bool nulls[Natts_pg_statistic] = {0};
188 1436 : bool replaces[Natts_pg_statistic] = {0};
189 :
190 1436 : bool result = true;
191 :
192 1436 : stats_check_required_arg(fcinfo, attarginfo, ATTRELSCHEMA_ARG);
193 1430 : stats_check_required_arg(fcinfo, attarginfo, ATTRELNAME_ARG);
194 :
195 1418 : nspname = TextDatumGetCString(PG_GETARG_DATUM(ATTRELSCHEMA_ARG));
196 1418 : relname = TextDatumGetCString(PG_GETARG_DATUM(ATTRELNAME_ARG));
197 :
198 1418 : if (RecoveryInProgress())
199 0 : ereport(ERROR,
200 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
201 : errmsg("recovery is in progress"),
202 : errhint("Statistics cannot be modified during recovery.")));
203 :
204 : /* lock before looking up attribute */
205 1418 : reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
206 : ShareUpdateExclusiveLock, 0,
207 : RangeVarCallbackForStats, &locked_table);
208 :
209 : /* user can specify either attname or attnum, but not both */
210 1406 : if (!PG_ARGISNULL(ATTNAME_ARG))
211 : {
212 1380 : if (!PG_ARGISNULL(ATTNUM_ARG))
213 6 : ereport(ERROR,
214 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
215 : errmsg("cannot specify both \"%s\" and \"%s\"", "attname", "attnum")));
216 1374 : attname = TextDatumGetCString(PG_GETARG_DATUM(ATTNAME_ARG));
217 1374 : attnum = get_attnum(reloid, attname);
218 : /* note that this test covers attisdropped cases too: */
219 1374 : if (attnum == InvalidAttrNumber)
220 6 : ereport(ERROR,
221 : (errcode(ERRCODE_UNDEFINED_COLUMN),
222 : errmsg("column \"%s\" of relation \"%s\" does not exist",
223 : attname, relname)));
224 : }
225 26 : else if (!PG_ARGISNULL(ATTNUM_ARG))
226 : {
227 14 : attnum = PG_GETARG_INT16(ATTNUM_ARG);
228 14 : attname = get_attname(reloid, attnum, true);
229 : /* annoyingly, get_attname doesn't check attisdropped */
230 14 : if (attname == NULL ||
231 14 : !SearchSysCacheExistsAttName(reloid, attname))
232 0 : ereport(ERROR,
233 : (errcode(ERRCODE_UNDEFINED_COLUMN),
234 : errmsg("column %d of relation \"%s\" does not exist",
235 : attnum, relname)));
236 : }
237 : else
238 : {
239 12 : ereport(ERROR,
240 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
241 : errmsg("must specify either \"%s\" or \"%s\"", "attname", "attnum")));
242 : attname = NULL; /* keep compiler quiet */
243 : attnum = 0;
244 : }
245 :
246 1382 : if (attnum < 0)
247 6 : ereport(ERROR,
248 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
249 : errmsg("cannot modify statistics on system column \"%s\"",
250 : attname)));
251 :
252 1376 : stats_check_required_arg(fcinfo, attarginfo, INHERITED_ARG);
253 1370 : inherited = PG_GETARG_BOOL(INHERITED_ARG);
254 :
255 : /*
256 : * Check argument sanity. If some arguments are unusable, emit a WARNING
257 : * and set the corresponding argument to NULL in fcinfo.
258 : */
259 :
260 1370 : if (!stats_check_arg_array(fcinfo, attarginfo, MOST_COMMON_FREQS_ARG))
261 : {
262 0 : do_mcv = false;
263 0 : result = false;
264 : }
265 :
266 1370 : if (!stats_check_arg_array(fcinfo, attarginfo, MOST_COMMON_ELEM_FREQS_ARG))
267 : {
268 0 : do_mcelem = false;
269 0 : result = false;
270 : }
271 1370 : if (!stats_check_arg_array(fcinfo, attarginfo, ELEM_COUNT_HISTOGRAM_ARG))
272 : {
273 6 : do_dechist = false;
274 6 : result = false;
275 : }
276 :
277 1370 : if (!stats_check_arg_pair(fcinfo, attarginfo,
278 : MOST_COMMON_VALS_ARG, MOST_COMMON_FREQS_ARG))
279 : {
280 18 : do_mcv = false;
281 18 : result = false;
282 : }
283 :
284 1370 : if (!stats_check_arg_pair(fcinfo, attarginfo,
285 : MOST_COMMON_ELEMS_ARG,
286 : MOST_COMMON_ELEM_FREQS_ARG))
287 : {
288 12 : do_mcelem = false;
289 12 : result = false;
290 : }
291 :
292 1370 : if (!stats_check_arg_pair(fcinfo, attarginfo,
293 : RANGE_LENGTH_HISTOGRAM_ARG,
294 : RANGE_EMPTY_FRAC_ARG))
295 : {
296 12 : do_range_length_histogram = false;
297 12 : result = false;
298 : }
299 :
300 : /* derive information from attribute */
301 1370 : get_attr_stat_type(reloid, attnum,
302 : &atttypid, &atttypmod,
303 : &atttyptype, &atttypcoll,
304 : &eq_opr, <_opr);
305 :
306 : /* if needed, derive element type */
307 1370 : if (do_mcelem || do_dechist)
308 : {
309 52 : if (!get_elem_stat_type(atttypid, atttyptype,
310 : &elemtypid, &elem_eq_opr))
311 : {
312 18 : ereport(WARNING,
313 : (errmsg("could not determine element type of column \"%s\"", attname),
314 : errdetail("Cannot set %s or %s.",
315 : "STATISTIC_KIND_MCELEM", "STATISTIC_KIND_DECHIST")));
316 18 : elemtypid = InvalidOid;
317 18 : elem_eq_opr = InvalidOid;
318 :
319 18 : do_mcelem = false;
320 18 : do_dechist = false;
321 18 : result = false;
322 : }
323 : }
324 :
325 : /* histogram and correlation require less-than operator */
326 1370 : if ((do_histogram || do_correlation) && !OidIsValid(lt_opr))
327 : {
328 0 : ereport(WARNING,
329 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
330 : errmsg("could not determine less-than operator for column \"%s\"", attname),
331 : errdetail("Cannot set %s or %s.",
332 : "STATISTIC_KIND_HISTOGRAM", "STATISTIC_KIND_CORRELATION")));
333 :
334 0 : do_histogram = false;
335 0 : do_correlation = false;
336 0 : result = false;
337 : }
338 :
339 : /* only range types can have range stats */
340 1370 : if ((do_range_length_histogram || do_bounds_histogram) &&
341 36 : !(atttyptype == TYPTYPE_RANGE || atttyptype == TYPTYPE_MULTIRANGE))
342 : {
343 12 : ereport(WARNING,
344 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
345 : errmsg("column \"%s\" is not a range type", attname),
346 : errdetail("Cannot set %s or %s.",
347 : "STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM", "STATISTIC_KIND_BOUNDS_HISTOGRAM")));
348 :
349 12 : do_bounds_histogram = false;
350 12 : do_range_length_histogram = false;
351 12 : result = false;
352 : }
353 :
354 1370 : fmgr_info(F_ARRAY_IN, &array_in_fn);
355 :
356 1370 : starel = table_open(StatisticRelationId, RowExclusiveLock);
357 :
358 1370 : statup = SearchSysCache3(STATRELATTINH, ObjectIdGetDatum(reloid), Int16GetDatum(attnum), BoolGetDatum(inherited));
359 :
360 : /* initialize from existing tuple if exists */
361 1370 : if (HeapTupleIsValid(statup))
362 126 : heap_deform_tuple(statup, RelationGetDescr(starel), values, nulls);
363 : else
364 1244 : init_empty_stats_tuple(reloid, attnum, inherited, values, nulls,
365 : replaces);
366 :
367 : /* if specified, set to argument values */
368 1370 : if (!PG_ARGISNULL(NULL_FRAC_ARG))
369 : {
370 1340 : values[Anum_pg_statistic_stanullfrac - 1] = PG_GETARG_DATUM(NULL_FRAC_ARG);
371 1340 : replaces[Anum_pg_statistic_stanullfrac - 1] = true;
372 : }
373 1370 : if (!PG_ARGISNULL(AVG_WIDTH_ARG))
374 : {
375 1226 : values[Anum_pg_statistic_stawidth - 1] = PG_GETARG_DATUM(AVG_WIDTH_ARG);
376 1226 : replaces[Anum_pg_statistic_stawidth - 1] = true;
377 : }
378 1370 : if (!PG_ARGISNULL(N_DISTINCT_ARG))
379 : {
380 1226 : values[Anum_pg_statistic_stadistinct - 1] = PG_GETARG_DATUM(N_DISTINCT_ARG);
381 1226 : replaces[Anum_pg_statistic_stadistinct - 1] = true;
382 : }
383 :
384 : /* STATISTIC_KIND_MCV */
385 1370 : if (do_mcv)
386 : {
387 : bool converted;
388 638 : Datum stanumbers = PG_GETARG_DATUM(MOST_COMMON_FREQS_ARG);
389 638 : Datum stavalues = text_to_stavalues("most_common_vals",
390 : &array_in_fn,
391 : PG_GETARG_DATUM(MOST_COMMON_VALS_ARG),
392 : atttypid, atttypmod,
393 : &converted);
394 :
395 638 : if (converted)
396 : {
397 632 : set_stats_slot(values, nulls, replaces,
398 : STATISTIC_KIND_MCV,
399 : eq_opr, atttypcoll,
400 : stanumbers, false, stavalues, false);
401 : }
402 : else
403 6 : result = false;
404 : }
405 :
406 : /* STATISTIC_KIND_HISTOGRAM */
407 1370 : if (do_histogram)
408 : {
409 : Datum stavalues;
410 616 : bool converted = false;
411 :
412 616 : stavalues = text_to_stavalues("histogram_bounds",
413 : &array_in_fn,
414 : PG_GETARG_DATUM(HISTOGRAM_BOUNDS_ARG),
415 : atttypid, atttypmod,
416 : &converted);
417 :
418 616 : if (converted)
419 : {
420 610 : set_stats_slot(values, nulls, replaces,
421 : STATISTIC_KIND_HISTOGRAM,
422 : lt_opr, atttypcoll,
423 : 0, true, stavalues, false);
424 : }
425 : else
426 6 : result = false;
427 : }
428 :
429 : /* STATISTIC_KIND_CORRELATION */
430 1370 : if (do_correlation)
431 : {
432 1152 : Datum elems[] = {PG_GETARG_DATUM(CORRELATION_ARG)};
433 1152 : ArrayType *arry = construct_array_builtin(elems, 1, FLOAT4OID);
434 1152 : Datum stanumbers = PointerGetDatum(arry);
435 :
436 1152 : set_stats_slot(values, nulls, replaces,
437 : STATISTIC_KIND_CORRELATION,
438 : lt_opr, atttypcoll,
439 : stanumbers, false, 0, true);
440 : }
441 :
442 : /* STATISTIC_KIND_MCELEM */
443 1370 : if (do_mcelem)
444 : {
445 28 : Datum stanumbers = PG_GETARG_DATUM(MOST_COMMON_ELEM_FREQS_ARG);
446 28 : bool converted = false;
447 : Datum stavalues;
448 :
449 28 : stavalues = text_to_stavalues("most_common_elems",
450 : &array_in_fn,
451 : PG_GETARG_DATUM(MOST_COMMON_ELEMS_ARG),
452 : elemtypid, atttypmod,
453 : &converted);
454 :
455 28 : if (converted)
456 : {
457 28 : set_stats_slot(values, nulls, replaces,
458 : STATISTIC_KIND_MCELEM,
459 : elem_eq_opr, atttypcoll,
460 : stanumbers, false, stavalues, false);
461 : }
462 : else
463 0 : result = false;
464 : }
465 :
466 : /* STATISTIC_KIND_DECHIST */
467 1370 : if (do_dechist)
468 : {
469 26 : Datum stanumbers = PG_GETARG_DATUM(ELEM_COUNT_HISTOGRAM_ARG);
470 :
471 26 : set_stats_slot(values, nulls, replaces,
472 : STATISTIC_KIND_DECHIST,
473 : elem_eq_opr, atttypcoll,
474 : stanumbers, false, 0, true);
475 : }
476 :
477 : /*
478 : * STATISTIC_KIND_BOUNDS_HISTOGRAM
479 : *
480 : * This stakind appears before STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM even
481 : * though it is numerically greater, and all other stakinds appear in
482 : * numerical order. We duplicate this quirk for consistency.
483 : */
484 1370 : if (do_bounds_histogram)
485 : {
486 18 : bool converted = false;
487 : Datum stavalues;
488 :
489 18 : stavalues = text_to_stavalues("range_bounds_histogram",
490 : &array_in_fn,
491 : PG_GETARG_DATUM(RANGE_BOUNDS_HISTOGRAM_ARG),
492 : atttypid, atttypmod,
493 : &converted);
494 :
495 18 : if (converted)
496 : {
497 18 : set_stats_slot(values, nulls, replaces,
498 : STATISTIC_KIND_BOUNDS_HISTOGRAM,
499 : InvalidOid, InvalidOid,
500 : 0, true, stavalues, false);
501 : }
502 : else
503 0 : result = false;
504 : }
505 :
506 : /* STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM */
507 1370 : if (do_range_length_histogram)
508 : {
509 : /* The anyarray is always a float8[] for this stakind */
510 18 : Datum elems[] = {PG_GETARG_DATUM(RANGE_EMPTY_FRAC_ARG)};
511 18 : ArrayType *arry = construct_array_builtin(elems, 1, FLOAT4OID);
512 18 : Datum stanumbers = PointerGetDatum(arry);
513 :
514 18 : bool converted = false;
515 : Datum stavalues;
516 :
517 18 : stavalues = text_to_stavalues("range_length_histogram",
518 : &array_in_fn,
519 : PG_GETARG_DATUM(RANGE_LENGTH_HISTOGRAM_ARG),
520 : FLOAT8OID, 0, &converted);
521 :
522 18 : if (converted)
523 : {
524 18 : set_stats_slot(values, nulls, replaces,
525 : STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM,
526 : Float8LessOperator, InvalidOid,
527 : stanumbers, false, stavalues, false);
528 : }
529 : else
530 0 : result = false;
531 : }
532 :
533 1370 : upsert_pg_statistic(starel, statup, values, nulls, replaces);
534 :
535 1370 : if (HeapTupleIsValid(statup))
536 126 : ReleaseSysCache(statup);
537 1370 : table_close(starel, RowExclusiveLock);
538 :
539 1370 : return result;
540 : }
541 :
542 : /*
543 : * If this relation is an index and that index has expressions in it, and
544 : * the attnum specified is known to be an expression, then we must walk
545 : * the list attributes up to the specified attnum to get the right
546 : * expression.
547 : */
548 : static Node *
549 1370 : get_attr_expr(Relation rel, int attnum)
550 : {
551 : List *index_exprs;
552 : ListCell *indexpr_item;
553 :
554 : /* relation is not an index */
555 1370 : if (rel->rd_rel->relkind != RELKIND_INDEX &&
556 1356 : rel->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
557 1356 : return NULL;
558 :
559 14 : index_exprs = RelationGetIndexExpressions(rel);
560 :
561 : /* index has no expressions to give */
562 14 : if (index_exprs == NIL)
563 0 : return NULL;
564 :
565 : /*
566 : * The index attnum points directly to a relation attnum, then it's not an
567 : * expression attribute.
568 : */
569 14 : if (rel->rd_index->indkey.values[attnum - 1] != 0)
570 0 : return NULL;
571 :
572 14 : indexpr_item = list_head(rel->rd_indexprs);
573 :
574 14 : for (int i = 0; i < attnum - 1; i++)
575 0 : if (rel->rd_index->indkey.values[i] == 0)
576 0 : indexpr_item = lnext(rel->rd_indexprs, indexpr_item);
577 :
578 14 : if (indexpr_item == NULL) /* shouldn't happen */
579 0 : elog(ERROR, "too few entries in indexprs list");
580 :
581 14 : return (Node *) lfirst(indexpr_item);
582 : }
583 :
584 : /*
585 : * Derive type information from the attribute.
586 : */
587 : static void
588 1370 : get_attr_stat_type(Oid reloid, AttrNumber attnum,
589 : Oid *atttypid, int32 *atttypmod,
590 : char *atttyptype, Oid *atttypcoll,
591 : Oid *eq_opr, Oid *lt_opr)
592 : {
593 1370 : Relation rel = relation_open(reloid, AccessShareLock);
594 : Form_pg_attribute attr;
595 : HeapTuple atup;
596 : Node *expr;
597 : TypeCacheEntry *typcache;
598 :
599 1370 : atup = SearchSysCache2(ATTNUM, ObjectIdGetDatum(reloid),
600 : Int16GetDatum(attnum));
601 :
602 : /* Attribute not found */
603 1370 : if (!HeapTupleIsValid(atup))
604 0 : ereport(ERROR,
605 : (errcode(ERRCODE_UNDEFINED_COLUMN),
606 : errmsg("column %d of relation \"%s\" does not exist",
607 : attnum, RelationGetRelationName(rel))));
608 :
609 1370 : attr = (Form_pg_attribute) GETSTRUCT(atup);
610 :
611 1370 : if (attr->attisdropped)
612 0 : ereport(ERROR,
613 : (errcode(ERRCODE_UNDEFINED_COLUMN),
614 : errmsg("column %d of relation \"%s\" does not exist",
615 : attnum, RelationGetRelationName(rel))));
616 :
617 1370 : expr = get_attr_expr(rel, attr->attnum);
618 :
619 : /*
620 : * When analyzing an expression index, believe the expression tree's type
621 : * not the column datatype --- the latter might be the opckeytype storage
622 : * type of the opclass, which is not interesting for our purposes. This
623 : * mimics the behavior of examine_attribute().
624 : */
625 1370 : if (expr == NULL)
626 : {
627 1356 : *atttypid = attr->atttypid;
628 1356 : *atttypmod = attr->atttypmod;
629 1356 : *atttypcoll = attr->attcollation;
630 : }
631 : else
632 : {
633 14 : *atttypid = exprType(expr);
634 14 : *atttypmod = exprTypmod(expr);
635 :
636 14 : if (OidIsValid(attr->attcollation))
637 0 : *atttypcoll = attr->attcollation;
638 : else
639 14 : *atttypcoll = exprCollation(expr);
640 : }
641 1370 : ReleaseSysCache(atup);
642 :
643 : /*
644 : * If it's a multirange, step down to the range type, as is done by
645 : * multirange_typanalyze().
646 : */
647 1370 : if (type_is_multirange(*atttypid))
648 2 : *atttypid = get_multirange_range(*atttypid);
649 :
650 : /* finds the right operators even if atttypid is a domain */
651 1370 : typcache = lookup_type_cache(*atttypid, TYPECACHE_LT_OPR | TYPECACHE_EQ_OPR);
652 1370 : *atttyptype = typcache->typtype;
653 1370 : *eq_opr = typcache->eq_opr;
654 1370 : *lt_opr = typcache->lt_opr;
655 :
656 : /*
657 : * Special case: collation for tsvector is DEFAULT_COLLATION_OID. See
658 : * compute_tsvector_stats().
659 : */
660 1370 : if (*atttypid == TSVECTOROID)
661 2 : *atttypcoll = DEFAULT_COLLATION_OID;
662 :
663 1370 : relation_close(rel, NoLock);
664 1370 : }
665 :
666 : /*
667 : * Derive element type information from the attribute type.
668 : */
669 : static bool
670 52 : get_elem_stat_type(Oid atttypid, char atttyptype,
671 : Oid *elemtypid, Oid *elem_eq_opr)
672 : {
673 : TypeCacheEntry *elemtypcache;
674 :
675 52 : if (atttypid == TSVECTOROID)
676 : {
677 : /*
678 : * Special case: element type for tsvector is text. See
679 : * compute_tsvector_stats().
680 : */
681 2 : *elemtypid = TEXTOID;
682 : }
683 : else
684 : {
685 : /* find underlying element type through any domain */
686 50 : *elemtypid = get_base_element_type(atttypid);
687 : }
688 :
689 52 : if (!OidIsValid(*elemtypid))
690 18 : return false;
691 :
692 : /* finds the right operator even if elemtypid is a domain */
693 34 : elemtypcache = lookup_type_cache(*elemtypid, TYPECACHE_EQ_OPR);
694 34 : if (!OidIsValid(elemtypcache->eq_opr))
695 0 : return false;
696 :
697 34 : *elem_eq_opr = elemtypcache->eq_opr;
698 :
699 34 : return true;
700 : }
701 :
702 : /*
703 : * Cast a text datum into an array with element type elemtypid.
704 : *
705 : * If an error is encountered, capture it and re-throw a WARNING, and set ok
706 : * to false. If the resulting array contains NULLs, raise a WARNING and set ok
707 : * to false. Otherwise, set ok to true.
708 : */
709 : static Datum
710 1318 : text_to_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid typid,
711 : int32 typmod, bool *ok)
712 : {
713 1318 : LOCAL_FCINFO(fcinfo, 8);
714 : char *s;
715 : Datum result;
716 1318 : ErrorSaveContext escontext = {T_ErrorSaveContext};
717 :
718 1318 : escontext.details_wanted = true;
719 :
720 1318 : s = TextDatumGetCString(d);
721 :
722 1318 : InitFunctionCallInfoData(*fcinfo, array_in, 3, InvalidOid,
723 : (Node *) &escontext, NULL);
724 :
725 1318 : fcinfo->args[0].value = CStringGetDatum(s);
726 1318 : fcinfo->args[0].isnull = false;
727 1318 : fcinfo->args[1].value = ObjectIdGetDatum(typid);
728 1318 : fcinfo->args[1].isnull = false;
729 1318 : fcinfo->args[2].value = Int32GetDatum(typmod);
730 1318 : fcinfo->args[2].isnull = false;
731 :
732 1318 : result = FunctionCallInvoke(fcinfo);
733 :
734 1318 : pfree(s);
735 :
736 1318 : if (escontext.error_occurred)
737 : {
738 6 : escontext.error_data->elevel = WARNING;
739 6 : ThrowErrorData(escontext.error_data);
740 6 : *ok = false;
741 6 : return (Datum) 0;
742 : }
743 :
744 1312 : if (array_contains_nulls(DatumGetArrayTypeP(result)))
745 : {
746 6 : ereport(WARNING,
747 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
748 : errmsg("\"%s\" array must not contain null values", staname)));
749 6 : *ok = false;
750 6 : return (Datum) 0;
751 : }
752 :
753 1306 : *ok = true;
754 :
755 1306 : return result;
756 : }
757 :
758 : /*
759 : * Find and update the slot with the given stakind, or use the first empty
760 : * slot.
761 : */
762 : static void
763 2484 : set_stats_slot(Datum *values, bool *nulls, bool *replaces,
764 : int16 stakind, Oid staop, Oid stacoll,
765 : Datum stanumbers, bool stanumbers_isnull,
766 : Datum stavalues, bool stavalues_isnull)
767 : {
768 : int slotidx;
769 2484 : int first_empty = -1;
770 : AttrNumber stakind_attnum;
771 : AttrNumber staop_attnum;
772 : AttrNumber stacoll_attnum;
773 :
774 : /* find existing slot with given stakind */
775 14904 : for (slotidx = 0; slotidx < STATISTIC_NUM_SLOTS; slotidx++)
776 : {
777 12420 : stakind_attnum = Anum_pg_statistic_stakind1 - 1 + slotidx;
778 :
779 16342 : if (first_empty < 0 &&
780 3922 : DatumGetInt16(values[stakind_attnum]) == 0)
781 2484 : first_empty = slotidx;
782 12420 : if (DatumGetInt16(values[stakind_attnum]) == stakind)
783 0 : break;
784 : }
785 :
786 2484 : if (slotidx >= STATISTIC_NUM_SLOTS && first_empty >= 0)
787 2484 : slotidx = first_empty;
788 :
789 2484 : if (slotidx >= STATISTIC_NUM_SLOTS)
790 0 : ereport(ERROR,
791 : (errmsg("maximum number of statistics slots exceeded: %d",
792 : slotidx + 1)));
793 :
794 2484 : stakind_attnum = Anum_pg_statistic_stakind1 - 1 + slotidx;
795 2484 : staop_attnum = Anum_pg_statistic_staop1 - 1 + slotidx;
796 2484 : stacoll_attnum = Anum_pg_statistic_stacoll1 - 1 + slotidx;
797 :
798 2484 : if (DatumGetInt16(values[stakind_attnum]) != stakind)
799 : {
800 2484 : values[stakind_attnum] = Int16GetDatum(stakind);
801 2484 : replaces[stakind_attnum] = true;
802 : }
803 2484 : if (DatumGetObjectId(values[staop_attnum]) != staop)
804 : {
805 2466 : values[staop_attnum] = ObjectIdGetDatum(staop);
806 2466 : replaces[staop_attnum] = true;
807 : }
808 2484 : if (DatumGetObjectId(values[stacoll_attnum]) != stacoll)
809 : {
810 644 : values[stacoll_attnum] = ObjectIdGetDatum(stacoll);
811 644 : replaces[stacoll_attnum] = true;
812 : }
813 2484 : if (!stanumbers_isnull)
814 : {
815 1856 : values[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = stanumbers;
816 1856 : nulls[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = false;
817 1856 : replaces[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = true;
818 : }
819 2484 : if (!stavalues_isnull)
820 : {
821 1306 : values[Anum_pg_statistic_stavalues1 - 1 + slotidx] = stavalues;
822 1306 : nulls[Anum_pg_statistic_stavalues1 - 1 + slotidx] = false;
823 1306 : replaces[Anum_pg_statistic_stavalues1 - 1 + slotidx] = true;
824 : }
825 2484 : }
826 :
827 : /*
828 : * Upsert the pg_statistic record.
829 : */
830 : static void
831 1370 : upsert_pg_statistic(Relation starel, HeapTuple oldtup,
832 : const Datum *values, const bool *nulls, const bool *replaces)
833 : {
834 : HeapTuple newtup;
835 :
836 1370 : if (HeapTupleIsValid(oldtup))
837 : {
838 126 : newtup = heap_modify_tuple(oldtup, RelationGetDescr(starel),
839 : values, nulls, replaces);
840 126 : CatalogTupleUpdate(starel, &newtup->t_self, newtup);
841 : }
842 : else
843 : {
844 1244 : newtup = heap_form_tuple(RelationGetDescr(starel), values, nulls);
845 1244 : CatalogTupleInsert(starel, newtup);
846 : }
847 :
848 1370 : heap_freetuple(newtup);
849 :
850 1370 : CommandCounterIncrement();
851 1370 : }
852 :
853 : /*
854 : * Delete pg_statistic record.
855 : */
856 : static bool
857 6 : delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit)
858 : {
859 6 : Relation sd = table_open(StatisticRelationId, RowExclusiveLock);
860 : HeapTuple oldtup;
861 6 : bool result = false;
862 :
863 : /* Is there already a pg_statistic tuple for this attribute? */
864 6 : oldtup = SearchSysCache3(STATRELATTINH,
865 : ObjectIdGetDatum(reloid),
866 : Int16GetDatum(attnum),
867 : BoolGetDatum(stainherit));
868 :
869 6 : if (HeapTupleIsValid(oldtup))
870 : {
871 6 : CatalogTupleDelete(sd, &oldtup->t_self);
872 6 : ReleaseSysCache(oldtup);
873 6 : result = true;
874 : }
875 :
876 6 : table_close(sd, RowExclusiveLock);
877 :
878 6 : CommandCounterIncrement();
879 :
880 6 : return result;
881 : }
882 :
883 : /*
884 : * Initialize values and nulls for a new stats tuple.
885 : */
886 : static void
887 1244 : init_empty_stats_tuple(Oid reloid, int16 attnum, bool inherited,
888 : Datum *values, bool *nulls, bool *replaces)
889 : {
890 1244 : memset(nulls, true, sizeof(bool) * Natts_pg_statistic);
891 1244 : memset(replaces, true, sizeof(bool) * Natts_pg_statistic);
892 :
893 : /* must initialize non-NULL attributes */
894 :
895 1244 : values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(reloid);
896 1244 : nulls[Anum_pg_statistic_starelid - 1] = false;
897 1244 : values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(attnum);
898 1244 : nulls[Anum_pg_statistic_staattnum - 1] = false;
899 1244 : values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(inherited);
900 1244 : nulls[Anum_pg_statistic_stainherit - 1] = false;
901 :
902 1244 : values[Anum_pg_statistic_stanullfrac - 1] = DEFAULT_NULL_FRAC;
903 1244 : nulls[Anum_pg_statistic_stanullfrac - 1] = false;
904 1244 : values[Anum_pg_statistic_stawidth - 1] = DEFAULT_AVG_WIDTH;
905 1244 : nulls[Anum_pg_statistic_stawidth - 1] = false;
906 1244 : values[Anum_pg_statistic_stadistinct - 1] = DEFAULT_N_DISTINCT;
907 1244 : nulls[Anum_pg_statistic_stadistinct - 1] = false;
908 :
909 : /* initialize stakind, staop, and stacoll slots */
910 7464 : for (int slotnum = 0; slotnum < STATISTIC_NUM_SLOTS; slotnum++)
911 : {
912 6220 : values[Anum_pg_statistic_stakind1 + slotnum - 1] = (Datum) 0;
913 6220 : nulls[Anum_pg_statistic_stakind1 + slotnum - 1] = false;
914 6220 : values[Anum_pg_statistic_staop1 + slotnum - 1] = ObjectIdGetDatum(InvalidOid);
915 6220 : nulls[Anum_pg_statistic_staop1 + slotnum - 1] = false;
916 6220 : values[Anum_pg_statistic_stacoll1 + slotnum - 1] = ObjectIdGetDatum(InvalidOid);
917 6220 : nulls[Anum_pg_statistic_stacoll1 + slotnum - 1] = false;
918 : }
919 1244 : }
920 :
921 : /*
922 : * Delete statistics for the given attribute.
923 : */
924 : Datum
925 6 : pg_clear_attribute_stats(PG_FUNCTION_ARGS)
926 : {
927 : char *nspname;
928 : char *relname;
929 : Oid reloid;
930 : char *attname;
931 : AttrNumber attnum;
932 : bool inherited;
933 6 : Oid locked_table = InvalidOid;
934 :
935 6 : stats_check_required_arg(fcinfo, cleararginfo, C_ATTRELSCHEMA_ARG);
936 6 : stats_check_required_arg(fcinfo, cleararginfo, C_ATTRELNAME_ARG);
937 6 : stats_check_required_arg(fcinfo, cleararginfo, C_ATTNAME_ARG);
938 6 : stats_check_required_arg(fcinfo, cleararginfo, C_INHERITED_ARG);
939 :
940 6 : nspname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTRELSCHEMA_ARG));
941 6 : relname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTRELNAME_ARG));
942 :
943 6 : if (RecoveryInProgress())
944 0 : ereport(ERROR,
945 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
946 : errmsg("recovery is in progress"),
947 : errhint("Statistics cannot be modified during recovery.")));
948 :
949 6 : reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
950 : ShareUpdateExclusiveLock, 0,
951 : RangeVarCallbackForStats, &locked_table);
952 :
953 6 : attname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTNAME_ARG));
954 6 : attnum = get_attnum(reloid, attname);
955 :
956 6 : if (attnum < 0)
957 0 : ereport(ERROR,
958 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
959 : errmsg("cannot clear statistics on system column \"%s\"",
960 : attname)));
961 :
962 6 : if (attnum == InvalidAttrNumber)
963 0 : ereport(ERROR,
964 : (errcode(ERRCODE_UNDEFINED_COLUMN),
965 : errmsg("column \"%s\" of relation \"%s\" does not exist",
966 : attname, get_rel_name(reloid))));
967 :
968 6 : inherited = PG_GETARG_BOOL(C_INHERITED_ARG);
969 :
970 6 : delete_pg_statistic(reloid, attnum, inherited);
971 6 : PG_RETURN_VOID();
972 : }
973 :
974 : /*
975 : * Import statistics for a given relation attribute.
976 : *
977 : * Inserts or replaces a row in pg_statistic for the given relation and
978 : * attribute name or number. It takes input parameters that correspond to
979 : * columns in the view pg_stats.
980 : *
981 : * Parameters are given in a pseudo named-attribute style: they must be
982 : * pairs of parameter names (as text) and values (of appropriate types).
983 : * We do that, rather than using regular named-parameter notation, so
984 : * that we can add or change parameters without fear of breaking
985 : * carelessly-written calls.
986 : *
987 : * Parameters null_frac, avg_width, and n_distinct all correspond to NOT NULL
988 : * columns in pg_statistic. The remaining parameters all belong to a specific
989 : * stakind. Some stakinds require multiple parameters, which must be specified
990 : * together (or neither specified).
991 : *
992 : * Parameters are only superficially validated. Omitting a parameter or
993 : * passing NULL leaves the statistic unchanged.
994 : *
995 : * Parameters corresponding to ANYARRAY columns are instead passed in as text
996 : * values, which is a valid input string for an array of the type or element
997 : * type of the attribute. Any error generated by the array_in() function will
998 : * in turn fail the function.
999 : */
1000 : Datum
1001 1436 : pg_restore_attribute_stats(PG_FUNCTION_ARGS)
1002 : {
1003 1436 : LOCAL_FCINFO(positional_fcinfo, NUM_ATTRIBUTE_STATS_ARGS);
1004 1436 : bool result = true;
1005 :
1006 1436 : InitFunctionCallInfoData(*positional_fcinfo, NULL, NUM_ATTRIBUTE_STATS_ARGS,
1007 : InvalidOid, NULL, NULL);
1008 :
1009 1436 : if (!stats_fill_fcinfo_from_arg_pairs(fcinfo, positional_fcinfo,
1010 : attarginfo))
1011 12 : result = false;
1012 :
1013 1436 : if (!attribute_statistics_update(positional_fcinfo))
1014 90 : result = false;
1015 :
1016 1370 : PG_RETURN_BOOL(result);
1017 : }
|