From: Jeremy Whiting <jwhiting@redhat.com>
To: Christopher Deckers <chrriis@gmail.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Batches of single-insert statements vs batches of multi-insert statements
Date: Wed, 8 Jun 2016 13:46:40 +0100
Message-ID: <6ea3d69b-9af6-7cdb-7313-a825a2bad7c4@redhat.com> (raw)
In-Reply-To: <CADFnS4SFdXVNr6pLeu_t-37pvqGFeV2=QTuYTT+sR+Op_GoopA@mail.gmail.com>
References: <CADFnS4TP2tqF5qXDpvAJykdREoZtAQ9nJZNrN3Xh4MQxTfAv7g@mail.gmail.com>
<CAB=Je-FLY9U4wfK_csoZH2cPSrna5EhZMsCUK+aE=8iwOyfbBA@mail.gmail.com>
<CADFnS4SFdXVNr6pLeu_t-37pvqGFeV2=QTuYTT+sR+Op_GoopA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>
Hi Christopher,
I suggest you avoid trying to hard coding data values in the statement
sql. Instead use parameter place-holders (?) and use the API to bind the
value. You will find it works much better for you. Like this ....
String insert = "INSERT INTO T_AbCd (T_AbCd_ID, SomeDate, ASmallInt)
VALUES (?, ?, ?)";
PreparedStatement pst = conn.prepareStatement(insert,
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
pst.setLong(1, 24202712L);
pst.setDate(2, new java.sql.Date(2014-1900, 3-1, 21));
pst.setInt(3, 0);
pst.addBatch();
pst.setLong(1, 24202713L);
pst.setDate(2, new java.sql.Date(2014-1900, 3-1, 22));
pst.setInt(3, 0);
pst.addBatch();
pst.executeBatch();
The reason is the batched re-write feature builds dynamically the sql
issued to the back end. Using parameter place-holders. So you can see
hard coding data values isn't going to work without some horribly
complex statement parsing added to the driver. This time parsing data
and recognizing all data values and type permutations. yikes
I'd say you have these options. If you want to enable batched re-writing.
a) Change your code using the same style of coding as the above example.
Re-deploying to production.
b) If re-deploy to production isn't possible in the short term try using
Byteman [1]. Byteman will unobtrusively mend your existing code. See the
attached Byteman rule. The attached rule fixes your test class.
c) Provide a patch to add implement sql statement parsing support for
hard coded data. I see you have already started to try this [2].
Regards,
Jeremy
[1] http://byteman.jboss.org/
[2] https://github.com/pgjdbc/pgjdbc/pull/580
On 05/06/16 21:39, Christopher Deckers wrote:
> Hi Vladimir,
>
> Thanks for your answer! It explains well the current situation.
>
> Believe me or not, pgjdbc has already that feature implemented.
>
>
> Oh good! But it definitely needs more testing and fixing :)
> I got: "Batch entry 0 INSERT INTO XXXX".
> Next exception: "VALUES lists must all be the same length".
>
> I narrowed down the issue to a simple test case, see attached file.
>
> Funny enough, if I change the name of the table, it seems to work. I
> used the convention that we have in our production code.
> Note that this test case does not produce any error if I remove the
> "reWriteBatchedInserts=true" parameter.
>
> Please let me know if I can be of any help!
> -Christopher
>
>
>
--
Jeremy Whiting
Senior Software Engineer, Middleware Performance Team
Red Hat
------------------------------------------------------------
Registered Address: Red Hat UK Ltd, 64 Baker Street, 4th Floor, London. W1U 7DF. United Kingdom.
Registered in England and Wales under Company Registration No. 03798903. Directors: Directors:Michael Cunningham (US), Michael O'Neill(Ireland), Eric Shander (US)
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbcAttachments:FixMultiInsertTest.btm (text/plain, 488B) download
# Rules to fix test code
RULE Fix hard coded data in sql parameter
CLASS postgres.MultiInsertTest
METHOD main
AT LINE 36
IF TRUE
DO $insert = "INSERT INTO T_AbCd (T_AbCd_ID, SomeDate, ASmallInt) VALUES (?, ?, ?)";
ENDRULE
RULE Fix hard coded data in sql first
CLASS postgres.MultiInsertTest
METHOD main
AT LINE 41
IF TRUE
DO $pst.setInt(3, 0);
ENDRULE
RULE Fix hard coded data in sql second
CLASS postgres.MultiInsertTest
METHOD main
AT LINE 38
IF TRUE
DO $pst.setInt(3, 0);
ENDRULE
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: jwhiting@redhat.com, chrriis@gmail.com
Subject: Re: Batches of single-insert statements vs batches of multi-insert statements
In-Reply-To: <6ea3d69b-9af6-7cdb-7313-a825a2bad7c4@redhat.com>
References: <CADFnS4TP2tqF5qXDpvAJykdREoZtAQ9nJZNrN3Xh4MQxTfAv7g@mail.gmail.com> <CAB=Je-FLY9U4wfK_csoZH2cPSrna5EhZMsCUK+aE=8iwOyfbBA@mail.gmail.com> <CADFnS4SFdXVNr6pLeu_t-37pvqGFeV2=QTuYTT+sR+Op_GoopA@mail.gmail.com> <6ea3d69b-9af6-7cdb-7313-a825a2bad7c4@redhat.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