Re: getting currval from a sequence

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: tony <tony(at)animaproductions(dot)com>, postgres list <pgsql-general(at)postgresql(dot)org>
Subject: Re: getting currval from a sequence
Date: 2002-02-11 18:50:05
Message-ID: 200202111850.g1BIo5C29098@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Bruno Wolff III wrote:
> On Mon, Feb 11, 2002 at 11:42:31AM +0100,
> tony <tony(at)animaproductions(dot)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(at)candle(dot)pha(dot)pa(dot)us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jan Wieck 2002-02-11 19:35:57 Re: [HACKERS] Feature enhancement request : use of libgda
Previous Message Doug McNaught 2002-02-11 18:49:56 Re: compiling on sco