From: Ken Johanson <pg-user@kensystem.com>
To: pgsql-jdbc@postgresql.org
Cc: Michael Paesold <mpaesold@gmx.at>
Cc: Dave Cramer <pg@fastcrypt.com>
Subject: Re: Synthesize support for Statement.getGeneratedKeys()?
Date: Tue, 23 Jan 2007 00:00:13 -0700
Message-ID: <45B5B27D.7050707@kensystem.com> (raw)
In-Reply-To: <45B4BE2F.3030702@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>
<B480E0B4-9CE7-4FA3-B6F2-BC912AF2BC58@fastcrypt.com>
<45B44726.1070301@kensystem.com>
<45B480E6.2010009@gmx.at>
<EFC27EC0-3877-4F9C-AEDC-FDAD1D6C306F@fastcrypt.com>
<45B4BE2F.3030702@gmx.at>
>>>> 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.
>>>
I think that, given everyone's input (including Vit's, thanks) and
mention of possible variation on query, possible need to parse for
table/column names, and/or need to call database metadata / or result
set metadata (to get keys?) (which require another trip to the
server?)... this might be out of my league. Well, even if I did get it
working, it likely would not work in every case (triggers etc), and
would eventually be replaced when V4 protocol comes around.
Unless one of the PG folks can prescribe, in exact terms, the very best
way to execute this (after which I would build out the actual patch)...
then I may have to bow out of this (it's complex / error prone enough to
frighten lil'ol me, and time is a bit short on my end too I'm afraid).
Perhaps it's better for everyone if we lobby to have the
backend/protocol to add this natively (as you all have suggested). So..
Does anyone know if the actual server core natively has the ability to
build created-keys resultsets (without having to modify the query /
RETURNS), or is this truly a protocl bottleneck?...
Thanks,
Ken
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: pg-user@kensystem.com, mpaesold@gmx.at, pg@fastcrypt.com
Subject: Re: Synthesize support for Statement.getGeneratedKeys()?
In-Reply-To: <45B5B27D.7050707@kensystem.com>
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> <B480E0B4-9CE7-4FA3-B6F2-BC912AF2BC58@fastcrypt.com> <45B44726.1070301@kensystem.com> <45B480E6.2010009@gmx.at> <EFC27EC0-3877-4F9C-AEDC-FDAD1D6C306F@fastcrypt.com> <45B4BE2F.3030702@gmx.at> <45B5B27D.7050707@kensystem.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox