Re: Double sequence increase on single insert with RULE on targeted table

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Sarunas Krisciukaitis <sarunask(at)lonus-tech(dot)com>
Cc: Tomas Zerolo <tomas(at)tuxteam(dot)de>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: Double sequence increase on single insert with RULE on targeted table
Date: 2005-11-16 16:26:49
Message-ID: 20051116162649.GA49742@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wed, Nov 16, 2005 at 10:31:10AM +0200, Sarunas Krisciukaitis wrote:
> I understand that RULES are like macros.
> Strangest thing here is that INSERT to test1 will touch only one
> sequence: test1_id_seq.
> And it increments test1_id_seq twice during insert with RULE.

Yes, that's a well-known effect of rewriting a query that includes
a call to nextval(). NEW.id in the rule doesn't refer to the value
that's inserted, but rather to the expression that's evaluated to
get that value. Since you didn't provide a value for id it gets
the default: nextval('test1_id_seq'). That expression is used in
both inserts, so the sequence gets incremented twice. See the
archives for numerous past discussions of this behavior.

--
Michael Fuhr

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Jeff Challender 2005-11-16 21:23:12 BUG #2047: Can't get to mirrors
Previous Message Tom Lane 2005-11-16 15:17:33 Re: Assigning null to an array element in plpgsql