Re: Double sequence increase on single insert with RULE on

From: "Jim C(dot) Nasby" <jnasby(at)pervasive(dot)com>
To: Sarunas Krisciukaitis <sarunask(at)lonus-tech(dot)com>
Cc: Michael Fuhr <mike(at)fuhr(dot)org>, Tomas Zerolo <tomas(at)tuxteam(dot)de>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: Double sequence increase on single insert with RULE on
Date: 2005-11-18 17:55:33
Message-ID: 20051118175533.GA19279@pervasive.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Is that safe or could lastval return the value of a previous insert if
an insert fails? Though I suppose if the insert fails then the rule
shouldn't get fired...

On Thu, Nov 17, 2005 at 10:11:29AM +0200, Sarunas Krisciukaitis wrote:
> Ok :) Then I found the solution in this partical case:
> CREATE RULE test1_on_insert AS ON INSERT TO test1 DO INSERT INTO
> test_log1 (qid) VALUES ( (SELECT lastval()) );
> With this rule all inserts are working as expected :)
>
> Thank you for you advise :)
>
> Sarunas
>
> Michael Fuhr wrote:
>
> >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.
> >
> >
> >
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
>

--
Jim C. Nasby, Sr. Engineering Consultant jnasby(at)pervasive(dot)com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2005-11-18 23:15:23 Re: Huge query stalls at PARSE/BIND stage (1)
Previous Message Alvaro Herrera 2005-11-18 17:29:26 Re: BUG #2047: Can't get to mirrors