pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: paul cannon <pik@debian.org>
Cc: pgsql-sql@postgresql.org
Subject: Re: rule causes nextval() to be invoked twice
Date: Wed, 23 Jul 2003 00:41:55 -0400
Message-ID: <10284.1058935315@sss.pgh.pa.us> (raw)
In-Reply-To: <20030723014700.GE24912@fslc.usu.edu>
References: <20030723014700.GE24912@fslc.usu.edu>

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



view thread (5+ messages)  latest in thread

Message-ID: <10284.1058935315@sss.pgh.pa.us>
Permalink:  ../10284.1058935315@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/10284.1058935315@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pik@debian.org
  Subject: Re: rule causes nextval() to be invoked twice
  In-Reply-To: <10284.1058935315@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox