From gsmith@eurocom.od.ua Tue Apr 18 07:05:49 2000 Received: from Main.eurocom.od.ua (root@Main.EuroCom.Od.UA [212.15.128.6]) by hub.org (8.9.3/8.9.3) with ESMTP id DAA77267 for ; Tue, 18 Apr 2000 03:05:49 -0400 (EDT) (envelope-from gsmith@eurocom.od.ua) Received: from eurocom.od.ua (FarHost.EuroCom.Od.UA [212.15.128.42]) by Main.eurocom.od.ua (8.10.0/8.10.0) with ESMTP id e3I75cK25234 for ; Tue, 18 Apr 2000 10:05:39 +0300 Message-ID: <38FC094F.EBF59629@eurocom.od.ua> Date: Tue, 18 Apr 2000 10:05:51 +0300 From: "Vladimir V. Zolotych" X-Mailer: Mozilla 4.51 [en] (X11; I; Linux 2.2.5-15 i686) X-Accept-Language: en MIME-Version: 1.0 To: pgsql-admin@postgresql.org Subject: sequence Content-Type: multipart/alternative; boundary="------------32EAB34F404BC446869A275A" X-Archive-Number: 200004/64 --------------32EAB34F404BC446869A275A Content-Type: text/plain; charset=koi8-r Content-Transfer-Encoding: 7bit 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 --------------32EAB34F404BC446869A275A Content-Type: text/html; charset=koi8-r Content-Transfer-Encoding: 7bit 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
  --------------32EAB34F404BC446869A275A-- From aperrin@demog.berkeley.edu Tue Apr 18 16:20:18 2000 Received: from davis.DEMOG.Berkeley.EDU (davis.DEMOG.Berkeley.EDU [128.32.93.8]) by hub.org (8.9.3/8.9.3) with ESMTP id MAA56096 for ; Tue, 18 Apr 2000 12:20:18 -0400 (EDT) (envelope-from aperrin@demog.berkeley.edu) Received: from localhost (aperrin@localhost) by davis.DEMOG.Berkeley.EDU (8.9.3/8.9.1) with ESMTP id JAA20109; Tue, 18 Apr 2000 09:18:35 -0700 (PDT) Date: Tue, 18 Apr 2000 09:18:35 -0700 (PDT) From: Andrew Perrin - Demography To: "Vladimir V. Zolotych" cc: pgsql-admin@postgresql.org Subject: Re: sequence In-Reply-To: <38FC094F.EBF59629@eurocom.od.ua> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Archive-Number: 200004/70 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 > > > From rbenitez22@yahoo.com Thu Jan 9 01:08:33 2003 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 2A3D44758C9 for ; Wed, 8 Jan 2003 20:08:33 -0500 (EST) Received: from web11907.mail.yahoo.com (web11907.mail.yahoo.com [216.136.172.191]) by postgresql.org (Postfix) with SMTP id 73CAE474E42 for ; Wed, 8 Jan 2003 20:08:32 -0500 (EST) Message-ID: <20030109010835.40007.qmail@web11907.mail.yahoo.com> Received: from [66.69.177.171] by web11907.mail.yahoo.com via HTTP; Wed, 08 Jan 2003 17:08:35 PST Date: Wed, 8 Jan 2003 17:08:35 -0800 (PST) From: Roberto Benitez Subject: sequence To: pgsql-admin@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/92 X-Sequence-Number: 6934 I have a field of serial type. for some reason, the sequence skips numbers every now and then. does anyone know why? --thanks --roberto c. benitez __________________________________________________ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com From arturp@plukwa.net Sat Jan 11 15:17:52 2003 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id AA18D475B85 for ; Sat, 11 Jan 2003 10:17:52 -0500 (EST) Received: from yoda.plukwa.net (yoda.plukwa.net [213.146.36.61]) by postgresql.org (Postfix) with ESMTP id 0A07A475AFF for ; Sat, 11 Jan 2003 10:17:52 -0500 (EST) Received: from arturp by yoda.plukwa.net with local (Exim 3.35 #1 (Debian)) id 18XNOM-0002Pa-00; Sat, 11 Jan 2003 16:17:46 +0100 Date: Sat, 11 Jan 2003 16:17:45 +0100 To: Roberto Benitez Cc: pgsql-admin@postgresql.org Subject: Re: sequence Message-ID: <20030111151745.GT7674@yoda.plukwa.net> References: <20030109010835.40007.qmail@web11907.mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030109010835.40007.qmail@web11907.mail.yahoo.com> User-Agent: Mutt/1.4i From: Artur Pietruk X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/105 X-Sequence-Number: 6947 On Wed, Jan 08, 2003 at 05:08:35PM -0800, Roberto Benitez wrote: > I have a field of serial type. for some reason, the > sequence skips numbers every now and then. does anyone > know why? Canceled/rollbacked transactions? BR, -- --- Artur Pietruk, arturp@plukwa.net From arturp@plukwa.pdi.net Sat Jan 11 15:23:51 2003 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id A5B53476237 for ; Sat, 11 Jan 2003 10:23:51 -0500 (EST) Received: from yoda.plukwa.net (yoda.plukwa.net [213.146.36.61]) by postgresql.org (Postfix) with ESMTP id 1C93B476220 for ; Sat, 11 Jan 2003 10:23:51 -0500 (EST) Received: from arturp by yoda.plukwa.net with local (Exim 3.35 #1 (Debian)) for pgsql-admin@postgresql.org id 18XNUB-0002SD-00; Sat, 11 Jan 2003 16:23:47 +0100 Date: Sat, 11 Jan 2003 16:23:47 +0100 From: Artur Pietruk To: pgsql-admin@postgresql.org Subject: Re: sequence Message-ID: <20030111152347.GU7674@yoda.plukwa.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4i X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/102 X-Sequence-Number: 6944 On Wed, Jan 08, 2003 at 05:08:35PM -0800, Roberto Benitez wrote: > I have a field of serial type. for some reason, the > sequence skips numbers every now and then. does anyone > know why? Canceled/rollbacked transactions? BR, -- --- Artur Pietruk, arturp@plukwa.net