Re: rule causes nextval() to be invoked twice

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: paul cannon <pik(at)debian(dot)org>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: rule causes nextval() to be invoked twice
Date: 2003-07-23 04:41:55
Message-ID: 10284.1058935315@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

paul cannon <pik(at)debian(dot)org> writes:
> I'm having trouble understanding the behavior of rules with regards to
> default values.
> ...
> If I remove the REFERENCES constraint, then I can see why. The insert
> made into main behaves as expected; it gets nextval('main_id_seq'),
> which comes out to 1. However, the main_insert rule gets _another_
> nextval('main_id_seq'), and the value 2 is inserted into othertable.

Yeah. Rules are macros, and as such have the usual issues about
multiple evaluations of arguments. Arguments with side effects are
bad news.

It looks to me like what you are trying to do is reflect a copy of an
inserted row into a log table. You'd be better advised to do this with
a trigger ... probably an AFTER trigger, so that you know exactly what
got inserted. (BEFORE triggers have to consider the possibility that
they're not the last BEFORE trigger.)

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message A.Bhuvaneswaran 2003-07-23 09:44:30 Re: rule causes nextval() to be invoked twice
Previous Message paul cannon 2003-07-23 02:08:53 Re: rule causes nextval() to be invoked twice