After reviewing this answer, I have created the following custom operators:
CREATE OR REPLACE FUNCTION is_not_distinct_from(
ANYELEMENT,
ANYELEMENT
)
RETURNS BOOLEAN AS
$$
SELECT $1 IS NOT DISTINCT FROM $2;
$$
LANGUAGE sql
IMMUTABLE;
CREATE OPERATOR =!= (
PROCEDURE = is_not_distinct_from(anyelement,anyelement),
LEFTARG = anyelement,
RIGHTARG = anyelement,
COMMUTATOR = =!=,
NEGATOR = <!>
);
CREATE OR REPLACE FUNCTION is_distinct_from(
ANYELEMENT,
ANYELEMENT
)
RETURNS BOOLEAN AS
$$
SELECT $1 IS DISTINCT FROM $2;
$$
LANGUAGE sql
IMMUTABLE;
CREATE OPERATOR <!> (
PROCEDURE = is_distinct_from(anyelement,anyelement),
LEFTARG = anyelement,
RIGHTARG = anyelement,
COMMUTATOR = <!>,
NEGATOR = =!=
);
I have a table that allows two NULL
values, and I would like to prevent duplicate records, treating NULL
as a value.
CREATE TABLE "Foo" (
"FooID" SERIAL PRIMARY KEY,
"Foo" TEXT NOT NULL,
"Bar" TIMESTAMPTZ
"Baz" TIMESTAMPTZ
EXCLUDE ("Foo" WITH =, "Bar" WITH =!=, "Baz" WITH =!=)
);
I get the following error:
ERROR: operator =!=(anyelement,anyelement) is not a member of operator family "datetime_ops" DETAIL: The exclusion operator must be related to the index operator class for the constraint.
I have reviewed the documentation (here and here) but I'm having a hard time comprehending the material.
Additionally, this question could be considered a duplicate of this one; however, the problem in that question was one of compatibility with other RDBMS... this question is specifically addressing how to handle the error above.