Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 5DDB69FA4B4 for ; Mon, 22 Jan 2007 08:29:59 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 96980-02 for ; Mon, 22 Jan 2007 08:29:51 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by postgresql.org (Postfix) with SMTP id DAE9C9FA354 for ; Mon, 22 Jan 2007 08:29:55 -0400 (AST) Received: (qmail 28153 invoked from network); 22 Jan 2007 12:29:54 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.14?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 22 Jan 2007 12:29:54 -0000 In-Reply-To: <45B480E6.2010009@gmx.at> References: <4580E681.3080106@kensystem.com> <45810230.80803@gmx.at> <45B1AC19.3090300@kensystem.com> <45B1F839.3060402@gmx.at> <45B254D7.4080304@kensystem.com> <55027648-0777-4BE4-8581-933E86180539@fastcrypt.com> <45B3B9CB.3070308@kensystem.com> <3C6B3C20-CD94-4D35-B146-27DA78E6E462@fastcrypt.com> <45B40AE1.8020003@kensystem.com> <45B44726.1070301@kensystem.com> <45B480E6.2010009@gmx.at> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: Ken Johanson , pgsql-jdbc@postgresql.org Content-Transfer-Encoding: 7bit From: Dave Cramer Subject: Re: Synthesize support for Statement.getGeneratedKeys()? Date: Mon, 22 Jan 2007 07:30:04 -0500 To: Michael Paesold X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/142 X-Sequence-Number: 16507 On 22-Jan-07, at 4:16 AM, Michael Paesold wrote: > Ken Johanson wrote: >>>> As an aside, how do PG jdbc users get the server generated keys? >>>> Or does everyone use some kind of UUID system (which I think is >>>> generally regarded as detrimental to indexes/memory under high >>>> load and large DB sizes - compared to int/bigint)? Or do PG >>>> users using some standard or server-specific (RETURNING) SQL >>>> clause? >>> >>> either create the key ahead of time select nextval('sequence') >>> and insert it explicitly, or insert the row and then select >>> currval('sequence') >>> >> That makes sense; the sequence is retrieved and it internally >> increments - regardless of whether the key was actually inserted >> or not. I'm personally not used to this though, it allows for >> actual keys in the database to possibly have gaps (if the key want >> actually used / rollback etc). Thats trivial / innocuous I guess, >> but I'm just used to having sequential keys tables. Would this >> require two trips to the server, or can we handle in one >> excecuteUpdate? >> My real question is, what about the case where multiple VALUES are >> inserted; if I have 3 values should I call the sequence 3 times? >> What is the most efficient was to do that? (Can I do it in a >> single query?) > > I don't think you should use "currval" or "nextval" at all. A > general solution in the JDBC driver should even work in the case of > triggers that interfere with the value of a sequence. Or which > might change the value actually inserted into the table. Just think > of an insert trigger that uses a sequence for a second time. > > There is only one way to reliably get the database generated > values: the RETURNING clause. > > So my basic suggestion was to rewrite a query written as: > "INSERT INTO tab VALUES (...)" > into > "INSERT INTO tab VALUES (...) RETURNING x" > > With x being either (a) what the user specified using the Java API > (i.e. any column names) or (b) the primary key column(s) (or other > columns having a "DEFAULT currval(...)"). > The second case (b) I would leave for later, since it requires > parsing the query and finding the table which will be inserted > into. And you would have to use database meta data to find the > columns to return. > Yes, agreed, Ken was just curious how it is being done now. > Of course, there should be a minimum amount of parsing to detect if > the query is a valid INSERT query and does not already have a > different RETURNING clause. > > Another option would be to convince backend developers to add a way > to specify a "RETURNING clause" on the protocol level, i.e. without > having to change the query string. Yes, this would be the best solution. > Best Regards > Michael Paesold > > > > ---------------------------(end of > broadcast)--------------------------- > TIP 6: explain analyze is your friend >