Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 135D8B5DC61 for ; Sat, 24 Sep 2011 19:46:01 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 91867-02 for ; Sat, 24 Sep 2011 22:45:52 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from auth-smtp.nebula.fi (auth-smtp.nebula.fi [217.30.180.105]) by mail.postgresql.org (Postfix) with ESMTP id 6BE6AB5DC09 for ; Sat, 24 Sep 2011 19:45:51 -0300 (ADT) Received: from quadvadis.localdomain (gw.nitor.fi [83.145.228.24]) (authenticated bits=0) by auth-smtp.nebula.fi (8.13.4/8.13.4) with ESMTP id p8OMjmOv013920 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 25 Sep 2011 01:45:49 +0300 Message-ID: <4E7E5D9D.2070409@nitorcreations.com> Date: Sun, 25 Sep 2011 01:45:49 +0300 From: Mikko Tiihonen User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:6.0.2) Gecko/20110906 Thunderbird/6.0.2 MIME-Version: 1.0 To: Kevin Grittner CC: pgsql-jdbc@postgresql.org Subject: Patch for allowing explicit enable/disable of binary transfer for each OID type References: <4E7DA6D802000025000416A9@gw.wicourts.gov> In-Reply-To: <4E7DA6D802000025000416A9@gw.wicourts.gov> Content-Type: multipart/mixed; boundary="------------000004060900010607010500" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201109/109 X-Sequence-Number: 22227 This is a multi-part message in MIME format. --------------000004060900010607010500 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 09/24/2011 05:46 PM, Kevin Grittner wrote: >> Mikko Tiihonen wrote: > >> In general could add a mechanism to add/remove specific oids from >> the binary transfer mode as a configuration option to the jdbc >> driver. >> >> The same mechanism could be used for by application code that uses >> custom types inside PostgreSQL to take advantage of binary >> transfers. >> >> There will always be trade-offs between txt and bin encoding and >> sometimes the differences are large enough to change from driver >> defaults. > > Yeah, all I'm saying is that when there are such large trade-offs > between CPU usage and network traffic requirements, it might be a > good idea to allow a user to choose with some connection option. For > perspective, just within our organization we have cases where we're > communicating between busy machines on the same 1Gbps switch, and > cases where we're communicating between nearly-idle machines over a > sometimes-saturated 3Mbps WAN connection. This change would be a big > win for one and could potentially be a big problem in the other. Here is a patch that allows using binaryTransferEnable and binaryTransferDisable options to allow driver used to override the defaults. It accepts a comma separated list of OID names (much match names in org.postgres.core.Oid) or any numeric oid value. For example one can use binaryTransferDisable=INT4_ARRAY,1016 binaryTransferEnable=TEXT It will also be useful if for some reason the binary encoding of a variable changes in the server to allow using of old jdbc driver by disabling the binary transfer for one type as a workaround while waiting for a new jdbc driver release. -Mikko --------------000004060900010607010500 Content-Type: text/plain; name="binary_tunable.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="binary_tunable.patch" diff -urN pgjdbc.bin_uuid/org/postgresql/core/Oid.java pgjdbc.bin_tunable/org/postgresql/core/Oid.java --- pgjdbc.bin_uuid/org/postgresql/core/Oid.java 2011-09-25 00:54:54.890049880 +0300 +++ pgjdbc.bin_tunable/org/postgresql/core/Oid.java 2011-09-25 01:03:27.412150519 +0300 @@ -11,6 +11,10 @@ import java.lang.reflect.Field; +import org.postgresql.util.GT; +import org.postgresql.util.PSQLException; +import org.postgresql.util.PSQLState; + /** * Provides constants for well-known backend OIDs for the types we commonly * use. @@ -89,6 +93,25 @@ } catch (IllegalAccessException e) { // never happens } - return ""; + return ""; + } + + public static int valueOf(String oid) throws PSQLException { + try { + return Integer.parseInt(oid); + } catch (NumberFormatException ex) { + } + try { + oid = oid.toUpperCase(); + Field[] fields = Oid.class.getFields(); + for (int i = 0; i < fields.length; ++i) { + if (fields[i].getName().toUpperCase().equals(oid)) { + return fields[i].getInt(null); + } + } + } catch (IllegalAccessException e) { + // never happens + } + throw new PSQLException(GT.tr("oid type {0} not known and not a number", oid), PSQLState.INVALID_PARAMETER_VALUE); } } diff -urN pgjdbc.bin_uuid/org/postgresql/Driver.java.in pgjdbc.bin_tunable/org/postgresql/Driver.java.in --- pgjdbc.bin_uuid/org/postgresql/Driver.java.in 2011-09-25 00:55:47.649752890 +0300 +++ pgjdbc.bin_tunable/org/postgresql/Driver.java.in 2011-09-25 01:03:27.412150519 +0300 @@ -444,7 +444,11 @@ { "prepareThreshold", Boolean.FALSE, "Default statement prepare threshold (numeric)." }, { "binaryTransfer", Boolean.FALSE, - "Receive data in binary format if possible." }, + "Use binary format for sending and receiving data if possible." }, + { "binaryTransferEnable", Boolean.FALSE, + "Comma separated list of types to enable binary transfer. Either OID numbers or names." }, + { "binaryTransferDisable", Boolean.FALSE, + "Comma separated list of types to disable binary transfer. Either OID numbers or names. Overrides values in the driver default set and values set with binaryTransferEnable." }, { "charSet", Boolean.FALSE, "When connecting to a pre-7.3 server, the database encoding to assume is in use." }, { "compatible", Boolean.FALSE, diff -urN pgjdbc.bin_uuid/org/postgresql/ds/common/BaseDataSource.java pgjdbc.bin_tunable/org/postgresql/ds/common/BaseDataSource.java --- pgjdbc.bin_uuid/org/postgresql/ds/common/BaseDataSource.java 2011-09-25 00:57:36.166140858 +0300 +++ pgjdbc.bin_tunable/org/postgresql/ds/common/BaseDataSource.java 2011-09-25 01:03:27.413150513 +0300 @@ -52,6 +52,8 @@ private int prepareThreshold = 5; private int unknownLength = Integer.MAX_VALUE; private boolean binaryTransfer = true; + private String binaryTransferEnable = ""; + private String binaryTransferDisable = ""; private int loginTimeout = 0; // in seconds private int socketTimeout = 0; // in seconds private boolean ssl = false; @@ -392,7 +394,6 @@ /** * Sets protocol transfer mode. - * See {@link org.postgresql.PGConnection#setBinaryTransfer(boolean)} for details. * * @param enabled True if the binary transfer mode is used for supported field types, * false if text based transfer is used. @@ -413,6 +414,47 @@ } /** + * Add types to the override set of {@link org.postgresql.core.Oid} values used for binary transfer. + * + * @param oidList The comma separated list of Oids. Either textual or numeric value. + */ + public void setBinaryTransferEnable(String oidList) + { + this.binaryTransferEnable = oidList; + } + + /** + * Gets override set of Oid values that have binary transfer enabled. + * + * @see #setBinaryTransferEnable(String) + */ + public String getBinaryTransferEnable() + { + return binaryTransferEnable; + } + + /** + * Add types to the override set of {@link org.postgresql.core.Oid} values that will not be used for binary transfer. + * This overrides any values in the driver detault set or values set with {@link #setBinaryTransferEnable(String)}. + * + * @param oidList The comma separated list of Oids. Either textual or numeric value. + */ + public void setBinaryTransferDisable(String oidList) + { + this.binaryTransferDisable = oidList; + } + + /** + * Gets override set of Oid values that have binary transfer disabled. + * + * @see #setBinaryTransferDisable(String) + */ + public String getBinaryTransferDisable() + { + return binaryTransferDisable; + } + + /** * Generates a DriverManager URL from the other properties supplied. */ private String getUrl() @@ -522,6 +564,8 @@ out.writeInt(protocolVersion); out.writeObject(applicationName); out.writeBoolean(binaryTransfer); + out.writeObject(binaryTransferEnable); + out.writeObject(binaryTransferDisable); } protected void readBaseObject(ObjectInputStream in) throws IOException, ClassNotFoundException @@ -543,6 +587,8 @@ protocolVersion = in.readInt(); applicationName = (String)in.readObject(); binaryTransfer = in.readBoolean(); + binaryTransferEnable = (String)in.readObject(); + binaryTransferDisable = (String)in.readObject(); } public void initializeFrom(BaseDataSource source) throws IOException, ClassNotFoundException { diff -urN pgjdbc.bin_uuid/org/postgresql/ds/common/PGObjectFactory.java pgjdbc.bin_tunable/org/postgresql/ds/common/PGObjectFactory.java --- pgjdbc.bin_uuid/org/postgresql/ds/common/PGObjectFactory.java 2011-09-25 00:57:16.354252710 +0300 +++ pgjdbc.bin_tunable/org/postgresql/ds/common/PGObjectFactory.java 2011-09-25 01:05:32.149440863 +0300 @@ -118,6 +118,14 @@ if (binaryTransfer != null) ds.setBinaryTransfer(Boolean.parseBoolean(binaryTransfer)); + String binaryTransferEnable = getProperty(ref, "binaryTransferEnable"); + if (binaryTransferEnable != null) + ds.setBinaryTransferEnable(binaryTransferEnable); + + String binaryTransferDisable = getProperty(ref, "binaryTransferDisable"); + if (binaryTransferDisable != null) + ds.setBinaryTransferDisable(binaryTransferDisable); + return ds; } diff -urN pgjdbc.bin_uuid/org/postgresql/jdbc2/AbstractJdbc2Connection.java pgjdbc.bin_tunable/org/postgresql/jdbc2/AbstractJdbc2Connection.java --- pgjdbc.bin_uuid/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2011-09-25 00:56:22.916554156 +0300 +++ pgjdbc.bin_tunable/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2011-09-25 01:03:27.414150507 +0300 @@ -186,6 +186,9 @@ binaryOids.clear(Oid.TEXT_ARRAY); } + binaryOids.or(getOidBitSet(info.getProperty("binaryTransferEnable", ""))); + binaryOids.andNot(getOidBitSet(info.getProperty("binaryTransferDisable", ""))); + // split for receive and send for better control useBinarySendForOids = new BitSet(); useBinarySendForOids.or(binaryOids); @@ -256,6 +259,16 @@ } } + private BitSet getOidBitSet(String oidList) throws PSQLException { + BitSet oids = new BitSet(); + StringTokenizer tokenizer = new StringTokenizer(oidList, ","); + while (tokenizer.hasMoreTokens()) { + String oid = tokenizer.nextToken(); + oids.set(Oid.valueOf(oid)); + } + return oids; + } + private String oidsToString(BitSet oids) { StringBuffer sb = new StringBuffer(); int oid = -1; --------------000004060900010607010500--