52 int bv = *(
const int16 *)
b;
74 bool nulls[Natts_pg_statistic_ext];
87 bool build_dependencies;
89 bool build_expressions;
90 bool requested_type =
false;
105 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
106 errmsg(
"only a single relation is allowed in CREATE STATISTICS")));
108 foreach(cell,
stmt->relations)
114 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
115 errmsg(
"only a single relation is allowed in CREATE STATISTICS")));
127 if (rel->
rd_rel->relkind != RELKIND_RELATION &&
128 rel->
rd_rel->relkind != RELKIND_MATVIEW &&
129 rel->
rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
130 rel->
rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
132 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
133 errmsg(
"cannot define statistics for relation \"%s\"",
145 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
146 errmsg(
"permission denied: \"%s\" is a system catalog",
179 if (
stmt->if_not_exists)
187 errmsg(
"statistics object \"%s\" already exists, skipping",
195 errmsg(
"statistics object \"%s\" already exists", namestr)));
205 (
errcode(ERRCODE_TOO_MANY_COLUMNS),
206 errmsg(
"cannot have more than %d columns in statistics",
222 foreach(cell,
stmt->exprs)
238 (
errcode(ERRCODE_UNDEFINED_COLUMN),
239 errmsg(
"column \"%s\" does not exist",
244 if (attForm->attnum <= 0)
246 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
247 errmsg(
"statistics creation on system columns is not supported")));
250 if (attForm->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
252 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
253 errmsg(
"statistics creation on virtual generated columns is not supported")));
259 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
260 errmsg(
"column \"%s\" cannot be used in statistics because its type %s has no default btree operator class",
263 attnums[nattnums] = attForm->attnum;
275 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
276 errmsg(
"statistics creation on system columns is not supported")));
281 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
282 errmsg(
"statistics creation on virtual generated columns is not supported")));
288 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
289 errmsg(
"column \"%s\" cannot be used in statistics because its type %s has no default btree operator class",
315 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
316 errmsg(
"statistics creation on system columns is not supported")));
321 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
322 errmsg(
"statistics creation on virtual generated columns is not supported")));
338 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
339 errmsg(
"expression cannot be used in multivariate statistics because its type %s has no default btree operator class",
343 stxexprs =
lappend(stxexprs, expr);
359 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
360 errmsg(
"when building statistics on a single expression, statistics kinds may not be specified")));
364 build_ndistinct =
false;
365 build_dependencies =
false;
367 foreach(cell,
stmt->stat_types)
371 if (strcmp(
type,
"ndistinct") == 0)
373 build_ndistinct =
true;
374 requested_type =
true;
376 else if (strcmp(
type,
"dependencies") == 0)
378 build_dependencies =
true;
379 requested_type =
true;
381 else if (strcmp(
type,
"mcv") == 0)
384 requested_type =
true;
388 (
errcode(ERRCODE_SYNTAX_ERROR),
389 errmsg(
"unrecognized statistics kind \"%s\"",
397 if ((!requested_type) && (numcols >= 2))
399 build_ndistinct =
true;
400 build_dependencies =
true;
409 build_expressions = (stxexprs !=
NIL);
417 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
418 errmsg(
"extended statistics require at least 2 columns")));
431 for (
i = 1;
i < nattnums;
i++)
433 if (attnums[
i] == attnums[
i - 1])
435 (
errcode(ERRCODE_DUPLICATE_COLUMN),
436 errmsg(
"duplicate column name in statistics definition")));
452 foreach(cell, stxexprs)
457 foreach(cell2, stxexprs)
461 if (
equal(expr1, expr2))
470 (
errcode(ERRCODE_DUPLICATE_COLUMN),
471 errmsg(
"duplicate expression in statistics definition")));
481 if (build_dependencies)
485 if (build_expressions)
500 exprsDatum = (
Datum) 0;
508 memset(nulls,
false,
sizeof(nulls));
511 Anum_pg_statistic_ext_oid);
518 nulls[Anum_pg_statistic_ext_stxstattarget - 1] =
true;
521 values[Anum_pg_statistic_ext_stxexprs - 1] = exprsDatum;
522 if (exprsDatum == (
Datum) 0)
523 nulls[Anum_pg_statistic_ext_stxexprs - 1] =
true;
554 for (
i = 0;
i < nattnums;
i++)
605 if (
stmt->stxcomment != NULL)
623 Datum repl_val[Natts_pg_statistic_ext];
624 bool repl_null[Natts_pg_statistic_ext];
625 bool repl_repl[Natts_pg_statistic_ext];
628 bool newtarget_default;
634 newtarget_default =
false;
637 newtarget_default =
true;
639 if (!newtarget_default)
645 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
646 errmsg(
"statistics target %d is too low",
653 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
654 errmsg(
"lowering statistics target to %d",
678 (
errmsg(
"statistics object \"%s.%s\" does not exist, skipping",
679 schemaname, statname)));
682 (
errmsg(
"statistics object \"%s\" does not exist, skipping",
693 elog(
ERROR,
"cache lookup failed for extended statistics object %u", stxoid);
701 memset(repl_val, 0,
sizeof(repl_val));
702 memset(repl_null,
false,
sizeof(repl_null));
703 memset(repl_repl,
false,
sizeof(repl_repl));
706 repl_repl[Anum_pg_statistic_ext_stxstattarget - 1] =
true;
707 if (!newtarget_default)
708 repl_val[Anum_pg_statistic_ext_stxstattarget - 1] =
Int16GetDatum(newtarget);
710 repl_null[Anum_pg_statistic_ext_stxstattarget - 1] =
true;
713 repl_val, repl_null, repl_repl);
782 elog(
ERROR,
"cache lookup failed for statistics object %u", statsOid);
785 relid = statext->stxrelid;
831 char *stxname = NULL;
843 existingstats =
GetSysCacheOid2(STATEXTNAMENSP, Anum_pg_statistic_ext_oid,
904 buflen += strlen(
buf + buflen);
927 elog(
ERROR,
"cache lookup failed for statistics object %u", statId);
930 Assert(stx->oid == statId);
932 result = stx->stxrelid;
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
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 Assert(condition)
#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
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, ItemPointer otid, HeapTuple tup)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
void CatalogTupleDelete(Relation heapRel, ItemPointer 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 * 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 AlterStatistics(AlterStatsStmt *stmt)
ObjectAddress CreateStatistics(CreateStatsStmt *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)