Re: Help with exclusion constraint

From: Igor Neyman <ineyman(at)perceptron(dot)com>
To: Moshe Jacobson <moshe(at)neadwerx(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Help with exclusion constraint
Date: 2014-03-28 16:21:10
Message-ID: A76B25F2823E954C9E45E32FA49D70EC7A9C30FA@mail.corp.perceptron.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

From: pgsql-general-owner(at)postgresql(dot)org [mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Moshe Jacobson
Sent: Friday, March 28, 2014 10:31 AM
To: pgsql-general
Subject: [GENERAL] Help with exclusion constraint

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 );
..........................
..........................
..........................
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

For this:

"any given value of fk_col that there is a maximum of one row with bool_col = true."

why don't you (instead) create partial unique index:

CREATE UNIQUE INDEX on exclusion_example(fk_col, bool_col) WHERE bool_col IS TRUE;

Regards,
Igor Neyman

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message hari.fuchs 2014-03-28 16:45:16 Re: Help with exclusion constraint
Previous Message Tom Lane 2014-03-28 16:17:13 Re: The result of the last function call overwrites the result of previous function calls