summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/man7/ALTER_DOMAIN.7
blob: 9e626243b9dddc2b72e5a59d360879e1af64ad0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'\" t
.\"     Title: ALTER DOMAIN
.\"    Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\"      Date: 2023
.\"    Manual: PostgreSQL 15.5 Documentation
.\"    Source: PostgreSQL 15.5
.\"  Language: English
.\"
.TH "ALTER DOMAIN" "7" "2023" "PostgreSQL 15.5" "PostgreSQL 15.5 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
ALTER_DOMAIN \- change the definition of a domain
.SH "SYNOPSIS"
.sp
.nf
ALTER DOMAIN \fIname\fR
    { SET DEFAULT \fIexpression\fR | DROP DEFAULT }
ALTER DOMAIN \fIname\fR
    { SET | DROP } NOT NULL
ALTER DOMAIN \fIname\fR
    ADD \fIdomain_constraint\fR [ NOT VALID ]
ALTER DOMAIN \fIname\fR
    DROP CONSTRAINT [ IF EXISTS ] \fIconstraint_name\fR [ RESTRICT | CASCADE ]
ALTER DOMAIN \fIname\fR
     RENAME CONSTRAINT \fIconstraint_name\fR TO \fInew_constraint_name\fR
ALTER DOMAIN \fIname\fR
    VALIDATE CONSTRAINT \fIconstraint_name\fR
ALTER DOMAIN \fIname\fR
    OWNER TO { \fInew_owner\fR | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
ALTER DOMAIN \fIname\fR
    RENAME TO \fInew_name\fR
ALTER DOMAIN \fIname\fR
    SET SCHEMA \fInew_schema\fR
.fi
.SH "DESCRIPTION"
.PP
\fBALTER DOMAIN\fR
changes the definition of an existing domain\&. There are several sub\-forms:
.PP
SET/DROP DEFAULT
.RS 4
These forms set or remove the default value for a domain\&. Note that defaults only apply to subsequent
\fBINSERT\fR
commands; they do not affect rows already in a table using the domain\&.
.RE
.PP
SET/DROP NOT NULL
.RS 4
These forms change whether a domain is marked to allow NULL values or to reject NULL values\&. You can only
SET NOT NULL
when the columns using the domain contain no null values\&.
.RE
.PP
ADD \fIdomain_constraint\fR [ NOT VALID ]
.RS 4
This form adds a new constraint to a domain using the same syntax as
\fBCREATE DOMAIN\fR\&. When a new constraint is added to a domain, all columns using that domain will be checked against the newly added constraint\&. These checks can be suppressed by adding the new constraint using the
NOT VALID
option; the constraint can later be made valid using
\fBALTER DOMAIN \&.\&.\&. VALIDATE CONSTRAINT\fR\&. Newly inserted or updated rows are always checked against all constraints, even those marked
NOT VALID\&.
NOT VALID
is only accepted for
CHECK
constraints\&.
.RE
.PP
DROP CONSTRAINT [ IF EXISTS ]
.RS 4
This form drops constraints on a domain\&. If
IF EXISTS
is specified and the constraint does not exist, no error is thrown\&. In this case a notice is issued instead\&.
.RE
.PP
RENAME CONSTRAINT
.RS 4
This form changes the name of a constraint on a domain\&.
.RE
.PP
VALIDATE CONSTRAINT
.RS 4
This form validates a constraint previously added as
NOT VALID, that is, it verifies that all values in table columns of the domain type satisfy the specified constraint\&.
.RE
.PP
OWNER
.RS 4
This form changes the owner of the domain to the specified user\&.
.RE
.PP
RENAME
.RS 4
This form changes the name of the domain\&.
.RE
.PP
SET SCHEMA
.RS 4
This form changes the schema of the domain\&. Any constraints associated with the domain are moved into the new schema as well\&.
.RE
.PP
You must own the domain to use
\fBALTER DOMAIN\fR\&. To change the schema of a domain, you must also have
CREATE
privilege on the new schema\&. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have
CREATE
privilege on the domain\*(Aqs schema\&. (These restrictions enforce that altering the owner doesn\*(Aqt do anything you couldn\*(Aqt do by dropping and recreating the domain\&. However, a superuser can alter ownership of any domain anyway\&.)
.SH "PARAMETERS"
.PP
.PP
\fIname\fR
.RS 4
The name (possibly schema\-qualified) of an existing domain to alter\&.
.RE
.PP
\fIdomain_constraint\fR
.RS 4
New domain constraint for the domain\&.
.RE
.PP
\fIconstraint_name\fR
.RS 4
Name of an existing constraint to drop or rename\&.
.RE
.PP
NOT VALID
.RS 4
Do not verify existing stored data for constraint validity\&.
.RE
.PP
CASCADE
.RS 4
Automatically drop objects that depend on the constraint, and in turn all objects that depend on those objects (see
Section\ \&5.14)\&.
.RE
.PP
RESTRICT
.RS 4
Refuse to drop the constraint if there are any dependent objects\&. This is the default behavior\&.
.RE
.PP
\fInew_name\fR
.RS 4
The new name for the domain\&.
.RE
.PP
\fInew_constraint_name\fR
.RS 4
The new name for the constraint\&.
.RE
.PP
\fInew_owner\fR
.RS 4
The user name of the new owner of the domain\&.
.RE
.PP
\fInew_schema\fR
.RS 4
The new schema for the domain\&.
.RE
.SH "NOTES"
.PP
Although
\fBALTER DOMAIN ADD CONSTRAINT\fR
attempts to verify that existing stored data satisfies the new constraint, this check is not bulletproof, because the command cannot
\(lqsee\(rq
table rows that are newly inserted or updated and not yet committed\&. If there is a hazard that concurrent operations might insert bad data, the way to proceed is to add the constraint using the
NOT VALID
option, commit that command, wait until all transactions started before that commit have finished, and then issue
\fBALTER DOMAIN VALIDATE CONSTRAINT\fR
to search for data violating the constraint\&. This method is reliable because once the constraint is committed, all new transactions are guaranteed to enforce it against new values of the domain type\&.
.PP
Currently,
\fBALTER DOMAIN ADD CONSTRAINT\fR,
\fBALTER DOMAIN VALIDATE CONSTRAINT\fR, and
\fBALTER DOMAIN SET NOT NULL\fR
will fail if the named domain or any derived domain is used within a container\-type column (a composite, array, or range column) in any table in the database\&. They should eventually be improved to be able to verify the new constraint for such nested values\&.
.SH "EXAMPLES"
.PP
To add a
NOT NULL
constraint to a domain:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER DOMAIN zipcode SET NOT NULL;
.fi
.if n \{\
.RE
.\}
.sp
To remove a
NOT NULL
constraint from a domain:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER DOMAIN zipcode DROP NOT NULL;
.fi
.if n \{\
.RE
.\}
.PP
To add a check constraint to a domain:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER DOMAIN zipcode ADD CONSTRAINT zipchk CHECK (char_length(VALUE) = 5);
.fi
.if n \{\
.RE
.\}
.PP
To remove a check constraint from a domain:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER DOMAIN zipcode DROP CONSTRAINT zipchk;
.fi
.if n \{\
.RE
.\}
.PP
To rename a check constraint on a domain:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check;
.fi
.if n \{\
.RE
.\}
.PP
To move the domain into a different schema:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER DOMAIN zipcode SET SCHEMA customers;
.fi
.if n \{\
.RE
.\}
.SH "COMPATIBILITY"
.PP
\fBALTER DOMAIN\fR
conforms to the
SQL
standard, except for the
OWNER,
RENAME,
SET SCHEMA, and
VALIDATE CONSTRAINT
variants, which are
PostgreSQL
extensions\&. The
NOT VALID
clause of the
ADD CONSTRAINT
variant is also a
PostgreSQL
extension\&.
.SH "SEE ALSO"
CREATE DOMAIN (\fBCREATE_DOMAIN\fR(7)), DROP DOMAIN (\fBDROP_DOMAIN\fR(7))