mirror of
https://github.com/postgres/postgres.git
synced 2025-11-28 11:44:57 +03:00
Allow SET STATISTICS on expression indexes
Index columns are referenced by ordinal number rather than name, e.g. CREATE INDEX coord_idx ON measured (x, y, (z + t)); ALTER INDEX coord_idx ALTER COLUMN 3 SET STATISTICS 1000; Incompatibility note for release notes: \d+ for indexes now also displays Stats Target Authors: Alexander Korotkov, with contribution by Adrien NAYRAT Review: Adrien NAYRAT, Simon Riggs Wordsmith: Simon Riggs
This commit is contained in:
@@ -375,9 +375,9 @@ static ObjectAddress ATExecAddIdentity(Relation rel, const char *colName,
|
||||
static ObjectAddress ATExecSetIdentity(Relation rel, const char *colName,
|
||||
Node *def, LOCKMODE lockmode);
|
||||
static ObjectAddress ATExecDropIdentity(Relation rel, const char *colName, bool missing_ok, LOCKMODE lockmode);
|
||||
static void ATPrepSetStatistics(Relation rel, const char *colName,
|
||||
static void ATPrepSetStatistics(Relation rel, const char *colName, int16 colNum,
|
||||
Node *newValue, LOCKMODE lockmode);
|
||||
static ObjectAddress ATExecSetStatistics(Relation rel, const char *colName,
|
||||
static ObjectAddress ATExecSetStatistics(Relation rel, const char *colName, int16 colNum,
|
||||
Node *newValue, LOCKMODE lockmode);
|
||||
static ObjectAddress ATExecSetOptions(Relation rel, const char *colName,
|
||||
Node *options, bool isReset, LOCKMODE lockmode);
|
||||
@@ -3525,7 +3525,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
|
||||
case AT_SetStatistics: /* ALTER COLUMN SET STATISTICS */
|
||||
ATSimpleRecursion(wqueue, rel, cmd, recurse, lockmode);
|
||||
/* Performs own permission checks */
|
||||
ATPrepSetStatistics(rel, cmd->name, cmd->def, lockmode);
|
||||
ATPrepSetStatistics(rel, cmd->name, cmd->num, cmd->def, lockmode);
|
||||
pass = AT_PASS_MISC;
|
||||
break;
|
||||
case AT_SetOptions: /* ALTER COLUMN SET ( options ) */
|
||||
@@ -3848,7 +3848,7 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
|
||||
address = ATExecSetNotNull(tab, rel, cmd->name, lockmode);
|
||||
break;
|
||||
case AT_SetStatistics: /* ALTER COLUMN SET STATISTICS */
|
||||
address = ATExecSetStatistics(rel, cmd->name, cmd->def, lockmode);
|
||||
address = ATExecSetStatistics(rel, cmd->name, cmd->num, cmd->def, lockmode);
|
||||
break;
|
||||
case AT_SetOptions: /* ALTER COLUMN SET ( options ) */
|
||||
address = ATExecSetOptions(rel, cmd->name, cmd->def, false, lockmode);
|
||||
@@ -6120,7 +6120,7 @@ ATExecDropIdentity(Relation rel, const char *colName, bool missing_ok, LOCKMODE
|
||||
* ALTER TABLE ALTER COLUMN SET STATISTICS
|
||||
*/
|
||||
static void
|
||||
ATPrepSetStatistics(Relation rel, const char *colName, Node *newValue, LOCKMODE lockmode)
|
||||
ATPrepSetStatistics(Relation rel, const char *colName, int16 colNum, Node *newValue, LOCKMODE lockmode)
|
||||
{
|
||||
/*
|
||||
* We do our own permission checking because (a) we want to allow SET
|
||||
@@ -6138,6 +6138,15 @@ ATPrepSetStatistics(Relation rel, const char *colName, Node *newValue, LOCKMODE
|
||||
errmsg("\"%s\" is not a table, materialized view, index, or foreign table",
|
||||
RelationGetRelationName(rel))));
|
||||
|
||||
/*
|
||||
* We allow referencing columns by numbers only for indexes, since
|
||||
* table column numbers could contain gaps if columns are later dropped.
|
||||
*/
|
||||
if (rel->rd_rel->relkind != RELKIND_INDEX && !colName)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("cannot refer to non-index column by number")));
|
||||
|
||||
/* Permissions checks */
|
||||
if (!pg_class_ownercheck(RelationGetRelid(rel), GetUserId()))
|
||||
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
|
||||
@@ -6148,7 +6157,7 @@ ATPrepSetStatistics(Relation rel, const char *colName, Node *newValue, LOCKMODE
|
||||
* Return value is the address of the modified column
|
||||
*/
|
||||
static ObjectAddress
|
||||
ATExecSetStatistics(Relation rel, const char *colName, Node *newValue, LOCKMODE lockmode)
|
||||
ATExecSetStatistics(Relation rel, const char *colName, int16 colNum, Node *newValue, LOCKMODE lockmode)
|
||||
{
|
||||
int newtarget;
|
||||
Relation attrelation;
|
||||
@@ -6181,13 +6190,27 @@ ATExecSetStatistics(Relation rel, const char *colName, Node *newValue, LOCKMODE
|
||||
|
||||
attrelation = heap_open(AttributeRelationId, RowExclusiveLock);
|
||||
|
||||
tuple = SearchSysCacheCopyAttName(RelationGetRelid(rel), colName);
|
||||
if (colName)
|
||||
{
|
||||
tuple = SearchSysCacheCopyAttName(RelationGetRelid(rel), colName);
|
||||
|
||||
if (!HeapTupleIsValid(tuple))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
||||
errmsg("column \"%s\" of relation \"%s\" does not exist",
|
||||
colName, RelationGetRelationName(rel))));
|
||||
}
|
||||
else
|
||||
{
|
||||
tuple = SearchSysCacheCopyAttNum(RelationGetRelid(rel), colNum);
|
||||
|
||||
if (!HeapTupleIsValid(tuple))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
||||
errmsg("column number %d of relation \"%s\" does not exist",
|
||||
colNum, RelationGetRelationName(rel))));
|
||||
}
|
||||
|
||||
if (!HeapTupleIsValid(tuple))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
||||
errmsg("column \"%s\" of relation \"%s\" does not exist",
|
||||
colName, RelationGetRelationName(rel))));
|
||||
attrtuple = (Form_pg_attribute) GETSTRUCT(tuple);
|
||||
|
||||
attnum = attrtuple->attnum;
|
||||
@@ -6197,6 +6220,14 @@ ATExecSetStatistics(Relation rel, const char *colName, Node *newValue, LOCKMODE
|
||||
errmsg("cannot alter system column \"%s\"",
|
||||
colName)));
|
||||
|
||||
if (rel->rd_rel->relkind == RELKIND_INDEX &&
|
||||
rel->rd_index->indkey.values[attnum - 1] != 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("cannot alter statistics on non-expression column \"%s\" of index \"%s\"",
|
||||
NameStr(attrtuple->attname), RelationGetRelationName(rel)),
|
||||
errhint("Alter statistics on table column instead.")));
|
||||
|
||||
attrtuple->attstattarget = newtarget;
|
||||
|
||||
CatalogTupleUpdate(attrelation, &tuple->t_self, tuple);
|
||||
|
||||
@@ -3087,6 +3087,7 @@ _copyAlterTableCmd(const AlterTableCmd *from)
|
||||
|
||||
COPY_SCALAR_FIELD(subtype);
|
||||
COPY_STRING_FIELD(name);
|
||||
COPY_SCALAR_FIELD(num);
|
||||
COPY_NODE_FIELD(newowner);
|
||||
COPY_NODE_FIELD(def);
|
||||
COPY_SCALAR_FIELD(behavior);
|
||||
|
||||
@@ -1098,6 +1098,7 @@ _equalAlterTableCmd(const AlterTableCmd *a, const AlterTableCmd *b)
|
||||
{
|
||||
COMPARE_SCALAR_FIELD(subtype);
|
||||
COMPARE_STRING_FIELD(name);
|
||||
COMPARE_SCALAR_FIELD(num);
|
||||
COMPARE_NODE_FIELD(newowner);
|
||||
COMPARE_NODE_FIELD(def);
|
||||
COMPARE_SCALAR_FIELD(behavior);
|
||||
|
||||
@@ -2078,6 +2078,22 @@ alter_table_cmd:
|
||||
n->def = (Node *) makeInteger($6);
|
||||
$$ = (Node *)n;
|
||||
}
|
||||
/* ALTER TABLE <name> ALTER [COLUMN] <colnum> SET STATISTICS <SignedIconst> */
|
||||
| ALTER opt_column Iconst SET STATISTICS SignedIconst
|
||||
{
|
||||
AlterTableCmd *n = makeNode(AlterTableCmd);
|
||||
|
||||
if ($3 <= 0 || $3 > PG_INT16_MAX)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("column number must be in range from 1 to %d", PG_INT16_MAX),
|
||||
parser_errposition(@3)));
|
||||
|
||||
n->subtype = AT_SetStatistics;
|
||||
n->num = (int16) $3;
|
||||
n->def = (Node *) makeInteger($6);
|
||||
$$ = (Node *)n;
|
||||
}
|
||||
/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
|
||||
| ALTER opt_column ColId SET reloptions
|
||||
{
|
||||
|
||||
46
src/backend/utils/cache/syscache.c
vendored
46
src/backend/utils/cache/syscache.c
vendored
@@ -1256,6 +1256,52 @@ SearchSysCacheExistsAttName(Oid relid, const char *attname)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* SearchSysCacheAttNum
|
||||
*
|
||||
* This routine is equivalent to SearchSysCache on the ATTNUM cache,
|
||||
* except that it will return NULL if the found attribute is marked
|
||||
* attisdropped. This is convenient for callers that want to act as
|
||||
* though dropped attributes don't exist.
|
||||
*/
|
||||
HeapTuple
|
||||
SearchSysCacheAttNum(Oid relid, int16 attnum)
|
||||
{
|
||||
HeapTuple tuple;
|
||||
|
||||
tuple = SearchSysCache2(ATTNUM,
|
||||
ObjectIdGetDatum(relid),
|
||||
Int16GetDatum(attnum));
|
||||
if (!HeapTupleIsValid(tuple))
|
||||
return NULL;
|
||||
if (((Form_pg_attribute) GETSTRUCT(tuple))->attisdropped)
|
||||
{
|
||||
ReleaseSysCache(tuple);
|
||||
return NULL;
|
||||
}
|
||||
return tuple;
|
||||
}
|
||||
|
||||
/*
|
||||
* SearchSysCacheCopyAttNum
|
||||
*
|
||||
* As above, an attisdropped-aware version of SearchSysCacheCopy.
|
||||
*/
|
||||
HeapTuple
|
||||
SearchSysCacheCopyAttNum(Oid relid, int16 attnum)
|
||||
{
|
||||
HeapTuple tuple,
|
||||
newtuple;
|
||||
|
||||
tuple = SearchSysCacheAttNum(relid, attnum);
|
||||
if (!HeapTupleIsValid(tuple))
|
||||
return NULL;
|
||||
newtuple = heap_copytuple(tuple);
|
||||
ReleaseSysCache(tuple);
|
||||
return newtuple;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* SysCacheGetAttr
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user