From: | Andrew Perrin - Demography <aperrin(at)demog(dot)berkeley(dot)edu> |
---|---|
To: | "Vladimir V(dot) Zolotych" <gsmith(at)eurocom(dot)od(dot)ua> |
Cc: | pgsql-admin(at)postgresql(dot)org |
Subject: | Re: sequence |
Date: | 2000-04-18 16:18:35 |
Message-ID: | Pine.SUN.4.10.10004180916360.20063-100000@davis.DEMOG.Berkeley.EDU |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-admin |
I figured this out recently with the help of the list - you want either
nextval() or currval(). In my case, I used currval(). Basically:
SELECT currval('table_field_seq');
will return the most recently assigned value to the field *by the current
backend* which means:
- There's no danger of a race condition (another user creating a record in
the time between your creating the record and calling currval); BUT
- You must be using the same backend as you did on the creation.
Hope this helps.
---------------------------------------------------------------------
Andrew J. Perrin - aperrin(at)demog(dot)berkeley(dot)edu - NT/Unix Admin/Support
Department of Demography - University of California at Berkeley
2232 Piedmont Avenue #2120 - Berkeley, California, 94720-2120 USA
http://demog.berkeley.edu/~aperrin --------------------------SEIU1199
On Tue, 18 Apr 2000, Vladimir V. Zolotych wrote:
> Hello all
>
> What is the safe way to add record to a table
> and get it id (generated by a sequence) consistently?
>
> E.g.
> 1) add record (INSERT INTO...) to a table
> (the record's id will be generated
> automatically due the ``id SERIAL'' column were defined).
>
> 2) get the id of a just created record.
>
> Thanks
>
>
> --
> Vladimir Zolotych gsmith(at)eurocom(dot)od(dot)ua
>
>
>
From | Date | Subject | |
---|---|---|---|
Next Message | Vladimir V. Zolotych | 2000-04-19 09:34:15 | |
Previous Message | Margarita Barvinok | 2000-04-18 15:55:06 | Re: DEFAULT now() ? |