Add ALTER DOMAIN ... RENAME

You could already rename domains using ALTER TYPE, but with this new
command it is more consistent with how other commands treat domains as
a subcategory of types.
This commit is contained in:
Peter Eisentraut 2011-12-22 22:43:56 +02:00
parent 8d15e3ec4f
commit f90dd28062
7 changed files with 59 additions and 4 deletions

View File

@ -35,6 +35,8 @@ ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
VALIDATE CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable>
ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
</synopsis>
@ -118,6 +120,15 @@ ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>RENAME</literal></term>
<listitem>
<para>
This form changes the name of the domain.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>SET SCHEMA</term>
<listitem>
@ -203,6 +214,15 @@ ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">new_name</replaceable></term>
<listitem>
<para>
The new name for the domain.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">new_owner</replaceable></term>
<listitem>
@ -278,7 +298,7 @@ ALTER DOMAIN zipcode SET SCHEMA customers;
<para>
<command>ALTER DOMAIN</command> conforms to the <acronym>SQL</acronym>
standard, except for the <literal>OWNER</>, <literal>SET SCHEMA</> and
standard, except for the <literal>OWNER</>, <literal>RENAME</literal>, <literal>SET SCHEMA</>, and
<literal>VALIDATE CONSTRAINT</> variants, which are
<productname>PostgreSQL</productname> extensions. The <literal>NOT VALID</>
clause of the <literal>ADD CONSTRAINT</> variant is also a

View File

@ -134,8 +134,9 @@ ExecRenameStmt(RenameStmt *stmt)
RenameTSConfiguration(stmt->object, stmt->newname);
break;
case OBJECT_DOMAIN:
case OBJECT_TYPE:
RenameType(stmt->object, stmt->newname);
RenameType(stmt);
break;
default:

View File

@ -3074,8 +3074,10 @@ GetDomainConstraints(Oid typeOid)
* Execute ALTER TYPE RENAME
*/
void
RenameType(List *names, const char *newTypeName)
RenameType(RenameStmt *stmt)
{
List *names = stmt->object;
const char *newTypeName = stmt->newname;
TypeName *typename;
Oid typeOid;
Relation rel;
@ -3099,6 +3101,13 @@ RenameType(List *names, const char *newTypeName)
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TYPE,
format_type_be(typeOid));
/* ALTER DOMAIN used on a non-domain? */
if (stmt->renameType == 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

View File

@ -6451,6 +6451,14 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
n->newname = $6;
$$ = (Node *)n;
}
| ALTER DOMAIN_P any_name RENAME TO name
{
RenameStmt *n = makeNode(RenameStmt);
n->renameType = OBJECT_DOMAIN;
n->object = $3;
n->newname = $6;
$$ = (Node *)n;
}
| ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
{
RenameStmt *n = makeNode(RenameStmt);

View File

@ -37,7 +37,7 @@ extern void AlterDomainDropConstraint(List *names, const char *constrName,
extern List *GetDomainConstraints(Oid typeOid);
extern void RenameType(List *names, const char *newTypeName);
extern void RenameType(RenameStmt *stmt);
extern void AlterTypeOwner(List *names, Oid newOwnerId);
extern void AlterTypeOwnerInternal(Oid typeOid, Oid newOwnerId,
bool hasDependEntry);

View File

@ -648,3 +648,10 @@ select array_elem_check(-1);
ERROR: value for domain orderedpair violates check constraint "orderedpair_check"
CONTEXT: PL/pgSQL function "array_elem_check" line 5 at assignment
drop function array_elem_check(int);
--
-- Renaming
--
create domain testdomain1 as int;
alter domain testdomain1 rename to testdomain2;
alter type testdomain2 rename to testdomain3; -- alter type also works
drop domain testdomain3;

View File

@ -483,3 +483,13 @@ select array_elem_check(3);
select array_elem_check(-1);
drop function array_elem_check(int);
--
-- Renaming
--
create domain testdomain1 as int;
alter domain testdomain1 rename to testdomain2;
alter type testdomain2 rename to testdomain3; -- alter type also works
drop domain testdomain3;