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

« back to all changes in this revision

Viewing changes to doc/src/sgml/ref/alter_table.sgml

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