From 27874583627e049a049dc1327deb12a02a7013ab Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Fri, 27 Jan 2012 21:20:34 +0200 Subject: [PATCH] Disallow ALTER DOMAIN on non-domain type everywhere This has been the behavior already in most cases, but through omission, ALTER DOMAIN / OWNER TO and ALTER DOMAIN / SET SCHEMA would silently work on non-domain types as well. --- src/backend/commands/alter.c | 4 ++-- src/backend/commands/typecmds.c | 18 ++++++++++++++++-- src/include/commands/typecmds.h | 4 ++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/backend/commands/alter.c b/src/backend/commands/alter.c index 7c658c0348..9175405af2 100644 --- a/src/backend/commands/alter.c +++ b/src/backend/commands/alter.c @@ -213,7 +213,7 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt) case OBJECT_TYPE: case OBJECT_DOMAIN: - AlterTypeNamespace(stmt->object, stmt->newschema); + AlterTypeNamespace(stmt->object, stmt->newschema, stmt->objectType); break; default: @@ -510,7 +510,7 @@ ExecAlterOwnerStmt(AlterOwnerStmt *stmt) case OBJECT_TYPE: case OBJECT_DOMAIN: /* same as TYPE */ - AlterTypeOwner(stmt->object, newowner); + AlterTypeOwner(stmt->object, newowner, stmt->objectType); break; case OBJECT_TSDICTIONARY: diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c index 03918486a1..c6bc6c8e87 100644 --- a/src/backend/commands/typecmds.c +++ b/src/backend/commands/typecmds.c @@ -3165,7 +3165,7 @@ RenameType(RenameStmt *stmt) * Change the owner of a type. */ void -AlterTypeOwner(List *names, Oid newOwnerId) +AlterTypeOwner(List *names, Oid newOwnerId, ObjectType objecttype) { TypeName *typename; Oid typeOid; @@ -3195,6 +3195,13 @@ AlterTypeOwner(List *names, Oid newOwnerId) tup = newtup; typTup = (Form_pg_type) GETSTRUCT(tup); + /* Don't allow ALTER DOMAIN on a type */ + if (objecttype == OBJECT_DOMAIN && typTup->typtype != TYPTYPE_DOMAIN) + ereport(ERROR, + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("%s is not a domain", + format_type_be(typeOid)))); + /* * If it's a composite type, we need to check that it really is a * free-standing composite type, and not a table's rowtype. We want people @@ -3328,7 +3335,7 @@ AlterTypeOwnerInternal(Oid typeOid, Oid newOwnerId, * Execute ALTER TYPE SET SCHEMA */ void -AlterTypeNamespace(List *names, const char *newschema) +AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype) { TypeName *typename; Oid typeOid; @@ -3338,6 +3345,13 @@ AlterTypeNamespace(List *names, const char *newschema) typename = makeTypeNameFromNameList(names); typeOid = typenameTypeId(NULL, typename); + /* Don't allow ALTER DOMAIN on a type */ + if (objecttype == OBJECT_DOMAIN && get_typtype(typeOid) != TYPTYPE_DOMAIN) + ereport(ERROR, + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("%s is not a domain", + format_type_be(typeOid)))); + /* get schema OID and check its permissions */ nspOid = LookupCreationNamespace(newschema); diff --git a/src/include/commands/typecmds.h b/src/include/commands/typecmds.h index 3748bd56af..9de5330924 100644 --- a/src/include/commands/typecmds.h +++ b/src/include/commands/typecmds.h @@ -38,10 +38,10 @@ extern void AlterDomainDropConstraint(List *names, const char *constrName, extern List *GetDomainConstraints(Oid typeOid); extern void RenameType(RenameStmt *stmt); -extern void AlterTypeOwner(List *names, Oid newOwnerId); +extern void AlterTypeOwner(List *names, Oid newOwnerId, ObjectType objecttype); extern void AlterTypeOwnerInternal(Oid typeOid, Oid newOwnerId, bool hasDependEntry); -extern void AlterTypeNamespace(List *names, const char *newschema); +extern void AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype); extern Oid AlterTypeNamespace_oid(Oid typeOid, Oid nspOid); extern Oid AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid, bool isImplicitArray, -- 2.40.0