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