Re: serial type vs. sequences

From: Jan Wieck <JanWieck(at)Yahoo(dot)com>
To: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
Cc: Suchandra Thapa <s-thapa-11(at)alumni(dot)uchicago(dot)edu>, pgsql-general(at)postgresql(dot)org
Subject: Re: serial type vs. sequences
Date: 2003-11-12 22:36:12
Message-ID: 3FB2B5DC.1020406@Yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Joshua D. Drake wrote:

> You are accurate.
>
> Suchandra Thapa wrote:
>
>>I was wondering if there was a reliable method to get the last id
>>inserted into a serial column in a table. I believe previous
>>recommendations given were to use a sequence, have your app get the
>>nextval for the sequence and then use that in the insert. I was
>>wondering if things had changed or if that is still the recommended
>>method.

If you don't need the value itself in your application, it is safe to
use currval('seqname') in queries following the use of
nextval('seqname'). currval() returns the last value assigned in your
session and is not affected by other concurrent transactions.

Jan

--
#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================== JanWieck(at)Yahoo(dot)com #

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-11-12 22:38:31 Re: SQL-question: returning the id of an insert querry
Previous Message Christopher Murtagh 2003-11-12 22:35:40 plpgsql return setof integer?