2 doc/src/sgml/ref/alter_table.sgml
3 PostgreSQL documentation
6 <refentry id="SQL-ALTERTABLE">
7 <indexterm zone="sql-altertable">
8 <primary>ALTER TABLE</primary>
12 <refentrytitle>ALTER TABLE</refentrytitle>
13 <manvolnum>7</manvolnum>
14 <refmiscinfo>SQL - Language Statements</refmiscinfo>
18 <refname>ALTER TABLE</refname>
19 <refpurpose>change the definition of a table</refpurpose>
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>
35 <phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
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>
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>
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}
74 <phrase>and <replaceable class="PARAMETER">table_constraint_using_index</replaceable> is:</phrase>
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 ]
83 <title>Description</title>
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.
94 <term><literal>ADD COLUMN</literal></term>
97 This form adds a new column to the table, using the same syntax as
98 <xref linkend="SQL-CREATETABLE">.
104 <term><literal>DROP COLUMN [ IF EXISTS ]</literal></term>
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
120 <term><literal>IF EXISTS</literal></term>
123 Do not throw an error if the table does not exist. A notice is issued
130 <term><literal>SET DATA TYPE</literal></term>
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
137 The optional <literal>COLLATE</literal> clause specifies a collation
138 for the new column; if omitted, the collation is the default for the
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.
151 <term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
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
163 <term><literal>SET</literal>/<literal>DROP NOT NULL</literal></term>
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.
174 <term><literal>SET STATISTICS</literal></term>
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">.
188 SET STATISTICS acquires a <literal>SHARE UPDATE EXCLUSIVE</literal> lock.
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>
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">.
223 Changing per-attribute options acquires a
224 <literal>SHARE UPDATE EXCLUSIVE</literal> lock.
231 <primary>TOAST</primary>
232 <secondary>per-column storage settings</secondary>
235 <term><literal>SET STORAGE</literal></term>
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
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.
259 <term><literal>ADD <replaceable class="PARAMETER">table_constraint</replaceable> [ NOT VALID ]</literal></term>
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.
281 <term><literal>ADD <replaceable class="PARAMETER">table_constraint_using_index</replaceable></literal></term>
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.
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</>
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.
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
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
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.
332 <term><literal>ALTER CONSTRAINT</literal></term>
335 This form alters the attributes of a constraint that was previously
336 created. Currently only foreign key constraints may be altered.
342 <term><literal>VALIDATE CONSTRAINT</literal></term>
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.
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.
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.
367 <term><literal>DROP CONSTRAINT [ IF EXISTS ]</literal></term>
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.
378 <term><literal>DISABLE</literal>/<literal>ENABLE [ REPLICA | ALWAYS ] TRIGGER</literal></term>
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.
406 <term><literal>DISABLE</literal>/<literal>ENABLE [ REPLICA | ALWAYS ] RULE</literal></term>
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.
420 <term><literal>CLUSTER ON</literal></term>
423 This form selects the default index for future
424 <xref linkend="SQL-CLUSTER">
425 operations. It does not actually re-cluster the table.
428 Changing cluster options acquires a <literal>SHARE UPDATE EXCLUSIVE</literal> lock.
434 <term><literal>SET WITHOUT CLUSTER</literal></term>
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.
443 Changing cluster options acquires a <literal>SHARE UPDATE EXCLUSIVE</literal> lock.
449 <term><literal>SET WITH OIDS</literal></term>
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.
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.
466 <term><literal>SET WITHOUT OIDS</literal></term>
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.
479 <term><literal>SET ( <replaceable class="PARAMETER">storage_parameter</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )</literal></term>
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.
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.
507 <term><literal>RESET ( <replaceable class="PARAMETER">storage_parameter</replaceable> [, ... ] )</literal></term>
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.
518 <term><literal>INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
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
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.
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.
546 <term><literal>NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
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.
558 <term><literal>OF <replaceable class="PARAMETER">type_name</replaceable></literal></term>
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
573 <term><literal>NOT OF</literal></term>
576 This form dissociates a typed table from its type.
582 <term><literal>OWNER</literal></term>
585 This form changes the owner of the table, sequence, view, materialized view,
586 or foreign table to the specified user.
592 <term><literal>SET TABLESPACE</literal></term>
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.
600 <xref linkend="SQL-CREATETABLESPACE">.
605 <varlistentry id="SQL-CREATETABLE-REPLICA-IDENTITY">
606 <term><literal>REPLICA IDENTITY</literal></term>
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.
624 <term><literal>RENAME</literal></term>
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.
636 <term><literal>SET SCHEMA</literal></term>
639 This form moves the table into another schema. Associated indexes,
640 constraints, and sequences owned by table columns are moved as well.
649 All the actions except <literal>RENAME</literal> and <literal>SET SCHEMA</>
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.
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
675 <title>Parameters</title>
680 <term><replaceable class="PARAMETER">name</replaceable></term>
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.
694 <term><replaceable class="PARAMETER">column_name</replaceable></term>
697 Name of a new or existing column.
703 <term><replaceable class="PARAMETER">new_column_name</replaceable></term>
706 New name for an existing column.
712 <term><replaceable class="PARAMETER">new_name</replaceable></term>
715 New name for the table.
721 <term><replaceable class="PARAMETER">type</replaceable></term>
724 Data type of the new column, or new data type for an existing
731 <term><replaceable class="PARAMETER">table_constraint</replaceable></term>
734 New table constraint for the table.
740 <term><replaceable class="PARAMETER">constraint_name</replaceable></term>
743 Name of an existing constraint to drop.
749 <term><literal>CASCADE</literal></term>
752 Automatically drop objects that depend on the dropped column
753 or constraint (for example, views referencing the column).
759 <term><literal>RESTRICT</literal></term>
762 Refuse to drop the column or constraint if there are any dependent
763 objects. This is the default behavior.
769 <term><replaceable class="PARAMETER">trigger_name</replaceable></term>
772 Name of a single trigger to disable or enable.
778 <term><literal>ALL</literal></term>
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.)
791 <term><literal>USER</literal></term>
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.
803 <term><replaceable class="PARAMETER">index_name</replaceable></term>
806 The index name on which the table should be marked for clustering.
812 <term><replaceable class="PARAMETER">storage_parameter</replaceable></term>
815 The name of a table storage parameter.
821 <term><replaceable class="PARAMETER">value</replaceable></term>
824 The new value for a table storage parameter.
825 This might be a number or a word depending on the parameter.
831 <term><replaceable class="PARAMETER">parent_table</replaceable></term>
834 A parent table to associate or de-associate with this table.
840 <term><replaceable class="PARAMETER">new_owner</replaceable></term>
843 The user name of the new owner of the table.
849 <term><replaceable class="PARAMETER">new_tablespace</replaceable></term>
852 The name of the tablespace to which the table will be moved.
858 <term><replaceable class="PARAMETER">new_schema</replaceable></term>
861 The name of the schema to which the table will be moved.
873 The key word <literal>COLUMN</literal> is noise and can be omitted.
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.
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
900 Adding a <literal>CHECK</> or <literal>NOT NULL</> constraint requires
901 scanning the table to verify that existing rows meet the constraint.
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.
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.)
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.
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
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.
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.
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</>.
975 Changing any part of a system catalog table is not permitted.
979 Refer to <xref linkend="sql-createtable"> for a further description of valid
980 parameters. <xref linkend="ddl"> has further information on
986 <title>Examples</title>
989 To add a column of type <type>varchar</type> to a table:
991 ALTER TABLE distributors ADD COLUMN address varchar(30);
996 To drop a column from a table:
998 ALTER TABLE distributors DROP COLUMN address RESTRICT;
1003 To change the types of two existing columns in one operation:
1005 ALTER TABLE distributors
1006 ALTER COLUMN address TYPE varchar(80),
1007 ALTER COLUMN name TYPE varchar(100);
1012 To change an integer column containing UNIX timestamps to <type>timestamp
1013 with time zone</type> via a <literal>USING</literal> clause:
1016 ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone
1018 timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';
1023 The same, when the column has a default expression that won't automatically
1024 cast to the new data type:
1027 ALTER COLUMN foo_timestamp DROP DEFAULT,
1028 ALTER COLUMN foo_timestamp TYPE timestamp with time zone
1030 timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',
1031 ALTER COLUMN foo_timestamp SET DEFAULT now();
1036 To rename an existing column:
1038 ALTER TABLE distributors RENAME COLUMN address TO city;
1043 To rename an existing table:
1045 ALTER TABLE distributors RENAME TO suppliers;
1050 To rename an existing constraint:
1052 ALTER TABLE distributors RENAME CONSTRAINT zipchk TO zip_check;
1057 To add a not-null constraint to a column:
1059 ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
1061 To remove a not-null constraint from a column:
1063 ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
1068 To add a check constraint to a table and all its children:
1070 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
1075 To add a check constraint only to a table and not to its children:
1077 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;
1079 (The check constraint will not be inherited by future children, either.)
1083 To remove a check constraint from a table and all its children:
1085 ALTER TABLE distributors DROP CONSTRAINT zipchk;
1090 To remove a check constraint from one table only:
1092 ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;
1094 (The check constraint remains in place for any child tables.)
1098 To add a foreign key constraint to a table:
1100 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address);
1105 To add a foreign key constraint to a table with the least impact on other work:
1107 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) NOT VALID;
1108 ALTER TABLE distributors VALIDATE CONSTRAINT distfk;
1113 To add a (multicolumn) unique constraint to a table:
1115 ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
1120 To add an automatically named primary key constraint to a table, noting
1121 that a table can only ever have one primary key:
1123 ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
1128 To move a table to a different tablespace:
1130 ALTER TABLE distributors SET TABLESPACE fasttablespace;
1135 To move a table to a different schema:
1137 ALTER TABLE myschema.distributors SET SCHEMA yourschema;
1142 To recreate a primary key constraint, without blocking updates while the
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>
1153 <title>Compatibility</title>
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.
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.
1173 <title>See Also</title>
1175 <simplelist type="inline">
1176 <member><xref linkend="sql-createtable"></member>