Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 2865B1E62E83 for ; Wed, 25 Apr 2012 12:46:19 -0300 (ADT) Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SN4Q4-0000OG-U8 for pgsql-bugs@postgresql.org; Wed, 25 Apr 2012 15:46:19 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id q3PFk1CL006553; Wed, 25 Apr 2012 11:46:01 -0400 (EDT) To: "Kevin Grittner" cc: atrigent@ccs.neu.edu, "Greg Stark" , pgsql-bugs@postgresql.org Subject: Re: BUG #6612: Functions can be called inside CHECK statements In-reply-to: <4F97CCB80200002500047332@gw.wicourts.gov> References: <4F97CCB80200002500047332@gw.wicourts.gov> Comments: In-reply-to "Kevin Grittner" message dated "Wed, 25 Apr 2012 10:06:48 -0500" Date: Wed, 25 Apr 2012 11:46:01 -0400 Message-ID: <6552.1335368761@sss.pgh.pa.us> From: Tom Lane X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201204/143 X-Sequence-Number: 32894 "Kevin Grittner" writes: > Greg Stark wrote: >> Only IMMUTABLE functions can be used in CHECK constraints. > It doesn't look like that to me: No, we have never enforced that. IIRC the idea has been discussed, but we thought that adding the restriction would break too many existing applications. > A CHECK constraint using a volatile function is potentially valid > and useful, IMO. Think about a column which is supposed to record > the moment of an event which has occurred. It could make sense to > ensure that the timestamptz value is < now(); On the other hand, an > index entry based on now() is clearly a problem. This example is actually stable not volatile, but if for some reason you wanted to use clock_timestamp() then it would be volatile. Probably a more interesting question is whether it'd ever be sane to use a function with side-effects in a check constraint. I find it hard to visualize a case where it wouldn't be saner to put the actions in a trigger, but that doesn't mean someone else might not wish to do it. In practice, the times when check constraints are checked are predictable enough that you should be able to get away with abusing the system like that, if you wished. regards, tom lane