1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-11 00:12:06 +03:00

Print out error position for some ALTER TABLE ALTER COLUMN type

A ParseState exists in ATPrepAlterColumnType() since its introduction
in 077db40fa1, and it has never relied on a query string that could be
used to point at a location in the origin string on error.

The output of some regression tests are updated, showing the error
location where applicable.  Six error strings are upgraded with the
error location.

Author: Jian He
Discussion: https://postgr.es/m/CACJufxGfbPfWLjcEz33G9eW_epDW0UDi2H05i9eSTPKGJ4rxSA@mail.gmail.com
This commit is contained in:
Michael Paquier
2025-01-27 13:51:23 +09:00
parent 14793f4719
commit 65281391a9
4 changed files with 34 additions and 10 deletions

View File

@@ -13410,10 +13410,13 @@ ATPrepAlterColumnType(List **wqueue,
AclResult aclresult;
bool is_expr;
pstate->p_sourcetext = context->queryString;
if (rel->rd_rel->reloftype && !recursing)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("cannot alter column type of typed table")));
errmsg("cannot alter column type of typed table"),
parser_errposition(pstate, def->location)));
/* lookup the attribute so we can check inheritance status */
tuple = SearchSysCacheAttName(RelationGetRelid(rel), colName);
@@ -13421,7 +13424,8 @@ ATPrepAlterColumnType(List **wqueue,
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("column \"%s\" of relation \"%s\" does not exist",
colName, RelationGetRelationName(rel))));
colName, RelationGetRelationName(rel)),
parser_errposition(pstate, def->location)));
attTup = (Form_pg_attribute) GETSTRUCT(tuple);
attnum = attTup->attnum;
@@ -13429,8 +13433,8 @@ ATPrepAlterColumnType(List **wqueue,
if (attnum <= 0)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot alter system column \"%s\"",
colName)));
errmsg("cannot alter system column \"%s\"", colName),
parser_errposition(pstate, def->location)));
/*
* Cannot specify USING when altering type of a generated column, because
@@ -13440,7 +13444,8 @@ ATPrepAlterColumnType(List **wqueue,
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
errmsg("cannot specify USING when altering type of generated column"),
errdetail("Column \"%s\" is a generated column.", colName)));
errdetail("Column \"%s\" is a generated column.", colName),
parser_errposition(pstate, def->location)));
/*
* Don't alter inherited columns. At outer level, there had better not be
@@ -13450,8 +13455,8 @@ ATPrepAlterColumnType(List **wqueue,
if (attTup->attinhcount > 0 && !recursing)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
errmsg("cannot alter inherited column \"%s\"",
colName)));
errmsg("cannot alter inherited column \"%s\"", colName),
parser_errposition(pstate, def->location)));
/* Don't alter columns used in the partition key */
if (has_partition_attrs(rel,
@@ -13460,17 +13465,18 @@ ATPrepAlterColumnType(List **wqueue,
ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
errmsg("cannot alter column \"%s\" because it is part of the partition key of relation \"%s\"",
colName, RelationGetRelationName(rel))));
colName, RelationGetRelationName(rel)),
parser_errposition(pstate, def->location)));
/* Look up the target type */
typenameTypeIdAndMod(NULL, typeName, &targettype, &targettypmod);
typenameTypeIdAndMod(pstate, typeName, &targettype, &targettypmod);
aclresult = object_aclcheck(TypeRelationId, targettype, GetUserId(), ACL_USAGE);
if (aclresult != ACLCHECK_OK)
aclcheck_error_type(aclresult, targettype);
/* And the collation */
targetcollid = GetColumnDefCollation(NULL, def, targettype);
targetcollid = GetColumnDefCollation(pstate, def, targettype);
/* make sure datatype is legal for a column */
CheckAttributeType(colName, targettype, targetcollid,