pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Patch: Force Primitives
7+ messages / 2 participants
[nested] [flat]

* Patch: Force Primitives
@ 2013-03-24 04:10  Nicholas White <n.j.white@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Nicholas White @ 2013-03-24 04:10 UTC (permalink / raw)
  To: pgsql-jdbc

Hi,

I'd like the JDBC driver's Array implementations to return primitive arrays
when I call getArray(). I can force this behaviour by passing
&compatible=8.2 in my JDBCUrl, but then I can't transfer the arrays from
the database via the binary protocol, as that requires a minimum compatible
version of 8.3. I've attached a patch that makes the driver recognise a
forcePrimitives boolean flag in the JDBCUrl, and if it sees this it will
return primitive arrays, not object arrays.


Is this OK? Thanks -

Nick

-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Attachments:

force-primitives.patch (application/octet-stream, 6.0K) download diff --git a/org/postgresql/core/BaseConnection.java b/org/postgresql/core/BaseConnection.java index 6373d53..2ed095c 100644 --- a/org/postgresql/core/BaseConnection.java +++ b/org/postgresql/core/BaseConnection.java @@ -154,4 +154,13 @@ public interface BaseConnection extends PGConnection, Connection * @return True for binary transfer, false for text transfer. */ public boolean binaryTransferSend(int oid); + + /** + * True if primitive types should be returned wherever possible, false for + * the default (usually object types in 8.3+ as they can represent nulls). + * If true, {@code null} values should be represented as {@code 0}. + * + * @return True if primitive types should be returned wherever possible + */ + public boolean forcePrimitives(); } diff --git a/org/postgresql/jdbc2/AbstractJdbc2Array.java b/org/postgresql/jdbc2/AbstractJdbc2Array.java index 23bc4f7..7190754 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Array.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Array.java @@ -95,7 +95,7 @@ public abstract class AbstractJdbc2Array private AbstractJdbc2Array(BaseConnection connection, int oid) throws SQLException { this.connection = connection; this.oid = oid; - this.useObjects = connection.haveMinimumCompatibleVersion("8.3"); + this.useObjects = connection.haveMinimumCompatibleVersion("8.3") && !connection.forcePrimitives(); this.haveMinServer82 = connection.haveMinimumServerVersion("8.2"); } diff --git a/org/postgresql/jdbc2/AbstractJdbc2Connection.java b/org/postgresql/jdbc2/AbstractJdbc2Connection.java index 905df2b..b963a8c 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Connection.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Connection.java @@ -77,6 +77,8 @@ public abstract class AbstractJdbc2Connection implements BaseConnection /** Set of oids that use binary transfer when receiving from server. */ private Set<Integer> useBinaryReceiveForOids; + private final boolean forcePrimitives; + public abstract DatabaseMetaData getMetaData() throws SQLException; // @@ -260,6 +262,8 @@ public abstract class AbstractJdbc2Connection implements BaseConnection openStackTrace = new Throwable("Connection was created at this point:"); enableDriverManagerLogging(); } + + forcePrimitives = Boolean.parseBoolean(info.getProperty("forcePrimitives")); } private Set<Integer> getOidSet(String oidList) throws PSQLException { @@ -1234,4 +1238,9 @@ public abstract class AbstractJdbc2Connection implements BaseConnection { return protoConnection.getBackendPID(); } + + @Override + public boolean forcePrimitives() { + return forcePrimitives; + } } diff --git a/org/postgresql/test/TestUtil.java b/org/postgresql/test/TestUtil.java index 7c6c612..0042085 100644 --- a/org/postgresql/test/TestUtil.java +++ b/org/postgresql/test/TestUtil.java @@ -45,6 +45,11 @@ public class TestUtil sendBufferSize = "&sendBufferSize="+getSendBufferSize(); } + String forcePrimitives = ""; + if (getForcePrimitives() != null && !getForcePrimitives().equals("")) { + forcePrimitives = "&forcePrimitives="+getForcePrimitives(); + } + return "jdbc:postgresql://" + getServer() + ":" + getPort() + "/" @@ -54,7 +59,8 @@ public class TestUtil + protocolVersion + binaryTransfer + receiveBufferSize - + sendBufferSize; + + sendBufferSize + + forcePrimitives; } /* @@ -126,6 +132,11 @@ public class TestUtil return System.getProperty("binaryTransfer"); } + public static String getForcePrimitives() + { + return System.getProperty("forcePrimitives"); + } + public static int getSendBufferSize() { return Integer.parseInt(System.getProperty("sendBufferSize", "-1")); diff --git a/org/postgresql/test/jdbc4/Jdbc4TestSuite.java b/org/postgresql/test/jdbc4/Jdbc4TestSuite.java index 0bf8b30..fbc9455 100644 --- a/org/postgresql/test/jdbc4/Jdbc4TestSuite.java +++ b/org/postgresql/test/jdbc4/Jdbc4TestSuite.java @@ -31,6 +31,7 @@ public class Jdbc4TestSuite extends TestSuite suite.addTestSuite(ArrayTest.class); suite.addTestSuite(ConnectionTest.class); suite.addTestSuite(WrapperTest.class); + suite.addTestSuite(PrimitivesTest.class); Connection connection = TestUtil.openDB(); try diff --git a/org/postgresql/test/jdbc4/PrimitivesTest.java b/org/postgresql/test/jdbc4/PrimitivesTest.java new file mode 100644 index 0000000..a0708d0 --- /dev/null +++ b/org/postgresql/test/jdbc4/PrimitivesTest.java @@ -0,0 +1,46 @@ +package org.postgresql.test.jdbc4; + +import java.sql.Array; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.postgresql.test.TestUtil; + +import junit.framework.TestCase; + +public class PrimitivesTest extends TestCase { + + private Connection _conn; + + public PrimitivesTest(String name) { + super(name); + } + + protected void setUp() throws Exception { + System.setProperty("forcePrimitives", "true"); + _conn = TestUtil.openDB(); + } + + protected void tearDown() throws SQLException { + TestUtil.closeDB(_conn); + System.clearProperty("forcePrimitives"); + } + + public void testCreateArrayOfInt() throws SQLException { + PreparedStatement pstmt = _conn.prepareStatement("SELECT ?::int[]"); + Integer in[] = { 0, -1, 2 }; + pstmt.setArray(1, _conn.createArrayOf("int4", in)); + + ResultSet rs = pstmt.executeQuery(); + assertTrue(rs.next()); + Array arr = rs.getArray(1); + int out[] = (int[]) arr.getArray(); + + assertEquals(3, out.length); + assertEquals(0, out[0]); + assertEquals(-1, out[1]); + assertEquals(2, out[2]); + } +}
^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: Patch: Force Primitives
@ 2013-03-25 15:51  Nicholas White <n.j.white@gmail.com>
  parent: Nicholas White <n.j.white@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Nicholas White @ 2013-03-25 15:51 UTC (permalink / raw)
  To: pgsql-jdbc

I've revised the patch to make Array.getArray() return a primitive array
even when using the binary transfer format. I had to add extra code to
force a statement to do a describe query before executing (previously it
would only do this if the org.postgresql.forcebinary system property was
set), as the QueryExecutorImpl needs the Field objects to be populated when
sendBind is called, or the query results will be sent back in Text format.
The PrimitivesTest unit test my patch adds verifies that a column is being
returned in the correct (text or binary) format. I think this fixes an
outstanding bug; I believe binary transfers will only be used if the system
property is set, and the binaryTransfer JDBCUrl parameter is currently
useless.

Thanks -

Nick

-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Attachments:

force-primitives.patch (application/octet-stream, 16.7K) download diff --git a/org/postgresql/PGResultSetMetaData.java b/org/postgresql/PGResultSetMetaData.java index 56dc1c9..0b3fd77 100644 --- a/org/postgresql/PGResultSetMetaData.java +++ b/org/postgresql/PGResultSetMetaData.java @@ -9,6 +9,8 @@ package org.postgresql; import java.sql.SQLException; +import org.postgresql.core.Field; + public interface PGResultSetMetaData { @@ -36,4 +38,12 @@ public interface PGResultSetMetaData */ public String getBaseSchemaName(int column) throws SQLException; + /** + * Is a column Text or Binary? + * + * @see Field#BINARY_FORMAT + * @see Field#TEXT_FORMAT + * @since 9.4 + */ + public int getFormat(int column) throws SQLException; } diff --git a/org/postgresql/core/BaseConnection.java b/org/postgresql/core/BaseConnection.java index 6373d53..30fa883 100644 --- a/org/postgresql/core/BaseConnection.java +++ b/org/postgresql/core/BaseConnection.java @@ -154,4 +154,21 @@ public interface BaseConnection extends PGConnection, Connection * @return True for binary transfer, false for text transfer. */ public boolean binaryTransferSend(int oid); + + /** + * True if primitive types should be returned wherever possible, false for + * the default (usually object types in 8.3+ as they can represent nulls). + * If true, {@code null} values should be represented as {@code 0}. + * + * @return True if primitive types should be returned wherever possible + */ + public boolean forcePrimitives(); + + /** + * Whether we'll try to do binary transfers. This can be set via the + * <tt>binaryTransfer</tt> JDBCUrl parameter, and defaults to true. + * + * @return True if binary transfers were requested + */ + public boolean useBinaryTransfers(); } diff --git a/org/postgresql/jdbc2/AbstractJdbc2Array.java b/org/postgresql/jdbc2/AbstractJdbc2Array.java index 23bc4f7..ad71e02 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Array.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Array.java @@ -14,7 +14,9 @@ import org.postgresql.util.PSQLState; import org.postgresql.util.GT; import java.io.IOException; +import java.lang.reflect.Array; import java.math.BigDecimal; +import java.nio.ByteBuffer; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; @@ -95,7 +97,7 @@ public abstract class AbstractJdbc2Array private AbstractJdbc2Array(BaseConnection connection, int oid) throws SQLException { this.connection = connection; this.oid = oid; - this.useObjects = connection.haveMinimumCompatibleVersion("8.3"); + this.useObjects = connection.haveMinimumCompatibleVersion("8.3") && !connection.forcePrimitives(); this.haveMinServer82 = connection.haveMinimumServerVersion("8.2"); } @@ -182,14 +184,14 @@ public abstract class AbstractJdbc2Array /*int lbound = ByteConverter.int4(fieldBytes, pos);*/ pos += 4; } if (dimensions == 0) { - return java.lang.reflect.Array.newInstance(elementOidToClass(elementOid), 0); + return Array.newInstance(elementOidToClass(elementOid), 0); } if (count > 0) { dims[0] = Math.min(count, dims[0]); } - Object arr = java.lang.reflect.Array.newInstance(elementOidToClass(elementOid), dims); + Object arr = Array.newInstance(elementOidToClass(elementOid), dims); try { - storeValues((Object[]) arr, elementOid, dims, pos, 0, index); + storeValues(arr, elementOid, dims, pos, 0, index); } catch (IOException ioe) { @@ -198,7 +200,7 @@ public abstract class AbstractJdbc2Array return arr; } - private int storeValues(final Object[] arr, int elementOid, final int[] dims, int pos, final int thisDimension, int index) throws SQLException, IOException { + private int storeValues(final Object arr, int elementOid, final int[] dims, int pos, final int thisDimension, int index) throws SQLException, IOException { if (thisDimension == dims.length - 1) { for (int i = 1; i < index; ++i) { int len = ByteConverter.int4(fieldBytes, pos); pos += 4; @@ -213,31 +215,56 @@ public abstract class AbstractJdbc2Array } switch (elementOid) { case Oid.INT2: - arr[i] = new Short(ByteConverter.int2(fieldBytes, pos)); + short asShort = ByteConverter.int2(fieldBytes, pos); + if(useObjects) { + Array.set(arr, i, Short.valueOf(asShort)); + } else { + Array.setShort(arr, i, asShort); + } break; case Oid.INT4: - arr[i] = new Integer(ByteConverter.int4(fieldBytes, pos)); + int asInt = ByteConverter.int4(fieldBytes, pos); + if(useObjects) { + Array.set(arr, i, Integer.valueOf(asInt)); + } else { + Array.setInt(arr, i, asInt); + } break; case Oid.INT8: - arr[i] = new Long(ByteConverter.int8(fieldBytes, pos)); + long asLong = ByteConverter.int8(fieldBytes, pos); + if(useObjects) { + Array.set(arr, i, Long.valueOf(asLong)); + } else { + Array.setLong(arr, i, asLong); + } break; case Oid.FLOAT4: - arr[i] = new Float(ByteConverter.float4(fieldBytes, pos)); + float asFloat = ByteConverter.float4(fieldBytes, pos); + if(useObjects) { + Array.set(arr, i, Float.valueOf(asFloat)); + } else { + Array.setFloat(arr, i, asFloat); + } break; case Oid.FLOAT8: - arr[i] = new Double(ByteConverter.float8(fieldBytes, pos)); + double asDouble = ByteConverter.float8(fieldBytes, pos); + if(useObjects) { + Array.set(arr, i, Double.valueOf(asDouble)); + } else { + Array.setDouble(arr, i, asDouble); + } break; case Oid.TEXT: case Oid.VARCHAR: Encoding encoding = connection.getEncoding(); - arr[i] = encoding.decode(fieldBytes, pos, len); + Array.set(arr, i, encoding.decode(fieldBytes, pos, len)); break; } pos += len; } } else { for (int i = 0; i < dims[thisDimension]; ++i) { - pos = storeValues((Object[]) arr[i], elementOid, dims, pos, thisDimension + 1, 0); + pos = storeValues(Array.get(arr, i), elementOid, dims, pos, thisDimension + 1, 0); } } return pos; @@ -334,19 +361,19 @@ public abstract class AbstractJdbc2Array return pos; } - private Class elementOidToClass(int oid) + private Class<?> elementOidToClass(int oid) throws SQLException { switch (oid) { case Oid.INT2: - return Short.class; + return useObjects ? Short.class : short.class; case Oid.INT4: - return Integer.class; + return useObjects ? Integer.class : int.class; case Oid.INT8: - return Long.class; + return useObjects ? Long.class : long.class; case Oid.FLOAT4: - return Float.class; + return useObjects ? Float.class : float.class; case Oid.FLOAT8: - return Double.class; + return useObjects ? Double.class : double.class; case Oid.TEXT: case Oid.VARCHAR: return String.class; diff --git a/org/postgresql/jdbc2/AbstractJdbc2Connection.java b/org/postgresql/jdbc2/AbstractJdbc2Connection.java index 905df2b..71d0911 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Connection.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Connection.java @@ -77,6 +77,9 @@ public abstract class AbstractJdbc2Connection implements BaseConnection /** Set of oids that use binary transfer when receiving from server. */ private Set<Integer> useBinaryReceiveForOids; + private final boolean binaryTransfer; + private final boolean forcePrimitives; + public abstract DatabaseMetaData getMetaData() throws SQLException; // @@ -119,14 +122,8 @@ public abstract class AbstractJdbc2Connection implements BaseConnection catch (Exception e) { } - boolean binaryTransfer = true; - try - { + binaryTransfer = Boolean.valueOf(info.getProperty("binaryTransfer", "true")).booleanValue(); - } - catch (Exception e) - { - } //Print out the driver version number if (logger.logInfo()) @@ -260,6 +257,8 @@ public abstract class AbstractJdbc2Connection implements BaseConnection openStackTrace = new Throwable("Connection was created at this point:"); enableDriverManagerLogging(); } + + forcePrimitives = Boolean.parseBoolean(info.getProperty("forcePrimitives")); } private Set<Integer> getOidSet(String oidList) throws PSQLException { @@ -1234,4 +1233,14 @@ public abstract class AbstractJdbc2Connection implements BaseConnection { return protoConnection.getBackendPID(); } + + @Override + public boolean forcePrimitives() { + return forcePrimitives; + } + + @Override + public boolean useBinaryTransfers() { + return binaryTransfer; + } } diff --git a/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java b/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java index 160f2d1..3df5147 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java +++ b/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java @@ -12,6 +12,7 @@ import org.postgresql.core.*; import org.postgresql.util.PSQLException; import org.postgresql.util.PSQLState; import java.sql.*; + import org.postgresql.util.GT; public abstract class AbstractJdbc2ResultSetMetaData implements PGResultSetMetaData @@ -335,6 +336,17 @@ public abstract class AbstractJdbc2ResultSetMetaData implements PGResultSetMetaD } /* + * Is a column Text or Binary? + * + * @param column the first column is 1, the second is 2... + * @return column name, or "" if not applicable + * @exception SQLException if a database access error occurs + */ + public int getFormat(int column) throws SQLException { + return getField(column).getFormat(); + } + + /* * Whats is the column's data source specific type name? * * @param column the first column is 1, the second is 2, etc. diff --git a/org/postgresql/jdbc2/AbstractJdbc2Statement.java b/org/postgresql/jdbc2/AbstractJdbc2Statement.java index 7671cf6..c6b1855 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Statement.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Statement.java @@ -540,7 +540,10 @@ public abstract class AbstractJdbc2Statement implements BaseStatement if (concurrency != ResultSet.CONCUR_READ_ONLY) flags |= QueryExecutor.QUERY_NO_BINARY_TRANSFER; - if (ForceBinaryTransfers) { + // we need to know what Fields are in the ResultSet so we can + // ask the database to return them in binary format (see + // sendBind in QueryExecutorImpl). + if (ForceBinaryTransfers || connection.useBinaryTransfers()) { int flags2 = flags | QueryExecutor.QUERY_DESCRIBE_ONLY; StatementResultHandler handler2 = new StatementResultHandler(); connection.getQueryExecutor().execute(queryToExecute, queryParameters, handler2, 0, 0, flags2); @@ -2617,7 +2620,7 @@ public abstract class AbstractJdbc2Statement implements BaseStatement public void setPrepareThreshold(int newThreshold) throws SQLException { checkClosed(); - if (ForceBinaryTransfers) + if (ForceBinaryTransfers || connection.useBinaryTransfers()) newThreshold = 1; if (newThreshold < 0) diff --git a/org/postgresql/test/TestUtil.java b/org/postgresql/test/TestUtil.java index 7c6c612..0042085 100644 --- a/org/postgresql/test/TestUtil.java +++ b/org/postgresql/test/TestUtil.java @@ -45,6 +45,11 @@ public class TestUtil sendBufferSize = "&sendBufferSize="+getSendBufferSize(); } + String forcePrimitives = ""; + if (getForcePrimitives() != null && !getForcePrimitives().equals("")) { + forcePrimitives = "&forcePrimitives="+getForcePrimitives(); + } + return "jdbc:postgresql://" + getServer() + ":" + getPort() + "/" @@ -54,7 +59,8 @@ public class TestUtil + protocolVersion + binaryTransfer + receiveBufferSize - + sendBufferSize; + + sendBufferSize + + forcePrimitives; } /* @@ -126,6 +132,11 @@ public class TestUtil return System.getProperty("binaryTransfer"); } + public static String getForcePrimitives() + { + return System.getProperty("forcePrimitives"); + } + public static int getSendBufferSize() { return Integer.parseInt(System.getProperty("sendBufferSize", "-1")); diff --git a/org/postgresql/test/jdbc4/Jdbc4TestSuite.java b/org/postgresql/test/jdbc4/Jdbc4TestSuite.java index 0bf8b30..fbc9455 100644 --- a/org/postgresql/test/jdbc4/Jdbc4TestSuite.java +++ b/org/postgresql/test/jdbc4/Jdbc4TestSuite.java @@ -31,6 +31,7 @@ public class Jdbc4TestSuite extends TestSuite suite.addTestSuite(ArrayTest.class); suite.addTestSuite(ConnectionTest.class); suite.addTestSuite(WrapperTest.class); + suite.addTestSuite(PrimitivesTest.class); Connection connection = TestUtil.openDB(); try diff --git a/org/postgresql/test/jdbc4/PrimitivesTest.java b/org/postgresql/test/jdbc4/PrimitivesTest.java new file mode 100644 index 0000000..b17a255 --- /dev/null +++ b/org/postgresql/test/jdbc4/PrimitivesTest.java @@ -0,0 +1,76 @@ +package org.postgresql.test.jdbc4; + +import java.sql.Array; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.postgresql.PGResultSetMetaData; +import org.postgresql.core.Field; +import org.postgresql.jdbc2.AbstractJdbc2ResultSetMetaData; +import org.postgresql.test.TestUtil; + +import junit.framework.TestCase; + +public class PrimitivesTest extends TestCase { + + private Connection _conn; + + public PrimitivesTest(String name) { + super(name); + } + + protected void tearDown() throws Exception { + TestUtil.closeDB(_conn); + System.clearProperty("forcePrimitives"); + System.clearProperty("binaryTransfer"); + } + + public void testCreateArrayOfIntText() throws Exception { + System.setProperty("binaryTransfer", "false"); + System.setProperty("forcePrimitives", "true"); + _conn = TestUtil.openDB(); + + PreparedStatement pstmt = _conn.prepareStatement("SELECT ?::int[]"); + Integer in[] = { 0, -1, 2 }; + pstmt.setArray(1, _conn.createArrayOf("int4", in)); + + ResultSet rs = pstmt.executeQuery(); + assertEquals(Field.TEXT_FORMAT, ((PGResultSetMetaData)rs.getMetaData()).getFormat(1)); + + assertTrue(rs.next()); + Array arr = rs.getArray(1); + int out[] = (int[]) arr.getArray(); + + assertEquals(3, out.length); + assertEquals(0, out[0]); + assertEquals(-1, out[1]); + assertEquals(2, out[2]); + } + + public void testCreateArrayOfIntBinary() throws Exception { + System.setProperty("forcePrimitives", "true"); + System.setProperty("binaryTransfer", "true"); + _conn = TestUtil.openDB(); + + PreparedStatement pstmt = _conn.prepareStatement( + "SELECT ?::int[]", + ResultSet.TYPE_FORWARD_ONLY, + ResultSet.CONCUR_READ_ONLY); + Integer in[] = { 0, -1, 2 }; + pstmt.setArray(1, _conn.createArrayOf("int4", in)); + + ResultSet rs = pstmt.executeQuery(); + assertEquals(Field.BINARY_FORMAT, ((PGResultSetMetaData)rs.getMetaData()).getFormat(1)); + + assertTrue(rs.next()); + Array arr = rs.getArray(1); + int out[] = (int[]) arr.getArray(); + + assertEquals(3, out.length); + assertEquals(0, out[0]); + assertEquals(-1, out[1]); + assertEquals(2, out[2]); + } +}
^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: Patch: Force Primitives
@ 2013-03-25 17:24  Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
  parent: Nicholas White <n.j.white@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Mikko Tiihonen @ 2013-03-25 17:24 UTC (permalink / raw)
  To: pgsql-jdbc

On 03/25/2013 05:51 PM, Nicholas White wrote:
> I've revised the patch to make Array.getArray() return a primitive array even when using the binary transfer format. I had to add extra code to force a
> statement to do a describe query before executing (previously it would only do this if the org.postgresql.forcebinary system property was set), as the
> QueryExecutorImpl needs the Field objects to be populated when sendBind is called, or the query results will be sent back in Text format. The PrimitivesTest
> unit test my patch adds verifies that a column is being returned in the correct (text or binary) format. I think this fixes an outstanding bug; I believe binary
> transfers will only be used if the system property is set, and the binaryTransfer JDBCUrl parameter is currently useless.

By default the binary transfers for receive kick in only after the 3rd execution of a prepared statement. Do you see the binary transfers activating for array 
receives if you run your prepared statement select in a loop?

The forceBinary option is for unit tests that do not want to repeat every statement 3 times.

-Mikko


-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Patch: Force Primitives
@ 2013-03-25 18:24  Nicholas White <n.j.white@gmail.com>
  parent: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Nicholas White @ 2013-03-25 18:24 UTC (permalink / raw)
  To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; +Cc: pgsql-jdbc

> Do you see the binary transfers activating for array receives if you run
your prepared statement select in a loop?
That's the behaviour I see, athough something's setting my
m_prepareThreshold to 5 rather than 3. I'm essentially using postgres as a
persistent cache for my application server; when my app server starts it
loads large amounts of data from postgres using a series of select-*-from-x
queries. In order to minimise network I/O I'd ideally like a way to ensure
I'm using the binary protocol from the very first query. Should I submit
another patch that lets you configure this behaviour (either via a new
JDBCUrl parameter or whether binaryTransfer is explicitly specified)?

Separately, do you know why this behaviour is the default? Is the binary
encoding more expensive (either server-side or client-side) than text
encoding the data?

Thanks -

Nick

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Patch: Force Primitives
@ 2013-03-25 18:43  Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
  parent: Nicholas White <n.j.white@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Mikko Tiihonen @ 2013-03-25 18:43 UTC (permalink / raw)
  To: Nicholas White <n.j.white@gmail.com>; +Cc: pgsql-jdbc

On 03/25/2013 08:24 PM, Nicholas White wrote:
>  > Do you see the binary transfers activating for array receives if you run your prepared statement select in a loop?
> That's the behaviour I see, athough something's setting my m_prepareThreshold to 5 rather than 3. I'm essentially using postgres as a persistent cache for my
> application server; when my app server starts it loads large amounts of data from postgres using a series of select-*-from-x queries. In order to minimise
> network I/O I'd ideally like a way to ensure I'm using the binary protocol from the very first query. Should I submit another patch that lets you configure this
> behaviour (either via a new JDBCUrl parameter or whether binaryTransfer is explicitly specified)?

I can't remember how the prepareTheshold goes. Is it so that even setting it to 1 (0 disables them) you still get real prepared statements (and at the same
time binary transfers) on the second execution. If you have an application that benefits from binary transfers on the first execution then yes, we should add
an option to enable that behaviour. So another patch for that would be nice.

> Separately, do you know why this behaviour is the default? Is the binary encoding more expensive (either server-side or client-side) than text encoding the data?

It's not because of binary encoding, which is faster on both server and client side.

The current functionality tries to avoid the extra describe overhead (is it even a roundtrip?) for prepared statements that are only executed once (or very few 
times), which is quite common in many applications that seem to use prepared statements also for one-shot cases.

PS. if you are concerned about binary array transfer performance I did some work last year to optimize the protocol for arrays that do not contain nulls. 
Unfortunately there was never any resolution to the server side patches. The discussion got stuck on weather updates with feature flags, adding a minor version 
number to the protocol or just wait for pg 10 to break the protocol is the correct way to go forward.

You can grab the server side + jdbc patches from here: http://www.postgresql.org/message-id/4ECC186A.30709@nitorcreations.com

-Mikko


-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Patch: Force Primitives
@ 2013-03-25 19:40  Nicholas White <n.j.white@gmail.com>
  parent: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Nicholas White @ 2013-03-25 19:40 UTC (permalink / raw)
  To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; +Cc: pgsql-jdbc

> So another patch for that would be nice.

I've attached a patch that fixes setPrepareThreshold; previously setting
the threshold to n would mean the n+1th query would be the first binary
one, and setting it to zero meant the binary protocol would never be used.
This means the first query of a statement could never use the binary
protocol. The patch makes setting the threshold to n mean the nth query is
binary, so setting it to 1 would make the first query use the binary
protocol. The patch includes some test cases that demonstrate this.

> The discussion got stuck on weather updates with feature flags, adding a
minor version number to the protocol or just wait for pg 10 to break the
protocol is the correct way to go forward.

That patch is pretty interesting - but maybe you could preserve
backwards-compatibility by making a new format type (so 0 is text, 1 is
binary and 2 is binary-with-implied-sizes). A new format type would give
you more flexibility; you could even use variable-length or run-length
encodings, or compression to represent (only large?) arrays.

Thanks -

Nick

-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Attachments:

set-prepare-threshold-bug.patch (application/octet-stream, 6.7K) download diff --git a/org/postgresql/PGResultSetMetaData.java b/org/postgresql/PGResultSetMetaData.java index 56dc1c9..0b3fd77 100644 --- a/org/postgresql/PGResultSetMetaData.java +++ b/org/postgresql/PGResultSetMetaData.java @@ -9,6 +9,8 @@ package org.postgresql; import java.sql.SQLException; +import org.postgresql.core.Field; + public interface PGResultSetMetaData { @@ -36,4 +38,12 @@ public interface PGResultSetMetaData */ public String getBaseSchemaName(int column) throws SQLException; + /** + * Is a column Text or Binary? + * + * @see Field#BINARY_FORMAT + * @see Field#TEXT_FORMAT + * @since 9.4 + */ + public int getFormat(int column) throws SQLException; } diff --git a/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java b/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java index 160f2d1..84a08a3 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java +++ b/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java @@ -335,6 +335,17 @@ public abstract class AbstractJdbc2ResultSetMetaData implements PGResultSetMetaD } /* + * Is a column Text or Binary? + * + * @param column the first column is 1, the second is 2... + * @return column name, or "" if not applicable + * @exception SQLException if a database access error occurs + */ + public int getFormat(int column) throws SQLException { + return getField(column).getFormat(); + } + + /* * Whats is the column's data source specific type name? * * @param column the first column is 1, the second is 2, etc. diff --git a/org/postgresql/jdbc2/AbstractJdbc2Statement.java b/org/postgresql/jdbc2/AbstractJdbc2Statement.java index 7671cf6..0ccc7b1 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Statement.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Statement.java @@ -525,7 +525,8 @@ public abstract class AbstractJdbc2Statement implements BaseStatement flags &= ~(QueryExecutor.QUERY_NO_RESULTS); } - // Only use named statements after we hit the threshold + // Only use named statements after we hit the threshold. Note that only + // named statements can be transferred in binary format. if (preparedQuery != null) { ++m_useCount; // We used this statement once more. @@ -540,7 +541,7 @@ public abstract class AbstractJdbc2Statement implements BaseStatement if (concurrency != ResultSet.CONCUR_READ_ONLY) flags |= QueryExecutor.QUERY_NO_BINARY_TRANSFER; - if (ForceBinaryTransfers) { + if (ForceBinaryTransfers || (flags & QueryExecutor.QUERY_ONESHOT) == 0) { int flags2 = flags | QueryExecutor.QUERY_DESCRIBE_ONLY; StatementResultHandler handler2 = new StatementResultHandler(); connection.getQueryExecutor().execute(queryToExecute, queryParameters, handler2, 0, 0, flags2); diff --git a/org/postgresql/test/jdbc4/BinaryTest.java b/org/postgresql/test/jdbc4/BinaryTest.java new file mode 100644 index 0000000..fddfa75 --- /dev/null +++ b/org/postgresql/test/jdbc4/BinaryTest.java @@ -0,0 +1,94 @@ +package org.postgresql.test.jdbc4; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.postgresql.PGConnection; +import org.postgresql.PGResultSetMetaData; +import org.postgresql.PGStatement; +import org.postgresql.core.Field; +import org.postgresql.test.TestUtil; + +import junit.framework.TestCase; + +/** + * We don't want to use the binary protocol for one-off queries as it involves + * another round-trip to the server to 'describe' the query. If we use the query + * enough times (see {@link PGConnection#setPrepareThreshold(int)} then we'll + * change to using the binary protocol to save bandwidth and reduce decoding + * time. + */ +public class BinaryTest extends TestCase { + private Connection connection; + private ResultSet results; + private PreparedStatement statement; + + public BinaryTest(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + connection = TestUtil.openDB(); + statement = connection.prepareStatement("select 1"); + } + + @Override + protected void tearDown() throws Exception { + TestUtil.closeDB(connection); + } + + public void testPreparedStatement_3() throws Exception { + ((PGStatement) statement).setPrepareThreshold(3); + + results = statement.executeQuery(); + assertEquals(Field.TEXT_FORMAT, getFormat(results)); + + results = statement.executeQuery(); + assertEquals(Field.TEXT_FORMAT, getFormat(results)); + + results = statement.executeQuery(); + assertEquals(Field.BINARY_FORMAT, getFormat(results)); + + results = statement.executeQuery(); + assertEquals(Field.BINARY_FORMAT, getFormat(results)); + } + + public void testPreparedStatement_1() throws Exception { + ((PGStatement) statement).setPrepareThreshold(1); + + results = statement.executeQuery(); + assertEquals(Field.BINARY_FORMAT, getFormat(results)); + + results = statement.executeQuery(); + assertEquals(Field.BINARY_FORMAT, getFormat(results)); + + results = statement.executeQuery(); + assertEquals(Field.BINARY_FORMAT, getFormat(results)); + + results = statement.executeQuery(); + assertEquals(Field.BINARY_FORMAT, getFormat(results)); + } + + public void testPreparedStatement_0() throws Exception { + ((PGStatement) statement).setPrepareThreshold(0); + + results = statement.executeQuery(); + assertEquals(Field.TEXT_FORMAT, getFormat(results)); + + results = statement.executeQuery(); + assertEquals(Field.TEXT_FORMAT, getFormat(results)); + + results = statement.executeQuery(); + assertEquals(Field.TEXT_FORMAT, getFormat(results)); + + results = statement.executeQuery(); + assertEquals(Field.TEXT_FORMAT, getFormat(results)); + } + + private int getFormat(ResultSet results) throws SQLException { + return ((PGResultSetMetaData) results.getMetaData()).getFormat(1); + } +} diff --git a/org/postgresql/test/jdbc4/Jdbc4TestSuite.java b/org/postgresql/test/jdbc4/Jdbc4TestSuite.java index 0bf8b30..ca0b175 100644 --- a/org/postgresql/test/jdbc4/Jdbc4TestSuite.java +++ b/org/postgresql/test/jdbc4/Jdbc4TestSuite.java @@ -31,6 +31,7 @@ public class Jdbc4TestSuite extends TestSuite suite.addTestSuite(ArrayTest.class); suite.addTestSuite(ConnectionTest.class); suite.addTestSuite(WrapperTest.class); + suite.addTestSuite(BinaryTest.class); Connection connection = TestUtil.openDB(); try
^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: Patch: Force Primitives
@ 2013-04-12 15:52  Nicholas White <n.j.white@gmail.com>
  parent: Nicholas White <n.j.white@gmail.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Nicholas White @ 2013-04-12 15:52 UTC (permalink / raw)
  To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; +Cc: pgsql-jdbc

I've submitting the setPrepareThreshold patch as a pull request here:
https://github.com/pgjdbc/pgjdbc/pull/51 . Could you review it? Thanks -

Nick

^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2013-04-12 15:52 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-03-24 04:10 Patch: Force Primitives Nicholas White <n.j.white@gmail.com>
2013-03-25 15:51 ` Nicholas White <n.j.white@gmail.com>
2013-03-25 17:24   ` Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
2013-03-25 18:24     ` Nicholas White <n.j.white@gmail.com>
2013-03-25 18:43       ` Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
2013-03-25 19:40         ` Nicholas White <n.j.white@gmail.com>
2013-04-12 15:52           ` Nicholas White <n.j.white@gmail.com>

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