X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id DEF31D1B193 for ; Wed, 16 Jun 2004 03:43:54 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 53647-08 for ; Wed, 16 Jun 2004 06:43:47 +0000 (GMT) Received: from mail.interzone.gr (server.interzone.gr [193.41.229.197]) by svr1.postgresql.org (Postfix) with ESMTP id EBBBCD1B172 for ; Wed, 16 Jun 2004 03:43:43 -0300 (ADT) Received: by mail.interzone.gr (Postfix, from userid 1005) id A303223CDF; Wed, 16 Jun 2004 09:43:42 +0300 (EEST) Received: from interzone.gr (unknown [212.54.222.195]) by mail.interzone.gr (Postfix) with ESMTP id 3284723CD0; Wed, 16 Jun 2004 09:43:35 +0300 (EEST) Message-ID: <40CFECF5.6060203@interzone.gr> Date: Wed, 16 Jun 2004 09:47:17 +0300 From: Michalis Kabrianis Organization: Interzone User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7b) Gecko/20040421 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Andrei Bintintan Cc: 'mixo' , pgsql-sql@postgresql.org Subject: Re: Last insert id References: <20040615070408.80EA4175A8@ar-sd.net> In-Reply-To: <20040615070408.80EA4175A8@ar-sd.net> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/173 X-Sequence-Number: 18118 Andrei Bintintan wrote: > "Is it safe to use "select max(table1_id) from table1" after the insert?" > > Yes it is safe, but ONLY if you use it inside a transaction.(BEGIN/COMMIT). > > BR. > Hi, I think it would be safe to use : select currval('tablename_idname_seq'); inside a session to receive the current value of the sequence that feeds the serial column (i.e. the last inserted value on that session). Every other session is irrelevant (as the currval returns the session-specific value). If I understand correctly, it doesn't even need the transaction to be open (in which case all consecutive inserts on the same table would block). M.K. > > -----Original Message----- > From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] > On Behalf Of mixo > Sent: Wednesday, June 09, 2004 9:24 AM > To: pgsql-sql@postgresql.org > Subject: [SQL] Last insert id > > I have three tables which are related a serial field, table1_id, in on of > the tables. Updating the tables is done through a transaction. My problem > is, once I have insert a row in the first tables with table1_id, I need for > the other two tables. How can I get this? Is it safe to use "select > max(table1_id) from table1" after the insert? > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faqs/FAQ.html > > > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faqs/FAQ.html