Re: subselect in CHECK constraint? - Mailing list pgsql-general

From Tom Lane
Subject Re: subselect in CHECK constraint?
Date
Msg-id 14071.968083196@sss.pgh.pa.us
Whole thread Raw
In response to subselect in CHECK constraint?  (Ian Turner <vectro@pipeline.com>)
Responses Re: subselect in CHECK constraint?
List pgsql-general
Ian Turner <vectro@pipeline.com> writes:
> I'm guessing this means I can't do subselects in CHECK statements.

Right.  Pushing the SELECT into a SQL or PLPGSQL function that's called
by the constraint is a good workaround, ie

    CHECK (testconstraint(a, b))

where FUNCTION testconstraint(a int, b int) RETURNS bool does all the
heavy lifting.  If you use plpgsql there should be a performance
advantage too --- the query plan for the function will be cached for
re-use across calls, which is not true for the text of CHECK conditions.

            regards, tom lane

pgsql-general by date:

Previous
From: Travis Bauer
Date:
Subject: Change to DatabaseMetaData.java for the jdbc2 driver (fwd)
Next
From: "Enrico Comini"
Date:
Subject: JDBC