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")));
253 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
254 errmsg(
"column \"%s\" cannot be used in statistics because its type %s has no default btree operator class",
257 attnums[nattnums] = attForm->attnum;
269 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
270 errmsg(
"statistics creation on system columns is not supported")));
276 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
277 errmsg(
"column \"%s\" cannot be used in statistics because its type %s has no default btree operator class",
303 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
304 errmsg(
"statistics creation on system columns is not supported")));
320 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
321 errmsg(
"expression cannot be used in multivariate statistics because its type %s has no default btree operator class",
325 stxexprs =
lappend(stxexprs, expr);
341 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
342 errmsg(
"when building statistics on a single expression, statistics kinds may not be specified")));
346 build_ndistinct =
false;
347 build_dependencies =
false;
349 foreach(cell,
stmt->stat_types)
353 if (strcmp(
type,
"ndistinct") == 0)
355 build_ndistinct =
true;
356 requested_type =
true;
358 else if (strcmp(
type,
"dependencies") == 0)
360 build_dependencies =
true;
361 requested_type =
true;
363 else if (strcmp(
type,
"mcv") == 0)
366 requested_type =
true;
370 (
errcode(ERRCODE_SYNTAX_ERROR),
371 errmsg(
"unrecognized statistics kind \"%s\"",
379 if ((!requested_type) && (numcols >= 2))
381 build_ndistinct =
true;
382 build_dependencies =
true;
391 build_expressions = (stxexprs !=
NIL);
399 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
400 errmsg(
"extended statistics require at least 2 columns")));
413 for (
i = 1;
i < nattnums;
i++)
415 if (attnums[
i] == attnums[
i - 1])
417 (
errcode(ERRCODE_DUPLICATE_COLUMN),
418 errmsg(
"duplicate column name in statistics definition")));
434 foreach(cell, stxexprs)
439 foreach(cell2, stxexprs)
443 if (
equal(expr1, expr2))
452 (
errcode(ERRCODE_DUPLICATE_COLUMN),
453 errmsg(
"duplicate expression in statistics definition")));
463 if (build_dependencies)
467 if (build_expressions)
482 exprsDatum = (
Datum) 0;
490 memset(nulls,
false,
sizeof(nulls));
493 Anum_pg_statistic_ext_oid);
500 nulls[Anum_pg_statistic_ext_stxstattarget - 1] =
true;
503 values[Anum_pg_statistic_ext_stxexprs - 1] = exprsDatum;
504 if (exprsDatum == (
Datum) 0)
505 nulls[Anum_pg_statistic_ext_stxexprs - 1] =
true;
536 for (
i = 0;
i < nattnums;
i++)
587 if (
stmt->stxcomment != NULL)
605 Datum repl_val[Natts_pg_statistic_ext];
606 bool repl_null[Natts_pg_statistic_ext];
607 bool repl_repl[Natts_pg_statistic_ext];
610 bool newtarget_default;
616 newtarget_default =
false;
619 newtarget_default =
true;
621 if (!newtarget_default)
627 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
628 errmsg(
"statistics target %d is too low",
635 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
636 errmsg(
"lowering statistics target to %d",
660 (
errmsg(
"statistics object \"%s.%s\" does not exist, skipping",
661 schemaname, statname)));
664 (
errmsg(
"statistics object \"%s\" does not exist, skipping",
675 elog(
ERROR,
"cache lookup failed for extended statistics object %u", stxoid);
683 memset(repl_val, 0,
sizeof(repl_val));
684 memset(repl_null,
false,
sizeof(repl_null));
685 memset(repl_repl,
false,
sizeof(repl_repl));
688 repl_repl[Anum_pg_statistic_ext_stxstattarget - 1] =
true;
689 if (!newtarget_default)
690 repl_val[Anum_pg_statistic_ext_stxstattarget - 1] =
Int16GetDatum(newtarget);
692 repl_null[Anum_pg_statistic_ext_stxstattarget - 1] =
true;
695 repl_val, repl_null, repl_repl);
764 elog(
ERROR,
"cache lookup failed for statistics object %u", statsOid);
767 relid = statext->stxrelid;
813 char *stxname = NULL;
825 existingstats =
GetSysCacheOid2(STATEXTNAMENSP, Anum_pg_statistic_ext_oid,
886 buflen += strlen(
buf + buflen);
909 elog(
ERROR,
"cache lookup failed for statistics object %u", statId);
912 Assert(stx->oid == statId);
914 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)
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_attname(Oid relid, AttrNumber attnum, bool missing_ok)
char * pstrdup(const char *in)
void pfree(void *pointer)
void namestrcpy(Name name, const char *str)
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)
char * NameListToString(const List *names)
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)