Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / doc / src / sgml / ref / alter_table.sgml
blobfe3f3889be8fd2f4d36958f66eb3426b19660bb7
1 <!--
2 $PostgreSQL$
3 PostgreSQL documentation
4 -->
6 <refentry id="SQL-ALTERTABLE">
7 <refmeta>
8 <refentrytitle id="sql-altertable-title">ALTER TABLE</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements</refmiscinfo>
11 </refmeta>
13 <refnamediv>
14 <refname>ALTER TABLE</refname>
15 <refpurpose>change the definition of a table</refpurpose>
16 </refnamediv>
18 <indexterm zone="sql-altertable">
19 <primary>ALTER TABLE</primary>
20 </indexterm>
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>
33 where <replaceable class="PARAMETER">action</replaceable> is one of:
35 ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable class="PARAMETER">type</replaceable> [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
36 DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> [ RESTRICT | CASCADE ]
37 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> [ SET DATA ] TYPE <replaceable class="PARAMETER">type</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 STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
43 ADD <replaceable class="PARAMETER">table_constraint</replaceable>
44 DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
45 DISABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
46 ENABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
47 ENABLE REPLICA TRIGGER <replaceable class="PARAMETER">trigger_name</replaceable>
48 ENABLE ALWAYS TRIGGER <replaceable class="PARAMETER">trigger_name</replaceable>
49 DISABLE RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
50 ENABLE RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
51 ENABLE REPLICA RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
52 ENABLE ALWAYS RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
53 CLUSTER ON <replaceable class="PARAMETER">index_name</replaceable>
54 SET WITHOUT CLUSTER
55 SET WITH OIDS
56 SET WITHOUT OIDS
57 SET ( <replaceable class="PARAMETER">storage_parameter</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )
58 RESET ( <replaceable class="PARAMETER">storage_parameter</replaceable> [, ... ] )
59 INHERIT <replaceable class="PARAMETER">parent_table</replaceable>
60 NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable>
61 OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
62 SET TABLESPACE <replaceable class="PARAMETER">new_tablespace</replaceable>
63 </synopsis>
64 </refsynopsisdiv>
66 <refsect1>
67 <title>Description</title>
69 <para>
70 <command>ALTER TABLE</command> changes the definition of an existing table.
71 There are several subforms:
73 <variablelist>
74 <varlistentry>
75 <term><literal>ADD COLUMN</literal></term>
76 <listitem>
77 <para>
78 This form adds a new column to the table, using the same syntax as
79 <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
80 </para>
81 </listitem>
82 </varlistentry>
84 <varlistentry>
85 <term><literal>DROP COLUMN</literal></term>
86 <listitem>
87 <para>
88 This form drops a column from a table. Indexes and
89 table constraints involving the column will be automatically
90 dropped as well. You will need to say <literal>CASCADE</> if
91 anything outside the table depends on the column, for example,
92 foreign key references or views.
93 </para>
94 </listitem>
95 </varlistentry>
97 <varlistentry>
98 <term><literal>SET DATA TYPE</literal></term>
99 <listitem>
100 <para>
101 This form changes the type of a column of a table. Indexes and
102 simple table constraints involving the column will be automatically
103 converted to use the new column type by reparsing the originally
104 supplied expression. The optional <literal>USING</literal>
105 clause specifies how to compute the new column value from the old;
106 if omitted, the default conversion is the same as an assignment
107 cast from old data type to new. A <literal>USING</literal>
108 clause must be provided if there is no implicit or assignment
109 cast from old to new type.
110 </para>
111 </listitem>
112 </varlistentry>
114 <varlistentry>
115 <term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
116 <listitem>
117 <para>
118 These forms set or remove the default value for a column.
119 The default values only apply to subsequent <command>INSERT</command>
120 commands; they do not cause rows already in the table to change.
121 Defaults can also be created for views, in which case they are
122 inserted into <command>INSERT</> statements on the view before
123 the view's <literal>ON INSERT</literal> rule is applied.
124 </para>
125 </listitem>
126 </varlistentry>
128 <varlistentry>
129 <term><literal>SET</literal>/<literal>DROP NOT NULL</literal></term>
130 <listitem>
131 <para>
132 These forms change whether a column is marked to allow null
133 values or to reject null values. You can only use <literal>SET
134 NOT NULL</> when the column contains no null values.
135 </para>
136 </listitem>
137 </varlistentry>
139 <varlistentry>
140 <term><literal>SET STATISTICS</literal></term>
141 <listitem>
142 <para>
143 This form
144 sets the per-column statistics-gathering target for subsequent
145 <xref linkend="sql-analyze" endterm="sql-analyze-title"> operations.
146 The target can be set in the range 0 to 10000; alternatively, set it
147 to -1 to revert to using the system default statistics
148 target (<xref linkend="guc-default-statistics-target">).
149 For more information on the use of statistics by the
150 <productname>PostgreSQL</productname> query planner, refer to
151 <xref linkend="planner-stats">.
152 </para>
153 </listitem>
154 </varlistentry>
156 <varlistentry>
157 <indexterm>
158 <primary>TOAST</primary>
159 <secondary>per-column storage settings</secondary>
160 </indexterm>
162 <term><literal>SET STORAGE</literal></term>
163 <listitem>
164 <para>
165 This form sets the storage mode for a column. This controls whether this
166 column is held inline or in a supplementary table, and whether the data
167 should be compressed or not. <literal>PLAIN</literal> must be used
168 for fixed-length values such as <type>integer</type> and is
169 inline, uncompressed. <literal>MAIN</literal> is for inline,
170 compressible data. <literal>EXTERNAL</literal> is for external,
171 uncompressed data, and <literal>EXTENDED</literal> is for external,
172 compressed data. <literal>EXTENDED</literal> is the default for most
173 data types that support non-<literal>PLAIN</literal> storage.
174 Use of <literal>EXTERNAL</literal> will
175 make substring operations on <type>text</type> and <type>bytea</type>
176 columns faster, at the penalty of increased storage space. Note that
177 <literal>SET STORAGE</> doesn't itself change anything in the table,
178 it just sets the strategy to be pursued during future table updates.
179 See <xref linkend="storage-toast"> for more information.
180 </para>
181 </listitem>
182 </varlistentry>
184 <varlistentry>
185 <term><literal>ADD <replaceable class="PARAMETER">table_constraint</replaceable></literal></term>
186 <listitem>
187 <para>
188 This form adds a new constraint to a table using the same syntax as
189 <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
190 </para>
191 </listitem>
192 </varlistentry>
194 <varlistentry>
195 <term><literal>DROP CONSTRAINT</literal></term>
196 <listitem>
197 <para>
198 This form drops the specified constraint on a table.
199 </para>
200 </listitem>
201 </varlistentry>
203 <varlistentry>
204 <term><literal>DISABLE</literal>/<literal>ENABLE [ REPLICA | ALWAYS ] TRIGGER</literal></term>
205 <listitem>
206 <para>
207 These forms configure the firing of trigger(s) belonging to the table.
208 A disabled trigger is still known to the system, but is not executed
209 when its triggering event occurs. For a deferred trigger, the enable
210 status is checked when the event occurs, not when the trigger function
211 is actually executed. One can disable or enable a single
212 trigger specified by name, or all triggers on the table, or only
213 user triggers (this option excludes triggers that are used to implement
214 foreign key constraints). Disabling or enabling constraint triggers
215 requires superuser privileges; it should be done with caution since
216 of course the integrity of the constraint cannot be guaranteed if the
217 triggers are not executed.
218 The trigger firing mechanism is also affected by the configuration
219 variable <xref linkend="guc-session-replication-role">. Simply enabled
220 triggers will fire when the replication role is <quote>origin</>
221 (the default) or <quote>local</>. Triggers configured as <literal>ENABLE
222 REPLICA</literal> will only fire if the session is in <quote>replica</>
223 mode, and triggers configured as <literal>ENABLE ALWAYS</literal> will
224 fire regardless of the current replication mode.
225 </para>
226 </listitem>
227 </varlistentry>
229 <varlistentry>
230 <term><literal>DISABLE</literal>/<literal>ENABLE [ REPLICA | ALWAYS ] RULE</literal></term>
231 <listitem>
232 <para>
233 These forms configure the firing of rewrite rules belonging to the table.
234 A disabled rule is still known to the system, but is not applied
235 during query rewriting. The semantics are as for disabled/enabled
236 triggers. This configuration is ignored for <literal>ON SELECT</literal> rules, which
237 are always applied in order to keep views working even if the current
238 session is in a non-default replication role.
239 </para>
240 </listitem>
241 </varlistentry>
243 <varlistentry>
244 <term><literal>CLUSTER</literal></term>
245 <listitem>
246 <para>
247 This form selects the default index for future
248 <xref linkend="SQL-CLUSTER" endterm="sql-cluster-title">
249 operations. It does not actually re-cluster the table.
250 </para>
251 </listitem>
252 </varlistentry>
254 <varlistentry>
255 <term><literal>SET WITHOUT CLUSTER</literal></term>
256 <listitem>
257 <para>
258 This form removes the most recently used
259 <xref linkend="SQL-CLUSTER" endterm="sql-cluster-title">
260 index specification from the table. This affects
261 future cluster operations that don't specify an index.
262 </para>
263 </listitem>
264 </varlistentry>
266 <varlistentry>
267 <term><literal>SET WITH OIDS</literal></term>
268 <listitem>
269 <para>
270 This form adds an <literal>oid</literal> system column to the
271 table (see <xref linkend="ddl-system-columns">).
272 It does nothing if the table already has OIDs.
273 </para>
275 <para>
276 Note that this is not equivalent to <literal>ADD COLUMN oid oid</>;
277 that would add a normal column that happened to be named
278 <literal>oid</>, not a system column.
279 </para>
280 </listitem>
281 </varlistentry>
283 <varlistentry>
284 <term><literal>SET WITHOUT OIDS</literal></term>
285 <listitem>
286 <para>
287 This form removes the <literal>oid</literal> system column from the
288 table. This is exactly equivalent to
289 <literal>DROP COLUMN oid RESTRICT</literal>,
290 except that it will not complain if there is already no
291 <literal>oid</literal> column.
292 </para>
293 </listitem>
294 </varlistentry>
296 <varlistentry>
297 <term><literal>SET ( <replaceable class="PARAMETER">storage_parameter</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )</literal></term>
298 <listitem>
299 <para>
300 This form changes one or more storage parameters for the table. See
301 <xref linkend="SQL-CREATETABLE-storage-parameters"
302 endterm="sql-createtable-storage-parameters-title">
303 for details on the available parameters. Note that the table contents
304 will not be modified immediately by this command; depending on the
305 parameter you might need to rewrite the table to get the desired effects.
306 That can be done with <xref linkend="SQL-CLUSTER"
307 endterm="sql-cluster-title"> or one of the forms of <command>ALTER
308 TABLE</> that forces a table rewrite.
309 </para>
311 <note>
312 <para>
313 While <command>CREATE TABLE</> allows <literal>OIDS</> to be specified
314 in the <literal>WITH (<replaceable
315 class="PARAMETER">storage_parameter</>)</literal> syntax,
316 <command>ALTER TABLE</> does not treat <literal>OIDS</> as a
317 storage parameter. Instead use the <literal>SET WITH OIDS</>
318 and <literal>SET WITHOUT OIDS</> forms to change OID status.
319 </para>
320 </note>
321 </listitem>
322 </varlistentry>
324 <varlistentry>
325 <term><literal>RESET ( <replaceable class="PARAMETER">storage_parameter</replaceable> [, ... ] )</literal></term>
326 <listitem>
327 <para>
328 This form resets one or more storage parameters to their
329 defaults. As with <literal>SET</>, a table rewrite might be
330 needed to update the table entirely.
331 </para>
332 </listitem>
333 </varlistentry>
335 <varlistentry>
336 <term><literal>INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
337 <listitem>
338 <para>
339 This form adds the target table as a new child of the specified parent
340 table. Subsequently, queries against the parent will include records
341 of the target table. To be added as a child, the target table must
342 already contain all the same columns as the parent (it could have
343 additional columns, too). The columns must have matching data types,
344 and if they have <literal>NOT NULL</literal> constraints in the parent
345 then they must also have <literal>NOT NULL</literal> constraints in the
346 child.
347 </para>
349 <para>
350 There must also be matching child-table constraints for all
351 <literal>CHECK</literal> constraints of the parent. Currently
352 <literal>UNIQUE</literal>, <literal>PRIMARY KEY</literal>, and
353 <literal>FOREIGN KEY</literal> constraints are not considered, but
354 this might change in the future.
355 </para>
356 </listitem>
357 </varlistentry>
359 <varlistentry>
360 <term><literal>NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
361 <listitem>
362 <para>
363 This form removes the target table from the list of children of the
364 specified parent table.
365 Queries against the parent table will no longer include records drawn
366 from the target table.
367 </para>
368 </listitem>
369 </varlistentry>
371 <varlistentry>
372 <term><literal>OWNER</literal></term>
373 <listitem>
374 <para>
375 This form changes the owner of the table, sequence, or view to the
376 specified user.
377 </para>
378 </listitem>
379 </varlistentry>
381 <varlistentry>
382 <term><literal>SET TABLESPACE</literal></term>
383 <listitem>
384 <para>
385 This form changes the table's tablespace to the specified tablespace and
386 moves the data file(s) associated with the table to the new tablespace.
387 Indexes on the table, if any, are not moved; but they can be moved
388 separately with additional <literal>SET TABLESPACE</literal> commands.
389 See also
390 <xref linkend="SQL-CREATETABLESPACE" endterm="sql-createtablespace-title">.
391 </para>
392 </listitem>
393 </varlistentry>
395 <varlistentry>
396 <term><literal>RENAME</literal></term>
397 <listitem>
398 <para>
399 The <literal>RENAME</literal> forms change the name of a table
400 (or an index, sequence, or view) or the name of an individual column in
401 a table. There is no effect on the stored data.
402 </para>
403 </listitem>
404 </varlistentry>
406 <varlistentry>
407 <term><literal>SET SCHEMA</literal></term>
408 <listitem>
409 <para>
410 This form moves the table into another schema. Associated indexes,
411 constraints, and sequences owned by table columns are moved as well.
412 </para>
413 </listitem>
414 </varlistentry>
416 </variablelist>
417 </para>
419 <para>
420 All the actions except <literal>RENAME</literal> and <literal>SET SCHEMA</>
421 can be combined into
422 a list of multiple alterations to apply in parallel. For example, it
423 is possible to add several columns and/or alter the type of several
424 columns in a single command. This is particularly useful with large
425 tables, since only one pass over the table need be made.
426 </para>
428 <para>
429 You must own the table to use <command>ALTER TABLE</>.
430 To change the schema of a table, you must also have
431 <literal>CREATE</literal> privilege on the new schema.
432 To add the table as a new child of a parent table, you must own the
433 parent table as well.
434 To alter the owner, you must also be a direct or indirect member of the new
435 owning role, and that role must have <literal>CREATE</literal> privilege on
436 the table's schema. (These restrictions enforce that altering the owner
437 doesn't do anything you couldn't do by dropping and recreating the table.
438 However, a superuser can alter ownership of any table anyway.)
439 </para>
440 </refsect1>
442 <refsect1>
443 <title>Parameters</title>
445 <variablelist>
447 <varlistentry>
448 <term><replaceable class="PARAMETER">name</replaceable></term>
449 <listitem>
450 <para>
451 The name (possibly schema-qualified) of an existing table to
452 alter. If <literal>ONLY</> is specified, only that table is
453 altered. If <literal>ONLY</> is not specified, the table and any
454 descendant tables are altered.
455 </para>
456 </listitem>
457 </varlistentry>
459 <varlistentry>
460 <term><replaceable class="PARAMETER">column</replaceable></term>
461 <listitem>
462 <para>
463 Name of a new or existing column.
464 </para>
465 </listitem>
466 </varlistentry>
468 <varlistentry>
469 <term><replaceable class="PARAMETER">new_column</replaceable></term>
470 <listitem>
471 <para>
472 New name for an existing column.
473 </para>
474 </listitem>
475 </varlistentry>
477 <varlistentry>
478 <term><replaceable class="PARAMETER">new_name</replaceable></term>
479 <listitem>
480 <para>
481 New name for the table.
482 </para>
483 </listitem>
484 </varlistentry>
486 <varlistentry>
487 <term><replaceable class="PARAMETER">type</replaceable></term>
488 <listitem>
489 <para>
490 Data type of the new column, or new data type for an existing
491 column.
492 </para>
493 </listitem>
494 </varlistentry>
496 <varlistentry>
497 <term><replaceable class="PARAMETER">table_constraint</replaceable></term>
498 <listitem>
499 <para>
500 New table constraint for the table.
501 </para>
502 </listitem>
503 </varlistentry>
505 <varlistentry>
506 <term><replaceable class="PARAMETER">constraint_name</replaceable></term>
507 <listitem>
508 <para>
509 Name of an existing constraint to drop.
510 </para>
511 </listitem>
512 </varlistentry>
514 <varlistentry>
515 <term><literal>CASCADE</literal></term>
516 <listitem>
517 <para>
518 Automatically drop objects that depend on the dropped column
519 or constraint (for example, views referencing the column).
520 </para>
521 </listitem>
522 </varlistentry>
524 <varlistentry>
525 <term><literal>RESTRICT</literal></term>
526 <listitem>
527 <para>
528 Refuse to drop the column or constraint if there are any dependent
529 objects. This is the default behavior.
530 </para>
531 </listitem>
532 </varlistentry>
534 <varlistentry>
535 <term><replaceable class="PARAMETER">trigger_name</replaceable></term>
536 <listitem>
537 <para>
538 Name of a single trigger to disable or enable.
539 </para>
540 </listitem>
541 </varlistentry>
543 <varlistentry>
544 <term><literal>ALL</literal></term>
545 <listitem>
546 <para>
547 Disable or enable all triggers belonging to the table.
548 (This requires superuser privilege if any of the triggers are for
549 foreign key constraints.)
550 </para>
551 </listitem>
552 </varlistentry>
554 <varlistentry>
555 <term><literal>USER</literal></term>
556 <listitem>
557 <para>
558 Disable or enable all triggers belonging to the table except for
559 foreign key constraint triggers.
560 </para>
561 </listitem>
562 </varlistentry>
564 <varlistentry>
565 <term><replaceable class="PARAMETER">index_name</replaceable></term>
566 <listitem>
567 <para>
568 The index name on which the table should be marked for clustering.
569 </para>
570 </listitem>
571 </varlistentry>
573 <varlistentry>
574 <term><replaceable class="PARAMETER">storage_parameter</replaceable></term>
575 <listitem>
576 <para>
577 The name of a table storage parameter.
578 </para>
579 </listitem>
580 </varlistentry>
582 <varlistentry>
583 <term><replaceable class="PARAMETER">value</replaceable></term>
584 <listitem>
585 <para>
586 The new value for a table storage parameter.
587 This might be a number or a word depending on the parameter.
588 </para>
589 </listitem>
590 </varlistentry>
592 <varlistentry>
593 <term><replaceable class="PARAMETER">parent_table</replaceable></term>
594 <listitem>
595 <para>
596 A parent table to associate or de-associate with this table.
597 </para>
598 </listitem>
599 </varlistentry>
601 <varlistentry>
602 <term><replaceable class="PARAMETER">new_owner</replaceable></term>
603 <listitem>
604 <para>
605 The user name of the new owner of the table.
606 </para>
607 </listitem>
608 </varlistentry>
610 <varlistentry>
611 <term><replaceable class="PARAMETER">new_tablespace</replaceable></term>
612 <listitem>
613 <para>
614 The name of the tablespace to which the table will be moved.
615 </para>
616 </listitem>
617 </varlistentry>
619 <varlistentry>
620 <term><replaceable class="PARAMETER">new_schema</replaceable></term>
621 <listitem>
622 <para>
623 The name of the schema to which the table will be moved.
624 </para>
625 </listitem>
626 </varlistentry>
628 </variablelist>
629 </refsect1>
631 <refsect1>
632 <title>Notes</title>
634 <para>
635 The key word <literal>COLUMN</literal> is noise and can be omitted.
636 </para>
638 <para>
639 When a column is added with <literal>ADD COLUMN</literal>, all existing
640 rows in the table are initialized with the column's default value
641 (NULL if no <literal>DEFAULT</> clause is specified).
642 </para>
644 <para>
645 Adding a column with a non-null default or changing the type of an
646 existing column will require the entire table to be rewritten. This
647 might take a significant amount of time for a large table; and it will
648 temporarily require double the disk space. Adding or removing a system
649 <literal>oid</> column likewise requires rewriting the entire table.
650 </para>
652 <para>
653 Adding a <literal>CHECK</> or <literal>NOT NULL</> constraint requires
654 scanning the table to verify that existing rows meet the constraint.
655 </para>
657 <para>
658 The main reason for providing the option to specify multiple changes
659 in a single <command>ALTER TABLE</> is that multiple table scans or
660 rewrites can thereby be combined into a single pass over the table.
661 </para>
663 <para>
664 The <literal>DROP COLUMN</literal> form does not physically remove
665 the column, but simply makes it invisible to SQL operations. Subsequent
666 insert and update operations in the table will store a null value for the
667 column. Thus, dropping a column is quick but it will not immediately
668 reduce the on-disk size of your table, as the space occupied
669 by the dropped column is not reclaimed. The space will be
670 reclaimed over time as existing rows are updated. (These statements do
671 not apply when dropping the system <literal>oid</> column; that is done
672 with an immediate rewrite.)
673 </para>
675 <para>
676 The fact that <literal>ALTER TYPE</> requires rewriting the whole table
677 is sometimes an advantage, because the rewriting process eliminates
678 any dead space in the table. For example, to reclaim the space occupied
679 by a dropped column immediately, the fastest way is:
680 <programlisting>
681 ALTER TABLE table ALTER COLUMN anycol TYPE anytype;
682 </programlisting>
683 where <literal>anycol</> is any remaining table column and
684 <literal>anytype</> is the same type that column already has.
685 This results in no semantically-visible change in the table,
686 but the command forces rewriting, which gets rid of no-longer-useful
687 data.
688 </para>
690 <para>
691 The <literal>USING</literal> option of <literal>ALTER TYPE</> can actually
692 specify any expression involving the old values of the row; that is, it
693 can refer to other columns as well as the one being converted. This allows
694 very general conversions to be done with the <literal>ALTER TYPE</>
695 syntax. Because of this flexibility, the <literal>USING</literal>
696 expression is not applied to the column's default value (if any); the
697 result might not be a constant expression as required for a default.
698 This means that when there is no implicit or assignment cast from old to
699 new type, <literal>ALTER TYPE</> might fail to convert the default even
700 though a <literal>USING</literal> clause is supplied. In such cases,
701 drop the default with <literal>DROP DEFAULT</>, perform the <literal>ALTER
702 TYPE</>, and then use <literal>SET DEFAULT</> to add a suitable new
703 default. Similar considerations apply to indexes and constraints involving
704 the column.
705 </para>
707 <para>
708 If a table has any descendant tables, it is not permitted to add,
709 rename, or change the type of a column in the parent table without doing
710 the same to the descendants. That is, <command>ALTER TABLE ONLY</command>
711 will be rejected. This ensures that the descendants always have
712 columns matching the parent.
713 </para>
715 <para>
716 A recursive <literal>DROP COLUMN</literal> operation will remove a
717 descendant table's column only if the descendant does not inherit
718 that column from any other parents and never had an independent
719 definition of the column. A nonrecursive <literal>DROP
720 COLUMN</literal> (i.e., <command>ALTER TABLE ONLY ... DROP
721 COLUMN</command>) never removes any descendant columns, but
722 instead marks them as independently defined rather than inherited.
723 </para>
725 <para>
726 The <literal>TRIGGER</>, <literal>CLUSTER</>, <literal>OWNER</>,
727 and <literal>TABLESPACE</> actions never recurse to descendant tables;
728 that is, they always act as though <literal>ONLY</> were specified.
729 Adding a constraint can recurse only for <literal>CHECK</> constraints,
730 and is required to do so for such constraints.
731 </para>
733 <para>
734 Changing any part of a system catalog table is not permitted.
735 </para>
737 <para>
738 Refer to <xref linkend="sql-createtable"
739 endterm="sql-createtable-title"> for a further description of valid
740 parameters. <xref linkend="ddl"> has further information on
741 inheritance.
742 </para>
743 </refsect1>
745 <refsect1>
746 <title>Examples</title>
748 <para>
749 To add a column of type <type>varchar</type> to a table:
750 <programlisting>
751 ALTER TABLE distributors ADD COLUMN address varchar(30);
752 </programlisting>
753 </para>
755 <para>
756 To drop a column from a table:
757 <programlisting>
758 ALTER TABLE distributors DROP COLUMN address RESTRICT;
759 </programlisting>
760 </para>
762 <para>
763 To change the types of two existing columns in one operation:
764 <programlisting>
765 ALTER TABLE distributors
766 ALTER COLUMN address TYPE varchar(80),
767 ALTER COLUMN name TYPE varchar(100);
768 </programlisting>
769 </para>
771 <para>
772 To change an integer column containing UNIX timestamps to <type>timestamp
773 with time zone</type> via a <literal>USING</literal> clause:
774 <programlisting>
775 ALTER TABLE foo
776 ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone
777 USING
778 timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';
779 </programlisting>
780 </para>
782 <para>
783 The same, when the column has a default expression that won't automatically
784 cast to the new data type:
785 <programlisting>
786 ALTER TABLE foo
787 ALTER COLUMN foo_timestamp DROP DEFAULT,
788 ALTER COLUMN foo_timestamp TYPE timestamp with time zone
789 USING
790 timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',
791 ALTER COLUMN foo_timestamp SET DEFAULT now();
792 </programlisting>
793 </para>
795 <para>
796 To rename an existing column:
797 <programlisting>
798 ALTER TABLE distributors RENAME COLUMN address TO city;
799 </programlisting>
800 </para>
802 <para>
803 To rename an existing table:
804 <programlisting>
805 ALTER TABLE distributors RENAME TO suppliers;
806 </programlisting>
807 </para>
809 <para>
810 To add a not-null constraint to a column:
811 <programlisting>
812 ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
813 </programlisting>
814 To remove a not-null constraint from a column:
815 <programlisting>
816 ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
817 </programlisting>
818 </para>
820 <para>
821 To add a check constraint to a table and all its children:
822 <programlisting>
823 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
824 </programlisting>
825 </para>
827 <para>
828 To remove a check constraint from a table and all its children:
829 <programlisting>
830 ALTER TABLE distributors DROP CONSTRAINT zipchk;
831 </programlisting>
832 </para>
834 <para>
835 To remove a check constraint from a table only:
836 <programlisting>
837 ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;
838 </programlisting>
839 (The check constraint remains in place for any child tables.)
840 </para>
842 <para>
843 To add a foreign key constraint to a table:
844 <programlisting>
845 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;
846 </programlisting>
847 </para>
849 <para>
850 To add a (multicolumn) unique constraint to a table:
851 <programlisting>
852 ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
853 </programlisting>
854 </para>
856 <para>
857 To add an automatically named primary key constraint to a table, noting
858 that a table can only ever have one primary key:
859 <programlisting>
860 ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
861 </programlisting>
862 </para>
864 <para>
865 To move a table to a different tablespace:
866 <programlisting>
867 ALTER TABLE distributors SET TABLESPACE fasttablespace;
868 </programlisting>
869 </para>
871 <para>
872 To move a table to a different schema:
873 <programlisting>
874 ALTER TABLE myschema.distributors SET SCHEMA yourschema;
875 </programlisting>
876 </para>
878 </refsect1>
880 <refsect1>
881 <title>Compatibility</title>
883 <para>
884 The forms <literal>ADD</literal>, <literal>DROP</>, <literal>SET DEFAULT</>,
885 and <literal>SET DATA TYPE</literal> (without <literal>USING</literal>)
886 conform with the SQL standard. The other forms are
887 <productname>PostgreSQL</productname> extensions of the SQL standard.
888 Also, the ability to specify more than one manipulation in a single
889 <command>ALTER TABLE</> command is an extension.
890 </para>
892 <para>
893 <command>ALTER TABLE DROP COLUMN</> can be used to drop the only
894 column of a table, leaving a zero-column table. This is an
895 extension of SQL, which disallows zero-column tables.
896 </para>
897 </refsect1>
898 </refentry>