Improve consistency of the error messages generated when you try to use

ALTER TABLE on a composite type or ALTER TYPE on a table's rowtype.
We already rejected these cases, but the error messages were a bit
random and didn't always provide a HINT to use the other command type.
This commit is contained in:
Tom Lane 2007-09-29 17:18:58 +00:00
parent e6022e7470
commit 34b44c3ba2
2 changed files with 50 additions and 24 deletions

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.232 2007/09/06 17:31:58 tgl Exp $ * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.233 2007/09/29 17:18:58 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -5430,8 +5430,16 @@ ATExecChangeOwner(Oid relationOid, Oid newOwnerId, bool recursing)
get_rel_name(tableId)))); get_rel_name(tableId))));
} }
break; break;
case RELKIND_TOASTVALUE:
case RELKIND_COMPOSITE_TYPE: case RELKIND_COMPOSITE_TYPE:
if (recursing)
break;
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is a composite type",
NameStr(tuple_class->relname)),
errhint("Use ALTER TYPE instead.")));
break;
case RELKIND_TOASTVALUE:
if (recursing) if (recursing)
break; break;
/* FALL THRU */ /* FALL THRU */
@ -6478,31 +6486,48 @@ AlterTableNamespace(RangeVar *relation, const char *newschema)
Oid nspOid; Oid nspOid;
Relation classRel; Relation classRel;
rel = heap_openrv(relation, AccessExclusiveLock); rel = relation_openrv(relation, AccessExclusiveLock);
relid = RelationGetRelid(rel); relid = RelationGetRelid(rel);
oldNspOid = RelationGetNamespace(rel); oldNspOid = RelationGetNamespace(rel);
/* heap_openrv allows TOAST, but we don't want to */ /* Can we change the schema of this tuple? */
if (rel->rd_rel->relkind == RELKIND_TOASTVALUE) switch (rel->rd_rel->relkind)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is a TOAST relation",
RelationGetRelationName(rel))));
/* if it's an owned sequence, disallow moving it by itself */
if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
{ {
Oid tableId; case RELKIND_RELATION:
int32 colId; case RELKIND_VIEW:
/* ok to change schema */
break;
case RELKIND_SEQUENCE:
{
/* if it's an owned sequence, disallow moving it by itself */
Oid tableId;
int32 colId;
if (sequenceIsOwned(relid, &tableId, &colId)) if (sequenceIsOwned(relid, &tableId, &colId))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot move an owned sequence into another schema"),
errdetail("Sequence \"%s\" is linked to table \"%s\".",
RelationGetRelationName(rel),
get_rel_name(tableId))));
}
break;
case RELKIND_COMPOSITE_TYPE:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("cannot move an owned sequence into another schema"), errmsg("\"%s\" is a composite type",
errdetail("Sequence \"%s\" is linked to table \"%s\".", RelationGetRelationName(rel)),
RelationGetRelationName(rel), errhint("Use ALTER TYPE instead.")));
get_rel_name(tableId)))); break;
case RELKIND_INDEX:
case RELKIND_TOASTVALUE:
/* FALL THRU */
default:
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is not a table, view, or sequence",
RelationGetRelationName(rel))));
} }
/* get schema OID and check its permissions */ /* get schema OID and check its permissions */

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.107 2007/09/04 16:41:42 adunstan Exp $ * $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.108 2007/09/29 17:18:58 tgl Exp $
* *
* DESCRIPTION * DESCRIPTION
* The "DefineFoo" routines take the parse tree and pick out the * The "DefineFoo" routines take the parse tree and pick out the
@ -2355,8 +2355,9 @@ AlterTypeOwner(List *names, Oid newOwnerId)
get_rel_relkind(typTup->typrelid) != RELKIND_COMPOSITE_TYPE) get_rel_relkind(typTup->typrelid) != RELKIND_COMPOSITE_TYPE)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE), (errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is a table's row type", errmsg("%s is a table's row type",
TypeNameToString(typename)))); format_type_be(typeOid)),
errhint("Use ALTER TABLE instead.")));
/* don't allow direct alteration of array types, either */ /* don't allow direct alteration of array types, either */
if (OidIsValid(typTup->typelem) && if (OidIsValid(typTup->typelem) &&
@ -2592,7 +2593,7 @@ AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
(errcode(ERRCODE_WRONG_OBJECT_TYPE), (errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("%s is a table's row type", errmsg("%s is a table's row type",
format_type_be(typeOid)), format_type_be(typeOid)),
errhint("Use ALTER TABLE SET SCHEMA instead."))); errhint("Use ALTER TABLE instead.")));
/* OK, modify the pg_type row */ /* OK, modify the pg_type row */