diff options
author | Peter Eisentraut <peter@eisentraut.org> | 2022-02-03 11:29:54 +0100 |
---|---|---|
committer | Peter Eisentraut <peter@eisentraut.org> | 2022-02-03 11:48:21 +0100 |
commit | 94aa7cc5f707712f592885995a28e018c7c80488 (patch) | |
tree | d80b1f0a82a43a60e29d9b4960bcff884cdf0183 /src/backend/access/nbtree/nbtinsert.c | |
parent | f862d57057fdc73e663fe09d8948ed06b1b71dd7 (diff) |
Add UNIQUE null treatment option
The SQL standard has been ambiguous about whether null values in
unique constraints should be considered equal or not. Different
implementations have different behaviors. In the SQL:202x draft, this
has been formalized by making this implementation-defined and adding
an option on unique constraint definitions UNIQUE [ NULLS [NOT]
DISTINCT ] to choose a behavior explicitly.
This patch adds this option to PostgreSQL. The default behavior
remains UNIQUE NULLS DISTINCT. Making this happen in the btree code
is pretty easy; most of the patch is just to carry the flag around to
all the places that need it.
The CREATE UNIQUE INDEX syntax extension is not from the standard,
it's my own invention.
I named all the internal flags, catalog columns, etc. in the negative
("nulls not distinct") so that the default PostgreSQL behavior is the
default if the flag is false.
Reviewed-by: Maxim Orlov <orlovmg@gmail.com>
Reviewed-by: Pavel Borisov <pashkin.elfe@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/84e5ee1b-387e-9a54-c326-9082674bde78@enterprisedb.com
Diffstat (limited to 'src/backend/access/nbtree/nbtinsert.c')
-rw-r--r-- | src/backend/access/nbtree/nbtinsert.c | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c index 62746c47219..68628ec000d 100644 --- a/src/backend/access/nbtree/nbtinsert.c +++ b/src/backend/access/nbtree/nbtinsert.c @@ -398,9 +398,9 @@ _bt_search_insert(Relation rel, BTInsertState insertstate) * _bt_findinsertloc() to reuse most of the binary search work we do * here. * - * Do not call here when there are NULL values in scan key. NULL should be - * considered unequal to NULL when checking for duplicates, but we are not - * prepared to handle that correctly. + * This code treats NULLs as equal, unlike the default semantics for unique + * indexes. So do not call here when there are NULL values in scan key and + * the index uses the default NULLS DISTINCT mode. */ static TransactionId _bt_check_unique(Relation rel, BTInsertState insertstate, Relation heapRel, |