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. <http://www.neadwerx.com>
2323 Cumberland Parkway · Suite 201 · Atlanta, GA 30339

"Quality is not an act, it is a habit." -- Aristotle

Reply via email to