~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/man7/ALTER_TABLE.7

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'\" t
 
2
.\"     Title: ALTER TABLE
 
3
.\"    Author: The PostgreSQL Global Development Group
 
4
.\" Generator: DocBook XSL Stylesheets v1.75.1 <http://docbook.sf.net/>
 
5
.\"      Date: 2011-04-27
 
6
.\"    Manual: PostgreSQL 9.1beta1 Documentation
 
7
.\"    Source: PostgreSQL 9.1beta1
 
8
.\"  Language: English
 
9
.\"
 
10
.TH "ALTER TABLE" "7" "2011-04-27" "PostgreSQL 9.1beta1" "PostgreSQL 9.1beta1 Documentation"
 
11
.\" -----------------------------------------------------------------
 
12
.\" * set default formatting
 
13
.\" -----------------------------------------------------------------
 
14
.\" disable hyphenation
 
15
.nh
 
16
.\" disable justification (adjust text to left margin only)
 
17
.ad l
 
18
.\" -----------------------------------------------------------------
 
19
.\" * MAIN CONTENT STARTS HERE *
 
20
.\" -----------------------------------------------------------------
 
21
.SH "NAME"
 
22
ALTER_TABLE \- change the definition of a table
 
23
.\" ALTER TABLE
 
24
.SH "SYNOPSIS"
 
25
.sp
 
26
.nf
 
27
ALTER TABLE [ ONLY ] \fIname\fR [ * ]
 
28
    \fIaction\fR [, \&.\&.\&. ]
 
29
ALTER TABLE [ ONLY ] \fIname\fR [ * ]
 
30
    RENAME [ COLUMN ] \fIcolumn\fR TO \fInew_column\fR
 
31
ALTER TABLE \fIname\fR
 
32
    RENAME TO \fInew_name\fR
 
33
ALTER TABLE \fIname\fR
 
34
    SET SCHEMA \fInew_schema\fR
 
35
 
 
36
where \fIaction\fR is one of:
 
37
 
 
38
    ADD [ COLUMN ] \fIcolumn\fR \fIdata_type\fR [ COLLATE \fIcollation\fR ] [ \fIcolumn_constraint\fR [ \&.\&.\&. ] ]
 
39
    DROP [ COLUMN ] [ IF EXISTS ] \fIcolumn\fR [ RESTRICT | CASCADE ]
 
40
    ALTER [ COLUMN ] \fIcolumn\fR [ SET DATA ] TYPE \fIdata_type\fR [ COLLATE \fIcollation\fR ] [ USING \fIexpression\fR ]
 
41
    ALTER [ COLUMN ] \fIcolumn\fR SET DEFAULT \fIexpression\fR
 
42
    ALTER [ COLUMN ] \fIcolumn\fR DROP DEFAULT
 
43
    ALTER [ COLUMN ] \fIcolumn\fR { SET | DROP } NOT NULL
 
44
    ALTER [ COLUMN ] \fIcolumn\fR SET STATISTICS \fIinteger\fR
 
45
    ALTER [ COLUMN ] \fIcolumn\fR SET ( \fIattribute_option\fR = \fIvalue\fR [, \&.\&.\&. ] )
 
46
    ALTER [ COLUMN ] \fIcolumn\fR RESET ( \fIattribute_option\fR [, \&.\&.\&. ] )
 
47
    ALTER [ COLUMN ] \fIcolumn\fR SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
 
48
    ADD \fItable_constraint\fR
 
49
    ADD \fItable_constraint_using_index\fR
 
50
    ADD \fItable_constraint\fR [ NOT VALID ]
 
51
    VALIDATE CONSTRAINT \fIconstraint_name\fR
 
52
    DROP CONSTRAINT [ IF EXISTS ]  \fIconstraint_name\fR [ RESTRICT | CASCADE ]
 
53
    DISABLE TRIGGER [ \fItrigger_name\fR | ALL | USER ]
 
54
    ENABLE TRIGGER [ \fItrigger_name\fR | ALL | USER ]
 
55
    ENABLE REPLICA TRIGGER \fItrigger_name\fR
 
56
    ENABLE ALWAYS TRIGGER \fItrigger_name\fR
 
57
    DISABLE RULE \fIrewrite_rule_name\fR
 
58
    ENABLE RULE \fIrewrite_rule_name\fR
 
59
    ENABLE REPLICA RULE \fIrewrite_rule_name\fR
 
60
    ENABLE ALWAYS RULE \fIrewrite_rule_name\fR
 
61
    CLUSTER ON \fIindex_name\fR
 
62
    SET WITHOUT CLUSTER
 
63
    SET WITH OIDS
 
64
    SET WITHOUT OIDS
 
65
    SET ( \fIstorage_parameter\fR = \fIvalue\fR [, \&.\&.\&. ] )
 
66
    RESET ( \fIstorage_parameter\fR [, \&.\&.\&. ] )
 
67
    INHERIT \fIparent_table\fR
 
68
    NO INHERIT \fIparent_table\fR
 
69
    OF \fItype_name\fR
 
70
    NOT OF
 
71
    OWNER TO \fInew_owner\fR
 
72
    SET TABLESPACE \fInew_tablespace\fR
 
73
 
 
74
and \fItable_constraint_using_index\fR is:
 
75
 
 
76
    [ CONSTRAINT \fIconstraint_name\fR ]
 
77
    { UNIQUE | PRIMARY KEY } USING INDEX \fIindex_name\fR
 
78
    [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
 
79
.fi
 
80
.SH "DESCRIPTION"
 
81
.PP
 
82
ALTER TABLE
 
83
changes the definition of an existing table\&. There are several subforms:
 
84
.PP
 
85
ADD COLUMN
 
86
.RS 4
 
87
This form adds a new column to the table, using the same syntax as
 
88
CREATE TABLE (\fBCREATE_TABLE\fR(7))\&.
 
89
.RE
 
90
.PP
 
91
DROP COLUMN [ IF EXISTS ]
 
92
.RS 4
 
93
This form drops a column from a table\&. Indexes and table constraints involving the column will be automatically dropped as well\&. You will need to say
 
94
CASCADE
 
95
if anything outside the table depends on the column, for example, foreign key references or views\&. If
 
96
IF EXISTS
 
97
is specified and the column does not exist, no error is thrown\&. In this case a notice is issued instead\&.
 
98
.RE
 
99
.PP
 
100
SET DATA TYPE
 
101
.RS 4
 
102
This form changes the type of a column of a table\&. Indexes and simple table constraints involving the column will be automatically converted to use the new column type by reparsing the originally supplied expression\&. The optional
 
103
COLLATE
 
104
clause specifies a collation for the new column; if omitted, the collation is the default for the new column type\&. The optional
 
105
USING
 
106
clause specifies how to compute the new column value from the old; if omitted, the default conversion is the same as an assignment cast from old data type to new\&. A
 
107
USING
 
108
clause must be provided if there is no implicit or assignment cast from old to new type\&.
 
109
.RE
 
110
.PP
 
111
SET/DROP DEFAULT
 
112
.RS 4
 
113
These forms set or remove the default value for a column\&. The default values only apply to subsequent
 
114
INSERT
 
115
commands; they do not cause rows already in the table to change\&. Defaults can also be created for views, in which case they are inserted into
 
116
INSERT
 
117
statements on the view before the view\(aqs
 
118
ON INSERT
 
119
rule is applied\&.
 
120
.RE
 
121
.PP
 
122
SET/DROP NOT NULL
 
123
.RS 4
 
124
These forms change whether a column is marked to allow null values or to reject null values\&. You can only use
 
125
SET NOT NULL
 
126
when the column contains no null values\&.
 
127
.RE
 
128
.PP
 
129
SET STATISTICS
 
130
.RS 4
 
131
This form sets the per\-column statistics\-gathering target for subsequent
 
132
\fBANALYZE\fR(7)
 
133
operations\&. The target can be set in the range 0 to 10000; alternatively, set it to \-1 to revert to using the system default statistics target (default_statistics_target)\&. For more information on the use of statistics by the
 
134
PostgreSQL
 
135
query planner, refer to
 
136
Section 14.2, \(lqStatistics Used by the Planner\(rq, in the documentation\&.
 
137
.RE
 
138
.PP
 
139
SET ( \fIattribute_option\fR = \fIvalue\fR [, \&.\&.\&. ] ), RESET ( \fIattribute_option\fR [, \&.\&.\&. ] )
 
140
.RS 4
 
141
This form sets or resets per\-attribute options\&. Currently, the only defined per\-attribute options are
 
142
n_distinct
 
143
and
 
144
n_distinct_inherited, which override the number\-of\-distinct\-values estimates made by subsequent
 
145
\fBANALYZE\fR(7)
 
146
operations\&.
 
147
n_distinct
 
148
affects the statistics for the table itself, while
 
149
n_distinct_inherited
 
150
affects the statistics gathered for the table plus its inheritance children\&. When set to a positive value,
 
151
ANALYZE
 
152
will assume that the column contains exactly the specified number of distinct nonnull values\&. When set to a negative value, which must be greater than or equal to \-1,
 
153
ANALYZE
 
154
will assume that the number of distinct nonnull values in the column is linear in the size of the table; the exact count is to be computed by multiplying the estimated table size by the absolute value of the given number\&. For example, a value of \-1 implies that all values in the column are distinct, while a value of \-0\&.5 implies that each value appears twice on the average\&. This can be useful when the size of the table changes over time, since the multiplication by the number of rows in the table is not performed until query planning time\&. Specify a value of 0 to revert to estimating the number of distinct values normally\&. For more information on the use of statistics by the
 
155
PostgreSQL
 
156
query planner, refer to
 
157
Section 14.2, \(lqStatistics Used by the Planner\(rq, in the documentation\&.
 
158
.RE
 
159
.PP
 
160
SET STORAGE
 
161
.RS 4
 
162
.\" TOAST: per-column storage settings
 
163
This form sets the storage mode for a column\&. This controls whether this column is held inline or in a secondary
 
164
TOAST
 
165
table, and whether the data should be compressed or not\&.
 
166
PLAIN
 
167
must be used for fixed\-length values such as
 
168
integer
 
169
and is inline, uncompressed\&.
 
170
MAIN
 
171
is for inline, compressible data\&.
 
172
EXTERNAL
 
173
is for external, uncompressed data, and
 
174
EXTENDED
 
175
is for external, compressed data\&.
 
176
EXTENDED
 
177
is the default for most data types that support non\-PLAIN
 
178
storage\&. Use of
 
179
EXTERNAL
 
180
will make substring operations on very large
 
181
text
 
182
and
 
183
bytea
 
184
values run faster, at the penalty of increased storage space\&. Note that
 
185
SET STORAGE
 
186
doesn\(aqt itself change anything in the table, it just sets the strategy to be pursued during future table updates\&. See
 
187
Section 55.2, \(lqTOAST\(rq, in the documentation
 
188
for more information\&.
 
189
.RE
 
190
.PP
 
191
ADD \fItable_constraint\fR [ NOT VALID ]
 
192
.RS 4
 
193
This form adds a new constraint to a table using the same syntax as
 
194
CREATE TABLE (\fBCREATE_TABLE\fR(7))\&. Newly added foreign key constraints can also be defined as
 
195
NOT VALID
 
196
to avoid the potentially lengthy initial check that must otherwise be performed\&. Constraint checks are skipped at create table time, so
 
197
CREATE TABLE (\fBCREATE_TABLE\fR(7))
 
198
does not contain this option\&.
 
199
.RE
 
200
.PP
 
201
VALIDATE CONSTRAINT
 
202
.RS 4
 
203
This form validates a foreign key constraint that was previously created as
 
204
NOT VALID\&. Constraints already marked valid do not cause an error response\&.
 
205
.RE
 
206
.PP
 
207
ADD \fItable_constraint_using_index\fR
 
208
.RS 4
 
209
This form adds a new
 
210
PRIMARY KEY
 
211
or
 
212
UNIQUE
 
213
constraint to a table based on an existing unique index\&. All the columns of the index will be included in the constraint\&.
 
214
.sp
 
215
The index cannot have expression columns nor be a partial index\&. Also, it must be a b\-tree index with default sort ordering\&. These restrictions ensure that the index is equivalent to one that would be built by a regular
 
216
ADD PRIMARY KEY
 
217
or
 
218
ADD UNIQUE
 
219
command\&.
 
220
.sp
 
221
If
 
222
PRIMARY KEY
 
223
is specified, and the index\(aqs columns are not already marked
 
224
NOT NULL, then this command will attempt to do
 
225
ALTER COLUMN SET NOT NULL
 
226
against each such column\&. That requires a full table scan to verify the column(s) contain no nulls\&. In all other cases, this is a fast operation\&.
 
227
.sp
 
228
If a constraint name is provided then the index will be renamed to match the constraint name\&. Otherwise the constraint will be named the same as the index\&.
 
229
.sp
 
230
After this command is executed, the index is
 
231
\(lqowned\(rq
 
232
by the constraint, in the same way as if the index had been built by a regular
 
233
ADD PRIMARY KEY
 
234
or
 
235
ADD UNIQUE
 
236
command\&. In particular, dropping the constraint will make the index disappear too\&.
 
237
.if n \{\
 
238
.sp
 
239
.\}
 
240
.RS 4
 
241
.it 1 an-trap
 
242
.nr an-no-space-flag 1
 
243
.nr an-break-flag 1
 
244
.br
 
245
.ps +1
 
246
\fBNote\fR
 
247
.ps -1
 
248
.br
 
249
Adding a constraint using an existing index can be helpful in situations where a new constraint needs to be added without blocking table updates for a long time\&. To do that, create the index using
 
250
CREATE INDEX CONCURRENTLY, and then install it as an official constraint using this syntax\&. See the example below\&.
 
251
.sp .5v
 
252
.RE
 
253
.RE
 
254
.PP
 
255
DROP CONSTRAINT [ IF EXISTS ]
 
256
.RS 4
 
257
This form drops the specified constraint on a table\&. If
 
258
IF EXISTS
 
259
is specified and the constraint does not exist, no error is thrown\&. In this case a notice is issued instead\&.
 
260
.RE
 
261
.PP
 
262
DISABLE/ENABLE [ REPLICA | ALWAYS ] TRIGGER
 
263
.RS 4
 
264
These forms configure the firing of trigger(s) belonging to the table\&. A disabled trigger is still known to the system, but is not executed when its triggering event occurs\&. For a deferred trigger, the enable status is checked when the event occurs, not when the trigger function is actually executed\&. One can disable or enable a single trigger specified by name, or all triggers on the table, or only user triggers (this option excludes internally generated constraint triggers such as those that are used to implement foreign key constraints or deferrable uniqueness and exclusion constraints)\&. Disabling or enabling internally generated constraint triggers requires superuser privileges; it should be done with caution since of course the integrity of the constraint cannot be guaranteed if the triggers are not executed\&. The trigger firing mechanism is also affected by the configuration variable
 
265
session_replication_role\&. Simply enabled triggers will fire when the replication role is
 
266
\(lqorigin\(rq
 
267
(the default) or
 
268
\(lqlocal\(rq\&. Triggers configured as
 
269
ENABLE REPLICA
 
270
will only fire if the session is in
 
271
\(lqreplica\(rq
 
272
mode, and triggers configured as
 
273
ENABLE ALWAYS
 
274
will fire regardless of the current replication mode\&.
 
275
.RE
 
276
.PP
 
277
DISABLE/ENABLE [ REPLICA | ALWAYS ] RULE
 
278
.RS 4
 
279
These forms configure the firing of rewrite rules belonging to the table\&. A disabled rule is still known to the system, but is not applied during query rewriting\&. The semantics are as for disabled/enabled triggers\&. This configuration is ignored for
 
280
ON SELECT
 
281
rules, which are always applied in order to keep views working even if the current session is in a non\-default replication role\&.
 
282
.RE
 
283
.PP
 
284
CLUSTER
 
285
.RS 4
 
286
This form selects the default index for future
 
287
\fBCLUSTER\fR(7)
 
288
operations\&. It does not actually re\-cluster the table\&.
 
289
.RE
 
290
.PP
 
291
SET WITHOUT CLUSTER
 
292
.RS 4
 
293
This form removes the most recently used
 
294
\fBCLUSTER\fR(7)
 
295
index specification from the table\&. This affects future cluster operations that don\(aqt specify an index\&.
 
296
.RE
 
297
.PP
 
298
SET WITH OIDS
 
299
.RS 4
 
300
This form adds an
 
301
oid
 
302
system column to the table (see
 
303
Section 5.4, \(lqSystem Columns\(rq, in the documentation)\&. It does nothing if the table already has OIDs\&.
 
304
.sp
 
305
Note that this is not equivalent to
 
306
ADD COLUMN oid oid; that would add a normal column that happened to be named
 
307
oid, not a system column\&.
 
308
.RE
 
309
.PP
 
310
SET WITHOUT OIDS
 
311
.RS 4
 
312
This form removes the
 
313
oid
 
314
system column from the table\&. This is exactly equivalent to
 
315
DROP COLUMN oid RESTRICT, except that it will not complain if there is already no
 
316
oid
 
317
column\&.
 
318
.RE
 
319
.PP
 
320
SET ( \fIstorage_parameter\fR = \fIvalue\fR [, \&.\&.\&. ] )
 
321
.RS 4
 
322
This form changes one or more storage parameters for the table\&. See
 
323
Storage Parameters
 
324
for details on the available parameters\&. Note that the table contents will not be modified immediately by this command; depending on the parameter you might need to rewrite the table to get the desired effects\&. That can be done with
 
325
VACUUM FULL,
 
326
\fBCLUSTER\fR(7)
 
327
or one of the forms of
 
328
ALTER TABLE
 
329
that forces a table rewrite\&.
 
330
.if n \{\
 
331
.sp
 
332
.\}
 
333
.RS 4
 
334
.it 1 an-trap
 
335
.nr an-no-space-flag 1
 
336
.nr an-break-flag 1
 
337
.br
 
338
.ps +1
 
339
\fBNote\fR
 
340
.ps -1
 
341
.br
 
342
While
 
343
CREATE TABLE
 
344
allows
 
345
OIDS
 
346
to be specified in the
 
347
WITH (\fIstorage_parameter\fR)
 
348
syntax,
 
349
ALTER TABLE
 
350
does not treat
 
351
OIDS
 
352
as a storage parameter\&. Instead use the
 
353
SET WITH OIDS
 
354
and
 
355
SET WITHOUT OIDS
 
356
forms to change OID status\&.
 
357
.sp .5v
 
358
.RE
 
359
.RE
 
360
.PP
 
361
RESET ( \fIstorage_parameter\fR [, \&.\&.\&. ] )
 
362
.RS 4
 
363
This form resets one or more storage parameters to their defaults\&. As with
 
364
SET, a table rewrite might be needed to update the table entirely\&.
 
365
.RE
 
366
.PP
 
367
INHERIT \fIparent_table\fR
 
368
.RS 4
 
369
This form adds the target table as a new child of the specified parent table\&. Subsequently, queries against the parent will include records of the target table\&. To be added as a child, the target table must already contain all the same columns as the parent (it could have additional columns, too)\&. The columns must have matching data types, and if they have
 
370
NOT NULL
 
371
constraints in the parent then they must also have
 
372
NOT NULL
 
373
constraints in the child\&.
 
374
.sp
 
375
There must also be matching child\-table constraints for all
 
376
CHECK
 
377
constraints of the parent\&. Currently
 
378
UNIQUE,
 
379
PRIMARY KEY, and
 
380
FOREIGN KEY
 
381
constraints are not considered, but this might change in the future\&.
 
382
.RE
 
383
.PP
 
384
NO INHERIT \fIparent_table\fR
 
385
.RS 4
 
386
This form removes the target table from the list of children of the specified parent table\&. Queries against the parent table will no longer include records drawn from the target table\&.
 
387
.RE
 
388
.PP
 
389
OF \fItype_name\fR
 
390
.RS 4
 
391
This form links the table to a composite type as though
 
392
CREATE TABLE OF
 
393
had formed it\&. The table\(aqs list of column names and types must precisely match that of the composite type; the presence of an
 
394
oid
 
395
system column is permitted to differ\&. The table must not inherit from any other table\&. These restrictions ensure that
 
396
CREATE TABLE OF
 
397
would permit an equivalent table definition\&.
 
398
.RE
 
399
.PP
 
400
NOT OF
 
401
.RS 4
 
402
This form dissociates a typed table from its type\&.
 
403
.RE
 
404
.PP
 
405
OWNER
 
406
.RS 4
 
407
This form changes the owner of the table, sequence, or view to the specified user\&.
 
408
.RE
 
409
.PP
 
410
SET TABLESPACE
 
411
.RS 4
 
412
This form changes the table\(aqs tablespace to the specified tablespace and moves the data file(s) associated with the table to the new tablespace\&. Indexes on the table, if any, are not moved; but they can be moved separately with additional
 
413
SET TABLESPACE
 
414
commands\&. See also
 
415
CREATE TABLESPACE (\fBCREATE_TABLESPACE\fR(7))\&.
 
416
.RE
 
417
.PP
 
418
RENAME
 
419
.RS 4
 
420
The
 
421
RENAME
 
422
forms change the name of a table (or an index, sequence, or view) or the name of an individual column in a table\&. There is no effect on the stored data\&.
 
423
.RE
 
424
.PP
 
425
SET SCHEMA
 
426
.RS 4
 
427
This form moves the table into another schema\&. Associated indexes, constraints, and sequences owned by table columns are moved as well\&.
 
428
.RE
 
429
.PP
 
430
All the actions except
 
431
RENAME
 
432
and
 
433
SET SCHEMA
 
434
can be combined into a list of multiple alterations to apply in parallel\&. For example, it is possible to add several columns and/or alter the type of several columns in a single command\&. This is particularly useful with large tables, since only one pass over the table need be made\&.
 
435
.PP
 
436
You must own the table to use
 
437
ALTER TABLE\&. To change the schema of a table, you must also have
 
438
CREATE
 
439
privilege on the new schema\&. To add the table as a new child of a parent table, you must own the parent table as well\&. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have
 
440
CREATE
 
441
privilege on the table\(aqs schema\&. (These restrictions enforce that altering the owner doesn\(aqt do anything you couldn\(aqt do by dropping and recreating the table\&. However, a superuser can alter ownership of any table anyway\&.)
 
442
.SH "PARAMETERS"
 
443
.PP
 
444
\fIname\fR
 
445
.RS 4
 
446
The name (possibly schema\-qualified) of an existing table to alter\&. If
 
447
ONLY
 
448
is specified, only that table is altered\&. If
 
449
ONLY
 
450
is not specified, the table and any descendant tables are altered\&.
 
451
.RE
 
452
.PP
 
453
\fIcolumn\fR
 
454
.RS 4
 
455
Name of a new or existing column\&.
 
456
.RE
 
457
.PP
 
458
\fInew_column\fR
 
459
.RS 4
 
460
New name for an existing column\&.
 
461
.RE
 
462
.PP
 
463
\fInew_name\fR
 
464
.RS 4
 
465
New name for the table\&.
 
466
.RE
 
467
.PP
 
468
\fItype\fR
 
469
.RS 4
 
470
Data type of the new column, or new data type for an existing column\&.
 
471
.RE
 
472
.PP
 
473
\fItable_constraint\fR
 
474
.RS 4
 
475
New table constraint for the table\&.
 
476
.RE
 
477
.PP
 
478
\fIconstraint_name\fR
 
479
.RS 4
 
480
Name of an existing constraint to drop\&.
 
481
.RE
 
482
.PP
 
483
CASCADE
 
484
.RS 4
 
485
Automatically drop objects that depend on the dropped column or constraint (for example, views referencing the column)\&.
 
486
.RE
 
487
.PP
 
488
RESTRICT
 
489
.RS 4
 
490
Refuse to drop the column or constraint if there are any dependent objects\&. This is the default behavior\&.
 
491
.RE
 
492
.PP
 
493
\fItrigger_name\fR
 
494
.RS 4
 
495
Name of a single trigger to disable or enable\&.
 
496
.RE
 
497
.PP
 
498
ALL
 
499
.RS 4
 
500
Disable or enable all triggers belonging to the table\&. (This requires superuser privilege if any of the triggers are internally generated constraint triggers such as those that are used to implement foreign key constraints or deferrable uniqueness and exclusion constraints\&.)
 
501
.RE
 
502
.PP
 
503
USER
 
504
.RS 4
 
505
Disable or enable all triggers belonging to the table except for internally generated constraint triggers such as those that are used to implement foreign key constraints or deferrable uniqueness and exclusion constraints\&.
 
506
.RE
 
507
.PP
 
508
\fIindex_name\fR
 
509
.RS 4
 
510
The index name on which the table should be marked for clustering\&.
 
511
.RE
 
512
.PP
 
513
\fIstorage_parameter\fR
 
514
.RS 4
 
515
The name of a table storage parameter\&.
 
516
.RE
 
517
.PP
 
518
\fIvalue\fR
 
519
.RS 4
 
520
The new value for a table storage parameter\&. This might be a number or a word depending on the parameter\&.
 
521
.RE
 
522
.PP
 
523
\fIparent_table\fR
 
524
.RS 4
 
525
A parent table to associate or de\-associate with this table\&.
 
526
.RE
 
527
.PP
 
528
\fInew_owner\fR
 
529
.RS 4
 
530
The user name of the new owner of the table\&.
 
531
.RE
 
532
.PP
 
533
\fInew_tablespace\fR
 
534
.RS 4
 
535
The name of the tablespace to which the table will be moved\&.
 
536
.RE
 
537
.PP
 
538
\fInew_schema\fR
 
539
.RS 4
 
540
The name of the schema to which the table will be moved\&.
 
541
.RE
 
542
.SH "NOTES"
 
543
.PP
 
544
The key word
 
545
COLUMN
 
546
is noise and can be omitted\&.
 
547
.PP
 
548
When a column is added with
 
549
ADD COLUMN, all existing rows in the table are initialized with the column\(aqs default value (NULL if no
 
550
DEFAULT
 
551
clause is specified)\&.
 
552
.PP
 
553
Adding a column with a non\-null default or changing the type of an existing column will require the entire table and indexes to be rewritten\&. As an exception, if the
 
554
USING
 
555
clause does not change the column contents and the old type is either binary coercible to the new type or an unconstrained domain over the new type, a table rewrite is not needed, but any indexes on the affected columns must still be rebuilt\&. Adding or removing a system
 
556
oid
 
557
column also requires rewriting the entire table\&. Table and/or index rebuilds may take a significant amount of time for a large table; and will temporarily require as much as double the disk space\&.
 
558
.PP
 
559
Adding a
 
560
CHECK
 
561
or
 
562
NOT NULL
 
563
constraint requires scanning the table to verify that existing rows meet the constraint\&.
 
564
.PP
 
565
The main reason for providing the option to specify multiple changes in a single
 
566
ALTER TABLE
 
567
is that multiple table scans or rewrites can thereby be combined into a single pass over the table\&.
 
568
.PP
 
569
The
 
570
DROP COLUMN
 
571
form does not physically remove the column, but simply makes it invisible to SQL operations\&. Subsequent insert and update operations in the table will store a null value for the column\&. Thus, dropping a column is quick but it will not immediately reduce the on\-disk size of your table, as the space occupied by the dropped column is not reclaimed\&. The space will be reclaimed over time as existing rows are updated\&. (These statements do not apply when dropping the system
 
572
oid
 
573
column; that is done with an immediate rewrite\&.)
 
574
.PP
 
575
To force an immediate rewrite of the table, you can use
 
576
VACUUM FULL,
 
577
\fBCLUSTER\fR(7)
 
578
or one of the forms of ALTER TABLE that forces a rewrite\&. This results in no semantically\-visible change in the table, but gets rid of no\-longer\-useful data\&.
 
579
.PP
 
580
The
 
581
USING
 
582
option of
 
583
SET DATA TYPE
 
584
can actually specify any expression involving the old values of the row; that is, it can refer to other columns as well as the one being converted\&. This allows very general conversions to be done with the
 
585
SET DATA TYPE
 
586
syntax\&. Because of this flexibility, the
 
587
USING
 
588
expression is not applied to the column\(aqs default value (if any); the result might not be a constant expression as required for a default\&. This means that when there is no implicit or assignment cast from old to new type,
 
589
SET DATA TYPE
 
590
might fail to convert the default even though a
 
591
USING
 
592
clause is supplied\&. In such cases, drop the default with
 
593
DROP DEFAULT, perform the
 
594
ALTER TYPE, and then use
 
595
SET DEFAULT
 
596
to add a suitable new default\&. Similar considerations apply to indexes and constraints involving the column\&.
 
597
.PP
 
598
If a table has any descendant tables, it is not permitted to add, rename, or change the type of a column in the parent table without doing the same to the descendants\&. That is,
 
599
ALTER TABLE ONLY
 
600
will be rejected\&. This ensures that the descendants always have columns matching the parent\&.
 
601
.PP
 
602
A recursive
 
603
DROP COLUMN
 
604
operation will remove a descendant table\(aqs column only if the descendant does not inherit that column from any other parents and never had an independent definition of the column\&. A nonrecursive
 
605
DROP COLUMN
 
606
(i\&.e\&.,
 
607
ALTER TABLE ONLY \&.\&.\&. DROP COLUMN) never removes any descendant columns, but instead marks them as independently defined rather than inherited\&.
 
608
.PP
 
609
The
 
610
TRIGGER,
 
611
CLUSTER,
 
612
OWNER, and
 
613
TABLESPACE
 
614
actions never recurse to descendant tables; that is, they always act as though
 
615
ONLY
 
616
were specified\&. Adding a constraint can recurse only for
 
617
CHECK
 
618
constraints, and is required to do so for such constraints\&.
 
619
.PP
 
620
Changing any part of a system catalog table is not permitted\&.
 
621
.PP
 
622
Refer to
 
623
CREATE TABLE (\fBCREATE_TABLE\fR(7))
 
624
for a further description of valid parameters\&.
 
625
Chapter 5, Data Definition, in the documentation
 
626
has further information on inheritance\&.
 
627
.SH "EXAMPLES"
 
628
.PP
 
629
To add a column of type
 
630
varchar
 
631
to a table:
 
632
.sp
 
633
.if n \{\
 
634
.RS 4
 
635
.\}
 
636
.nf
 
637
ALTER TABLE distributors ADD COLUMN address varchar(30);
 
638
.fi
 
639
.if n \{\
 
640
.RE
 
641
.\}
 
642
.PP
 
643
To drop a column from a table:
 
644
.sp
 
645
.if n \{\
 
646
.RS 4
 
647
.\}
 
648
.nf
 
649
ALTER TABLE distributors DROP COLUMN address RESTRICT;
 
650
.fi
 
651
.if n \{\
 
652
.RE
 
653
.\}
 
654
.PP
 
655
To change the types of two existing columns in one operation:
 
656
.sp
 
657
.if n \{\
 
658
.RS 4
 
659
.\}
 
660
.nf
 
661
ALTER TABLE distributors
 
662
    ALTER COLUMN address TYPE varchar(80),
 
663
    ALTER COLUMN name TYPE varchar(100);
 
664
.fi
 
665
.if n \{\
 
666
.RE
 
667
.\}
 
668
.PP
 
669
To change an integer column containing UNIX timestamps to
 
670
timestamp with time zone
 
671
via a
 
672
USING
 
673
clause:
 
674
.sp
 
675
.if n \{\
 
676
.RS 4
 
677
.\}
 
678
.nf
 
679
ALTER TABLE foo
 
680
    ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone
 
681
    USING
 
682
        timestamp with time zone \(aqepoch\(aq + foo_timestamp * interval \(aq1 second\(aq;
 
683
.fi
 
684
.if n \{\
 
685
.RE
 
686
.\}
 
687
.PP
 
688
The same, when the column has a default expression that won\(aqt automatically cast to the new data type:
 
689
.sp
 
690
.if n \{\
 
691
.RS 4
 
692
.\}
 
693
.nf
 
694
ALTER TABLE foo
 
695
    ALTER COLUMN foo_timestamp DROP DEFAULT,
 
696
    ALTER COLUMN foo_timestamp TYPE timestamp with time zone
 
697
    USING
 
698
        timestamp with time zone \(aqepoch\(aq + foo_timestamp * interval \(aq1 second\(aq,
 
699
    ALTER COLUMN foo_timestamp SET DEFAULT now();
 
700
.fi
 
701
.if n \{\
 
702
.RE
 
703
.\}
 
704
.PP
 
705
To rename an existing column:
 
706
.sp
 
707
.if n \{\
 
708
.RS 4
 
709
.\}
 
710
.nf
 
711
ALTER TABLE distributors RENAME COLUMN address TO city;
 
712
.fi
 
713
.if n \{\
 
714
.RE
 
715
.\}
 
716
.PP
 
717
To rename an existing table:
 
718
.sp
 
719
.if n \{\
 
720
.RS 4
 
721
.\}
 
722
.nf
 
723
ALTER TABLE distributors RENAME TO suppliers;
 
724
.fi
 
725
.if n \{\
 
726
.RE
 
727
.\}
 
728
.PP
 
729
To add a not\-null constraint to a column:
 
730
.sp
 
731
.if n \{\
 
732
.RS 4
 
733
.\}
 
734
.nf
 
735
ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
 
736
.fi
 
737
.if n \{\
 
738
.RE
 
739
.\}
 
740
.sp
 
741
To remove a not\-null constraint from a column:
 
742
.sp
 
743
.if n \{\
 
744
.RS 4
 
745
.\}
 
746
.nf
 
747
ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
 
748
.fi
 
749
.if n \{\
 
750
.RE
 
751
.\}
 
752
.PP
 
753
To add a check constraint to a table and all its children:
 
754
.sp
 
755
.if n \{\
 
756
.RS 4
 
757
.\}
 
758
.nf
 
759
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
 
760
.fi
 
761
.if n \{\
 
762
.RE
 
763
.\}
 
764
.PP
 
765
To remove a check constraint from a table and all its children:
 
766
.sp
 
767
.if n \{\
 
768
.RS 4
 
769
.\}
 
770
.nf
 
771
ALTER TABLE distributors DROP CONSTRAINT zipchk;
 
772
.fi
 
773
.if n \{\
 
774
.RE
 
775
.\}
 
776
.PP
 
777
To remove a check constraint from a table only:
 
778
.sp
 
779
.if n \{\
 
780
.RS 4
 
781
.\}
 
782
.nf
 
783
ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;
 
784
.fi
 
785
.if n \{\
 
786
.RE
 
787
.\}
 
788
.sp
 
789
(The check constraint remains in place for any child tables\&.)
 
790
.PP
 
791
To add a foreign key constraint to a table:
 
792
.sp
 
793
.if n \{\
 
794
.RS 4
 
795
.\}
 
796
.nf
 
797
ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;
 
798
.fi
 
799
.if n \{\
 
800
.RE
 
801
.\}
 
802
.PP
 
803
To add a (multicolumn) unique constraint to a table:
 
804
.sp
 
805
.if n \{\
 
806
.RS 4
 
807
.\}
 
808
.nf
 
809
ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
 
810
.fi
 
811
.if n \{\
 
812
.RE
 
813
.\}
 
814
.PP
 
815
To add an automatically named primary key constraint to a table, noting that a table can only ever have one primary key:
 
816
.sp
 
817
.if n \{\
 
818
.RS 4
 
819
.\}
 
820
.nf
 
821
ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
 
822
.fi
 
823
.if n \{\
 
824
.RE
 
825
.\}
 
826
.PP
 
827
To move a table to a different tablespace:
 
828
.sp
 
829
.if n \{\
 
830
.RS 4
 
831
.\}
 
832
.nf
 
833
ALTER TABLE distributors SET TABLESPACE fasttablespace;
 
834
.fi
 
835
.if n \{\
 
836
.RE
 
837
.\}
 
838
.PP
 
839
To move a table to a different schema:
 
840
.sp
 
841
.if n \{\
 
842
.RS 4
 
843
.\}
 
844
.nf
 
845
ALTER TABLE myschema\&.distributors SET SCHEMA yourschema;
 
846
.fi
 
847
.if n \{\
 
848
.RE
 
849
.\}
 
850
.PP
 
851
To recreate a primary key constraint, without blocking updates while the index is rebuilt:
 
852
.sp
 
853
.if n \{\
 
854
.RS 4
 
855
.\}
 
856
.nf
 
857
CREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);
 
858
ALTER TABLE distributors DROP CONSTRAINT distributors_pkey,
 
859
    ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;
 
860
.fi
 
861
.if n \{\
 
862
.RE
 
863
.\}
 
864
.SH "COMPATIBILITY"
 
865
.PP
 
866
The forms
 
867
ADD
 
868
(without
 
869
USING INDEX),
 
870
DROP,
 
871
SET DEFAULT, and
 
872
SET DATA TYPE
 
873
(without
 
874
USING) conform with the SQL standard\&. The other forms are
 
875
PostgreSQL
 
876
extensions of the SQL standard\&. Also, the ability to specify more than one manipulation in a single
 
877
ALTER TABLE
 
878
command is an extension\&.
 
879
.PP
 
880
ALTER TABLE DROP COLUMN
 
881
can be used to drop the only column of a table, leaving a zero\-column table\&. This is an extension of SQL, which disallows zero\-column tables\&.
 
882
.SH "SEE ALSO"
 
883
CREATE TABLE (\fBCREATE_TABLE\fR(7))