Re: Subqueries in Check() -- Still Intentionally Omitted? - Mailing list pgsql-general

From Jeff Davis
Subject Re: Subqueries in Check() -- Still Intentionally Omitted?
Date
Msg-id 1220396816.10936.17.camel@dell.linuxdev.us.dell.com
Whole thread Raw
In response to Subqueries in Check() -- Still Intentionally Omitted?  ("Richard Broersma" <richard.broersma@gmail.com>)
Responses Re: Subqueries in Check() -- Still Intentionally Omitted?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
On Tue, 2008-09-02 at 15:30 -0700, Richard Broersma wrote:
> I am curious if the motivation is still valid for intentionally
> omitting check sub-queries. (what was the motivation to begin with?)
>
> Since we can effectively work around this limitation by doing the same
> thing with a function in a CHECK constraint, why would we want to

Wow, I assumed you needed an immutable function for that to work. Then I
tried it:

=> create table foo(i int check (random() > 0.5));

My question is not why don't we allow subqueries in CHECK, my question
is why do we allow stable/volatile functions?

As I understand it, CHECK is meant for simple declarative tuple
constraints. It's not designed for sophisticated inter-relation
constraints -- or even intra-relation constraints, for that matter.

Consider:

CREATE TABLE foo(
  ...
  CHECK ((SELECT COUNT(*) FROM foo) < 10)
);

We'd need some big locks for that to actually be a true declaration.

All of this can be solved with triggered procedures, where you can
define the locks as needed.

Regards,
    Jeff Davis


pgsql-general by date:

Previous
From: Joseph S
Date:
Subject: Index non-usage problem in 8.2.9
Next
From: Jeff Davis
Date:
Subject: Re: Subqueries in Check() -- Still Intentionally Omitted?