Re: can this be done with a check expression? - Mailing list pgsql-sql

From Tom Lane
Subject Re: can this be done with a check expression?
Date
Msg-id 29693.1343949814@sss.pgh.pa.us
Whole thread Raw
In response to can this be done with a check expression?  (Wayne Cuddy <lists-pgsql@useunix.net>)
Responses Re: can this be done with a check expression?
List pgsql-sql
Wayne Cuddy <lists-pgsql@useunix.net> writes:
> I have a table with 3 columns:
> name text
> start_id integer
> end_id integer

> start_id and end_id are ranges which must not overlap but can have gaps
> between them. Is it possible to formulate a table check constraint that
> can verify that either id does not fall within an existing range at
> insert time? IE prevent overlaps during insert?

You can't do it reliably with a check constraint, at least not short of
taking table-wide locks to serialize all modifications of the table.
(If you were willing to do that, a check constraint calling a function
that does an EXISTS probe would work; although personally I'd use a
trigger instead.  Either way, performance is likely to suck.)

A less bogus way of doing things is to use an EXCLUDE constraint,
although that will restrict you to be running PG 9.0 or newer.  You
also need some way of representing the ranges as indexable objects.
In 9.0 or 9.1, probably the best way is to use contrib/seg/ to
represent the ranges as line segments.  9.2 will have a cleaner
solution, ie range types.
        regards, tom lane


pgsql-sql by date:

Previous
From: Wayne Cuddy
Date:
Subject: can this be done with a check expression?
Next
From: Andreas Kretschmer
Date:
Subject: Re: can this be done with a check expression?