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