Re: BUG #4684: lastval in function

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "andreas" <postgresql(at)elbrief(dot)de>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4684: lastval in function
Date: 2009-02-28 16:09:55
Message-ID: 10880.1235837395@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

"andreas" <postgresql(at)elbrief(dot)de> writes:
> select lastval() ;
> lastval
> ---------
> 2

> i expected lastval() should be 1, because this is the id from the
> insertstatement.

Well, you can't really rely on that when the statement you're executing
contains two different nextval() calls, as this does. The order of
evaluation of those calls is unspecified.

Personally I'm of the opinion that anyone who uses lastval() deserves to
lose, precisely because of the risk of this type of interaction. Use
currval() on one or the other of those sequences, and you'll be at least
a little bit safer. Even better is to use INSERT RETURNING or some
other alternative so that you can avoid currval() too.

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Heikki Linnakangas 2009-03-02 08:26:02 Re: BUG #4684: lastval in function
Previous Message andreas 2009-02-28 11:03:13 BUG #4684: lastval in function