I'm trying to use ON CONFLICT
on two columns where one can be null
. Unfortunatelly with partial index I don't seem to be able to do it.
create table tbl( col1 int, col2 int, col3 boolean);
CREATE UNIQUE INDEX ON tbl (col1, col2)
WHERE col2 IS NOT NULL; -- NOT NULL
CREATE UNIQUE INDEX ON tbl (col1, col2)
WHERE col2 IS NULL; -- NULL
INSERT INTO tbl(col1, col2)
values(1, 3)
ON CONFLICT(col1, col2) DO UPDATE SET col3 = true;
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
CREATE UNIQUE INDEX ON tbl (col1, col2) WHERE col2 IS NULL;
doesn't make sense.