Re: EXCLUDE constraint with not equals

From: Kai Groner <kai(at)gronr(dot)com>
To: emre(at)hasegeli(dot)com
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: EXCLUDE constraint with not equals
Date: 2015-03-03 16:15:43
Message-ID: CALiRuxm22LXHo2YjQ7HEWsEmWeSSc=xg1Kr8ZYo83=LzuKy3Rg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Mar 3, 2015 at 3:27 AM, Emre Hasegeli <emre(at)hasegeli(dot)com> wrote:
>
> > Given the following table, I would like to ensure that all the rows for
an
> > email that have a user defined map to the same user.
> >
> > CREATE TABLE person (
> > id INTEGER PRIMARY KEY,
> > user TEXT,
> > email TEXT NOT NULL);
>
> You can use the btree_gist extension from contrib:
>
> CREATE EXTENSION btree_gist;
>
> CREATE TABLE person (
> id INTEGER PRIMARY KEY,
> "user" TEXT,
> email TEXT NOT NULL,
> EXCLUDE USING gist (email WITH =, "user" WITH <>)
> WHERE ("user" IS NOT NULL));

Thanks, Emre. The btree_gist extension seems to be just what I was looking
for.

I found it necessary to add the gist_text_ops opclass for the inequality:

CREATE TABLE person (
id INTEGER PRIMARY KEY,
"user" TEXT,
email TEXT NOT NULL,
EXCLUDE USING gist (email WITH =, "user" gist_text_ops WITH <>)
WHERE ("user" IS NOT NULL));

Is that expected?

Kai

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message gmb 2015-03-03 16:24:35 Re: Performance on DISABLE TRIGGER
Previous Message Adrian Klaver 2015-03-03 15:44:48 Re: Copy Data between different databases