Branch data 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-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"
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 : : /*
34 : : * Positional argument numbers, names, and types for
35 : : * attribute_statistics_update() and pg_restore_attribute_stats().
36 : : */
37 : :
38 : : enum attribute_stats_argnum
39 : : {
40 : : ATTRELSCHEMA_ARG = 0,
41 : : ATTRELNAME_ARG,
42 : : ATTNAME_ARG,
43 : : ATTNUM_ARG,
44 : : INHERITED_ARG,
45 : : NULL_FRAC_ARG,
46 : : AVG_WIDTH_ARG,
47 : : N_DISTINCT_ARG,
48 : : MOST_COMMON_VALS_ARG,
49 : : MOST_COMMON_FREQS_ARG,
50 : : HISTOGRAM_BOUNDS_ARG,
51 : : CORRELATION_ARG,
52 : : MOST_COMMON_ELEMS_ARG,
53 : : MOST_COMMON_ELEM_FREQS_ARG,
54 : : ELEM_COUNT_HISTOGRAM_ARG,
55 : : RANGE_LENGTH_HISTOGRAM_ARG,
56 : : RANGE_EMPTY_FRAC_ARG,
57 : : RANGE_BOUNDS_HISTOGRAM_ARG,
58 : : NUM_ATTRIBUTE_STATS_ARGS
59 : : };
60 : :
61 : : static 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},
81 : : [NUM_ATTRIBUTE_STATS_ARGS] = {0}
82 : : };
83 : :
84 : : /*
85 : : * Positional argument numbers, names, and types for
86 : : * pg_clear_attribute_stats().
87 : : */
88 : :
89 : : enum clear_attribute_stats_argnum
90 : : {
91 : : C_ATTRELSCHEMA_ARG = 0,
92 : : C_ATTRELNAME_ARG,
93 : : C_ATTNAME_ARG,
94 : : C_INHERITED_ARG,
95 : : C_NUM_ATTRIBUTE_STATS_ARGS
96 : : };
97 : :
98 : : static 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},
104 : : [C_NUM_ATTRIBUTE_STATS_ARGS] = {0}
105 : : };
106 : :
107 : : static bool attribute_statistics_update(FunctionCallInfo fcinfo);
108 : : static void upsert_pg_statistic(Relation starel, HeapTuple oldtup,
109 : : const Datum *values, const bool *nulls, const bool *replaces);
110 : : static 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 : : */
128 : : static bool
129 : 38 : attribute_statistics_update(FunctionCallInfo fcinfo)
130 : : {
131 : 38 : char *nspname;
132 : 38 : char *relname;
133 : 38 : Oid reloid;
134 : 38 : char *attname;
135 : 38 : AttrNumber attnum;
136 : 38 : bool inherited;
137 : 38 : Oid locked_table = InvalidOid;
138 : :
139 : 38 : Relation starel;
140 : 38 : HeapTuple statup;
141 : :
142 : 38 : Oid atttypid = InvalidOid;
143 : 38 : int32 atttypmod;
144 : 38 : char atttyptype;
145 : 38 : Oid atttypcoll = InvalidOid;
146 : 38 : Oid eq_opr = InvalidOid;
147 : 38 : Oid lt_opr = InvalidOid;
148 : :
149 : 38 : Oid elemtypid = InvalidOid;
150 : 38 : Oid elem_eq_opr = InvalidOid;
151 : :
152 : 38 : FmgrInfo array_in_fn;
153 : :
154 [ + + ]: 42 : bool do_mcv = !PG_ARGISNULL(MOST_COMMON_FREQS_ARG) &&
155 : 4 : !PG_ARGISNULL(MOST_COMMON_VALS_ARG);
156 : 38 : bool do_histogram = !PG_ARGISNULL(HISTOGRAM_BOUNDS_ARG);
157 : 38 : bool do_correlation = !PG_ARGISNULL(CORRELATION_ARG);
158 [ + + ]: 43 : bool do_mcelem = !PG_ARGISNULL(MOST_COMMON_ELEMS_ARG) &&
159 : 5 : !PG_ARGISNULL(MOST_COMMON_ELEM_FREQS_ARG);
160 : 38 : bool do_dechist = !PG_ARGISNULL(ELEM_COUNT_HISTOGRAM_ARG);
161 : 38 : bool do_bounds_histogram = !PG_ARGISNULL(RANGE_BOUNDS_HISTOGRAM_ARG);
162 [ + + ]: 43 : bool do_range_length_histogram = !PG_ARGISNULL(RANGE_LENGTH_HISTOGRAM_ARG) &&
163 : 5 : !PG_ARGISNULL(RANGE_EMPTY_FRAC_ARG);
164 : :
165 : 38 : Datum values[Natts_pg_statistic] = {0};
166 : 38 : bool nulls[Natts_pg_statistic] = {0};
167 : 38 : bool replaces[Natts_pg_statistic] = {0};
168 : :
169 : 38 : bool result = true;
170 : :
171 : 38 : stats_check_required_arg(fcinfo, attarginfo, ATTRELSCHEMA_ARG);
172 : 38 : stats_check_required_arg(fcinfo, attarginfo, ATTRELNAME_ARG);
173 : :
174 : 38 : nspname = TextDatumGetCString(PG_GETARG_DATUM(ATTRELSCHEMA_ARG));
175 : 38 : relname = TextDatumGetCString(PG_GETARG_DATUM(ATTRELNAME_ARG));
176 : :
177 [ + - ]: 38 : if (RecoveryInProgress())
178 [ # # # # ]: 0 : ereport(ERROR,
179 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
180 : : errmsg("recovery is in progress"),
181 : : errhint("Statistics cannot be modified during recovery.")));
182 : :
183 : : /* lock before looking up attribute */
184 : 38 : reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
185 : : ShareUpdateExclusiveLock, 0,
186 : : RangeVarCallbackForStats, &locked_table);
187 : :
188 : : /* user can specify either attname or attnum, but not both */
189 [ + + ]: 38 : if (!PG_ARGISNULL(ATTNAME_ARG))
190 : : {
191 [ + + ]: 35 : if (!PG_ARGISNULL(ATTNUM_ARG))
192 [ + - + - ]: 1 : ereport(ERROR,
193 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
194 : : errmsg("cannot specify both \"%s\" and \"%s\"", "attname", "attnum")));
195 : 34 : attname = TextDatumGetCString(PG_GETARG_DATUM(ATTNAME_ARG));
196 : 34 : attnum = get_attnum(reloid, attname);
197 : : /* note that this test covers attisdropped cases too: */
198 [ + + ]: 34 : if (attnum == InvalidAttrNumber)
199 [ + - + - ]: 1 : ereport(ERROR,
200 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
201 : : errmsg("column \"%s\" of relation \"%s\" does not exist",
202 : : attname, relname)));
203 : 33 : }
204 [ + + ]: 3 : else if (!PG_ARGISNULL(ATTNUM_ARG))
205 : : {
206 : 1 : attnum = PG_GETARG_INT16(ATTNUM_ARG);
207 : 1 : attname = get_attname(reloid, attnum, true);
208 : : /* annoyingly, get_attname doesn't check attisdropped */
209 [ + - ]: 1 : if (attname == NULL ||
210 : 1 : !SearchSysCacheExistsAttName(reloid, attname))
211 [ # # # # ]: 0 : ereport(ERROR,
212 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
213 : : errmsg("column %d of relation \"%s\" does not exist",
214 : : attnum, relname)));
215 : 1 : }
216 : : else
217 : : {
218 [ + - + - ]: 2 : ereport(ERROR,
219 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
220 : : errmsg("must specify either \"%s\" or \"%s\"", "attname", "attnum")));
221 : 0 : attname = NULL; /* keep compiler quiet */
222 : 0 : attnum = 0;
223 : : }
224 : :
225 [ + + ]: 34 : if (attnum < 0)
226 [ + - + - ]: 1 : ereport(ERROR,
227 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
228 : : errmsg("cannot modify statistics on system column \"%s\"",
229 : : attname)));
230 : :
231 : 33 : stats_check_required_arg(fcinfo, attarginfo, INHERITED_ARG);
232 : 33 : inherited = PG_GETARG_BOOL(INHERITED_ARG);
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 : :
239 [ + - ]: 33 : if (!stats_check_arg_array(fcinfo, attarginfo, MOST_COMMON_FREQS_ARG))
240 : : {
241 : 0 : do_mcv = false;
242 : 0 : result = false;
243 : 0 : }
244 : :
245 [ + - ]: 33 : if (!stats_check_arg_array(fcinfo, attarginfo, MOST_COMMON_ELEM_FREQS_ARG))
246 : : {
247 : 0 : do_mcelem = false;
248 : 0 : result = false;
249 : 0 : }
250 [ + + ]: 33 : if (!stats_check_arg_array(fcinfo, attarginfo, ELEM_COUNT_HISTOGRAM_ARG))
251 : : {
252 : 1 : do_dechist = false;
253 : 1 : result = false;
254 : 1 : }
255 : :
256 [ + + ]: 33 : if (!stats_check_arg_pair(fcinfo, attarginfo,
257 : : MOST_COMMON_VALS_ARG, MOST_COMMON_FREQS_ARG))
258 : : {
259 : 3 : do_mcv = false;
260 : 3 : result = false;
261 : 3 : }
262 : :
263 [ + + ]: 33 : if (!stats_check_arg_pair(fcinfo, attarginfo,
264 : : MOST_COMMON_ELEMS_ARG,
265 : : MOST_COMMON_ELEM_FREQS_ARG))
266 : : {
267 : 2 : do_mcelem = false;
268 : 2 : result = false;
269 : 2 : }
270 : :
271 [ + + ]: 33 : if (!stats_check_arg_pair(fcinfo, attarginfo,
272 : : RANGE_LENGTH_HISTOGRAM_ARG,
273 : : RANGE_EMPTY_FRAC_ARG))
274 : : {
275 : 2 : do_range_length_histogram = false;
276 : 2 : result = false;
277 : 2 : }
278 : :
279 : : /* derive information from attribute */
280 : 33 : statatt_get_type(reloid, attnum,
281 : : &atttypid, &atttypmod,
282 : : &atttyptype, &atttypcoll,
283 : : &eq_opr, <_opr);
284 : :
285 : : /* if needed, derive element type */
286 [ + + + + ]: 33 : if (do_mcelem || do_dechist)
287 : : {
288 [ + + ]: 6 : if (!statatt_get_elem_type(atttypid, atttyptype,
289 : : &elemtypid, &elem_eq_opr))
290 : : {
291 [ - + + - ]: 3 : ereport(WARNING,
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")));
295 : 3 : elemtypid = InvalidOid;
296 : 3 : elem_eq_opr = InvalidOid;
297 : :
298 : 3 : do_mcelem = false;
299 : 3 : do_dechist = false;
300 : 3 : result = false;
301 : 3 : }
302 : 6 : }
303 : :
304 : : /* histogram and correlation require less-than operator */
305 [ + + + - ]: 33 : if ((do_histogram || do_correlation) && !OidIsValid(lt_opr))
306 : : {
307 [ # # # # ]: 0 : ereport(WARNING,
308 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
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 : 0 : do_histogram = false;
314 : 0 : do_correlation = false;
315 : 0 : result = false;
316 : 0 : }
317 : :
318 : : /* only range types can have range stats */
319 [ + + - + ]: 35 : if ((do_range_length_histogram || do_bounds_histogram) &&
320 [ + + ]: 33 : !(atttyptype == TYPTYPE_RANGE || atttyptype == TYPTYPE_MULTIRANGE))
321 : : {
322 [ - + + - ]: 2 : ereport(WARNING,
323 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
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 : 2 : do_bounds_histogram = false;
329 : 2 : do_range_length_histogram = false;
330 : 2 : result = false;
331 : 2 : }
332 : :
333 : 33 : fmgr_info(F_ARRAY_IN, &array_in_fn);
334 : :
335 : 33 : starel = table_open(StatisticRelationId, RowExclusiveLock);
336 : :
337 : 33 : statup = SearchSysCache3(STATRELATTINH, ObjectIdGetDatum(reloid), Int16GetDatum(attnum), BoolGetDatum(inherited));
338 : :
339 : : /* initialize from existing tuple if exists */
340 [ + + ]: 33 : if (HeapTupleIsValid(statup))
341 : 22 : heap_deform_tuple(statup, RelationGetDescr(starel), values, nulls);
342 : : else
343 : 22 : statatt_init_empty_tuple(reloid, attnum, inherited, values, nulls,
344 : 11 : replaces);
345 : :
346 : : /* if specified, set to argument values */
347 [ + + ]: 33 : if (!PG_ARGISNULL(NULL_FRAC_ARG))
348 : : {
349 : 26 : values[Anum_pg_statistic_stanullfrac - 1] = PG_GETARG_DATUM(NULL_FRAC_ARG);
350 : 26 : replaces[Anum_pg_statistic_stanullfrac - 1] = true;
351 : 26 : }
352 [ + + ]: 33 : if (!PG_ARGISNULL(AVG_WIDTH_ARG))
353 : : {
354 : 7 : values[Anum_pg_statistic_stawidth - 1] = PG_GETARG_DATUM(AVG_WIDTH_ARG);
355 : 7 : replaces[Anum_pg_statistic_stawidth - 1] = true;
356 : 7 : }
357 [ + + ]: 33 : if (!PG_ARGISNULL(N_DISTINCT_ARG))
358 : : {
359 : 7 : values[Anum_pg_statistic_stadistinct - 1] = PG_GETARG_DATUM(N_DISTINCT_ARG);
360 : 7 : replaces[Anum_pg_statistic_stadistinct - 1] = true;
361 : 7 : }
362 : :
363 : : /* STATISTIC_KIND_MCV */
364 [ + + ]: 33 : if (do_mcv)
365 : : {
366 : 3 : bool converted;
367 : 3 : Datum stanumbers = PG_GETARG_DATUM(MOST_COMMON_FREQS_ARG);
368 : 6 : Datum stavalues = statatt_build_stavalues("most_common_vals",
369 : : &array_in_fn,
370 : 3 : PG_GETARG_DATUM(MOST_COMMON_VALS_ARG),
371 : 3 : atttypid, atttypmod,
372 : : &converted);
373 : :
374 [ + + ]: 3 : if (converted)
375 : : {
376 : 4 : statatt_set_slot(values, nulls, replaces,
377 : : STATISTIC_KIND_MCV,
378 : 2 : eq_opr, atttypcoll,
379 : 2 : stanumbers, false, stavalues, false);
380 : 2 : }
381 : : else
382 : 1 : result = false;
383 : 3 : }
384 : :
385 : : /* STATISTIC_KIND_HISTOGRAM */
386 [ + + ]: 33 : if (do_histogram)
387 : : {
388 : 6 : Datum stavalues;
389 : 6 : bool converted = false;
390 : :
391 : 6 : stavalues = statatt_build_stavalues("histogram_bounds",
392 : : &array_in_fn,
393 : 6 : PG_GETARG_DATUM(HISTOGRAM_BOUNDS_ARG),
394 : 6 : atttypid, atttypmod,
395 : : &converted);
396 : :
397 [ + + ]: 6 : if (converted)
398 : : {
399 : 10 : statatt_set_slot(values, nulls, replaces,
400 : : STATISTIC_KIND_HISTOGRAM,
401 : 5 : lt_opr, atttypcoll,
402 : 5 : 0, true, stavalues, false);
403 : 5 : }
404 : : else
405 : 1 : result = false;
406 : 6 : }
407 : :
408 : : /* STATISTIC_KIND_CORRELATION */
409 [ + + ]: 33 : if (do_correlation)
410 : : {
411 : 5 : Datum elems[] = {PG_GETARG_DATUM(CORRELATION_ARG)};
412 : 5 : ArrayType *arry = construct_array_builtin(elems, 1, FLOAT4OID);
413 : 5 : Datum stanumbers = PointerGetDatum(arry);
414 : :
415 : 10 : statatt_set_slot(values, nulls, replaces,
416 : : STATISTIC_KIND_CORRELATION,
417 : 5 : lt_opr, atttypcoll,
418 : 5 : stanumbers, false, 0, true);
419 : 5 : }
420 : :
421 : : /* STATISTIC_KIND_MCELEM */
422 [ + + ]: 33 : if (do_mcelem)
423 : : {
424 : 2 : Datum stanumbers = PG_GETARG_DATUM(MOST_COMMON_ELEM_FREQS_ARG);
425 : 2 : bool converted = false;
426 : 2 : Datum stavalues;
427 : :
428 : 2 : stavalues = statatt_build_stavalues("most_common_elems",
429 : : &array_in_fn,
430 : 2 : PG_GETARG_DATUM(MOST_COMMON_ELEMS_ARG),
431 : 2 : elemtypid, atttypmod,
432 : : &converted);
433 : :
434 [ + - ]: 2 : if (converted)
435 : : {
436 : 4 : statatt_set_slot(values, nulls, replaces,
437 : : STATISTIC_KIND_MCELEM,
438 : 2 : elem_eq_opr, atttypcoll,
439 : 2 : stanumbers, false, stavalues, false);
440 : 2 : }
441 : : else
442 : 0 : result = false;
443 : 2 : }
444 : :
445 : : /* STATISTIC_KIND_DECHIST */
446 [ + + ]: 33 : if (do_dechist)
447 : : {
448 : 2 : Datum stanumbers = PG_GETARG_DATUM(ELEM_COUNT_HISTOGRAM_ARG);
449 : :
450 : 4 : statatt_set_slot(values, nulls, replaces,
451 : : STATISTIC_KIND_DECHIST,
452 : 2 : elem_eq_opr, atttypcoll,
453 : 2 : stanumbers, false, 0, true);
454 : 2 : }
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 : : */
463 [ + + ]: 33 : if (do_bounds_histogram)
464 : : {
465 : 3 : bool converted = false;
466 : 3 : Datum stavalues;
467 : :
468 : 3 : stavalues = statatt_build_stavalues("range_bounds_histogram",
469 : : &array_in_fn,
470 : 3 : PG_GETARG_DATUM(RANGE_BOUNDS_HISTOGRAM_ARG),
471 : 3 : atttypid, atttypmod,
472 : : &converted);
473 : :
474 [ + - ]: 3 : if (converted)
475 : : {
476 : 6 : statatt_set_slot(values, nulls, replaces,
477 : : STATISTIC_KIND_BOUNDS_HISTOGRAM,
478 : : InvalidOid, InvalidOid,
479 : 3 : 0, true, stavalues, false);
480 : 3 : }
481 : : else
482 : 0 : result = false;
483 : 3 : }
484 : :
485 : : /* STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM */
486 [ + + ]: 33 : if (do_range_length_histogram)
487 : : {
488 : : /* The anyarray is always a float8[] for this stakind */
489 : 3 : Datum elems[] = {PG_GETARG_DATUM(RANGE_EMPTY_FRAC_ARG)};
490 : 3 : ArrayType *arry = construct_array_builtin(elems, 1, FLOAT4OID);
491 : 3 : Datum stanumbers = PointerGetDatum(arry);
492 : :
493 : 3 : bool converted = false;
494 : 3 : Datum stavalues;
495 : :
496 : 3 : stavalues = statatt_build_stavalues("range_length_histogram",
497 : : &array_in_fn,
498 : 3 : PG_GETARG_DATUM(RANGE_LENGTH_HISTOGRAM_ARG),
499 : : FLOAT8OID, 0, &converted);
500 : :
501 [ + - ]: 3 : if (converted)
502 : : {
503 : 6 : statatt_set_slot(values, nulls, replaces,
504 : : STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM,
505 : : Float8LessOperator, InvalidOid,
506 : 3 : stanumbers, false, stavalues, false);
507 : 3 : }
508 : : else
509 : 0 : result = false;
510 : 3 : }
511 : :
512 : 33 : upsert_pg_statistic(starel, statup, values, nulls, replaces);
513 : :
514 [ + + ]: 33 : if (HeapTupleIsValid(statup))
515 : 21 : ReleaseSysCache(statup);
516 : 33 : table_close(starel, RowExclusiveLock);
517 : :
518 : 66 : return result;
519 : 33 : }
520 : :
521 : : /*
522 : : * Upsert the pg_statistic record.
523 : : */
524 : : static void
525 : 32 : upsert_pg_statistic(Relation starel, HeapTuple oldtup,
526 : : const Datum *values, const bool *nulls, const bool *replaces)
527 : : {
528 : 32 : HeapTuple newtup;
529 : :
530 [ + + ]: 32 : if (HeapTupleIsValid(oldtup))
531 : : {
532 : 42 : newtup = heap_modify_tuple(oldtup, RelationGetDescr(starel),
533 : 21 : values, nulls, replaces);
534 : 21 : CatalogTupleUpdate(starel, &newtup->t_self, newtup);
535 : 21 : }
536 : : else
537 : : {
538 : 11 : newtup = heap_form_tuple(RelationGetDescr(starel), values, nulls);
539 : 11 : CatalogTupleInsert(starel, newtup);
540 : : }
541 : :
542 : 32 : heap_freetuple(newtup);
543 : :
544 : 32 : CommandCounterIncrement();
545 : 32 : }
546 : :
547 : : /*
548 : : * Delete pg_statistic record.
549 : : */
550 : : static bool
551 : 1 : delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit)
552 : : {
553 : 1 : Relation sd = table_open(StatisticRelationId, RowExclusiveLock);
554 : 1 : HeapTuple oldtup;
555 : 1 : bool result = false;
556 : :
557 : : /* Is there already a pg_statistic tuple for this attribute? */
558 : 1 : oldtup = SearchSysCache3(STATRELATTINH,
559 : 1 : ObjectIdGetDatum(reloid),
560 : 1 : Int16GetDatum(attnum),
561 : 1 : BoolGetDatum(stainherit));
562 : :
563 [ - + ]: 1 : if (HeapTupleIsValid(oldtup))
564 : : {
565 : 1 : CatalogTupleDelete(sd, &oldtup->t_self);
566 : 1 : ReleaseSysCache(oldtup);
567 : 1 : result = true;
568 : 1 : }
569 : :
570 : 1 : table_close(sd, RowExclusiveLock);
571 : :
572 : 1 : CommandCounterIncrement();
573 : :
574 : 2 : return result;
575 : 1 : }
576 : :
577 : : /*
578 : : * Delete statistics for the given attribute.
579 : : */
580 : : Datum
581 : 1 : pg_clear_attribute_stats(PG_FUNCTION_ARGS)
582 : : {
583 : 1 : char *nspname;
584 : 1 : char *relname;
585 : 1 : Oid reloid;
586 : 1 : char *attname;
587 : 1 : AttrNumber attnum;
588 : 1 : bool inherited;
589 : 1 : Oid locked_table = InvalidOid;
590 : :
591 : 1 : stats_check_required_arg(fcinfo, cleararginfo, C_ATTRELSCHEMA_ARG);
592 : 1 : stats_check_required_arg(fcinfo, cleararginfo, C_ATTRELNAME_ARG);
593 : 1 : stats_check_required_arg(fcinfo, cleararginfo, C_ATTNAME_ARG);
594 : 1 : stats_check_required_arg(fcinfo, cleararginfo, C_INHERITED_ARG);
595 : :
596 : 1 : nspname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTRELSCHEMA_ARG));
597 : 1 : relname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTRELNAME_ARG));
598 : :
599 [ + - ]: 1 : if (RecoveryInProgress())
600 [ # # # # ]: 0 : ereport(ERROR,
601 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
602 : : errmsg("recovery is in progress"),
603 : : errhint("Statistics cannot be modified during recovery.")));
604 : :
605 : 1 : reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
606 : : ShareUpdateExclusiveLock, 0,
607 : : RangeVarCallbackForStats, &locked_table);
608 : :
609 : 1 : attname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTNAME_ARG));
610 : 1 : attnum = get_attnum(reloid, attname);
611 : :
612 [ + - ]: 1 : if (attnum < 0)
613 [ # # # # ]: 0 : ereport(ERROR,
614 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
615 : : errmsg("cannot clear statistics on system column \"%s\"",
616 : : attname)));
617 : :
618 [ + - ]: 1 : if (attnum == InvalidAttrNumber)
619 [ # # # # ]: 0 : ereport(ERROR,
620 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
621 : : errmsg("column \"%s\" of relation \"%s\" does not exist",
622 : : attname, get_rel_name(reloid))));
623 : :
624 : 1 : inherited = PG_GETARG_BOOL(C_INHERITED_ARG);
625 : :
626 : 1 : delete_pg_statistic(reloid, attnum, inherited);
627 : 1 : PG_RETURN_VOID();
628 : 1 : }
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 : : */
656 : : Datum
657 : 43 : pg_restore_attribute_stats(PG_FUNCTION_ARGS)
658 : : {
659 : 43 : LOCAL_FCINFO(positional_fcinfo, NUM_ATTRIBUTE_STATS_ARGS);
660 : 43 : bool result = true;
661 : :
662 : 43 : InitFunctionCallInfoData(*positional_fcinfo, NULL, NUM_ATTRIBUTE_STATS_ARGS,
663 : : InvalidOid, NULL, NULL);
664 : :
665 [ + + ]: 43 : if (!stats_fill_fcinfo_from_arg_pairs(fcinfo, positional_fcinfo,
666 : : attarginfo))
667 : 2 : result = false;
668 : :
669 [ + + ]: 43 : if (!attribute_statistics_update(positional_fcinfo))
670 : 15 : result = false;
671 : :
672 : 86 : PG_RETURN_BOOL(result);
673 : 43 : }
|