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