1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>ALTER TABLE</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-altersystem.html" title="ALTER SYSTEM" /><link rel="next" href="sql-altertablespace.html" title="ALTER TABLESPACE" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">ALTER TABLE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-altersystem.html" title="ALTER SYSTEM">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-altertablespace.html" title="ALTER TABLESPACE">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-ALTERTABLE"><div class="titlepage"></div><a id="id-1.9.3.35.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">ALTER TABLE</span></h2><p>ALTER TABLE — change the definition of a table</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 ALTER TABLE [ IF EXISTS ] [ ONLY ] <em class="replaceable"><code>name</code></em> [ * ]
4 <em class="replaceable"><code>action</code></em> [, ... ]
5 ALTER TABLE [ IF EXISTS ] [ ONLY ] <em class="replaceable"><code>name</code></em> [ * ]
6 RENAME [ COLUMN ] <em class="replaceable"><code>column_name</code></em> TO <em class="replaceable"><code>new_column_name</code></em>
7 ALTER TABLE [ IF EXISTS ] [ ONLY ] <em class="replaceable"><code>name</code></em> [ * ]
8 RENAME CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> TO <em class="replaceable"><code>new_constraint_name</code></em>
9 ALTER TABLE [ IF EXISTS ] <em class="replaceable"><code>name</code></em>
10 RENAME TO <em class="replaceable"><code>new_name</code></em>
11 ALTER TABLE [ IF EXISTS ] <em class="replaceable"><code>name</code></em>
12 SET SCHEMA <em class="replaceable"><code>new_schema</code></em>
13 ALTER TABLE ALL IN TABLESPACE <em class="replaceable"><code>name</code></em> [ OWNED BY <em class="replaceable"><code>role_name</code></em> [, ... ] ]
14 SET TABLESPACE <em class="replaceable"><code>new_tablespace</code></em> [ NOWAIT ]
15 ALTER TABLE [ IF EXISTS ] <em class="replaceable"><code>name</code></em>
16 ATTACH PARTITION <em class="replaceable"><code>partition_name</code></em> { FOR VALUES <em class="replaceable"><code>partition_bound_spec</code></em> | DEFAULT }
17 ALTER TABLE [ IF EXISTS ] <em class="replaceable"><code>name</code></em>
18 DETACH PARTITION <em class="replaceable"><code>partition_name</code></em> [ CONCURRENTLY | FINALIZE ]
20 <span class="phrase">where <em class="replaceable"><code>action</code></em> is one of:</span>
22 ADD [ COLUMN ] [ IF NOT EXISTS ] <em class="replaceable"><code>column_name</code></em> <em class="replaceable"><code>data_type</code></em> [ COLLATE <em class="replaceable"><code>collation</code></em> ] [ <em class="replaceable"><code>column_constraint</code></em> [ ... ] ]
23 DROP [ COLUMN ] [ IF EXISTS ] <em class="replaceable"><code>column_name</code></em> [ RESTRICT | CASCADE ]
24 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> [ SET DATA ] TYPE <em class="replaceable"><code>data_type</code></em> [ COLLATE <em class="replaceable"><code>collation</code></em> ] [ USING <em class="replaceable"><code>expression</code></em> ]
25 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET DEFAULT <em class="replaceable"><code>expression</code></em>
26 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> DROP DEFAULT
27 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> { SET | DROP } NOT NULL
28 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET EXPRESSION AS ( <em class="replaceable"><code>expression</code></em> )
29 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> DROP EXPRESSION [ IF EXISTS ]
30 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <em class="replaceable"><code>sequence_options</code></em> ) ]
31 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> { SET GENERATED { ALWAYS | BY DEFAULT } | SET <em class="replaceable"><code>sequence_option</code></em> | RESTART [ [ WITH ] <em class="replaceable"><code>restart</code></em> ] } [...]
32 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> DROP IDENTITY [ IF EXISTS ]
33 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET STATISTICS { <em class="replaceable"><code>integer</code></em> | DEFAULT }
34 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET ( <em class="replaceable"><code>attribute_option</code></em> = <em class="replaceable"><code>value</code></em> [, ... ] )
35 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> RESET ( <em class="replaceable"><code>attribute_option</code></em> [, ... ] )
36 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }
37 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET COMPRESSION <em class="replaceable"><code>compression_method</code></em>
38 ADD <em class="replaceable"><code>table_constraint</code></em> [ NOT VALID ]
39 ADD <em class="replaceable"><code>table_constraint_using_index</code></em>
40 ALTER CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]
41 ALTER CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> [ INHERIT | NO INHERIT ]
42 VALIDATE CONSTRAINT <em class="replaceable"><code>constraint_name</code></em>
43 DROP CONSTRAINT [ IF EXISTS ] <em class="replaceable"><code>constraint_name</code></em> [ RESTRICT | CASCADE ]
44 DISABLE TRIGGER [ <em class="replaceable"><code>trigger_name</code></em> | ALL | USER ]
45 ENABLE TRIGGER [ <em class="replaceable"><code>trigger_name</code></em> | ALL | USER ]
46 ENABLE REPLICA TRIGGER <em class="replaceable"><code>trigger_name</code></em>
47 ENABLE ALWAYS TRIGGER <em class="replaceable"><code>trigger_name</code></em>
48 DISABLE RULE <em class="replaceable"><code>rewrite_rule_name</code></em>
49 ENABLE RULE <em class="replaceable"><code>rewrite_rule_name</code></em>
50 ENABLE REPLICA RULE <em class="replaceable"><code>rewrite_rule_name</code></em>
51 ENABLE ALWAYS RULE <em class="replaceable"><code>rewrite_rule_name</code></em>
52 DISABLE ROW LEVEL SECURITY
53 ENABLE ROW LEVEL SECURITY
54 FORCE ROW LEVEL SECURITY
55 NO FORCE ROW LEVEL SECURITY
56 CLUSTER ON <em class="replaceable"><code>index_name</code></em>
59 SET ACCESS METHOD { <em class="replaceable"><code>new_access_method</code></em> | DEFAULT }
60 SET TABLESPACE <em class="replaceable"><code>new_tablespace</code></em>
61 SET { LOGGED | UNLOGGED }
62 SET ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] )
63 RESET ( <em class="replaceable"><code>storage_parameter</code></em> [, ... ] )
64 INHERIT <em class="replaceable"><code>parent_table</code></em>
65 NO INHERIT <em class="replaceable"><code>parent_table</code></em>
66 OF <em class="replaceable"><code>type_name</code></em>
68 OWNER TO { <em class="replaceable"><code>new_owner</code></em> | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
69 REPLICA IDENTITY { DEFAULT | USING INDEX <em class="replaceable"><code>index_name</code></em> | FULL | NOTHING }
71 <span class="phrase">and <em class="replaceable"><code>partition_bound_spec</code></em> is:</span>
73 IN ( <em class="replaceable"><code>partition_bound_expr</code></em> [, ...] ) |
74 FROM ( { <em class="replaceable"><code>partition_bound_expr</code></em> | MINVALUE | MAXVALUE } [, ...] )
75 TO ( { <em class="replaceable"><code>partition_bound_expr</code></em> | MINVALUE | MAXVALUE } [, ...] ) |
76 WITH ( MODULUS <em class="replaceable"><code>numeric_literal</code></em>, REMAINDER <em class="replaceable"><code>numeric_literal</code></em> )
78 <span class="phrase">and <em class="replaceable"><code>column_constraint</code></em> is:</span>
80 [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]
81 { NOT NULL [ NO INHERIT ] |
83 CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ] |
84 DEFAULT <em class="replaceable"><code>default_expr</code></em> |
85 GENERATED ALWAYS AS ( <em class="replaceable"><code>generation_expr</code></em> ) [ STORED | VIRTUAL ] |
86 GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <em class="replaceable"><code>sequence_options</code></em> ) ] |
87 UNIQUE [ NULLS [ NOT ] DISTINCT ] <em class="replaceable"><code>index_parameters</code></em> |
88 PRIMARY KEY <em class="replaceable"><code>index_parameters</code></em> |
89 REFERENCES <em class="replaceable"><code>reftable</code></em> [ ( <em class="replaceable"><code>refcolumn</code></em> ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
90 [ ON DELETE <em class="replaceable"><code>referential_action</code></em> ] [ ON UPDATE <em class="replaceable"><code>referential_action</code></em> ] }
91 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]
93 <span class="phrase">and <em class="replaceable"><code>table_constraint</code></em> is:</span>
95 [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]
96 { CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ] |
97 NOT NULL <em class="replaceable"><code>column_name</code></em> [ NO INHERIT ] |
98 UNIQUE [ NULLS [ NOT ] DISTINCT ] ( <em class="replaceable"><code>column_name</code></em> [, ... ] [, <em class="replaceable"><code>column_name</code></em> WITHOUT OVERLAPS ] ) <em class="replaceable"><code>index_parameters</code></em> |
99 PRIMARY KEY ( <em class="replaceable"><code>column_name</code></em> [, ... ] [, <em class="replaceable"><code>column_name</code></em> WITHOUT OVERLAPS ] ) <em class="replaceable"><code>index_parameters</code></em> |
100 EXCLUDE [ USING <em class="replaceable"><code>index_method</code></em> ] ( <em class="replaceable"><code>exclude_element</code></em> WITH <em class="replaceable"><code>operator</code></em> [, ... ] ) <em class="replaceable"><code>index_parameters</code></em> [ WHERE ( <em class="replaceable"><code>predicate</code></em> ) ] |
101 FOREIGN KEY ( <em class="replaceable"><code>column_name</code></em> [, ... ] [, PERIOD <em class="replaceable"><code>column_name</code></em> ] ) REFERENCES <em class="replaceable"><code>reftable</code></em> [ ( <em class="replaceable"><code>refcolumn</code></em> [, ... ] [, PERIOD <em class="replaceable"><code>refcolumn</code></em> ] ) ]
102 [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE <em class="replaceable"><code>referential_action</code></em> ] [ ON UPDATE <em class="replaceable"><code>referential_action</code></em> ] }
103 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]
105 <span class="phrase">and <em class="replaceable"><code>table_constraint_using_index</code></em> is:</span>
107 [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]
108 { UNIQUE | PRIMARY KEY } USING INDEX <em class="replaceable"><code>index_name</code></em>
109 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
111 <span class="phrase"><em class="replaceable"><code>index_parameters</code></em> in <code class="literal">UNIQUE</code>, <code class="literal">PRIMARY KEY</code>, and <code class="literal">EXCLUDE</code> constraints are:</span>
113 [ INCLUDE ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) ]
114 [ WITH ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] ) ]
115 [ USING INDEX TABLESPACE <em class="replaceable"><code>tablespace_name</code></em> ]
117 <span class="phrase"><em class="replaceable"><code>exclude_element</code></em> in an <code class="literal">EXCLUDE</code> constraint is:</span>
119 { <em class="replaceable"><code>column_name</code></em> | ( <em class="replaceable"><code>expression</code></em> ) } [ COLLATE <em class="replaceable"><code>collation</code></em> ] [ <em class="replaceable"><code>opclass</code></em> [ ( <em class="replaceable"><code>opclass_parameter</code></em> = <em class="replaceable"><code>value</code></em> [, ... ] ) ] ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]
121 <span class="phrase"><em class="replaceable"><code>referential_action</code></em> in a <code class="literal">FOREIGN KEY</code>/<code class="literal">REFERENCES</code> constraint is:</span>
123 { NO ACTION | RESTRICT | CASCADE | SET NULL [ ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) ] | SET DEFAULT [ ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) ] }
124 </pre></div><div class="refsect1" id="id-1.9.3.35.5"><h2>Description</h2><p>
125 <code class="command">ALTER TABLE</code> changes the definition of an existing table.
126 There are several subforms described below. Note that the lock level required
127 may differ for each subform. An <code class="literal">ACCESS EXCLUSIVE</code> lock is
128 acquired unless explicitly noted. When multiple subcommands are given, the
129 lock acquired will be the strictest one required by any subcommand.
131 </p><div class="variablelist"><dl class="variablelist"><dt id="SQL-ALTERTABLE-DESC-ADD-COLUMN"><span class="term"><code class="literal">ADD COLUMN [ IF NOT EXISTS ]</code></span> <a href="#SQL-ALTERTABLE-DESC-ADD-COLUMN" class="id_link">#</a></dt><dd><p>
132 This form adds a new column to the table, using the same syntax as
133 <a class="link" href="sql-createtable.html" title="CREATE TABLE"><code class="command">CREATE TABLE</code></a>. If <code class="literal">IF NOT EXISTS</code>
134 is specified and a column already exists with this name,
136 </p></dd><dt id="SQL-ALTERTABLE-DESC-DROP-COLUMN"><span class="term"><code class="literal">DROP COLUMN [ IF EXISTS ]</code></span> <a href="#SQL-ALTERTABLE-DESC-DROP-COLUMN" class="id_link">#</a></dt><dd><p>
137 This form drops a column from a table. Indexes and
138 table constraints involving the column will be automatically
140 Multivariate statistics referencing the dropped column will also be
141 removed if the removal of the column would cause the statistics to
142 contain data for only a single column.
143 You will need to say <code class="literal">CASCADE</code> if anything outside the table
144 depends on the column, for example, foreign key references or views.
145 If <code class="literal">IF EXISTS</code> is specified and the column
146 does not exist, no error is thrown. In this case a notice
148 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-DATA-TYPE"><span class="term"><code class="literal">SET DATA TYPE</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-DATA-TYPE" class="id_link">#</a></dt><dd><p>
149 This form changes the type of a column of a table. Indexes and
150 simple table constraints involving the column will be automatically
151 converted to use the new column type by reparsing the originally
153 The optional <code class="literal">COLLATE</code> clause specifies a collation
154 for the new column; if omitted, the collation is the default for the
156 The optional <code class="literal">USING</code>
157 clause specifies how to compute the new column value from the old;
158 if omitted, the default conversion is the same as an assignment
159 cast from old data type to new. A <code class="literal">USING</code>
160 clause must be provided if there is no implicit or assignment
161 cast from old to new type.
163 When this form is used, the column's statistics are removed,
164 so running <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a>
165 on the table afterwards is recommended.
166 For a virtual generated column, <code class="command">ANALYZE</code>
167 is not necessary because such columns never have statistics.
168 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-DROP-DEFAULT"><span class="term"><code class="literal">SET</code>/<code class="literal">DROP DEFAULT</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-DROP-DEFAULT" class="id_link">#</a></dt><dd><p>
169 These forms set or remove the default value for a column (where
170 removal is equivalent to setting the default value to NULL). The new
171 default value will only apply in subsequent <code class="command">INSERT</code>
172 or <code class="command">UPDATE</code> commands; it does not cause rows already
173 in the table to change.
174 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-DROP-NOT-NULL"><span class="term"><code class="literal">SET</code>/<code class="literal">DROP NOT NULL</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-DROP-NOT-NULL" class="id_link">#</a></dt><dd><p>
175 These forms change whether a column is marked to allow null
176 values or to reject null values.
178 <code class="literal">SET NOT NULL</code> may only be applied to a column
179 provided none of the records in the table contain a
180 <code class="literal">NULL</code> value for the column. Ordinarily this is
181 checked during the <code class="literal">ALTER TABLE</code> by scanning the
182 entire table, unless <code class="literal">NOT VALID</code> is specified;
183 however, if a valid <code class="literal">CHECK</code> constraint exists
184 (and is not dropped in the same command) which proves no
185 <code class="literal">NULL</code> can exist, then the table scan is skipped.
186 If a column has an invalid not-null constraint,
187 <code class="literal">SET NOT NULL</code> validates it.
189 If this table is a partition, one cannot perform <code class="literal">DROP NOT NULL</code>
190 on a column if it is marked <code class="literal">NOT NULL</code> in the parent
191 table. To drop the <code class="literal">NOT NULL</code> constraint from all the
192 partitions, perform <code class="literal">DROP NOT NULL</code> on the parent
193 table. Even if there is no <code class="literal">NOT NULL</code> constraint on the
194 parent, such a constraint can still be added to individual partitions,
195 if desired; that is, the children can disallow nulls even if the parent
196 allows them, but not the other way around. It is also possible to drop
197 the <code class="literal">NOT NULL</code> constraint from <code class="literal">ONLY</code>
198 the parent table, which does not remove it from the children.
199 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-EXPRESSION"><span class="term"><code class="literal">SET EXPRESSION AS</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-EXPRESSION" class="id_link">#</a></dt><dd><p>
200 This form replaces the expression of a generated column. Existing data
201 in a stored generated column is rewritten and all the future changes
202 will apply the new generation expression.
204 When this form is used on a stored generated column, its statistics
205 are removed, so running
206 <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a>
207 on the table afterwards is recommended.
208 For a virtual generated column, <code class="command">ANALYZE</code>
209 is not necessary because such columns never have statistics.
210 </p></dd><dt id="SQL-ALTERTABLE-DESC-DROP-EXPRESSION"><span class="term"><code class="literal">DROP EXPRESSION [ IF EXISTS ]</code></span> <a href="#SQL-ALTERTABLE-DESC-DROP-EXPRESSION" class="id_link">#</a></dt><dd><p>
211 This form turns a stored generated column into a normal base column.
212 Existing data in the columns is retained, but future changes will no
213 longer apply the generation expression.
215 This form is currently only supported for stored generated columns (not
218 If <code class="literal">DROP EXPRESSION IF EXISTS</code> is specified and the
219 column is not a generated column, no error is thrown. In this case a
220 notice is issued instead.
221 </p></dd><dt id="SQL-ALTERTABLE-DESC-GENERATED-IDENTITY"><span class="term"><code class="literal">ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY</code><br /></span><span class="term"><code class="literal">SET GENERATED { ALWAYS | BY DEFAULT }</code><br /></span><span class="term"><code class="literal">DROP IDENTITY [ IF EXISTS ]</code></span> <a href="#SQL-ALTERTABLE-DESC-GENERATED-IDENTITY" class="id_link">#</a></dt><dd><p>
222 These forms change whether a column is an identity column or change the
223 generation attribute of an existing identity column.
224 See <a class="link" href="sql-createtable.html" title="CREATE TABLE"><code class="command">CREATE TABLE</code></a> for details.
225 Like <code class="literal">SET DEFAULT</code>, these forms only affect the
226 behavior of subsequent <code class="command">INSERT</code>
227 and <code class="command">UPDATE</code> commands; they do not cause rows
228 already in the table to change.
230 If <code class="literal">DROP IDENTITY IF EXISTS</code> is specified and the
231 column is not an identity column, no error is thrown. In this case a
232 notice is issued instead.
233 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-SEQUENCE-OPTION"><span class="term"><code class="literal">SET <em class="replaceable"><code>sequence_option</code></em></code><br /></span><span class="term"><code class="literal">RESTART</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-SEQUENCE-OPTION" class="id_link">#</a></dt><dd><p>
234 These forms alter the sequence that underlies an existing identity
235 column. <em class="replaceable"><code>sequence_option</code></em> is an option
236 supported by <a class="link" href="sql-altersequence.html" title="ALTER SEQUENCE"><code class="command">ALTER SEQUENCE</code></a> such
237 as <code class="literal">INCREMENT BY</code>.
238 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-STATISTICS"><span class="term"><code class="literal">SET STATISTICS</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-STATISTICS" class="id_link">#</a></dt><dd><p>
240 sets the per-column statistics-gathering target for subsequent
241 <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a> operations.
242 The target can be set in the range 0 to 10000. Set it
243 to <code class="literal">DEFAULT</code> to revert to using the system default
244 statistics target (<a class="xref" href="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET">default_statistics_target</a>).
245 (Setting to a value of -1 is an obsolete way spelling to get the same
247 For more information on the use of statistics by the
248 <span class="productname">PostgreSQL</span> query planner, refer to
249 <a class="xref" href="planner-stats.html" title="14.2. Statistics Used by the Planner">Section 14.2</a>.
251 <code class="literal">SET STATISTICS</code> acquires a
252 <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.
253 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-ATTRIBUTE-OPTION"><span class="term"><code class="literal">SET ( <em class="replaceable"><code>attribute_option</code></em> = <em class="replaceable"><code>value</code></em> [, ... ] )</code><br /></span><span class="term"><code class="literal">RESET ( <em class="replaceable"><code>attribute_option</code></em> [, ... ] )</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-ATTRIBUTE-OPTION" class="id_link">#</a></dt><dd><p>
254 This form sets or resets per-attribute options. Currently, the only
255 defined per-attribute options are <code class="literal">n_distinct</code> and
256 <code class="literal">n_distinct_inherited</code>, which override the
257 number-of-distinct-values estimates made by subsequent
258 <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a>
259 operations. <code class="literal">n_distinct</code> affects the statistics for the table
260 itself, while <code class="literal">n_distinct_inherited</code> affects the statistics
261 gathered for the table plus its inheritance children. When set to a
262 positive value, <code class="command">ANALYZE</code> will assume that the column contains
263 exactly the specified number of distinct nonnull values. When set to a
264 negative value, which must be greater
265 than or equal to -1, <code class="command">ANALYZE</code> will assume that the number of
266 distinct nonnull values in the column is linear in the size of the
267 table; the exact count is to be computed by multiplying the estimated
268 table size by the absolute value of the given number. For example,
269 a value of -1 implies that all values in the column are distinct, while
270 a value of -0.5 implies that each value appears twice on the average.
271 This can be useful when the size of the table changes over time, since
272 the multiplication by the number of rows in the table is not performed
273 until query planning time. Specify a value of 0 to revert to estimating
274 the number of distinct values normally. For more information on the use
275 of statistics by the <span class="productname">PostgreSQL</span> query
276 planner, refer to <a class="xref" href="planner-stats.html" title="14.2. Statistics Used by the Planner">Section 14.2</a>.
278 Changing per-attribute options acquires a
279 <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.
280 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-STORAGE"><span class="term">
281 <code class="literal">SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }</code>
282 <a id="id-1.9.3.35.5.2.3.12.1.2" class="indexterm"></a>
283 </span> <a href="#SQL-ALTERTABLE-DESC-SET-STORAGE" class="id_link">#</a></dt><dd><p>
284 This form sets the storage mode for a column. This controls whether this
285 column is held inline or in a secondary <acronym class="acronym">TOAST</acronym> table,
287 should be compressed or not. <code class="literal">PLAIN</code> must be used
288 for fixed-length values such as <code class="type">integer</code> and is
289 inline, uncompressed. <code class="literal">MAIN</code> is for inline,
290 compressible data. <code class="literal">EXTERNAL</code> is for external,
291 uncompressed data, and <code class="literal">EXTENDED</code> is for external,
293 Writing <code class="literal">DEFAULT</code> sets the storage mode to the default
294 mode for the column's data type. <code class="literal">EXTENDED</code> is the
295 default for most data types that support non-<code class="literal">PLAIN</code>
297 Use of <code class="literal">EXTERNAL</code> will make substring operations on
298 very large <code class="type">text</code> and <code class="type">bytea</code> values run faster,
299 at the penalty of increased storage space.
300 Note that <code class="literal">ALTER TABLE ... SET STORAGE</code> doesn't itself
301 change anything in the table; it just sets the strategy to be pursued
302 during future table updates.
303 See <a class="xref" href="storage-toast.html" title="66.2. TOAST">Section 66.2</a> for more information.
304 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-COMPRESSION"><span class="term">
305 <code class="literal">SET COMPRESSION <em class="replaceable"><code>compression_method</code></em></code>
306 </span> <a href="#SQL-ALTERTABLE-DESC-SET-COMPRESSION" class="id_link">#</a></dt><dd><p>
307 This form sets the compression method for a column, determining how
308 values inserted in future will be compressed (if the storage mode
309 permits compression at all).
310 This does not cause the table to be rewritten, so existing data may still
311 be compressed with other compression methods. If the table is restored
312 with <span class="application">pg_restore</span>, then all values are rewritten
313 with the configured compression method.
314 However, when data is inserted from another relation (for example,
315 by <code class="command">INSERT ... SELECT</code>), values from the source table are
316 not necessarily detoasted, so any previously compressed data may retain
317 its existing compression method, rather than being recompressed with the
318 compression method of the target column.
319 The supported compression
320 methods are <code class="literal">pglz</code> and <code class="literal">lz4</code>.
321 (<code class="literal">lz4</code> is available only if <code class="option">--with-lz4</code>
322 was used when building <span class="productname">PostgreSQL</span>.) In
323 addition, <em class="replaceable"><code>compression_method</code></em>
324 can be <code class="literal">default</code>, which selects the default behavior of
325 consulting the <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TOAST-COMPRESSION">default_toast_compression</a> setting
326 at the time of data insertion to determine the method to use.
327 </p></dd><dt id="SQL-ALTERTABLE-DESC-ADD-TABLE-CONSTRAINT"><span class="term"><code class="literal">ADD <em class="replaceable"><code>table_constraint</code></em> [ NOT VALID ]</code></span> <a href="#SQL-ALTERTABLE-DESC-ADD-TABLE-CONSTRAINT" class="id_link">#</a></dt><dd><p>
328 This form adds a new constraint to a table using the same constraint
329 syntax as <a class="link" href="sql-createtable.html" title="CREATE TABLE"><code class="command">CREATE TABLE</code></a>, plus the option <code class="literal">NOT
330 VALID</code>, which is currently only allowed for foreign-key,
331 <code class="literal">CHECK</code>, and not-null constraints.
333 Normally, this form will cause a scan of the table to verify that all
334 existing rows in the table satisfy the new constraint. But if
335 the <code class="literal">NOT VALID</code> option is used, this
336 potentially-lengthy scan is skipped. The constraint will still be
337 applied against subsequent inserts or updates (that is, they'll fail
338 unless there is a matching row in the referenced table, in the case
339 of foreign keys, or they'll fail unless the new row matches the
340 specified check condition). But the
341 database will not assume that the constraint holds for all rows in
342 the table, until it is validated by using the <code class="literal">VALIDATE
343 CONSTRAINT</code> option.
344 See <a class="xref" href="sql-altertable.html#SQL-ALTERTABLE-NOTES" title="Notes">Notes</a> below for more information
345 about using the <code class="literal">NOT VALID</code> option.
347 Although most forms of <code class="literal">ADD
348 <em class="replaceable"><code>table_constraint</code></em></code>
349 require an <code class="literal">ACCESS EXCLUSIVE</code> lock, <code class="literal">ADD
350 FOREIGN KEY</code> requires only a <code class="literal">SHARE ROW
351 EXCLUSIVE</code> lock. Note that <code class="literal">ADD FOREIGN KEY</code>
352 also acquires a <code class="literal">SHARE ROW EXCLUSIVE</code> lock on the
353 referenced table, in addition to the lock on the table on which the
354 constraint is declared.
356 Additional restrictions apply when unique or primary key constraints
357 are added to partitioned tables; see <a class="link" href="sql-createtable.html" title="CREATE TABLE"><code class="command">CREATE TABLE</code></a>.
358 </p></dd><dt id="SQL-ALTERTABLE-DESC-ADD-TABLE-CONSTRAINT-USING-INDEX"><span class="term"><code class="literal">ADD <em class="replaceable"><code>table_constraint_using_index</code></em></code></span> <a href="#SQL-ALTERTABLE-DESC-ADD-TABLE-CONSTRAINT-USING-INDEX" class="id_link">#</a></dt><dd><p>
359 This form adds a new <code class="literal">PRIMARY KEY</code> or <code class="literal">UNIQUE</code>
360 constraint to a table based on an existing unique index. All the
361 columns of the index will be included in the constraint.
363 The index cannot have expression columns nor be a partial index.
364 Also, it must be a b-tree index with default sort ordering. These
365 restrictions ensure that the index is equivalent to one that would be
366 built by a regular <code class="literal">ADD PRIMARY KEY</code> or <code class="literal">ADD UNIQUE</code>
369 If <code class="literal">PRIMARY KEY</code> is specified, and the index's columns are not
370 already marked <code class="literal">NOT NULL</code>, then this command will attempt to
371 do <code class="literal">ALTER COLUMN SET NOT NULL</code> against each such column.
372 That requires a full table scan to verify the column(s) contain no
373 nulls. In all other cases, this is a fast operation.
375 If a constraint name is provided then the index will be renamed to match
376 the constraint name. Otherwise the constraint will be named the same as
379 After this command is executed, the index is <span class="quote">“<span class="quote">owned</span>”</span> by the
380 constraint, in the same way as if the index had been built by
381 a regular <code class="literal">ADD PRIMARY KEY</code> or <code class="literal">ADD UNIQUE</code>
382 command. In particular, dropping the constraint will make the index
385 This form is not currently supported on partitioned tables.
386 </p><div class="note"><h3 class="title">Note</h3><p>
387 Adding a constraint using an existing index can be helpful in
388 situations where a new constraint needs to be added without blocking
389 table updates for a long time. To do that, create the index using
390 <code class="command">CREATE UNIQUE INDEX CONCURRENTLY</code>, and then convert it to a
391 constraint using this syntax. See the example below.
392 </p></div></dd><dt id="SQL-ALTERTABLE-DESC-ALTER-CONSTRAINT"><span class="term"><code class="literal">ALTER CONSTRAINT</code></span> <a href="#SQL-ALTERTABLE-DESC-ALTER-CONSTRAINT" class="id_link">#</a></dt><dd><p>
393 This form alters the attributes of a constraint that was previously
394 created. Currently only foreign key constraints may be altered in
395 this fashion, but see below.
396 </p></dd><dt id="SQL-ALTERTABLE-DESC-ALTER-CONSTRAINT-INHERIT"><span class="term"><code class="literal">ALTER CONSTRAINT ... INHERIT</code><br /></span><span class="term"><code class="literal">ALTER CONSTRAINT ... NO INHERIT</code></span> <a href="#SQL-ALTERTABLE-DESC-ALTER-CONSTRAINT-INHERIT" class="id_link">#</a></dt><dd><p>
397 These forms modify a inheritable constraint so that it becomes not
398 inheritable, or vice-versa. Only not-null constraints may be altered
399 in this fashion at present.
400 In addition to changing the inheritability status of the constraint,
401 in the case where a non-inheritable constraint is being marked
402 inheritable, if the table has children, an equivalent constraint
403 will be added to them. If marking an inheritable constraint as
404 non-inheritable on a table with children, then the corresponding
405 constraint on children will be marked as no longer inherited,
407 </p></dd><dt id="SQL-ALTERTABLE-DESC-VALIDATE-CONSTRAINT"><span class="term"><code class="literal">VALIDATE CONSTRAINT</code></span> <a href="#SQL-ALTERTABLE-DESC-VALIDATE-CONSTRAINT" class="id_link">#</a></dt><dd><p>
408 This form validates a foreign key, check, or not-null constraint that was
409 previously created as <code class="literal">NOT VALID</code>, by scanning the
410 table to ensure there are no rows for which the constraint is not
411 satisfied. If the constraint was set to <code class="literal">NOT ENFORCED</code>, an error is thrown.
412 Nothing happens if the constraint is already marked valid.
413 (See <a class="xref" href="sql-altertable.html#SQL-ALTERTABLE-NOTES" title="Notes">Notes</a> below for an explanation
414 of the usefulness of this command.)
416 This command acquires a <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.
417 </p></dd><dt id="SQL-ALTERTABLE-DESC-DROP-CONSTRAINT"><span class="term"><code class="literal">DROP CONSTRAINT [ IF EXISTS ]</code></span> <a href="#SQL-ALTERTABLE-DESC-DROP-CONSTRAINT" class="id_link">#</a></dt><dd><p>
418 This form drops the specified constraint on a table, along with
419 any index underlying the constraint.
420 If <code class="literal">IF EXISTS</code> is specified and the constraint
421 does not exist, no error is thrown. In this case a notice is issued instead.
422 </p></dd><dt id="SQL-ALTERTABLE-DESC-DISABLE-ENABLE-TRIGGER"><span class="term"><code class="literal">DISABLE</code>/<code class="literal">ENABLE [ REPLICA | ALWAYS ] TRIGGER</code></span> <a href="#SQL-ALTERTABLE-DESC-DISABLE-ENABLE-TRIGGER" class="id_link">#</a></dt><dd><p>
423 These forms configure the firing of trigger(s) belonging to the table.
424 A disabled trigger is still known to the system, but is not executed
425 when its triggering event occurs. (For a deferred trigger, the enable
426 status is checked when the event occurs, not when the trigger function
427 is actually executed.) One can disable or enable a single
428 trigger specified by name, or all triggers on the table, or only
429 user triggers (this option excludes internally generated constraint
430 triggers, such as those that are used to implement foreign key
431 constraints or deferrable uniqueness and exclusion constraints).
432 Disabling or enabling internally generated constraint triggers
433 requires superuser privileges; it should be done with caution since
434 of course the integrity of the constraint cannot be guaranteed if the
435 triggers are not executed.
437 The trigger firing mechanism is also affected by the configuration
438 variable <a class="xref" href="runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE">session_replication_role</a>. Simply enabled
439 triggers (the default) will fire when the replication role is <span class="quote">“<span class="quote">origin</span>”</span>
440 (the default) or <span class="quote">“<span class="quote">local</span>”</span>. Triggers configured as <code class="literal">ENABLE
441 REPLICA</code> will only fire if the session is in <span class="quote">“<span class="quote">replica</span>”</span>
442 mode, and triggers configured as <code class="literal">ENABLE ALWAYS</code> will
443 fire regardless of the current replication role.
445 The effect of this mechanism is that in the default configuration,
446 triggers do not fire on replicas. This is useful because if a trigger
447 is used on the origin to propagate data between tables, then the
448 replication system will also replicate the propagated data; so the
449 trigger should not fire a second time on the replica, because that would
450 lead to duplication. However, if a trigger is used for another purpose
451 such as creating external alerts, then it might be appropriate to set it
452 to <code class="literal">ENABLE ALWAYS</code> so that it is also fired on
455 When this command is applied to a partitioned table, the states of
456 corresponding clone triggers in the partitions are updated too,
457 unless <code class="literal">ONLY</code> is specified.
459 This command acquires a <code class="literal">SHARE ROW EXCLUSIVE</code> lock.
460 </p></dd><dt id="SQL-ALTERTABLE-DESC-DISABLE-ENABLE-RULE"><span class="term"><code class="literal">DISABLE</code>/<code class="literal">ENABLE [ REPLICA | ALWAYS ] RULE</code></span> <a href="#SQL-ALTERTABLE-DESC-DISABLE-ENABLE-RULE" class="id_link">#</a></dt><dd><p>
461 These forms configure the firing of rewrite rules belonging to the table.
462 A disabled rule is still known to the system, but is not applied
463 during query rewriting. The semantics are as for disabled/enabled
464 triggers. This configuration is ignored for <code class="literal">ON SELECT</code> rules, which
465 are always applied in order to keep views working even if the current
466 session is in a non-default replication role.
468 The rule firing mechanism is also affected by the configuration variable
469 <a class="xref" href="runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE">session_replication_role</a>, analogous to triggers as
471 </p></dd><dt id="SQL-ALTERTABLE-DESC-DISABLE-ENABLE-ROW-LEVEL-SECURITY"><span class="term"><code class="literal">DISABLE</code>/<code class="literal">ENABLE ROW LEVEL SECURITY</code></span> <a href="#SQL-ALTERTABLE-DESC-DISABLE-ENABLE-ROW-LEVEL-SECURITY" class="id_link">#</a></dt><dd><p>
472 These forms control the application of row security policies belonging
473 to the table. If enabled and no policies exist for the table, then a
474 default-deny policy is applied. Note that policies can exist for a table
475 even if row-level security is disabled. In this case, the policies will
476 <span class="emphasis"><em>not</em></span> be applied and the policies will be ignored.
478 <a class="link" href="sql-createpolicy.html" title="CREATE POLICY"><code class="command">CREATE POLICY</code></a>.
479 </p></dd><dt id="SQL-ALTERTABLE-DESC-FORCE-ROW-LEVEL-SECURITY"><span class="term"><code class="literal">NO FORCE</code>/<code class="literal">FORCE ROW LEVEL SECURITY</code></span> <a href="#SQL-ALTERTABLE-DESC-FORCE-ROW-LEVEL-SECURITY" class="id_link">#</a></dt><dd><p>
480 These forms control the application of row security policies belonging
481 to the table when the user is the table owner. If enabled, row-level
482 security policies will be applied when the user is the table owner. If
483 disabled (the default) then row-level security will not be applied when
484 the user is the table owner.
486 <a class="link" href="sql-createpolicy.html" title="CREATE POLICY"><code class="command">CREATE POLICY</code></a>.
487 </p></dd><dt id="SQL-ALTERTABLE-DESC-CLUSTER-ON"><span class="term"><code class="literal">CLUSTER ON</code></span> <a href="#SQL-ALTERTABLE-DESC-CLUSTER-ON" class="id_link">#</a></dt><dd><p>
488 This form selects the default index for future
489 <a class="link" href="sql-cluster.html" title="CLUSTER"><code class="command">CLUSTER</code></a>
490 operations. It does not actually re-cluster the table.
492 Changing cluster options acquires a <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.
493 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-WITHOUT-CLUSTER"><span class="term"><code class="literal">SET WITHOUT CLUSTER</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-WITHOUT-CLUSTER" class="id_link">#</a></dt><dd><p>
494 This form removes the most recently used
495 <a class="link" href="sql-cluster.html" title="CLUSTER"><code class="command">CLUSTER</code></a>
496 index specification from the table. This affects
497 future cluster operations that don't specify an index.
499 Changing cluster options acquires a <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.
500 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-WITHOUT-OIDS"><span class="term"><code class="literal">SET WITHOUT OIDS</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-WITHOUT-OIDS" class="id_link">#</a></dt><dd><p>
501 Backward-compatible syntax for removing the <code class="literal">oid</code>
502 system column. As <code class="literal">oid</code> system columns cannot be
503 added anymore, this never has an effect.
504 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-ACCESS-METHOD"><span class="term"><code class="literal">SET ACCESS METHOD</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-ACCESS-METHOD" class="id_link">#</a></dt><dd><p>
505 This form changes the access method of the table by rewriting it
506 using the indicated access method; specifying
507 <code class="literal">DEFAULT</code> selects the access method set as the
508 <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD">default_table_access_method</a> configuration
510 See <a class="xref" href="tableam.html" title="Chapter 62. Table Access Method Interface Definition">Chapter 62</a> for more information.
512 When applied to a partitioned table, there is no data to rewrite,
513 but partitions created afterwards will default to the given access
514 method unless overridden by a <code class="literal">USING</code> clause.
515 Specifying <code class="varname">DEFAULT</code> removes a previous value,
516 causing future partitions to default to
517 <code class="varname">default_table_access_method</code>.
518 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-TABLESPACE"><span class="term"><code class="literal">SET TABLESPACE</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-TABLESPACE" class="id_link">#</a></dt><dd><p>
519 This form changes the table's tablespace to the specified tablespace and
520 moves the data file(s) associated with the table to the new tablespace.
521 Indexes on the table, if any, are not moved; but they can be moved
522 separately with additional <code class="literal">SET TABLESPACE</code> commands.
523 When applied to a partitioned table, nothing is moved, but any
524 partitions created afterwards with
525 <code class="command">CREATE TABLE PARTITION OF</code> will use that tablespace,
526 unless overridden by a <code class="literal">TABLESPACE</code> clause.
528 All tables in the current database in a tablespace can be moved by using
529 the <code class="literal">ALL IN TABLESPACE</code> form, which will lock all tables
530 to be moved first and then move each one. This form also supports
531 <code class="literal">OWNED BY</code>, which will only move tables owned by the
532 roles specified. If the <code class="literal">NOWAIT</code> option is specified
533 then the command will fail if it is unable to acquire all of the locks
534 required immediately. Note that system catalogs are not moved by this
535 command; use <code class="command">ALTER DATABASE</code> or explicit
536 <code class="command">ALTER TABLE</code> invocations instead if desired. The
537 <code class="literal">information_schema</code> relations are not considered part
538 of the system catalogs and will be moved.
540 <a class="link" href="sql-createtablespace.html" title="CREATE TABLESPACE"><code class="command">CREATE TABLESPACE</code></a>.
541 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-LOGGED-UNLOGGED"><span class="term"><code class="literal">SET { LOGGED | UNLOGGED }</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-LOGGED-UNLOGGED" class="id_link">#</a></dt><dd><p>
542 This form changes the table from unlogged to logged or vice-versa
543 (see <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-UNLOGGED"><code class="literal">UNLOGGED</code></a>). It cannot be applied
544 to a temporary table.
546 This also changes the persistence of any sequences linked to the table
547 (for identity or serial columns). However, it is also possible to
548 change the persistence of such sequences separately.
550 This form is not supported for partitioned tables.
551 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-STORAGE-PARAMETER"><span class="term"><code class="literal">SET ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] )</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-STORAGE-PARAMETER" class="id_link">#</a></dt><dd><p>
552 This form changes one or more storage parameters for the table. See
553 <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS" title="Storage Parameters">Storage Parameters</a> in the
554 <a class="link" href="sql-createtable.html" title="CREATE TABLE"><code class="command">CREATE TABLE</code></a> documentation
555 for details on the available parameters. Note that the table contents
556 will not be modified immediately by this command; depending on the
557 parameter you might need to rewrite the table to get the desired effects.
558 That can be done with <a class="link" href="sql-vacuum.html" title="VACUUM"><code class="command">VACUUM
559 FULL</code></a>, <a class="link" href="sql-cluster.html" title="CLUSTER"><code class="command">CLUSTER</code></a> or one of the forms
560 of <code class="command">ALTER TABLE</code> that forces a table rewrite.
561 For planner related parameters, changes will take effect from the next
562 time the table is locked so currently executing queries will not be
565 <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock will be taken for
566 fillfactor, toast and autovacuum storage parameters, as well as the
567 planner parameter <code class="varname">parallel_workers</code>.
568 </p></dd><dt id="SQL-ALTERTABLE-DESC-RESET-STORAGE-PARAMETER"><span class="term"><code class="literal">RESET ( <em class="replaceable"><code>storage_parameter</code></em> [, ... ] )</code></span> <a href="#SQL-ALTERTABLE-DESC-RESET-STORAGE-PARAMETER" class="id_link">#</a></dt><dd><p>
569 This form resets one or more storage parameters to their
570 defaults. As with <code class="literal">SET</code>, a table rewrite might be
571 needed to update the table entirely.
572 </p></dd><dt id="SQL-ALTERTABLE-DESC-INHERIT"><span class="term"><code class="literal">INHERIT <em class="replaceable"><code>parent_table</code></em></code></span> <a href="#SQL-ALTERTABLE-DESC-INHERIT" class="id_link">#</a></dt><dd><p>
573 This form adds the target table as a new child of the specified parent
574 table. Subsequently, queries against the parent will include records
575 of the target table. To be added as a child, the target table must
576 already contain all the same columns as the parent (it could have
577 additional columns, too). The columns must have matching data types.
579 In addition, all <code class="literal">CHECK</code> and <code class="literal">NOT NULL</code>
580 constraints on the parent must also exist on the child, except those
581 marked non-inheritable (that is, created with
582 <code class="literal">ALTER TABLE ... ADD CONSTRAINT ... NO INHERIT</code>), which
583 are ignored. All child-table constraints matched must not be marked
584 non-inheritable. Currently
585 <code class="literal">UNIQUE</code>, <code class="literal">PRIMARY KEY</code>, and
586 <code class="literal">FOREIGN KEY</code> constraints are not considered, but
587 this might change in the future.
588 </p></dd><dt id="SQL-ALTERTABLE-DESC-NO-INHERIT"><span class="term"><code class="literal">NO INHERIT <em class="replaceable"><code>parent_table</code></em></code></span> <a href="#SQL-ALTERTABLE-DESC-NO-INHERIT" class="id_link">#</a></dt><dd><p>
589 This form removes the target table from the list of children of the
590 specified parent table.
591 Queries against the parent table will no longer include records drawn
592 from the target table.
593 </p></dd><dt id="SQL-ALTERTABLE-DESC-OF"><span class="term"><code class="literal">OF <em class="replaceable"><code>type_name</code></em></code></span> <a href="#SQL-ALTERTABLE-DESC-OF" class="id_link">#</a></dt><dd><p>
594 This form links the table to a composite type as though <code class="command">CREATE
595 TABLE OF</code> had formed it. The table's list of column names and types
596 must precisely match that of the composite type. The table must
597 not inherit from any other table. These restrictions ensure
598 that <code class="command">CREATE TABLE OF</code> would permit an equivalent table
600 </p></dd><dt id="SQL-ALTERTABLE-DESC-NOT-OF"><span class="term"><code class="literal">NOT OF</code></span> <a href="#SQL-ALTERTABLE-DESC-NOT-OF" class="id_link">#</a></dt><dd><p>
601 This form dissociates a typed table from its type.
602 </p></dd><dt id="SQL-ALTERTABLE-DESC-OWNER-TO"><span class="term"><code class="literal">OWNER TO</code></span> <a href="#SQL-ALTERTABLE-DESC-OWNER-TO" class="id_link">#</a></dt><dd><p>
603 This form changes the owner of the table, sequence, view, materialized view,
604 or foreign table to the specified user.
605 </p></dd><dt id="SQL-ALTERTABLE-REPLICA-IDENTITY"><span class="term"><code class="literal">REPLICA IDENTITY</code></span> <a href="#SQL-ALTERTABLE-REPLICA-IDENTITY" class="id_link">#</a></dt><dd><p>
606 This form changes the information which is written to the write-ahead log
607 to identify rows which are updated or deleted.
608 In most cases, the old value of each column is only logged if it differs
609 from the new value; however, if the old value is stored externally, it is
610 always logged regardless of whether it changed.
611 This option has no effect except when logical replication is in use.
612 </p><div class="variablelist"><dl class="variablelist"><dt id="SQL-ALTERTABLE-REPLICA-IDENTITY-DEFAULT"><span class="term"><code class="literal">DEFAULT</code></span> <a href="#SQL-ALTERTABLE-REPLICA-IDENTITY-DEFAULT" class="id_link">#</a></dt><dd><p>
613 Records the old values of the columns of the primary key.
614 This is the default for non-system tables.
615 When there is no primary key, the behavior is the same as <code class="literal">NOTHING</code>.
616 </p></dd><dt id="SQL-ALTERTABLE-REPLICA-IDENTITY-USING-INDEX"><span class="term"><code class="literal">USING INDEX <em class="replaceable"><code>index_name</code></em></code></span> <a href="#SQL-ALTERTABLE-REPLICA-IDENTITY-USING-INDEX" class="id_link">#</a></dt><dd><p>
617 Records the old values of the columns covered by the named index,
618 that must be unique, not partial, not deferrable, and include only
619 columns marked <code class="literal">NOT NULL</code>. If this index is
620 dropped, the behavior is the same as <code class="literal">NOTHING</code>.
621 </p></dd><dt id="SQL-ALTERTABLE-REPLICA-IDENTITY-FULL"><span class="term"><code class="literal">FULL</code></span> <a href="#SQL-ALTERTABLE-REPLICA-IDENTITY-FULL" class="id_link">#</a></dt><dd><p>
622 Records the old values of all columns in the row.
623 </p></dd><dt id="SQL-ALTERTABLE-REPLICA-IDENTITY-NOTHING"><span class="term"><code class="literal">NOTHING</code></span> <a href="#SQL-ALTERTABLE-REPLICA-IDENTITY-NOTHING" class="id_link">#</a></dt><dd><p>
624 Records no information about the old row. This is the default for
626 </p></dd></dl></div></dd><dt id="SQL-ALTERTABLE-DESC-RENAME"><span class="term"><code class="literal">RENAME</code></span> <a href="#SQL-ALTERTABLE-DESC-RENAME" class="id_link">#</a></dt><dd><p>
627 The <code class="literal">RENAME</code> forms change the name of a table
628 (or an index, sequence, view, materialized view, or foreign table), the
629 name of an individual column in a table, or the name of a constraint of
630 the table. When renaming a constraint that has an underlying index,
631 the index is renamed as well.
632 There is no effect on the stored data.
633 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-SCHEMA"><span class="term"><code class="literal">SET SCHEMA</code></span> <a href="#SQL-ALTERTABLE-DESC-SET-SCHEMA" class="id_link">#</a></dt><dd><p>
634 This form moves the table into another schema. Associated indexes,
635 constraints, and sequences owned by table columns are moved as well.
636 </p></dd><dt id="SQL-ALTERTABLE-ATTACH-PARTITION"><span class="term"><code class="literal">ATTACH PARTITION <em class="replaceable"><code>partition_name</code></em> { FOR VALUES <em class="replaceable"><code>partition_bound_spec</code></em> | DEFAULT }</code></span> <a href="#SQL-ALTERTABLE-ATTACH-PARTITION" class="id_link">#</a></dt><dd><p>
637 This form attaches an existing table (which might itself be partitioned)
638 as a partition of the target table. The table can be attached
639 as a partition for specific values using <code class="literal">FOR VALUES</code>
640 or as a default partition by using <code class="literal">DEFAULT</code>.
641 For each index in the target table, a corresponding
642 one will be created in the attached table; or, if an equivalent
643 index already exists, it will be attached to the target table's index,
644 as if <code class="command">ALTER INDEX ATTACH PARTITION</code> had been executed.
645 Note that if the existing table is a foreign table, it is currently not
646 allowed to attach the table as a partition of the target table if there
647 are <code class="literal">UNIQUE</code> indexes on the target table. (See also
648 <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a>.) For each user-defined
649 row-level trigger that exists in the target table, a corresponding one
650 is created in the attached table.
652 A partition using <code class="literal">FOR VALUES</code> uses same syntax for
653 <em class="replaceable"><code>partition_bound_spec</code></em> as
654 <a class="link" href="sql-createtable.html" title="CREATE TABLE"><code class="command">CREATE TABLE</code></a>.
655 The partition bound specification
656 must correspond to the partitioning strategy and partition key of the
657 target table. The table to be attached must have all the same columns
658 as the target table and no more; moreover, the column types must also
659 match. Also, it must have all the <code class="literal">NOT NULL</code> and
660 <code class="literal">CHECK</code> constraints of the target table, not marked
661 <code class="literal">NO INHERIT</code>. Currently
662 <code class="literal">FOREIGN KEY</code> constraints are not considered.
663 <code class="literal">UNIQUE</code> and <code class="literal">PRIMARY KEY</code> constraints
664 from the parent table will be created in the partition, if they don't
667 If the new partition is a regular table, a full table scan is performed
668 to check that existing rows in the table do not violate the partition
669 constraint. It is possible to avoid this scan by adding a valid
670 <code class="literal">CHECK</code> constraint to the table that allows only
671 rows satisfying the desired partition constraint before running this
672 command. The <code class="literal">CHECK</code> constraint will be used to
673 determine that the table need not be scanned to validate the partition
674 constraint. This does not work, however, if any of the partition keys
675 is an expression and the partition does not accept
676 <code class="literal">NULL</code> values. If attaching a list partition that will
677 not accept <code class="literal">NULL</code> values, also add a
678 <code class="literal">NOT NULL</code> constraint to the partition key column,
679 unless it's an expression.
681 If the new partition is a foreign table, nothing is done to verify
682 that all the rows in the foreign table obey the partition constraint.
683 (See the discussion in <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a> about
684 constraints on the foreign table.)
686 When a table has a default partition, defining a new partition changes
687 the partition constraint for the default partition. The default
688 partition can't contain any rows that would need to be moved to the new
689 partition, and will be scanned to verify that none are present. This
690 scan, like the scan of the new partition, can be avoided if an
691 appropriate <code class="literal">CHECK</code> constraint is present. Also like
692 the scan of the new partition, it is always skipped when the default
693 partition is a foreign table.
695 Attaching a partition acquires a
696 <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock on the parent table,
697 in addition to the <code class="literal">ACCESS EXCLUSIVE</code> locks on the table
698 being attached and on the default partition (if any).
700 Further locks must also be held on all sub-partitions if the table being
701 attached is itself a partitioned table. Likewise if the default
702 partition is itself a partitioned table. The locking of the
703 sub-partitions can be avoided by adding a <code class="literal">CHECK</code>
704 constraint as described in
705 <a class="xref" href="ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE-MAINTENANCE" title="5.12.2.2. Partition Maintenance">Section 5.12.2.2</a>.
706 </p></dd><dt id="SQL-ALTERTABLE-DETACH-PARTITION"><span class="term"><code class="literal">DETACH PARTITION <em class="replaceable"><code>partition_name</code></em> [ CONCURRENTLY | FINALIZE ]</code></span> <a href="#SQL-ALTERTABLE-DETACH-PARTITION" class="id_link">#</a></dt><dd><p>
707 This form detaches the specified partition of the target table. The detached
708 partition continues to exist as a standalone table, but no longer has any
709 ties to the table from which it was detached. Any indexes that were
710 attached to the target table's indexes are detached. Any triggers that
711 were created as clones of those in the target table are removed.
712 <code class="literal">SHARE</code> lock is obtained on any tables that reference
713 this partitioned table in foreign key constraints.
715 If <code class="literal">CONCURRENTLY</code> is specified, it runs using a reduced
716 lock level to avoid blocking other sessions that might be accessing the
717 partitioned table. In this mode, two transactions are used internally.
718 During the first transaction, a <code class="literal">SHARE UPDATE EXCLUSIVE</code>
719 lock is taken on both parent table and partition, and the partition is
720 marked as undergoing detach; at that point, the transaction is committed
721 and all other transactions using the partitioned table are waited for.
722 Once all those transactions have completed, the second transaction
723 acquires <code class="literal">SHARE UPDATE EXCLUSIVE</code> on the partitioned
724 table and <code class="literal">ACCESS EXCLUSIVE</code> on the partition,
725 and the detach process completes. A <code class="literal">CHECK</code> constraint
726 that duplicates the partition constraint is added to the partition.
727 <code class="literal">CONCURRENTLY</code> cannot be run in a transaction block and
728 is not allowed if the partitioned table contains a default partition.
730 If <code class="literal">FINALIZE</code> is specified, a previous
731 <code class="literal">DETACH CONCURRENTLY</code> invocation that was canceled or
732 interrupted is completed.
733 At most one partition in a partitioned table can be pending detach at
735 </p></dd></dl></div><p>
737 All the forms of <code class="command">ALTER TABLE</code> that act on a single table,
738 except <code class="literal">RENAME</code>, <code class="literal">SET SCHEMA</code>,
739 <code class="literal">ATTACH PARTITION</code>, and
740 <code class="literal">DETACH PARTITION</code> can be combined into
741 a list of multiple alterations to be applied together. For example, it
742 is possible to add several columns and/or alter the type of several
743 columns in a single command. This is particularly useful with large
744 tables, since only one pass over the table need be made.
746 You must own the table to use <code class="command">ALTER TABLE</code>.
747 To change the schema or tablespace of a table, you must also have
748 <code class="literal">CREATE</code> privilege on the new schema or tablespace.
749 To add the table as a new child of a parent table, you must own the parent
750 table as well. Also, to attach a table as a new partition of the table,
751 you must own the table being attached.
752 To alter the owner, you must be able to <code class="literal">SET ROLE</code> to the
753 new owning role, and that role must have <code class="literal">CREATE</code>
754 privilege on the table's schema.
755 (These restrictions enforce that altering the owner
756 doesn't do anything you couldn't do by dropping and recreating the table.
757 However, a superuser can alter ownership of any table anyway.)
758 To add a column or alter a column type or use the <code class="literal">OF</code>
759 clause, you must also have <code class="literal">USAGE</code> privilege on the data
761 </p></div><div class="refsect1" id="id-1.9.3.35.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt id="SQL-ALTERTABLE-PARMS-IF-EXISTS"><span class="term"><code class="literal">IF EXISTS</code></span> <a href="#SQL-ALTERTABLE-PARMS-IF-EXISTS" class="id_link">#</a></dt><dd><p>
762 Do not throw an error if the table does not exist. A notice is issued
764 </p></dd><dt id="SQL-ALTERTABLE-PARMS-NAME"><span class="term"><em class="replaceable"><code>name</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-NAME" class="id_link">#</a></dt><dd><p>
765 The name (optionally schema-qualified) of an existing table to
766 alter. If <code class="literal">ONLY</code> is specified before the table name, only
767 that table is altered. If <code class="literal">ONLY</code> is not specified, the table
768 and all its descendant tables (if any) are altered. Optionally,
769 <code class="literal">*</code> can be specified after the table name to explicitly
770 indicate that descendant tables are included.
771 </p></dd><dt id="SQL-ALTERTABLE-PARMS-COLUMN-NAME"><span class="term"><em class="replaceable"><code>column_name</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-COLUMN-NAME" class="id_link">#</a></dt><dd><p>
772 Name of a new or existing column.
773 </p></dd><dt id="SQL-ALTERTABLE-PARMS-NEW-COLUMN-NAME"><span class="term"><em class="replaceable"><code>new_column_name</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-NEW-COLUMN-NAME" class="id_link">#</a></dt><dd><p>
774 New name for an existing column.
775 </p></dd><dt id="SQL-ALTERTABLE-PARMS-NEW-NAME"><span class="term"><em class="replaceable"><code>new_name</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-NEW-NAME" class="id_link">#</a></dt><dd><p>
776 New name for the table.
777 </p></dd><dt id="SQL-ALTERTABLE-PARMS-DATA-TYPE"><span class="term"><em class="replaceable"><code>data_type</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-DATA-TYPE" class="id_link">#</a></dt><dd><p>
778 Data type of the new column, or new data type for an existing
780 </p></dd><dt id="SQL-ALTERTABLE-PARMS-TABLE-CONSTRAINT"><span class="term"><em class="replaceable"><code>table_constraint</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-TABLE-CONSTRAINT" class="id_link">#</a></dt><dd><p>
781 New table constraint for the table.
782 </p></dd><dt id="SQL-ALTERTABLE-PARMS-CONSTRAINT-NAME"><span class="term"><em class="replaceable"><code>constraint_name</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-CONSTRAINT-NAME" class="id_link">#</a></dt><dd><p>
783 Name of a new or existing constraint.
784 </p></dd><dt id="SQL-ALTERTABLE-PARMS-CASCADE"><span class="term"><code class="literal">CASCADE</code></span> <a href="#SQL-ALTERTABLE-PARMS-CASCADE" class="id_link">#</a></dt><dd><p>
785 Automatically drop objects that depend on the dropped column
786 or constraint (for example, views referencing the column),
787 and in turn all objects that depend on those objects
788 (see <a class="xref" href="ddl-depend.html" title="5.15. Dependency Tracking">Section 5.15</a>).
789 </p></dd><dt id="SQL-ALTERTABLE-PARMS-RESTRICT"><span class="term"><code class="literal">RESTRICT</code></span> <a href="#SQL-ALTERTABLE-PARMS-RESTRICT" class="id_link">#</a></dt><dd><p>
790 Refuse to drop the column or constraint if there are any dependent
791 objects. This is the default behavior.
792 </p></dd><dt id="SQL-ALTERTABLE-PARMS-TRIGGER-NAME"><span class="term"><em class="replaceable"><code>trigger_name</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-TRIGGER-NAME" class="id_link">#</a></dt><dd><p>
793 Name of a single trigger to disable or enable.
794 </p></dd><dt id="SQL-ALTERTABLE-PARMS-ALL"><span class="term"><code class="literal">ALL</code></span> <a href="#SQL-ALTERTABLE-PARMS-ALL" class="id_link">#</a></dt><dd><p>
795 Disable or enable all triggers belonging to the table.
796 (This requires superuser privilege if any of the triggers are
797 internally generated constraint triggers, such as those that are used
798 to implement foreign key constraints or deferrable uniqueness and
799 exclusion constraints.)
800 </p></dd><dt id="SQL-ALTERTABLE-PARMS-USER"><span class="term"><code class="literal">USER</code></span> <a href="#SQL-ALTERTABLE-PARMS-USER" class="id_link">#</a></dt><dd><p>
801 Disable or enable all triggers belonging to the table except for
802 internally generated constraint triggers, such as those that are used
803 to implement foreign key constraints or deferrable uniqueness and
804 exclusion constraints.
805 </p></dd><dt id="SQL-ALTERTABLE-PARMS-INDEX-NAME"><span class="term"><em class="replaceable"><code>index_name</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-INDEX-NAME" class="id_link">#</a></dt><dd><p>
806 The name of an existing index.
807 </p></dd><dt id="SQL-ALTERTABLE-PARMS-STORAGE-PARAMETER"><span class="term"><em class="replaceable"><code>storage_parameter</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-STORAGE-PARAMETER" class="id_link">#</a></dt><dd><p>
808 The name of a table storage parameter.
809 </p></dd><dt id="SQL-ALTERTABLE-PARMS-VALUE"><span class="term"><em class="replaceable"><code>value</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-VALUE" class="id_link">#</a></dt><dd><p>
810 The new value for a table storage parameter.
811 This might be a number or a word depending on the parameter.
812 </p></dd><dt id="SQL-ALTERTABLE-PARMS-PARENT-TABLE"><span class="term"><em class="replaceable"><code>parent_table</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-PARENT-TABLE" class="id_link">#</a></dt><dd><p>
813 A parent table to associate or de-associate with this table.
814 </p></dd><dt id="SQL-ALTERTABLE-PARMS-NEW-OWNER"><span class="term"><em class="replaceable"><code>new_owner</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-NEW-OWNER" class="id_link">#</a></dt><dd><p>
815 The user name of the new owner of the table.
816 </p></dd><dt id="SQL-ALTERTABLE-PARMS-NEW-ACCESS-METHOD"><span class="term"><em class="replaceable"><code>new_access_method</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-NEW-ACCESS-METHOD" class="id_link">#</a></dt><dd><p>
817 The name of the access method to which the table will be converted.
818 </p></dd><dt id="SQL-ALTERTABLE-PARMS-NEW-TABLESPACE"><span class="term"><em class="replaceable"><code>new_tablespace</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-NEW-TABLESPACE" class="id_link">#</a></dt><dd><p>
819 The name of the tablespace to which the table will be moved.
820 </p></dd><dt id="SQL-ALTERTABLE-PARMS-NEW-SCHEMA"><span class="term"><em class="replaceable"><code>new_schema</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-NEW-SCHEMA" class="id_link">#</a></dt><dd><p>
821 The name of the schema to which the table will be moved.
822 </p></dd><dt id="SQL-ALTERTABLE-PARMS-PARTITION-NAME"><span class="term"><em class="replaceable"><code>partition_name</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-PARTITION-NAME" class="id_link">#</a></dt><dd><p>
823 The name of the table to attach as a new partition or to detach from this table.
824 </p></dd><dt id="SQL-ALTERTABLE-PARMS-PARTITION-BOUND-SPEC"><span class="term"><em class="replaceable"><code>partition_bound_spec</code></em></span> <a href="#SQL-ALTERTABLE-PARMS-PARTITION-BOUND-SPEC" class="id_link">#</a></dt><dd><p>
825 The partition bound specification for a new partition. Refer to
826 <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for more details on the syntax of the same.
827 </p></dd></dl></div></div><div class="refsect1" id="SQL-ALTERTABLE-NOTES"><h2>Notes</h2><p>
828 The key word <code class="literal">COLUMN</code> is noise and can be omitted.
830 When a column is added with <code class="literal">ADD COLUMN</code> and a
831 non-volatile <code class="literal">DEFAULT</code> is specified, the default value is
832 evaluated at the time of the statement and the result stored in the
833 table's metadata, where it will be returned when any existing rows are
834 accessed. The value will be only applied when the table is rewritten,
835 making the <code class="command">ALTER TABLE</code> very fast even on large tables.
836 If no column constraints are specified, NULL is used as the
837 <code class="literal">DEFAULT</code>. In neither case is a rewrite of the table
840 Adding a column with a volatile <code class="literal">DEFAULT</code>
841 (e.g., <code class="function">clock_timestamp()</code>), a stored generated column,
842 an identity column, or a column with a domain data type that has
843 constraints will cause the entire table and its indexes to be rewritten.
844 Adding a virtual generated column never requires a rewrite.
846 Changing the type of an existing column will normally cause the entire table
847 and its indexes to be rewritten.
848 As an exception, when changing the type of an existing column,
849 if the <code class="literal">USING</code> clause does not change the column
850 contents and the old type is either binary coercible to the new type
851 or an unconstrained domain over the new type, a table rewrite is not
852 needed. However, indexes will still be rebuilt unless the system
853 can verify that the new index would be logically equivalent to the
854 existing one. For example, if the collation for a column has been
855 changed, an index rebuild is required because the new sort
856 order might be different. However, in the absence of a collation
857 change, a column can be changed from <code class="type">text</code> to
858 <code class="type">varchar</code> (or vice versa) without rebuilding the indexes
859 because these data types sort identically.
862 rebuilds may take a significant amount of time for a large table,
863 and will temporarily require as much as double the disk space.
865 Adding a <code class="literal">CHECK</code> or <code class="literal">NOT NULL</code>
866 constraint requires scanning the table to verify that existing rows meet the
867 constraint, but does not require a table rewrite. If a <code class="literal">CHECK</code>
868 constraint is added as <code class="literal">NOT ENFORCED</code>, no verification will
871 Similarly, when attaching a new partition it may be scanned to verify that
872 existing rows meet the partition constraint.
874 The main reason for providing the option to specify multiple changes
875 in a single <code class="command">ALTER TABLE</code> is that multiple table scans or
876 rewrites can thereby be combined into a single pass over the table.
878 Scanning a large table to verify new foreign-key, check, or not-null constraints
879 can take a long time, and other updates to the table are locked out
880 until the <code class="command">ALTER TABLE ADD CONSTRAINT</code> command is
881 committed. The main purpose of the <code class="literal">NOT VALID</code>
882 constraint option is to reduce the impact of adding a constraint on
883 concurrent updates. With <code class="literal">NOT VALID</code>,
884 the <code class="command">ADD CONSTRAINT</code> command does not scan the table
885 and can be committed immediately. After that, a <code class="literal">VALIDATE
886 CONSTRAINT</code> command can be issued to verify that existing rows
887 satisfy the constraint. The validation step does not need to lock out
888 concurrent updates, since it knows that other transactions will be
889 enforcing the constraint for rows that they insert or update; only
890 pre-existing rows need to be checked. Hence, validation acquires only
891 a <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock on the table being
892 altered. (If the constraint is a foreign key then a <code class="literal">ROW
893 SHARE</code> lock is also required on the table referenced by the
894 constraint.) In addition to improving concurrency, it can be useful to
895 use <code class="literal">NOT VALID</code> and <code class="literal">VALIDATE
896 CONSTRAINT</code> in cases where the table is known to contain
897 pre-existing violations. Once the constraint is in place, no new
898 violations can be inserted, and the existing problems can be corrected
899 at leisure until <code class="literal">VALIDATE CONSTRAINT</code> finally
902 The <code class="literal">DROP COLUMN</code> form does not physically remove
903 the column, but simply makes it invisible to SQL operations. Subsequent
904 insert and update operations in the table will store a null value for the
905 column. Thus, dropping a column is quick but it will not immediately
906 reduce the on-disk size of your table, as the space occupied
907 by the dropped column is not reclaimed. The space will be
908 reclaimed over time as existing rows are updated.
910 To force immediate reclamation of space occupied by a dropped column,
911 you can execute one of the forms of <code class="command">ALTER TABLE</code> that
912 performs a rewrite of the whole table. This results in reconstructing
913 each row with the dropped column replaced by a null value.
915 The rewriting forms of <code class="command">ALTER TABLE</code> are not MVCC-safe.
916 After a table rewrite, the table will appear empty to concurrent
917 transactions, if they are using a snapshot taken before the rewrite
918 occurred. See <a class="xref" href="mvcc-caveats.html" title="13.6. Caveats">Section 13.6</a> for more details.
920 The <code class="literal">USING</code> option of <code class="literal">SET DATA TYPE</code> can actually
921 specify any expression involving the old values of the row; that is, it
922 can refer to other columns as well as the one being converted. This allows
923 very general conversions to be done with the <code class="literal">SET DATA TYPE</code>
924 syntax. Because of this flexibility, the <code class="literal">USING</code>
925 expression is not applied to the column's default value (if any); the
926 result might not be a constant expression as required for a default.
927 This means that when there is no implicit or assignment cast from old to
928 new type, <code class="literal">SET DATA TYPE</code> might fail to convert the default even
929 though a <code class="literal">USING</code> clause is supplied. In such cases,
930 drop the default with <code class="literal">DROP DEFAULT</code>, perform the <code class="literal">ALTER
931 TYPE</code>, and then use <code class="literal">SET DEFAULT</code> to add a suitable new
932 default. Similar considerations apply to indexes and constraints involving
935 If a table has any descendant tables, it is not permitted to add,
936 rename, or change the type of a column in the parent table without doing
937 the same to the descendants. This ensures that the descendants always
938 have columns matching the parent. Similarly, a <code class="literal">CHECK</code>
939 constraint cannot be renamed in the parent without also renaming it in
940 all descendants, so that <code class="literal">CHECK</code> constraints also match
941 between the parent and its descendants. (That restriction does not apply
942 to index-based constraints, however.)
943 Also, because selecting from the parent also selects from its descendants,
944 a constraint on the parent cannot be marked valid unless it is also marked
945 valid for those descendants. In all of these cases, <code class="command">ALTER TABLE
946 ONLY</code> will be rejected.
948 A recursive <code class="literal">DROP COLUMN</code> operation will remove a
949 descendant table's column only if the descendant does not inherit
950 that column from any other parents and never had an independent
951 definition of the column. A nonrecursive <code class="literal">DROP
952 COLUMN</code> (i.e., <code class="command">ALTER TABLE ONLY ... DROP
953 COLUMN</code>) never removes any descendant columns, but
954 instead marks them as independently defined rather than inherited.
955 A nonrecursive <code class="literal">DROP COLUMN</code> command will fail for a
956 partitioned table, because all partitions of a table must have the same
957 columns as the partitioning root.
959 The actions for identity columns (<code class="literal">ADD
960 GENERATED</code>, <code class="literal">SET</code> etc., <code class="literal">DROP
961 IDENTITY</code>), as well as the actions
962 <code class="literal">CLUSTER</code>, <code class="literal">OWNER</code>,
963 and <code class="literal">TABLESPACE</code> never recurse to descendant tables;
964 that is, they always act as though <code class="literal">ONLY</code> were specified.
965 Actions affecting trigger states recurse to partitions of partitioned
966 tables (unless <code class="literal">ONLY</code> is specified), but never to
967 traditional-inheritance descendants.
968 Adding a constraint recurses only for <code class="literal">CHECK</code> constraints
969 that are not marked <code class="literal">NO INHERIT</code>.
971 Changing any part of a system catalog table is not permitted.
973 Refer to <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for a further description of valid
974 parameters. <a class="xref" href="ddl.html" title="Chapter 5. Data Definition">Chapter 5</a> has further information on
976 </p></div><div class="refsect1" id="id-1.9.3.35.8"><h2>Examples</h2><p>
977 To add a column of type <code class="type">varchar</code> to a table:
978 </p><pre class="programlisting">
979 ALTER TABLE distributors ADD COLUMN address varchar(30);
981 That will cause all existing rows in the table to be filled with null
982 values for the new column.
984 To add a column with a non-null default:
985 </p><pre class="programlisting">
986 ALTER TABLE measurements
987 ADD COLUMN mtime timestamp with time zone DEFAULT now();
989 Existing rows will be filled with the current time as the value of the
990 new column, and then new rows will receive the time of their insertion.
992 To add a column and fill it with a value different from the default to
994 </p><pre class="programlisting">
995 ALTER TABLE transactions
996 ADD COLUMN status varchar(30) DEFAULT 'old',
997 ALTER COLUMN status SET default 'current';
999 Existing rows will be filled with <code class="literal">old</code>, but then
1000 the default for subsequent commands will be <code class="literal">current</code>.
1001 The effects are the same as if the two sub-commands had been issued
1002 in separate <code class="command">ALTER TABLE</code> commands.
1004 To drop a column from a table:
1005 </p><pre class="programlisting">
1006 ALTER TABLE distributors DROP COLUMN address RESTRICT;
1009 To change the types of two existing columns in one operation:
1010 </p><pre class="programlisting">
1011 ALTER TABLE distributors
1012 ALTER COLUMN address TYPE varchar(80),
1013 ALTER COLUMN name TYPE varchar(100);
1016 To change an integer column containing Unix timestamps to <code class="type">timestamp
1017 with time zone</code> via a <code class="literal">USING</code> clause:
1018 </p><pre class="programlisting">
1020 ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone
1022 timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';
1025 The same, when the column has a default expression that won't automatically
1026 cast to the new data type:
1027 </p><pre class="programlisting">
1029 ALTER COLUMN foo_timestamp DROP DEFAULT,
1030 ALTER COLUMN foo_timestamp TYPE timestamp with time zone
1032 timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',
1033 ALTER COLUMN foo_timestamp SET DEFAULT now();
1036 To rename an existing column:
1037 </p><pre class="programlisting">
1038 ALTER TABLE distributors RENAME COLUMN address TO city;
1041 To rename an existing table:
1042 </p><pre class="programlisting">
1043 ALTER TABLE distributors RENAME TO suppliers;
1046 To rename an existing constraint:
1047 </p><pre class="programlisting">
1048 ALTER TABLE distributors RENAME CONSTRAINT zipchk TO zip_check;
1051 To add a not-null constraint to a column:
1052 </p><pre class="programlisting">
1053 ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
1055 To remove a not-null constraint from a column:
1056 </p><pre class="programlisting">
1057 ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
1060 To add a check constraint to a table and all its children:
1061 </p><pre class="programlisting">
1062 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
1065 To add a check constraint only to a table and not to its children:
1066 </p><pre class="programlisting">
1067 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;
1069 (The check constraint will not be inherited by future children, either.)
1071 To remove a check constraint from a table and all its children:
1072 </p><pre class="programlisting">
1073 ALTER TABLE distributors DROP CONSTRAINT zipchk;
1076 To remove a check constraint from one table only:
1077 </p><pre class="programlisting">
1078 ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;
1080 (The check constraint remains in place for any child tables.)
1082 To add a foreign key constraint to a table:
1083 </p><pre class="programlisting">
1084 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address);
1087 To add a foreign key constraint to a table with the least impact on other work:
1088 </p><pre class="programlisting">
1089 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) NOT VALID;
1090 ALTER TABLE distributors VALIDATE CONSTRAINT distfk;
1093 To add a (multicolumn) unique constraint to a table:
1094 </p><pre class="programlisting">
1095 ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
1098 To add an automatically named primary key constraint to a table, noting
1099 that a table can only ever have one primary key:
1100 </p><pre class="programlisting">
1101 ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
1104 To move a table to a different tablespace:
1105 </p><pre class="programlisting">
1106 ALTER TABLE distributors SET TABLESPACE fasttablespace;
1109 To move a table to a different schema:
1110 </p><pre class="programlisting">
1111 ALTER TABLE myschema.distributors SET SCHEMA yourschema;
1114 To recreate a primary key constraint, without blocking updates while the
1116 </p><pre class="programlisting">
1117 CREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);
1118 ALTER TABLE distributors DROP CONSTRAINT distributors_pkey,
1119 ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;
1121 To attach a partition to a range-partitioned table:
1122 </p><pre class="programlisting">
1123 ALTER TABLE measurement
1124 ATTACH PARTITION measurement_y2016m07 FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');
1126 To attach a partition to a list-partitioned table:
1127 </p><pre class="programlisting">
1129 ATTACH PARTITION cities_ab FOR VALUES IN ('a', 'b');
1131 To attach a partition to a hash-partitioned table:
1132 </p><pre class="programlisting">
1134 ATTACH PARTITION orders_p4 FOR VALUES WITH (MODULUS 4, REMAINDER 3);
1136 To attach a default partition to a partitioned table:
1137 </p><pre class="programlisting">
1139 ATTACH PARTITION cities_partdef DEFAULT;
1141 To detach a partition from a partitioned table:
1142 </p><pre class="programlisting">
1143 ALTER TABLE measurement
1144 DETACH PARTITION measurement_y2015m12;
1145 </pre></div><div class="refsect1" id="id-1.9.3.35.9"><h2>Compatibility</h2><p>
1146 The forms <code class="literal">ADD [COLUMN]</code>,
1147 <code class="literal">DROP [COLUMN]</code>, <code class="literal">DROP IDENTITY</code>, <code class="literal">RESTART</code>,
1148 <code class="literal">SET DEFAULT</code>, <code class="literal">SET DATA TYPE</code> (without <code class="literal">USING</code>),
1149 <code class="literal">SET GENERATED</code>, and <code class="literal">SET <em class="replaceable"><code>sequence_option</code></em></code>
1150 conform with the SQL standard.
1151 The form <code class="literal">ADD <em class="replaceable"><code>table_constraint</code></em></code>
1152 conforms with the SQL standard when the <code class="literal">USING INDEX</code> and
1153 <code class="literal">NOT VALID</code> clauses are omitted and the constraint type is
1154 one of <code class="literal">CHECK</code>, <code class="literal">UNIQUE</code>, <code class="literal">PRIMARY KEY</code>,
1155 or <code class="literal">REFERENCES</code>.
1157 <span class="productname">PostgreSQL</span> extensions of the SQL standard.
1158 Also, the ability to specify more than one manipulation in a single
1159 <code class="command">ALTER TABLE</code> command is an extension.
1161 <code class="command">ALTER TABLE DROP COLUMN</code> can be used to drop the only
1162 column of a table, leaving a zero-column table. This is an
1163 extension of SQL, which disallows zero-column tables.
1164 </p></div><div class="refsect1" id="id-1.9.3.35.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-altersystem.html" title="ALTER SYSTEM">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-altertablespace.html" title="ALTER TABLESPACE">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ALTER SYSTEM </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> ALTER TABLESPACE</td></tr></table></div></body></html>