PostgreSQL Source Code git master
attribute_stats.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/pg_operator.h"
#include "nodes/makefuncs.h"
#include "statistics/statistics.h"
#include "statistics/stat_utils.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Include dependency graph for attribute_stats.c:

Go to the source code of this file.

Enumerations

enum  attribute_stats_argnum {
  ATTRELSCHEMA_ARG = 0 , ATTRELNAME_ARG , ATTNAME_ARG , ATTNUM_ARG ,
  INHERITED_ARG , NULL_FRAC_ARG , AVG_WIDTH_ARG , N_DISTINCT_ARG ,
  MOST_COMMON_VALS_ARG , MOST_COMMON_FREQS_ARG , HISTOGRAM_BOUNDS_ARG , CORRELATION_ARG ,
  MOST_COMMON_ELEMS_ARG , MOST_COMMON_ELEM_FREQS_ARG , ELEM_COUNT_HISTOGRAM_ARG , RANGE_LENGTH_HISTOGRAM_ARG ,
  RANGE_EMPTY_FRAC_ARG , RANGE_BOUNDS_HISTOGRAM_ARG , NUM_ATTRIBUTE_STATS_ARGS
}
 
enum  clear_attribute_stats_argnum {
  C_ATTRELSCHEMA_ARG = 0 , C_ATTRELNAME_ARG , C_ATTNAME_ARG , C_INHERITED_ARG ,
  C_NUM_ATTRIBUTE_STATS_ARGS
}
 

Functions

static bool attribute_statistics_update (FunctionCallInfo fcinfo)
 
static void upsert_pg_statistic (Relation starel, HeapTuple oldtup, const Datum *values, const bool *nulls, const bool *replaces)
 
static bool delete_pg_statistic (Oid reloid, AttrNumber attnum, bool stainherit)
 
Datum pg_clear_attribute_stats (PG_FUNCTION_ARGS)
 
Datum pg_restore_attribute_stats (PG_FUNCTION_ARGS)
 

Variables

static struct StatsArgInfo attarginfo []
 
static struct StatsArgInfo cleararginfo []
 

Enumeration Type Documentation

◆ attribute_stats_argnum

Enumerator
ATTRELSCHEMA_ARG 
ATTRELNAME_ARG 
ATTNAME_ARG 
ATTNUM_ARG 
INHERITED_ARG 
NULL_FRAC_ARG 
AVG_WIDTH_ARG 
N_DISTINCT_ARG 
MOST_COMMON_VALS_ARG 
MOST_COMMON_FREQS_ARG 
HISTOGRAM_BOUNDS_ARG 
CORRELATION_ARG 
MOST_COMMON_ELEMS_ARG 
MOST_COMMON_ELEM_FREQS_ARG 
ELEM_COUNT_HISTOGRAM_ARG 
RANGE_LENGTH_HISTOGRAM_ARG 
RANGE_EMPTY_FRAC_ARG 
RANGE_BOUNDS_HISTOGRAM_ARG 
NUM_ATTRIBUTE_STATS_ARGS 

Definition at line 38 of file attribute_stats.c.

39{
59};
@ ATTNUM_ARG
@ RANGE_LENGTH_HISTOGRAM_ARG
@ RANGE_BOUNDS_HISTOGRAM_ARG
@ ATTRELSCHEMA_ARG
@ AVG_WIDTH_ARG
@ INHERITED_ARG
@ ATTRELNAME_ARG
@ MOST_COMMON_ELEMS_ARG
@ NULL_FRAC_ARG
@ NUM_ATTRIBUTE_STATS_ARGS
@ MOST_COMMON_FREQS_ARG
@ CORRELATION_ARG
@ HISTOGRAM_BOUNDS_ARG
@ MOST_COMMON_VALS_ARG
@ RANGE_EMPTY_FRAC_ARG
@ ELEM_COUNT_HISTOGRAM_ARG
@ ATTNAME_ARG
@ N_DISTINCT_ARG
@ MOST_COMMON_ELEM_FREQS_ARG

◆ clear_attribute_stats_argnum

Enumerator
C_ATTRELSCHEMA_ARG 
C_ATTRELNAME_ARG 
C_ATTNAME_ARG 
C_INHERITED_ARG 
C_NUM_ATTRIBUTE_STATS_ARGS 

Definition at line 89 of file attribute_stats.c.

90{
96};
@ C_INHERITED_ARG
@ C_NUM_ATTRIBUTE_STATS_ARGS
@ C_ATTNAME_ARG
@ C_ATTRELNAME_ARG
@ C_ATTRELSCHEMA_ARG

Function Documentation

◆ attribute_statistics_update()

static bool attribute_statistics_update ( FunctionCallInfo  fcinfo)
static

Definition at line 129 of file attribute_stats.c.

130{
131 char *nspname;
132 char *relname;
133 Oid reloid;
134 char *attname;
136 bool inherited;
137 Oid locked_table = InvalidOid;
138
139 Relation starel;
140 HeapTuple statup;
141
142 Oid atttypid = InvalidOid;
143 int32 atttypmod;
144 char atttyptype;
145 Oid atttypcoll = InvalidOid;
146 Oid eq_opr = InvalidOid;
147 Oid lt_opr = InvalidOid;
148
149 Oid elemtypid = InvalidOid;
150 Oid elem_eq_opr = InvalidOid;
151
152 FmgrInfo array_in_fn;
153
154 bool do_mcv = !PG_ARGISNULL(MOST_COMMON_FREQS_ARG) &&
156 bool do_histogram = !PG_ARGISNULL(HISTOGRAM_BOUNDS_ARG);
157 bool do_correlation = !PG_ARGISNULL(CORRELATION_ARG);
158 bool do_mcelem = !PG_ARGISNULL(MOST_COMMON_ELEMS_ARG) &&
160 bool do_dechist = !PG_ARGISNULL(ELEM_COUNT_HISTOGRAM_ARG);
161 bool do_bounds_histogram = !PG_ARGISNULL(RANGE_BOUNDS_HISTOGRAM_ARG);
162 bool do_range_length_histogram = !PG_ARGISNULL(RANGE_LENGTH_HISTOGRAM_ARG) &&
164
165 Datum values[Natts_pg_statistic] = {0};
166 bool nulls[Natts_pg_statistic] = {0};
167 bool replaces[Natts_pg_statistic] = {0};
168
169 bool result = true;
170
173
176
177 if (RecoveryInProgress())
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 reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
186 RangeVarCallbackForStats, &locked_table);
187
188 /* user can specify either attname or attnum, but not both */
190 {
193 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
194 errmsg("cannot specify both \"%s\" and \"%s\"", "attname", "attnum")));
196 attnum = get_attnum(reloid, attname);
197 /* note that this test covers attisdropped cases too: */
200 (errcode(ERRCODE_UNDEFINED_COLUMN),
201 errmsg("column \"%s\" of relation \"%s\" does not exist",
202 attname, relname)));
203 }
204 else if (!PG_ARGISNULL(ATTNUM_ARG))
205 {
207 attname = get_attname(reloid, attnum, true);
208 /* annoyingly, get_attname doesn't check attisdropped */
209 if (attname == NULL ||
212 (errcode(ERRCODE_UNDEFINED_COLUMN),
213 errmsg("column %d of relation \"%s\" does not exist",
214 attnum, relname)));
215 }
216 else
217 {
219 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
220 errmsg("must specify either \"%s\" or \"%s\"", "attname", "attnum")));
221 attname = NULL; /* keep compiler quiet */
222 attnum = 0;
223 }
224
225 if (attnum < 0)
227 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
228 errmsg("cannot modify statistics on system column \"%s\"",
229 attname)));
230
232 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
240 {
241 do_mcv = false;
242 result = false;
243 }
244
246 {
247 do_mcelem = false;
248 result = false;
249 }
251 {
252 do_dechist = false;
253 result = false;
254 }
255
256 if (!stats_check_arg_pair(fcinfo, attarginfo,
258 {
259 do_mcv = false;
260 result = false;
261 }
262
263 if (!stats_check_arg_pair(fcinfo, attarginfo,
266 {
267 do_mcelem = false;
268 result = false;
269 }
270
271 if (!stats_check_arg_pair(fcinfo, attarginfo,
274 {
275 do_range_length_histogram = false;
276 result = false;
277 }
278
279 /* derive information from attribute */
280 statatt_get_type(reloid, attnum,
281 &atttypid, &atttypmod,
282 &atttyptype, &atttypcoll,
283 &eq_opr, &lt_opr);
284
285 /* if needed, derive element type */
286 if (do_mcelem || do_dechist)
287 {
288 if (!statatt_get_elem_type(atttypid, atttyptype,
289 &elemtypid, &elem_eq_opr))
290 {
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 elemtypid = InvalidOid;
296 elem_eq_opr = InvalidOid;
297
298 do_mcelem = false;
299 do_dechist = false;
300 result = false;
301 }
302 }
303
304 /* histogram and correlation require less-than operator */
305 if ((do_histogram || do_correlation) && !OidIsValid(lt_opr))
306 {
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 do_histogram = false;
314 do_correlation = false;
315 result = false;
316 }
317
318 /* only range types can have range stats */
319 if ((do_range_length_histogram || do_bounds_histogram) &&
320 !(atttyptype == TYPTYPE_RANGE || atttyptype == TYPTYPE_MULTIRANGE))
321 {
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 do_bounds_histogram = false;
329 do_range_length_histogram = false;
330 result = false;
331 }
332
333 fmgr_info(F_ARRAY_IN, &array_in_fn);
334
335 starel = table_open(StatisticRelationId, RowExclusiveLock);
336
337 statup = SearchSysCache3(STATRELATTINH, ObjectIdGetDatum(reloid), Int16GetDatum(attnum), BoolGetDatum(inherited));
338
339 /* initialize from existing tuple if exists */
340 if (HeapTupleIsValid(statup))
341 heap_deform_tuple(statup, RelationGetDescr(starel), values, nulls);
342 else
343 statatt_init_empty_tuple(reloid, attnum, inherited, values, nulls,
344 replaces);
345
346 /* if specified, set to argument values */
348 {
349 values[Anum_pg_statistic_stanullfrac - 1] = PG_GETARG_DATUM(NULL_FRAC_ARG);
350 replaces[Anum_pg_statistic_stanullfrac - 1] = true;
351 }
353 {
354 values[Anum_pg_statistic_stawidth - 1] = PG_GETARG_DATUM(AVG_WIDTH_ARG);
355 replaces[Anum_pg_statistic_stawidth - 1] = true;
356 }
358 {
359 values[Anum_pg_statistic_stadistinct - 1] = PG_GETARG_DATUM(N_DISTINCT_ARG);
360 replaces[Anum_pg_statistic_stadistinct - 1] = true;
361 }
362
363 /* STATISTIC_KIND_MCV */
364 if (do_mcv)
365 {
366 bool converted;
368 Datum stavalues = statatt_build_stavalues("most_common_vals",
369 &array_in_fn,
371 atttypid, atttypmod,
372 &converted);
373
374 if (converted)
375 {
376 statatt_set_slot(values, nulls, replaces,
377 STATISTIC_KIND_MCV,
378 eq_opr, atttypcoll,
379 stanumbers, false, stavalues, false);
380 }
381 else
382 result = false;
383 }
384
385 /* STATISTIC_KIND_HISTOGRAM */
386 if (do_histogram)
387 {
388 Datum stavalues;
389 bool converted = false;
390
391 stavalues = statatt_build_stavalues("histogram_bounds",
392 &array_in_fn,
394 atttypid, atttypmod,
395 &converted);
396
397 if (converted)
398 {
399 statatt_set_slot(values, nulls, replaces,
400 STATISTIC_KIND_HISTOGRAM,
401 lt_opr, atttypcoll,
402 0, true, stavalues, false);
403 }
404 else
405 result = false;
406 }
407
408 /* STATISTIC_KIND_CORRELATION */
409 if (do_correlation)
410 {
412 ArrayType *arry = construct_array_builtin(elems, 1, FLOAT4OID);
413 Datum stanumbers = PointerGetDatum(arry);
414
415 statatt_set_slot(values, nulls, replaces,
416 STATISTIC_KIND_CORRELATION,
417 lt_opr, atttypcoll,
418 stanumbers, false, 0, true);
419 }
420
421 /* STATISTIC_KIND_MCELEM */
422 if (do_mcelem)
423 {
425 bool converted = false;
426 Datum stavalues;
427
428 stavalues = statatt_build_stavalues("most_common_elems",
429 &array_in_fn,
431 elemtypid, atttypmod,
432 &converted);
433
434 if (converted)
435 {
436 statatt_set_slot(values, nulls, replaces,
437 STATISTIC_KIND_MCELEM,
438 elem_eq_opr, atttypcoll,
439 stanumbers, false, stavalues, false);
440 }
441 else
442 result = false;
443 }
444
445 /* STATISTIC_KIND_DECHIST */
446 if (do_dechist)
447 {
449
450 statatt_set_slot(values, nulls, replaces,
451 STATISTIC_KIND_DECHIST,
452 elem_eq_opr, atttypcoll,
453 stanumbers, false, 0, true);
454 }
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 if (do_bounds_histogram)
464 {
465 bool converted = false;
466 Datum stavalues;
467
468 stavalues = statatt_build_stavalues("range_bounds_histogram",
469 &array_in_fn,
471 atttypid, atttypmod,
472 &converted);
473
474 if (converted)
475 {
476 statatt_set_slot(values, nulls, replaces,
477 STATISTIC_KIND_BOUNDS_HISTOGRAM,
479 0, true, stavalues, false);
480 }
481 else
482 result = false;
483 }
484
485 /* STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM */
486 if (do_range_length_histogram)
487 {
488 /* The anyarray is always a float8[] for this stakind */
490 ArrayType *arry = construct_array_builtin(elems, 1, FLOAT4OID);
491 Datum stanumbers = PointerGetDatum(arry);
492
493 bool converted = false;
494 Datum stavalues;
495
496 stavalues = statatt_build_stavalues("range_length_histogram",
497 &array_in_fn,
499 FLOAT8OID, 0, &converted);
500
501 if (converted)
502 {
503 statatt_set_slot(values, nulls, replaces,
504 STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM,
505 Float8LessOperator, InvalidOid,
506 stanumbers, false, stavalues, false);
507 }
508 else
509 result = false;
510 }
511
512 upsert_pg_statistic(starel, statup, values, nulls, replaces);
513
514 if (HeapTupleIsValid(statup))
515 ReleaseSysCache(statup);
517
518 return result;
519}
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3382
int16 AttrNumber
Definition: attnum.h:21
#define InvalidAttrNumber
Definition: attnum.h:23
static void upsert_pg_statistic(Relation starel, HeapTuple oldtup, const Datum *values, const bool *nulls, const bool *replaces)
static struct StatsArgInfo attarginfo[]
static Datum values[MAXATTR]
Definition: bootstrap.c:155
#define TextDatumGetCString(d)
Definition: builtins.h:98
int32_t int32
Definition: c.h:548
#define OidIsValid(objectId)
Definition: c.h:794
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errhint(const char *fmt,...)
Definition: elog.c:1330
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:128
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1346
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define RowExclusiveLock
Definition: lockdefs.h:38
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:934
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:903
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:473
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:440
NameData attname
Definition: pg_attribute.h:41
int16 attnum
Definition: pg_attribute.h:74
NameData relname
Definition: pg_class.h:38
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:352
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:182
static Datum BoolGetDatum(bool X)
Definition: postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
uint64_t Datum
Definition: postgres.h:70
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationGetDescr(relation)
Definition: rel.h:541
bool statatt_get_elem_type(Oid atttypid, char atttyptype, Oid *elemtypid, Oid *elem_eq_opr)
Definition: stat_utils.c:522
Datum statatt_build_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid typid, int32 typmod, bool *ok)
Definition: stat_utils.c:566
void RangeVarCallbackForStats(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition: stat_utils.c:141
void statatt_init_empty_tuple(Oid reloid, int16 attnum, bool inherited, Datum *values, bool *nulls, bool *replaces)
Definition: stat_utils.c:712
bool stats_check_arg_array(FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum)
Definition: stat_utils.c:69
void statatt_get_type(Oid reloid, AttrNumber attnum, Oid *atttypid, int32 *atttypmod, char *atttyptype, Oid *atttypcoll, Oid *eq_opr, Oid *lt_opr)
Definition: stat_utils.c:436
void stats_check_required_arg(FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum)
Definition: stat_utils.c:50
void statatt_set_slot(Datum *values, bool *nulls, bool *replaces, int16 stakind, Oid staop, Oid stacoll, Datum stanumbers, bool stanumbers_isnull, Datum stavalues, bool stavalues_isnull)
Definition: stat_utils.c:634
bool stats_check_arg_pair(FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum1, int argnum2)
Definition: stat_utils.c:110
Definition: fmgr.h:57
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Definition: syscache.c:240
bool SearchSysCacheExistsAttName(Oid relid, const char *attname)
Definition: syscache.c:517
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
bool RecoveryInProgress(void)
Definition: xlog.c:6461

References attarginfo, attname, ATTNAME_ARG, attnum, ATTNUM_ARG, ATTRELNAME_ARG, ATTRELSCHEMA_ARG, AVG_WIDTH_ARG, BoolGetDatum(), construct_array_builtin(), CORRELATION_ARG, ELEM_COUNT_HISTOGRAM_ARG, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, fmgr_info(), get_attname(), get_attnum(), heap_deform_tuple(), HeapTupleIsValid, HISTOGRAM_BOUNDS_ARG, INHERITED_ARG, Int16GetDatum(), InvalidAttrNumber, InvalidOid, makeRangeVar(), MOST_COMMON_ELEM_FREQS_ARG, MOST_COMMON_ELEMS_ARG, MOST_COMMON_FREQS_ARG, MOST_COMMON_VALS_ARG, N_DISTINCT_ARG, NULL_FRAC_ARG, ObjectIdGetDatum(), OidIsValid, PG_ARGISNULL, PG_GETARG_BOOL, PG_GETARG_DATUM, PG_GETARG_INT16, PointerGetDatum(), RANGE_BOUNDS_HISTOGRAM_ARG, RANGE_EMPTY_FRAC_ARG, RANGE_LENGTH_HISTOGRAM_ARG, RangeVarCallbackForStats(), RangeVarGetRelidExtended(), RecoveryInProgress(), RelationGetDescr, ReleaseSysCache(), relname, RowExclusiveLock, SearchSysCache3(), SearchSysCacheExistsAttName(), ShareUpdateExclusiveLock, statatt_build_stavalues(), statatt_get_elem_type(), statatt_get_type(), statatt_init_empty_tuple(), statatt_set_slot(), stats_check_arg_array(), stats_check_arg_pair(), stats_check_required_arg(), table_close(), table_open(), TextDatumGetCString, upsert_pg_statistic(), values, and WARNING.

Referenced by pg_restore_attribute_stats().

◆ delete_pg_statistic()

static bool delete_pg_statistic ( Oid  reloid,
AttrNumber  attnum,
bool  stainherit 
)
static

Definition at line 551 of file attribute_stats.c.

552{
553 Relation sd = table_open(StatisticRelationId, RowExclusiveLock);
554 HeapTuple oldtup;
555 bool result = false;
556
557 /* Is there already a pg_statistic tuple for this attribute? */
558 oldtup = SearchSysCache3(STATRELATTINH,
559 ObjectIdGetDatum(reloid),
561 BoolGetDatum(stainherit));
562
563 if (HeapTupleIsValid(oldtup))
564 {
565 CatalogTupleDelete(sd, &oldtup->t_self);
566 ReleaseSysCache(oldtup);
567 result = true;
568 }
569
571
573
574 return result;
575}
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition: indexing.c:365
ItemPointerData t_self
Definition: htup.h:65
void CommandCounterIncrement(void)
Definition: xact.c:1101

References attnum, BoolGetDatum(), CatalogTupleDelete(), CommandCounterIncrement(), HeapTupleIsValid, Int16GetDatum(), ObjectIdGetDatum(), ReleaseSysCache(), RowExclusiveLock, SearchSysCache3(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by pg_clear_attribute_stats().

◆ pg_clear_attribute_stats()

Datum pg_clear_attribute_stats ( PG_FUNCTION_ARGS  )

Definition at line 581 of file attribute_stats.c.

582{
583 char *nspname;
584 char *relname;
585 Oid reloid;
586 char *attname;
588 bool inherited;
589 Oid locked_table = InvalidOid;
590
595
598
599 if (RecoveryInProgress())
601 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
602 errmsg("recovery is in progress"),
603 errhint("Statistics cannot be modified during recovery.")));
604
605 reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
607 RangeVarCallbackForStats, &locked_table);
608
610 attnum = get_attnum(reloid, attname);
611
612 if (attnum < 0)
614 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
615 errmsg("cannot clear statistics on system column \"%s\"",
616 attname)));
617
620 (errcode(ERRCODE_UNDEFINED_COLUMN),
621 errmsg("column \"%s\" of relation \"%s\" does not exist",
622 attname, get_rel_name(reloid))));
623
624 inherited = PG_GETARG_BOOL(C_INHERITED_ARG);
625
626 delete_pg_statistic(reloid, attnum, inherited);
628}
static bool delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit)
static struct StatsArgInfo cleararginfo[]
#define PG_RETURN_VOID()
Definition: fmgr.h:350
char * get_rel_name(Oid relid)
Definition: lsyscache.c:2078

References attname, attnum, C_ATTNAME_ARG, C_ATTRELNAME_ARG, C_ATTRELSCHEMA_ARG, C_INHERITED_ARG, cleararginfo, delete_pg_statistic(), ereport, errcode(), errhint(), errmsg(), ERROR, get_attnum(), get_rel_name(), InvalidAttrNumber, InvalidOid, makeRangeVar(), PG_GETARG_BOOL, PG_GETARG_DATUM, PG_RETURN_VOID, RangeVarCallbackForStats(), RangeVarGetRelidExtended(), RecoveryInProgress(), relname, ShareUpdateExclusiveLock, stats_check_required_arg(), and TextDatumGetCString.

◆ pg_restore_attribute_stats()

Datum pg_restore_attribute_stats ( PG_FUNCTION_ARGS  )

Definition at line 657 of file attribute_stats.c.

658{
659 LOCAL_FCINFO(positional_fcinfo, NUM_ATTRIBUTE_STATS_ARGS);
660 bool result = true;
661
662 InitFunctionCallInfoData(*positional_fcinfo, NULL, NUM_ATTRIBUTE_STATS_ARGS,
663 InvalidOid, NULL, NULL);
664
665 if (!stats_fill_fcinfo_from_arg_pairs(fcinfo, positional_fcinfo,
666 attarginfo))
667 result = false;
668
669 if (!attribute_statistics_update(positional_fcinfo))
670 result = false;
671
672 PG_RETURN_BOOL(result);
673}
static bool attribute_statistics_update(FunctionCallInfo fcinfo)
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:360
bool stats_fill_fcinfo_from_arg_pairs(FunctionCallInfo pairs_fcinfo, FunctionCallInfo positional_fcinfo, struct StatsArgInfo *arginfo)
Definition: stat_utils.c:347

References attarginfo, attribute_statistics_update(), InitFunctionCallInfoData, InvalidOid, LOCAL_FCINFO, NUM_ATTRIBUTE_STATS_ARGS, PG_RETURN_BOOL, and stats_fill_fcinfo_from_arg_pairs().

◆ upsert_pg_statistic()

static void upsert_pg_statistic ( Relation  starel,
HeapTuple  oldtup,
const Datum values,
const bool *  nulls,
const bool *  replaces 
)
static

Definition at line 525 of file attribute_stats.c.

527{
528 HeapTuple newtup;
529
530 if (HeapTupleIsValid(oldtup))
531 {
532 newtup = heap_modify_tuple(oldtup, RelationGetDescr(starel),
533 values, nulls, replaces);
534 CatalogTupleUpdate(starel, &newtup->t_self, newtup);
535 }
536 else
537 {
538 newtup = heap_form_tuple(RelationGetDescr(starel), values, nulls);
539 CatalogTupleInsert(starel, newtup);
540 }
541
542 heap_freetuple(newtup);
543
545}
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition: heaptuple.c:1210
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition: indexing.c:313
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233

References CatalogTupleInsert(), CatalogTupleUpdate(), CommandCounterIncrement(), heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, RelationGetDescr, HeapTupleData::t_self, and values.

Referenced by attribute_statistics_update().

Variable Documentation

◆ attarginfo

struct StatsArgInfo attarginfo[]
static
Initial value:
=
{
[ATTRELSCHEMA_ARG] = {"schemaname", TEXTOID},
[ATTRELNAME_ARG] = {"relname", TEXTOID},
[ATTNAME_ARG] = {"attname", TEXTOID},
[ATTNUM_ARG] = {"attnum", INT2OID},
[INHERITED_ARG] = {"inherited", BOOLOID},
[NULL_FRAC_ARG] = {"null_frac", FLOAT4OID},
[AVG_WIDTH_ARG] = {"avg_width", INT4OID},
[N_DISTINCT_ARG] = {"n_distinct", FLOAT4OID},
[MOST_COMMON_VALS_ARG] = {"most_common_vals", TEXTOID},
[MOST_COMMON_FREQS_ARG] = {"most_common_freqs", FLOAT4ARRAYOID},
[HISTOGRAM_BOUNDS_ARG] = {"histogram_bounds", TEXTOID},
[CORRELATION_ARG] = {"correlation", FLOAT4OID},
[MOST_COMMON_ELEMS_ARG] = {"most_common_elems", TEXTOID},
[MOST_COMMON_ELEM_FREQS_ARG] = {"most_common_elem_freqs", FLOAT4ARRAYOID},
[ELEM_COUNT_HISTOGRAM_ARG] = {"elem_count_histogram", FLOAT4ARRAYOID},
[RANGE_LENGTH_HISTOGRAM_ARG] = {"range_length_histogram", TEXTOID},
[RANGE_EMPTY_FRAC_ARG] = {"range_empty_frac", FLOAT4OID},
[RANGE_BOUNDS_HISTOGRAM_ARG] = {"range_bounds_histogram", TEXTOID},
}

Definition at line 61 of file attribute_stats.c.

Referenced by attribute_statistics_update(), and pg_restore_attribute_stats().

◆ cleararginfo

struct StatsArgInfo cleararginfo[]
static
Initial value:
=
{
[C_ATTRELSCHEMA_ARG] = {"relation", TEXTOID},
[C_ATTRELNAME_ARG] = {"relation", TEXTOID},
[C_ATTNAME_ARG] = {"attname", TEXTOID},
[C_INHERITED_ARG] = {"inherited", BOOLOID},
}

Definition at line 98 of file attribute_stats.c.

Referenced by pg_clear_attribute_stats().