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/utils/adt/ruleutils.c | |
parent | f862d57057fdc73e663fe09d8948ed06b1b71dd7 (diff) | |
download | postgresql-94aa7cc5f707712f592885995a28e018c7c80488.tar.gz postgresql-94aa7cc5f707712f592885995a28e018c7c80488.zip |
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/utils/adt/ruleutils.c')
-rw-r--r-- | src/backend/utils/adt/ruleutils.c | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 039b1d2b951..b16526e65e9 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -1444,6 +1444,9 @@ pg_get_indexdef_worker(Oid indexrelid, int colno, { appendStringInfoChar(&buf, ')'); + if (idxrec->indnullsnotdistinct) + appendStringInfo(&buf, " NULLS NOT DISTINCT"); + /* * If it has options, append "WITH (options)" */ @@ -2312,9 +2315,20 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, /* Start off the constraint definition */ if (conForm->contype == CONSTRAINT_PRIMARY) - appendStringInfoString(&buf, "PRIMARY KEY ("); + appendStringInfoString(&buf, "PRIMARY KEY "); else - appendStringInfoString(&buf, "UNIQUE ("); + appendStringInfoString(&buf, "UNIQUE "); + + indexId = conForm->conindid; + + indtup = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId)); + if (!HeapTupleIsValid(indtup)) + elog(ERROR, "cache lookup failed for index %u", indexId); + if (conForm->contype == CONSTRAINT_UNIQUE && + ((Form_pg_index) GETSTRUCT(indtup))->indnullsnotdistinct) + appendStringInfoString(&buf, "NULLS NOT DISTINCT "); + + appendStringInfoString(&buf, "("); /* Fetch and build target column list */ val = SysCacheGetAttr(CONSTROID, tup, @@ -2327,12 +2341,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, appendStringInfoChar(&buf, ')'); - indexId = conForm->conindid; - /* Build including column list (from pg_index.indkeys) */ - indtup = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId)); - if (!HeapTupleIsValid(indtup)) - elog(ERROR, "cache lookup failed for index %u", indexId); val = SysCacheGetAttr(INDEXRELID, indtup, Anum_pg_index_indnatts, &isnull); if (isnull) |