PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 bool attribute_statistics_update_internal (Oid reloid, const char *attname, AttrNumber attnum, bool inherited, 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)
 
bool import_attribute_statistics (Relation rel, AttrNumber attnum, bool inherited, const NullableDatum *version, const NullableDatum *null_frac, const NullableDatum *avg_width, const NullableDatum *n_distinct, const NullableDatum *most_common_vals, const NullableDatum *most_common_freqs, const NullableDatum *histogram_bounds, const NullableDatum *correlation, const NullableDatum *most_common_elems, const NullableDatum *most_common_elem_freqs, const NullableDatum *elem_count_histogram, const NullableDatum *range_length_histogram, const NullableDatum *range_empty_frac, const NullableDatum *range_bounds_histogram)
 
bool delete_attribute_statistics (Relation rel, AttrNumber attnum, bool inherited)
 

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 134 of file attribute_stats.c.

135{
136 char *nspname;
137 char *relname;
138 Oid reloid;
139 char *attname;
141 bool inherited;
143
146
149
150 if (RecoveryInProgress())
153 errmsg("recovery is in progress"),
154 errhint("Statistics cannot be modified during recovery.")));
155
156 /* lock before looking up attribute */
157 reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
160
161 /* user can specify either attname or attnum, but not both */
163 {
167 errmsg("cannot specify both \"%s\" and \"%s\"", "attname", "attnum")));
169 attnum = get_attnum(reloid, attname);
170 /* note that this test covers attisdropped cases too: */
174 errmsg("column \"%s\" of relation \"%s\" does not exist",
175 attname, relname)));
176 }
177 else if (!PG_ARGISNULL(ATTNUM_ARG))
178 {
180 attname = get_attname(reloid, attnum, true);
181 /* annoyingly, get_attname doesn't check attisdropped */
182 if (attname == NULL ||
186 errmsg("column %d of relation \"%s\" does not exist",
187 attnum, relname)));
188 }
189 else
190 {
193 errmsg("must specify either \"%s\" or \"%s\"", "attname", "attnum")));
194 attname = NULL; /* keep compiler quiet */
195 attnum = 0;
196 }
197
198 if (attnum < 0)
201 errmsg("cannot modify statistics on system column \"%s\"",
202 attname)));
203
206
208 inherited, fcinfo);
209}
int16 AttrNumber
Definition attnum.h:21
#define InvalidAttrNumber
Definition attnum.h:23
static struct StatsArgInfo attarginfo[]
static bool attribute_statistics_update_internal(Oid reloid, const char *attname, AttrNumber attnum, bool inherited, FunctionCallInfo fcinfo)
#define TextDatumGetCString(d)
Definition builtins.h:99
int errcode(int sqlerrcode)
Definition elog.c:875
int errhint(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
#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
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
AttrNumber get_attnum(Oid relid, const char *attname)
Definition lsyscache.c:1084
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition lsyscache.c:1053
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:442
static char * errmsg
NameData attname
int16 attnum
NameData relname
Definition pg_class.h:40
#define InvalidOid
unsigned int Oid
static int fb(int x)
void RangeVarCallbackForStats(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition stat_utils.c:143
void stats_check_required_arg(FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum)
Definition stat_utils.c:52
bool SearchSysCacheExistsAttName(Oid relid, const char *attname)
Definition syscache.c:518
bool RecoveryInProgress(void)
Definition xlog.c:6835

References attarginfo, attname, ATTNAME_ARG, attnum, ATTNUM_ARG, ATTRELNAME_ARG, ATTRELSCHEMA_ARG, attribute_statistics_update_internal(), ereport, errcode(), errhint(), errmsg, ERROR, fb(), get_attname(), get_attnum(), INHERITED_ARG, InvalidAttrNumber, InvalidOid, makeRangeVar(), PG_ARGISNULL, PG_GETARG_BOOL, PG_GETARG_DATUM, PG_GETARG_INT16, RangeVarCallbackForStats(), RangeVarGetRelidExtended(), RecoveryInProgress(), relname, SearchSysCacheExistsAttName(), ShareUpdateExclusiveLock, stats_check_required_arg(), and TextDatumGetCString.

Referenced by pg_restore_attribute_stats().

◆ attribute_statistics_update_internal()

static bool attribute_statistics_update_internal ( Oid  reloid,
const char attname,
AttrNumber  attnum,
bool  inherited,
FunctionCallInfo  fcinfo 
)
static

Definition at line 215 of file attribute_stats.c.

218{
221
222 Oid atttypid = InvalidOid;
223 int32 atttypmod;
224 char atttyptype;
226 Oid eq_opr = InvalidOid;
227 Oid lt_opr = InvalidOid;
228
231
233
244
246 bool nulls[Natts_pg_statistic] = {0};
247 bool replaces[Natts_pg_statistic] = {0};
248
249 bool result = true;
250
251 /*
252 * Check argument sanity. If some arguments are unusable, emit a WARNING
253 * and set the corresponding argument to NULL in fcinfo.
254 */
255
257 {
258 do_mcv = false;
259 result = false;
260 }
261
263 {
264 do_mcelem = false;
265 result = false;
266 }
268 {
269 do_dechist = false;
270 result = false;
271 }
272
273 if (!stats_check_arg_pair(fcinfo, attarginfo,
275 {
276 do_mcv = false;
277 result = false;
278 }
279
280 if (!stats_check_arg_pair(fcinfo, attarginfo,
283 {
284 do_mcelem = false;
285 result = false;
286 }
287
288 if (!stats_check_arg_pair(fcinfo, attarginfo,
291 {
293 result = false;
294 }
295
296 /* derive information from attribute */
297 statatt_get_type(reloid, attnum,
298 &atttypid, &atttypmod,
300 &eq_opr, &lt_opr);
301
302 /* if needed, derive element type */
303 if (do_mcelem || do_dechist)
304 {
305 if (!statatt_get_elem_type(atttypid, atttyptype,
307 {
309 (errmsg("could not determine element type of column \"%s\"", attname),
310 errdetail("Cannot set %s or %s.",
311 "STATISTIC_KIND_MCELEM", "STATISTIC_KIND_DECHIST")));
314
315 do_mcelem = false;
316 do_dechist = false;
317 result = false;
318 }
319 }
320
321 /* histogram and correlation require less-than operator */
322 if ((do_histogram || do_correlation) && !OidIsValid(lt_opr))
323 {
326 errmsg("could not determine less-than operator for column \"%s\"", attname),
327 errdetail("Cannot set %s or %s.",
328 "STATISTIC_KIND_HISTOGRAM", "STATISTIC_KIND_CORRELATION")));
329
330 do_histogram = false;
331 do_correlation = false;
332 result = false;
333 }
334
335 /* only range types can have range stats */
338 {
341 errmsg("column \"%s\" is not a range type", attname),
342 errdetail("Cannot set %s or %s.",
343 "STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM", "STATISTIC_KIND_BOUNDS_HISTOGRAM")));
344
345 do_bounds_histogram = false;
347 result = false;
348 }
349
351
353
355
356 /* initialize from existing tuple if exists */
359 else
361 replaces);
362
363 /* if specified, set to argument values */
365 {
368 }
370 {
373 }
375 {
378 }
379
380 /* STATISTIC_KIND_MCV */
381 if (do_mcv)
382 {
383 bool converted;
385 Datum stavalues = statatt_build_stavalues("most_common_vals",
388 atttypid, atttypmod,
389 &converted);
390
391 if (converted)
392 {
395 int nvals = ARR_DIMS(vals_arr)[0];
396 int nnums = ARR_DIMS(nums_arr)[0];
397
398 if (nvals != nnums)
399 {
402 errmsg("could not parse \"%s\": incorrect number of elements (same as \"%s\" required)",
403 "most_common_vals",
404 "most_common_freqs")));
405 result = false;
406 }
407 else
408 {
411 eq_opr, atttypcoll,
412 stanumbers, false, stavalues, false);
413 }
414 }
415 else
416 result = false;
417 }
418
419 /* STATISTIC_KIND_HISTOGRAM */
420 if (do_histogram)
421 {
422 Datum stavalues;
423 bool converted = false;
424
425 stavalues = statatt_build_stavalues("histogram_bounds",
428 atttypid, atttypmod,
429 &converted);
430
431 if (converted)
432 {
435 lt_opr, atttypcoll,
436 0, true, stavalues, false);
437 }
438 else
439 result = false;
440 }
441
442 /* STATISTIC_KIND_CORRELATION */
443 if (do_correlation)
444 {
447 Datum stanumbers = PointerGetDatum(arry);
448
451 lt_opr, atttypcoll,
452 stanumbers, false, 0, true);
453 }
454
455 /* STATISTIC_KIND_MCELEM */
456 if (do_mcelem)
457 {
459 bool converted = false;
460 Datum stavalues;
461
462 stavalues = statatt_build_stavalues("most_common_elems",
465 elemtypid, atttypmod,
466 &converted);
467
468 if (converted)
469 {
473 stanumbers, false, stavalues, false);
474 }
475 else
476 result = false;
477 }
478
479 /* STATISTIC_KIND_DECHIST */
480 if (do_dechist)
481 {
483
487 stanumbers, false, 0, true);
488 }
489
490 /*
491 * STATISTIC_KIND_BOUNDS_HISTOGRAM
492 *
493 * This stakind appears before STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM even
494 * though it is numerically greater, and all other stakinds appear in
495 * numerical order. We duplicate this quirk for consistency.
496 */
498 {
499 bool converted = false;
500 Datum stavalues;
501
502 stavalues = statatt_build_stavalues("range_bounds_histogram",
505 atttypid, atttypmod,
506 &converted);
507
508 if (converted &&
510 {
514 0, true, stavalues, false);
515 }
516 else
517 result = false;
518 }
519
520 /* STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM */
522 {
523 /* The anyarray is always a float8[] for this stakind */
526 Datum stanumbers = PointerGetDatum(arry);
527
528 bool converted = false;
529 Datum stavalues;
530
531 stavalues = statatt_build_stavalues("range_length_histogram",
534 FLOAT8OID, 0, &converted);
535
536 if (converted)
537 {
541 stanumbers, false, stavalues, false);
542 }
543 else
544 result = false;
545 }
546
548
552
553 return result;
554}
#define DatumGetArrayTypeP(X)
Definition array.h:261
#define ARR_DIMS(a)
Definition array.h:294
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
static void upsert_pg_statistic(Relation starel, HeapTuple oldtup, const Datum *values, const bool *nulls, const bool *replaces)
static Datum values[MAXATTR]
Definition bootstrap.c:190
int32_t int32
Definition c.h:679
#define OidIsValid(objectId)
Definition c.h:917
uint32 result
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define WARNING
Definition elog.h:37
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition fmgr.c:129
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition heaptuple.c:1254
#define HeapTupleIsValid(tuple)
Definition htup.h:78
#define RowExclusiveLock
Definition lockdefs.h:38
static Datum Int16GetDatum(int16 X)
Definition postgres.h:172
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
#define PointerGetDatum(X)
Definition postgres.h:354
#define RelationGetDescr(relation)
Definition rel.h:542
bool statatt_get_elem_type(Oid atttypid, char atttyptype, Oid *elemtypid, Oid *elem_eq_opr)
Definition stat_utils.c:524
Datum statatt_build_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid typid, int32 typmod, bool *ok)
Definition stat_utils.c:568
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:71
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:438
bool statatt_check_bounds_histogram(Datum arrayval)
Definition stat_utils.c:753
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:112
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache3(SysCacheIdentifier cacheId, Datum key1, Datum key2, Datum key3)
Definition syscache.c:241
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40

References ARR_DIMS, attarginfo, attname, attnum, AVG_WIDTH_ARG, BoolGetDatum(), construct_array_builtin(), CORRELATION_ARG, DatumGetArrayTypeP, ELEM_COUNT_HISTOGRAM_ARG, ereport, errcode(), errdetail(), errmsg, fb(), fmgr_info(), heap_deform_tuple(), HeapTupleIsValid, HISTOGRAM_BOUNDS_ARG, Int16GetDatum(), InvalidOid, 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_DATUM, PointerGetDatum, RANGE_BOUNDS_HISTOGRAM_ARG, RANGE_EMPTY_FRAC_ARG, RANGE_LENGTH_HISTOGRAM_ARG, RelationGetDescr, ReleaseSysCache(), result, RowExclusiveLock, SearchSysCache3(), statatt_build_stavalues(), statatt_check_bounds_histogram(), statatt_get_elem_type(), statatt_get_type(), statatt_init_empty_tuple(), statatt_set_slot(), stats_check_arg_array(), stats_check_arg_pair(), table_close(), table_open(), upsert_pg_statistic(), values, and WARNING.

Referenced by attribute_statistics_update(), and import_attribute_statistics().

◆ delete_attribute_statistics()

bool delete_attribute_statistics ( Relation  rel,
AttrNumber  attnum,
bool  inherited 
)

Definition at line 798 of file attribute_stats.c.

799{
801}
static bool delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit)
#define RelationGetRelid(relation)
Definition rel.h:516

References attnum, delete_pg_statistic(), fb(), and RelationGetRelid.

Referenced by import_fetched_statistics().

◆ delete_pg_statistic()

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

Definition at line 586 of file attribute_stats.c.

587{
590 bool result = false;
591
592 /* Is there already a pg_statistic tuple for this attribute? */
594 ObjectIdGetDatum(reloid),
597
599 {
600 CatalogTupleDelete(sd, &oldtup->t_self);
602 result = true;
603 }
604
606
608
609 return result;
610}
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition indexing.c:365
void CommandCounterIncrement(void)
Definition xact.c:1130

References attnum, BoolGetDatum(), CatalogTupleDelete(), CommandCounterIncrement(), fb(), HeapTupleIsValid, Int16GetDatum(), ObjectIdGetDatum(), ReleaseSysCache(), result, RowExclusiveLock, SearchSysCache3(), table_close(), and table_open().

Referenced by delete_attribute_statistics(), and pg_clear_attribute_stats().

◆ import_attribute_statistics()

bool import_attribute_statistics ( Relation  rel,
AttrNumber  attnum,
bool  inherited,
const NullableDatum version,
const NullableDatum null_frac,
const NullableDatum avg_width,
const NullableDatum n_distinct,
const NullableDatum most_common_vals,
const NullableDatum most_common_freqs,
const NullableDatum histogram_bounds,
const NullableDatum correlation,
const NullableDatum most_common_elems,
const NullableDatum most_common_elem_freqs,
const NullableDatum elem_count_histogram,
const NullableDatum range_length_histogram,
const NullableDatum range_empty_frac,
const NullableDatum range_bounds_histogram 
)

Definition at line 718 of file attribute_stats.c.

733{
735 Oid reloid = RelationGetRelid(rel);
736 char *relname = RelationGetRelationName(rel);
737 char *attname = get_attname(reloid, attnum, true);
738
741 Assert(n_distinct);
752
753 /* annoyingly, get_attname doesn't check attisdropped */
754 if (attname == NULL ||
758 errmsg("column %d of relation \"%s\" does not exist",
759 attnum, relname)));
760
763
764 newfcinfo->args[ATTRELSCHEMA_ARG].value =
766 newfcinfo->args[ATTRELSCHEMA_ARG].isnull = false;
768 newfcinfo->args[ATTRELNAME_ARG].isnull = false;
770 newfcinfo->args[ATTNAME_ARG].isnull = false;
772 newfcinfo->args[ATTNUM_ARG].isnull = false;
774 newfcinfo->args[INHERITED_ARG].isnull = false;
775
778 newfcinfo->args[N_DISTINCT_ARG] = *n_distinct;
789
792}
#define CStringGetTextDatum(s)
Definition builtins.h:98
#define Assert(condition)
Definition c.h:1002
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition fmgr.h:150
#define LOCAL_FCINFO(name, nargs)
Definition fmgr.h:110
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3682
#define RelationGetRelationName(relation)
Definition rel.h:550
#define RelationGetNamespace(relation)
Definition rel.h:557

References Assert, attname, ATTNAME_ARG, attnum, ATTNUM_ARG, ATTRELNAME_ARG, ATTRELSCHEMA_ARG, attribute_statistics_update_internal(), AVG_WIDTH_ARG, BoolGetDatum(), CORRELATION_ARG, CStringGetTextDatum, ELEM_COUNT_HISTOGRAM_ARG, ereport, errcode(), errmsg, ERROR, fb(), get_attname(), get_namespace_name(), HISTOGRAM_BOUNDS_ARG, INHERITED_ARG, InitFunctionCallInfoData, Int16GetDatum(), InvalidOid, LOCAL_FCINFO, MOST_COMMON_ELEM_FREQS_ARG, MOST_COMMON_ELEMS_ARG, MOST_COMMON_FREQS_ARG, MOST_COMMON_VALS_ARG, N_DISTINCT_ARG, NULL_FRAC_ARG, NUM_ATTRIBUTE_STATS_ARGS, RANGE_BOUNDS_HISTOGRAM_ARG, RANGE_EMPTY_FRAC_ARG, RANGE_LENGTH_HISTOGRAM_ARG, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, relname, and SearchSysCacheExistsAttName().

Referenced by import_fetched_statistics().

◆ pg_clear_attribute_stats()

Datum pg_clear_attribute_stats ( PG_FUNCTION_ARGS  )

Definition at line 616 of file attribute_stats.c.

617{
618 char *nspname;
619 char *relname;
620 Oid reloid;
621 char *attname;
623 bool inherited;
625
630
633
634 if (RecoveryInProgress())
637 errmsg("recovery is in progress"),
638 errhint("Statistics cannot be modified during recovery.")));
639
640 reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
643
645 attnum = get_attnum(reloid, attname);
646
647 if (attnum < 0)
650 errmsg("cannot clear statistics on system column \"%s\"",
651 attname)));
652
656 errmsg("column \"%s\" of relation \"%s\" does not exist",
657 attname, get_rel_name(reloid))));
658
660
663}
static struct StatsArgInfo cleararginfo[]
#define PG_RETURN_VOID()
Definition fmgr.h:350
char * get_rel_name(Oid relid)
Definition lsyscache.c:2242

References attname, attnum, C_ATTNAME_ARG, C_ATTRELNAME_ARG, C_ATTRELSCHEMA_ARG, C_INHERITED_ARG, cleararginfo, delete_pg_statistic(), ereport, errcode(), errhint(), errmsg, ERROR, fb(), 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 692 of file attribute_stats.c.

693{
695 bool result = true;
696
699
701 attarginfo))
702 result = false;
703
705 result = false;
706
708}
static bool attribute_statistics_update(FunctionCallInfo fcinfo)
#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:349

References attarginfo, attribute_statistics_update(), fb(), InitFunctionCallInfoData, InvalidOid, LOCAL_FCINFO, NUM_ATTRIBUTE_STATS_ARGS, PG_RETURN_BOOL, result, 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 560 of file attribute_stats.c.

562{
564
566 {
568 values, nulls, replaces);
570 }
571 else
572 {
575 }
576
578
580}
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1118
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1025
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1372
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(), fb(), heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, RelationGetDescr, and values.

Referenced by attribute_statistics_update_internal().

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.

62{
63 [ATTRELSCHEMA_ARG] = {"schemaname", TEXTOID},
64 [ATTRELNAME_ARG] = {"relname", TEXTOID},
65 [ATTNAME_ARG] = {"attname", TEXTOID},
66 [ATTNUM_ARG] = {"attnum", INT2OID},
67 [INHERITED_ARG] = {"inherited", BOOLOID},
68 [NULL_FRAC_ARG] = {"null_frac", FLOAT4OID},
69 [AVG_WIDTH_ARG] = {"avg_width", INT4OID},
70 [N_DISTINCT_ARG] = {"n_distinct", FLOAT4OID},
71 [MOST_COMMON_VALS_ARG] = {"most_common_vals", TEXTOID},
72 [MOST_COMMON_FREQS_ARG] = {"most_common_freqs", FLOAT4ARRAYOID},
73 [HISTOGRAM_BOUNDS_ARG] = {"histogram_bounds", TEXTOID},
74 [CORRELATION_ARG] = {"correlation", FLOAT4OID},
75 [MOST_COMMON_ELEMS_ARG] = {"most_common_elems", TEXTOID},
76 [MOST_COMMON_ELEM_FREQS_ARG] = {"most_common_elem_freqs", FLOAT4ARRAYOID},
77 [ELEM_COUNT_HISTOGRAM_ARG] = {"elem_count_histogram", FLOAT4ARRAYOID},
78 [RANGE_LENGTH_HISTOGRAM_ARG] = {"range_length_histogram", TEXTOID},
79 [RANGE_EMPTY_FRAC_ARG] = {"range_empty_frac", FLOAT4OID},
80 [RANGE_BOUNDS_HISTOGRAM_ARG] = {"range_bounds_histogram", TEXTOID},
82};

Referenced by attribute_statistics_update(), attribute_statistics_update_internal(), and pg_restore_attribute_stats().

◆ cleararginfo

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

Definition at line 98 of file attribute_stats.c.

99{
100 [C_ATTRELSCHEMA_ARG] = {"schemaname", TEXTOID},
101 [C_ATTRELNAME_ARG] = {"relname", TEXTOID},
102 [C_ATTNAME_ARG] = {"attname", TEXTOID},
103 [C_INHERITED_ARG] = {"inherited", BOOLOID},
105};

Referenced by pg_clear_attribute_stats().