Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 34b44c3

Browse files
committed
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.
1 parent e6022e7 commit 34b44c3

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

src/backend/commands/tablecmds.c

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.232 2007/09/06 17:31:58 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.233 2007/09/29 17:18:58 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -5430,8 +5430,16 @@ ATExecChangeOwner(Oid relationOid, Oid newOwnerId, bool recursing)
54305430
get_rel_name(tableId))));
54315431
}
54325432
break;
5433-
case RELKIND_TOASTVALUE:
54345433
case RELKIND_COMPOSITE_TYPE:
5434+
if (recursing)
5435+
break;
5436+
ereport(ERROR,
5437+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
5438+
errmsg("\"%s\" is a composite type",
5439+
NameStr(tuple_class->relname)),
5440+
errhint("Use ALTER TYPE instead.")));
5441+
break;
5442+
case RELKIND_TOASTVALUE:
54355443
if (recursing)
54365444
break;
54375445
/* FALL THRU */
@@ -6478,31 +6486,48 @@ AlterTableNamespace(RangeVar *relation, const char *newschema)
64786486
Oid nspOid;
64796487
Relation classRel;
64806488

6481-
rel = heap_openrv(relation, AccessExclusiveLock);
6489+
rel = relation_openrv(relation, AccessExclusiveLock);
64826490

64836491
relid = RelationGetRelid(rel);
64846492
oldNspOid = RelationGetNamespace(rel);
64856493

6486-
/* heap_openrv allows TOAST, but we don't want to */
6487-
if (rel->rd_rel->relkind == RELKIND_TOASTVALUE)
6488-
ereport(ERROR,
6489-
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
6490-
errmsg("\"%s\" is a TOAST relation",
6491-
RelationGetRelationName(rel))));
6492-
6493-
/* if it's an owned sequence, disallow moving it by itself */
6494-
if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
6494+
/* Can we change the schema of this tuple? */
6495+
switch (rel->rd_rel->relkind)
64956496
{
6496-
Oid tableId;
6497-
int32 colId;
6497+
case RELKIND_RELATION:
6498+
case RELKIND_VIEW:
6499+
/* ok to change schema */
6500+
break;
6501+
case RELKIND_SEQUENCE:
6502+
{
6503+
/* if it's an owned sequence, disallow moving it by itself */
6504+
Oid tableId;
6505+
int32 colId;
64986506

6499-
if (sequenceIsOwned(relid, &tableId, &colId))
6507+
if (sequenceIsOwned(relid, &tableId, &colId))
6508+
ereport(ERROR,
6509+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6510+
errmsg("cannot move an owned sequence into another schema"),
6511+
errdetail("Sequence \"%s\" is linked to table \"%s\".",
6512+
RelationGetRelationName(rel),
6513+
get_rel_name(tableId))));
6514+
}
6515+
break;
6516+
case RELKIND_COMPOSITE_TYPE:
65006517
ereport(ERROR,
6501-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6502-
errmsg("cannot move an owned sequence into another schema"),
6503-
errdetail("Sequence \"%s\" is linked to table \"%s\".",
6504-
RelationGetRelationName(rel),
6505-
get_rel_name(tableId))));
6518+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
6519+
errmsg("\"%s\" is a composite type",
6520+
RelationGetRelationName(rel)),
6521+
errhint("Use ALTER TYPE instead.")));
6522+
break;
6523+
case RELKIND_INDEX:
6524+
case RELKIND_TOASTVALUE:
6525+
/* FALL THRU */
6526+
default:
6527+
ereport(ERROR,
6528+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
6529+
errmsg("\"%s\" is not a table, view, or sequence",
6530+
RelationGetRelationName(rel))));
65066531
}
65076532

65086533
/* get schema OID and check its permissions */

src/backend/commands/typecmds.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.107 2007/09/04 16:41:42 adunstan Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.108 2007/09/29 17:18:58 tgl Exp $
1212
*
1313
* DESCRIPTION
1414
* The "DefineFoo" routines take the parse tree and pick out the
@@ -2355,8 +2355,9 @@ AlterTypeOwner(List *names, Oid newOwnerId)
23552355
get_rel_relkind(typTup->typrelid) != RELKIND_COMPOSITE_TYPE)
23562356
ereport(ERROR,
23572357
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
2358-
errmsg("\"%s\" is a table's row type",
2359-
TypeNameToString(typename))));
2358+
errmsg("%s is a table's row type",
2359+
format_type_be(typeOid)),
2360+
errhint("Use ALTER TABLE instead.")));
23602361

23612362
/* don't allow direct alteration of array types, either */
23622363
if (OidIsValid(typTup->typelem) &&
@@ -2592,7 +2593,7 @@ AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
25922593
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
25932594
errmsg("%s is a table's row type",
25942595
format_type_be(typeOid)),
2595-
errhint("Use ALTER TABLE SET SCHEMA instead.")));
2596+
errhint("Use ALTER TABLE instead.")));
25962597

25972598
/* OK, modify the pg_type row */
25982599

0 commit comments

Comments
 (0)