From f9368a06b7e608d13a6e9718c9b95878eae9bd9c Mon Sep 17 00:00:00 2001 From: Giuseppe Sucameli Date: Tue, 24 Jan 2012 14:45:50 +0100 Subject: better error message executing ALTER TABLE to add/rename a column named like a system column --- src/backend/commands/tablecmds.c | 34 ++++++++++++++++++++++++++++++++++ src/test/regress/expected/errors.out | 2 +- 2 files changed, 35 insertions(+), 1 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index cc210f0..3431a89 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2258,6 +2258,24 @@ renameatt_internal(Oid myrelid, /* new name should not already exist */ + /* + * first check for collision with system attribute names + * + * Skip this for a view or type relation, since those don't have system + * attributes. + */ + if (targetrelation->rd_rel->relkind != RELKIND_VIEW + && targetrelation->rd_rel->relkind != RELKIND_COMPOSITE_TYPE) + { + if (SystemAttributeByName(newattname, + targetrelation->rd_rel->relhasoids) != NULL) + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_COLUMN), + errmsg("column name \"%s\" conflicts with a system column name", + newattname))); + } + + /* this test is deliberately not attisdropped-aware */ if (SearchSysCacheExists2(ATTNAME, ObjectIdGetDatum(myrelid), @@ -4294,6 +4312,22 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, relkind = ((Form_pg_class) GETSTRUCT(reltup))->relkind; /* + * first check for collision with system attribute names + * + * Skip this for a view or type relation, since those don't have system + * attributes. + */ + if (relkind != RELKIND_VIEW && relkind != RELKIND_COMPOSITE_TYPE) + { + if (SystemAttributeByName(colDef->colname, + ((Form_pg_class) GETSTRUCT(reltup))->relhasoids && !isOid) != NULL) + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_COLUMN), + errmsg("column name \"%s\" conflicts with a system column name", + colDef->colname))); + } + + /* * this test is deliberately not attisdropped-aware, since if one tries to * add a column matching a dropped column name, it's gonna fail anyway. */ diff --git a/src/test/regress/expected/errors.out b/src/test/regress/expected/errors.out index 4a10c6a..fa0bd82 100644 --- a/src/test/regress/expected/errors.out +++ b/src/test/regress/expected/errors.out @@ -109,7 +109,7 @@ alter table emp rename column salary to manager; ERROR: column "manager" of relation "stud_emp" already exists -- conflict alter table emp rename column salary to oid; -ERROR: column "oid" of relation "stud_emp" already exists +ERROR: column name "oid" conflicts with a system column name -- -- TRANSACTION STUFF -- not in a xact -- 1.7.4.1