Re: Serial - last value

From: Richard Huxton <dev(at)archonet(dot)com>
To: hendra kusuma <penguinroad(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Serial - last value
Date: 2008-11-20 10:36:18
Message-ID: 49253DA2.3060403@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

hendra kusuma wrote:
> Hi there
> a little basic question here
>
> I usually use something like this in stored function
> to get the last value of a serial type column
>
> select last_value into ret from id_sequence
>> return ret
>
> What i'm asking is,
> if many people run the same stored function at the same time
> will they get the last_value they should really get
> or is there another way to code that can ensure they really get the right
> value

You should use the functional interface: SELECT currval('my_sequence').
That guarantees you the right value for your current session.

This only works if you've called nextval('my_sequence') at some point in
the current session (although that could be automatic for a column of
type SERIAL).

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2008-11-20 11:01:46 Re: Using database to find file doublettes in my computer
Previous Message Richard Huxton 2008-11-20 10:33:30 Re: where in (select array)