Skip to content

Commit daec989

Browse files
committed
ALTER DOMAIN OWNER, from Rod Taylor.
1 parent 1bf1597 commit daec989

File tree

6 files changed

+120
-19
lines changed

6 files changed

+120
-19
lines changed

doc/src/sgml/ref/alter_domain.sgml

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_domain.sgml,v 1.6 2002/12/06 16:40:13 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_domain.sgml,v 1.7 2003/01/06 00:31:44 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -29,6 +29,8 @@ ALTER DOMAIN <replaceable class="PARAMETER">domain</replaceable>
2929
ADD <replaceable class="PARAMETER">domain_constraint</replaceable>
3030
ALTER DOMAIN <replaceable class="PARAMETER">domain</replaceable>
3131
DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
32+
ALTER DOMAIN <replaceable class="PARAMETER">domain</replaceable>
33+
OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
3234
</synopsis>
3335

3436
<refsect2 id="R2-SQL-ALTERDOMAIN-1">
@@ -73,7 +75,7 @@ ALTER DOMAIN <replaceable class="PARAMETER">domain</replaceable>
7375
<term>CASCADE</term>
7476
<listitem>
7577
<para>
76-
Automatically drop objects that depend constraint.
78+
Automatically drop objects that depend on the constraint.
7779
</para>
7880
</listitem>
7981
</varlistentry>
@@ -88,6 +90,15 @@ ALTER DOMAIN <replaceable class="PARAMETER">domain</replaceable>
8890
</listitem>
8991
</varlistentry>
9092

93+
<varlistentry>
94+
<term><replaceable class="PARAMETER">new_owner</replaceable></term>
95+
<listitem>
96+
<para>
97+
The user name of the new owner of the domain.
98+
</para>
99+
</listitem>
100+
</varlistentry>
101+
91102
</variablelist>
92103
</para>
93104
</refsect2>
@@ -141,9 +152,9 @@ ALTER DOMAIN <replaceable class="PARAMETER">domain</replaceable>
141152
<term>SET/DROP DEFAULT</term>
142153
<listitem>
143154
<para>
144-
These forms set or remove the default value for a column. Note
155+
These forms set or remove the default value for a domain. Note
145156
that defaults only apply to subsequent <command>INSERT</command>
146-
commands; they do not cause rows already in a table using the domain.
157+
commands; they do not affect rows already in a table using the domain.
147158
</para>
148159
</listitem>
149160
</varlistentry>
@@ -154,8 +165,7 @@ ALTER DOMAIN <replaceable class="PARAMETER">domain</replaceable>
154165
<para>
155166
These forms change whether a domain is marked to allow NULL
156167
values or to reject NULL values. You may only <literal>SET NOT NULL</>
157-
when the tables using the domain contain no null values in the domain
158-
based column.
168+
when the columns using the domain contain no null values.
159169
</para>
160170
</listitem>
161171
</varlistentry>
@@ -164,8 +174,10 @@ ALTER DOMAIN <replaceable class="PARAMETER">domain</replaceable>
164174
<term>ADD <replaceable class="PARAMETER">domain_constraint</replaceable></term>
165175
<listitem>
166176
<para>
167-
This form adds a new constraint to a table using the same syntax as
168-
<xref linkend="SQL-CREATEDOMAIN" endterm="SQL-CREATEDOMAIN-TITLE">.
177+
This form adds a new constraint to a domain using the same syntax as
178+
<xref linkend="SQL-CREATEDOMAIN" endterm="SQL-CREATEDOMAIN-TITLE">.
179+
This will only succeed if all columns using the domain satisfy the
180+
new constraint.
169181
</para>
170182
</listitem>
171183
</varlistentry>
@@ -179,11 +191,19 @@ ALTER DOMAIN <replaceable class="PARAMETER">domain</replaceable>
179191
</listitem>
180192
</varlistentry>
181193

194+
<varlistentry>
195+
<term>OWNER</term>
196+
<listitem>
197+
<para>
198+
This form changes the owner of the domain to the specified user.
199+
</para>
200+
</listitem>
201+
</varlistentry>
182202
</variablelist>
183203

184204
<para>
185205
You must own the domain to use <command>ALTER DOMAIN</>; except for
186-
<command>ALTER TABLE OWNER</>, which may only be executed by a superuser.
206+
<command>ALTER DOMAIN OWNER</>, which may only be executed by a superuser.
187207
</para>
188208
</refsect1>
189209

src/backend/commands/typecmds.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.26 2003/01/04 00:46:08 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.27 2003/01/06 00:31:44 tgl Exp $
1212
*
1313
* DESCRIPTION
1414
* The "DefineFoo" routines take the parse tree and pick out the
@@ -1673,3 +1673,61 @@ domainAddConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid,
16731673
*/
16741674
return ccbin;
16751675
}
1676+
1677+
/*
1678+
* ALTER DOMAIN .. OWNER TO
1679+
*
1680+
* Eventually this should allow changing ownership of other kinds of types,
1681+
* but some thought must be given to handling complex types. (A table's
1682+
* rowtype probably shouldn't be allowed as target, but what of a standalone
1683+
* composite type?)
1684+
*
1685+
* Assumes that permission checks have been completed earlier.
1686+
*/
1687+
void
1688+
AlterTypeOwner(List *names, AclId newOwnerSysId)
1689+
{
1690+
TypeName *typename;
1691+
Oid typeOid;
1692+
Relation rel;
1693+
HeapTuple tup;
1694+
Form_pg_type typTup;
1695+
1696+
/* Make a TypeName so we can use standard type lookup machinery */
1697+
typename = makeNode(TypeName);
1698+
typename->names = names;
1699+
typename->typmod = -1;
1700+
typename->arrayBounds = NIL;
1701+
1702+
/* Lock the type table */
1703+
rel = heap_openr(TypeRelationName, RowExclusiveLock);
1704+
1705+
/* Use LookupTypeName here so that shell types can be processed (why?) */
1706+
typeOid = LookupTypeName(typename);
1707+
if (!OidIsValid(typeOid))
1708+
elog(ERROR, "Type \"%s\" does not exist",
1709+
TypeNameToString(typename));
1710+
1711+
tup = SearchSysCacheCopy(TYPEOID,
1712+
ObjectIdGetDatum(typeOid),
1713+
0, 0, 0);
1714+
if (!HeapTupleIsValid(tup))
1715+
elog(ERROR, "AlterDomain: type \"%s\" does not exist",
1716+
TypeNameToString(typename));
1717+
typTup = (Form_pg_type) GETSTRUCT(tup);
1718+
1719+
/* Check that this is actually a domain */
1720+
if (typTup->typtype != 'd')
1721+
elog(ERROR, "%s is not a domain",
1722+
TypeNameToString(typename));
1723+
1724+
/* Modify the owner --- okay to scribble on typTup because it's a copy */
1725+
typTup->typowner = newOwnerSysId;
1726+
1727+
simple_heap_update(rel, &tup->t_self, tup);
1728+
1729+
CatalogUpdateIndexes(rel, tup);
1730+
1731+
/* Clean up */
1732+
heap_close(rel, RowExclusiveLock);
1733+
}

src/backend/parser/gram.y

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.389 2002/12/30 15:31:47 momjian Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.390 2003/01/06 00:31:44 tgl Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -3764,6 +3764,15 @@ AlterDomainStmt:
37643764
n->behavior = $7;
37653765
$$ = (Node *)n;
37663766
}
3767+
/* ALTER DOMAIN <domain> OWNER TO UserId */
3768+
| ALTER DOMAIN_P any_name OWNER TO UserId
3769+
{
3770+
AlterDomainStmt *n = makeNode(AlterDomainStmt);
3771+
n->subtype = 'U';
3772+
n->typename = $3;
3773+
n->name = $6;
3774+
$$ = (Node *)n;
3775+
}
37673776
;
37683777

37693778
opt_as: AS {}

src/backend/tcop/utility.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.187 2002/12/30 18:42:16 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.188 2003/01/06 00:31:44 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -439,11 +439,11 @@ ProcessUtility(Node *parsetree,
439439
stmt->newname); /* new att name */
440440
break;
441441
case RENAME_RULE:
442-
elog(ERROR, "ProcessUtility: Invalid target for RENAME: %d",
442+
elog(ERROR, "ProcessUtility: Invalid type for RENAME: %d",
443443
stmt->renameType);
444444
break;
445445
default:
446-
elog(ERROR, "ProcessUtility: Invalid target for RENAME: %d",
446+
elog(ERROR, "ProcessUtility: Invalid type for RENAME: %d",
447447
stmt->renameType);
448448
}
449449
}
@@ -553,7 +553,8 @@ ProcessUtility(Node *parsetree,
553553
get_usesysid(stmt->name));
554554
break;
555555
default: /* oops */
556-
elog(ERROR, "T_AlterTableStmt: unknown subtype");
556+
elog(ERROR, "ProcessUtility: Invalid type for AlterTableStmt: %d",
557+
stmt->subtype);
557558
break;
558559
}
559560
}
@@ -595,8 +596,17 @@ ProcessUtility(Node *parsetree,
595596
stmt->name,
596597
stmt->behavior);
597598
break;
599+
case 'U': /* OWNER TO */
600+
/* check that we are the superuser */
601+
if (!superuser())
602+
elog(ERROR, "ALTER DOMAIN: permission denied");
603+
/* get_usesysid raises an error if no such user */
604+
AlterTypeOwner(stmt->typename,
605+
get_usesysid(stmt->name));
606+
break;
598607
default: /* oops */
599-
elog(ERROR, "T_AlterDomainStmt: unknown subtype");
608+
elog(ERROR, "ProcessUtility: Invalid type for AlterDomainStmt: %d",
609+
stmt->subtype);
600610
break;
601611
}
602612
}

src/include/commands/typecmds.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: typecmds.h,v 1.2 2002/12/10 16:12:53 tgl Exp $
10+
* $Id: typecmds.h,v 1.3 2003/01/06 00:31:44 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#ifndef TYPECMDS_H
1515
#define TYPECMDS_H
1616

17+
#include "miscadmin.h"
1718
#include "nodes/parsenodes.h"
1819

20+
1921
#define DEFAULT_TYPDELIM ','
2022

2123
extern void DefineType(List *names, List *parameters);
@@ -31,4 +33,6 @@ extern void AlterDomainAddConstraint(List *names, Node *constr);
3133
extern void AlterDomainDropConstraint(List *names, const char *constrName,
3234
DropBehavior behavior);
3335

36+
extern void AlterTypeOwner(List *names, AclId newOwnerSysId);
37+
3438
#endif /* TYPECMDS_H */

src/include/nodes/parsenodes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: parsenodes.h,v 1.224 2002/12/30 15:31:51 momjian Exp $
10+
* $Id: parsenodes.h,v 1.225 2003/01/06 00:31:45 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -711,7 +711,7 @@ typedef struct AlterTableStmt
711711
* Alter Domain
712712
*
713713
* The fields are used in different ways by the different variants of
714-
* this command. Subtypes should match AlterTable subtypes
714+
* this command. Subtypes should match AlterTable subtypes where possible.
715715
* ----------------------
716716
*/
717717
typedef struct AlterDomainStmt

0 commit comments

Comments
 (0)