34 #include "utils/fmgroids.h" 52 int bv = *(
const int16 *) b;
73 bool nulls[Natts_pg_statistic_ext];
74 Datum datavalues[Natts_pg_statistic_ext_data];
75 bool datanulls[Natts_pg_statistic_ext_data];
87 bool build_dependencies;
89 bool requested_type =
false;
103 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
104 errmsg(
"only a single relation is allowed in CREATE STATISTICS")));
112 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
113 errmsg(
"only a single relation is allowed in CREATE STATISTICS")));
125 if (rel->
rd_rel->relkind != RELKIND_RELATION &&
126 rel->
rd_rel->relkind != RELKIND_MATVIEW &&
127 rel->
rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
128 rel->
rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
130 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
131 errmsg(
"relation \"%s\" is not a table, foreign table, or materialized view",
142 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
143 errmsg(
"permission denied: \"%s\" is a system catalog",
181 errmsg(
"statistics object \"%s\" already exists, skipping",
189 errmsg(
"statistics object \"%s\" already exists", namestr)));
199 foreach(cell, stmt->
exprs)
210 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
211 errmsg(
"only simple column references are allowed in CREATE STATISTICS")));
216 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
217 errmsg(
"only simple column references are allowed in CREATE STATISTICS")));
223 (
errcode(ERRCODE_UNDEFINED_COLUMN),
224 errmsg(
"column \"%s\" does not exist",
229 if (attForm->attnum <= 0)
231 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
232 errmsg(
"statistics creation on system columns is not supported")));
238 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
239 errmsg(
"column \"%s\" cannot be used in statistics because its type %s has no default btree operator class",
245 (
errcode(ERRCODE_TOO_MANY_COLUMNS),
246 errmsg(
"cannot have more than %d columns in statistics",
249 attnums[numcols] = attForm->attnum;
260 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
261 errmsg(
"extended statistics require at least 2 columns")));
274 for (i = 1; i < numcols; i++)
276 if (attnums[i] == attnums[i - 1])
278 (
errcode(ERRCODE_DUPLICATE_COLUMN),
279 errmsg(
"duplicate column name in statistics definition")));
288 build_ndistinct =
false;
289 build_dependencies =
false;
295 if (strcmp(type,
"ndistinct") == 0)
297 build_ndistinct =
true;
298 requested_type =
true;
300 else if (strcmp(type,
"dependencies") == 0)
302 build_dependencies =
true;
303 requested_type =
true;
305 else if (strcmp(type,
"mcv") == 0)
308 requested_type =
true;
312 (
errcode(ERRCODE_SYNTAX_ERROR),
313 errmsg(
"unrecognized statistics kind \"%s\"",
319 build_ndistinct =
true;
320 build_dependencies =
true;
328 if (build_dependencies)
333 stxkind =
construct_array(types, ntypes, CHAROID, 1,
true, TYPALIGN_CHAR);
340 memset(values, 0,
sizeof(values));
341 memset(nulls,
false,
sizeof(nulls));
344 Anum_pg_statistic_ext_oid);
347 values[Anum_pg_statistic_ext_stxname - 1] =
NameGetDatum(&stxname);
348 values[Anum_pg_statistic_ext_stxnamespace - 1] =
ObjectIdGetDatum(namespaceId);
349 values[Anum_pg_statistic_ext_stxstattarget - 1] =
Int32GetDatum(-1);
367 memset(datavalues, 0,
sizeof(datavalues));
368 memset(datanulls,
false,
sizeof(datanulls));
370 datavalues[Anum_pg_statistic_ext_data_stxoid - 1] =
ObjectIdGetDatum(statoid);
373 datanulls[Anum_pg_statistic_ext_data_stxdndistinct - 1] =
true;
374 datanulls[Anum_pg_statistic_ext_data_stxddependencies - 1] =
true;
375 datanulls[Anum_pg_statistic_ext_data_stxdmcv - 1] =
true;
399 for (i = 0; i < numcols; i++)
440 Datum repl_val[Natts_pg_statistic_ext];
441 bool repl_null[Natts_pg_statistic_ext];
442 bool repl_repl[Natts_pg_statistic_ext];
450 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
451 errmsg(
"statistics target %d is too low",
454 else if (newtarget > 10000)
458 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
459 errmsg(
"lowering statistics target to %d",
482 (
errmsg(
"statistics object \"%s.%s\" does not exist, skipping",
483 schemaname, statname)));
486 (
errmsg(
"statistics object \"%s\" does not exist, skipping",
503 memset(repl_val, 0,
sizeof(repl_val));
504 memset(repl_null,
false,
sizeof(repl_null));
505 memset(repl_repl,
false,
sizeof(repl_repl));
508 repl_repl[Anum_pg_statistic_ext_stxstattarget - 1] =
true;
509 repl_val[Anum_pg_statistic_ext_stxstattarget - 1] =
Int32GetDatum(newtarget);
512 repl_val, repl_null, repl_repl);
554 elog(
ERROR,
"cache lookup failed for statistics data %u", statsOid);
571 elog(
ERROR,
"cache lookup failed for statistics object %u", statsOid);
574 relid = statext->stxrelid;
612 Oid oldColumnType,
Oid newColumnType)
620 bool nulls[Natts_pg_statistic_ext_data];
621 bool replaces[Natts_pg_statistic_ext_data];
625 elog(
ERROR,
"cache lookup failed for statistics object %u", statsOid);
642 memset(nulls, 0, Natts_pg_statistic_ext_data *
sizeof(
bool));
643 memset(replaces, 0, Natts_pg_statistic_ext_data *
sizeof(
bool));
644 memset(values, 0, Natts_pg_statistic_ext_data *
sizeof(
Datum));
646 replaces[Anum_pg_statistic_ext_data_stxdmcv - 1] =
true;
647 nulls[Anum_pg_statistic_ext_data_stxdmcv - 1] =
true;
687 char *stxname = NULL;
691 strlcpy(modlabel, label,
sizeof(modlabel));
707 snprintf(modlabel,
sizeof(modlabel),
"%s%d", label, ++pass);
751 buflen += strlen(buf + buflen);
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
#define IsA(nodeptr, _type_)
void table_close(Relation relation, LOCKMODE lockmode)
static char * ChooseExtendedStatisticName(const char *name1, const char *name2, const char *label, Oid namespaceid)
#define InvokeObjectPostCreateHook(classId, objectId, subId)
void RemoveStatisticsById(Oid statsOid)
bool IsSystemRelation(Relation relation)
#define RelationGetDescr(relation)
void namestrcpy(Name name, const char *str)
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
#define PointerGetDatum(X)
#define StatisticExtOidIndexId
char * pstrdup(const char *in)
int2vector * buildint2vector(const int16 *int2s, int n)
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
int errcode(int sqlerrcode)
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
bool statext_is_kind_built(HeapTuple htup, char type)
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
void DeconstructQualifiedName(List *names, char **nspname_p, char **objname_p)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
void heap_freetuple(HeapTuple htup)
#define OidIsValid(objectId)
char * makeObjectName(const char *name1, const char *name2, const char *label)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
ObjectAddress AlterStatistics(AlterStatsStmt *stmt)
void pfree(void *pointer)
#define ObjectIdGetDatum(X)
Oid get_statistics_object_oid(List *names, bool missing_ok)
#define CStringGetDatum(X)
void CacheInvalidateRelcacheByRelid(Oid relid)
#define RelationGetRelationName(relation)
FormData_pg_attribute * Form_pg_attribute
static int compare_int16(const void *a, const void *b)
#define InvokeObjectPostAlterHook(classId, objectId, subId)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
char * NameListToString(List *names)
void UpdateStatisticsForTypeChange(Oid statsOid, Oid relationOid, int attnum, Oid oldColumnType, Oid newColumnType)
void ReleaseSysCache(HeapTuple tuple)
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
bool allowSystemTableMods
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
#define ereport(elevel,...)
size_t strlcpy(char *dst, const char *src, size_t siz)
#define ShareUpdateExclusiveLock
#define HeapTupleIsValid(tuple)
void relation_close(Relation relation, LOCKMODE lockmode)
#define Assert(condition)
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
static char * ChooseExtendedStatisticNameAddition(List *exprs)
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
static int list_length(const List *l)
#define SearchSysCacheExists2(cacheId, key1, key2)
HeapTuple SearchSysCacheAttName(Oid relid, const char *attname)
#define ObjectAddressSubSet(addr, class_id, object_id, object_sub_id)
#define ObjectAddressSet(addr, class_id, object_id)
void CacheInvalidateRelcache(Relation relation)
static Datum values[MAXATTR]
#define STATS_MAX_DIMENSIONS
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
ObjectType get_relkind_objtype(char relkind)
#define qsort(a, b, c, d)
Relation table_open(Oid relationId, LOCKMODE lockmode)
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
#define ERRCODE_DUPLICATE_OBJECT
#define RelationGetRelid(relation)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
FormData_pg_statistic_ext * Form_pg_statistic_ext
#define RelationGetNamespace(relation)
bool pg_statistics_object_ownercheck(Oid stat_oid, Oid roleid)
ObjectAddress CreateStatistics(CreateStatsStmt *stmt)