Re: Range constraint with unique index

From: "Kevin Grittner" <kgrittn(at)mail(dot)com>
To: "Steven Plummer" <splummer(at)mttltd(dot)com>,pgsql-general(at)postgresql(dot)org
Subject: Re: Range constraint with unique index
Date: 2012-11-08 22:08:16
Message-ID: 20121108220817.77870@gmx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Steven Plummer wrote:

> I have a table that has an integer and a int8range.

> What I want is to add a constraint that stops anyone adding

Maybe you want an exclusion constraint:

http://www.postgresql.org/docs/9.0/static/ddl-constraints.html#DDL-CONSTRAINTS-EXCLUSION

For example:

CREATE EXTENSION btree_gist ;
CREATE TABLE t1 (c1 INT NOT NULL,
                c2 int8range NOT NULL,
                EXCLUDE USING gist (c1 WITH =, c2 WITH &&));
INSERT INTO t1 VALUES (1, '[1,100]');

Now try to insert an overlapping row:

INSERT INTO t1 VALUES (1, '[51,150]');

-Kevin

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2012-11-08 23:29:28 Re: Does PostgreSQL have complete functional test cases?
Previous Message David Boreham 2012-11-08 21:11:41 Re: Message: incomplete startup packet