Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 8A0C62E003E for ; Wed, 20 Feb 2008 07:37:55 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 21413-05 for ; Wed, 20 Feb 2008 07:37:43 -0400 (AST) Received: from mail.mnc.ch (62-2-77-205.static.cablecom.ch [62.2.77.205]) by postgresql.org (Postfix) with ESMTP id 379A12E0044 for ; Wed, 20 Feb 2008 07:37:49 -0400 (AST) Received: from localhost (mail2.mnc.ch [127.0.0.1]) by mail.mnc.ch (Postfix) with ESMTP id 071CF2307A; Wed, 20 Feb 2008 11:37:47 +0000 (GMT) X-Virus-Scanned: amavisd-new at mnc.ch Received: from mail.mnc.ch ([127.0.0.1]) by localhost (mail2.mnc.ch [127.0.0.1]) (amavisd-new, port 10025) with ESMTP id y6cwzTbWIVcy; Wed, 20 Feb 2008 11:37:46 +0000 (GMT) Received: from meuh.localdomain (62-2-77-204.static.cablecom.ch [62.2.77.204]) by mail.mnc.ch (Postfix) with ESMTP id 7FC5423079; Wed, 20 Feb 2008 11:37:46 +0000 (GMT) Received: by meuh.localdomain (Postfix, from userid 1000) id 95D0F183709; Wed, 20 Feb 2008 12:37:44 +0100 (CET) To: "Heikki Linnakangas" Cc: , Subject: Re: Retrieving last InsertedID : INSERT... RETURNING safe ? References: <38245.1203500677@motionsponsor.com> <47BC0AF6.2060500@enterprisedb.com> From: Guillaume Cottenceau X-Hashcash: 1:20:080220:heikki@enterprisedb.com::LHHRd3g1LrygxlcI:000000000000000000000000000000000000002OKh X-Hashcash: 1:20:080220:spastor@motionsponsor.com::LMby7WtsVu0ZwjZy:0000000000000000000000000000000000001uOK X-Hashcash: 1:20:080220:pgsql-jdbc@postgresql.org::W9PcLYdywPr0Fs9i:0000000000000000000000000000000000001mxu Date: Wed, 20 Feb 2008 12:37:44 +0100 In-Reply-To: <47BC0AF6.2060500@enterprisedb.com> (Heikki Linnakangas's message of "Wed\, 20 Feb 2008 11\:11\:50 +0000") Message-ID: <87prurrh87.fsf@mnc.ch> User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/156 X-Sequence-Number: 18363 "Heikki Linnakangas" writes: > 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. Well, that other solution is dangerous in case multiple inserts to that table are done concurrently; a quite common usage pattern with java web applications handling multiple HTTP requests with concurrent java threads.. -- Guillaume Cottenceau