Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bAcsk-0000v6-I7 for pgsql-jdbc@arkaria.postgresql.org; Wed, 08 Jun 2016 12:46:50 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bAcsk-0005J0-4i for pgsql-jdbc@arkaria.postgresql.org; Wed, 08 Jun 2016 12:46:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bAcsj-0005Ik-Kz for pgsql-jdbc@postgresql.org; Wed, 08 Jun 2016 12:46:49 +0000 Received: from mx1.redhat.com ([209.132.183.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bAcsf-00021t-D0 for pgsql-jdbc@postgresql.org; Wed, 08 Jun 2016 12:46:49 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 016A37733E; Wed, 8 Jun 2016 12:46:42 +0000 (UTC) Received: from [10.42.17.24] ([10.42.17.24]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u58CkeZe028833; Wed, 8 Jun 2016 08:46:41 -0400 Subject: Re: Batches of single-insert statements vs batches of multi-insert statements To: Christopher Deckers References: From: Jeremy Whiting Cc: pgsql-jdbc@postgresql.org Message-ID: <6ea3d69b-9af6-7cdb-7313-a825a2bad7c4@redhat.com> Date: Wed, 8 Jun 2016 13:46:40 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------645B50BA0EB3D1C62818381D" X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 08 Jun 2016 12:46:42 +0000 (UTC) X-Pg-Spam-Score: -8.3 (--------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org This is a multi-part message in MIME format. --------------645B50BA0EB3D1C62818381D Content-Type: multipart/alternative; boundary="------------86DE6AFC8D91462FAEF4C0FA" --------------86DE6AFC8D91462FAEF4C0FA Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit 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) --------------86DE6AFC8D91462FAEF4C0FA Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
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)
--------------86DE6AFC8D91462FAEF4C0FA-- --------------645B50BA0EB3D1C62818381D Content-Type: text/plain; charset=UTF-8; name="FixMultiInsertTest.btm" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="FixMultiInsertTest.btm" IyBSdWxlcyB0byBmaXggdGVzdCBjb2RlIAoKUlVMRSBGaXggaGFyZCBjb2Rl ZCBkYXRhIGluIHNxbCBwYXJhbWV0ZXIKQ0xBU1MgcG9zdGdyZXMuTXVsdGlJ bnNlcnRUZXN0Ck1FVEhPRCBtYWluCkFUIExJTkUgMzYKSUYgVFJVRQpETyAk aW5zZXJ0ID0gIklOU0VSVCBJTlRPIFRfQWJDZCAoVF9BYkNkX0lELCBTb21l RGF0ZSwgQVNtYWxsSW50KSBWQUxVRVMgKD8sID8sID8pIjsKRU5EUlVMRQoK UlVMRSBGaXggaGFyZCBjb2RlZCBkYXRhIGluIHNxbCBmaXJzdApDTEFTUyBw b3N0Z3Jlcy5NdWx0aUluc2VydFRlc3QKTUVUSE9EIG1haW4KQVQgTElORSA0 MQpJRiBUUlVFCkRPICRwc3Quc2V0SW50KDMsIDApOwpFTkRSVUxFCgpSVUxF IEZpeCBoYXJkIGNvZGVkIGRhdGEgaW4gc3FsIHNlY29uZApDTEFTUyBwb3N0 Z3Jlcy5NdWx0aUluc2VydFRlc3QKTUVUSE9EIG1haW4KQVQgTElORSAzOApJ RiBUUlVFCkRPICRwc3Quc2V0SW50KDMsIDApOwpFTkRSVUxFCgo= --------------645B50BA0EB3D1C62818381D Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc --------------645B50BA0EB3D1C62818381D--