Re: Subqueries in Check() -- Still Intentionally Omitted?

From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Richard Broersma <richard(dot)broersma(at)gmail(dot)com>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Subqueries in Check() -- Still Intentionally Omitted?
Date: 2008-09-02 23:06:56
Message-ID: 1220396816.10936.17.camel@dell.linuxdev.us.dell.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Davis 2008-09-02 23:14:32 Re: Subqueries in Check() -- Still Intentionally Omitted?
Previous Message Tom Lane 2008-09-02 22:57:05 Re: Subqueries in Check() -- Still Intentionally Omitted?