37 #include "utils/fmgroids.h"
55 int bv = *(
const int16 *)
b;
77 bool nulls[Natts_pg_statistic_ext];
90 bool build_dependencies;
92 bool build_expressions;
93 bool requested_type =
false;
108 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
109 errmsg(
"only a single relation is allowed in CREATE STATISTICS")));
111 foreach(cell,
stmt->relations)
117 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
118 errmsg(
"only a single relation is allowed in CREATE STATISTICS")));
130 if (rel->
rd_rel->relkind != RELKIND_RELATION &&
131 rel->
rd_rel->relkind != RELKIND_MATVIEW &&
132 rel->
rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
133 rel->
rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
135 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
136 errmsg(
"cannot define statistics for relation \"%s\"",
148 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
149 errmsg(
"permission denied: \"%s\" is a system catalog",
182 if (
stmt->if_not_exists)
190 errmsg(
"statistics object \"%s\" already exists, skipping",
198 errmsg(
"statistics object \"%s\" already exists", namestr)));
208 (
errcode(ERRCODE_TOO_MANY_COLUMNS),
209 errmsg(
"cannot have more than %d columns in statistics",
225 foreach(cell,
stmt->exprs)
241 (
errcode(ERRCODE_UNDEFINED_COLUMN),
242 errmsg(
"column \"%s\" does not exist",
247 if (attForm->attnum <= 0)
249 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
250 errmsg(
"statistics creation on system columns is not supported")));
256 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
257 errmsg(
"column \"%s\" cannot be used in statistics because its type %s has no default btree operator class",
260 attnums[nattnums] = attForm->attnum;
272 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
273 errmsg(
"statistics creation on system columns is not supported")));
279 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
280 errmsg(
"column \"%s\" cannot be used in statistics because its type %s has no default btree operator class",
306 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
307 errmsg(
"statistics creation on system columns is not supported")));
323 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
324 errmsg(
"expression cannot be used in multivariate statistics because its type %s has no default btree operator class",
328 stxexprs =
lappend(stxexprs, expr);
344 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
345 errmsg(
"when building statistics on a single expression, statistics kinds may not be specified")));
349 build_ndistinct =
false;
350 build_dependencies =
false;
352 foreach(cell,
stmt->stat_types)
356 if (strcmp(
type,
"ndistinct") == 0)
358 build_ndistinct =
true;
359 requested_type =
true;
361 else if (strcmp(
type,
"dependencies") == 0)
363 build_dependencies =
true;
364 requested_type =
true;
366 else if (strcmp(
type,
"mcv") == 0)
369 requested_type =
true;
373 (
errcode(ERRCODE_SYNTAX_ERROR),
374 errmsg(
"unrecognized statistics kind \"%s\"",
382 if ((!requested_type) && (numcols >= 2))
384 build_ndistinct =
true;
385 build_dependencies =
true;
394 build_expressions = (stxexprs !=
NIL);
402 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
403 errmsg(
"extended statistics require at least 2 columns")));
416 for (
i = 1;
i < nattnums;
i++)
418 if (attnums[
i] == attnums[
i - 1])
420 (
errcode(ERRCODE_DUPLICATE_COLUMN),
421 errmsg(
"duplicate column name in statistics definition")));
437 foreach(cell, stxexprs)
442 foreach(cell2, stxexprs)
446 if (
equal(expr1, expr2))
455 (
errcode(ERRCODE_DUPLICATE_COLUMN),
456 errmsg(
"duplicate expression in statistics definition")));
466 if (build_dependencies)
470 if (build_expressions)
485 exprsDatum = (
Datum) 0;
493 memset(nulls,
false,
sizeof(nulls));
496 Anum_pg_statistic_ext_oid);
506 values[Anum_pg_statistic_ext_stxexprs - 1] = exprsDatum;
507 if (exprsDatum == (
Datum) 0)
508 nulls[Anum_pg_statistic_ext_stxexprs - 1] =
true;
539 for (
i = 0;
i < nattnums;
i++)
590 if (
stmt->stxcomment != NULL)
608 Datum repl_val[Natts_pg_statistic_ext];
609 bool repl_null[Natts_pg_statistic_ext];
610 bool repl_repl[Natts_pg_statistic_ext];
612 int newtarget =
stmt->stxstattarget;
618 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
619 errmsg(
"statistics target %d is too low",
626 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
627 errmsg(
"lowering statistics target to %d",
650 (
errmsg(
"statistics object \"%s.%s\" does not exist, skipping",
651 schemaname, statname)));
654 (
errmsg(
"statistics object \"%s\" does not exist, skipping",
665 elog(
ERROR,
"cache lookup failed for extended statistics object %u", stxoid);
673 memset(repl_val, 0,
sizeof(repl_val));
674 memset(repl_null,
false,
sizeof(repl_null));
675 memset(repl_repl,
false,
sizeof(repl_repl));
678 repl_repl[Anum_pg_statistic_ext_stxstattarget - 1] =
true;
679 repl_val[Anum_pg_statistic_ext_stxstattarget - 1] =
Int16GetDatum(newtarget);
682 repl_val, repl_null, repl_repl);
758 elog(
ERROR,
"cache lookup failed for statistics object %u", statsOid);
761 relid = statext->stxrelid;
793 char *stxname = NULL;
866 buflen += strlen(
buf + buflen);
889 elog(
ERROR,
"cache lookup failed for statistics object %u", statId);
892 Assert(stx->oid == statId);
894 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 OidIsValid(objectId)
bool IsSystemRelation(Relation relation)
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
elog(ERROR, "%s: %s", p2, msg)
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_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
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)
Assert(fmt[strlen(fmt) - 1] !='\n')
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)