Help with exclusion constraint - Mailing list pgsql-general

From Moshe Jacobson
Subject Help with exclusion constraint
Date
Msg-id CAJ4CxLn9F8TYZJm9bdAk6pnQRFO5des2zXmQaMa=_n-UggeuVQ@mail.gmail.com
Whole thread Raw
Responses Re: Help with exclusion constraint  (Igor Neyman <ineyman@perceptron.com>)
List pgsql-general

Take the following table:

CREATE TABLE exclusion_example AS
(    pk_col  integer primary key,    fk_col integer not null references other_table,    bool_col boolean not null
);

I want to ensure that for any given value of fk_col that there is a maximum of one row with bool_col = true. I wanted to write an exclusion constraint such as this:

alter table exclusion_example add exclude using btree ( fk_col with = , bool_col with and );

But this doesn’t work because “and” is not an operator.
So I created my own operator &&(bool, bool):

create function fn_boolean_and( bool, bool ) returns bool as$_$   select $1 and $2;$_$   language sql stable strict;

create operator &&
(   procedure = fn_boolean_and(bool, bool),   leftarg = bool,   rightarg = bool,   commutator = &&
);

But now when I try to create the exclusion constraint, I get the following:

mydb=# alter table exclusion_example add exclude using btree ( fk_col with = , bool_col with and );
ERROR:  operator &&(boolean,boolean) is not a member of operator family "bool_ops"
DETAIL:  The exclusion operator must be related to the index operator class for the constraint.

I’m not sure what to do about this. Any help would be appreciated.

Moshe Jacobson
Manager of Systems Engineering, Nead Werx Inc.
2323 Cumberland Parkway · Suite 201 · Atlanta, GA 30339

“Quality is not an act, it is a habit.” — Aristotle

pgsql-general by date:

Previous
From: Khangelani Gama
Date:
Subject: Re: Synchronizing a table that is in two different databases : Need to dump a table as inserts from db1 and change the insert statements into UPDATE statements
Next
From: Markella Skempri
Date:
Subject: Re: Synchronizing a table that is in two different databases : Need to dump a table as inserts from db1 and change the insert statements into UPDATE statements