pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
To: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Cc: pgsql-jdbc@postgresql.org
Subject: Patch for allowing explicit enable/disable of binary transfer for each OID type
Date: Sun, 25 Sep 2011 01:45:49 +0300
Message-ID: <4E7E5D9D.2070409@nitorcreations.com> (raw)
In-Reply-To: <4E7DA6D802000025000416A9@gw.wicourts.gov>
References: <4E7DA6D802000025000416A9@gw.wicourts.gov>

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

Attachments:

binary_tunable.patch (text/plain, 7.9K) download 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 "<unknown>"; + return "<unknown:" + oid + ">"; + } + + 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;

Message-ID: <4E7E5D9D.2070409@nitorcreations.com>
Permalink:  ../4E7E5D9D.2070409@nitorcreations.com/
Also on:    postgresql.org/message-id/4E7E5D9D.2070409@nitorcreations.com

 ·  · 

reply

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: mikko.tiihonen@nitorcreations.com, Kevin.Grittner@wicourts.gov
  Subject: Re: Patch for allowing explicit enable/disable of binary transfer for each OID type
  In-Reply-To: <4E7E5D9D.2070409@nitorcreations.com>
  References: <4E7DA6D802000025000416A9@gw.wicourts.gov> <4E7E5D9D.2070409@nitorcreations.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