gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

1384 líneas
95KB

  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>CREATE 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-createsubscription.html" title="CREATE SUBSCRIPTION" /><link rel="next" href="sql-createtableas.html" title="CREATE TABLE AS" /></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">CREATE TABLE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createsubscription.html" title="CREATE SUBSCRIPTION">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-createtableas.html" title="CREATE TABLE AS">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATETABLE"><div class="titlepage"></div><a id="id-1.9.3.85.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE TABLE</span></h2><p>CREATE TABLE — define a new table</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <em class="replaceable"><code>table_name</code></em> ( [
  4. { <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> [ ... ] ]
  5. | <em class="replaceable"><code>table_constraint</code></em>
  6. | LIKE <em class="replaceable"><code>source_table</code></em> [ <em class="replaceable"><code>like_option</code></em> ... ] }
  7. [, ... ]
  8. ] )
  9. [ INHERITS ( <em class="replaceable"><code>parent_table</code></em> [, ... ] ) ]
  10. [ PARTITION BY { RANGE | LIST | HASH } ( { <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> ] [, ... ] ) ]
  11. [ USING <em class="replaceable"><code>method</code></em> ]
  12. [ WITH ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] ) | WITHOUT OIDS ]
  13. [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
  14. [ TABLESPACE <em class="replaceable"><code>tablespace_name</code></em> ]
  15. CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <em class="replaceable"><code>table_name</code></em>
  16. OF <em class="replaceable"><code>type_name</code></em> [ (
  17. { <em class="replaceable"><code>column_name</code></em> [ WITH OPTIONS ] [ <em class="replaceable"><code>column_constraint</code></em> [ ... ] ]
  18. | <em class="replaceable"><code>table_constraint</code></em> }
  19. [, ... ]
  20. ) ]
  21. [ PARTITION BY { RANGE | LIST | HASH } ( { <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> ] [, ... ] ) ]
  22. [ USING <em class="replaceable"><code>method</code></em> ]
  23. [ WITH ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] ) | WITHOUT OIDS ]
  24. [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
  25. [ TABLESPACE <em class="replaceable"><code>tablespace_name</code></em> ]
  26. CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <em class="replaceable"><code>table_name</code></em>
  27. PARTITION OF <em class="replaceable"><code>parent_table</code></em> [ (
  28. { <em class="replaceable"><code>column_name</code></em> [ WITH OPTIONS ] [ <em class="replaceable"><code>column_constraint</code></em> [ ... ] ]
  29. | <em class="replaceable"><code>table_constraint</code></em> }
  30. [, ... ]
  31. ) ] { FOR VALUES <em class="replaceable"><code>partition_bound_spec</code></em> | DEFAULT }
  32. [ PARTITION BY { RANGE | LIST | HASH } ( { <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> ] [, ... ] ) ]
  33. [ USING <em class="replaceable"><code>method</code></em> ]
  34. [ WITH ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] ) | WITHOUT OIDS ]
  35. [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
  36. [ TABLESPACE <em class="replaceable"><code>tablespace_name</code></em> ]
  37. <span class="phrase">where <em class="replaceable"><code>column_constraint</code></em> is:</span>
  38. [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]
  39. { NOT NULL |
  40. NULL |
  41. CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ] |
  42. DEFAULT <em class="replaceable"><code>default_expr</code></em> |
  43. GENERATED ALWAYS AS ( <em class="replaceable"><code>generation_expr</code></em> ) STORED |
  44. GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <em class="replaceable"><code>sequence_options</code></em> ) ] |
  45. UNIQUE <em class="replaceable"><code>index_parameters</code></em> |
  46. PRIMARY KEY <em class="replaceable"><code>index_parameters</code></em> |
  47. REFERENCES <em class="replaceable"><code>reftable</code></em> [ ( <em class="replaceable"><code>refcolumn</code></em> ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
  48. [ ON DELETE <em class="replaceable"><code>referential_action</code></em> ] [ ON UPDATE <em class="replaceable"><code>referential_action</code></em> ] }
  49. [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
  50. <span class="phrase">and <em class="replaceable"><code>table_constraint</code></em> is:</span>
  51. [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]
  52. { CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ] |
  53. UNIQUE ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) <em class="replaceable"><code>index_parameters</code></em> |
  54. PRIMARY KEY ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) <em class="replaceable"><code>index_parameters</code></em> |
  55. 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> ) ] |
  56. 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> [, ... ] ) ]
  57. [ 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> ] }
  58. [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
  59. <span class="phrase">and <em class="replaceable"><code>like_option</code></em> is:</span>
  60. { INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }
  61. <span class="phrase">and <em class="replaceable"><code>partition_bound_spec</code></em> is:</span>
  62. IN ( <em class="replaceable"><code>partition_bound_expr</code></em> [, ...] ) |
  63. FROM ( { <em class="replaceable"><code>partition_bound_expr</code></em> | MINVALUE | MAXVALUE } [, ...] )
  64. TO ( { <em class="replaceable"><code>partition_bound_expr</code></em> | MINVALUE | MAXVALUE } [, ...] ) |
  65. WITH ( MODULUS <em class="replaceable"><code>numeric_literal</code></em>, REMAINDER <em class="replaceable"><code>numeric_literal</code></em> )
  66. <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>
  67. [ INCLUDE ( <em class="replaceable"><code>column_name</code></em> [, ... ] ) ]
  68. [ WITH ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] ) ]
  69. [ USING INDEX TABLESPACE <em class="replaceable"><code>tablespace_name</code></em> ]
  70. <span class="phrase"><em class="replaceable"><code>exclude_element</code></em> in an <code class="literal">EXCLUDE</code> constraint is:</span>
  71. { <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 } ]
  72. </pre></div><div class="refsect1" id="SQL-CREATETABLE-DESCRIPTION"><h2>Description</h2><p>
  73. <code class="command">CREATE TABLE</code> will create a new, initially empty table
  74. in the current database. The table will be owned by the user issuing the
  75. command.
  76. </p><p>
  77. If a schema name is given (for example, <code class="literal">CREATE TABLE
  78. myschema.mytable ...</code>) then the table is created in the specified
  79. schema. Otherwise it is created in the current schema. Temporary
  80. tables exist in a special schema, so a schema name cannot be given
  81. when creating a temporary table. The name of the table must be
  82. distinct from the name of any other table, sequence, index, view,
  83. or foreign table in the same schema.
  84. </p><p>
  85. <code class="command">CREATE TABLE</code> also automatically creates a data
  86. type that represents the composite type corresponding
  87. to one row of the table. Therefore, tables cannot have the same
  88. name as any existing data type in the same schema.
  89. </p><p>
  90. The optional constraint clauses specify constraints (tests) that
  91. new or updated rows must satisfy for an insert or update operation
  92. to succeed. A constraint is an SQL object that helps define the
  93. set of valid values in the table in various ways.
  94. </p><p>
  95. There are two ways to define constraints: table constraints and
  96. column constraints. A column constraint is defined as part of a
  97. column definition. A table constraint definition is not tied to a
  98. particular column, and it can encompass more than one column.
  99. Every column constraint can also be written as a table constraint;
  100. a column constraint is only a notational convenience for use when the
  101. constraint only affects one column.
  102. </p><p>
  103. To be able to create a table, you must have <code class="literal">USAGE</code>
  104. privilege on all column types or the type in the <code class="literal">OF</code>
  105. clause, respectively.
  106. </p></div><div class="refsect1" id="id-1.9.3.85.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt id="SQL-CREATETABLE-TEMPORARY"><span class="term"><code class="literal">TEMPORARY</code> or <code class="literal">TEMP</code></span></dt><dd><p>
  107. If specified, the table is created as a temporary table.
  108. Temporary tables are automatically dropped at the end of a
  109. session, or optionally at the end of the current transaction
  110. (see <code class="literal">ON COMMIT</code> below). Existing permanent
  111. tables with the same name are not visible to the current session
  112. while the temporary table exists, unless they are referenced
  113. with schema-qualified names. Any indexes created on a temporary
  114. table are automatically temporary as well.
  115. </p><p>
  116. The <a class="link" href="routine-vacuuming.html#AUTOVACUUM" title="24.1.6. The Autovacuum Daemon">autovacuum daemon</a> cannot
  117. access and therefore cannot vacuum or analyze temporary tables.
  118. For this reason, appropriate vacuum and analyze operations should be
  119. performed via session SQL commands. For example, if a temporary
  120. table is going to be used in complex queries, it is wise to run
  121. <code class="command">ANALYZE</code> on the temporary table after it is populated.
  122. </p><p>
  123. Optionally, <code class="literal">GLOBAL</code> or <code class="literal">LOCAL</code>
  124. can be written before <code class="literal">TEMPORARY</code> or <code class="literal">TEMP</code>.
  125. This presently makes no difference in <span class="productname">PostgreSQL</span>
  126. and is deprecated; see
  127. <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-COMPATIBILITY" title="Compatibility">Compatibility</a>.
  128. </p></dd><dt id="SQL-CREATETABLE-UNLOGGED"><span class="term"><code class="literal">UNLOGGED</code></span></dt><dd><p>
  129. If specified, the table is created as an unlogged table. Data written
  130. to unlogged tables is not written to the write-ahead log (see <a class="xref" href="wal.html" title="Chapter 29. Reliability and the Write-Ahead Log">Chapter 29</a>), which makes them considerably faster than ordinary
  131. tables. However, they are not crash-safe: an unlogged table is
  132. automatically truncated after a crash or unclean shutdown. The contents
  133. of an unlogged table are also not replicated to standby servers.
  134. Any indexes created on an unlogged table are automatically unlogged as
  135. well.
  136. </p></dd><dt><span class="term"><code class="literal">IF NOT EXISTS</code></span></dt><dd><p>
  137. Do not throw an error if a relation with the same name already exists.
  138. A notice is issued in this case. Note that there is no guarantee that
  139. the existing relation is anything like the one that would have been
  140. created.
  141. </p></dd><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
  142. The name (optionally schema-qualified) of the table to be created.
  143. </p></dd><dt><span class="term"><code class="literal">OF <em class="replaceable"><code>type_name</code></em></code></span></dt><dd><p>
  144. Creates a <em class="firstterm">typed table</em>, which takes its
  145. structure from the specified composite type (name optionally
  146. schema-qualified). A typed table is tied to its type; for
  147. example the table will be dropped if the type is dropped
  148. (with <code class="literal">DROP TYPE ... CASCADE</code>).
  149. </p><p>
  150. When a typed table is created, then the data types of the
  151. columns are determined by the underlying composite type and are
  152. not specified by the <code class="literal">CREATE TABLE</code> command.
  153. But the <code class="literal">CREATE TABLE</code> command can add defaults
  154. and constraints to the table and can specify storage parameters.
  155. </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>
  156. The name of a column to be created in the new table.
  157. </p></dd><dt><span class="term"><em class="replaceable"><code>data_type</code></em></span></dt><dd><p>
  158. The data type of the column. This can include array
  159. specifiers. For more information on the data types supported by
  160. <span class="productname">PostgreSQL</span>, refer to <a class="xref" href="datatype.html" title="Chapter 8. Data Types">Chapter 8</a>.
  161. </p></dd><dt><span class="term"><code class="literal">COLLATE <em class="replaceable"><code>collation</code></em></code></span></dt><dd><p>
  162. The <code class="literal">COLLATE</code> clause assigns a collation to
  163. the column (which must be of a collatable data type).
  164. If not specified, the column data type's default collation is used.
  165. </p></dd><dt><span class="term"><code class="literal">INHERITS ( <em class="replaceable"><code>parent_table</code></em> [, ... ] )</code></span></dt><dd><p>
  166. The optional <code class="literal">INHERITS</code> clause specifies a list of
  167. tables from which the new table automatically inherits all
  168. columns. Parent tables can be plain tables or foreign tables.
  169. </p><p>
  170. Use of <code class="literal">INHERITS</code> creates a persistent relationship
  171. between the new child table and its parent table(s). Schema
  172. modifications to the parent(s) normally propagate to children
  173. as well, and by default the data of the child table is included in
  174. scans of the parent(s).
  175. </p><p>
  176. If the same column name exists in more than one parent
  177. table, an error is reported unless the data types of the columns
  178. match in each of the parent tables. If there is no conflict,
  179. then the duplicate columns are merged to form a single column in
  180. the new table. If the column name list of the new table
  181. contains a column name that is also inherited, the data type must
  182. likewise match the inherited column(s), and the column
  183. definitions are merged into one. If the
  184. new table explicitly specifies a default value for the column,
  185. this default overrides any defaults from inherited declarations
  186. of the column. Otherwise, any parents that specify default
  187. values for the column must all specify the same default, or an
  188. error will be reported.
  189. </p><p>
  190. <code class="literal">CHECK</code> constraints are merged in essentially the same way as
  191. columns: if multiple parent tables and/or the new table definition
  192. contain identically-named <code class="literal">CHECK</code> constraints, these
  193. constraints must all have the same check expression, or an error will be
  194. reported. Constraints having the same name and expression will
  195. be merged into one copy. A constraint marked <code class="literal">NO INHERIT</code> in a
  196. parent will not be considered. Notice that an unnamed <code class="literal">CHECK</code>
  197. constraint in the new table will never be merged, since a unique name
  198. will always be chosen for it.
  199. </p><p>
  200. Column <code class="literal">STORAGE</code> settings are also copied from parent tables.
  201. </p><p>
  202. If a column in the parent table is an identity column, that property is
  203. not inherited. A column in the child table can be declared identity
  204. column if desired.
  205. </p></dd><dt><span class="term"><code class="literal">PARTITION BY { RANGE | LIST | HASH } ( { <em class="replaceable"><code>column_name</code></em> | ( <em class="replaceable"><code>expression</code></em> ) } [ <em class="replaceable"><code>opclass</code></em> ] [, ...] ) </code></span></dt><dd><p>
  206. The optional <code class="literal">PARTITION BY</code> clause specifies a strategy
  207. of partitioning the table. The table thus created is called a
  208. <em class="firstterm">partitioned</em> table. The parenthesized list of
  209. columns or expressions forms the <em class="firstterm">partition key</em>
  210. for the table. When using range or hash partitioning, the partition key
  211. can include multiple columns or expressions (up to 32, but this limit can
  212. be altered when building <span class="productname">PostgreSQL</span>), but for
  213. list partitioning, the partition key must consist of a single column or
  214. expression.
  215. </p><p>
  216. Range and list partitioning require a btree operator class, while hash
  217. partitioning requires a hash operator class. If no operator class is
  218. specified explicitly, the default operator class of the appropriate
  219. type will be used; if no default operator class exists, an error will
  220. be raised. When hash partitioning is used, the operator class used
  221. must implement support function 2 (see <a class="xref" href="xindex.html#XINDEX-SUPPORT" title="37.16.3. Index Method Support Routines">Section 37.16.3</a>
  222. for details).
  223. </p><p>
  224. A partitioned table is divided into sub-tables (called partitions),
  225. which are created using separate <code class="literal">CREATE TABLE</code> commands.
  226. The partitioned table is itself empty. A data row inserted into the
  227. table is routed to a partition based on the value of columns or
  228. expressions in the partition key. If no existing partition matches
  229. the values in the new row, an error will be reported.
  230. </p><p>
  231. Partitioned tables do not support <code class="literal">EXCLUDE</code> constraints;
  232. however, you can define these constraints on individual partitions.
  233. </p><p>
  234. See <a class="xref" href="ddl-partitioning.html" title="5.11. Table Partitioning">Section 5.11</a> for more discussion on table
  235. partitioning.
  236. </p></dd><dt id="SQL-CREATETABLE-PARTITION"><span class="term"><code class="literal">PARTITION OF <em class="replaceable"><code>parent_table</code></em> { FOR VALUES <em class="replaceable"><code>partition_bound_spec</code></em> | DEFAULT }</code></span></dt><dd><p>
  237. Creates the table as a <em class="firstterm">partition</em> of the specified
  238. parent table. The table can be created either as a partition for specific
  239. values using <code class="literal">FOR VALUES</code> or as a default partition
  240. using <code class="literal">DEFAULT</code>. Any indexes, constraints and
  241. user-defined row-level triggers that exist in the parent table are cloned
  242. on the new partition.
  243. </p><p>
  244. The <em class="replaceable"><code>partition_bound_spec</code></em>
  245. must correspond to the partitioning method and partition key of the
  246. parent table, and must not overlap with any existing partition of that
  247. parent. The form with <code class="literal">IN</code> is used for list partitioning,
  248. the form with <code class="literal">FROM</code> and <code class="literal">TO</code> is used
  249. for range partitioning, and the form with <code class="literal">WITH</code> is used
  250. for hash partitioning.
  251. </p><p>
  252. <em class="replaceable"><code>partition_bound_expr</code></em> is
  253. any variable-free expression (subqueries, window functions, aggregate
  254. functions, and set-returning functions are not allowed). Its data type
  255. must match the data type of the corresponding partition key column.
  256. The expression is evaluated once at table creation time, so it can
  257. even contain volatile expressions such as
  258. <code class="literal"><code class="function">CURRENT_TIMESTAMP</code></code>.
  259. </p><p>
  260. When creating a list partition, <code class="literal">NULL</code> can be
  261. specified to signify that the partition allows the partition key
  262. column to be null. However, there cannot be more than one such
  263. list partition for a given parent table. <code class="literal">NULL</code>
  264. cannot be specified for range partitions.
  265. </p><p>
  266. When creating a range partition, the lower bound specified with
  267. <code class="literal">FROM</code> is an inclusive bound, whereas the upper
  268. bound specified with <code class="literal">TO</code> is an exclusive bound.
  269. That is, the values specified in the <code class="literal">FROM</code> list
  270. are valid values of the corresponding partition key columns for this
  271. partition, whereas those in the <code class="literal">TO</code> list are
  272. not. Note that this statement must be understood according to the
  273. rules of row-wise comparison (<a class="xref" href="functions-comparisons.html#ROW-WISE-COMPARISON" title="9.23.5. Row Constructor Comparison">Section 9.23.5</a>).
  274. For example, given <code class="literal">PARTITION BY RANGE (x,y)</code>, a partition
  275. bound <code class="literal">FROM (1, 2) TO (3, 4)</code>
  276. allows <code class="literal">x=1</code> with any <code class="literal">y&gt;=2</code>,
  277. <code class="literal">x=2</code> with any non-null <code class="literal">y</code>,
  278. and <code class="literal">x=3</code> with any <code class="literal">y&lt;4</code>.
  279. </p><p>
  280. The special values <code class="literal">MINVALUE</code> and <code class="literal">MAXVALUE</code>
  281. may be used when creating a range partition to indicate that there
  282. is no lower or upper bound on the column's value. For example, a
  283. partition defined using <code class="literal">FROM (MINVALUE) TO (10)</code> allows
  284. any values less than 10, and a partition defined using
  285. <code class="literal">FROM (10) TO (MAXVALUE)</code> allows any values greater than
  286. or equal to 10.
  287. </p><p>
  288. When creating a range partition involving more than one column, it
  289. can also make sense to use <code class="literal">MAXVALUE</code> as part of the lower
  290. bound, and <code class="literal">MINVALUE</code> as part of the upper bound. For
  291. example, a partition defined using
  292. <code class="literal">FROM (0, MAXVALUE) TO (10, MAXVALUE)</code> allows any rows
  293. where the first partition key column is greater than 0 and less than
  294. or equal to 10. Similarly, a partition defined using
  295. <code class="literal">FROM ('a', MINVALUE) TO ('b', MINVALUE)</code> allows any rows
  296. where the first partition key column starts with "a".
  297. </p><p>
  298. Note that if <code class="literal">MINVALUE</code> or <code class="literal">MAXVALUE</code> is used for
  299. one column of a partitioning bound, the same value must be used for all
  300. subsequent columns. For example, <code class="literal">(10, MINVALUE, 0)</code> is not
  301. a valid bound; you should write <code class="literal">(10, MINVALUE, MINVALUE)</code>.
  302. </p><p>
  303. Also note that some element types, such as <code class="literal">timestamp</code>,
  304. have a notion of "infinity", which is just another value that can
  305. be stored. This is different from <code class="literal">MINVALUE</code> and
  306. <code class="literal">MAXVALUE</code>, which are not real values that can be stored,
  307. but rather they are ways of saying that the value is unbounded.
  308. <code class="literal">MAXVALUE</code> can be thought of as being greater than any
  309. other value, including "infinity" and <code class="literal">MINVALUE</code> as being
  310. less than any other value, including "minus infinity". Thus the range
  311. <code class="literal">FROM ('infinity') TO (MAXVALUE)</code> is not an empty range; it
  312. allows precisely one value to be stored — "infinity".
  313. </p><p>
  314. If <code class="literal">DEFAULT</code> is specified, the table will be
  315. created as the default partition of the parent table. This option
  316. is not available for hash-partitioned tables. A partition key value
  317. not fitting into any other partition of the given parent will be
  318. routed to the default partition.
  319. </p><p>
  320. When a table has an existing <code class="literal">DEFAULT</code> partition and
  321. a new partition is added to it, the default partition must
  322. be scanned to verify that it does not contain any rows which properly
  323. belong in the new partition. If the default partition contains a
  324. large number of rows, this may be slow. The scan will be skipped if
  325. the default partition is a foreign table or if it has a constraint which
  326. proves that it cannot contain rows which should be placed in the new
  327. partition.
  328. </p><p>
  329. When creating a hash partition, a modulus and remainder must be specified.
  330. The modulus must be a positive integer, and the remainder must be a
  331. non-negative integer less than the modulus. Typically, when initially
  332. setting up a hash-partitioned table, you should choose a modulus equal to
  333. the number of partitions and assign every table the same modulus and a
  334. different remainder (see examples, below). However, it is not required
  335. that every partition have the same modulus, only that every modulus which
  336. occurs among the partitions of a hash-partitioned table is a factor of the
  337. next larger modulus. This allows the number of partitions to be increased
  338. incrementally without needing to move all the data at once. For example,
  339. suppose you have a hash-partitioned table with 8 partitions, each of which
  340. has modulus 8, but find it necessary to increase the number of partitions
  341. to 16. You can detach one of the modulus-8 partitions, create two new
  342. modulus-16 partitions covering the same portion of the key space (one with
  343. a remainder equal to the remainder of the detached partition, and the
  344. other with a remainder equal to that value plus 8), and repopulate them
  345. with data. You can then repeat this -- perhaps at a later time -- for
  346. each modulus-8 partition until none remain. While this may still involve
  347. a large amount of data movement at each step, it is still better than
  348. having to create a whole new table and move all the data at once.
  349. </p><p>
  350. A partition must have the same column names and types as the partitioned
  351. table to which it belongs. Modifications to the column names or types of
  352. a partitioned table will automatically propagate to all partitions.
  353. <code class="literal">CHECK</code> constraints will be inherited automatically by
  354. every partition, but an individual partition may specify additional
  355. <code class="literal">CHECK</code> constraints; additional constraints with the
  356. same name and condition as in the parent will be merged with the parent
  357. constraint. Defaults may be specified separately for each partition.
  358. But note that a partition's default value is not applied when inserting
  359. a tuple through a partitioned table.
  360. </p><p>
  361. Rows inserted into a partitioned table will be automatically routed to
  362. the correct partition. If no suitable partition exists, an error will
  363. occur.
  364. </p><p>
  365. Operations such as TRUNCATE which normally affect a table and all of its
  366. inheritance children will cascade to all partitions, but may also be
  367. performed on an individual partition. Note that dropping a partition
  368. with <code class="literal">DROP TABLE</code> requires taking an <code class="literal">ACCESS
  369. EXCLUSIVE</code> lock on the parent table.
  370. </p></dd><dt><span class="term"><code class="literal">LIKE <em class="replaceable"><code>source_table</code></em> [ <em class="replaceable"><code>like_option</code></em> ... ]</code></span></dt><dd><p>
  371. The <code class="literal">LIKE</code> clause specifies a table from which
  372. the new table automatically copies all column names, their data types,
  373. and their not-null constraints.
  374. </p><p>
  375. Unlike <code class="literal">INHERITS</code>, the new table and original table
  376. are completely decoupled after creation is complete. Changes to the
  377. original table will not be applied to the new table, and it is not
  378. possible to include data of the new table in scans of the original
  379. table.
  380. </p><p>
  381. Also unlike <code class="literal">INHERITS</code>, columns and
  382. constraints copied by <code class="literal">LIKE</code> are not merged with similarly
  383. named columns and constraints.
  384. If the same name is specified explicitly or in another
  385. <code class="literal">LIKE</code> clause, an error is signaled.
  386. </p><p>
  387. The optional <em class="replaceable"><code>like_option</code></em> clauses specify
  388. which additional properties of the original table to copy. Specifying
  389. <code class="literal">INCLUDING</code> copies the property, specifying
  390. <code class="literal">EXCLUDING</code> omits the property.
  391. <code class="literal">EXCLUDING</code> is the default. If multiple specifications
  392. are made for the same kind of object, the last one is used. The
  393. available options are:
  394. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">INCLUDING COMMENTS</code></span></dt><dd><p>
  395. Comments for the copied columns, constraints, and indexes will be
  396. copied. The default behavior is to exclude comments, resulting in
  397. the copied columns and constraints in the new table having no
  398. comments.
  399. </p></dd><dt><span class="term"><code class="literal">INCLUDING CONSTRAINTS</code></span></dt><dd><p>
  400. <code class="literal">CHECK</code> constraints will be copied. No distinction
  401. is made between column constraints and table constraints. Not-null
  402. constraints are always copied to the new table.
  403. </p></dd><dt><span class="term"><code class="literal">INCLUDING DEFAULTS</code></span></dt><dd><p>
  404. Default expressions for the copied column definitions will be
  405. copied. Otherwise, default expressions are not copied, resulting in
  406. the copied columns in the new table having null defaults. Note that
  407. copying defaults that call database-modification functions, such as
  408. <code class="function">nextval</code>, may create a functional linkage
  409. between the original and new tables.
  410. </p></dd><dt><span class="term"><code class="literal">INCLUDING GENERATED</code></span></dt><dd><p>
  411. Any generation expressions of copied column definitions will be
  412. copied. By default, new columns will be regular base columns.
  413. </p></dd><dt><span class="term"><code class="literal">INCLUDING IDENTITY</code></span></dt><dd><p>
  414. Any identity specifications of copied column definitions will be
  415. copied. A new sequence is created for each identity column of the
  416. new table, separate from the sequences associated with the old
  417. table.
  418. </p></dd><dt><span class="term"><code class="literal">INCLUDING INDEXES</code></span></dt><dd><p>
  419. Indexes, <code class="literal">PRIMARY KEY</code>, <code class="literal">UNIQUE</code>,
  420. and <code class="literal">EXCLUDE</code> constraints on the original table
  421. will be created on the new table. Names for the new indexes and
  422. constraints are chosen according to the default rules, regardless of
  423. how the originals were named. (This behavior avoids possible
  424. duplicate-name failures for the new indexes.)
  425. </p></dd><dt><span class="term"><code class="literal">INCLUDING STATISTICS</code></span></dt><dd><p>
  426. Extended statistics are copied to the new table.
  427. </p></dd><dt><span class="term"><code class="literal">INCLUDING STORAGE</code></span></dt><dd><p>
  428. <code class="literal">STORAGE</code> settings for the copied column
  429. definitions will be copied. The default behavior is to exclude
  430. <code class="literal">STORAGE</code> settings, resulting in the copied columns
  431. in the new table having type-specific default settings. For more on
  432. <code class="literal">STORAGE</code> settings, see <a class="xref" href="storage-toast.html" title="68.2. TOAST">Section 68.2</a>.
  433. </p></dd><dt><span class="term"><code class="literal">INCLUDING ALL</code></span></dt><dd><p>
  434. <code class="literal">INCLUDING ALL</code> is an abbreviated form selecting
  435. all the available individual options. (It could be useful to write
  436. individual <code class="literal">EXCLUDING</code> clauses after
  437. <code class="literal">INCLUDING ALL</code> to select all but some specific
  438. options.)
  439. </p></dd></dl></div><p>
  440. </p><p>
  441. The <code class="literal">LIKE</code> clause can also be used to copy column
  442. definitions from views, foreign tables, or composite types.
  443. Inapplicable options (e.g., <code class="literal">INCLUDING INDEXES</code> from
  444. a view) are ignored.
  445. </p></dd><dt><span class="term"><code class="literal">CONSTRAINT <em class="replaceable"><code>constraint_name</code></em></code></span></dt><dd><p>
  446. An optional name for a column or table constraint. If the
  447. constraint is violated, the constraint name is present in error messages,
  448. so constraint names like <code class="literal">col must be positive</code> can be used
  449. to communicate helpful constraint information to client applications.
  450. (Double-quotes are needed to specify constraint names that contain spaces.)
  451. If a constraint name is not specified, the system generates a name.
  452. </p></dd><dt><span class="term"><code class="literal">NOT NULL</code></span></dt><dd><p>
  453. The column is not allowed to contain null values.
  454. </p></dd><dt><span class="term"><code class="literal">NULL</code></span></dt><dd><p>
  455. The column is allowed to contain null values. This is the default.
  456. </p><p>
  457. This clause is only provided for compatibility with
  458. non-standard SQL databases. Its use is discouraged in new
  459. applications.
  460. </p></dd><dt><span class="term"><code class="literal">CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ] </code></span></dt><dd><p>
  461. The <code class="literal">CHECK</code> clause specifies an expression producing a
  462. Boolean result which new or updated rows must satisfy for an
  463. insert or update operation to succeed. Expressions evaluating
  464. to TRUE or UNKNOWN succeed. Should any row of an insert or
  465. update operation produce a FALSE result, an error exception is
  466. raised and the insert or update does not alter the database. A
  467. check constraint specified as a column constraint should
  468. reference that column's value only, while an expression
  469. appearing in a table constraint can reference multiple columns.
  470. </p><p>
  471. Currently, <code class="literal">CHECK</code> expressions cannot contain
  472. subqueries nor refer to variables other than columns of the
  473. current row (see <a class="xref" href="ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS" title="5.4.1. Check Constraints">Section 5.4.1</a>).
  474. The system column <code class="literal">tableoid</code>
  475. may be referenced, but not any other system column.
  476. </p><p>
  477. A constraint marked with <code class="literal">NO INHERIT</code> will not propagate to
  478. child tables.
  479. </p><p>
  480. When a table has multiple <code class="literal">CHECK</code> constraints,
  481. they will be tested for each row in alphabetical order by name,
  482. after checking <code class="literal">NOT NULL</code> constraints.
  483. (<span class="productname">PostgreSQL</span> versions before 9.5 did not honor any
  484. particular firing order for <code class="literal">CHECK</code> constraints.)
  485. </p></dd><dt><span class="term"><code class="literal">DEFAULT
  486. <em class="replaceable"><code>default_expr</code></em></code></span></dt><dd><p>
  487. The <code class="literal">DEFAULT</code> clause assigns a default data value for
  488. the column whose column definition it appears within. The value
  489. is any variable-free expression (in particular, cross-references
  490. to other columns in the current table are not allowed). Subqueries
  491. are not allowed either. The data type of the default expression must
  492. match the data type of the column.
  493. </p><p>
  494. The default expression will be used in any insert operation that
  495. does not specify a value for the column. If there is no default
  496. for a column, then the default is null.
  497. </p></dd><dt><span class="term"><code class="literal">GENERATED ALWAYS AS ( <em class="replaceable"><code>generation_expr</code></em> ) STORED</code><a id="id-1.9.3.85.6.2.18.1.2" class="indexterm"></a></span></dt><dd><p>
  498. This clause creates the column as a <em class="firstterm">generated
  499. column</em>. The column cannot be written to, and when read the
  500. result of the specified expression will be returned.
  501. </p><p>
  502. The keyword <code class="literal">STORED</code> is required to signify that the
  503. column will be computed on write and will be stored on disk.
  504. </p><p>
  505. The generation expression can refer to other columns in the table, but
  506. not other generated columns. Any functions and operators used must be
  507. immutable. References to other tables are not allowed.
  508. </p></dd><dt><span class="term"><code class="literal">GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <em class="replaceable"><code>sequence_options</code></em> ) ]</code></span></dt><dd><p>
  509. This clause creates the column as an <em class="firstterm">identity
  510. column</em>. It will have an implicit sequence attached to it
  511. and the column in new rows will automatically have values from the
  512. sequence assigned to it.
  513. </p><p>
  514. The clauses <code class="literal">ALWAYS</code> and <code class="literal">BY DEFAULT</code>
  515. determine how the sequence value is given precedence over a
  516. user-specified value in an <code class="command">INSERT</code> statement.
  517. If <code class="literal">ALWAYS</code> is specified, a user-specified value is
  518. only accepted if the <code class="command">INSERT</code> statement
  519. specifies <code class="literal">OVERRIDING SYSTEM VALUE</code>. If <code class="literal">BY
  520. DEFAULT</code> is specified, then the user-specified value takes
  521. precedence. See <a class="xref" href="sql-insert.html" title="INSERT"><span class="refentrytitle">INSERT</span></a> for details. (In
  522. the <code class="command">COPY</code> command, user-specified values are always
  523. used regardless of this setting.)
  524. </p><p>
  525. The optional <em class="replaceable"><code>sequence_options</code></em> clause can be
  526. used to override the options of the sequence.
  527. See <a class="xref" href="sql-createsequence.html" title="CREATE SEQUENCE"><span class="refentrytitle">CREATE SEQUENCE</span></a> for details.
  528. </p></dd><dt><span class="term"><code class="literal">UNIQUE</code> (column constraint)<br /></span><span class="term"><code class="literal">UNIQUE ( <em class="replaceable"><code>column_name</code></em> [, ... ] )</code>
  529. [<span class="optional"> INCLUDE ( <em class="replaceable"><code>column_name</code></em> [, ...]) </span>] (table constraint)</span></dt><dd><p>
  530. The <code class="literal">UNIQUE</code> constraint specifies that a
  531. group of one or more columns of a table can contain
  532. only unique values. The behavior of the unique table constraint
  533. is the same as that for column constraints, with the additional
  534. capability to span multiple columns.
  535. </p><p>
  536. For the purpose of a unique constraint, null values are not
  537. considered equal.
  538. </p><p>
  539. Each unique table constraint must name a set of columns that is
  540. different from the set of columns named by any other unique or
  541. primary key constraint defined for the table. (Otherwise it
  542. would just be the same constraint listed twice.)
  543. </p><p>
  544. When establishing a unique constraint for a multi-level partition
  545. hierarchy, all the columns in the partition key of the target
  546. partitioned table, as well as those of all its descendant partitioned
  547. tables, must be included in the constraint definition.
  548. </p><p>
  549. Adding a unique constraint will automatically create a unique btree
  550. index on the column or group of columns used in the constraint.
  551. The optional clause <code class="literal">INCLUDE</code> adds to that index
  552. one or more columns on which the uniqueness is not enforced.
  553. Note that although the constraint is not enforced on the included columns,
  554. it still depends on them. Consequently, some operations on these columns
  555. (e.g. <code class="literal">DROP COLUMN</code>) can cause cascaded constraint and
  556. index deletion.
  557. </p></dd><dt><span class="term"><code class="literal">PRIMARY KEY</code> (column constraint)<br /></span><span class="term"><code class="literal">PRIMARY KEY ( <em class="replaceable"><code>column_name</code></em> [, ... ] )</code>
  558. [<span class="optional"> INCLUDE ( <em class="replaceable"><code>column_name</code></em> [, ...]) </span>] (table constraint)</span></dt><dd><p>
  559. The <code class="literal">PRIMARY KEY</code> constraint specifies that a column or
  560. columns of a table can contain only unique (non-duplicate), nonnull
  561. values. Only one primary key can be specified for a table, whether as a
  562. column constraint or a table constraint.
  563. </p><p>
  564. The primary key constraint should name a set of columns that is
  565. different from the set of columns named by any unique
  566. constraint defined for the same table. (Otherwise, the unique
  567. constraint is redundant and will be discarded.)
  568. </p><p>
  569. <code class="literal">PRIMARY KEY</code> enforces the same data constraints as
  570. a combination of <code class="literal">UNIQUE</code> and <code class="literal">NOT NULL</code>, but
  571. identifying a set of columns as the primary key also provides metadata
  572. about the design of the schema, since a primary key implies that other
  573. tables can rely on this set of columns as a unique identifier for rows.
  574. </p><p>
  575. <code class="literal">PRIMARY KEY</code> constraints share the restrictions that
  576. <code class="literal">UNIQUE</code> constraints have when placed on partitioned
  577. tables.
  578. </p><p>
  579. Adding a <code class="literal">PRIMARY KEY</code> constraint will automatically
  580. create a unique btree index on the column or group of columns used in the
  581. constraint. The optional <code class="literal">INCLUDE</code> clause allows a list
  582. of columns to be specified which will be included in the non-key portion
  583. of the index. Although uniqueness is not enforced on the included columns,
  584. the constraint still depends on them. Consequently, some operations on the
  585. included columns (e.g. <code class="literal">DROP COLUMN</code>) can cause cascaded
  586. constraint and index deletion.
  587. </p></dd><dt id="SQL-CREATETABLE-EXCLUDE"><span class="term"><code class="literal">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> ) ]</code></span></dt><dd><p>
  588. The <code class="literal">EXCLUDE</code> clause defines an exclusion
  589. constraint, which guarantees that if
  590. any two rows are compared on the specified column(s) or
  591. expression(s) using the specified operator(s), not all of these
  592. comparisons will return <code class="literal">TRUE</code>. If all of the
  593. specified operators test for equality, this is equivalent to a
  594. <code class="literal">UNIQUE</code> constraint, although an ordinary unique constraint
  595. will be faster. However, exclusion constraints can specify
  596. constraints that are more general than simple equality.
  597. For example, you can specify a constraint that
  598. no two rows in the table contain overlapping circles
  599. (see <a class="xref" href="datatype-geometric.html" title="8.8. Geometric Types">Section 8.8</a>) by using the
  600. <code class="literal">&amp;&amp;</code> operator.
  601. </p><p>
  602. Exclusion constraints are implemented using
  603. an index, so each specified operator must be associated with an
  604. appropriate operator class
  605. (see <a class="xref" href="indexes-opclass.html" title="11.10. Operator Classes and Operator Families">Section 11.10</a>) for the index access
  606. method <em class="replaceable"><code>index_method</code></em>.
  607. The operators are required to be commutative.
  608. Each <em class="replaceable"><code>exclude_element</code></em>
  609. can optionally specify an operator class and/or ordering options;
  610. these are described fully under
  611. <a class="xref" href="sql-createindex.html" title="CREATE INDEX"><span class="refentrytitle">CREATE INDEX</span></a>.
  612. </p><p>
  613. The access method must support <code class="literal">amgettuple</code> (see <a class="xref" href="indexam.html" title="Chapter 61. Index Access Method Interface Definition">Chapter 61</a>); at present this means <acronym class="acronym">GIN</acronym>
  614. cannot be used. Although it's allowed, there is little point in using
  615. B-tree or hash indexes with an exclusion constraint, because this
  616. does nothing that an ordinary unique constraint doesn't do better.
  617. So in practice the access method will always be <acronym class="acronym">GiST</acronym> or
  618. <acronym class="acronym">SP-GiST</acronym>.
  619. </p><p>
  620. The <em class="replaceable"><code>predicate</code></em> allows you to specify an
  621. exclusion constraint on a subset of the table; internally this creates a
  622. partial index. Note that parentheses are required around the predicate.
  623. </p></dd><dt><span class="term"><code class="literal">REFERENCES <em class="replaceable"><code>reftable</code></em> [ ( <em class="replaceable"><code>refcolumn</code></em> ) ] [ MATCH <em class="replaceable"><code>matchtype</code></em> ] [ ON DELETE <em class="replaceable"><code>referential_action</code></em> ] [ ON UPDATE <em class="replaceable"><code>referential_action</code></em> ]</code> (column constraint)<br /></span><span class="term"><code class="literal">FOREIGN KEY ( <em class="replaceable"><code>column_name</code></em> [, ... ] )
  624. REFERENCES <em class="replaceable"><code>reftable</code></em> [ ( <em class="replaceable"><code>refcolumn</code></em> [, ... ] ) ]
  625. [ MATCH <em class="replaceable"><code>matchtype</code></em> ]
  626. [ ON DELETE <em class="replaceable"><code>referential_action</code></em> ]
  627. [ ON UPDATE <em class="replaceable"><code>referential_action</code></em> ]</code>
  628. (table constraint)</span></dt><dd><p>
  629. These clauses specify a foreign key constraint, which requires
  630. that a group of one or more columns of the new table must only
  631. contain values that match values in the referenced
  632. column(s) of some row of the referenced table. If the <em class="replaceable"><code>refcolumn</code></em> list is omitted, the
  633. primary key of the <em class="replaceable"><code>reftable</code></em>
  634. is used. The referenced columns must be the columns of a non-deferrable
  635. unique or primary key constraint in the referenced table. The user
  636. must have <code class="literal">REFERENCES</code> permission on the referenced table
  637. (either the whole table, or the specific referenced columns). The
  638. addition of a foreign key constraint requires a
  639. <code class="literal">SHARE ROW EXCLUSIVE</code> lock on the referenced table.
  640. Note that foreign key constraints cannot be defined between temporary
  641. tables and permanent tables.
  642. </p><p>
  643. A value inserted into the referencing column(s) is matched against the
  644. values of the referenced table and referenced columns using the
  645. given match type. There are three match types: <code class="literal">MATCH
  646. FULL</code>, <code class="literal">MATCH PARTIAL</code>, and <code class="literal">MATCH
  647. SIMPLE</code> (which is the default). <code class="literal">MATCH
  648. FULL</code> will not allow one column of a multicolumn foreign key
  649. to be null unless all foreign key columns are null; if they are all
  650. null, the row is not required to have a match in the referenced table.
  651. <code class="literal">MATCH SIMPLE</code> allows any of the foreign key columns
  652. to be null; if any of them are null, the row is not required to have a
  653. match in the referenced table.
  654. <code class="literal">MATCH PARTIAL</code> is not yet implemented.
  655. (Of course, <code class="literal">NOT NULL</code> constraints can be applied to the
  656. referencing column(s) to prevent these cases from arising.)
  657. </p><p>
  658. In addition, when the data in the referenced columns is changed,
  659. certain actions are performed on the data in this table's
  660. columns. The <code class="literal">ON DELETE</code> clause specifies the
  661. action to perform when a referenced row in the referenced table is
  662. being deleted. Likewise, the <code class="literal">ON UPDATE</code>
  663. clause specifies the action to perform when a referenced column
  664. in the referenced table is being updated to a new value. If the
  665. row is updated, but the referenced column is not actually
  666. changed, no action is done. Referential actions other than the
  667. <code class="literal">NO ACTION</code> check cannot be deferred, even if
  668. the constraint is declared deferrable. There are the following possible
  669. actions for each clause:
  670. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">NO ACTION</code></span></dt><dd><p>
  671. Produce an error indicating that the deletion or update
  672. would create a foreign key constraint violation.
  673. If the constraint is deferred, this
  674. error will be produced at constraint check time if there still
  675. exist any referencing rows. This is the default action.
  676. </p></dd><dt><span class="term"><code class="literal">RESTRICT</code></span></dt><dd><p>
  677. Produce an error indicating that the deletion or update
  678. would create a foreign key constraint violation.
  679. This is the same as <code class="literal">NO ACTION</code> except that
  680. the check is not deferrable.
  681. </p></dd><dt><span class="term"><code class="literal">CASCADE</code></span></dt><dd><p>
  682. Delete any rows referencing the deleted row, or update the
  683. values of the referencing column(s) to the new values of the
  684. referenced columns, respectively.
  685. </p></dd><dt><span class="term"><code class="literal">SET NULL</code></span></dt><dd><p>
  686. Set the referencing column(s) to null.
  687. </p></dd><dt><span class="term"><code class="literal">SET DEFAULT</code></span></dt><dd><p>
  688. Set the referencing column(s) to their default values.
  689. (There must be a row in the referenced table matching the default
  690. values, if they are not null, or the operation will fail.)
  691. </p></dd></dl></div><p>
  692. </p><p>
  693. If the referenced column(s) are changed frequently, it might be wise to
  694. add an index to the referencing column(s) so that referential actions
  695. associated with the foreign key constraint can be performed more
  696. efficiently.
  697. </p></dd><dt><span class="term"><code class="literal">DEFERRABLE</code><br /></span><span class="term"><code class="literal">NOT DEFERRABLE</code></span></dt><dd><p>
  698. This controls whether the constraint can be deferred. A
  699. constraint that is not deferrable will be checked immediately
  700. after every command. Checking of constraints that are
  701. deferrable can be postponed until the end of the transaction
  702. (using the <a class="xref" href="sql-set-constraints.html" title="SET CONSTRAINTS"><span class="refentrytitle">SET CONSTRAINTS</span></a> command).
  703. <code class="literal">NOT DEFERRABLE</code> is the default.
  704. Currently, only <code class="literal">UNIQUE</code>, <code class="literal">PRIMARY KEY</code>,
  705. <code class="literal">EXCLUDE</code>, and
  706. <code class="literal">REFERENCES</code> (foreign key) constraints accept this
  707. clause. <code class="literal">NOT NULL</code> and <code class="literal">CHECK</code> constraints are not
  708. deferrable. Note that deferrable constraints cannot be used as
  709. conflict arbitrators in an <code class="command">INSERT</code> statement that
  710. includes an <code class="literal">ON CONFLICT DO UPDATE</code> clause.
  711. </p></dd><dt><span class="term"><code class="literal">INITIALLY IMMEDIATE</code><br /></span><span class="term"><code class="literal">INITIALLY DEFERRED</code></span></dt><dd><p>
  712. If a constraint is deferrable, this clause specifies the default
  713. time to check the constraint. If the constraint is
  714. <code class="literal">INITIALLY IMMEDIATE</code>, it is checked after each
  715. statement. This is the default. If the constraint is
  716. <code class="literal">INITIALLY DEFERRED</code>, it is checked only at the
  717. end of the transaction. The constraint check time can be
  718. altered with the <a class="xref" href="sql-set-constraints.html" title="SET CONSTRAINTS"><span class="refentrytitle">SET CONSTRAINTS</span></a> command.
  719. </p></dd><dt id="SQL-CREATETABLE-METHOD"><span class="term"><code class="literal">USING <em class="replaceable"><code>method</code></em></code></span></dt><dd><p>
  720. This optional clause specifies the table access method to use to store
  721. the contents for the new table; the method needs be an access method of
  722. type <code class="literal">TABLE</code>. See <a class="xref" href="tableam.html" title="Chapter 60. Table Access Method Interface Definition">Chapter 60</a> for more
  723. information. If this option is not specified, the default table access
  724. method is chosen for the new table. See <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD">default_table_access_method</a> for more information.
  725. </p></dd><dt><span class="term"><code class="literal">WITH ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] )</code></span></dt><dd><p>
  726. This clause specifies optional storage parameters for a table or index;
  727. see <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS" title="Storage Parameters">Storage Parameters</a> for more
  728. information. For backward-compatibility the <code class="literal">WITH</code>
  729. clause for a table can also include <code class="literal">OIDS=FALSE</code> to
  730. specify that rows of the new table should not contain OIDs (object
  731. identifiers), <code class="literal">OIDS=TRUE</code> is not supported anymore.
  732. </p></dd><dt><span class="term"><code class="literal">WITHOUT OIDS</code></span></dt><dd><p>
  733. This is backward-compatible syntax for declaring a table
  734. <code class="literal">WITHOUT OIDS</code>, creating a table <code class="literal">WITH
  735. OIDS</code> is not supported anymore.
  736. </p></dd><dt><span class="term"><code class="literal">ON COMMIT</code></span></dt><dd><p>
  737. The behavior of temporary tables at the end of a transaction
  738. block can be controlled using <code class="literal">ON COMMIT</code>.
  739. The three options are:
  740. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">PRESERVE ROWS</code></span></dt><dd><p>
  741. No special action is taken at the ends of transactions.
  742. This is the default behavior.
  743. </p></dd><dt><span class="term"><code class="literal">DELETE ROWS</code></span></dt><dd><p>
  744. All rows in the temporary table will be deleted at the end
  745. of each transaction block. Essentially, an automatic <a class="xref" href="sql-truncate.html" title="TRUNCATE"><span class="refentrytitle">TRUNCATE</span></a> is done
  746. at each commit. When used on a partitioned table, this
  747. is not cascaded to its partitions.
  748. </p></dd><dt><span class="term"><code class="literal">DROP</code></span></dt><dd><p>
  749. The temporary table will be dropped at the end of the current
  750. transaction block. When used on a partitioned table, this action
  751. drops its partitions and when used on tables with inheritance
  752. children, it drops the dependent children.
  753. </p></dd></dl></div></dd><dt id="SQL-CREATETABLE-TABLESPACE"><span class="term"><code class="literal">TABLESPACE <em class="replaceable"><code>tablespace_name</code></em></code></span></dt><dd><p>
  754. The <em class="replaceable"><code>tablespace_name</code></em> is the name
  755. of the tablespace in which the new table is to be created.
  756. If not specified,
  757. <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TABLESPACE">default_tablespace</a> is consulted, or
  758. <a class="xref" href="runtime-config-client.html#GUC-TEMP-TABLESPACES">temp_tablespaces</a> if the table is temporary. For
  759. partitioned tables, since no storage is required for the table itself,
  760. the tablespace specified overrides <code class="literal">default_tablespace</code>
  761. as the default tablespace to use for any newly created partitions when no
  762. other tablespace is explicitly specified.
  763. </p></dd><dt><span class="term"><code class="literal">USING INDEX TABLESPACE <em class="replaceable"><code>tablespace_name</code></em></code></span></dt><dd><p>
  764. This clause allows selection of the tablespace in which the index
  765. associated with a <code class="literal">UNIQUE</code>, <code class="literal">PRIMARY
  766. KEY</code>, or <code class="literal">EXCLUDE</code> constraint will be created.
  767. If not specified,
  768. <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TABLESPACE">default_tablespace</a> is consulted, or
  769. <a class="xref" href="runtime-config-client.html#GUC-TEMP-TABLESPACES">temp_tablespaces</a> if the table is temporary.
  770. </p></dd></dl></div><div class="refsect2" id="SQL-CREATETABLE-STORAGE-PARAMETERS"><h3>Storage Parameters</h3><a id="id-1.9.3.85.6.3.2" class="indexterm"></a><p>
  771. The <code class="literal">WITH</code> clause can specify <em class="firstterm">storage parameters</em>
  772. for tables, and for indexes associated with a <code class="literal">UNIQUE</code>,
  773. <code class="literal">PRIMARY KEY</code>, or <code class="literal">EXCLUDE</code> constraint.
  774. Storage parameters for
  775. indexes are documented in <a class="xref" href="sql-createindex.html" title="CREATE INDEX"><span class="refentrytitle">CREATE INDEX</span></a>.
  776. The storage parameters currently
  777. available for tables are listed below. For many of these parameters, as
  778. shown, there is an additional parameter with the same name prefixed with
  779. <code class="literal">toast.</code>, which controls the behavior of the
  780. table's secondary <acronym class="acronym">TOAST</acronym> table, if any
  781. (see <a class="xref" href="storage-toast.html" title="68.2. TOAST">Section 68.2</a> for more information about TOAST).
  782. If a table parameter value is set and the
  783. equivalent <code class="literal">toast.</code> parameter is not, the TOAST table
  784. will use the table's parameter value.
  785. Specifying these parameters for partitioned tables is not supported,
  786. but you may specify them for individual leaf partitions.
  787. </p><div class="variablelist"><dl class="variablelist"><dt id="RELOPTION-FILLFACTOR"><span class="term"><code class="varname">fillfactor</code> (<code class="type">integer</code>)
  788. <a id="id-1.9.3.85.6.3.4.1.1.3" class="indexterm"></a>
  789. </span></dt><dd><p>
  790. The fillfactor for a table is a percentage between 10 and 100.
  791. 100 (complete packing) is the default. When a smaller fillfactor
  792. is specified, <code class="command">INSERT</code> operations pack table pages only
  793. to the indicated percentage; the remaining space on each page is
  794. reserved for updating rows on that page. This gives <code class="command">UPDATE</code>
  795. a chance to place the updated copy of a row on the same page as the
  796. original, which is more efficient than placing it on a different page.
  797. For a table whose entries are never updated, complete packing is the
  798. best choice, but in heavily updated tables smaller fillfactors are
  799. appropriate. This parameter cannot be set for TOAST tables.
  800. </p></dd><dt id="RELOPTION-TOAST-TUPLE-TARGET"><span class="term"><code class="literal">toast_tuple_target</code> (<code class="type">integer</code>)
  801. <a id="id-1.9.3.85.6.3.4.2.1.3" class="indexterm"></a>
  802. </span></dt><dd><p>
  803. The toast_tuple_target specifies the minimum tuple length required before
  804. we try to move long column values into TOAST tables, and is also the
  805. target length we try to reduce the length below once toasting begins.
  806. This only affects columns marked as either External or Extended
  807. and applies only to new tuples - there is no effect on existing rows.
  808. By default this parameter is set to allow at least 4 tuples per block,
  809. which with the default blocksize will be 2040 bytes. Valid values are
  810. between 128 bytes and the (blocksize - header), by default 8160 bytes.
  811. Changing this value may not be useful for very short or very long rows.
  812. Note that the default setting is often close to optimal, and
  813. it is possible that setting this parameter could have negative
  814. effects in some cases.
  815. This parameter cannot be set for TOAST tables.
  816. </p></dd><dt id="RELOPTION-PARALLEL-WORKERS"><span class="term"><code class="literal">parallel_workers</code> (<code class="type">integer</code>)
  817. <a id="id-1.9.3.85.6.3.4.3.1.3" class="indexterm"></a>
  818. </span></dt><dd><p>
  819. This sets the number of workers that should be used to assist a parallel
  820. scan of this table. If not set, the system will determine a value based
  821. on the relation size. The actual number of workers chosen by the planner
  822. or by utility statements that use parallel scans may be less, for example
  823. due to the setting of <a class="xref" href="runtime-config-resource.html#GUC-MAX-WORKER-PROCESSES">max_worker_processes</a>.
  824. </p></dd><dt id="RELOPTION-AUTOVACUUM-ENABLED"><span class="term"><code class="literal">autovacuum_enabled</code>, <code class="literal">toast.autovacuum_enabled</code> (<code class="type">boolean</code>)
  825. <a id="id-1.9.3.85.6.3.4.4.1.4" class="indexterm"></a>
  826. </span></dt><dd><p>
  827. Enables or disables the autovacuum daemon for a particular table.
  828. If true, the autovacuum daemon will perform automatic <code class="command">VACUUM</code>
  829. and/or <code class="command">ANALYZE</code> operations on this table following the rules
  830. discussed in <a class="xref" href="routine-vacuuming.html#AUTOVACUUM" title="24.1.6. The Autovacuum Daemon">Section 24.1.6</a>.
  831. If false, this table will not be autovacuumed, except to prevent
  832. transaction ID wraparound. See <a class="xref" href="routine-vacuuming.html#VACUUM-FOR-WRAPAROUND" title="24.1.5. Preventing Transaction ID Wraparound Failures">Section 24.1.5</a> for
  833. more about wraparound prevention.
  834. Note that the autovacuum daemon does not run at all (except to prevent
  835. transaction ID wraparound) if the <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM">autovacuum</a>
  836. parameter is false; setting individual tables' storage parameters does
  837. not override that. Therefore there is seldom much point in explicitly
  838. setting this storage parameter to <code class="literal">true</code>, only
  839. to <code class="literal">false</code>.
  840. </p></dd><dt id="RELOPTION-VACUUM-INDEX-CLEANUP"><span class="term"><code class="literal">vacuum_index_cleanup</code>, <code class="literal">toast.vacuum_index_cleanup</code> (<code class="type">boolean</code>)
  841. <a id="id-1.9.3.85.6.3.4.5.1.4" class="indexterm"></a>
  842. </span></dt><dd><p>
  843. Enables or disables index cleanup when <code class="command">VACUUM</code> is
  844. run on this table. The default value is <code class="literal">true</code>.
  845. Disabling index cleanup can speed up <code class="command">VACUUM</code> very
  846. significantly, but may also lead to severely bloated indexes if table
  847. modifications are frequent. The <code class="literal">INDEX_CLEANUP</code>
  848. parameter of <a class="xref" href="sql-vacuum.html" title="VACUUM"><span class="refentrytitle">VACUUM</span></a>, if specified, overrides
  849. the value of this option.
  850. </p></dd><dt id="RELOPTION-VACUUM-TRUNCATE"><span class="term"><code class="literal">vacuum_truncate</code>, <code class="literal">toast.vacuum_truncate</code> (<code class="type">boolean</code>)
  851. <a id="id-1.9.3.85.6.3.4.6.1.4" class="indexterm"></a>
  852. </span></dt><dd><p>
  853. Enables or disables vacuum to try to truncate off any empty pages
  854. at the end of this table. The default value is <code class="literal">true</code>.
  855. If <code class="literal">true</code>, <code class="command">VACUUM</code> and
  856. autovacuum do the truncation and the disk space for
  857. the truncated pages is returned to the operating system.
  858. Note that the truncation requires <code class="literal">ACCESS EXCLUSIVE</code>
  859. lock on the table. The <code class="literal">TRUNCATE</code> parameter
  860. of <a class="xref" href="sql-vacuum.html" title="VACUUM"><span class="refentrytitle">VACUUM</span></a>, if specified, overrides the value
  861. of this option.
  862. </p></dd><dt id="RELOPTION-AUTOVACUUM-VACUUM-THRESHOLD"><span class="term"><code class="literal">autovacuum_vacuum_threshold</code>, <code class="literal">toast.autovacuum_vacuum_threshold</code> (<code class="type">integer</code>)
  863. <a id="id-1.9.3.85.6.3.4.7.1.4" class="indexterm"></a>
  864. </span></dt><dd><p>
  865. Per-table value for <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-VACUUM-THRESHOLD">autovacuum_vacuum_threshold</a>
  866. parameter.
  867. </p></dd><dt id="RELOPTION-AUTOVACUUM-VAUUM-SCALE-FACTOR"><span class="term"><code class="literal">autovacuum_vacuum_scale_factor</code>, <code class="literal">toast.autovacuum_vacuum_scale_factor</code> (<code class="type">floating point</code>)
  868. <a id="id-1.9.3.85.6.3.4.8.1.4" class="indexterm"></a>
  869. </span></dt><dd><p>
  870. Per-table value for <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-VACUUM-SCALE-FACTOR">autovacuum_vacuum_scale_factor</a>
  871. parameter.
  872. </p></dd><dt id="RELOPTION-AUTOVACUUM-ANALYZE-THRESHOLD"><span class="term"><code class="literal">autovacuum_analyze_threshold</code> (<code class="type">integer</code>)
  873. <a id="id-1.9.3.85.6.3.4.9.1.3" class="indexterm"></a>
  874. </span></dt><dd><p>
  875. Per-table value for <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-ANALYZE-THRESHOLD">autovacuum_analyze_threshold</a>
  876. parameter.
  877. </p></dd><dt id="RELOPTION-AUTOVACUUM-ANALYZE-SCALE-FACTOR"><span class="term"><code class="literal">autovacuum_analyze_scale_factor</code> (<code class="type">floating point</code>)
  878. <a id="id-1.9.3.85.6.3.4.10.1.3" class="indexterm"></a>
  879. </span></dt><dd><p>
  880. Per-table value for <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-ANALYZE-SCALE-FACTOR">autovacuum_analyze_scale_factor</a>
  881. parameter.
  882. </p></dd><dt id="RELOPTION-AUTOVACUUM-VACUUM-COST-DELAY"><span class="term"><code class="literal">autovacuum_vacuum_cost_delay</code>, <code class="literal">toast.autovacuum_vacuum_cost_delay</code> (<code class="type">floating point</code>)
  883. <a id="id-1.9.3.85.6.3.4.11.1.4" class="indexterm"></a>
  884. </span></dt><dd><p>
  885. Per-table value for <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-VACUUM-COST-DELAY">autovacuum_vacuum_cost_delay</a>
  886. parameter.
  887. </p></dd><dt id="RELOPTION-AUTOVACUUM-VAUUM-COST-LIMIT"><span class="term"><code class="literal">autovacuum_vacuum_cost_limit</code>, <code class="literal">toast.autovacuum_vacuum_cost_limit</code> (<code class="type">integer</code>)
  888. <a id="id-1.9.3.85.6.3.4.12.1.4" class="indexterm"></a>
  889. </span></dt><dd><p>
  890. Per-table value for <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-VACUUM-COST-LIMIT">autovacuum_vacuum_cost_limit</a>
  891. parameter.
  892. </p></dd><dt id="RELOPTION-AUTOVACUUM-FREEZE-MIN-AGE"><span class="term"><code class="literal">autovacuum_freeze_min_age</code>, <code class="literal">toast.autovacuum_freeze_min_age</code> (<code class="type">integer</code>)
  893. <a id="id-1.9.3.85.6.3.4.13.1.4" class="indexterm"></a>
  894. </span></dt><dd><p>
  895. Per-table value for <a class="xref" href="runtime-config-client.html#GUC-VACUUM-FREEZE-MIN-AGE">vacuum_freeze_min_age</a>
  896. parameter. Note that autovacuum will ignore
  897. per-table <code class="literal">autovacuum_freeze_min_age</code> parameters that are
  898. larger than half the
  899. system-wide <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-FREEZE-MAX-AGE">autovacuum_freeze_max_age</a> setting.
  900. </p></dd><dt id="RELOPTION-AUTOVACUUM-FREEZE-MAX-AGE"><span class="term"><code class="literal">autovacuum_freeze_max_age</code>, <code class="literal">toast.autovacuum_freeze_max_age</code> (<code class="type">integer</code>)
  901. <a id="id-1.9.3.85.6.3.4.14.1.4" class="indexterm"></a>
  902. </span></dt><dd><p>
  903. Per-table value for <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-FREEZE-MAX-AGE">autovacuum_freeze_max_age</a>
  904. parameter. Note that autovacuum will ignore
  905. per-table <code class="literal">autovacuum_freeze_max_age</code> parameters that are
  906. larger than the system-wide setting (it can only be set smaller).
  907. </p></dd><dt id="RELOPTION-AUTOVACUUM-FREEZE-TABLE-AGE"><span class="term"><code class="literal">autovacuum_freeze_table_age</code>, <code class="literal">toast.autovacuum_freeze_table_age</code> (<code class="type">integer</code>)
  908. <a id="id-1.9.3.85.6.3.4.15.1.4" class="indexterm"></a>
  909. </span></dt><dd><p>
  910. Per-table value for <a class="xref" href="runtime-config-client.html#GUC-VACUUM-FREEZE-TABLE-AGE">vacuum_freeze_table_age</a>
  911. parameter.
  912. </p></dd><dt id="RELOPTION-AUTOVACUUM-MULTIXACT-FREEZE-MIN-AGE"><span class="term"><code class="literal">autovacuum_multixact_freeze_min_age</code>, <code class="literal">toast.autovacuum_multixact_freeze_min_age</code> (<code class="type">integer</code>)
  913. <a id="id-1.9.3.85.6.3.4.16.1.4" class="indexterm"></a>
  914. </span></dt><dd><p>
  915. Per-table value for <a class="xref" href="runtime-config-client.html#GUC-VACUUM-MULTIXACT-FREEZE-MIN-AGE">vacuum_multixact_freeze_min_age</a>
  916. parameter. Note that autovacuum will ignore
  917. per-table <code class="literal">autovacuum_multixact_freeze_min_age</code> parameters
  918. that are larger than half the
  919. system-wide <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-MULTIXACT-FREEZE-MAX-AGE">autovacuum_multixact_freeze_max_age</a>
  920. setting.
  921. </p></dd><dt id="RELOPTION-AUTOVACUUM-MULTIXACT-FREEZE-MAX-AGE"><span class="term"><code class="literal">autovacuum_multixact_freeze_max_age</code>, <code class="literal">toast.autovacuum_multixact_freeze_max_age</code> (<code class="type">integer</code>)
  922. <a id="id-1.9.3.85.6.3.4.17.1.4" class="indexterm"></a>
  923. </span></dt><dd><p>
  924. Per-table value
  925. for <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-MULTIXACT-FREEZE-MAX-AGE">autovacuum_multixact_freeze_max_age</a> parameter.
  926. Note that autovacuum will ignore
  927. per-table <code class="literal">autovacuum_multixact_freeze_max_age</code> parameters
  928. that are larger than the system-wide setting (it can only be set
  929. smaller).
  930. </p></dd><dt id="RELOPTION-AUTOVACUUM-MULTIXACT-FREEZE-TABLE-AGE"><span class="term"><code class="literal">autovacuum_multixact_freeze_table_age</code>, <code class="literal">toast.autovacuum_multixact_freeze_table_age</code> (<code class="type">integer</code>)
  931. <a id="id-1.9.3.85.6.3.4.18.1.4" class="indexterm"></a>
  932. </span></dt><dd><p>
  933. Per-table value
  934. for <a class="xref" href="runtime-config-client.html#GUC-VACUUM-MULTIXACT-FREEZE-TABLE-AGE">vacuum_multixact_freeze_table_age</a> parameter.
  935. </p></dd><dt id="RELOPTION-LOG-AUTOVACUUM-MIN-DURATION"><span class="term"><code class="literal">log_autovacuum_min_duration</code>, <code class="literal">toast.log_autovacuum_min_duration</code> (<code class="type">integer</code>)
  936. <a id="id-1.9.3.85.6.3.4.19.1.4" class="indexterm"></a>
  937. </span></dt><dd><p>
  938. Per-table value for <a class="xref" href="runtime-config-autovacuum.html#GUC-LOG-AUTOVACUUM-MIN-DURATION">log_autovacuum_min_duration</a>
  939. parameter.
  940. </p></dd><dt id="RELOPTION-USER-CATALOG-TABLE"><span class="term"><code class="literal">user_catalog_table</code> (<code class="type">boolean</code>)
  941. <a id="id-1.9.3.85.6.3.4.20.1.3" class="indexterm"></a>
  942. </span></dt><dd><p>
  943. Declare the table as an additional catalog table for purposes of
  944. logical replication. See
  945. <a class="xref" href="logicaldecoding-output-plugin.html#LOGICALDECODING-CAPABILITIES" title="48.6.2. Capabilities">Section 48.6.2</a> for details.
  946. This parameter cannot be set for TOAST tables.
  947. </p></dd></dl></div></div></div><div class="refsect1" id="SQL-CREATETABLE-NOTES"><h2>Notes</h2><p>
  948. <span class="productname">PostgreSQL</span> automatically creates an
  949. index for each unique constraint and primary key constraint to
  950. enforce uniqueness. Thus, it is not necessary to create an
  951. index explicitly for primary key columns. (See <a class="xref" href="sql-createindex.html" title="CREATE INDEX"><span class="refentrytitle">CREATE INDEX</span></a> for more information.)
  952. </p><p>
  953. Unique constraints and primary keys are not inherited in the
  954. current implementation. This makes the combination of
  955. inheritance and unique constraints rather dysfunctional.
  956. </p><p>
  957. A table cannot have more than 1600 columns. (In practice, the
  958. effective limit is usually lower because of tuple-length constraints.)
  959. </p></div><div class="refsect1" id="SQL-CREATETABLE-EXAMPLES"><h2>Examples</h2><p>
  960. Create table <code class="structname">films</code> and table
  961. <code class="structname">distributors</code>:
  962. </p><pre class="programlisting">
  963. CREATE TABLE films (
  964. code char(5) CONSTRAINT firstkey PRIMARY KEY,
  965. title varchar(40) NOT NULL,
  966. did integer NOT NULL,
  967. date_prod date,
  968. kind varchar(10),
  969. len interval hour to minute
  970. );
  971. CREATE TABLE distributors (
  972. did integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
  973. name varchar(40) NOT NULL CHECK (name &lt;&gt; '')
  974. );
  975. </pre><p>
  976. </p><p>
  977. Create a table with a 2-dimensional array:
  978. </p><pre class="programlisting">
  979. CREATE TABLE array_int (
  980. vector int[][]
  981. );
  982. </pre><p>
  983. </p><p>
  984. Define a unique table constraint for the table
  985. <code class="literal">films</code>. Unique table constraints can be defined
  986. on one or more columns of the table:
  987. </p><pre class="programlisting">
  988. CREATE TABLE films (
  989. code char(5),
  990. title varchar(40),
  991. did integer,
  992. date_prod date,
  993. kind varchar(10),
  994. len interval hour to minute,
  995. CONSTRAINT production UNIQUE(date_prod)
  996. );
  997. </pre><p>
  998. </p><p>
  999. Define a check column constraint:
  1000. </p><pre class="programlisting">
  1001. CREATE TABLE distributors (
  1002. did integer CHECK (did &gt; 100),
  1003. name varchar(40)
  1004. );
  1005. </pre><p>
  1006. </p><p>
  1007. Define a check table constraint:
  1008. </p><pre class="programlisting">
  1009. CREATE TABLE distributors (
  1010. did integer,
  1011. name varchar(40),
  1012. CONSTRAINT con1 CHECK (did &gt; 100 AND name &lt;&gt; '')
  1013. );
  1014. </pre><p>
  1015. </p><p>
  1016. Define a primary key table constraint for the table
  1017. <code class="structname">films</code>:
  1018. </p><pre class="programlisting">
  1019. CREATE TABLE films (
  1020. code char(5),
  1021. title varchar(40),
  1022. did integer,
  1023. date_prod date,
  1024. kind varchar(10),
  1025. len interval hour to minute,
  1026. CONSTRAINT code_title PRIMARY KEY(code,title)
  1027. );
  1028. </pre><p>
  1029. </p><p>
  1030. Define a primary key constraint for table
  1031. <code class="structname">distributors</code>. The following two examples are
  1032. equivalent, the first using the table constraint syntax, the second
  1033. the column constraint syntax:
  1034. </p><pre class="programlisting">
  1035. CREATE TABLE distributors (
  1036. did integer,
  1037. name varchar(40),
  1038. PRIMARY KEY(did)
  1039. );
  1040. CREATE TABLE distributors (
  1041. did integer PRIMARY KEY,
  1042. name varchar(40)
  1043. );
  1044. </pre><p>
  1045. </p><p>
  1046. Assign a literal constant default value for the column
  1047. <code class="literal">name</code>, arrange for the default value of column
  1048. <code class="literal">did</code> to be generated by selecting the next value
  1049. of a sequence object, and make the default value of
  1050. <code class="literal">modtime</code> be the time at which the row is
  1051. inserted:
  1052. </p><pre class="programlisting">
  1053. CREATE TABLE distributors (
  1054. name varchar(40) DEFAULT 'Luso Films',
  1055. did integer DEFAULT nextval('distributors_serial'),
  1056. modtime timestamp DEFAULT current_timestamp
  1057. );
  1058. </pre><p>
  1059. </p><p>
  1060. Define two <code class="literal">NOT NULL</code> column constraints on the table
  1061. <code class="classname">distributors</code>, one of which is explicitly
  1062. given a name:
  1063. </p><pre class="programlisting">
  1064. CREATE TABLE distributors (
  1065. did integer CONSTRAINT no_null NOT NULL,
  1066. name varchar(40) NOT NULL
  1067. );
  1068. </pre><p>
  1069. </p><p>
  1070. Define a unique constraint for the <code class="literal">name</code> column:
  1071. </p><pre class="programlisting">
  1072. CREATE TABLE distributors (
  1073. did integer,
  1074. name varchar(40) UNIQUE
  1075. );
  1076. </pre><p>
  1077. The same, specified as a table constraint:
  1078. </p><pre class="programlisting">
  1079. CREATE TABLE distributors (
  1080. did integer,
  1081. name varchar(40),
  1082. UNIQUE(name)
  1083. );
  1084. </pre><p>
  1085. </p><p>
  1086. Create the same table, specifying 70% fill factor for both the table
  1087. and its unique index:
  1088. </p><pre class="programlisting">
  1089. CREATE TABLE distributors (
  1090. did integer,
  1091. name varchar(40),
  1092. UNIQUE(name) WITH (fillfactor=70)
  1093. )
  1094. WITH (fillfactor=70);
  1095. </pre><p>
  1096. </p><p>
  1097. Create table <code class="structname">circles</code> with an exclusion
  1098. constraint that prevents any two circles from overlapping:
  1099. </p><pre class="programlisting">
  1100. CREATE TABLE circles (
  1101. c circle,
  1102. EXCLUDE USING gist (c WITH &amp;&amp;)
  1103. );
  1104. </pre><p>
  1105. </p><p>
  1106. Create table <code class="structname">cinemas</code> in tablespace <code class="structname">diskvol1</code>:
  1107. </p><pre class="programlisting">
  1108. CREATE TABLE cinemas (
  1109. id serial,
  1110. name text,
  1111. location text
  1112. ) TABLESPACE diskvol1;
  1113. </pre><p>
  1114. </p><p>
  1115. Create a composite type and a typed table:
  1116. </p><pre class="programlisting">
  1117. CREATE TYPE employee_type AS (name text, salary numeric);
  1118. CREATE TABLE employees OF employee_type (
  1119. PRIMARY KEY (name),
  1120. salary WITH OPTIONS DEFAULT 1000
  1121. );
  1122. </pre><p>
  1123. Create a range partitioned table:
  1124. </p><pre class="programlisting">
  1125. CREATE TABLE measurement (
  1126. logdate date not null,
  1127. peaktemp int,
  1128. unitsales int
  1129. ) PARTITION BY RANGE (logdate);
  1130. </pre><p>
  1131. Create a range partitioned table with multiple columns in the partition key:
  1132. </p><pre class="programlisting">
  1133. CREATE TABLE measurement_year_month (
  1134. logdate date not null,
  1135. peaktemp int,
  1136. unitsales int
  1137. ) PARTITION BY RANGE (EXTRACT(YEAR FROM logdate), EXTRACT(MONTH FROM logdate));
  1138. </pre><p>
  1139. Create a list partitioned table:
  1140. </p><pre class="programlisting">
  1141. CREATE TABLE cities (
  1142. city_id bigserial not null,
  1143. name text not null,
  1144. population bigint
  1145. ) PARTITION BY LIST (left(lower(name), 1));
  1146. </pre><p>
  1147. Create a hash partitioned table:
  1148. </p><pre class="programlisting">
  1149. CREATE TABLE orders (
  1150. order_id bigint not null,
  1151. cust_id bigint not null,
  1152. status text
  1153. ) PARTITION BY HASH (order_id);
  1154. </pre><p>
  1155. Create partition of a range partitioned table:
  1156. </p><pre class="programlisting">
  1157. CREATE TABLE measurement_y2016m07
  1158. PARTITION OF measurement (
  1159. unitsales DEFAULT 0
  1160. ) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');
  1161. </pre><p>
  1162. Create a few partitions of a range partitioned table with multiple
  1163. columns in the partition key:
  1164. </p><pre class="programlisting">
  1165. CREATE TABLE measurement_ym_older
  1166. PARTITION OF measurement_year_month
  1167. FOR VALUES FROM (MINVALUE, MINVALUE) TO (2016, 11);
  1168. CREATE TABLE measurement_ym_y2016m11
  1169. PARTITION OF measurement_year_month
  1170. FOR VALUES FROM (2016, 11) TO (2016, 12);
  1171. CREATE TABLE measurement_ym_y2016m12
  1172. PARTITION OF measurement_year_month
  1173. FOR VALUES FROM (2016, 12) TO (2017, 01);
  1174. CREATE TABLE measurement_ym_y2017m01
  1175. PARTITION OF measurement_year_month
  1176. FOR VALUES FROM (2017, 01) TO (2017, 02);
  1177. </pre><p>
  1178. Create partition of a list partitioned table:
  1179. </p><pre class="programlisting">
  1180. CREATE TABLE cities_ab
  1181. PARTITION OF cities (
  1182. CONSTRAINT city_id_nonzero CHECK (city_id != 0)
  1183. ) FOR VALUES IN ('a', 'b');
  1184. </pre><p>
  1185. Create partition of a list partitioned table that is itself further
  1186. partitioned and then add a partition to it:
  1187. </p><pre class="programlisting">
  1188. CREATE TABLE cities_ab
  1189. PARTITION OF cities (
  1190. CONSTRAINT city_id_nonzero CHECK (city_id != 0)
  1191. ) FOR VALUES IN ('a', 'b') PARTITION BY RANGE (population);
  1192. CREATE TABLE cities_ab_10000_to_100000
  1193. PARTITION OF cities_ab FOR VALUES FROM (10000) TO (100000);
  1194. </pre><p>
  1195. Create partitions of a hash partitioned table:
  1196. </p><pre class="programlisting">
  1197. CREATE TABLE orders_p1 PARTITION OF orders
  1198. FOR VALUES WITH (MODULUS 4, REMAINDER 0);
  1199. CREATE TABLE orders_p2 PARTITION OF orders
  1200. FOR VALUES WITH (MODULUS 4, REMAINDER 1);
  1201. CREATE TABLE orders_p3 PARTITION OF orders
  1202. FOR VALUES WITH (MODULUS 4, REMAINDER 2);
  1203. CREATE TABLE orders_p4 PARTITION OF orders
  1204. FOR VALUES WITH (MODULUS 4, REMAINDER 3);
  1205. </pre><p>
  1206. Create a default partition:
  1207. </p><pre class="programlisting">
  1208. CREATE TABLE cities_partdef
  1209. PARTITION OF cities DEFAULT;
  1210. </pre></div><div class="refsect1" id="SQL-CREATETABLE-COMPATIBILITY"><h2>Compatibility</h2><p>
  1211. The <code class="command">CREATE TABLE</code> command conforms to the
  1212. <acronym class="acronym">SQL</acronym> standard, with exceptions listed below.
  1213. </p><div class="refsect2" id="id-1.9.3.85.9.3"><h3>Temporary Tables</h3><p>
  1214. Although the syntax of <code class="literal">CREATE TEMPORARY TABLE</code>
  1215. resembles that of the SQL standard, the effect is not the same. In the
  1216. standard,
  1217. temporary tables are defined just once and automatically exist (starting
  1218. with empty contents) in every session that needs them.
  1219. <span class="productname">PostgreSQL</span> instead
  1220. requires each session to issue its own <code class="literal">CREATE TEMPORARY
  1221. TABLE</code> command for each temporary table to be used. This allows
  1222. different sessions to use the same temporary table name for different
  1223. purposes, whereas the standard's approach constrains all instances of a
  1224. given temporary table name to have the same table structure.
  1225. </p><p>
  1226. The standard's definition of the behavior of temporary tables is
  1227. widely ignored. <span class="productname">PostgreSQL</span>'s behavior
  1228. on this point is similar to that of several other SQL databases.
  1229. </p><p>
  1230. The SQL standard also distinguishes between global and local temporary
  1231. tables, where a local temporary table has a separate set of contents for
  1232. each SQL module within each session, though its definition is still shared
  1233. across sessions. Since <span class="productname">PostgreSQL</span> does not
  1234. support SQL modules, this distinction is not relevant in
  1235. <span class="productname">PostgreSQL</span>.
  1236. </p><p>
  1237. For compatibility's sake, <span class="productname">PostgreSQL</span> will
  1238. accept the <code class="literal">GLOBAL</code> and <code class="literal">LOCAL</code> keywords
  1239. in a temporary table declaration, but they currently have no effect.
  1240. Use of these keywords is discouraged, since future versions of
  1241. <span class="productname">PostgreSQL</span> might adopt a more
  1242. standard-compliant interpretation of their meaning.
  1243. </p><p>
  1244. The <code class="literal">ON COMMIT</code> clause for temporary tables
  1245. also resembles the SQL standard, but has some differences.
  1246. If the <code class="literal">ON COMMIT</code> clause is omitted, SQL specifies that the
  1247. default behavior is <code class="literal">ON COMMIT DELETE ROWS</code>. However, the
  1248. default behavior in <span class="productname">PostgreSQL</span> is
  1249. <code class="literal">ON COMMIT PRESERVE ROWS</code>. The <code class="literal">ON COMMIT
  1250. DROP</code> option does not exist in SQL.
  1251. </p></div><div class="refsect2" id="id-1.9.3.85.9.4"><h3>Non-Deferred Uniqueness Constraints</h3><p>
  1252. When a <code class="literal">UNIQUE</code> or <code class="literal">PRIMARY KEY</code> constraint is
  1253. not deferrable, <span class="productname">PostgreSQL</span> checks for
  1254. uniqueness immediately whenever a row is inserted or modified.
  1255. The SQL standard says that uniqueness should be enforced only at
  1256. the end of the statement; this makes a difference when, for example,
  1257. a single command updates multiple key values. To obtain
  1258. standard-compliant behavior, declare the constraint as
  1259. <code class="literal">DEFERRABLE</code> but not deferred (i.e., <code class="literal">INITIALLY
  1260. IMMEDIATE</code>). Be aware that this can be significantly slower than
  1261. immediate uniqueness checking.
  1262. </p></div><div class="refsect2" id="id-1.9.3.85.9.5"><h3>Column Check Constraints</h3><p>
  1263. The SQL standard says that <code class="literal">CHECK</code> column constraints
  1264. can only refer to the column they apply to; only <code class="literal">CHECK</code>
  1265. table constraints can refer to multiple columns.
  1266. <span class="productname">PostgreSQL</span> does not enforce this
  1267. restriction; it treats column and table check constraints alike.
  1268. </p></div><div class="refsect2" id="id-1.9.3.85.9.6"><h3><code class="literal">EXCLUDE</code> Constraint</h3><p>
  1269. The <code class="literal">EXCLUDE</code> constraint type is a
  1270. <span class="productname">PostgreSQL</span> extension.
  1271. </p></div><div class="refsect2" id="id-1.9.3.85.9.7"><h3><code class="literal">NULL</code> <span class="quote">“<span class="quote">Constraint</span>”</span></h3><p>
  1272. The <code class="literal">NULL</code> <span class="quote">“<span class="quote">constraint</span>”</span> (actually a
  1273. non-constraint) is a <span class="productname">PostgreSQL</span>
  1274. extension to the SQL standard that is included for compatibility with some
  1275. other database systems (and for symmetry with the <code class="literal">NOT
  1276. NULL</code> constraint). Since it is the default for any
  1277. column, its presence is simply noise.
  1278. </p></div><div class="refsect2" id="id-1.9.3.85.9.8"><h3>Constraint Naming</h3><p>
  1279. The SQL standard says that table and domain constraints must have names
  1280. that are unique across the schema containing the table or domain.
  1281. <span class="productname">PostgreSQL</span> is laxer: it only requires
  1282. constraint names to be unique across the constraints attached to a
  1283. particular table or domain. However, this extra freedom does not exist
  1284. for index-based constraints (<code class="literal">UNIQUE</code>,
  1285. <code class="literal">PRIMARY KEY</code>, and <code class="literal">EXCLUDE</code>
  1286. constraints), because the associated index is named the same as the
  1287. constraint, and index names must be unique across all relations within
  1288. the same schema.
  1289. </p><p>
  1290. Currently, <span class="productname">PostgreSQL</span> does not record names
  1291. for <code class="literal">NOT NULL</code> constraints at all, so they are not
  1292. subject to the uniqueness restriction. This might change in a future
  1293. release.
  1294. </p></div><div class="refsect2" id="id-1.9.3.85.9.9"><h3>Inheritance</h3><p>
  1295. Multiple inheritance via the <code class="literal">INHERITS</code> clause is
  1296. a <span class="productname">PostgreSQL</span> language extension.
  1297. SQL:1999 and later define single inheritance using a
  1298. different syntax and different semantics. SQL:1999-style
  1299. inheritance is not yet supported by
  1300. <span class="productname">PostgreSQL</span>.
  1301. </p></div><div class="refsect2" id="id-1.9.3.85.9.10"><h3>Zero-Column Tables</h3><p>
  1302. <span class="productname">PostgreSQL</span> allows a table of no columns
  1303. to be created (for example, <code class="literal">CREATE TABLE foo();</code>). This
  1304. is an extension from the SQL standard, which does not allow zero-column
  1305. tables. Zero-column tables are not in themselves very useful, but
  1306. disallowing them creates odd special cases for <code class="command">ALTER TABLE
  1307. DROP COLUMN</code>, so it seems cleaner to ignore this spec restriction.
  1308. </p></div><div class="refsect2" id="id-1.9.3.85.9.11"><h3>Multiple Identity Columns</h3><p>
  1309. <span class="productname">PostgreSQL</span> allows a table to have more than one
  1310. identity column. The standard specifies that a table can have at most one
  1311. identity column. This is relaxed mainly to give more flexibility for
  1312. doing schema changes or migrations. Note that
  1313. the <code class="command">INSERT</code> command supports only one override clause
  1314. that applies to the entire statement, so having multiple identity columns
  1315. with different behaviors is not well supported.
  1316. </p></div><div class="refsect2" id="id-1.9.3.85.9.12"><h3>Generated Columns</h3><p>
  1317. The option <code class="literal">STORED</code> is not standard but is also used by
  1318. other SQL implementations. The SQL standard does not specify the storage
  1319. of generated columns.
  1320. </p></div><div class="refsect2" id="id-1.9.3.85.9.13"><h3><code class="literal">LIKE</code> Clause</h3><p>
  1321. While a <code class="literal">LIKE</code> clause exists in the SQL standard, many of the
  1322. options that <span class="productname">PostgreSQL</span> accepts for it are not
  1323. in the standard, and some of the standard's options are not implemented
  1324. by <span class="productname">PostgreSQL</span>.
  1325. </p></div><div class="refsect2" id="id-1.9.3.85.9.14"><h3><code class="literal">WITH</code> Clause</h3><p>
  1326. The <code class="literal">WITH</code> clause is a <span class="productname">PostgreSQL</span>
  1327. extension; storage parameters are not in the standard.
  1328. </p></div><div class="refsect2" id="id-1.9.3.85.9.15"><h3>Tablespaces</h3><p>
  1329. The <span class="productname">PostgreSQL</span> concept of tablespaces is not
  1330. part of the standard. Hence, the clauses <code class="literal">TABLESPACE</code>
  1331. and <code class="literal">USING INDEX TABLESPACE</code> are extensions.
  1332. </p></div><div class="refsect2" id="id-1.9.3.85.9.16"><h3>Typed Tables</h3><p>
  1333. Typed tables implement a subset of the SQL standard. According to
  1334. the standard, a typed table has columns corresponding to the
  1335. underlying composite type as well as one other column that is
  1336. the <span class="quote">“<span class="quote">self-referencing column</span>”</span>.
  1337. <span class="productname">PostgreSQL</span> does not support self-referencing
  1338. columns explicitly.
  1339. </p></div><div class="refsect2" id="id-1.9.3.85.9.17"><h3><code class="literal">PARTITION BY</code> Clause</h3><p>
  1340. The <code class="literal">PARTITION BY</code> clause is a
  1341. <span class="productname">PostgreSQL</span> extension.
  1342. </p></div><div class="refsect2" id="id-1.9.3.85.9.18"><h3><code class="literal">PARTITION OF</code> Clause</h3><p>
  1343. The <code class="literal">PARTITION OF</code> clause is a
  1344. <span class="productname">PostgreSQL</span> extension.
  1345. </p></div></div><div class="refsect1" id="id-1.9.3.85.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-altertable.html" title="ALTER TABLE"><span class="refentrytitle">ALTER TABLE</span></a>, <a class="xref" href="sql-droptable.html" title="DROP TABLE"><span class="refentrytitle">DROP TABLE</span></a>, <a class="xref" href="sql-createtableas.html" title="CREATE TABLE AS"><span class="refentrytitle">CREATE TABLE AS</span></a>, <a class="xref" href="sql-createtablespace.html" title="CREATE TABLESPACE"><span class="refentrytitle">CREATE TABLESPACE</span></a>, <a class="xref" href="sql-createtype.html" title="CREATE TYPE"><span class="refentrytitle">CREATE TYPE</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-createsubscription.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-createtableas.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE SUBSCRIPTION </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE TABLE AS</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1