gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

963 rindas
69KB

  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 V1.79.1" /><link rel="prev" href="sql-altersystem.html" title="ALTER SYSTEM" /><link rel="next" href="sql-altertablespace.html" title="ALTER TABLESPACE" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" 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 12.4 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></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>
  19. <span class="phrase">where <em class="replaceable"><code>action</code></em> is one of:</span>
  20. 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> [ ... ] ]
  21. DROP [ COLUMN ] [ IF EXISTS ] <em class="replaceable"><code>column_name</code></em> [ RESTRICT | CASCADE ]
  22. 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> ]
  23. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET DEFAULT <em class="replaceable"><code>expression</code></em>
  24. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> DROP DEFAULT
  25. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> { SET | DROP } NOT NULL
  26. 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> ) ]
  27. 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> ] } [...]
  28. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> DROP IDENTITY [ IF EXISTS ]
  29. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET STATISTICS <em class="replaceable"><code>integer</code></em>
  30. 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> [, ... ] )
  31. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> RESET ( <em class="replaceable"><code>attribute_option</code></em> [, ... ] )
  32. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
  33. ADD <em class="replaceable"><code>table_constraint</code></em> [ NOT VALID ]
  34. ADD <em class="replaceable"><code>table_constraint_using_index</code></em>
  35. ALTER CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
  36. VALIDATE CONSTRAINT <em class="replaceable"><code>constraint_name</code></em>
  37. DROP CONSTRAINT [ IF EXISTS ] <em class="replaceable"><code>constraint_name</code></em> [ RESTRICT | CASCADE ]
  38. DISABLE TRIGGER [ <em class="replaceable"><code>trigger_name</code></em> | ALL | USER ]
  39. ENABLE TRIGGER [ <em class="replaceable"><code>trigger_name</code></em> | ALL | USER ]
  40. ENABLE REPLICA TRIGGER <em class="replaceable"><code>trigger_name</code></em>
  41. ENABLE ALWAYS TRIGGER <em class="replaceable"><code>trigger_name</code></em>
  42. DISABLE RULE <em class="replaceable"><code>rewrite_rule_name</code></em>
  43. ENABLE RULE <em class="replaceable"><code>rewrite_rule_name</code></em>
  44. ENABLE REPLICA RULE <em class="replaceable"><code>rewrite_rule_name</code></em>
  45. ENABLE ALWAYS RULE <em class="replaceable"><code>rewrite_rule_name</code></em>
  46. DISABLE ROW LEVEL SECURITY
  47. ENABLE ROW LEVEL SECURITY
  48. FORCE ROW LEVEL SECURITY
  49. NO FORCE ROW LEVEL SECURITY
  50. CLUSTER ON <em class="replaceable"><code>index_name</code></em>
  51. SET WITHOUT CLUSTER
  52. SET WITHOUT OIDS
  53. SET TABLESPACE <em class="replaceable"><code>new_tablespace</code></em>
  54. SET { LOGGED | UNLOGGED }
  55. SET ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] )
  56. RESET ( <em class="replaceable"><code>storage_parameter</code></em> [, ... ] )
  57. INHERIT <em class="replaceable"><code>parent_table</code></em>
  58. NO INHERIT <em class="replaceable"><code>parent_table</code></em>
  59. OF <em class="replaceable"><code>type_name</code></em>
  60. NOT OF
  61. OWNER TO { <em class="replaceable"><code>new_owner</code></em> | CURRENT_USER | SESSION_USER }
  62. REPLICA IDENTITY { DEFAULT | USING INDEX <em class="replaceable"><code>index_name</code></em> | FULL | NOTHING }
  63. <span class="phrase">and <em class="replaceable"><code>partition_bound_spec</code></em> is:</span>
  64. IN ( <em class="replaceable"><code>partition_bound_expr</code></em> [, ...] ) |
  65. FROM ( { <em class="replaceable"><code>partition_bound_expr</code></em> | MINVALUE | MAXVALUE } [, ...] )
  66. TO ( { <em class="replaceable"><code>partition_bound_expr</code></em> | MINVALUE | MAXVALUE } [, ...] ) |
  67. WITH ( MODULUS <em class="replaceable"><code>numeric_literal</code></em>, REMAINDER <em class="replaceable"><code>numeric_literal</code></em> )
  68. <span class="phrase">and <em class="replaceable"><code>column_constraint</code></em> is:</span>
  69. [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]
  70. { NOT NULL |
  71. NULL |
  72. CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ] |
  73. DEFAULT <em class="replaceable"><code>default_expr</code></em> |
  74. GENERATED ALWAYS AS ( <em class="replaceable"><code>generation_expr</code></em> ) STORED |
  75. GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <em class="replaceable"><code>sequence_options</code></em> ) ] |
  76. UNIQUE <em class="replaceable"><code>index_parameters</code></em> |
  77. PRIMARY KEY <em class="replaceable"><code>index_parameters</code></em> |
  78. REFERENCES <em class="replaceable"><code>reftable</code></em> [ ( <em class="replaceable"><code>refcolumn</code></em> ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
  79. [ ON DELETE <em class="replaceable"><code>referential_action</code></em> ] [ ON UPDATE <em class="replaceable"><code>referential_action</code></em> ] }
  80. [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
  81. <span class="phrase">and <em class="replaceable"><code>table_constraint</code></em> is:</span>
  82. [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]
  83. { CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ] |
  84. UNIQUE ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) <em class="replaceable"><code>index_parameters</code></em> |
  85. PRIMARY KEY ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) <em class="replaceable"><code>index_parameters</code></em> |
  86. 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> ) ] |
  87. 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> [, ... ] ) ]
  88. [ 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> ] }
  89. [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
  90. <span class="phrase">and <em class="replaceable"><code>table_constraint_using_index</code></em> is:</span>
  91. [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]
  92. { UNIQUE | PRIMARY KEY } USING INDEX <em class="replaceable"><code>index_name</code></em>
  93. [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
  94. <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>
  95. [ INCLUDE ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) ]
  96. [ WITH ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] ) ]
  97. [ USING INDEX TABLESPACE <em class="replaceable"><code>tablespace_name</code></em> ]
  98. <span class="phrase"><em class="replaceable"><code>exclude_element</code></em> in an <code class="literal">EXCLUDE</code> constraint is:</span>
  99. { <em class="replaceable"><code>column_name</code></em> | ( <em class="replaceable"><code>expression</code></em> ) } [ <em class="replaceable"><code>opclass</code></em> ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]
  100. </pre></div><div class="refsect1" id="id-1.9.3.35.5"><h2>Description</h2><p>
  101. <code class="command">ALTER TABLE</code> changes the definition of an existing table.
  102. There are several subforms described below. Note that the lock level required
  103. may differ for each subform. An <code class="literal">ACCESS EXCLUSIVE</code> lock is
  104. acquired unless explicitly noted. When multiple subcommands are given, the
  105. lock acquired will be the strictest one required by any subcommand.
  106. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">ADD COLUMN [ IF NOT EXISTS ]</code></span></dt><dd><p>
  107. This form adds a new column to the table, using the same syntax as
  108. <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a>. If <code class="literal">IF NOT EXISTS</code>
  109. is specified and a column already exists with this name,
  110. no error is thrown.
  111. </p></dd><dt><span class="term"><code class="literal">DROP COLUMN [ IF EXISTS ]</code></span></dt><dd><p>
  112. This form drops a column from a table. Indexes and
  113. table constraints involving the column will be automatically
  114. dropped as well.
  115. Multivariate statistics referencing the dropped column will also be
  116. removed if the removal of the column would cause the statistics to
  117. contain data for only a single column.
  118. You will need to say <code class="literal">CASCADE</code> if anything outside the table
  119. depends on the column, for example, foreign key references or views.
  120. If <code class="literal">IF EXISTS</code> is specified and the column
  121. does not exist, no error is thrown. In this case a notice
  122. is issued instead.
  123. </p></dd><dt><span class="term"><code class="literal">SET DATA TYPE</code></span></dt><dd><p>
  124. This form changes the type of a column of a table. Indexes and
  125. simple table constraints involving the column will be automatically
  126. converted to use the new column type by reparsing the originally
  127. supplied expression.
  128. The optional <code class="literal">COLLATE</code> clause specifies a collation
  129. for the new column; if omitted, the collation is the default for the
  130. new column type.
  131. The optional <code class="literal">USING</code>
  132. clause specifies how to compute the new column value from the old;
  133. if omitted, the default conversion is the same as an assignment
  134. cast from old data type to new. A <code class="literal">USING</code>
  135. clause must be provided if there is no implicit or assignment
  136. cast from old to new type.
  137. </p></dd><dt><span class="term"><code class="literal">SET</code>/<code class="literal">DROP DEFAULT</code></span></dt><dd><p>
  138. These forms set or remove the default value for a column.
  139. Default values only apply in subsequent <code class="command">INSERT</code>
  140. or <code class="command">UPDATE</code> commands; they do not cause rows already in the
  141. table to change.
  142. </p></dd><dt><span class="term"><code class="literal">SET</code>/<code class="literal">DROP NOT NULL</code></span></dt><dd><p>
  143. These forms change whether a column is marked to allow null
  144. values or to reject null values.
  145. </p><p>
  146. <code class="literal">SET NOT NULL</code> may only be applied to a column
  147. provided none of the records in the table contain a
  148. <code class="literal">NULL</code> value for the column. Ordinarily this is
  149. checked during the <code class="literal">ALTER TABLE</code> by scanning the
  150. entire table; however, if a valid <code class="literal">CHECK</code> constraint is
  151. found which proves no <code class="literal">NULL</code> can exist, then the
  152. table scan is skipped.
  153. </p><p>
  154. If this table is a partition, one cannot perform <code class="literal">DROP NOT NULL</code>
  155. on a column if it is marked <code class="literal">NOT NULL</code> in the parent
  156. table. To drop the <code class="literal">NOT NULL</code> constraint from all the
  157. partitions, perform <code class="literal">DROP NOT NULL</code> on the parent
  158. table. Even if there is no <code class="literal">NOT NULL</code> constraint on the
  159. parent, such a constraint can still be added to individual partitions,
  160. if desired; that is, the children can disallow nulls even if the parent
  161. allows them, but not the other way around.
  162. </p></dd><dt><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></dt><dd><p>
  163. These forms change whether a column is an identity column or change the
  164. generation attribute of an existing identity column.
  165. See <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for details.
  166. </p><p>
  167. If <code class="literal">DROP IDENTITY IF EXISTS</code> is specified and the
  168. column is not an identity column, no error is thrown. In this case a
  169. notice is issued instead.
  170. </p></dd><dt><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></dt><dd><p>
  171. These forms alter the sequence that underlies an existing identity
  172. column. <em class="replaceable"><code>sequence_option</code></em> is an option
  173. supported by <a class="xref" href="sql-altersequence.html" title="ALTER SEQUENCE"><span class="refentrytitle">ALTER SEQUENCE</span></a> such
  174. as <code class="literal">INCREMENT BY</code>.
  175. </p></dd><dt><span class="term"><code class="literal">SET STATISTICS</code></span></dt><dd><p>
  176. This form
  177. sets the per-column statistics-gathering target for subsequent
  178. <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a> operations.
  179. The target can be set in the range 0 to 10000; alternatively, set it
  180. to -1 to revert to using the system default statistics
  181. target (<a class="xref" href="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET">default_statistics_target</a>).
  182. For more information on the use of statistics by the
  183. <span class="productname">PostgreSQL</span> query planner, refer to
  184. <a class="xref" href="planner-stats.html" title="14.2. Statistics Used by the Planner">Section 14.2</a>.
  185. </p><p>
  186. <code class="literal">SET STATISTICS</code> acquires a
  187. <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.
  188. </p></dd><dt><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></dt><dd><p>
  189. This form sets or resets per-attribute options. Currently, the only
  190. defined per-attribute options are <code class="literal">n_distinct</code> and
  191. <code class="literal">n_distinct_inherited</code>, which override the
  192. number-of-distinct-values estimates made by subsequent
  193. <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a>
  194. operations. <code class="literal">n_distinct</code> affects the statistics for the table
  195. itself, while <code class="literal">n_distinct_inherited</code> affects the statistics
  196. gathered for the table plus its inheritance children. When set to a
  197. positive value, <code class="command">ANALYZE</code> will assume that the column contains
  198. exactly the specified number of distinct nonnull values. When set to a
  199. negative value, which must be greater
  200. than or equal to -1, <code class="command">ANALYZE</code> will assume that the number of
  201. distinct nonnull values in the column is linear in the size of the
  202. table; the exact count is to be computed by multiplying the estimated
  203. table size by the absolute value of the given number. For example,
  204. a value of -1 implies that all values in the column are distinct, while
  205. a value of -0.5 implies that each value appears twice on the average.
  206. This can be useful when the size of the table changes over time, since
  207. the multiplication by the number of rows in the table is not performed
  208. until query planning time. Specify a value of 0 to revert to estimating
  209. the number of distinct values normally. For more information on the use
  210. of statistics by the <span class="productname">PostgreSQL</span> query
  211. planner, refer to <a class="xref" href="planner-stats.html" title="14.2. Statistics Used by the Planner">Section 14.2</a>.
  212. </p><p>
  213. Changing per-attribute options acquires a
  214. <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.
  215. </p></dd><dt><span class="term">
  216. <code class="literal">SET STORAGE</code>
  217. <a id="id-1.9.3.35.5.2.3.10.1.2" class="indexterm"></a>
  218. </span></dt><dd><p>
  219. This form sets the storage mode for a column. This controls whether this
  220. column is held inline or in a secondary <acronym class="acronym">TOAST</acronym> table, and
  221. whether the data
  222. should be compressed or not. <code class="literal">PLAIN</code> must be used
  223. for fixed-length values such as <code class="type">integer</code> and is
  224. inline, uncompressed. <code class="literal">MAIN</code> is for inline,
  225. compressible data. <code class="literal">EXTERNAL</code> is for external,
  226. uncompressed data, and <code class="literal">EXTENDED</code> is for external,
  227. compressed data. <code class="literal">EXTENDED</code> is the default for most
  228. data types that support non-<code class="literal">PLAIN</code> storage.
  229. Use of <code class="literal">EXTERNAL</code> will make substring operations on
  230. very large <code class="type">text</code> and <code class="type">bytea</code> values run faster,
  231. at the penalty of increased storage space. Note that
  232. <code class="literal">SET STORAGE</code> doesn't itself change anything in the table,
  233. it just sets the strategy to be pursued during future table updates.
  234. See <a class="xref" href="storage-toast.html" title="68.2. TOAST">Section 68.2</a> for more information.
  235. </p></dd><dt><span class="term"><code class="literal">ADD <em class="replaceable"><code>table_constraint</code></em> [ NOT VALID ]</code></span></dt><dd><p>
  236. This form adds a new constraint to a table using the same constraint
  237. syntax as <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a>, plus the option <code class="literal">NOT
  238. VALID</code>, which is currently only allowed for foreign key
  239. and CHECK constraints.
  240. </p><p>
  241. Normally, this form will cause a scan of the table to verify that all
  242. existing rows in the table satisfy the new constraint. But if
  243. the <code class="literal">NOT VALID</code> option is used, this
  244. potentially-lengthy scan is skipped. The constraint will still be
  245. enforced against subsequent inserts or updates (that is, they'll fail
  246. unless there is a matching row in the referenced table, in the case
  247. of foreign keys, or they'll fail unless the new row matches the
  248. specified check condition). But the
  249. database will not assume that the constraint holds for all rows in
  250. the table, until it is validated by using the <code class="literal">VALIDATE
  251. CONSTRAINT</code> option.
  252. See <a class="xref" href="sql-altertable.html#SQL-ALTERTABLE-NOTES" title="Notes">Notes</a> below for more information
  253. about using the <code class="literal">NOT VALID</code> option.
  254. </p><p>
  255. Although most forms of <code class="literal">ADD
  256. <em class="replaceable"><code>table_constraint</code></em></code>
  257. require an <code class="literal">ACCESS EXCLUSIVE</code> lock, <code class="literal">ADD
  258. FOREIGN KEY</code> requires only a <code class="literal">SHARE ROW
  259. EXCLUSIVE</code> lock. Note that <code class="literal">ADD FOREIGN KEY</code>
  260. also acquires a <code class="literal">SHARE ROW EXCLUSIVE</code> lock on the
  261. referenced table, in addition to the lock on the table on which the
  262. constraint is declared.
  263. </p><p>
  264. Additional restrictions apply when unique or primary key constraints
  265. are added to partitioned tables; see <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a>.
  266. Also, foreign key constraints on partitioned
  267. tables may not be declared <code class="literal">NOT VALID</code> at present.
  268. </p></dd><dt><span class="term"><code class="literal">ADD <em class="replaceable"><code>table_constraint_using_index</code></em></code></span></dt><dd><p>
  269. This form adds a new <code class="literal">PRIMARY KEY</code> or <code class="literal">UNIQUE</code>
  270. constraint to a table based on an existing unique index. All the
  271. columns of the index will be included in the constraint.
  272. </p><p>
  273. The index cannot have expression columns nor be a partial index.
  274. Also, it must be a b-tree index with default sort ordering. These
  275. restrictions ensure that the index is equivalent to one that would be
  276. built by a regular <code class="literal">ADD PRIMARY KEY</code> or <code class="literal">ADD UNIQUE</code>
  277. command.
  278. </p><p>
  279. If <code class="literal">PRIMARY KEY</code> is specified, and the index's columns are not
  280. already marked <code class="literal">NOT NULL</code>, then this command will attempt to
  281. do <code class="literal">ALTER COLUMN SET NOT NULL</code> against each such column.
  282. That requires a full table scan to verify the column(s) contain no
  283. nulls. In all other cases, this is a fast operation.
  284. </p><p>
  285. If a constraint name is provided then the index will be renamed to match
  286. the constraint name. Otherwise the constraint will be named the same as
  287. the index.
  288. </p><p>
  289. After this command is executed, the index is <span class="quote">“<span class="quote">owned</span>”</span> by the
  290. constraint, in the same way as if the index had been built by
  291. a regular <code class="literal">ADD PRIMARY KEY</code> or <code class="literal">ADD UNIQUE</code>
  292. command. In particular, dropping the constraint will make the index
  293. disappear too.
  294. </p><p>
  295. This form is not currently supported on partitioned tables.
  296. </p><div class="note"><h3 class="title">Note</h3><p>
  297. Adding a constraint using an existing index can be helpful in
  298. situations where a new constraint needs to be added without blocking
  299. table updates for a long time. To do that, create the index using
  300. <code class="command">CREATE INDEX CONCURRENTLY</code>, and then install it as an
  301. official constraint using this syntax. See the example below.
  302. </p></div></dd><dt><span class="term"><code class="literal">ALTER CONSTRAINT</code></span></dt><dd><p>
  303. This form alters the attributes of a constraint that was previously
  304. created. Currently only foreign key constraints may be altered.
  305. </p></dd><dt><span class="term"><code class="literal">VALIDATE CONSTRAINT</code></span></dt><dd><p>
  306. This form validates a foreign key or check constraint that was
  307. previously created as <code class="literal">NOT VALID</code>, by scanning the
  308. table to ensure there are no rows for which the constraint is not
  309. satisfied. Nothing happens if the constraint is already marked valid.
  310. (See <a class="xref" href="sql-altertable.html#SQL-ALTERTABLE-NOTES" title="Notes">Notes</a> below for an explanation of the
  311. usefulness of this command.)
  312. </p></dd><dt><span class="term"><code class="literal">DROP CONSTRAINT [ IF EXISTS ]</code></span></dt><dd><p>
  313. This form drops the specified constraint on a table, along with
  314. any index underlying the constraint.
  315. If <code class="literal">IF EXISTS</code> is specified and the constraint
  316. does not exist, no error is thrown. In this case a notice is issued instead.
  317. </p></dd><dt><span class="term"><code class="literal">DISABLE</code>/<code class="literal">ENABLE [ REPLICA | ALWAYS ] TRIGGER</code></span></dt><dd><p>
  318. These forms configure the firing of trigger(s) belonging to the table.
  319. A disabled trigger is still known to the system, but is not executed
  320. when its triggering event occurs. For a deferred trigger, the enable
  321. status is checked when the event occurs, not when the trigger function
  322. is actually executed. One can disable or enable a single
  323. trigger specified by name, or all triggers on the table, or only
  324. user triggers (this option excludes internally generated constraint
  325. triggers such as those that are used to implement foreign key
  326. constraints or deferrable uniqueness and exclusion constraints).
  327. Disabling or enabling internally generated constraint triggers
  328. requires superuser privileges; it should be done with caution since
  329. of course the integrity of the constraint cannot be guaranteed if the
  330. triggers are not executed.
  331. </p><p>
  332. The trigger firing mechanism is also affected by the configuration
  333. variable <a class="xref" href="runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE">session_replication_role</a>. Simply enabled
  334. triggers (the default) will fire when the replication role is <span class="quote">“<span class="quote">origin</span>”</span>
  335. (the default) or <span class="quote">“<span class="quote">local</span>”</span>. Triggers configured as <code class="literal">ENABLE
  336. REPLICA</code> will only fire if the session is in <span class="quote">“<span class="quote">replica</span>”</span>
  337. mode, and triggers configured as <code class="literal">ENABLE ALWAYS</code> will
  338. fire regardless of the current replication role.
  339. </p><p>
  340. The effect of this mechanism is that in the default configuration,
  341. triggers do not fire on replicas. This is useful because if a trigger
  342. is used on the origin to propagate data between tables, then the
  343. replication system will also replicate the propagated data, and the
  344. trigger should not fire a second time on the replica, because that would
  345. lead to duplication. However, if a trigger is used for another purpose
  346. such as creating external alerts, then it might be appropriate to set it
  347. to <code class="literal">ENABLE ALWAYS</code> so that it is also fired on
  348. replicas.
  349. </p><p>
  350. This command acquires a <code class="literal">SHARE ROW EXCLUSIVE</code> lock.
  351. </p></dd><dt><span class="term"><code class="literal">DISABLE</code>/<code class="literal">ENABLE [ REPLICA | ALWAYS ] RULE</code></span></dt><dd><p>
  352. These forms configure the firing of rewrite rules belonging to the table.
  353. A disabled rule is still known to the system, but is not applied
  354. during query rewriting. The semantics are as for disabled/enabled
  355. triggers. This configuration is ignored for <code class="literal">ON SELECT</code> rules, which
  356. are always applied in order to keep views working even if the current
  357. session is in a non-default replication role.
  358. </p><p>
  359. The rule firing mechanism is also affected by the configuration variable
  360. <a class="xref" href="runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE">session_replication_role</a>, analogous to triggers as
  361. described above.
  362. </p></dd><dt><span class="term"><code class="literal">DISABLE</code>/<code class="literal">ENABLE ROW LEVEL SECURITY</code></span></dt><dd><p>
  363. These forms control the application of row security policies belonging
  364. to the table. If enabled and no policies exist for the table, then a
  365. default-deny policy is applied. Note that policies can exist for a table
  366. even if row level security is disabled - in this case, the policies will
  367. NOT be applied and the policies will be ignored.
  368. See also
  369. <a class="xref" href="sql-createpolicy.html" title="CREATE POLICY"><span class="refentrytitle">CREATE POLICY</span></a>.
  370. </p></dd><dt><span class="term"><code class="literal">NO FORCE</code>/<code class="literal">FORCE ROW LEVEL SECURITY</code></span></dt><dd><p>
  371. These forms control the application of row security policies belonging
  372. to the table when the user is the table owner. If enabled, row level
  373. security policies will be applied when the user is the table owner. If
  374. disabled (the default) then row level security will not be applied when
  375. the user is the table owner.
  376. See also
  377. <a class="xref" href="sql-createpolicy.html" title="CREATE POLICY"><span class="refentrytitle">CREATE POLICY</span></a>.
  378. </p></dd><dt><span class="term"><code class="literal">CLUSTER ON</code></span></dt><dd><p>
  379. This form selects the default index for future
  380. <a class="xref" href="sql-cluster.html" title="CLUSTER"><span class="refentrytitle">CLUSTER</span></a>
  381. operations. It does not actually re-cluster the table.
  382. </p><p>
  383. Changing cluster options acquires a <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.
  384. </p></dd><dt><span class="term"><code class="literal">SET WITHOUT CLUSTER</code></span></dt><dd><p>
  385. This form removes the most recently used
  386. <a class="xref" href="sql-cluster.html" title="CLUSTER"><span class="refentrytitle">CLUSTER</span></a>
  387. index specification from the table. This affects
  388. future cluster operations that don't specify an index.
  389. </p><p>
  390. Changing cluster options acquires a <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock.
  391. </p></dd><dt><span class="term"><code class="literal">SET WITHOUT OIDS</code></span></dt><dd><p>
  392. Backward-compatible syntax for removing the <code class="literal">oid</code>
  393. system column. As <code class="literal">oid</code> system columns cannot be
  394. added anymore, this never has an effect.
  395. </p></dd><dt><span class="term"><code class="literal">SET TABLESPACE</code></span></dt><dd><p>
  396. This form changes the table's tablespace to the specified tablespace and
  397. moves the data file(s) associated with the table to the new tablespace.
  398. Indexes on the table, if any, are not moved; but they can be moved
  399. separately with additional <code class="literal">SET TABLESPACE</code> commands.
  400. When applied to a partitioned table, nothing is moved, but any
  401. partitions created afterwards with
  402. <code class="command">CREATE TABLE PARTITION OF</code> will use that tablespace,
  403. unless the <code class="literal">TABLESPACE</code> clause is used to override it.
  404. </p><p>
  405. All tables in the current database in a tablespace can be moved by using
  406. the <code class="literal">ALL IN TABLESPACE</code> form, which will lock all tables
  407. to be moved first and then move each one. This form also supports
  408. <code class="literal">OWNED BY</code>, which will only move tables owned by the
  409. roles specified. If the <code class="literal">NOWAIT</code> option is specified
  410. then the command will fail if it is unable to acquire all of the locks
  411. required immediately. Note that system catalogs are not moved by this
  412. command; use <code class="command">ALTER DATABASE</code> or explicit
  413. <code class="command">ALTER TABLE</code> invocations instead if desired. The
  414. <code class="literal">information_schema</code> relations are not considered part
  415. of the system catalogs and will be moved.
  416. See also
  417. <a class="xref" href="sql-createtablespace.html" title="CREATE TABLESPACE"><span class="refentrytitle">CREATE TABLESPACE</span></a>.
  418. </p></dd><dt><span class="term"><code class="literal">SET { LOGGED | UNLOGGED }</code></span></dt><dd><p>
  419. This form changes the table from unlogged to logged or vice-versa
  420. (see <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-UNLOGGED"><code class="literal">UNLOGGED</code></a>). It cannot be applied
  421. to a temporary table.
  422. </p></dd><dt><span class="term"><code class="literal">SET ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] )</code></span></dt><dd><p>
  423. This form changes one or more storage parameters for the table. See
  424. <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS" title="Storage Parameters">Storage Parameters</a>
  425. for details on the available parameters. Note that the table contents
  426. will not be modified immediately by this command; depending on the
  427. parameter you might need to rewrite the table to get the desired effects.
  428. That can be done with <a class="link" href="sql-vacuum.html" title="VACUUM">VACUUM
  429. FULL</a>, <a class="xref" href="sql-cluster.html" title="CLUSTER"><span class="refentrytitle">CLUSTER</span></a> or one of the forms
  430. of <code class="command">ALTER TABLE</code> that forces a table rewrite.
  431. For planner related parameters, changes will take effect from the next
  432. time the table is locked so currently executing queries will not be
  433. affected.
  434. </p><p>
  435. <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock will be taken for
  436. fillfactor, toast and autovacuum storage parameters, as well as the
  437. planner parameter <code class="varname">parallel_workers</code>.
  438. </p></dd><dt><span class="term"><code class="literal">RESET ( <em class="replaceable"><code>storage_parameter</code></em> [, ... ] )</code></span></dt><dd><p>
  439. This form resets one or more storage parameters to their
  440. defaults. As with <code class="literal">SET</code>, a table rewrite might be
  441. needed to update the table entirely.
  442. </p></dd><dt><span class="term"><code class="literal">INHERIT <em class="replaceable"><code>parent_table</code></em></code></span></dt><dd><p>
  443. This form adds the target table as a new child of the specified parent
  444. table. Subsequently, queries against the parent will include records
  445. of the target table. To be added as a child, the target table must
  446. already contain all the same columns as the parent (it could have
  447. additional columns, too). The columns must have matching data types,
  448. and if they have <code class="literal">NOT NULL</code> constraints in the parent
  449. then they must also have <code class="literal">NOT NULL</code> constraints in the
  450. child.
  451. </p><p>
  452. There must also be matching child-table constraints for all
  453. <code class="literal">CHECK</code> constraints of the parent, except those
  454. marked non-inheritable (that is, created with <code class="literal">ALTER TABLE ... ADD CONSTRAINT ... NO INHERIT</code>)
  455. in the parent, which are ignored; all child-table constraints matched
  456. must not be marked non-inheritable.
  457. Currently
  458. <code class="literal">UNIQUE</code>, <code class="literal">PRIMARY KEY</code>, and
  459. <code class="literal">FOREIGN KEY</code> constraints are not considered, but
  460. this might change in the future.
  461. </p></dd><dt><span class="term"><code class="literal">NO INHERIT <em class="replaceable"><code>parent_table</code></em></code></span></dt><dd><p>
  462. This form removes the target table from the list of children of the
  463. specified parent table.
  464. Queries against the parent table will no longer include records drawn
  465. from the target table.
  466. </p></dd><dt><span class="term"><code class="literal">OF <em class="replaceable"><code>type_name</code></em></code></span></dt><dd><p>
  467. This form links the table to a composite type as though <code class="command">CREATE
  468. TABLE OF</code> had formed it. The table's list of column names and types
  469. must precisely match that of the composite type. The table must
  470. not inherit from any other table. These restrictions ensure
  471. that <code class="command">CREATE TABLE OF</code> would permit an equivalent table
  472. definition.
  473. </p></dd><dt><span class="term"><code class="literal">NOT OF</code></span></dt><dd><p>
  474. This form dissociates a typed table from its type.
  475. </p></dd><dt><span class="term"><code class="literal">OWNER TO</code></span></dt><dd><p>
  476. This form changes the owner of the table, sequence, view, materialized view,
  477. or foreign table to the specified user.
  478. </p></dd><dt id="SQL-CREATETABLE-REPLICA-IDENTITY"><span class="term"><code class="literal">REPLICA IDENTITY</code></span></dt><dd><p>
  479. This form changes the information which is written to the write-ahead log
  480. to identify rows which are updated or deleted. This option has no effect
  481. except when logical replication is in use. <code class="literal">DEFAULT</code>
  482. (the default for non-system tables) records the
  483. old values of the columns of the primary key, if any. <code class="literal">USING INDEX</code>
  484. records the old values of the columns covered by the named index, which
  485. must be unique, not partial, not deferrable, and include only columns marked
  486. <code class="literal">NOT NULL</code>. <code class="literal">FULL</code> records the old values of all columns
  487. in the row. <code class="literal">NOTHING</code> records no information about the old row.
  488. (This is the default for system tables.)
  489. In all cases, no old values are logged unless at least one of the columns
  490. that would be logged differs between the old and new versions of the row.
  491. </p></dd><dt><span class="term"><code class="literal">RENAME</code></span></dt><dd><p>
  492. The <code class="literal">RENAME</code> forms change the name of a table
  493. (or an index, sequence, view, materialized view, or foreign table), the
  494. name of an individual column in a table, or the name of a constraint of
  495. the table. When renaming a constraint that has an underlying index,
  496. the index is renamed as well.
  497. There is no effect on the stored data.
  498. </p></dd><dt><span class="term"><code class="literal">SET SCHEMA</code></span></dt><dd><p>
  499. This form moves the table into another schema. Associated indexes,
  500. constraints, and sequences owned by table columns are moved as well.
  501. </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></dt><dd><p>
  502. This form attaches an existing table (which might itself be partitioned)
  503. as a partition of the target table. The table can be attached
  504. as a partition for specific values using <code class="literal">FOR VALUES</code>
  505. or as a default partition by using <code class="literal">DEFAULT</code>.
  506. For each index in the target table, a corresponding
  507. one will be created in the attached table; or, if an equivalent
  508. index already exists, it will be attached to the target table's index,
  509. as if <code class="command">ALTER INDEX ATTACH PARTITION</code> had been executed.
  510. Note that if the existing table is a foreign table, it is currently not
  511. allowed to attach the table as a partition of the target table if there
  512. are <code class="literal">UNIQUE</code> indexes on the target table. (See also
  513. <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a>.) For each user-defined
  514. row-level trigger that exists in the target table, a corresponding one
  515. is created in the attached table.
  516. </p><p>
  517. A partition using <code class="literal">FOR VALUES</code> uses same syntax for
  518. <em class="replaceable"><code>partition_bound_spec</code></em> as
  519. <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a>. The partition bound specification
  520. must correspond to the partitioning strategy and partition key of the
  521. target table. The table to be attached must have all the same columns
  522. as the target table and no more; moreover, the column types must also
  523. match. Also, it must have all the <code class="literal">NOT NULL</code> and
  524. <code class="literal">CHECK</code> constraints of the target table. Currently
  525. <code class="literal">FOREIGN KEY</code> constraints are not considered.
  526. <code class="literal">UNIQUE</code> and <code class="literal">PRIMARY KEY</code> constraints
  527. from the parent table will be created in the partition, if they don't
  528. already exist.
  529. If any of the <code class="literal">CHECK</code> constraints of the table being
  530. attached is marked <code class="literal">NO INHERIT</code>, the command will fail;
  531. such constraints must be recreated without the
  532. <code class="literal">NO INHERIT</code> clause.
  533. </p><p>
  534. If the new partition is a regular table, a full table scan is performed
  535. to check that existing rows in the table do not violate the partition
  536. constraint. It is possible to avoid this scan by adding a valid
  537. <code class="literal">CHECK</code> constraint to the table that allows only
  538. rows satisfying the desired partition constraint before running this
  539. command. The <code class="literal">CHECK</code> constraint will be used to
  540. determine that the table need not be scanned to validate the partition
  541. constraint. This does not work, however, if any of the partition keys
  542. is an expression and the partition does not accept
  543. <code class="literal">NULL</code> values. If attaching a list partition that will
  544. not accept <code class="literal">NULL</code> values, also add
  545. <code class="literal">NOT NULL</code> constraint to the partition key column,
  546. unless it's an expression.
  547. </p><p>
  548. If the new partition is a foreign table, nothing is done to verify
  549. that all the rows in the foreign table obey the partition constraint.
  550. (See the discussion in <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a> about
  551. constraints on the foreign table.)
  552. </p><p>
  553. When a table has a default partition, defining a new partition changes
  554. the partition constraint for the default partition. The default
  555. partition can't contain any rows that would need to be moved to the new
  556. partition, and will be scanned to verify that none are present. This
  557. scan, like the scan of the new partition, can be avoided if an
  558. appropriate <code class="literal">CHECK</code> constraint is present. Also like
  559. the scan of the new partition, it is always skipped when the default
  560. partition is a foreign table.
  561. </p><p>
  562. Attaching a partition acquires a
  563. <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock on the parent table,
  564. in addition to <code class="literal">ACCESS EXCLUSIVE</code> locks on the table
  565. to be attached and on the default partition (if any).
  566. </p></dd><dt><span class="term"><code class="literal">DETACH PARTITION</code> <em class="replaceable"><code>partition_name</code></em></span></dt><dd><p>
  567. This form detaches the specified partition of the target table. The detached
  568. partition continues to exist as a standalone table, but no longer has any
  569. ties to the table from which it was detached. Any indexes that were
  570. attached to the target table's indexes are detached. Any triggers that
  571. were created as clones of those in the target table are removed.
  572. </p></dd></dl></div><p>
  573. </p><p>
  574. All the forms of ALTER TABLE that act on a single table, except
  575. <code class="literal">RENAME</code>, <code class="literal">SET SCHEMA</code>,
  576. <code class="literal">ATTACH PARTITION</code>, and
  577. <code class="literal">DETACH PARTITION</code> can be combined into
  578. a list of multiple alterations to be applied together. For example, it
  579. is possible to add several columns and/or alter the type of several
  580. columns in a single command. This is particularly useful with large
  581. tables, since only one pass over the table need be made.
  582. </p><p>
  583. You must own the table to use <code class="command">ALTER TABLE</code>.
  584. To change the schema or tablespace of a table, you must also have
  585. <code class="literal">CREATE</code> privilege on the new schema or tablespace.
  586. To add the table as a new child of a parent table, you must own the parent
  587. table as well. Also, to attach a table as a new partition of the table,
  588. you must own the table being attached.
  589. To alter the owner, you must also be a direct or indirect member of the new
  590. owning role, and that role must have <code class="literal">CREATE</code> privilege on
  591. the table's schema. (These restrictions enforce that altering the owner
  592. doesn't do anything you couldn't do by dropping and recreating the table.
  593. However, a superuser can alter ownership of any table anyway.)
  594. To add a column or alter a column type or use the <code class="literal">OF</code>
  595. clause, you must also have <code class="literal">USAGE</code> privilege on the data
  596. type.
  597. </p></div><div class="refsect1" id="id-1.9.3.35.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">IF EXISTS</code></span></dt><dd><p>
  598. Do not throw an error if the table does not exist. A notice is issued
  599. in this case.
  600. </p></dd><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  601. The name (optionally schema-qualified) of an existing table to
  602. alter. If <code class="literal">ONLY</code> is specified before the table name, only
  603. that table is altered. If <code class="literal">ONLY</code> is not specified, the table
  604. and all its descendant tables (if any) are altered. Optionally,
  605. <code class="literal">*</code> can be specified after the table name to explicitly
  606. indicate that descendant tables are included.
  607. </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>
  608. Name of a new or existing column.
  609. </p></dd><dt><span class="term"><em class="replaceable"><code>new_column_name</code></em></span></dt><dd><p>
  610. New name for an existing column.
  611. </p></dd><dt><span class="term"><em class="replaceable"><code>new_name</code></em></span></dt><dd><p>
  612. New name for the table.
  613. </p></dd><dt><span class="term"><em class="replaceable"><code>data_type</code></em></span></dt><dd><p>
  614. Data type of the new column, or new data type for an existing
  615. column.
  616. </p></dd><dt><span class="term"><em class="replaceable"><code>table_constraint</code></em></span></dt><dd><p>
  617. New table constraint for the table.
  618. </p></dd><dt><span class="term"><em class="replaceable"><code>constraint_name</code></em></span></dt><dd><p>
  619. Name of a new or existing constraint.
  620. </p></dd><dt><span class="term"><code class="literal">CASCADE</code></span></dt><dd><p>
  621. Automatically drop objects that depend on the dropped column
  622. or constraint (for example, views referencing the column),
  623. and in turn all objects that depend on those objects
  624. (see <a class="xref" href="ddl-depend.html" title="5.14. Dependency Tracking">Section 5.14</a>).
  625. </p></dd><dt><span class="term"><code class="literal">RESTRICT</code></span></dt><dd><p>
  626. Refuse to drop the column or constraint if there are any dependent
  627. objects. This is the default behavior.
  628. </p></dd><dt><span class="term"><em class="replaceable"><code>trigger_name</code></em></span></dt><dd><p>
  629. Name of a single trigger to disable or enable.
  630. </p></dd><dt><span class="term"><code class="literal">ALL</code></span></dt><dd><p>
  631. Disable or enable all triggers belonging to the table.
  632. (This requires superuser privilege if any of the triggers are
  633. internally generated constraint triggers such as those that are used
  634. to implement foreign key constraints or deferrable uniqueness and
  635. exclusion constraints.)
  636. </p></dd><dt><span class="term"><code class="literal">USER</code></span></dt><dd><p>
  637. Disable or enable all triggers belonging to the table except for
  638. internally generated constraint triggers such as those that are used
  639. to implement foreign key constraints or deferrable uniqueness and
  640. exclusion constraints.
  641. </p></dd><dt><span class="term"><em class="replaceable"><code>index_name</code></em></span></dt><dd><p>
  642. The name of an existing index.
  643. </p></dd><dt><span class="term"><em class="replaceable"><code>storage_parameter</code></em></span></dt><dd><p>
  644. The name of a table storage parameter.
  645. </p></dd><dt><span class="term"><em class="replaceable"><code>value</code></em></span></dt><dd><p>
  646. The new value for a table storage parameter.
  647. This might be a number or a word depending on the parameter.
  648. </p></dd><dt><span class="term"><em class="replaceable"><code>parent_table</code></em></span></dt><dd><p>
  649. A parent table to associate or de-associate with this table.
  650. </p></dd><dt><span class="term"><em class="replaceable"><code>new_owner</code></em></span></dt><dd><p>
  651. The user name of the new owner of the table.
  652. </p></dd><dt><span class="term"><em class="replaceable"><code>new_tablespace</code></em></span></dt><dd><p>
  653. The name of the tablespace to which the table will be moved.
  654. </p></dd><dt><span class="term"><em class="replaceable"><code>new_schema</code></em></span></dt><dd><p>
  655. The name of the schema to which the table will be moved.
  656. </p></dd><dt><span class="term"><em class="replaceable"><code>partition_name</code></em></span></dt><dd><p>
  657. The name of the table to attach as a new partition or to detach from this table.
  658. </p></dd><dt><span class="term"><em class="replaceable"><code>partition_bound_spec</code></em></span></dt><dd><p>
  659. The partition bound specification for a new partition. Refer to
  660. <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.
  661. </p></dd></dl></div></div><div class="refsect1" id="SQL-ALTERTABLE-NOTES"><h2>Notes</h2><p>
  662. The key word <code class="literal">COLUMN</code> is noise and can be omitted.
  663. </p><p>
  664. When a column is added with <code class="literal">ADD COLUMN</code> and a
  665. non-volatile <code class="literal">DEFAULT</code> is specified, the default is
  666. evaluated at the time of the statement and the result stored in the
  667. table's metadata. That value will be used for the column for all existing
  668. rows. If no <code class="literal">DEFAULT</code> is specified, NULL is used. In
  669. neither case is a rewrite of the table required.
  670. </p><p>
  671. Adding a column with a volatile <code class="literal">DEFAULT</code> or
  672. changing the type of an existing column will require the entire table and
  673. its indexes to be rewritten. As an exception, when changing the type of an
  674. existing column, if the <code class="literal">USING</code> clause does not change
  675. the column contents and the old type is either binary coercible to the new
  676. type or an unconstrained domain over the new type, a table rewrite is not
  677. needed; but any indexes on the affected columns must still be rebuilt.
  678. Table and/or index rebuilds may take a
  679. significant amount of time for a large table; and will temporarily require
  680. as much as double the disk space.
  681. </p><p>
  682. Adding a <code class="literal">CHECK</code> or <code class="literal">NOT NULL</code> constraint requires
  683. scanning the table to verify that existing rows meet the constraint,
  684. but does not require a table rewrite.
  685. </p><p>
  686. Similarly, when attaching a new partition it may be scanned to verify that
  687. existing rows meet the partition constraint.
  688. </p><p>
  689. The main reason for providing the option to specify multiple changes
  690. in a single <code class="command">ALTER TABLE</code> is that multiple table scans or
  691. rewrites can thereby be combined into a single pass over the table.
  692. </p><p>
  693. Scanning a large table to verify a new foreign key or check constraint
  694. can take a long time, and other updates to the table are locked out
  695. until the <code class="command">ALTER TABLE ADD CONSTRAINT</code> command is
  696. committed. The main purpose of the <code class="literal">NOT VALID</code>
  697. constraint option is to reduce the impact of adding a constraint on
  698. concurrent updates. With <code class="literal">NOT VALID</code>,
  699. the <code class="command">ADD CONSTRAINT</code> command does not scan the table
  700. and can be committed immediately. After that, a <code class="literal">VALIDATE
  701. CONSTRAINT</code> command can be issued to verify that existing rows
  702. satisfy the constraint. The validation step does not need to lock out
  703. concurrent updates, since it knows that other transactions will be
  704. enforcing the constraint for rows that they insert or update; only
  705. pre-existing rows need to be checked. Hence, validation acquires only
  706. a <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock on the table being
  707. altered. (If the constraint is a foreign key then a <code class="literal">ROW
  708. SHARE</code> lock is also required on the table referenced by the
  709. constraint.) In addition to improving concurrency, it can be useful to
  710. use <code class="literal">NOT VALID</code> and <code class="literal">VALIDATE
  711. CONSTRAINT</code> in cases where the table is known to contain
  712. pre-existing violations. Once the constraint is in place, no new
  713. violations can be inserted, and the existing problems can be corrected
  714. at leisure until <code class="literal">VALIDATE CONSTRAINT</code> finally
  715. succeeds.
  716. </p><p>
  717. The <code class="literal">DROP COLUMN</code> form does not physically remove
  718. the column, but simply makes it invisible to SQL operations. Subsequent
  719. insert and update operations in the table will store a null value for the
  720. column. Thus, dropping a column is quick but it will not immediately
  721. reduce the on-disk size of your table, as the space occupied
  722. by the dropped column is not reclaimed. The space will be
  723. reclaimed over time as existing rows are updated.
  724. </p><p>
  725. To force immediate reclamation of space occupied by a dropped column,
  726. you can execute one of the forms of <code class="command">ALTER TABLE</code> that
  727. performs a rewrite of the whole table. This results in reconstructing
  728. each row with the dropped column replaced by a null value.
  729. </p><p>
  730. The rewriting forms of <code class="command">ALTER TABLE</code> are not MVCC-safe.
  731. After a table rewrite, the table will appear empty to concurrent
  732. transactions, if they are using a snapshot taken before the rewrite
  733. occurred. See <a class="xref" href="mvcc-caveats.html" title="13.5. Caveats">Section 13.5</a> for more details.
  734. </p><p>
  735. The <code class="literal">USING</code> option of <code class="literal">SET DATA TYPE</code> can actually
  736. specify any expression involving the old values of the row; that is, it
  737. can refer to other columns as well as the one being converted. This allows
  738. very general conversions to be done with the <code class="literal">SET DATA TYPE</code>
  739. syntax. Because of this flexibility, the <code class="literal">USING</code>
  740. expression is not applied to the column's default value (if any); the
  741. result might not be a constant expression as required for a default.
  742. This means that when there is no implicit or assignment cast from old to
  743. new type, <code class="literal">SET DATA TYPE</code> might fail to convert the default even
  744. though a <code class="literal">USING</code> clause is supplied. In such cases,
  745. drop the default with <code class="literal">DROP DEFAULT</code>, perform the <code class="literal">ALTER
  746. TYPE</code>, and then use <code class="literal">SET DEFAULT</code> to add a suitable new
  747. default. Similar considerations apply to indexes and constraints involving
  748. the column.
  749. </p><p>
  750. If a table has any descendant tables, it is not permitted to add,
  751. rename, or change the type of a column in the parent table without doing
  752. the same to the descendants. This ensures that the descendants always
  753. have columns matching the parent. Similarly, a <code class="literal">CHECK</code>
  754. constraint cannot be renamed in the parent without also renaming it in
  755. all descendants, so that <code class="literal">CHECK</code> constraints also match
  756. between the parent and its descendants. (That restriction does not apply
  757. to index-based constraints, however.)
  758. Also, because selecting from the parent also selects from its descendants,
  759. a constraint on the parent cannot be marked valid unless it is also marked
  760. valid for those descendants. In all of these cases, <code class="command">ALTER TABLE
  761. ONLY</code> will be rejected.
  762. </p><p>
  763. A recursive <code class="literal">DROP COLUMN</code> operation will remove a
  764. descendant table's column only if the descendant does not inherit
  765. that column from any other parents and never had an independent
  766. definition of the column. A nonrecursive <code class="literal">DROP
  767. COLUMN</code> (i.e., <code class="command">ALTER TABLE ONLY ... DROP
  768. COLUMN</code>) never removes any descendant columns, but
  769. instead marks them as independently defined rather than inherited.
  770. A nonrecursive <code class="literal">DROP COLUMN</code> command will fail for a
  771. partitioned table, because all partitions of a table must have the same
  772. columns as the partitioning root.
  773. </p><p>
  774. The actions for identity columns (<code class="literal">ADD
  775. GENERATED</code>, <code class="literal">SET</code> etc., <code class="literal">DROP
  776. IDENTITY</code>), as well as the actions
  777. <code class="literal">TRIGGER</code>, <code class="literal">CLUSTER</code>, <code class="literal">OWNER</code>,
  778. and <code class="literal">TABLESPACE</code> never recurse to descendant tables;
  779. that is, they always act as though <code class="literal">ONLY</code> were specified.
  780. Adding a constraint recurses only for <code class="literal">CHECK</code> constraints
  781. that are not marked <code class="literal">NO INHERIT</code>.
  782. </p><p>
  783. Changing any part of a system catalog table is not permitted.
  784. </p><p>
  785. 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
  786. parameters. <a class="xref" href="ddl.html" title="Chapter 5. Data Definition">Chapter 5</a> has further information on
  787. inheritance.
  788. </p></div><div class="refsect1" id="id-1.9.3.35.8"><h2>Examples</h2><p>
  789. To add a column of type <code class="type">varchar</code> to a table:
  790. </p><pre class="programlisting">
  791. ALTER TABLE distributors ADD COLUMN address varchar(30);
  792. </pre><p>
  793. </p><p>
  794. To drop a column from a table:
  795. </p><pre class="programlisting">
  796. ALTER TABLE distributors DROP COLUMN address RESTRICT;
  797. </pre><p>
  798. </p><p>
  799. To change the types of two existing columns in one operation:
  800. </p><pre class="programlisting">
  801. ALTER TABLE distributors
  802. ALTER COLUMN address TYPE varchar(80),
  803. ALTER COLUMN name TYPE varchar(100);
  804. </pre><p>
  805. </p><p>
  806. To change an integer column containing Unix timestamps to <code class="type">timestamp
  807. with time zone</code> via a <code class="literal">USING</code> clause:
  808. </p><pre class="programlisting">
  809. ALTER TABLE foo
  810. ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone
  811. USING
  812. timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';
  813. </pre><p>
  814. </p><p>
  815. The same, when the column has a default expression that won't automatically
  816. cast to the new data type:
  817. </p><pre class="programlisting">
  818. ALTER TABLE foo
  819. ALTER COLUMN foo_timestamp DROP DEFAULT,
  820. ALTER COLUMN foo_timestamp TYPE timestamp with time zone
  821. USING
  822. timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',
  823. ALTER COLUMN foo_timestamp SET DEFAULT now();
  824. </pre><p>
  825. </p><p>
  826. To rename an existing column:
  827. </p><pre class="programlisting">
  828. ALTER TABLE distributors RENAME COLUMN address TO city;
  829. </pre><p>
  830. </p><p>
  831. To rename an existing table:
  832. </p><pre class="programlisting">
  833. ALTER TABLE distributors RENAME TO suppliers;
  834. </pre><p>
  835. </p><p>
  836. To rename an existing constraint:
  837. </p><pre class="programlisting">
  838. ALTER TABLE distributors RENAME CONSTRAINT zipchk TO zip_check;
  839. </pre><p>
  840. </p><p>
  841. To add a not-null constraint to a column:
  842. </p><pre class="programlisting">
  843. ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
  844. </pre><p>
  845. To remove a not-null constraint from a column:
  846. </p><pre class="programlisting">
  847. ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
  848. </pre><p>
  849. </p><p>
  850. To add a check constraint to a table and all its children:
  851. </p><pre class="programlisting">
  852. ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
  853. </pre><p>
  854. </p><p>
  855. To add a check constraint only to a table and not to its children:
  856. </p><pre class="programlisting">
  857. ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;
  858. </pre><p>
  859. (The check constraint will not be inherited by future children, either.)
  860. </p><p>
  861. To remove a check constraint from a table and all its children:
  862. </p><pre class="programlisting">
  863. ALTER TABLE distributors DROP CONSTRAINT zipchk;
  864. </pre><p>
  865. </p><p>
  866. To remove a check constraint from one table only:
  867. </p><pre class="programlisting">
  868. ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;
  869. </pre><p>
  870. (The check constraint remains in place for any child tables.)
  871. </p><p>
  872. To add a foreign key constraint to a table:
  873. </p><pre class="programlisting">
  874. ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address);
  875. </pre><p>
  876. </p><p>
  877. To add a foreign key constraint to a table with the least impact on other work:
  878. </p><pre class="programlisting">
  879. ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) NOT VALID;
  880. ALTER TABLE distributors VALIDATE CONSTRAINT distfk;
  881. </pre><p>
  882. </p><p>
  883. To add a (multicolumn) unique constraint to a table:
  884. </p><pre class="programlisting">
  885. ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
  886. </pre><p>
  887. </p><p>
  888. To add an automatically named primary key constraint to a table, noting
  889. that a table can only ever have one primary key:
  890. </p><pre class="programlisting">
  891. ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
  892. </pre><p>
  893. </p><p>
  894. To move a table to a different tablespace:
  895. </p><pre class="programlisting">
  896. ALTER TABLE distributors SET TABLESPACE fasttablespace;
  897. </pre><p>
  898. </p><p>
  899. To move a table to a different schema:
  900. </p><pre class="programlisting">
  901. ALTER TABLE myschema.distributors SET SCHEMA yourschema;
  902. </pre><p>
  903. </p><p>
  904. To recreate a primary key constraint, without blocking updates while the
  905. index is rebuilt:
  906. </p><pre class="programlisting">
  907. CREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);
  908. ALTER TABLE distributors DROP CONSTRAINT distributors_pkey,
  909. ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;
  910. </pre><p>
  911. To attach a partition to a range-partitioned table:
  912. </p><pre class="programlisting">
  913. ALTER TABLE measurement
  914. ATTACH PARTITION measurement_y2016m07 FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');
  915. </pre><p>
  916. To attach a partition to a list-partitioned table:
  917. </p><pre class="programlisting">
  918. ALTER TABLE cities
  919. ATTACH PARTITION cities_ab FOR VALUES IN ('a', 'b');
  920. </pre><p>
  921. To attach a partition to a hash-partitioned table:
  922. </p><pre class="programlisting">
  923. ALTER TABLE orders
  924. ATTACH PARTITION orders_p4 FOR VALUES WITH (MODULUS 4, REMAINDER 3);
  925. </pre><p>
  926. To attach a default partition to a partitioned table:
  927. </p><pre class="programlisting">
  928. ALTER TABLE cities
  929. ATTACH PARTITION cities_partdef DEFAULT;
  930. </pre><p>
  931. To detach a partition from a partitioned table:
  932. </p><pre class="programlisting">
  933. ALTER TABLE measurement
  934. DETACH PARTITION measurement_y2015m12;
  935. </pre></div><div class="refsect1" id="id-1.9.3.35.9"><h2>Compatibility</h2><p>
  936. The forms <code class="literal">ADD</code> (without <code class="literal">USING INDEX</code>),
  937. <code class="literal">DROP [COLUMN]</code>, <code class="literal">DROP IDENTITY</code>, <code class="literal">RESTART</code>,
  938. <code class="literal">SET DEFAULT</code>, <code class="literal">SET DATA TYPE</code> (without <code class="literal">USING</code>),
  939. <code class="literal">SET GENERATED</code>, and <code class="literal">SET <em class="replaceable"><code>sequence_option</code></em></code>
  940. conform with the SQL standard. The other forms are
  941. <span class="productname">PostgreSQL</span> extensions of the SQL standard.
  942. Also, the ability to specify more than one manipulation in a single
  943. <code class="command">ALTER TABLE</code> command is an extension.
  944. </p><p>
  945. <code class="command">ALTER TABLE DROP COLUMN</code> can be used to drop the only
  946. column of a table, leaving a zero-column table. This is an
  947. extension of SQL, which disallows zero-column tables.
  948. </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-altertablespace.html">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">Home</a></td><td width="40%" align="right" valign="top"> ALTER TABLESPACE</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1