Re: getting currval from a sequence - Mailing list pgsql-general

From Bruce Momjian
Subject Re: getting currval from a sequence
Date
Msg-id 200202111850.g1BIo5C29098@candle.pha.pa.us
Whole thread Raw
In response to Re: getting currval from a sequence  (Bruno Wolff III <bruno@wolff.to>)
List pgsql-general
Bruno Wolff III wrote:
> On Mon, Feb 11, 2002 at 11:42:31AM +0100,
>   tony <tony@animaproductions.com> wrote:
> >
> > How does one get the last record from a series of records? I just need
> > the id field which is a sequence field
>
> select max(id) from whatever;
> (This assumes the sequence hasn't rolled over.)

You can do a select on the sequence table that lays behind the sequence.
usually ending in _seq.  You can't use currval() because that is a
per-backend value that saves your previous nextval call.  Make sure you
lock the sequence table during the retrieve if you want an accurate
number and don't unlock it until you are done with the value.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

pgsql-general by date:

Previous
From: Darren Ferguson
Date:
Subject: Re: getting currval from a sequence - BUG ?
Next
From: Doug McNaught
Date:
Subject: Re: compiling on sco