53 int bv = *(
const int16 *)
b;
75 bool nulls[Natts_pg_statistic_ext];
88 bool build_dependencies;
90 bool build_expressions;
91 bool requested_type =
false;
106 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
107 errmsg(
"only a single relation is allowed in CREATE STATISTICS")));
109 foreach(cell,
stmt->relations)
115 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
116 errmsg(
"only a single relation is allowed in CREATE STATISTICS")));
128 if (rel->
rd_rel->relkind != RELKIND_RELATION &&
129 rel->
rd_rel->relkind != RELKIND_MATVIEW &&
130 rel->
rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
131 rel->
rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
133 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
134 errmsg(
"cannot define statistics for relation \"%s\"",
152 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
153 errmsg(
"permission denied: \"%s\" is a system catalog",
201 if (
stmt->if_not_exists)
209 errmsg(
"statistics object \"%s\" already exists, skipping",
217 errmsg(
"statistics object \"%s\" already exists", namestr)));
227 (
errcode(ERRCODE_TOO_MANY_COLUMNS),
228 errmsg(
"cannot have more than %d columns in statistics",
244 foreach(cell,
stmt->exprs)
260 (
errcode(ERRCODE_UNDEFINED_COLUMN),
261 errmsg(
"column \"%s\" does not exist",
266 if (attForm->attnum <= 0)
268 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
269 errmsg(
"statistics creation on system columns is not supported")));
272 if (attForm->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
274 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
275 errmsg(
"statistics creation on virtual generated columns is not supported")));
281 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
282 errmsg(
"column \"%s\" cannot be used in statistics because its type %s has no default btree operator class",
285 attnums[nattnums] = attForm->attnum;
297 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
298 errmsg(
"statistics creation on system columns is not supported")));
303 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
304 errmsg(
"statistics creation on virtual generated columns is not supported")));
310 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
311 errmsg(
"column \"%s\" cannot be used in statistics because its type %s has no default btree operator class",
337 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
338 errmsg(
"statistics creation on system columns is not supported")));
343 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
344 errmsg(
"statistics creation on virtual generated columns is not supported")));
360 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
361 errmsg(
"expression cannot be used in multivariate statistics because its type %s has no default btree operator class",
365 stxexprs =
lappend(stxexprs, expr);
381 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
382 errmsg(
"when building statistics on a single expression, statistics kinds may not be specified")));
386 build_ndistinct =
false;
387 build_dependencies =
false;
389 foreach(cell,
stmt->stat_types)
393 if (strcmp(
type,
"ndistinct") == 0)
395 build_ndistinct =
true;
396 requested_type =
true;
398 else if (strcmp(
type,
"dependencies") == 0)
400 build_dependencies =
true;
401 requested_type =
true;
403 else if (strcmp(
type,
"mcv") == 0)
406 requested_type =
true;
410 (
errcode(ERRCODE_SYNTAX_ERROR),
411 errmsg(
"unrecognized statistics kind \"%s\"",
419 if ((!requested_type) && (numcols >= 2))
421 build_ndistinct =
true;
422 build_dependencies =
true;
431 build_expressions = (stxexprs !=
NIL);
439 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
440 errmsg(
"extended statistics require at least 2 columns")));
453 for (
i = 1;
i < nattnums;
i++)
455 if (attnums[
i] == attnums[
i - 1])
457 (
errcode(ERRCODE_DUPLICATE_COLUMN),
458 errmsg(
"duplicate column name in statistics definition")));
474 foreach(cell, stxexprs)
479 foreach(cell2, stxexprs)
483 if (
equal(expr1, expr2))
492 (
errcode(ERRCODE_DUPLICATE_COLUMN),
493 errmsg(
"duplicate expression in statistics definition")));
503 if (build_dependencies)
507 if (build_expressions)
522 exprsDatum = (
Datum) 0;
530 memset(nulls,
false,
sizeof(nulls));
533 Anum_pg_statistic_ext_oid);
540 nulls[Anum_pg_statistic_ext_stxstattarget - 1] =
true;
543 values[Anum_pg_statistic_ext_stxexprs - 1] = exprsDatum;
544 if (exprsDatum == (
Datum) 0)
545 nulls[Anum_pg_statistic_ext_stxexprs - 1] =
true;
576 for (
i = 0;
i < nattnums;
i++)
627 if (
stmt->stxcomment != NULL)
645 Datum repl_val[Natts_pg_statistic_ext];
646 bool repl_null[Natts_pg_statistic_ext];
647 bool repl_repl[Natts_pg_statistic_ext];
650 bool newtarget_default;
656 newtarget_default =
false;
659 newtarget_default =
true;
661 if (!newtarget_default)
667 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
668 errmsg(
"statistics target %d is too low",
675 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
676 errmsg(
"lowering statistics target to %d",
700 (
errmsg(
"statistics object \"%s.%s\" does not exist, skipping",
701 schemaname, statname)));
704 (
errmsg(
"statistics object \"%s\" does not exist, skipping",
715 elog(
ERROR,
"cache lookup failed for extended statistics object %u", stxoid);
723 memset(repl_val, 0,
sizeof(repl_val));
724 memset(repl_null,
false,
sizeof(repl_null));
725 memset(repl_repl,
false,
sizeof(repl_repl));
728 repl_repl[Anum_pg_statistic_ext_stxstattarget - 1] =
true;
729 if (!newtarget_default)
730 repl_val[Anum_pg_statistic_ext_stxstattarget - 1] =
Int16GetDatum(newtarget);
732 repl_null[Anum_pg_statistic_ext_stxstattarget - 1] =
true;
735 repl_val, repl_null, repl_repl);
804 elog(
ERROR,
"cache lookup failed for statistics object %u", statsOid);
807 relid = statext->stxrelid;
853 char *stxname = NULL;
865 existingstats =
GetSysCacheOid2(STATEXTNAMENSP, Anum_pg_statistic_ext_oid,
926 buflen += strlen(
buf + buflen);
949 elog(
ERROR,
"cache lookup failed for statistics object %u", statId);
952 Assert(stx->oid == statId);
954 result = stx->stxrelid;
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
int bms_next_member(const Bitmapset *a, int prevbit)
static Datum values[MAXATTR]
#define CStringGetTextDatum(s)
#define OidIsValid(objectId)
bool IsSystemRelation(Relation relation)
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool reverse_self)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool equal(const void *a, const void *b)
bool allowSystemTableMods
Assert(PointerIsAligned(start, uint64))
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
void heap_freetuple(HeapTuple htup)
#define HeapTupleIsValid(tuple)
static void * GETSTRUCT(const HeapTupleData *tuple)
char * makeObjectName(const char *name1, const char *name2, const char *label)
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
int2vector * buildint2vector(const int16 *int2s, int n)
void CacheInvalidateRelcache(Relation relation)
void CacheInvalidateRelcacheByRelid(Oid relid)
if(TABLE==NULL||TABLE_index==NULL)
List * lappend(List *list, void *datum)
#define ShareUpdateExclusiveLock
char get_attgenerated(Oid relid, AttrNumber attnum)
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
char * get_namespace_name(Oid nspid)
char * pstrdup(const char *in)
void pfree(void *pointer)
void namestrcpy(Name name, const char *str)
char * NameListToString(const List *names)
Oid QualifiedNameGetCreationNamespace(const List *names, char **objname_p)
Oid get_statistics_object_oid(List *names, bool missing_ok)
void DeconstructQualifiedName(const List *names, char **nspname_p, char **objname_p)
Oid exprType(const Node *expr)
#define IsA(nodeptr, _type_)
#define InvokeObjectPostCreateHook(classId, objectId, subId)
#define InvokeObjectPostAlterHook(classId, objectId, subId)
ObjectType get_relkind_objtype(char relkind)
const ObjectAddress InvalidObjectAddress
#define ObjectAddressSet(addr, class_id, object_id)
#define ObjectAddressSubSet(addr, class_id, object_id, object_sub_id)
char * nodeToString(const void *obj)
FormData_pg_attribute * Form_pg_attribute
int errdetail_relkind_not_supported(char relkind)
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
#define lfirst_node(type, lc)
static int list_length(const List *l)
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
FormData_pg_statistic_ext * Form_pg_statistic_ext
#define qsort(a, b, c, d)
size_t strlcpy(char *dst, const char *src, size_t siz)
static Datum PointerGetDatum(const void *X)
static Datum Int16GetDatum(int16 X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static Datum NameGetDatum(const NameData *X)
static Datum CStringGetDatum(const char *X)
static Datum CharGetDatum(char X)
#define RelationGetRelid(relation)
#define RelationGetDescr(relation)
#define RelationGetRelationName(relation)
#define RelationGetNamespace(relation)
void relation_close(Relation relation, LOCKMODE lockmode)
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
#define STATS_MAX_DIMENSIONS
ObjectAddress CreateStatistics(CreateStatsStmt *stmt, bool check_rights)
ObjectAddress AlterStatistics(AlterStatsStmt *stmt)
static char * ChooseExtendedStatisticNameAddition(List *exprs)
void RemoveStatisticsDataById(Oid statsOid, bool inh)
static char * ChooseExtendedStatisticName(const char *name1, const char *name2, const char *label, Oid namespaceid)
void RemoveStatisticsById(Oid statsOid)
static int compare_int16(const void *a, const void *b)
Oid StatisticsGetRelation(Oid statId, bool missing_ok)
#define ERRCODE_DUPLICATE_OBJECT
#define FirstLowInvalidHeapAttributeNumber
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
HeapTuple SearchSysCacheAttName(Oid relid, const char *attname)
#define SearchSysCacheExists2(cacheId, key1, key2)
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
#define MAX_STATISTICS_TARGET
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)