Help with exclusion constraint

From: Moshe Jacobson <moshe(at)neadwerx(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Help with exclusion constraint
Date: 2014-03-28 14:30:52
Message-ID: CAJ4CxLn9F8TYZJm9bdAk6pnQRFO5des2zXmQaMa=_n-UggeuVQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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. <http://www.neadwerx.com>
2323 Cumberland Parkway · Suite 201 · Atlanta, GA 30339

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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Markella Skempri 2014-03-28 16:03:00 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
Previous Message Adrian Klaver 2014-03-28 13:52:03 Re: [GENERAL] openvz and shared memory trouble