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

Re: BUG #1175: insert rule action with defaults

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Joe Carlson" <joe(at)fruitfly(dot)org>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #1175: insert rule action with defaults
Date: 2004-06-22 02:14:53
Message-ID: 9196.1087870493@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-bugs
"PostgreSQL Bugs List" <pgsql-bugs(at)postgresql(dot)org> writes:
> [ double evaluation of serial-column default with ]
>
> create rule silly_insert_r as on insert to silly do
> insert into action (item_id,whence)
> values (new.id,'now'); 

The standard answer to this is "use a trigger, not a rule, to copy
inserted or updated data to another table".  A rule is basically a
macro and as such has the usual multiple-evaluation hazards that
all C programmers are familiar with :-(.  There are times when this
behavior is just what you want, but not when trying to copy the
results of volatile expressions.

People are often scared away from triggers because of the apparent
notational complexity.  This is too bad, because conceptually triggers
are *much* simpler than rules.  Someday I'd like to rewrite the docs
so that triggers are discussed first and made to look like the simpler
facility ...

			regards, tom lane

In response to

Responses

pgsql-bugs by date

Next:From: Michael MeskesDate: 2004-06-22 06:42:44
Subject: Re: BUG #1164: Informix compatibility ecpg
Previous:From: PostgreSQL Bugs ListDate: 2004-06-21 23:30:51
Subject: BUG #1175: insert rule action with defaults

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