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 F1B8DD1B238 for ; Wed, 9 Jun 2004 04:23:01 -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 35377-01 for ; Wed, 9 Jun 2004 07:22:58 +0000 (GMT) Received: from anchor-post-37.mail.demon.net (anchor-post-36.mail.demon.net [194.217.242.86]) by svr1.postgresql.org (Postfix) with ESMTP id D0B4AD1B18A for ; Wed, 9 Jun 2004 04:22:53 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-37.mail.demon.net with esmtp (Exim 3.35 #1) id 1BXxQ8-0008s7-0b; Wed, 09 Jun 2004 08:22:51 +0100 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 26B9216269; Wed, 9 Jun 2004 08:22:38 +0100 (BST) Message-ID: <40C6BABD.6060204@archonet.com> Date: Wed, 09 Jun 2004 08:22:37 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 0.6 (X11/20040502) X-Accept-Language: en-us, en MIME-Version: 1.0 To: mixo Cc: pgsql-sql@postgresql.org Subject: Re: Last insert id References: <40C6AD19.70302@coza.net.za> In-Reply-To: <40C6AD19.70302@coza.net.za> 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/68 X-Sequence-Number: 18013 mixo wrote: > 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? A SERIAL column is just an ordinary integer with a default of nextval(). All you need to do is: INSERT INTO t1 (...) VALUES (...) INSERT INTO t2 (...) VALUES (currval()...) Sequences are guaranteed to give values relevant to your current connection. -- Richard Huxton Archonet Ltd