Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 001371E62E83 for ; Wed, 25 Apr 2012 12:07:04 -0300 (ADT) Received: from gwmta.wicourts.gov ([165.219.244.99] helo=gw.wicourts.gov) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SN3o7-0008AF-SU for pgsql-bugs@postgresql.org; Wed, 25 Apr 2012 15:07:04 +0000 Received: from Courts-MTA by gw.wicourts.gov with Novell_GroupWise; Wed, 25 Apr 2012 10:06:51 -0500 Message-Id: <4F97CCB80200002500047332@gw.wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 8.0.2 Date: Wed, 25 Apr 2012 10:06:48 -0500 From: "Kevin Grittner" To: ,"Greg Stark" Cc: Subject: Re: BUG #6612: Functions can be called inside CHECK statements References: In-Reply-To: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Pg-Spam-Score: -4.2 (----) X-Archive-Number: 201204/141 X-Sequence-Number: 32892 Greg Stark wrote: > Only IMMUTABLE functions can be used in CHECK constraints. > It's a feature that expressions including subqueries are > automatically detected as not being immutable and automatically > barred. It doesn't look like that to me: test=# create function xxx() returns text volatile language plpgsql as $$ begin return 'xxx'; end; $$; CREATE FUNCTION test=# create table x (id int not null primary key, val text check (val <> xxx())); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "x_pkey" for table "x" CREATE TABLE test=# insert into x values (1, 'aaa'); INSERT 0 1 test=# insert into x values (2, 'xxx'); ERROR: new row for relation "x" violates check constraint "x_val_check" DETAIL: Failing row contains (2, xxx). Perhaps you're thinking of function usage in index definitions? 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. Otherwise I agree with your response -- this is clearly *not* a bug. -Kevin