Skip site navigation (1) Skip section navigation (2)

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: (view raw, whole thread or download thread mbox)
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

pgsql-bugs by date

Next:From: Heikki LinnakangasDate: 2009-03-02 08:26:02
Subject: Re: BUG #4684: lastval in function
Previous:From: andreasDate: 2009-02-28 11:03:13
Subject: BUG #4684: lastval in function

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group