Age Owner Branch data TLA 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
193 jdavis@postgresql.or 138 :CBC 798 : 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 : :
319 150 : 798 : Oid atttypid = InvalidOid;
151 : : int32 atttypmod;
152 : : char atttyptype;
153 : 798 : Oid atttypcoll = InvalidOid;
154 : 798 : Oid eq_opr = InvalidOid;
155 : 798 : Oid lt_opr = InvalidOid;
156 : :
157 : 798 : Oid elemtypid = InvalidOid;
158 : 798 : Oid elem_eq_opr = InvalidOid;
159 : :
160 : : FmgrInfo array_in_fn;
161 : :
162 [ + + ]: 1177 : bool do_mcv = !PG_ARGISNULL(MOST_COMMON_FREQS_ARG) &&
163 [ + + ]: 379 : !PG_ARGISNULL(MOST_COMMON_VALS_ARG);
164 : 798 : bool do_histogram = !PG_ARGISNULL(HISTOGRAM_BOUNDS_ARG);
165 : 798 : bool do_correlation = !PG_ARGISNULL(CORRELATION_ARG);
166 [ + + ]: 821 : bool do_mcelem = !PG_ARGISNULL(MOST_COMMON_ELEMS_ARG) &&
167 [ + + ]: 23 : !PG_ARGISNULL(MOST_COMMON_ELEM_FREQS_ARG);
168 : 798 : bool do_dechist = !PG_ARGISNULL(ELEM_COUNT_HISTOGRAM_ARG);
169 : 798 : bool do_bounds_histogram = !PG_ARGISNULL(RANGE_BOUNDS_HISTOGRAM_ARG);
170 [ + + ]: 814 : bool do_range_length_histogram = !PG_ARGISNULL(RANGE_LENGTH_HISTOGRAM_ARG) &&
171 [ + + ]: 16 : !PG_ARGISNULL(RANGE_EMPTY_FRAC_ARG);
172 : :
173 : 798 : Datum values[Natts_pg_statistic] = {0};
174 : 798 : bool nulls[Natts_pg_statistic] = {0};
175 : 798 : bool replaces[Natts_pg_statistic] = {0};
176 : :
177 : 798 : bool result = true;
178 : :
165 179 : 798 : stats_check_required_arg(fcinfo, attarginfo, ATTRELSCHEMA_ARG);
180 : 795 : stats_check_required_arg(fcinfo, attarginfo, ATTRELNAME_ARG);
181 : :
182 : 789 : nspname = TextDatumGetCString(PG_GETARG_DATUM(ATTRELSCHEMA_ARG));
183 : 789 : relname = TextDatumGetCString(PG_GETARG_DATUM(ATTRELNAME_ARG));
184 : :
185 : 789 : reloid = stats_lookup_relid(nspname, relname);
186 : :
290 fujii@postgresql.org 187 [ - + ]: 783 : if (RecoveryInProgress())
290 fujii@postgresql.org 188 [ # # ]:UBC 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 */
319 jdavis@postgresql.or 194 :CBC 783 : stats_lock_check_privileges(reloid);
195 : :
196 : : /* user can specify either attname or attnum, but not both */
192 tgl@sss.pgh.pa.us 197 [ + + ]: 783 : if (!PG_ARGISNULL(ATTNAME_ARG))
198 : : {
199 [ + + ]: 770 : if (!PG_ARGISNULL(ATTNUM_ARG))
200 [ + - ]: 3 : ereport(ERROR,
201 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
202 : : errmsg("cannot specify both \"%s\" and \"%s\"", "attname", "attnum")));
165 jdavis@postgresql.or 203 : 767 : attname = TextDatumGetCString(PG_GETARG_DATUM(ATTNAME_ARG));
192 tgl@sss.pgh.pa.us 204 : 767 : attnum = get_attnum(reloid, attname);
205 : : /* note that this test covers attisdropped cases too: */
206 [ + + ]: 767 : if (attnum == InvalidAttrNumber)
207 [ + - ]: 3 : ereport(ERROR,
208 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
209 : : errmsg("column \"%s\" of relation \"%s\" does not exist",
210 : : attname, relname)));
211 : : }
212 [ + + ]: 13 : else if (!PG_ARGISNULL(ATTNUM_ARG))
213 : : {
214 : 7 : attnum = PG_GETARG_INT16(ATTNUM_ARG);
215 : 7 : attname = get_attname(reloid, attnum, true);
216 : : /* annoyingly, get_attname doesn't check attisdropped */
217 [ + - ]: 7 : if (attname == NULL ||
218 [ - + ]: 7 : !SearchSysCacheExistsAttName(reloid, attname))
192 tgl@sss.pgh.pa.us 219 [ # # ]:UBC 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 : : {
192 tgl@sss.pgh.pa.us 226 [ + - ]:CBC 6 : 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 : :
288 jdavis@postgresql.or 233 [ + + ]: 771 : if (attnum < 0)
234 [ + - ]: 3 : ereport(ERROR,
235 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
236 : : errmsg("cannot modify statistics on system column \"%s\"",
237 : : attname)));
238 : :
319 239 : 768 : stats_check_required_arg(fcinfo, attarginfo, INHERITED_ARG);
240 : 765 : 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 : :
193 247 [ - + ]: 765 : if (!stats_check_arg_array(fcinfo, attarginfo, MOST_COMMON_FREQS_ARG))
248 : : {
319 jdavis@postgresql.or 249 :UBC 0 : do_mcv = false;
250 : 0 : result = false;
251 : : }
252 : :
193 jdavis@postgresql.or 253 [ - + ]:CBC 765 : if (!stats_check_arg_array(fcinfo, attarginfo, MOST_COMMON_ELEM_FREQS_ARG))
254 : : {
319 jdavis@postgresql.or 255 :UBC 0 : do_mcelem = false;
256 : 0 : result = false;
257 : : }
193 jdavis@postgresql.or 258 [ + + ]:CBC 765 : if (!stats_check_arg_array(fcinfo, attarginfo, ELEM_COUNT_HISTOGRAM_ARG))
259 : : {
319 260 : 3 : do_dechist = false;
261 : 3 : result = false;
262 : : }
263 : :
264 [ + + ]: 765 : if (!stats_check_arg_pair(fcinfo, attarginfo,
265 : : MOST_COMMON_VALS_ARG, MOST_COMMON_FREQS_ARG))
266 : : {
267 : 9 : do_mcv = false;
268 : 9 : result = false;
269 : : }
270 : :
271 [ + + ]: 765 : if (!stats_check_arg_pair(fcinfo, attarginfo,
272 : : MOST_COMMON_ELEMS_ARG,
273 : : MOST_COMMON_ELEM_FREQS_ARG))
274 : : {
275 : 6 : do_mcelem = false;
276 : 6 : result = false;
277 : : }
278 : :
279 [ + + ]: 765 : if (!stats_check_arg_pair(fcinfo, attarginfo,
280 : : RANGE_LENGTH_HISTOGRAM_ARG,
281 : : RANGE_EMPTY_FRAC_ARG))
282 : : {
283 : 6 : do_range_length_histogram = false;
284 : 6 : result = false;
285 : : }
286 : :
287 : : /* derive information from attribute */
193 288 : 765 : get_attr_stat_type(reloid, attnum,
289 : : &atttypid, &atttypmod,
290 : : &atttyptype, &atttypcoll,
291 : : &eq_opr, <_opr);
292 : :
293 : : /* if needed, derive element type */
319 294 [ + + + + ]: 765 : if (do_mcelem || do_dechist)
295 : : {
193 296 [ + + ]: 26 : if (!get_elem_stat_type(atttypid, atttyptype,
297 : : &elemtypid, &elem_eq_opr))
298 : : {
299 [ + - ]: 9 : 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")));
319 303 : 9 : elemtypid = InvalidOid;
304 : 9 : elem_eq_opr = InvalidOid;
305 : :
306 : 9 : do_mcelem = false;
307 : 9 : do_dechist = false;
308 : 9 : result = false;
309 : : }
310 : : }
311 : :
312 : : /* histogram and correlation require less-than operator */
313 [ + + + + : 765 : if ((do_histogram || do_correlation) && !OidIsValid(lt_opr))
- + ]
314 : : {
193 jdavis@postgresql.or 315 [ # # ]:UBC 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 : :
319 321 : 0 : do_histogram = false;
322 : 0 : do_correlation = false;
323 : 0 : result = false;
324 : : }
325 : :
326 : : /* only range types can have range stats */
319 jdavis@postgresql.or 327 [ + + + + ]:CBC 765 : if ((do_range_length_histogram || do_bounds_histogram) &&
328 [ + + + - ]: 19 : !(atttyptype == TYPTYPE_RANGE || atttyptype == TYPTYPE_MULTIRANGE))
329 : : {
193 330 [ + - ]: 6 : 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 : :
319 336 : 6 : do_bounds_histogram = false;
337 : 6 : do_range_length_histogram = false;
338 : 6 : result = false;
339 : : }
340 : :
341 : 765 : fmgr_info(F_ARRAY_IN, &array_in_fn);
342 : :
343 : 765 : starel = table_open(StatisticRelationId, RowExclusiveLock);
344 : :
29 peter@eisentraut.org 345 :GNC 765 : statup = SearchSysCache3(STATRELATTINH, ObjectIdGetDatum(reloid), Int16GetDatum(attnum), BoolGetDatum(inherited));
346 : :
347 : : /* initialize from existing tuple if exists */
319 jdavis@postgresql.or 348 [ + + ]:CBC 765 : if (HeapTupleIsValid(statup))
349 : 63 : heap_deform_tuple(statup, RelationGetDescr(starel), values, nulls);
350 : : else
351 : 702 : init_empty_stats_tuple(reloid, attnum, inherited, values, nulls,
352 : : replaces);
353 : :
354 : : /* if specified, set to argument values */
355 [ + + ]: 765 : if (!PG_ARGISNULL(NULL_FRAC_ARG))
356 : : {
357 : 750 : values[Anum_pg_statistic_stanullfrac - 1] = PG_GETARG_DATUM(NULL_FRAC_ARG);
358 : 750 : replaces[Anum_pg_statistic_stanullfrac - 1] = true;
359 : : }
360 [ + + ]: 765 : if (!PG_ARGISNULL(AVG_WIDTH_ARG))
361 : : {
362 : 693 : values[Anum_pg_statistic_stawidth - 1] = PG_GETARG_DATUM(AVG_WIDTH_ARG);
363 : 693 : replaces[Anum_pg_statistic_stawidth - 1] = true;
364 : : }
365 [ + + ]: 765 : if (!PG_ARGISNULL(N_DISTINCT_ARG))
366 : : {
367 : 693 : values[Anum_pg_statistic_stadistinct - 1] = PG_GETARG_DATUM(N_DISTINCT_ARG);
368 : 693 : replaces[Anum_pg_statistic_stadistinct - 1] = true;
369 : : }
370 : :
371 : : /* STATISTIC_KIND_MCV */
372 [ + + ]: 765 : if (do_mcv)
373 : : {
374 : : bool converted;
375 : 376 : Datum stanumbers = PG_GETARG_DATUM(MOST_COMMON_FREQS_ARG);
376 : 376 : 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 [ + + ]: 376 : if (converted)
383 : : {
384 : 373 : set_stats_slot(values, nulls, replaces,
385 : : STATISTIC_KIND_MCV,
386 : : eq_opr, atttypcoll,
387 : : stanumbers, false, stavalues, false);
388 : : }
389 : : else
390 : 3 : result = false;
391 : : }
392 : :
393 : : /* STATISTIC_KIND_HISTOGRAM */
394 [ + + ]: 765 : if (do_histogram)
395 : : {
396 : : Datum stavalues;
397 : 371 : bool converted = false;
398 : :
399 : 371 : stavalues = text_to_stavalues("histogram_bounds",
400 : : &array_in_fn,
401 : : PG_GETARG_DATUM(HISTOGRAM_BOUNDS_ARG),
402 : : atttypid, atttypmod,
403 : : &converted);
404 : :
405 [ + + ]: 371 : if (converted)
406 : : {
407 : 368 : set_stats_slot(values, nulls, replaces,
408 : : STATISTIC_KIND_HISTOGRAM,
409 : : lt_opr, atttypcoll,
410 : : 0, true, stavalues, false);
411 : : }
412 : : else
413 : 3 : result = false;
414 : : }
415 : :
416 : : /* STATISTIC_KIND_CORRELATION */
417 [ + + ]: 765 : if (do_correlation)
418 : : {
419 : 647 : Datum elems[] = {PG_GETARG_DATUM(CORRELATION_ARG)};
420 : 647 : ArrayType *arry = construct_array_builtin(elems, 1, FLOAT4OID);
421 : 647 : Datum stanumbers = PointerGetDatum(arry);
422 : :
423 : 647 : 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 [ + + ]: 765 : if (do_mcelem)
431 : : {
432 : 14 : Datum stanumbers = PG_GETARG_DATUM(MOST_COMMON_ELEM_FREQS_ARG);
433 : 14 : bool converted = false;
434 : : Datum stavalues;
435 : :
436 : 14 : 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 [ + - ]: 14 : if (converted)
443 : : {
444 : 14 : set_stats_slot(values, nulls, replaces,
445 : : STATISTIC_KIND_MCELEM,
446 : : elem_eq_opr, atttypcoll,
447 : : stanumbers, false, stavalues, false);
448 : : }
449 : : else
319 jdavis@postgresql.or 450 :UBC 0 : result = false;
451 : : }
452 : :
453 : : /* STATISTIC_KIND_DECHIST */
319 jdavis@postgresql.or 454 [ + + ]:CBC 765 : if (do_dechist)
455 : : {
456 : 13 : Datum stanumbers = PG_GETARG_DATUM(ELEM_COUNT_HISTOGRAM_ARG);
457 : :
458 : 13 : 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 [ + + ]: 765 : if (do_bounds_histogram)
472 : : {
473 : 10 : bool converted = false;
474 : : Datum stavalues;
475 : :
476 : 10 : 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 [ + - ]: 10 : if (converted)
483 : : {
484 : 10 : set_stats_slot(values, nulls, replaces,
485 : : STATISTIC_KIND_BOUNDS_HISTOGRAM,
486 : : InvalidOid, InvalidOid,
487 : : 0, true, stavalues, false);
488 : : }
489 : : else
319 jdavis@postgresql.or 490 :UBC 0 : result = false;
491 : : }
492 : :
493 : : /* STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM */
319 jdavis@postgresql.or 494 [ + + ]:CBC 765 : if (do_range_length_histogram)
495 : : {
496 : : /* The anyarray is always a float8[] for this stakind */
497 : 10 : Datum elems[] = {PG_GETARG_DATUM(RANGE_EMPTY_FRAC_ARG)};
498 : 10 : ArrayType *arry = construct_array_builtin(elems, 1, FLOAT4OID);
499 : 10 : Datum stanumbers = PointerGetDatum(arry);
500 : :
501 : 10 : bool converted = false;
502 : : Datum stavalues;
503 : :
504 : 10 : 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 [ + - ]: 10 : if (converted)
510 : : {
511 : 10 : set_stats_slot(values, nulls, replaces,
512 : : STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM,
513 : : Float8LessOperator, InvalidOid,
514 : : stanumbers, false, stavalues, false);
515 : : }
516 : : else
319 jdavis@postgresql.or 517 :UBC 0 : result = false;
518 : : }
519 : :
319 jdavis@postgresql.or 520 :CBC 765 : upsert_pg_statistic(starel, statup, values, nulls, replaces);
521 : :
522 [ + + ]: 765 : if (HeapTupleIsValid(statup))
523 : 63 : ReleaseSysCache(statup);
524 : 765 : table_close(starel, RowExclusiveLock);
525 : :
526 : 765 : 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 : 765 : get_attr_expr(Relation rel, int attnum)
537 : : {
538 : : List *index_exprs;
539 : : ListCell *indexpr_item;
540 : :
541 : : /* relation is not an index */
198 542 [ + + ]: 765 : if (rel->rd_rel->relkind != RELKIND_INDEX &&
543 [ + - ]: 758 : rel->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
544 : 758 : return NULL;
545 : :
546 : 7 : index_exprs = RelationGetIndexExpressions(rel);
547 : :
548 : : /* index has no expressions to give */
549 [ - + ]: 7 : if (index_exprs == NIL)
198 jdavis@postgresql.or 550 :UBC 0 : return NULL;
551 : :
552 : : /*
553 : : * The index attnum points directly to a relation attnum, then it's not an
554 : : * expression attribute.
555 : : */
198 jdavis@postgresql.or 556 [ - + ]:CBC 7 : if (rel->rd_index->indkey.values[attnum - 1] != 0)
198 jdavis@postgresql.or 557 :UBC 0 : return NULL;
558 : :
198 jdavis@postgresql.or 559 :CBC 7 : indexpr_item = list_head(rel->rd_indexprs);
560 : :
561 [ - + ]: 7 : for (int i = 0; i < attnum - 1; i++)
198 jdavis@postgresql.or 562 [ # # ]:UBC 0 : if (rel->rd_index->indkey.values[i] == 0)
563 : 0 : indexpr_item = lnext(rel->rd_indexprs, indexpr_item);
564 : :
198 jdavis@postgresql.or 565 [ - + ]:CBC 7 : if (indexpr_item == NULL) /* shouldn't happen */
198 jdavis@postgresql.or 566 [ # # ]:UBC 0 : elog(ERROR, "too few entries in indexprs list");
567 : :
198 jdavis@postgresql.or 568 :CBC 7 : return (Node *) lfirst(indexpr_item);
569 : : }
570 : :
571 : : /*
572 : : * Derive type information from the attribute.
573 : : */
574 : : static void
193 575 : 765 : 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 : : {
319 580 : 765 : Relation rel = relation_open(reloid, AccessShareLock);
581 : : Form_pg_attribute attr;
582 : : HeapTuple atup;
583 : : Node *expr;
584 : : TypeCacheEntry *typcache;
585 : :
586 : 765 : atup = SearchSysCache2(ATTNUM, ObjectIdGetDatum(reloid),
587 : : Int16GetDatum(attnum));
588 : :
589 : : /* Attribute not found */
590 [ - + ]: 765 : if (!HeapTupleIsValid(atup))
319 jdavis@postgresql.or 591 [ # # ]:UBC 0 : ereport(ERROR,
592 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
593 : : errmsg("column %d of relation \"%s\" does not exist",
594 : : attnum, RelationGetRelationName(rel))));
595 : :
319 jdavis@postgresql.or 596 :CBC 765 : attr = (Form_pg_attribute) GETSTRUCT(atup);
597 : :
598 [ - + ]: 765 : if (attr->attisdropped)
319 jdavis@postgresql.or 599 [ # # ]:UBC 0 : ereport(ERROR,
600 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
601 : : errmsg("column %d of relation \"%s\" does not exist",
602 : : attnum, RelationGetRelationName(rel))));
603 : :
319 jdavis@postgresql.or 604 :CBC 765 : 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 [ + + ]: 765 : if (expr == NULL)
613 : : {
614 : 758 : *atttypid = attr->atttypid;
615 : 758 : *atttypmod = attr->atttypmod;
616 : 758 : *atttypcoll = attr->attcollation;
617 : : }
618 : : else
619 : : {
620 : 7 : *atttypid = exprType(expr);
621 : 7 : *atttypmod = exprTypmod(expr);
622 : :
623 [ - + ]: 7 : if (OidIsValid(attr->attcollation))
319 jdavis@postgresql.or 624 :UBC 0 : *atttypcoll = attr->attcollation;
625 : : else
319 jdavis@postgresql.or 626 :CBC 7 : *atttypcoll = exprCollation(expr);
627 : : }
628 : 765 : 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 [ + + ]: 765 : if (type_is_multirange(*atttypid))
635 : 1 : *atttypid = get_multirange_range(*atttypid);
636 : :
637 : : /* finds the right operators even if atttypid is a domain */
638 : 765 : typcache = lookup_type_cache(*atttypid, TYPECACHE_LT_OPR | TYPECACHE_EQ_OPR);
639 : 765 : *atttyptype = typcache->typtype;
640 : 765 : *eq_opr = typcache->eq_opr;
641 : 765 : *lt_opr = typcache->lt_opr;
642 : :
643 : : /*
644 : : * Special case: collation for tsvector is DEFAULT_COLLATION_OID. See
645 : : * compute_tsvector_stats().
646 : : */
647 [ + + ]: 765 : if (*atttypid == TSVECTOROID)
648 : 1 : *atttypcoll = DEFAULT_COLLATION_OID;
649 : :
650 : 765 : relation_close(rel, NoLock);
651 : 765 : }
652 : :
653 : : /*
654 : : * Derive element type information from the attribute type.
655 : : */
656 : : static bool
193 657 : 26 : get_elem_stat_type(Oid atttypid, char atttyptype,
658 : : Oid *elemtypid, Oid *elem_eq_opr)
659 : : {
660 : : TypeCacheEntry *elemtypcache;
661 : :
319 662 [ + + ]: 26 : if (atttypid == TSVECTOROID)
663 : : {
664 : : /*
665 : : * Special case: element type for tsvector is text. See
666 : : * compute_tsvector_stats().
667 : : */
668 : 1 : *elemtypid = TEXTOID;
669 : : }
670 : : else
671 : : {
672 : : /* find underlying element type through any domain */
673 : 25 : *elemtypid = get_base_element_type(atttypid);
674 : : }
675 : :
676 [ + + ]: 26 : if (!OidIsValid(*elemtypid))
677 : 9 : return false;
678 : :
679 : : /* finds the right operator even if elemtypid is a domain */
680 : 17 : elemtypcache = lookup_type_cache(*elemtypid, TYPECACHE_EQ_OPR);
681 [ - + ]: 17 : if (!OidIsValid(elemtypcache->eq_opr))
319 jdavis@postgresql.or 682 :UBC 0 : return false;
683 : :
319 jdavis@postgresql.or 684 :CBC 17 : *elem_eq_opr = elemtypcache->eq_opr;
685 : :
686 : 17 : 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 : 781 : text_to_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid typid,
698 : : int32 typmod, bool *ok)
699 : : {
700 : 781 : LOCAL_FCINFO(fcinfo, 8);
701 : : char *s;
702 : : Datum result;
703 : 781 : ErrorSaveContext escontext = {T_ErrorSaveContext};
704 : :
705 : 781 : escontext.details_wanted = true;
706 : :
707 : 781 : s = TextDatumGetCString(d);
708 : :
709 : 781 : InitFunctionCallInfoData(*fcinfo, array_in, 3, InvalidOid,
710 : : (Node *) &escontext, NULL);
711 : :
712 : 781 : fcinfo->args[0].value = CStringGetDatum(s);
713 : 781 : fcinfo->args[0].isnull = false;
714 : 781 : fcinfo->args[1].value = ObjectIdGetDatum(typid);
715 : 781 : fcinfo->args[1].isnull = false;
716 : 781 : fcinfo->args[2].value = Int32GetDatum(typmod);
717 : 781 : fcinfo->args[2].isnull = false;
718 : :
719 : 781 : result = FunctionCallInvoke(fcinfo);
720 : :
721 : 781 : pfree(s);
722 : :
318 723 [ + + ]: 781 : if (escontext.error_occurred)
724 : : {
193 725 : 3 : escontext.error_data->elevel = WARNING;
319 726 : 3 : ThrowErrorData(escontext.error_data);
727 : 3 : *ok = false;
728 : 3 : return (Datum) 0;
729 : : }
730 : :
731 [ + + ]: 778 : if (array_contains_nulls(DatumGetArrayTypeP(result)))
732 : : {
193 733 [ + - ]: 3 : ereport(WARNING,
734 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
735 : : errmsg("\"%s\" array must not contain null values", staname)));
319 736 : 3 : *ok = false;
737 : 3 : return (Datum) 0;
738 : : }
739 : :
740 : 775 : *ok = true;
741 : :
742 : 775 : 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 : 1435 : 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 : 1435 : 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 [ + + ]: 8610 : for (slotidx = 0; slotidx < STATISTIC_NUM_SLOTS; slotidx++)
763 : : {
764 : 7175 : stakind_attnum = Anum_pg_statistic_stakind1 - 1 + slotidx;
765 : :
766 [ + + + + ]: 9499 : if (first_empty < 0 &&
767 : 2324 : DatumGetInt16(values[stakind_attnum]) == 0)
768 : 1435 : first_empty = slotidx;
769 [ - + ]: 7175 : if (DatumGetInt16(values[stakind_attnum]) == stakind)
319 jdavis@postgresql.or 770 :UBC 0 : break;
771 : : }
772 : :
319 jdavis@postgresql.or 773 [ + - + - ]:CBC 1435 : if (slotidx >= STATISTIC_NUM_SLOTS && first_empty >= 0)
774 : 1435 : slotidx = first_empty;
775 : :
776 [ - + ]: 1435 : if (slotidx >= STATISTIC_NUM_SLOTS)
319 jdavis@postgresql.or 777 [ # # ]:UBC 0 : ereport(ERROR,
778 : : (errmsg("maximum number of statistics slots exceeded: %d",
779 : : slotidx + 1)));
780 : :
319 jdavis@postgresql.or 781 :CBC 1435 : stakind_attnum = Anum_pg_statistic_stakind1 - 1 + slotidx;
782 : 1435 : staop_attnum = Anum_pg_statistic_staop1 - 1 + slotidx;
783 : 1435 : stacoll_attnum = Anum_pg_statistic_stacoll1 - 1 + slotidx;
784 : :
785 [ + - ]: 1435 : if (DatumGetInt16(values[stakind_attnum]) != stakind)
786 : : {
787 : 1435 : values[stakind_attnum] = Int16GetDatum(stakind);
788 : 1435 : replaces[stakind_attnum] = true;
789 : : }
790 [ + + ]: 1435 : if (DatumGetObjectId(values[staop_attnum]) != staop)
791 : : {
792 : 1425 : values[staop_attnum] = ObjectIdGetDatum(staop);
793 : 1425 : replaces[staop_attnum] = true;
794 : : }
795 [ + + ]: 1435 : if (DatumGetObjectId(values[stacoll_attnum]) != stacoll)
796 : : {
797 : 342 : values[stacoll_attnum] = ObjectIdGetDatum(stacoll);
798 : 342 : replaces[stacoll_attnum] = true;
799 : : }
800 [ + + ]: 1435 : if (!stanumbers_isnull)
801 : : {
802 : 1057 : values[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = stanumbers;
803 : 1057 : nulls[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = false;
804 : 1057 : replaces[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = true;
805 : : }
806 [ + + ]: 1435 : if (!stavalues_isnull)
807 : : {
808 : 775 : values[Anum_pg_statistic_stavalues1 - 1 + slotidx] = stavalues;
809 : 775 : nulls[Anum_pg_statistic_stavalues1 - 1 + slotidx] = false;
810 : 775 : replaces[Anum_pg_statistic_stavalues1 - 1 + slotidx] = true;
811 : : }
812 : 1435 : }
813 : :
814 : : /*
815 : : * Upsert the pg_statistic record.
816 : : */
817 : : static void
818 : 765 : upsert_pg_statistic(Relation starel, HeapTuple oldtup,
819 : : Datum *values, bool *nulls, bool *replaces)
820 : : {
821 : : HeapTuple newtup;
822 : :
823 [ + + ]: 765 : if (HeapTupleIsValid(oldtup))
824 : : {
825 : 63 : newtup = heap_modify_tuple(oldtup, RelationGetDescr(starel),
826 : : values, nulls, replaces);
827 : 63 : CatalogTupleUpdate(starel, &newtup->t_self, newtup);
828 : : }
829 : : else
830 : : {
831 : 702 : newtup = heap_form_tuple(RelationGetDescr(starel), values, nulls);
832 : 702 : CatalogTupleInsert(starel, newtup);
833 : : }
834 : :
835 : 765 : heap_freetuple(newtup);
836 : :
312 837 : 765 : CommandCounterIncrement();
319 838 : 765 : }
839 : :
840 : : /*
841 : : * Delete pg_statistic record.
842 : : */
843 : : static bool
844 : 3 : delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit)
845 : : {
846 : 3 : Relation sd = table_open(StatisticRelationId, RowExclusiveLock);
847 : : HeapTuple oldtup;
312 848 : 3 : bool result = false;
849 : :
850 : : /* Is there already a pg_statistic tuple for this attribute? */
319 851 : 3 : oldtup = SearchSysCache3(STATRELATTINH,
852 : : ObjectIdGetDatum(reloid),
853 : : Int16GetDatum(attnum),
854 : : BoolGetDatum(stainherit));
855 : :
856 [ + - ]: 3 : if (HeapTupleIsValid(oldtup))
857 : : {
858 : 3 : CatalogTupleDelete(sd, &oldtup->t_self);
859 : 3 : ReleaseSysCache(oldtup);
312 860 : 3 : result = true;
861 : : }
862 : :
319 863 : 3 : table_close(sd, RowExclusiveLock);
864 : :
312 865 : 3 : CommandCounterIncrement();
866 : :
867 : 3 : return result;
868 : : }
869 : :
870 : : /*
871 : : * Initialize values and nulls for a new stats tuple.
872 : : */
873 : : static void
319 874 : 702 : init_empty_stats_tuple(Oid reloid, int16 attnum, bool inherited,
875 : : Datum *values, bool *nulls, bool *replaces)
876 : : {
877 : 702 : memset(nulls, true, sizeof(bool) * Natts_pg_statistic);
878 : 702 : memset(replaces, true, sizeof(bool) * Natts_pg_statistic);
879 : :
880 : : /* must initialize non-NULL attributes */
881 : :
882 : 702 : values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(reloid);
883 : 702 : nulls[Anum_pg_statistic_starelid - 1] = false;
884 : 702 : values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(attnum);
885 : 702 : nulls[Anum_pg_statistic_staattnum - 1] = false;
886 : 702 : values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(inherited);
887 : 702 : nulls[Anum_pg_statistic_stainherit - 1] = false;
888 : :
889 : 702 : values[Anum_pg_statistic_stanullfrac - 1] = DEFAULT_NULL_FRAC;
890 : 702 : nulls[Anum_pg_statistic_stanullfrac - 1] = false;
891 : 702 : values[Anum_pg_statistic_stawidth - 1] = DEFAULT_AVG_WIDTH;
892 : 702 : nulls[Anum_pg_statistic_stawidth - 1] = false;
893 : 702 : values[Anum_pg_statistic_stadistinct - 1] = DEFAULT_N_DISTINCT;
894 : 702 : nulls[Anum_pg_statistic_stadistinct - 1] = false;
895 : :
896 : : /* initialize stakind, staop, and stacoll slots */
897 [ + + ]: 4212 : for (int slotnum = 0; slotnum < STATISTIC_NUM_SLOTS; slotnum++)
898 : : {
899 : 3510 : values[Anum_pg_statistic_stakind1 + slotnum - 1] = (Datum) 0;
900 : 3510 : nulls[Anum_pg_statistic_stakind1 + slotnum - 1] = false;
29 peter@eisentraut.org 901 :GNC 3510 : values[Anum_pg_statistic_staop1 + slotnum - 1] = ObjectIdGetDatum(InvalidOid);
319 jdavis@postgresql.or 902 :CBC 3510 : nulls[Anum_pg_statistic_staop1 + slotnum - 1] = false;
29 peter@eisentraut.org 903 :GNC 3510 : values[Anum_pg_statistic_stacoll1 + slotnum - 1] = ObjectIdGetDatum(InvalidOid);
319 jdavis@postgresql.or 904 :CBC 3510 : nulls[Anum_pg_statistic_stacoll1 + slotnum - 1] = false;
905 : : }
906 : 702 : }
907 : :
908 : : /*
909 : : * Delete statistics for the given attribute.
910 : : */
911 : : Datum
912 : 3 : 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 : :
165 921 : 3 : stats_check_required_arg(fcinfo, cleararginfo, C_ATTRELSCHEMA_ARG);
922 : 3 : stats_check_required_arg(fcinfo, cleararginfo, C_ATTRELNAME_ARG);
923 : 3 : stats_check_required_arg(fcinfo, cleararginfo, C_ATTNAME_ARG);
924 : 3 : stats_check_required_arg(fcinfo, cleararginfo, C_INHERITED_ARG);
925 : :
926 : 3 : nspname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTRELSCHEMA_ARG));
927 : 3 : relname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTRELNAME_ARG));
928 : :
929 : 3 : reloid = stats_lookup_relid(nspname, relname);
930 : :
290 fujii@postgresql.org 931 [ - + ]: 3 : if (RecoveryInProgress())
290 fujii@postgresql.org 932 [ # # ]:UBC 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 : :
319 jdavis@postgresql.or 937 :CBC 3 : stats_lock_check_privileges(reloid);
938 : :
165 939 : 3 : attname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTNAME_ARG));
940 : 3 : attnum = get_attnum(reloid, attname);
941 : :
288 942 [ - + ]: 3 : if (attnum < 0)
288 jdavis@postgresql.or 943 [ # # ]:UBC 0 : ereport(ERROR,
944 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
945 : : errmsg("cannot clear statistics on system column \"%s\"",
946 : : attname)));
947 : :
318 jdavis@postgresql.or 948 [ - + ]:CBC 3 : if (attnum == InvalidAttrNumber)
318 jdavis@postgresql.or 949 [ # # ]:UBC 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 : :
192 tgl@sss.pgh.pa.us 954 :CBC 3 : inherited = PG_GETARG_BOOL(C_INHERITED_ARG);
955 : :
319 jdavis@postgresql.or 956 : 3 : delete_pg_statistic(reloid, attnum, inherited);
957 : 3 : 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
317 987 : 798 : pg_restore_attribute_stats(PG_FUNCTION_ARGS)
988 : : {
989 : 798 : LOCAL_FCINFO(positional_fcinfo, NUM_ATTRIBUTE_STATS_ARGS);
990 : 798 : bool result = true;
991 : :
992 : 798 : InitFunctionCallInfoData(*positional_fcinfo, NULL, NUM_ATTRIBUTE_STATS_ARGS,
993 : : InvalidOid, NULL, NULL);
994 : :
995 [ + + ]: 798 : if (!stats_fill_fcinfo_from_arg_pairs(fcinfo, positional_fcinfo,
996 : : attarginfo))
997 : 6 : result = false;
998 : :
193 999 [ + + ]: 798 : if (!attribute_statistics_update(positional_fcinfo))
317 1000 : 45 : result = false;
1001 : :
1002 : 765 : PG_RETURN_BOOL(result);
1003 : : }
|