@@ -78,8 +78,8 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
78
78
[ CONSTRAINT <replaceable class="parameter">constraint_name</replaceable> ]
79
79
{ CHECK ( <replaceable class="parameter">expression</replaceable> ) [ NO INHERIT ] |
80
80
NOT NULL <replaceable class="parameter">column_name</replaceable> [ NO INHERIT ] |
81
- UNIQUE [ NULLS [ NOT ] DISTINCT ] ( <replaceable class="parameter">column_name</replaceable> [, ... ] ) <replaceable class="parameter">index_parameters</replaceable> |
82
- PRIMARY KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] ) <replaceable class="parameter">index_parameters</replaceable> |
81
+ UNIQUE [ NULLS [ NOT ] DISTINCT ] ( <replaceable class="parameter">column_name</replaceable> [, ... ] [, <replaceable class="parameter">column_name</replaceable> WITHOUT OVERLAPS ] ) <replaceable class="parameter">index_parameters</replaceable> |
82
+ PRIMARY KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] [, <replaceable class="parameter">column_name</replaceable> WITHOUT OVERLAPS ] ) <replaceable class="parameter">index_parameters</replaceable> |
83
83
EXCLUDE [ USING <replaceable class="parameter">index_method</replaceable> ] ( <replaceable class="parameter">exclude_element</replaceable> WITH <replaceable class="parameter">operator</replaceable> [, ... ] ) <replaceable class="parameter">index_parameters</replaceable> [ WHERE ( <replaceable class="parameter">predicate</replaceable> ) ] |
84
84
FOREIGN KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] ) REFERENCES <replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> [, ... ] ) ]
85
85
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE <replaceable
@@ -965,7 +965,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
965
965
966
966
<varlistentry id="sql-createtable-parms-unique">
967
967
<term><literal>UNIQUE [ NULLS [ NOT ] DISTINCT ]</literal> (column constraint)</term>
968
- <term><literal>UNIQUE [ NULLS [ NOT ] DISTINCT ] ( <replaceable class="parameter">column_name</replaceable> [, ... ] )</literal>
968
+ <term><literal>UNIQUE [ NULLS [ NOT ] DISTINCT ] ( <replaceable class="parameter">column_name</replaceable> [, ... ] [, <replaceable class="parameter">column_name</replaceable> WITHOUT OVERLAPS ] )</literal>
969
969
<optional> <literal>INCLUDE ( <replaceable class="parameter">column_name</replaceable> [, ...])</literal> </optional> (table constraint)</term>
970
970
971
971
<listitem>
@@ -979,6 +979,30 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
979
979
of these columns.
980
980
</para>
981
981
982
+ <para>
983
+ If the <literal>WITHOUT OVERLAPS</literal> option is specified for the
984
+ last column, then that column is checked for overlaps instead of
985
+ equality. In that case, the other columns of the constraint will allow
986
+ duplicates so long as the duplicates don't overlap in the
987
+ <literal>WITHOUT OVERLAPS</literal> column. (This is sometimes called a
988
+ temporal key, if the column is a range of dates or timestamps, but
989
+ PostgreSQL allows ranges over any base type.) In effect, such a
990
+ constraint is enforced with an <literal>EXCLUDE</literal> constraint
991
+ rather than a <literal>UNIQUE</literal> constraint. So for example
992
+ <literal>UNIQUE (id, valid_at WITHOUT OVERLAPS)</literal> behaves like
993
+ <literal>EXCLUDE USING GIST (id WITH =, valid_at WITH
994
+ &&)</literal>. The <literal>WITHOUT OVERLAPS</literal> column
995
+ must have a range or multirange type. (Technically, any type is allowed
996
+ whose default GiST opclass includes an overlaps operator. See the
997
+ <literal>stratnum</literal> support function under <xref
998
+ linkend="gist-extensibility"/> for details.) The non-<literal>WITHOUT
999
+ OVERLAPS</literal> columns of the constraint can be any type that can be
1000
+ compared for equality in a GiST index. By default, only range types are
1001
+ supported, but you can use other types by adding the <xref
1002
+ linkend="btree-gist"/> extension (which is the expected way to use this
1003
+ feature).
1004
+ </para>
1005
+
982
1006
<para>
983
1007
For the purpose of a unique constraint, null values are not
984
1008
considered equal, unless <literal>NULLS NOT DISTINCT</literal> is
@@ -1000,8 +1024,10 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
1000
1024
</para>
1001
1025
1002
1026
<para>
1003
- Adding a unique constraint will automatically create a unique btree
1004
- index on the column or group of columns used in the constraint.
1027
+ Adding a unique constraint will automatically create a unique B-tree
1028
+ index on the column or group of columns used in the constraint. But if
1029
+ the constraint includes a <literal>WITHOUT OVERLAPS</literal> clause, it
1030
+ will use a GiST index.
1005
1031
</para>
1006
1032
1007
1033
<para>
@@ -1019,7 +1045,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
1019
1045
1020
1046
<varlistentry id="sql-createtable-parms-primary-key">
1021
1047
<term><literal>PRIMARY KEY</literal> (column constraint)</term>
1022
- <term><literal>PRIMARY KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] )</literal>
1048
+ <term><literal>PRIMARY KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] [, <replaceable class="parameter">column_name</replaceable> WITHOUT OVERLAPS ] )</literal>
1023
1049
<optional> <literal>INCLUDE ( <replaceable class="parameter">column_name</replaceable> [, ...])</literal> </optional> (table constraint)</term>
1024
1050
<listitem>
1025
1051
<para>
@@ -1052,9 +1078,10 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
1052
1078
</para>
1053
1079
1054
1080
<para>
1055
- Adding a <literal>PRIMARY KEY</literal> constraint will automatically
1056
- create a unique btree index on the column or group of columns used in the
1057
- constraint.
1081
+ As with a <literal>UNIQUE</literal> constraint, adding a
1082
+ <literal>PRIMARY KEY</literal> constraint will automatically create a
1083
+ unique B-tree index, or GiST if <literal>WITHOUT OVERLAPS</literal> was
1084
+ specified, on the column or group of columns used in the constraint.
1058
1085
</para>
1059
1086
1060
1087
<para>
0 commit comments