codekingpro/portable-devtools
115k
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 16.3 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">3ALTER TABLE [ IF EXISTS ] [ ONLY ] <em class="replaceable"><code>name</code></em> [ * ]4 <em class="replaceable"><code>action</code></em> [, ... ]5ALTER 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>7ALTER 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>9ALTER TABLE [ IF EXISTS ] <em class="replaceable"><code>name</code></em>10 RENAME TO <em class="replaceable"><code>new_name</code></em>11ALTER TABLE [ IF EXISTS ] <em class="replaceable"><code>name</code></em>12 SET SCHEMA <em class="replaceable"><code>new_schema</code></em>13ALTER 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 ]15ALTER 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 }17ALTER TABLE [ IF EXISTS ] <em class="replaceable"><code>name</code></em>18 DETACH PARTITION <em class="replaceable"><code>partition_name</code></em> [ CONCURRENTLY | FINALIZE ]19 20<span class="phrase">where <em class="replaceable"><code>action</code></em> is one of:</span>21 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 DEFAULT27 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> { SET | DROP } NOT NULL28 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> DROP EXPRESSION [ IF EXISTS ]29 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> ) ]30 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> ] } [...]31 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> DROP IDENTITY [ IF EXISTS ]32 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET STATISTICS <em class="replaceable"><code>integer</code></em>33 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> [, ... ] )34 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> RESET ( <em class="replaceable"><code>attribute_option</code></em> [, ... ] )35 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }36 ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET COMPRESSION <em class="replaceable"><code>compression_method</code></em>37 ADD <em class="replaceable"><code>table_constraint</code></em> [ NOT VALID ]38 ADD <em class="replaceable"><code>table_constraint_using_index</code></em>39 ALTER CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]40 VALIDATE CONSTRAINT <em class="replaceable"><code>constraint_name</code></em>41 DROP CONSTRAINT [ IF EXISTS ] <em class="replaceable"><code>constraint_name</code></em> [ RESTRICT | CASCADE ]42 DISABLE TRIGGER [ <em class="replaceable"><code>trigger_name</code></em> | ALL | USER ]43 ENABLE TRIGGER [ <em class="replaceable"><code>trigger_name</code></em> | ALL | USER ]44 ENABLE REPLICA TRIGGER <em class="replaceable"><code>trigger_name</code></em>45 ENABLE ALWAYS TRIGGER <em class="replaceable"><code>trigger_name</code></em>46 DISABLE RULE <em class="replaceable"><code>rewrite_rule_name</code></em>47 ENABLE RULE <em class="replaceable"><code>rewrite_rule_name</code></em>48 ENABLE REPLICA RULE <em class="replaceable"><code>rewrite_rule_name</code></em>49 ENABLE ALWAYS RULE <em class="replaceable"><code>rewrite_rule_name</code></em>50 DISABLE ROW LEVEL SECURITY51 ENABLE ROW LEVEL SECURITY52 FORCE ROW LEVEL SECURITY53 NO FORCE ROW LEVEL SECURITY54 CLUSTER ON <em class="replaceable"><code>index_name</code></em>55 SET WITHOUT CLUSTER56 SET WITHOUT OIDS57 SET ACCESS METHOD <em class="replaceable"><code>new_access_method</code></em>58 SET TABLESPACE <em class="replaceable"><code>new_tablespace</code></em>59 SET { LOGGED | UNLOGGED }60 SET ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] )61 RESET ( <em class="replaceable"><code>storage_parameter</code></em> [, ... ] )62 INHERIT <em class="replaceable"><code>parent_table</code></em>63 NO INHERIT <em class="replaceable"><code>parent_table</code></em>64 OF <em class="replaceable"><code>type_name</code></em>65 NOT OF66 OWNER TO { <em class="replaceable"><code>new_owner</code></em> | CURRENT_ROLE | CURRENT_USER | SESSION_USER }67 REPLICA IDENTITY { DEFAULT | USING INDEX <em class="replaceable"><code>index_name</code></em> | FULL | NOTHING }68 69<span class="phrase">and <em class="replaceable"><code>partition_bound_spec</code></em> is:</span>70 71IN ( <em class="replaceable"><code>partition_bound_expr</code></em> [, ...] ) |72FROM ( { <em class="replaceable"><code>partition_bound_expr</code></em> | MINVALUE | MAXVALUE } [, ...] )73 TO ( { <em class="replaceable"><code>partition_bound_expr</code></em> | MINVALUE | MAXVALUE } [, ...] ) |74WITH ( MODULUS <em class="replaceable"><code>numeric_literal</code></em>, REMAINDER <em class="replaceable"><code>numeric_literal</code></em> )75 76<span class="phrase">and <em class="replaceable"><code>column_constraint</code></em> is:</span>77 78[ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]79{ NOT NULL |80 NULL |81 CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ] |82 DEFAULT <em class="replaceable"><code>default_expr</code></em> |83 GENERATED ALWAYS AS ( <em class="replaceable"><code>generation_expr</code></em> ) STORED |84 GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <em class="replaceable"><code>sequence_options</code></em> ) ] |85 UNIQUE [ NULLS [ NOT ] DISTINCT ] <em class="replaceable"><code>index_parameters</code></em> |86 PRIMARY KEY <em class="replaceable"><code>index_parameters</code></em> |87 REFERENCES <em class="replaceable"><code>reftable</code></em> [ ( <em class="replaceable"><code>refcolumn</code></em> ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]88 [ ON DELETE <em class="replaceable"><code>referential_action</code></em> ] [ ON UPDATE <em class="replaceable"><code>referential_action</code></em> ] }89[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]90 91<span class="phrase">and <em class="replaceable"><code>table_constraint</code></em> is:</span>92 93[ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]94{ CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ] |95 UNIQUE [ NULLS [ NOT ] DISTINCT ] ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) <em class="replaceable"><code>index_parameters</code></em> |96 PRIMARY KEY ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) <em class="replaceable"><code>index_parameters</code></em> |97 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> ) ] |98 FOREIGN KEY ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) REFERENCES <em class="replaceable"><code>reftable</code></em> [ ( <em class="replaceable"><code>refcolumn</code></em> [, ... ] ) ]99 [ 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> ] }100[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]101 102<span class="phrase">and <em class="replaceable"><code>table_constraint_using_index</code></em> is:</span>103 104 [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]105 { UNIQUE | PRIMARY KEY } USING INDEX <em class="replaceable"><code>index_name</code></em>106 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]107 108<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>109 110[ INCLUDE ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) ]111[ WITH ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] ) ]112[ USING INDEX TABLESPACE <em class="replaceable"><code>tablespace_name</code></em> ]113 114<span class="phrase"><em class="replaceable"><code>exclude_element</code></em> in an <code class="literal">EXCLUDE</code> constraint is:</span>115 116{ <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 } ]117 118<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>119 120{ NO ACTION | RESTRICT | CASCADE | SET NULL [ ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) ] | SET DEFAULT [ ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) ] }121</pre></div><div class="refsect1" id="id-1.9.3.35.5"><h2>Description</h2><p>122 <code class="command">ALTER TABLE</code> changes the definition of an existing table.123 There are several subforms described below. Note that the lock level required124 may differ for each subform. An <code class="literal">ACCESS EXCLUSIVE</code> lock is125 acquired unless explicitly noted. When multiple subcommands are given, the126 lock acquired will be the strictest one required by any subcommand.127 128 </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>129 This form adds a new column to the table, using the same syntax as130 <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>131 is specified and a column already exists with this name,132 no error is thrown.133 </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>134 This form drops a column from a table. Indexes and135 table constraints involving the column will be automatically136 dropped as well.137 Multivariate statistics referencing the dropped column will also be138 removed if the removal of the column would cause the statistics to139 contain data for only a single column.140 You will need to say <code class="literal">CASCADE</code> if anything outside the table141 depends on the column, for example, foreign key references or views.142 If <code class="literal">IF EXISTS</code> is specified and the column143 does not exist, no error is thrown. In this case a notice144 is issued instead.145 </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>146 This form changes the type of a column of a table. Indexes and147 simple table constraints involving the column will be automatically148 converted to use the new column type by reparsing the originally149 supplied expression.150 The optional <code class="literal">COLLATE</code> clause specifies a collation151 for the new column; if omitted, the collation is the default for the152 new column type.153 The optional <code class="literal">USING</code>154 clause specifies how to compute the new column value from the old;155 if omitted, the default conversion is the same as an assignment156 cast from old data type to new. A <code class="literal">USING</code>157 clause must be provided if there is no implicit or assignment158 cast from old to new type.159 </p><p>160 When this form is used, the column's statistics are removed,161 so running <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a>162 on the table afterwards is recommended.163 </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>164 These forms set or remove the default value for a column (where165 removal is equivalent to setting the default value to NULL). The new166 default value will only apply in subsequent <code class="command">INSERT</code>167 or <code class="command">UPDATE</code> commands; it does not cause rows already168 in the table to change.169 </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>170 These forms change whether a column is marked to allow null171 values or to reject null values.172 </p><p>173 <code class="literal">SET NOT NULL</code> may only be applied to a column174 provided none of the records in the table contain a175 <code class="literal">NULL</code> value for the column. Ordinarily this is176 checked during the <code class="literal">ALTER TABLE</code> by scanning the177 entire table; however, if a valid <code class="literal">CHECK</code> constraint is178 found which proves no <code class="literal">NULL</code> can exist, then the179 table scan is skipped.180 </p><p>181 If this table is a partition, one cannot perform <code class="literal">DROP NOT NULL</code>182 on a column if it is marked <code class="literal">NOT NULL</code> in the parent183 table. To drop the <code class="literal">NOT NULL</code> constraint from all the184 partitions, perform <code class="literal">DROP NOT NULL</code> on the parent185 table. Even if there is no <code class="literal">NOT NULL</code> constraint on the186 parent, such a constraint can still be added to individual partitions,187 if desired; that is, the children can disallow nulls even if the parent188 allows them, but not the other way around.189 </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>190 This form turns a stored generated column into a normal base column.191 Existing data in the columns is retained, but future changes will no192 longer apply the generation expression.193 </p><p>194 If <code class="literal">DROP EXPRESSION IF EXISTS</code> is specified and the195 column is not a stored generated column, no error is thrown. In this196 case a notice is issued instead.197 </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>198 These forms change whether a column is an identity column or change the199 generation attribute of an existing identity column.200 See <a class="link" href="sql-createtable.html" title="CREATE TABLE"><code class="command">CREATE TABLE</code></a> for details.201 Like <code class="literal">SET DEFAULT</code>, these forms only affect the202 behavior of subsequent <code class="command">INSERT</code>203 and <code class="command">UPDATE</code> commands; they do not cause rows204 already in the table to change.205 </p><p>206 If <code class="literal">DROP IDENTITY IF EXISTS</code> is specified and the207 column is not an identity column, no error is thrown. In this case a208 notice is issued instead.209 </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>210 These forms alter the sequence that underlies an existing identity211 column. <em class="replaceable"><code>sequence_option</code></em> is an option212 supported by <a class="link" href="sql-altersequence.html" title="ALTER SEQUENCE"><code class="command">ALTER SEQUENCE</code></a> such213 as <code class="literal">INCREMENT BY</code>.214 </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>215 This form216 sets the per-column statistics-gathering target for subsequent217 <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a> operations.218 The target can be set in the range 0 to 10000; alternatively, set it219 to -1 to revert to using the system default statistics220 target (<a class="xref" href="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET">default_statistics_target</a>).221 For more information on the use of statistics by the222 <span class="productname">PostgreSQL</span> query planner, refer to223 <a class="xref" href="planner-stats.html" title="14.2. Statistics Used by the Planner">Section 14.2</a>.224 </p><p>225 <code class="literal">SET STATISTICS</code> acquires a226 <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.227 </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>228 This form sets or resets per-attribute options. Currently, the only229 defined per-attribute options are <code class="literal">n_distinct</code> and230 <code class="literal">n_distinct_inherited</code>, which override the231 number-of-distinct-values estimates made by subsequent232 <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a>233 operations. <code class="literal">n_distinct</code> affects the statistics for the table234 itself, while <code class="literal">n_distinct_inherited</code> affects the statistics235 gathered for the table plus its inheritance children. When set to a236 positive value, <code class="command">ANALYZE</code> will assume that the column contains237 exactly the specified number of distinct nonnull values. When set to a238 negative value, which must be greater239 than or equal to -1, <code class="command">ANALYZE</code> will assume that the number of240 distinct nonnull values in the column is linear in the size of the241 table; the exact count is to be computed by multiplying the estimated242 table size by the absolute value of the given number. For example,243 a value of -1 implies that all values in the column are distinct, while244 a value of -0.5 implies that each value appears twice on the average.245 This can be useful when the size of the table changes over time, since246 the multiplication by the number of rows in the table is not performed247 until query planning time. Specify a value of 0 to revert to estimating248 the number of distinct values normally. For more information on the use249 of statistics by the <span class="productname">PostgreSQL</span> query250 planner, refer to <a class="xref" href="planner-stats.html" title="14.2. Statistics Used by the Planner">Section 14.2</a>.251 </p><p>252 Changing per-attribute options acquires a253 <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.254 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-STORAGE"><span class="term">255 <code class="literal">SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }</code>256 <a id="id-1.9.3.35.5.2.3.11.1.2" class="indexterm"></a>257 </span> <a href="#SQL-ALTERTABLE-DESC-SET-STORAGE" class="id_link">#</a></dt><dd><p>258 This form sets the storage mode for a column. This controls whether this259 column is held inline or in a secondary <acronym class="acronym">TOAST</acronym> table,260 and whether the data261 should be compressed or not. <code class="literal">PLAIN</code> must be used262 for fixed-length values such as <code class="type">integer</code> and is263 inline, uncompressed. <code class="literal">MAIN</code> is for inline,264 compressible data. <code class="literal">EXTERNAL</code> is for external,265 uncompressed data, and <code class="literal">EXTENDED</code> is for external,266 compressed data.267 Writing <code class="literal">DEFAULT</code> sets the storage mode to the default268 mode for the column's data type. <code class="literal">EXTENDED</code> is the269 default for most data types that support non-<code class="literal">PLAIN</code>270 storage.271 Use of <code class="literal">EXTERNAL</code> will make substring operations on272 very large <code class="type">text</code> and <code class="type">bytea</code> values run faster,273 at the penalty of increased storage space.274 Note that <code class="literal">ALTER TABLE ... SET STORAGE</code> doesn't itself275 change anything in the table; it just sets the strategy to be pursued276 during future table updates.277 See <a class="xref" href="storage-toast.html" title="73.2. TOAST">Section 73.2</a> for more information.278 </p></dd><dt id="SQL-ALTERTABLE-DESC-SET-COMPRESSION"><span class="term">279 <code class="literal">SET COMPRESSION <em class="replaceable"><code>compression_method</code></em></code>280 </span> <a href="#SQL-ALTERTABLE-DESC-SET-COMPRESSION" class="id_link">#</a></dt><dd><p>281 This form sets the compression method for a column, determining how282 values inserted in future will be compressed (if the storage mode283 permits compression at all).284 This does not cause the table to be rewritten, so existing data may still285 be compressed with other compression methods. If the table is restored286 with <span class="application">pg_restore</span>, then all values are rewritten287 with the configured compression method.288 However, when data is inserted from another relation (for example,289 by <code class="command">INSERT ... SELECT</code>), values from the source table are290 not necessarily detoasted, so any previously compressed data may retain291 its existing compression method, rather than being recompressed with the292 compression method of the target column.293 The supported compression294 methods are <code class="literal">pglz</code> and <code class="literal">lz4</code>.295 (<code class="literal">lz4</code> is available only if <code class="option">--with-lz4</code>296 was used when building <span class="productname">PostgreSQL</span>.) In297 addition, <em class="replaceable"><code>compression_method</code></em>298 can be <code class="literal">default</code>, which selects the default behavior of299 consulting the <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TOAST-COMPRESSION">default_toast_compression</a> setting300 at the time of data insertion to determine the method to use.301 </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>302 This form adds a new constraint to a table using the same constraint303 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">NOT304 VALID</code>, which is currently only allowed for foreign key305 and CHECK constraints.306 </p><p>307 Normally, this form will cause a scan of the table to verify that all308 existing rows in the table satisfy the new constraint. But if309 the <code class="literal">NOT VALID</code> option is used, this310 potentially-lengthy scan is skipped. The constraint will still be311 enforced against subsequent inserts or updates (that is, they'll fail312 unless there is a matching row in the referenced table, in the case313 of foreign keys, or they'll fail unless the new row matches the314 specified check condition). But the315 database will not assume that the constraint holds for all rows in316 the table, until it is validated by using the <code class="literal">VALIDATE317 CONSTRAINT</code> option.318 See <a class="xref" href="sql-altertable.html#SQL-ALTERTABLE-NOTES" title="Notes">Notes</a> below for more information319 about using the <code class="literal">NOT VALID</code> option.320 </p><p>321 Although most forms of <code class="literal">ADD322 <em class="replaceable"><code>table_constraint</code></em></code>323 require an <code class="literal">ACCESS EXCLUSIVE</code> lock, <code class="literal">ADD324 FOREIGN KEY</code> requires only a <code class="literal">SHARE ROW325 EXCLUSIVE</code> lock. Note that <code class="literal">ADD FOREIGN KEY</code>326 also acquires a <code class="literal">SHARE ROW EXCLUSIVE</code> lock on the327 referenced table, in addition to the lock on the table on which the328 constraint is declared.329 </p><p>330 Additional restrictions apply when unique or primary key constraints331 are added to partitioned tables; see <a class="link" href="sql-createtable.html" title="CREATE TABLE"><code class="command">CREATE TABLE</code></a>.332 Also, foreign key constraints on partitioned333 tables may not be declared <code class="literal">NOT VALID</code> at present.334 </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>335 This form adds a new <code class="literal">PRIMARY KEY</code> or <code class="literal">UNIQUE</code>336 constraint to a table based on an existing unique index. All the337 columns of the index will be included in the constraint.338 </p><p>339 The index cannot have expression columns nor be a partial index.340 Also, it must be a b-tree index with default sort ordering. These341 restrictions ensure that the index is equivalent to one that would be342 built by a regular <code class="literal">ADD PRIMARY KEY</code> or <code class="literal">ADD UNIQUE</code>343 command.344 </p><p>345 If <code class="literal">PRIMARY KEY</code> is specified, and the index's columns are not346 already marked <code class="literal">NOT NULL</code>, then this command will attempt to347 do <code class="literal">ALTER COLUMN SET NOT NULL</code> against each such column.348 That requires a full table scan to verify the column(s) contain no349 nulls. In all other cases, this is a fast operation.350 </p><p>351 If a constraint name is provided then the index will be renamed to match352 the constraint name. Otherwise the constraint will be named the same as353 the index.354 </p><p>355 After this command is executed, the index is <span class="quote">“<span class="quote">owned</span>”</span> by the356 constraint, in the same way as if the index had been built by357 a regular <code class="literal">ADD PRIMARY KEY</code> or <code class="literal">ADD UNIQUE</code>358 command. In particular, dropping the constraint will make the index359 disappear too.360 </p><p>361 This form is not currently supported on partitioned tables.362 </p><div class="note"><h3 class="title">Note</h3><p>363 Adding a constraint using an existing index can be helpful in364 situations where a new constraint needs to be added without blocking365 table updates for a long time. To do that, create the index using366 <code class="command">CREATE INDEX CONCURRENTLY</code>, and then install it as an367 official constraint using this syntax. See the example below.368 </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>369 This form alters the attributes of a constraint that was previously370 created. Currently only foreign key constraints may be altered.371 </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>372 This form validates a foreign key or check constraint that was373 previously created as <code class="literal">NOT VALID</code>, by scanning the374 table to ensure there are no rows for which the constraint is not375 satisfied. Nothing happens if the constraint is already marked valid.376 (See <a class="xref" href="sql-altertable.html#SQL-ALTERTABLE-NOTES" title="Notes">Notes</a> below for an explanation377 of the usefulness of this command.)378 </p><p>379 This command acquires a <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.380 </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>381 This form drops the specified constraint on a table, along with382 any index underlying the constraint.383 If <code class="literal">IF EXISTS</code> is specified and the constraint384 does not exist, no error is thrown. In this case a notice is issued instead.385 </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>386 These forms configure the firing of trigger(s) belonging to the table.387 A disabled trigger is still known to the system, but is not executed388 when its triggering event occurs. (For a deferred trigger, the enable389 status is checked when the event occurs, not when the trigger function390 is actually executed.) One can disable or enable a single391 trigger specified by name, or all triggers on the table, or only392 user triggers (this option excludes internally generated constraint393 triggers, such as those that are used to implement foreign key394 constraints or deferrable uniqueness and exclusion constraints).395 Disabling or enabling internally generated constraint triggers396 requires superuser privileges; it should be done with caution since397 of course the integrity of the constraint cannot be guaranteed if the398 triggers are not executed.399 </p><p>400 The trigger firing mechanism is also affected by the configuration401 variable <a class="xref" href="runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE">session_replication_role</a>. Simply enabled402 triggers (the default) will fire when the replication role is <span class="quote">“<span class="quote">origin</span>”</span>403 (the default) or <span class="quote">“<span class="quote">local</span>”</span>. Triggers configured as <code class="literal">ENABLE404 REPLICA</code> will only fire if the session is in <span class="quote">“<span class="quote">replica</span>”</span>405 mode, and triggers configured as <code class="literal">ENABLE ALWAYS</code> will406 fire regardless of the current replication role.407 </p><p>408 The effect of this mechanism is that in the default configuration,409 triggers do not fire on replicas. This is useful because if a trigger410 is used on the origin to propagate data between tables, then the411 replication system will also replicate the propagated data; so the412 trigger should not fire a second time on the replica, because that would413 lead to duplication. However, if a trigger is used for another purpose414 such as creating external alerts, then it might be appropriate to set it415 to <code class="literal">ENABLE ALWAYS</code> so that it is also fired on416 replicas.417 </p><p>418 When this command is applied to a partitioned table, the states of419 corresponding clone triggers in the partitions are updated too,420 unless <code class="literal">ONLY</code> is specified.421 </p><p>422 This command acquires a <code class="literal">SHARE ROW EXCLUSIVE</code> lock.423 </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>424 These forms configure the firing of rewrite rules belonging to the table.425 A disabled rule is still known to the system, but is not applied426 during query rewriting. The semantics are as for disabled/enabled427 triggers. This configuration is ignored for <code class="literal">ON SELECT</code> rules, which428 are always applied in order to keep views working even if the current429 session is in a non-default replication role.430 </p><p>431 The rule firing mechanism is also affected by the configuration variable432 <a class="xref" href="runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE">session_replication_role</a>, analogous to triggers as433 described above.434 </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>435 These forms control the application of row security policies belonging436 to the table. If enabled and no policies exist for the table, then a437 default-deny policy is applied. Note that policies can exist for a table438 even if row-level security is disabled. In this case, the policies will439 <span class="emphasis"><em>not</em></span> be applied and the policies will be ignored.440 See also441 <a class="link" href="sql-createpolicy.html" title="CREATE POLICY"><code class="command">CREATE POLICY</code></a>.442 </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>443 These forms control the application of row security policies belonging444 to the table when the user is the table owner. If enabled, row-level445 security policies will be applied when the user is the table owner. If446 disabled (the default) then row-level security will not be applied when447 the user is the table owner.448 See also449 <a class="link" href="sql-createpolicy.html" title="CREATE POLICY"><code class="command">CREATE POLICY</code></a>.450 </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>451 This form selects the default index for future452 <a class="link" href="sql-cluster.html" title="CLUSTER"><code class="command">CLUSTER</code></a>453 operations. It does not actually re-cluster the table.454 </p><p>455 Changing cluster options acquires a <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.456 </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>457 This form removes the most recently used458 <a class="link" href="sql-cluster.html" title="CLUSTER"><code class="command">CLUSTER</code></a>459 index specification from the table. This affects460 future cluster operations that don't specify an index.461 </p><p>462 Changing cluster options acquires a <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.463 </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>464 Backward-compatible syntax for removing the <code class="literal">oid</code>465 system column. As <code class="literal">oid</code> system columns cannot be466 added anymore, this never has an effect.467 </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>468 This form changes the access method of the table by rewriting it. See469 <a class="xref" href="tableam.html" title="Chapter 63. Table Access Method Interface Definition">Chapter 63</a> for more information.470 </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>471 This form changes the table's tablespace to the specified tablespace and472 moves the data file(s) associated with the table to the new tablespace.473 Indexes on the table, if any, are not moved; but they can be moved474 separately with additional <code class="literal">SET TABLESPACE</code> commands.475 When applied to a partitioned table, nothing is moved, but any476 partitions created afterwards with477 <code class="command">CREATE TABLE PARTITION OF</code> will use that tablespace,478 unless overridden by a <code class="literal">TABLESPACE</code> clause.479 </p><p>480 All tables in the current database in a tablespace can be moved by using481 the <code class="literal">ALL IN TABLESPACE</code> form, which will lock all tables482 to be moved first and then move each one. This form also supports483 <code class="literal">OWNED BY</code>, which will only move tables owned by the484 roles specified. If the <code class="literal">NOWAIT</code> option is specified485 then the command will fail if it is unable to acquire all of the locks486 required immediately. Note that system catalogs are not moved by this487 command; use <code class="command">ALTER DATABASE</code> or explicit488 <code class="command">ALTER TABLE</code> invocations instead if desired. The489 <code class="literal">information_schema</code> relations are not considered part490 of the system catalogs and will be moved.491 See also492 <a class="link" href="sql-createtablespace.html" title="CREATE TABLESPACE"><code class="command">CREATE TABLESPACE</code></a>.493 </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>494 This form changes the table from unlogged to logged or vice-versa495 (see <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-UNLOGGED"><code class="literal">UNLOGGED</code></a>). It cannot be applied496 to a temporary table.497 </p><p>498 This also changes the persistence of any sequences linked to the table499 (for identity or serial columns). However, it is also possible to500 change the persistence of such sequences separately.501 </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>502 This form changes one or more storage parameters for the table. See503 <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS" title="Storage Parameters">Storage Parameters</a> in the504 <a class="link" href="sql-createtable.html" title="CREATE TABLE"><code class="command">CREATE TABLE</code></a> documentation505 for details on the available parameters. Note that the table contents506 will not be modified immediately by this command; depending on the507 parameter you might need to rewrite the table to get the desired effects.508 That can be done with <a class="link" href="sql-vacuum.html" title="VACUUM"><code class="command">VACUUM509 FULL</code></a>, <a class="link" href="sql-cluster.html" title="CLUSTER"><code class="command">CLUSTER</code></a> or one of the forms510 of <code class="command">ALTER TABLE</code> that forces a table rewrite.511 For planner related parameters, changes will take effect from the next512 time the table is locked so currently executing queries will not be513 affected.514 </p><p>515 <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock will be taken for516 fillfactor, toast and autovacuum storage parameters, as well as the517 planner parameter <code class="varname">parallel_workers</code>.518 </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>519 This form resets one or more storage parameters to their520 defaults. As with <code class="literal">SET</code>, a table rewrite might be521 needed to update the table entirely.522 </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>523 This form adds the target table as a new child of the specified parent524 table. Subsequently, queries against the parent will include records525 of the target table. To be added as a child, the target table must526 already contain all the same columns as the parent (it could have527 additional columns, too). The columns must have matching data types,528 and if they have <code class="literal">NOT NULL</code> constraints in the parent529 then they must also have <code class="literal">NOT NULL</code> constraints in the530 child.531 </p><p>532 There must also be matching child-table constraints for all533 <code class="literal">CHECK</code> constraints of the parent, except those534 marked non-inheritable (that is, created with <code class="literal">ALTER TABLE ... ADD CONSTRAINT ... NO INHERIT</code>)535 in the parent, which are ignored; all child-table constraints matched536 must not be marked non-inheritable.537 Currently538 <code class="literal">UNIQUE</code>, <code class="literal">PRIMARY KEY</code>, and539 <code class="literal">FOREIGN KEY</code> constraints are not considered, but540 this might change in the future.541 </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>542 This form removes the target table from the list of children of the543 specified parent table.544 Queries against the parent table will no longer include records drawn545 from the target table.546 </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>547 This form links the table to a composite type as though <code class="command">CREATE548 TABLE OF</code> had formed it. The table's list of column names and types549 must precisely match that of the composite type. The table must550 not inherit from any other table. These restrictions ensure551 that <code class="command">CREATE TABLE OF</code> would permit an equivalent table552 definition.553 </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>554 This form dissociates a typed table from its type.555 </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>556 This form changes the owner of the table, sequence, view, materialized view,557 or foreign table to the specified user.558 </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>559 This form changes the information which is written to the write-ahead log560 to identify rows which are updated or deleted.561 In most cases, the old value of each column is only logged if it differs562 from the new value; however, if the old value is stored externally, it is563 always logged regardless of whether it changed.564 This option has no effect except when logical replication is in use.565 </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>566 Records the old values of the columns of the primary key, if any.567 This is the default for non-system tables.568 </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>569 Records the old values of the columns covered by the named index,570 that must be unique, not partial, not deferrable, and include only571 columns marked <code class="literal">NOT NULL</code>. If this index is572 dropped, the behavior is the same as <code class="literal">NOTHING</code>.573 </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>574 Records the old values of all columns in the row.575 </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>576 Records no information about the old row. This is the default for577 system tables.578 </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>579 The <code class="literal">RENAME</code> forms change the name of a table580 (or an index, sequence, view, materialized view, or foreign table), the581 name of an individual column in a table, or the name of a constraint of582 the table. When renaming a constraint that has an underlying index,583 the index is renamed as well.584 There is no effect on the stored data.585 </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>586 This form moves the table into another schema. Associated indexes,587 constraints, and sequences owned by table columns are moved as well.588 </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>589 This form attaches an existing table (which might itself be partitioned)590 as a partition of the target table. The table can be attached591 as a partition for specific values using <code class="literal">FOR VALUES</code>592 or as a default partition by using <code class="literal">DEFAULT</code>.593 For each index in the target table, a corresponding594 one will be created in the attached table; or, if an equivalent595 index already exists, it will be attached to the target table's index,596 as if <code class="command">ALTER INDEX ATTACH PARTITION</code> had been executed.597 Note that if the existing table is a foreign table, it is currently not598 allowed to attach the table as a partition of the target table if there599 are <code class="literal">UNIQUE</code> indexes on the target table. (See also600 <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a>.) For each user-defined601 row-level trigger that exists in the target table, a corresponding one602 is created in the attached table.603 </p><p>604 A partition using <code class="literal">FOR VALUES</code> uses same syntax for605 <em class="replaceable"><code>partition_bound_spec</code></em> as606 <a class="link" href="sql-createtable.html" title="CREATE TABLE"><code class="command">CREATE TABLE</code></a>. The partition bound specification607 must correspond to the partitioning strategy and partition key of the608 target table. The table to be attached must have all the same columns609 as the target table and no more; moreover, the column types must also610 match. Also, it must have all the <code class="literal">NOT NULL</code> and611 <code class="literal">CHECK</code> constraints of the target table. Currently612 <code class="literal">FOREIGN KEY</code> constraints are not considered.613 <code class="literal">UNIQUE</code> and <code class="literal">PRIMARY KEY</code> constraints614 from the parent table will be created in the partition, if they don't615 already exist.616 If any of the <code class="literal">CHECK</code> constraints of the table being617 attached are marked <code class="literal">NO INHERIT</code>, the command will fail;618 such constraints must be recreated without the619 <code class="literal">NO INHERIT</code> clause.620 </p><p>621 If the new partition is a regular table, a full table scan is performed622 to check that existing rows in the table do not violate the partition623 constraint. It is possible to avoid this scan by adding a valid624 <code class="literal">CHECK</code> constraint to the table that allows only625 rows satisfying the desired partition constraint before running this626 command. The <code class="literal">CHECK</code> constraint will be used to627 determine that the table need not be scanned to validate the partition628 constraint. This does not work, however, if any of the partition keys629 is an expression and the partition does not accept630 <code class="literal">NULL</code> values. If attaching a list partition that will631 not accept <code class="literal">NULL</code> values, also add a632 <code class="literal">NOT NULL</code> constraint to the partition key column,633 unless it's an expression.634 </p><p>635 If the new partition is a foreign table, nothing is done to verify636 that all the rows in the foreign table obey the partition constraint.637 (See the discussion in <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a> about638 constraints on the foreign table.)639 </p><p>640 When a table has a default partition, defining a new partition changes641 the partition constraint for the default partition. The default642 partition can't contain any rows that would need to be moved to the new643 partition, and will be scanned to verify that none are present. This644 scan, like the scan of the new partition, can be avoided if an645 appropriate <code class="literal">CHECK</code> constraint is present. Also like646 the scan of the new partition, it is always skipped when the default647 partition is a foreign table.648 </p><p>649 Attaching a partition acquires a650 <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock on the parent table,651 in addition to the <code class="literal">ACCESS EXCLUSIVE</code> locks on the table652 being attached and on the default partition (if any).653 </p><p>654 Further locks must also be held on all sub-partitions if the table being655 attached is itself a partitioned table. Likewise if the default656 partition is itself a partitioned table. The locking of the657 sub-partitions can be avoided by adding a <code class="literal">CHECK</code>658 constraint as described in659 <a class="xref" href="ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE-MAINTENANCE" title="5.11.2.2. Partition Maintenance">Section 5.11.2.2</a>.660 </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>661 This form detaches the specified partition of the target table. The detached662 partition continues to exist as a standalone table, but no longer has any663 ties to the table from which it was detached. Any indexes that were664 attached to the target table's indexes are detached. Any triggers that665 were created as clones of those in the target table are removed.666 <code class="literal">SHARE</code> lock is obtained on any tables that reference667 this partitioned table in foreign key constraints.668 </p><p>669 If <code class="literal">CONCURRENTLY</code> is specified, it runs using a reduced670 lock level to avoid blocking other sessions that might be accessing the671 partitioned table. In this mode, two transactions are used internally.672 During the first transaction, a <code class="literal">SHARE UPDATE EXCLUSIVE</code>673 lock is taken on both parent table and partition, and the partition is674 marked as undergoing detach; at that point, the transaction is committed675 and all other transactions using the partitioned table are waited for.676 Once all those transactions have completed, the second transaction677 acquires <code class="literal">SHARE UPDATE EXCLUSIVE</code> on the partitioned678 table and <code class="literal">ACCESS EXCLUSIVE</code> on the partition,679 and the detach process completes. A <code class="literal">CHECK</code> constraint680 that duplicates the partition constraint is added to the partition.681 <code class="literal">CONCURRENTLY</code> cannot be run in a transaction block and682 is not allowed if the partitioned table contains a default partition.683 </p><p>684 If <code class="literal">FINALIZE</code> is specified, a previous685 <code class="literal">DETACH CONCURRENTLY</code> invocation that was canceled or686 interrupted is completed.687 At most one partition in a partitioned table can be pending detach at688 a time.689 </p></dd></dl></div><p>690 </p><p>691 All the forms of ALTER TABLE that act on a single table, except692 <code class="literal">RENAME</code>, <code class="literal">SET SCHEMA</code>,693 <code class="literal">ATTACH PARTITION</code>, and694 <code class="literal">DETACH PARTITION</code> can be combined into695 a list of multiple alterations to be applied together. For example, it696 is possible to add several columns and/or alter the type of several697 columns in a single command. This is particularly useful with large698 tables, since only one pass over the table need be made.699 </p><p>700 You must own the table to use <code class="command">ALTER TABLE</code>.701 To change the schema or tablespace of a table, you must also have702 <code class="literal">CREATE</code> privilege on the new schema or tablespace.703 To add the table as a new child of a parent table, you must own the parent704 table as well. Also, to attach a table as a new partition of the table,705 you must own the table being attached.706 To alter the owner, you must be able to <code class="literal">SET ROLE</code> to the707 new owning role, and that role must have <code class="literal">CREATE</code>708 privilege on the table's schema.709 (These restrictions enforce that altering the owner710 doesn't do anything you couldn't do by dropping and recreating the table.711 However, a superuser can alter ownership of any table anyway.)712 To add a column or alter a column type or use the <code class="literal">OF</code>713 clause, you must also have <code class="literal">USAGE</code> privilege on the data714 type.715 </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>716 Do not throw an error if the table does not exist. A notice is issued717 in this case.718 </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>719 The name (optionally schema-qualified) of an existing table to720 alter. If <code class="literal">ONLY</code> is specified before the table name, only721 that table is altered. If <code class="literal">ONLY</code> is not specified, the table722 and all its descendant tables (if any) are altered. Optionally,723 <code class="literal">*</code> can be specified after the table name to explicitly724 indicate that descendant tables are included.725 </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>726 Name of a new or existing column.727 </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>728 New name for an existing column.729 </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>730 New name for the table.731 </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>732 Data type of the new column, or new data type for an existing733 column.734 </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>735 New table constraint for the table.736 </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>737 Name of a new or existing constraint.738 </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>739 Automatically drop objects that depend on the dropped column740 or constraint (for example, views referencing the column),741 and in turn all objects that depend on those objects742 (see <a class="xref" href="ddl-depend.html" title="5.14. Dependency Tracking">Section 5.14</a>).743 </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>744 Refuse to drop the column or constraint if there are any dependent745 objects. This is the default behavior.746 </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>747 Name of a single trigger to disable or enable.748 </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>749 Disable or enable all triggers belonging to the table.750 (This requires superuser privilege if any of the triggers are751 internally generated constraint triggers, such as those that are used752 to implement foreign key constraints or deferrable uniqueness and753 exclusion constraints.)754 </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>755 Disable or enable all triggers belonging to the table except for756 internally generated constraint triggers, such as those that are used757 to implement foreign key constraints or deferrable uniqueness and758 exclusion constraints.759 </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>760 The name of an existing index.761 </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>762 The name of a table storage parameter.763 </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>764 The new value for a table storage parameter.765 This might be a number or a word depending on the parameter.766 </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>767 A parent table to associate or de-associate with this table.768 </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>769 The user name of the new owner of the table.770 </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>771 The name of the access method to which the table will be converted.772 </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>773 The name of the tablespace to which the table will be moved.774 </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>775 The name of the schema to which the table will be moved.776 </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>777 The name of the table to attach as a new partition or to detach from this table.778 </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>779 The partition bound specification for a new partition. Refer to780 <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.781 </p></dd></dl></div></div><div class="refsect1" id="SQL-ALTERTABLE-NOTES"><h2>Notes</h2><p>782 The key word <code class="literal">COLUMN</code> is noise and can be omitted.783 </p><p>784 When a column is added with <code class="literal">ADD COLUMN</code> and a785 non-volatile <code class="literal">DEFAULT</code> is specified, the default is786 evaluated at the time of the statement and the result stored in the787 table's metadata. That value will be used for the column for all existing788 rows. If no <code class="literal">DEFAULT</code> is specified, NULL is used. In789 neither case is a rewrite of the table required.790 </p><p>791 Adding a column with a volatile <code class="literal">DEFAULT</code> or792 changing the type of an existing column will require the entire table and793 its indexes to be rewritten. As an exception, when changing the type of an794 existing column, if the <code class="literal">USING</code> clause does not change795 the column contents and the old type is either binary coercible to the new796 type or an unconstrained domain over the new type, a table rewrite is not797 needed. However, indexes must always be rebuilt unless the system can798 verify that the new index would be logically equivalent to the existing799 one. For example, if the collation for a column has been changed, an index800 rebuild is always required because the new sort order might be different.801 However, in the absence of a collation change, a column can be changed802 from <code class="type">text</code> to <code class="type">varchar</code> (or vice versa) without803 rebuilding the indexes because these data types sort identically.804 Table and/or index rebuilds may take a805 significant amount of time for a large table; and will temporarily require806 as much as double the disk space.807 </p><p>808 Adding a <code class="literal">CHECK</code> or <code class="literal">NOT NULL</code> constraint requires809 scanning the table to verify that existing rows meet the constraint,810 but does not require a table rewrite.811 </p><p>812 Similarly, when attaching a new partition it may be scanned to verify that813 existing rows meet the partition constraint.814 </p><p>815 The main reason for providing the option to specify multiple changes816 in a single <code class="command">ALTER TABLE</code> is that multiple table scans or817 rewrites can thereby be combined into a single pass over the table.818 </p><p>819 Scanning a large table to verify a new foreign key or check constraint820 can take a long time, and other updates to the table are locked out821 until the <code class="command">ALTER TABLE ADD CONSTRAINT</code> command is822 committed. The main purpose of the <code class="literal">NOT VALID</code>823 constraint option is to reduce the impact of adding a constraint on824 concurrent updates. With <code class="literal">NOT VALID</code>,825 the <code class="command">ADD CONSTRAINT</code> command does not scan the table826 and can be committed immediately. After that, a <code class="literal">VALIDATE827 CONSTRAINT</code> command can be issued to verify that existing rows828 satisfy the constraint. The validation step does not need to lock out829 concurrent updates, since it knows that other transactions will be830 enforcing the constraint for rows that they insert or update; only831 pre-existing rows need to be checked. Hence, validation acquires only832 a <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock on the table being833 altered. (If the constraint is a foreign key then a <code class="literal">ROW834 SHARE</code> lock is also required on the table referenced by the835 constraint.) In addition to improving concurrency, it can be useful to836 use <code class="literal">NOT VALID</code> and <code class="literal">VALIDATE837 CONSTRAINT</code> in cases where the table is known to contain838 pre-existing violations. Once the constraint is in place, no new839 violations can be inserted, and the existing problems can be corrected840 at leisure until <code class="literal">VALIDATE CONSTRAINT</code> finally841 succeeds.842 </p><p>843 The <code class="literal">DROP COLUMN</code> form does not physically remove844 the column, but simply makes it invisible to SQL operations. Subsequent845 insert and update operations in the table will store a null value for the846 column. Thus, dropping a column is quick but it will not immediately847 reduce the on-disk size of your table, as the space occupied848 by the dropped column is not reclaimed. The space will be849 reclaimed over time as existing rows are updated.850 </p><p>851 To force immediate reclamation of space occupied by a dropped column,852 you can execute one of the forms of <code class="command">ALTER TABLE</code> that853 performs a rewrite of the whole table. This results in reconstructing854 each row with the dropped column replaced by a null value.855 </p><p>856 The rewriting forms of <code class="command">ALTER TABLE</code> are not MVCC-safe.857 After a table rewrite, the table will appear empty to concurrent858 transactions, if they are using a snapshot taken before the rewrite859 occurred. See <a class="xref" href="mvcc-caveats.html" title="13.6. Caveats">Section 13.6</a> for more details.860 </p><p>861 The <code class="literal">USING</code> option of <code class="literal">SET DATA TYPE</code> can actually862 specify any expression involving the old values of the row; that is, it863 can refer to other columns as well as the one being converted. This allows864 very general conversions to be done with the <code class="literal">SET DATA TYPE</code>865 syntax. Because of this flexibility, the <code class="literal">USING</code>866 expression is not applied to the column's default value (if any); the867 result might not be a constant expression as required for a default.868 This means that when there is no implicit or assignment cast from old to869 new type, <code class="literal">SET DATA TYPE</code> might fail to convert the default even870 though a <code class="literal">USING</code> clause is supplied. In such cases,871 drop the default with <code class="literal">DROP DEFAULT</code>, perform the <code class="literal">ALTER872 TYPE</code>, and then use <code class="literal">SET DEFAULT</code> to add a suitable new873 default. Similar considerations apply to indexes and constraints involving874 the column.875 </p><p>876 If a table has any descendant tables, it is not permitted to add,877 rename, or change the type of a column in the parent table without doing878 the same to the descendants. This ensures that the descendants always879 have columns matching the parent. Similarly, a <code class="literal">CHECK</code>880 constraint cannot be renamed in the parent without also renaming it in881 all descendants, so that <code class="literal">CHECK</code> constraints also match882 between the parent and its descendants. (That restriction does not apply883 to index-based constraints, however.)884 Also, because selecting from the parent also selects from its descendants,885 a constraint on the parent cannot be marked valid unless it is also marked886 valid for those descendants. In all of these cases, <code class="command">ALTER TABLE887 ONLY</code> will be rejected.888 </p><p>889 A recursive <code class="literal">DROP COLUMN</code> operation will remove a890 descendant table's column only if the descendant does not inherit891 that column from any other parents and never had an independent892 definition of the column. A nonrecursive <code class="literal">DROP893 COLUMN</code> (i.e., <code class="command">ALTER TABLE ONLY ... DROP894 COLUMN</code>) never removes any descendant columns, but895 instead marks them as independently defined rather than inherited.896 A nonrecursive <code class="literal">DROP COLUMN</code> command will fail for a897 partitioned table, because all partitions of a table must have the same898 columns as the partitioning root.899 </p><p>900 The actions for identity columns (<code class="literal">ADD901 GENERATED</code>, <code class="literal">SET</code> etc., <code class="literal">DROP902 IDENTITY</code>), as well as the actions903 <code class="literal">CLUSTER</code>, <code class="literal">OWNER</code>,904 and <code class="literal">TABLESPACE</code> never recurse to descendant tables;905 that is, they always act as though <code class="literal">ONLY</code> were specified.906 Actions affecting trigger states recurse to partitions of partitioned907 tables (unless <code class="literal">ONLY</code> is specified), but never to908 traditional-inheritance descendants.909 Adding a constraint recurses only for <code class="literal">CHECK</code> constraints910 that are not marked <code class="literal">NO INHERIT</code>.911 </p><p>912 Changing any part of a system catalog table is not permitted.913 </p><p>914 Refer to <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for a further description of valid915 parameters. <a class="xref" href="ddl.html" title="Chapter 5. Data Definition">Chapter 5</a> has further information on916 inheritance.917 </p></div><div class="refsect1" id="id-1.9.3.35.8"><h2>Examples</h2><p>918 To add a column of type <code class="type">varchar</code> to a table:919</p><pre class="programlisting">920ALTER TABLE distributors ADD COLUMN address varchar(30);921</pre><p>922 That will cause all existing rows in the table to be filled with null923 values for the new column.924 </p><p>925 To add a column with a non-null default:926</p><pre class="programlisting">927ALTER TABLE measurements928 ADD COLUMN mtime timestamp with time zone DEFAULT now();929</pre><p>930 Existing rows will be filled with the current time as the value of the931 new column, and then new rows will receive the time of their insertion.932 </p><p>933 To add a column and fill it with a value different from the default to934 be used later:935</p><pre class="programlisting">936ALTER TABLE transactions937 ADD COLUMN status varchar(30) DEFAULT 'old',938 ALTER COLUMN status SET default 'current';939</pre><p>940 Existing rows will be filled with <code class="literal">old</code>, but then941 the default for subsequent commands will be <code class="literal">current</code>.942 The effects are the same as if the two sub-commands had been issued943 in separate <code class="command">ALTER TABLE</code> commands.944 </p><p>945 To drop a column from a table:946</p><pre class="programlisting">947ALTER TABLE distributors DROP COLUMN address RESTRICT;948</pre><p>949 </p><p>950 To change the types of two existing columns in one operation:951</p><pre class="programlisting">952ALTER TABLE distributors953 ALTER COLUMN address TYPE varchar(80),954 ALTER COLUMN name TYPE varchar(100);955</pre><p>956 </p><p>957 To change an integer column containing Unix timestamps to <code class="type">timestamp958 with time zone</code> via a <code class="literal">USING</code> clause:959</p><pre class="programlisting">960ALTER TABLE foo961 ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone962 USING963 timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';964</pre><p>965 </p><p>966 The same, when the column has a default expression that won't automatically967 cast to the new data type:968</p><pre class="programlisting">969ALTER TABLE foo970 ALTER COLUMN foo_timestamp DROP DEFAULT,971 ALTER COLUMN foo_timestamp TYPE timestamp with time zone972 USING973 timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',974 ALTER COLUMN foo_timestamp SET DEFAULT now();975</pre><p>976 </p><p>977 To rename an existing column:978</p><pre class="programlisting">979ALTER TABLE distributors RENAME COLUMN address TO city;980</pre><p>981 </p><p>982 To rename an existing table:983</p><pre class="programlisting">984ALTER TABLE distributors RENAME TO suppliers;985</pre><p>986 </p><p>987 To rename an existing constraint:988</p><pre class="programlisting">989ALTER TABLE distributors RENAME CONSTRAINT zipchk TO zip_check;990</pre><p>991 </p><p>992 To add a not-null constraint to a column:993</p><pre class="programlisting">994ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;995</pre><p>996 To remove a not-null constraint from a column:997</p><pre class="programlisting">998ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;999</pre><p>1000 </p><p>1001 To add a check constraint to a table and all its children:1002</p><pre class="programlisting">1003ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);1004</pre><p>1005 </p><p>1006 To add a check constraint only to a table and not to its children:1007</p><pre class="programlisting">1008ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;1009</pre><p>1010 (The check constraint will not be inherited by future children, either.)1011 </p><p>1012 To remove a check constraint from a table and all its children:1013</p><pre class="programlisting">1014ALTER TABLE distributors DROP CONSTRAINT zipchk;1015</pre><p>1016 </p><p>1017 To remove a check constraint from one table only:1018</p><pre class="programlisting">1019ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;1020</pre><p>1021 (The check constraint remains in place for any child tables.)1022 </p><p>1023 To add a foreign key constraint to a table:1024</p><pre class="programlisting">1025ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address);1026</pre><p>1027 </p><p>1028 To add a foreign key constraint to a table with the least impact on other work:1029</p><pre class="programlisting">1030ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) NOT VALID;1031ALTER TABLE distributors VALIDATE CONSTRAINT distfk;1032</pre><p>1033 </p><p>1034 To add a (multicolumn) unique constraint to a table:1035</p><pre class="programlisting">1036ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);1037</pre><p>1038 </p><p>1039 To add an automatically named primary key constraint to a table, noting1040 that a table can only ever have one primary key:1041</p><pre class="programlisting">1042ALTER TABLE distributors ADD PRIMARY KEY (dist_id);1043</pre><p>1044 </p><p>1045 To move a table to a different tablespace:1046</p><pre class="programlisting">1047ALTER TABLE distributors SET TABLESPACE fasttablespace;1048</pre><p>1049 </p><p>1050 To move a table to a different schema:1051</p><pre class="programlisting">1052ALTER TABLE myschema.distributors SET SCHEMA yourschema;1053</pre><p>1054 </p><p>1055 To recreate a primary key constraint, without blocking updates while the1056 index is rebuilt:1057</p><pre class="programlisting">1058CREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);1059ALTER TABLE distributors DROP CONSTRAINT distributors_pkey,1060 ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;1061</pre><p>1062 To attach a partition to a range-partitioned table:1063</p><pre class="programlisting">1064ALTER TABLE measurement1065 ATTACH PARTITION measurement_y2016m07 FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');1066</pre><p>1067 To attach a partition to a list-partitioned table:1068</p><pre class="programlisting">1069ALTER TABLE cities1070 ATTACH PARTITION cities_ab FOR VALUES IN ('a', 'b');1071</pre><p>1072 To attach a partition to a hash-partitioned table:1073</p><pre class="programlisting">1074ALTER TABLE orders1075 ATTACH PARTITION orders_p4 FOR VALUES WITH (MODULUS 4, REMAINDER 3);1076</pre><p>1077 To attach a default partition to a partitioned table:1078</p><pre class="programlisting">1079ALTER TABLE cities1080 ATTACH PARTITION cities_partdef DEFAULT;1081</pre><p>1082 To detach a partition from a partitioned table:1083</p><pre class="programlisting">1084ALTER TABLE measurement1085 DETACH PARTITION measurement_y2015m12;1086</pre></div><div class="refsect1" id="id-1.9.3.35.9"><h2>Compatibility</h2><p>1087 The forms <code class="literal">ADD</code> (without <code class="literal">USING INDEX</code>),1088 <code class="literal">DROP [COLUMN]</code>, <code class="literal">DROP IDENTITY</code>, <code class="literal">RESTART</code>,1089 <code class="literal">SET DEFAULT</code>, <code class="literal">SET DATA TYPE</code> (without <code class="literal">USING</code>),1090 <code class="literal">SET GENERATED</code>, and <code class="literal">SET <em class="replaceable"><code>sequence_option</code></em></code>1091 conform with the SQL standard. The other forms are1092 <span class="productname">PostgreSQL</span> extensions of the SQL standard.1093 Also, the ability to specify more than one manipulation in a single1094 <code class="command">ALTER TABLE</code> command is an extension.1095 </p><p>1096 <code class="command">ALTER TABLE DROP COLUMN</code> can be used to drop the only1097 column of a table, leaving a zero-column table. This is an1098 extension of SQL, which disallows zero-column tables.1099 </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 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> ALTER TABLESPACE</td></tr></table></div></body></html>