Re: functions allowed in CHECK constraints

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: itdev(at)itdevel(dot)internal(dot)net, pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: functions allowed in CHECK constraints
Date: 2018-02-09 14:32:16
Message-ID: 1002.1518186736@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"David G. Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> writes:
> On Fri, Feb 9, 2018 at 1:37 AM, Achilleas Mantzios <
> itdev(at)itdevel(dot)internal(dot)net> wrote:
>> So, what's the point in forbidding the use of subselects if one can use
>> functions? And OTOH if effectively doing so is bad for some reason, why let
>> it happen with a function?

> Short answer, the system treats the function as a black-box and doesn't
> know that you've used it to circumvent its prohibition on subselects. The
> prohibition still is in place though. If you use a function you are
> expected to ensure you do not violate any of the restrictions yourself.

Actually, I'd say that the possibility to use a function is a workaround
for the fact that you can't write a sub-select directly ;-). The latter
is more of an implementation restriction than anything else, stemming
from the fact that we don't run CHECK expressions through the planner.

Now, it's certainly true that you can use a sub-select to write a
non-immutable CHECK expression and thereby shoot yourself in the foot.
But you can shoot yourself in the foot that way without sub-selects,
too. We do not try to enforce that CHECK expressions are immutable.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Lahari Sengupta 2018-02-12 12:05:04 Conditional update
Previous Message David G. Johnston 2018-02-09 14:06:31 Re: functions allowed in CHECK constraints