Improve consistency of the error messages generated when you try to use
authorTom Lane
Sat, 29 Sep 2007 17:18:58 +0000 (17:18 +0000)
committerTom Lane
Sat, 29 Sep 2007 17:18:58 +0000 (17:18 +0000)
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.

src/backend/commands/tablecmds.c
src/backend/commands/typecmds.c

index 10fc87cca8afa2e2656ab550be5da6b982e37026..ea8d7f608988eede467729a4f7c26c3c3a48615e 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * 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))));
            }
            break;
-       case RELKIND_TOASTVALUE:
        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)
                break;
            /* FALL THRU */
@@ -6478,31 +6486,48 @@ AlterTableNamespace(RangeVar *relation, const char *newschema)
    Oid         nspOid;
    Relation    classRel;
 
-   rel = heap_openrv(relation, AccessExclusiveLock);
+   rel = relation_openrv(relation, AccessExclusiveLock);
 
    relid = RelationGetRelid(rel);
    oldNspOid = RelationGetNamespace(rel);
 
-   /* heap_openrv allows TOAST, but we don't want to */
-   if (rel->rd_rel->relkind == RELKIND_TOASTVALUE)
-       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)
+   /* Can we change the schema of this tuple? */
+   switch (rel->rd_rel->relkind)
    {
-       Oid         tableId;
-       int32       colId;
+       case RELKIND_RELATION:
+       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,
-                   (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))));
+                   (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+                    errmsg("\"%s\" is a composite type",
+                           RelationGetRelationName(rel)),
+                    errhint("Use ALTER TYPE instead.")));
+           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 */
index 3550d00f0783e63c460e16df9acf21494e7dfcec..75a8b7530e8d687070e754f96e3c9e79a2bad1b3 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * 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
  *   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)
        ereport(ERROR,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
-                errmsg("\"%s\" is a table's row type",
-                       TypeNameToString(typename))));
+                errmsg("%s is a table's row type",
+                       format_type_be(typeOid)),
+                errhint("Use ALTER TABLE instead.")));
 
    /* don't allow direct alteration of array types, either */
    if (OidIsValid(typTup->typelem) &&
@@ -2592,7 +2593,7 @@ AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                 errmsg("%s is a table's row type",
                        format_type_be(typeOid)),
-                errhint("Use ALTER TABLE SET SCHEMA instead.")));
+                errhint("Use ALTER TABLE instead.")));
 
    /* OK, modify the pg_type row */