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