Re: rule causes nextval() to be invoked twice - Mailing list pgsql-sql

From Tom Lane
Subject Re: rule causes nextval() to be invoked twice
Date
Msg-id 10284.1058935315@sss.pgh.pa.us
Whole thread Raw
In response to rule causes nextval() to be invoked twice  (paul cannon <pik@debian.org>)
List pgsql-sql
paul cannon <pik@debian.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


pgsql-sql by date:

Previous
From: paul cannon
Date:
Subject: Re: rule causes nextval() to be invoked twice
Next
From: "A.Bhuvaneswaran"
Date:
Subject: Re: rule causes nextval() to be invoked twice