Re: BUG #4684: lastval in function - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #4684: lastval in function
Date
Msg-id 10880.1235837395@sss.pgh.pa.us
Whole thread Raw
In response to BUG #4684: lastval in function  ("andreas" <postgresql@elbrief.de>)
List pgsql-bugs
"andreas" <postgresql@elbrief.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

pgsql-bugs by date:

Previous
From: "andreas"
Date:
Subject: BUG #4684: lastval in function
Next
From: Heikki Linnakangas
Date:
Subject: Re: BUG #4684: lastval in function