RE: subselect in CHECK constraint?

From: "Hiroshi Inoue" <Inoue(at)tpf(dot)co(dot)jp>
To: "Ian Turner" <vectro(at)pipeline(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: RE: subselect in CHECK constraint?
Date: 2000-09-04 09:16:45
Message-ID: 000901c01650$d8df9520$2801007e@tpf.co.jp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> -----Original Message-----
> From: Ian Turner
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> When I try to do this:
>
> CREATE TABLE test (
> a Integer,
> b Integer,
> CHECK ((SELECT SUM(t.a) FROM test t WHERE t.b = b) < 1000)
> );
>
> INSERT INTO test (a, b) VALUES (100, 2);
>
> I get this error on the second query:
>
> ERROR: ExecEvalExpr: unknown expression type 108
>
> I'm guessing this means I can't do subselects in CHECK statements.
>

Yes.
It would be very difficult to implement constraints other than column
constraints. There seems to be 2 reasons at least.
1) We have to check the constraint not only for the row itself which is
about to be insert/update/deleted but also for other related rows.
As for your case,if b is updated the constraints not only for new b
but also for old b should be checked. If the WHERE clause is more
complicated what kind of check should we do ?
2) The implementation is very difficult without acquiring a table level
locking. As for your case I couldn't think of any standard way to
prevent the following other than acquiring a table level locking.

When there's no row which satisfies b = 2,two backends insert values
(500, 2) at the same time.

Regards.

Hiroshi Inoue

In response to

Browse pgsql-general by date

  From Date Subject
Next Message fabrizio.ermini 2000-09-04 09:27:31 Instability in copying large quantities of data
Previous Message Hiroshi Inoue 2000-09-04 09:11:52 RE: Updating cursors