Skip site navigation (1) Skip section navigation (2)

Re: BUG #6612: Functions can be called inside CHECK statements

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: <atrigent(at)ccs(dot)neu(dot)edu>,"Greg Stark" <stark(at)mit(dot)edu>
Cc: <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #6612: Functions can be called inside CHECK statements
Date: 2012-04-25 15:06:48
Message-ID: (view raw or flat)
Lists: pgsql-bugs
Greg Stark <stark(at)mit(dot)edu> 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; $$;
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"
test=# insert into x values (1, 'aaa');
test=# insert into x values (2, 'xxx');
ERROR:  new row for relation "x" violates check constraint
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.

In response to


pgsql-bugs by date

Next:From: Kevin GrittnerDate: 2012-04-25 15:10:05
Subject: Re: Broken Pipe Error
Previous:From: Greg StarkDate: 2012-04-25 14:57:08
Subject: Re: BUG #6612: Functions can be called inside CHECK statements

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group