RE: unique index with several columns - Mailing list pgsql-sql

From Klaus Kaisersberger
Subject RE: unique index with several columns
Date
Msg-id PAXP193MB2058AB2DF5D9A24C32C726E9E3059@PAXP193MB2058.EURP193.PROD.OUTLOOK.COM
Whole thread Raw
In response to RE: unique index with several columns  (Klaus Kaisersberger <klaus.kaisersberger@outlook.com>)
List pgsql-sql
Here a usable example for an arbitrary number of columns (might require casting the columns to e.g. bit varying before
makingthe arrays):
 
Just replace c1,c2 with your (casted) number of columns on index creation.

create function fn(cs anyelement)
    returns int
    as
        'select count(1) from unnest(cs) t(c) where c is not null;'
    language sql
    immutable;


create unique index idx on t ((
    case
        when fn(ARRAY[c1,c2])=0 then null
        else ARRAY[c1,c2]
    end
));


Result:

db=# insert into t(c1,c2) values (null,null);
INSERT 0 1
db=# insert into t(c1,c2) values (null,null);
INSERT 0 1
db=# insert into t(c1,c2) values (null,null);
INSERT 0 1
db=# insert into t(c1,c2) values (1,null);
INSERT 0 1

db=# insert into t(c1,c2) values (1,null);
ERROR:  duplicate key value violates unique constraint "idx"
DETAIL:  Key ((
CASE
    WHEN fn(ARRAY[c1, c2]) = 0 THEN NULL::integer[]
    ELSE ARRAY[c1, c2]
END))=({1,NULL}) already exists.

db=# insert into t(c1,c2) values (null,1);
INSERT 0 1

db=# insert into t(c1,c2) values (null,1);
ERROR:  duplicate key value violates unique constraint "idx"
DETAIL:  Key ((
CASE
    WHEN fn(ARRAY[c1, c2]) = 0 THEN NULL::integer[]
    ELSE ARRAY[c1, c2]
END))=({NULL,1}) already exists.
db=# insert into t(c1,c2) values (null,2);
INSERT 0 1

db=# insert into t(c1,c2) values (null,2);
ERROR:  duplicate key value violates unique constraint "idx"
DETAIL:  Key ((
CASE
    WHEN fn(ARRAY[c1, c2]) = 0 THEN NULL::integer[]
    ELSE ARRAY[c1, c2]
END))=({NULL,2}) already exists.

pgsql-sql by date:

Previous
From: Klaus Kaisersberger
Date:
Subject: RE: unique index with several columns
Next
From: Alexey M Boltenkov
Date:
Subject: Re: unique index with several columns