Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id D71E22E0053 for ; Wed, 20 Feb 2008 07:13:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 14610-08 for ; Wed, 20 Feb 2008 07:13:09 -0400 (AST) Received: from mail01.enterprisedb.com (mail01.enterprisedb.com [63.246.7.168]) by postgresql.org (Postfix) with ESMTP id E274E2E004F for ; Wed, 20 Feb 2008 07:13:08 -0400 (AST) thread-index: AchzsaZLa5Kli1yrQrK1Q0ETHV6muA== Received: from [192.168.1.33] ([81.109.253.218]) by mail01.enterprisedb.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.3959); Wed, 20 Feb 2008 06:13:38 -0500 Content-Class: urn:content-classes:message Importance: normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4133 Message-ID: <47BC0AF6.2060500@enterprisedb.com> Date: Wed, 20 Feb 2008 11:11:50 +0000 From: "Heikki Linnakangas" Organization: EnterpriseDB User-Agent: Mozilla-Thunderbird 2.0.0.9 (X11/20080110) MIME-Version: 1.0 To: Cc: Subject: Re: Retrieving last InsertedID : INSERT... RETURNING safe ? References: <38245.1203500677@motionsponsor.com> In-Reply-To: <38245.1203500677@motionsponsor.com> Content-Type: text/plain; format=flowed; charset="UTF-8" Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 20 Feb 2008 11:13:38.0687 (UTC) FILETIME=[A48870F0:01C873B1] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/154 X-Sequence-Number: 18361 spastor@motionsponsor.com wrote: > Only one exception is regarding the best method to retrieve the last inserted id (serial). There are many posts on this topic but i couldn t find a definitive response. > I am using postgresql version 8.2.6 and jdbc 8.2_p505, i ve been experiencing INSERT statement with RETURNING myID, using an executeQuery it works great, i retrieve the proper value everytime at least from the few tests i have been doin locally. > Is it THAT easy or could this method break somehow in a live environement ? It is that easy, and that is a good way to do it. Alternatively, if you need to support older PostgreSQL version for example, you could use a "SELECT currval('sequence')" query after the INSERT. Much of the of discussion you saw were probably about implementing the JDBC getGeneratedKeys() function that some applications need for portability reasons, or because of middleware that doesn't know how to do INSERT RETURNING. PostgreSQL driver doesn't support that at the moment, but as long as you don't need it, you'll be fine. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com