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

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search archives
  Advanced Search

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: Sat, 28 Feb 2009 11:09:55 -0500
  • Message-id: <10880.1235837395@sss.pgh.pa.us> <text/plain>

"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



Home | Main Index | Thread Index

Privacy Policy | About PostgreSQL
Copyright © 1996 – 2012 PostgreSQL Global Development Group