Re: BUG #4620: Unexpected(doc'd) side effects of using serial and rules

From: "Simon Keen" <simon(dot)keen(at)eglimited(dot)co(dot)uk>
To: "'Tom Lane'" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #4620: Unexpected(doc'd) side effects of using serial and rules
Date: 2009-01-19 09:08:45
Message-ID: B7644E056735419AB7E9F0A3F48C691F@grommit
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Many thanks. I guessed as much. It would be useful to add something to the
documentation to explain that functions used as defaults are re-written into
the sql and the implications of this if used with rules.

I did re-write it with a trigger but because statement level triggers do not
have access to NEW and OLD it means row level triggers and the performance
issues they bring with changes in large numbers of rows.

Cheers

Simon Keen

-----Original Message-----
From: Tom Lane [mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us]
Sent: 18 January 2009 16:35
To: Simon Keen
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: [BUGS] BUG #4620: Unexpected(doc'd) side effects of using
serial and rules

"Simon Keen" <simon(dot)keen(at)eglimited(dot)co(dot)uk> writes:
> I have used serial columns as primary keys in some tables. I have a
> rule that on insert to a table inserts in another table. However, the
> rule appears to cause the seq nextval() to be invoked multiple times
> thus destroying any value in NEW.serial_col.

This is a well-known issue arising from the fact that a rule is really a
macro: multiple references to any expression in the original statement
result in multiple evaluations of that expression.

By and large, the best way to propagate inserted information to other tables
is with a trigger.

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Magnus Hagander 2009-01-19 14:39:30 Re: [BUGS] BUG #4186: set lc_messages does not work
Previous Message Dave Page 2009-01-19 08:48:03 Re: postgres 8.4