pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Perrin - Demography <aperrin@demog.berkeley.edu>
To: Vladimir V. Zolotych <gsmith@eurocom.od.ua>
Cc: pgsql-admin@postgresql.org
Subject: Re: sequence
Date: Tue, 18 Apr 2000 09:18:35 -0700 (PDT)
Message-ID: <Pine.SUN.4.10.10004180916360.20063-100000@davis.DEMOG.Berkeley.EDU> (raw)
In-Reply-To: <38FC094F.EBF59629@eurocom.od.ua>

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@demog.berkeley.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@eurocom.od.ua
> 
> 
> 




view thread (5+ messages)  latest in thread

Message-ID: <Pine.SUN.4.10.10004180916360.20063-100000@davis.DEMOG.Berkeley.EDU>
Permalink:  ../Pine.SUN.4.10.10004180916360.20063-100000@davis.DEMOG.Berkeley.EDU/
Also on:    postgresql.org/message-id/Pine.SUN.4.10.10004180916360.20063-100000@davis.DEMOG.Berkeley.EDU

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: aperrin@demog.berkeley.edu, gsmith@eurocom.od.ua
  Subject: Re: sequence
  In-Reply-To: <Pine.SUN.4.10.10004180916360.20063-100000@davis.DEMOG.Berkeley.EDU>
  References: <38FC094F.EBF59629@eurocom.od.ua> <Pine.SUN.4.10.10004180916360.20063-100000@davis.DEMOG.Berkeley.EDU>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox