Re: constraint and ordered value

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: David Garamond <lists(at)zara(dot)6(dot)isreserved(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: constraint and ordered value
Date: 2005-12-29 07:16:25
Message-ID: 20051229071625.GA2815@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Dec 28, 2005 at 00:52:18 +0700,
David Garamond <lists(at)zara(dot)6(dot)isreserved(dot)com> wrote:
> Is it possible to use only CHECK constraint (and not triggers) to
> completely enforce ordered value of a column (colx) in a table? By that
> I mean:
>
> 1. Rows must be inserted in the order of colx=1, then colx=2, 3, and so on;
>
> 2. When deleting (or updating), "holes" must not be formed, e.g. if
> there are three rows then row with colx=3 must be the first one deleted,
> and then colx=2 the second, and so on.
>
> I can see #1 being accomplished using a NOT NULL + UNIQUE constraint and
> a CHECK constraint that calls some PL function where the function does a
> simple checking (new.colx=1 if COUNT(colx)==0, or new.colx=MAX(colx)+1).
>
> But is it possible to do #2 using only constraints?

No. A constraint only applies to one row at a time. If you try to work around
this by calling a function that does queries it isn't guarenteed to work.
And if you are thinking of calling a function that does a query, you aren't
looking at saving time over using triggers.

Also, if you are going to have concurrent updates, you are going to need to
do table locking to make this work.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Daryl Richter 2005-12-29 12:15:18 Re: constraint and ordered value
Previous Message Tom Lane 2005-12-29 03:03:19 Re: DISTINCT with NULLs and INT fields