Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id F07619F9FB2 for ; Sun, 17 Dec 2006 15:47:55 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 31071-02 for ; Sun, 17 Dec 2006 15:47:39 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from gw02.mail.saunalahti.fi (gw02.mail.saunalahti.fi [195.197.172.116]) by postgresql.org (Postfix) with ESMTP id D96189F9386 for ; Sun, 17 Dec 2006 15:47:40 -0400 (AST) Received: from [10.0.0.4] (a88-115-1-94.elisa-laajakaista.fi [88.115.1.94]) (using SSLv3 with cipher RC4-MD5 (128/128 bits)) (No client certificate requested) by gw02.mail.saunalahti.fi (Postfix) with ESMTP id 8CDDD1394CC for ; Sun, 17 Dec 2006 21:47:38 +0200 (EET) Subject: Binary binding patch v1 From: Mikko Tiihonen To: pgsql-jdbc@postgresql.org Content-Type: multipart/mixed; boundary="=-kfCdGSCEc02DxrZ2njUO" Date: Sun, 17 Dec 2006 21:47:37 +0200 Message-Id: <1166384857.6923.30.camel@dual.local> Mime-Version: 1.0 X-Mailer: Evolution 2.8.2.1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/95 X-Sequence-Number: 16327 --=-kfCdGSCEc02DxrZ2njUO Content-Type: text/plain Content-Transfer-Encoding: 7bit Hi, It seems I found nothing better to do... So here is the first patch that allows using of binary transfers for bind data. The patch must be applied on top of the binaryResultSet-v6.patch. The old code supported binary transfers only for BYTEA type. Now also INT2, INT4, INT8, FLOAT4 and FLOAT8 are supported. The patch is actually quite simple, but testing for performance improvements is harder than for selects because the backend settings have larger effect on the result. My current results show around 5-10% speedup and 20-30% memory use reduction, measured from the test code. I started working on DATE/TIME/TIMEZONE binary sending too, but I'm afraid it might not be possible. At least based on the long comments describing problems with the current text format sending which requires the use of UNSPECIFIED oid. I did actually get DATE to work by sending it as Oid.DATE. But since the java.sql.Date actually contains also the time part I'm not sure if it should be sent using Oid.TIMESTAMP. Actually I think if binary sending of time/date is ever done it most likely should always use TIMESTAMPTZ format and then let the backend do the conversion to correct type. But as the comments in the jdbc driver show the backend seems a bit buggy in this regard. -Mikko --=-kfCdGSCEc02DxrZ2njUO Content-Disposition: attachment; filename=binarySend-v1.patch Content-Type: text/x-patch; name=binarySend-v1.patch; charset=us-ascii Content-Transfer-Encoding: 7bit diff -uNr --exclude='*.jar' --exclude='*.class' --exclude=CVS pgjdbc.oldbin5/org/postgresql/core/BaseConnection.java pgjdbc/org/postgresql/core/BaseConnection.java --- pgjdbc.oldbin5/org/postgresql/core/BaseConnection.java 2006-12-15 20:44:30.000000000 +0200 +++ pgjdbc/org/postgresql/core/BaseConnection.java 2006-12-17 20:40:44.000000000 +0200 @@ -176,4 +176,13 @@ // Get the bind-string-as-varchar config flag public boolean getStringVarcharFlag(); + + /** + * Returns true if value for the given oid should be sent using binary + * transfer. False if value should be sent using text transfer. + * + * @param oid The oid to check. + * @return True for binary transfer, false for text transfer. + */ + public boolean binaryTransferSend(int oid); } diff -uNr --exclude='*.jar' --exclude='*.class' --exclude=CVS pgjdbc.oldbin5/org/postgresql/core/ParameterList.java pgjdbc/org/postgresql/core/ParameterList.java --- pgjdbc.oldbin5/org/postgresql/core/ParameterList.java 2006-05-22 12:52:36.000000000 +0300 +++ pgjdbc/org/postgresql/core/ParameterList.java 2006-12-17 20:40:44.000000000 +0200 @@ -123,6 +123,17 @@ void setBytea(int index, InputStream stream, int length) throws SQLException; /** + * Binds given byte[] value to a parameter. The bytes must already + * be in correct format matching the OID. + * + * @param index the 1-based parameter index to bind. + * @param value the bytes to send. + * @param oid the type OID of the parameter. + * @throws SQLException on error or if index is out of range + */ + void setBinaryParameter(int index, byte[] value, int oid) throws SQLException; + + /** * Binds a SQL NULL value to a parameter. * Associated with the parameter is a typename for the parameter that * should correspond to an entry in pg_types. diff -uNr --exclude='*.jar' --exclude='*.class' --exclude=CVS pgjdbc.oldbin5/org/postgresql/core/v2/FastpathParameterList.java pgjdbc/org/postgresql/core/v2/FastpathParameterList.java --- pgjdbc.oldbin5/org/postgresql/core/v2/FastpathParameterList.java 2006-05-22 12:52:36.000000000 +0300 +++ pgjdbc/org/postgresql/core/v2/FastpathParameterList.java 2006-12-17 20:40:44.000000000 +0200 @@ -155,6 +155,10 @@ Arrays.fill(paramValues, null); } + public void setBinaryParameter(int index, byte[] value, int oid) { + throw new UnsupportedOperationException(); + } + private final Object[] paramValues; } diff -uNr --exclude='*.jar' --exclude='*.class' --exclude=CVS pgjdbc.oldbin5/org/postgresql/core/v2/SimpleParameterList.java pgjdbc/org/postgresql/core/v2/SimpleParameterList.java --- pgjdbc.oldbin5/org/postgresql/core/v2/SimpleParameterList.java 2006-12-15 20:44:30.000000000 +0200 +++ pgjdbc/org/postgresql/core/v2/SimpleParameterList.java 2006-12-17 20:40:44.000000000 +0200 @@ -167,6 +167,10 @@ public void clear() { Arrays.fill(paramValues, null); } + + public void setBinaryParameter(int index, byte[] value, int oid) { + throw new UnsupportedOperationException(); + } private final Object[] paramValues; diff -uNr --exclude='*.jar' --exclude='*.class' --exclude=CVS pgjdbc.oldbin5/org/postgresql/core/v3/CompositeParameterList.java pgjdbc/org/postgresql/core/v3/CompositeParameterList.java --- pgjdbc.oldbin5/org/postgresql/core/v3/CompositeParameterList.java 2006-05-22 12:52:37.000000000 +0300 +++ pgjdbc/org/postgresql/core/v3/CompositeParameterList.java 2006-12-17 20:40:44.000000000 +0200 @@ -84,6 +84,11 @@ subparams[sub].setStringParameter(index - offsets[sub], value, oid); } + public void setBinaryParameter(int index, byte[] value, int oid) throws SQLException { + int sub = findSubParam(index); + subparams[sub].setBinaryParameter(index, value, oid); + } + public void setBytea(int index, byte[] data, int offset, int length) throws SQLException { int sub = findSubParam(index); subparams[sub].setBytea(index - offsets[sub], data, offset, length); diff -uNr --exclude='*.jar' --exclude='*.class' --exclude=CVS pgjdbc.oldbin5/org/postgresql/core/v3/SimpleParameterList.java pgjdbc/org/postgresql/core/v3/SimpleParameterList.java --- pgjdbc.oldbin5/org/postgresql/core/v3/SimpleParameterList.java 2006-05-24 02:05:21.000000000 +0300 +++ pgjdbc/org/postgresql/core/v3/SimpleParameterList.java 2006-12-17 20:40:44.000000000 +0200 @@ -31,12 +31,15 @@ private final static int IN = 1; private final static int OUT = 2; private final static int INOUT = IN|OUT; + + private final static int TEXT = 0; + private final static int BINARY = 4; SimpleParameterList(int paramCount) { this.paramValues = new Object[paramCount]; this.paramTypes = new int[paramCount]; this.encoded = new byte[paramCount][]; - this.direction = new int[paramCount]; + this.flags = new int[paramCount]; } public void registerOutParameter( int index, int sqlType ) throws SQLException @@ -44,18 +47,18 @@ if (index < 1 || index > paramValues.length) throw new PSQLException(GT.tr("The column index is out of range: {0}, number of columns: {1}.", new Object[]{new Integer(index), new Integer(paramValues.length)}), PSQLState.INVALID_PARAMETER_VALUE ); - direction[index-1] |= OUT; + flags[index-1] |= OUT; } - private void bind(int index, Object value, int oid) throws SQLException { + private void bind(int index, Object value, int oid, int binary) throws SQLException { if (index < 1 || index > paramValues.length) throw new PSQLException(GT.tr("The column index is out of range: {0}, number of columns: {1}.", new Object[]{new Integer(index), new Integer(paramValues.length)}), PSQLState.INVALID_PARAMETER_VALUE ); --index; encoded[index] = null; - paramValues[index] = value ; - direction[index] |= IN; + paramValues[index] = value; + flags[index] = (flags[index] & INOUT) | IN | binary; // If we are setting something to an UNSPECIFIED NULL, don't overwrite // our existing type for it. We don't need the correct type info to @@ -76,7 +79,7 @@ int count=0; for( int i=paramTypes.length; --i >= 0;) { - if ((direction[i] & OUT) == OUT ) + if ((flags[i] & OUT) == OUT ) { count++; } @@ -92,7 +95,7 @@ int count=0; for( int i=0; i< paramTypes.length;i++) { - if (direction[i] != OUT ) + if (flags[i] != OUT ) { count++; } @@ -106,27 +109,31 @@ data[2] = (byte)(value >> 8); data[1] = (byte)(value >> 16); data[0] = (byte)(value >> 24); - bind(index, data, Oid.INT4); + bind(index, data, Oid.INT4, BINARY); } public void setLiteralParameter(int index, String value, int oid) throws SQLException { - bind(index, value, oid); + bind(index, value, oid, TEXT); } public void setStringParameter(int index, String value, int oid) throws SQLException { - bind(index, value, oid); + bind(index, value, oid, TEXT); + } + + public void setBinaryParameter(int index, byte[] value, int oid) throws SQLException { + bind(index, value, oid, BINARY); } public void setBytea(int index, byte[] data, int offset, int length) throws SQLException { - bind(index, new StreamWrapper(data, offset, length), Oid.BYTEA); + bind(index, new StreamWrapper(data, offset, length), Oid.BYTEA, BINARY); } public void setBytea(int index, InputStream stream, int length) throws SQLException { - bind(index, new StreamWrapper(stream, length), Oid.BYTEA); + bind(index, new StreamWrapper(stream, length), Oid.BYTEA, BINARY); } public void setNull(int index, int oid) throws SQLException { - bind(index, NULL_OBJECT, oid); + bind(index, NULL_OBJECT, oid, BINARY); } public String toString(int index) { @@ -142,7 +149,7 @@ public void checkAllParametersSet() throws SQLException { for (int i = 0; i < paramTypes.length; ++i) { - if (direction[i] != OUT && paramValues[i] == null) + if (flags[i] != OUT && paramValues[i] == null) throw new PSQLException(GT.tr("No value specified for parameter {0}.", new Integer(i + 1)), PSQLState.INVALID_PARAMETER_VALUE); } } @@ -171,7 +178,7 @@ // int getTypeOID(int index) { - if (direction[index-1] == OUT) + if (flags[index-1] == OUT) { paramTypes[index-1] = Oid.VOID; paramValues[index-1] = "null"; @@ -202,8 +209,7 @@ } boolean isBinary(int index) { - // Currently, only StreamWrapper uses the binary parameter form. - return (paramValues[index-1] instanceof StreamWrapper); + return (flags[index-1] & BINARY) != 0; } int getV3Length(int index) { @@ -264,7 +270,7 @@ SimpleParameterList newCopy = new SimpleParameterList(paramValues.length); System.arraycopy(paramValues, 0, newCopy.paramValues, 0, paramValues.length); System.arraycopy(paramTypes, 0, newCopy.paramTypes, 0, paramTypes.length); - System.arraycopy(direction, 0, newCopy.direction, 0, direction.length); + System.arraycopy(flags, 0, newCopy.flags, 0, flags.length); return newCopy; } @@ -272,7 +278,7 @@ Arrays.fill(paramValues, null); Arrays.fill(paramTypes, 0); Arrays.fill(encoded, null); - Arrays.fill(direction, 0); + Arrays.fill(flags, 0); } public SimpleParameterList[] getSubparams() { return null; @@ -280,7 +286,7 @@ private final Object[] paramValues; private final int[] paramTypes; - private final int[] direction; + private final int[] flags; private final byte[][] encoded; /** diff -uNr --exclude='*.jar' --exclude='*.class' --exclude=CVS pgjdbc.oldbin5/org/postgresql/jdbc2/AbstractJdbc2Connection.java pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java --- pgjdbc.oldbin5/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2006-12-15 20:45:06.000000000 +0200 +++ pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2006-12-17 20:40:44.000000000 +0200 @@ -13,6 +13,7 @@ import java.io.PrintWriter; import java.sql.*; import java.util.*; + import org.postgresql.core.*; import org.postgresql.Driver; import org.postgresql.PGNotification; @@ -73,6 +74,11 @@ // Current warnings; there might be more on protoConnection too. public SQLWarning firstWarning = null; + /** + * BitSet of oids that use binary transfers. + */ + private BitSet useBinaryForOids; + public abstract DatabaseMetaData getMetaData() throws SQLException; // @@ -136,7 +142,7 @@ // Bit set that has a bit set for each oid which should be received // using binary format. - BitSet useBinaryForOids = new BitSet(); + useBinaryForOids = new BitSet(); if (binaryTransfer && protoConnection.getProtocolVersion() >= 3) { // currently supported binary encodings useBinaryForOids.set(Oid.BYTEA); @@ -1141,4 +1147,8 @@ { return bindStringAsVarchar; } + + public boolean binaryTransferSend(int oid) { + return useBinaryForOids.get(oid); + } } diff -uNr --exclude='*.jar' --exclude='*.class' --exclude=CVS pgjdbc.oldbin5/org/postgresql/jdbc2/AbstractJdbc2Statement.java pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java --- pgjdbc.oldbin5/org/postgresql/jdbc2/AbstractJdbc2Statement.java 2006-12-15 20:45:06.000000000 +0200 +++ pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java 2006-12-17 20:52:41.000000000 +0200 @@ -24,6 +24,7 @@ import org.postgresql.largeobject.*; import org.postgresql.core.*; import org.postgresql.core.types.*; +import org.postgresql.util.ByteConverter; import org.postgresql.util.PSQLException; import org.postgresql.util.PSQLState; import org.postgresql.util.PGobject; @@ -1123,8 +1124,7 @@ */ public void setByte(int parameterIndex, byte x) throws SQLException { - checkClosed(); - bindLiteral(parameterIndex, Integer.toString(x), Oid.INT2); + setShort(parameterIndex, x); } /* @@ -1138,6 +1138,12 @@ public void setShort(int parameterIndex, short x) throws SQLException { checkClosed(); + if (connection.binaryTransferSend(Oid.INT2)) { + byte[] val = new byte[2]; + ByteConverter.int2(val, 0, x); + preparedParameters.setBinaryParameter(parameterIndex, val, Oid.INT2); + return; + } bindLiteral(parameterIndex, Integer.toString(x), Oid.INT2); } @@ -1151,7 +1157,13 @@ */ public void setInt(int parameterIndex, int x) throws SQLException { - checkClosed(); + checkClosed(); + if (connection.binaryTransferSend(Oid.INT4)) { + byte[] val = new byte[4]; + ByteConverter.int4(val, 0, x); + preparedParameters.setBinaryParameter(parameterIndex, val, Oid.INT4); + return; + } bindLiteral(parameterIndex, Integer.toString(x), Oid.INT4); } @@ -1166,6 +1178,12 @@ public void setLong(int parameterIndex, long x) throws SQLException { checkClosed(); + if (connection.binaryTransferSend(Oid.INT8)) { + byte[] val = new byte[8]; + ByteConverter.int8(val, 0, x); + preparedParameters.setBinaryParameter(parameterIndex, val, Oid.INT8); + return; + } bindLiteral(parameterIndex, Long.toString(x), Oid.INT8); } @@ -1180,6 +1198,12 @@ public void setFloat(int parameterIndex, float x) throws SQLException { checkClosed(); + if (connection.binaryTransferSend(Oid.FLOAT4)) { + byte[] val = new byte[4]; + ByteConverter.float4(val, 0, x); + preparedParameters.setBinaryParameter(parameterIndex, val, Oid.FLOAT4); + return; + } bindLiteral(parameterIndex, Float.toString(x), Oid.FLOAT8); } @@ -1194,6 +1218,11 @@ public void setDouble(int parameterIndex, double x) throws SQLException { checkClosed(); + if (connection.binaryTransferSend(Oid.FLOAT8)) { + byte[] val = new byte[8]; + ByteConverter.float8(val, 0, x); + preparedParameters.setBinaryParameter(parameterIndex, val, Oid.FLOAT8); + } bindLiteral(parameterIndex, Double.toString(x), Oid.FLOAT8); } diff -uNr --exclude='*.jar' --exclude='*.class' --exclude=CVS pgjdbc.oldbin5/org/postgresql/util/ByteConverter.java pgjdbc/org/postgresql/util/ByteConverter.java --- pgjdbc.oldbin5/org/postgresql/util/ByteConverter.java 2006-12-15 22:28:45.000000000 +0200 +++ pgjdbc/org/postgresql/util/ByteConverter.java 2006-12-17 20:41:39.000000000 +0200 @@ -89,4 +89,69 @@ return Double.longBitsToDouble(int8(bytes, idx)); } + /** + * Encodes a long value to the byte array. + * + * @param target The byte array to encode to. + * @param idx The starting index in the byte array. + * @param value The value to encode. + */ + public static void int8(byte[] target, int idx, long value) { + target[idx+0] = (byte) (value >>> 56); + target[idx+1] = (byte) (value >>> 48); + target[idx+2] = (byte) (value >>> 40); + target[idx+3] = (byte) (value >>> 32); + target[idx+4] = (byte) (value >>> 24); + target[idx+5] = (byte) (value >>> 16); + target[idx+6] = (byte) (value >>> 8); + target[idx+7] = (byte) value; + } + + /** + * Encodes a int value to the byte array. + * + * @param target The byte array to encode to. + * @param idx The starting index in the byte array. + * @param value The value to encode. + */ + public static void int4(byte[] target, int idx, int value) { + target[idx+0] = (byte) (value >>> 24); + target[idx+1] = (byte) (value >>> 16); + target[idx+2] = (byte) (value >>> 8); + target[idx+3] = (byte) value; + } + + /** + * Encodes a int value to the byte array. + * + * @param target The byte array to encode to. + * @param idx The starting index in the byte array. + * @param value The value to encode. + */ + public static void int2(byte[] target, int idx, int value) { + target[idx+0] = (byte) (value >>> 8); + target[idx+1] = (byte) value; + } + + /** + * Encodes a int value to the byte array. + * + * @param target The byte array to encode to. + * @param idx The starting index in the byte array. + * @param value The value to encode. + */ + public static void float4(byte[] target, int idx, float value) { + int4(target, idx, Float.floatToRawIntBits(value)); + } + + /** + * Encodes a int value to the byte array. + * + * @param target The byte array to encode to. + * @param idx The starting index in the byte array. + * @param value The value to encode. + */ + public static void float8(byte[] target, int idx, double value) { + int8(target, idx, Double.doubleToRawLongBits(value)); + } } --=-kfCdGSCEc02DxrZ2njUO--