Add UNIQUE null treatment option

Peter Eisentraut <peter@eisentraut.org>

Commit: 94aa7cc5f707712f592885995a28e018c7c80488
Author: Peter Eisentraut <peter@eisentraut.org>
Date: 2022-02-03T10:48:21Z
Releases: 15.0
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

Files

PathChange+/−
doc/src/sgml/catalogs.sgml modified +13 −0
doc/src/sgml/ddl.sgml modified +24 −5
doc/src/sgml/information_schema.sgml modified +12 −0
doc/src/sgml/ref/alter_table.sgml modified +2 −2
doc/src/sgml/ref/create_index.sgml modified +13 −0
doc/src/sgml/ref/create_table.sgml modified +6 −5
src/backend/access/nbtree/nbtinsert.c modified +3 −3
src/backend/access/nbtree/nbtsort.c modified +13 −2
src/backend/access/nbtree/nbtutils.c modified +7 −0
src/backend/catalog/index.c modified +7 −0
src/backend/catalog/information_schema.sql modified +7 −2
src/backend/catalog/sql_features.txt modified +1 −0
src/backend/catalog/toasting.c modified +1 −0
src/backend/commands/indexcmds.c modified +2 −1
src/backend/nodes/copyfuncs.c modified +2 −0
src/backend/nodes/equalfuncs.c modified +2 −0
src/backend/nodes/makefuncs.c modified +2 −1
src/backend/nodes/outfuncs.c modified +2 −0
src/backend/parser/gram.y modified +29 −18
src/backend/parser/parse_utilcmd.c modified +3 −0
src/backend/utils/adt/ruleutils.c modified +16 −7
src/backend/utils/cache/relcache.c modified +1 −0
src/backend/utils/sort/tuplesort.c modified +6 −2
src/bin/pg_dump/pg_dump.c modified +16 −3
src/bin/pg_dump/pg_dump.h modified +1 −0
src/bin/psql/describe.c modified +15 −4
src/include/catalog/catversion.h modified +1 −1
src/include/catalog/pg_index.h modified +1 −0
src/include/nodes/execnodes.h modified +1 −0
src/include/nodes/makefuncs.h modified +1 −1
src/include/nodes/parsenodes.h modified +2 −0
src/include/utils/tuplesort.h modified +1 −0
src/test/regress/expected/constraints.out modified +23 −0
src/test/regress/expected/create_index.out modified +61 −0
src/test/regress/sql/constraints.sql modified +14 −0
src/test/regress/sql/create_index.sql modified +37 −0

Documentation touched

Discussion