Skip to content

Commit f2a69b3

Browse files
committed
Doc: clarify that CREATE TABLE discards redundant unique constraints.
The SQL standard says that redundant unique constraints are disallowed, but we long ago decided that throwing an error would be too user-unfriendly, so we just drop redundant ones. The docs weren't very clear about that though, as this behavior was only explained for PRIMARY KEY vs UNIQUE, not UNIQUE vs UNIQUE. While here, I couldn't resist doing some copy-editing and markup-fixing on the adjacent text about INCLUDE options. Per bug #16767 from Matthias vd Meent. Discussion: https://postgr.es/m/16767-1714a2056ca516d0@postgresql.org
1 parent 9a26419 commit f2a69b3

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

doc/src/sgml/ref/create_table.sgml

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -878,15 +878,17 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
878878
<varlistentry>
879879
<term><literal>UNIQUE</literal> (column constraint)</term>
880880
<term><literal>UNIQUE ( <replaceable class="parameter">column_name</replaceable> [, ... ] )</literal>
881-
<optional> INCLUDE ( <replaceable class="parameter">column_name</replaceable> [, ...]) </optional> (table constraint)</term>
881+
<optional> <literal>INCLUDE ( <replaceable class="parameter">column_name</replaceable> [, ...])</literal> </optional> (table constraint)</term>
882882

883883
<listitem>
884884
<para>
885885
The <literal>UNIQUE</literal> constraint specifies that a
886886
group of one or more columns of a table can contain
887-
only unique values. The behavior of the unique table constraint
888-
is the same as that for column constraints, with the additional
889-
capability to span multiple columns.
887+
only unique values. The behavior of a unique table constraint
888+
is the same as that of a unique column constraint, with the
889+
additional capability to span multiple columns. The constraint
890+
therefore enforces that any two rows must differ in at least one
891+
of these columns.
890892
</para>
891893

892894
<para>
@@ -895,10 +897,10 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
895897
</para>
896898

897899
<para>
898-
Each unique table constraint must name a set of columns that is
900+
Each unique constraint should name a set of columns that is
899901
different from the set of columns named by any other unique or
900-
primary key constraint defined for the table. (Otherwise it
901-
would just be the same constraint listed twice.)
902+
primary key constraint defined for the table. (Otherwise, redundant
903+
unique constraints will be discarded.)
902904
</para>
903905

904906
<para>
@@ -911,10 +913,15 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
911913
<para>
912914
Adding a unique constraint will automatically create a unique btree
913915
index on the column or group of columns used in the constraint.
914-
The optional clause <literal>INCLUDE</literal> adds to that index
915-
one or more columns on which the uniqueness is not enforced.
916-
Note that although the constraint is not enforced on the included columns,
917-
it still depends on them. Consequently, some operations on these columns
916+
</para>
917+
918+
<para>
919+
The optional <literal>INCLUDE</literal> clause adds to that index
920+
one or more columns that are simply <quote>payload</quote>: uniqueness
921+
is not enforced on them, and the index cannot be searched on the basis
922+
of those columns. However they can be retrieved by an index-only scan.
923+
Note that although the constraint is not enforced on included columns,
924+
it still depends on them. Consequently, some operations on such columns
918925
(e.g., <literal>DROP COLUMN</literal>) can cause cascaded constraint and
919926
index deletion.
920927
</para>
@@ -924,7 +931,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
924931
<varlistentry>
925932
<term><literal>PRIMARY KEY</literal> (column constraint)</term>
926933
<term><literal>PRIMARY KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] )</literal>
927-
<optional> INCLUDE ( <replaceable class="parameter">column_name</replaceable> [, ...]) </optional> (table constraint)</term>
934+
<optional> <literal>INCLUDE ( <replaceable class="parameter">column_name</replaceable> [, ...])</literal> </optional> (table constraint)</term>
928935
<listitem>
929936
<para>
930937
The <literal>PRIMARY KEY</literal> constraint specifies that a column or
@@ -942,27 +949,34 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
942949

943950
<para>
944951
<literal>PRIMARY KEY</literal> enforces the same data constraints as
945-
a combination of <literal>UNIQUE</literal> and <literal>NOT NULL</literal>, but
952+
a combination of <literal>UNIQUE</literal> and <literal>NOT
953+
NULL</literal>. However,
946954
identifying a set of columns as the primary key also provides metadata
947955
about the design of the schema, since a primary key implies that other
948956
tables can rely on this set of columns as a unique identifier for rows.
949957
</para>
950958

951959
<para>
952-
<literal>PRIMARY KEY</literal> constraints share the restrictions that
953-
<literal>UNIQUE</literal> constraints have when placed on partitioned
954-
tables.
960+
When placed on a partitioned table, <literal>PRIMARY KEY</literal>
961+
constraints share the restrictions previously decribed
962+
for <literal>UNIQUE</literal> constraints.
955963
</para>
956964

957965
<para>
958966
Adding a <literal>PRIMARY KEY</literal> constraint will automatically
959967
create a unique btree index on the column or group of columns used in the
960-
constraint. The optional <literal>INCLUDE</literal> clause allows a list
961-
of columns to be specified which will be included in the non-key portion
962-
of the index. Although uniqueness is not enforced on the included columns,
963-
the constraint still depends on them. Consequently, some operations on the
964-
included columns (e.g., <literal>DROP COLUMN</literal>) can cause cascaded
965-
constraint and index deletion.
968+
constraint.
969+
</para>
970+
971+
<para>
972+
The optional <literal>INCLUDE</literal> clause adds to that index
973+
one or more columns that are simply <quote>payload</quote>: uniqueness
974+
is not enforced on them, and the index cannot be searched on the basis
975+
of those columns. However they can be retrieved by an index-only scan.
976+
Note that although the constraint is not enforced on included columns,
977+
it still depends on them. Consequently, some operations on such columns
978+
(e.g., <literal>DROP COLUMN</literal>) can cause cascaded constraint and
979+
index deletion.
966980
</para>
967981
</listitem>
968982
</varlistentry>

0 commit comments

Comments
 (0)