Skip to content

Commit 6528139

Browse files
committed
Print out error position for some ALTER TABLE ALTER COLUMN type
A ParseState exists in ATPrepAlterColumnType() since its introduction in 077db40, 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
1 parent 14793f4 commit 6528139

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

src/backend/commands/tablecmds.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -13410,27 +13410,31 @@ ATPrepAlterColumnType(List **wqueue,
1341013410
AclResult aclresult;
1341113411
bool is_expr;
1341213412

13413+
pstate->p_sourcetext = context->queryString;
13414+
1341313415
if (rel->rd_rel->reloftype && !recursing)
1341413416
ereport(ERROR,
1341513417
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
13416-
errmsg("cannot alter column type of typed table")));
13418+
errmsg("cannot alter column type of typed table"),
13419+
parser_errposition(pstate, def->location)));
1341713420

1341813421
/* lookup the attribute so we can check inheritance status */
1341913422
tuple = SearchSysCacheAttName(RelationGetRelid(rel), colName);
1342013423
if (!HeapTupleIsValid(tuple))
1342113424
ereport(ERROR,
1342213425
(errcode(ERRCODE_UNDEFINED_COLUMN),
1342313426
errmsg("column \"%s\" of relation \"%s\" does not exist",
13424-
colName, RelationGetRelationName(rel))));
13427+
colName, RelationGetRelationName(rel)),
13428+
parser_errposition(pstate, def->location)));
1342513429
attTup = (Form_pg_attribute) GETSTRUCT(tuple);
1342613430
attnum = attTup->attnum;
1342713431

1342813432
/* Can't alter a system attribute */
1342913433
if (attnum <= 0)
1343013434
ereport(ERROR,
1343113435
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13432-
errmsg("cannot alter system column \"%s\"",
13433-
colName)));
13436+
errmsg("cannot alter system column \"%s\"", colName),
13437+
parser_errposition(pstate, def->location)));
1343413438

1343513439
/*
1343613440
* Cannot specify USING when altering type of a generated column, because
@@ -13440,7 +13444,8 @@ ATPrepAlterColumnType(List **wqueue,
1344013444
ereport(ERROR,
1344113445
(errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
1344213446
errmsg("cannot specify USING when altering type of generated column"),
13443-
errdetail("Column \"%s\" is a generated column.", colName)));
13447+
errdetail("Column \"%s\" is a generated column.", colName),
13448+
parser_errposition(pstate, def->location)));
1344413449

1344513450
/*
1344613451
* Don't alter inherited columns. At outer level, there had better not be
@@ -13450,8 +13455,8 @@ ATPrepAlterColumnType(List **wqueue,
1345013455
if (attTup->attinhcount > 0 && !recursing)
1345113456
ereport(ERROR,
1345213457
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
13453-
errmsg("cannot alter inherited column \"%s\"",
13454-
colName)));
13458+
errmsg("cannot alter inherited column \"%s\"", colName),
13459+
parser_errposition(pstate, def->location)));
1345513460

1345613461
/* Don't alter columns used in the partition key */
1345713462
if (has_partition_attrs(rel,
@@ -13460,17 +13465,18 @@ ATPrepAlterColumnType(List **wqueue,
1346013465
ereport(ERROR,
1346113466
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
1346213467
errmsg("cannot alter column \"%s\" because it is part of the partition key of relation \"%s\"",
13463-
colName, RelationGetRelationName(rel))));
13468+
colName, RelationGetRelationName(rel)),
13469+
parser_errposition(pstate, def->location)));
1346413470

1346513471
/* Look up the target type */
13466-
typenameTypeIdAndMod(NULL, typeName, &targettype, &targettypmod);
13472+
typenameTypeIdAndMod(pstate, typeName, &targettype, &targettypmod);
1346713473

1346813474
aclresult = object_aclcheck(TypeRelationId, targettype, GetUserId(), ACL_USAGE);
1346913475
if (aclresult != ACLCHECK_OK)
1347013476
aclcheck_error_type(aclresult, targettype);
1347113477

1347213478
/* And the collation */
13473-
targetcollid = GetColumnDefCollation(NULL, def, targettype);
13479+
targetcollid = GetColumnDefCollation(pstate, def, targettype);
1347413480

1347513481
/* make sure datatype is legal for a column */
1347613482
CheckAttributeType(colName, targettype, targetcollid,

src/test/regress/expected/alter_table.out

+14
Original file line numberDiff line numberDiff line change
@@ -3426,10 +3426,16 @@ ALTER TABLE comment_test ALTER COLUMN positive_col SET DATA TYPE bigint;
34263426
-- Some error cases.
34273427
ALTER TABLE comment_test ALTER COLUMN xmin SET DATA TYPE x;
34283428
ERROR: cannot alter system column "xmin"
3429+
LINE 1: ALTER TABLE comment_test ALTER COLUMN xmin SET DATA TYPE x;
3430+
^
34293431
ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE x;
34303432
ERROR: type "x" does not exist
3433+
LINE 1: ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE x;
3434+
^
34313435
ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE int COLLATE "C";
34323436
ERROR: collations are not supported by type integer
3437+
LINE 1: ...LE comment_test ALTER COLUMN id SET DATA TYPE int COLLATE "C...
3438+
^
34333439
-- Check that the comments are intact.
34343440
SELECT col_description('comment_test'::regclass, 1) as comment;
34353441
comment
@@ -3895,10 +3901,14 @@ ALTER TABLE partitioned DROP COLUMN a;
38953901
ERROR: cannot drop column "a" because it is part of the partition key of relation "partitioned"
38963902
ALTER TABLE partitioned ALTER COLUMN a TYPE char(5);
38973903
ERROR: cannot alter column "a" because it is part of the partition key of relation "partitioned"
3904+
LINE 1: ALTER TABLE partitioned ALTER COLUMN a TYPE char(5);
3905+
^
38983906
ALTER TABLE partitioned DROP COLUMN b;
38993907
ERROR: cannot drop column "b" because it is part of the partition key of relation "partitioned"
39003908
ALTER TABLE partitioned ALTER COLUMN b TYPE char(5);
39013909
ERROR: cannot alter column "b" because it is part of the partition key of relation "partitioned"
3910+
LINE 1: ALTER TABLE partitioned ALTER COLUMN b TYPE char(5);
3911+
^
39023912
-- specifying storage parameters for partitioned tables is not supported
39033913
ALTER TABLE partitioned SET (fillfactor=100);
39043914
ERROR: cannot specify storage parameters for a partitioned table
@@ -4423,6 +4433,8 @@ ALTER TABLE part_2 RENAME COLUMN b to c;
44234433
ERROR: cannot rename inherited column "b"
44244434
ALTER TABLE part_2 ALTER COLUMN b TYPE text;
44254435
ERROR: cannot alter inherited column "b"
4436+
LINE 1: ALTER TABLE part_2 ALTER COLUMN b TYPE text;
4437+
^
44264438
-- cannot add NOT NULL or check constraints to *only* the parent, when
44274439
-- partitions exist
44284440
ALTER TABLE ONLY list_parted2 ALTER b SET NOT NULL;
@@ -4484,6 +4496,8 @@ ALTER TABLE list_parted2 DROP COLUMN b;
44844496
ERROR: cannot drop column "b" because it is part of the partition key of relation "part_5"
44854497
ALTER TABLE list_parted2 ALTER COLUMN b TYPE text;
44864498
ERROR: cannot alter column "b" because it is part of the partition key of relation "part_5"
4499+
LINE 1: ALTER TABLE list_parted2 ALTER COLUMN b TYPE text;
4500+
^
44874501
-- dropping non-partition key columns should be allowed on the parent table.
44884502
ALTER TABLE list_parted DROP COLUMN b;
44894503
SELECT * FROM list_parted;

src/test/regress/expected/generated_stored.out

+2
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,8 @@ SELECT * FROM gtest27;
11241124

11251125
ALTER TABLE gtest27 ALTER COLUMN x TYPE boolean USING x <> 0; -- error
11261126
ERROR: cannot specify USING when altering type of generated column
1127+
LINE 1: ALTER TABLE gtest27 ALTER COLUMN x TYPE boolean USING x <> 0...
1128+
^
11271129
DETAIL: Column "x" is a generated column.
11281130
ALTER TABLE gtest27 ALTER COLUMN x DROP DEFAULT; -- error
11291131
ERROR: column "x" of relation "gtest27" is a generated column

src/test/regress/expected/typed_table.out

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ ALTER TABLE persons RENAME COLUMN id TO num;
3838
ERROR: cannot rename column of typed table
3939
ALTER TABLE persons ALTER COLUMN name TYPE varchar;
4040
ERROR: cannot alter column type of typed table
41+
LINE 1: ALTER TABLE persons ALTER COLUMN name TYPE varchar;
42+
^
4143
CREATE TABLE stuff (id int);
4244
ALTER TABLE persons INHERIT stuff;
4345
ERROR: cannot change inheritance of typed table

0 commit comments

Comments
 (0)