pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Optimize postgres protocol for fixed size arrays
11+ messages / 6 participants
[nested] [flat]

* Optimize postgres protocol for fixed size arrays
@ 2011-11-22 21:47 Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
  2011-11-22 21:58 ` Re: Optimize postgres protocol for fixed size arrays ktm@rice.edu <ktm@rice.edu>
  2011-11-22 22:46 ` Re: Optimize postgres protocol for fixed size arrays Merlin Moncure <mmoncure@gmail.com>
  2011-11-22 23:41 ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Oliver Jowett <oliver@opencloud.com>
  0 siblings, 3 replies; 11+ messages in thread

From: Mikko Tiihonen @ 2011-11-22 21:47 UTC (permalink / raw)
  To: pgsql-jdbc; pgsql-hackers@postgresql.org

Hi,

During conversion of the jdbc driver to use binary encoding when receiving array objects from postgres it was noticed
that for example for int[] arrays the binary encoding is normally 30% to 200% larger in bytes than the text encoding.

This was of concern to some users with slower links to database. Even though the encoded size was larger the binary
encoding was still constantly faster (with 50% speed up for float[]).

Here is a patch that adds a new flag to the protocol that is set when all elements of the array are of same fixed size.
When the bit is set the 4 byte length is only sent once and not for each element. Another restriction is that the flag
can only be set when there are no NULLs in the array.

The postgres part is my first try at the problem and I really need some feedback around the detection of fixed size
elements. I just made a guess and noticed that typlen != 0 seemed to work for the basic types I user for testing.

First the postgres patch:

inline patch 1: src/backend/utils/adt/arrayfuncs.c (+12/-7 lines) download inline-1.patchdiff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index bfb6065..970272f 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -86,7 +86,7 @@ static void ReadArrayBinary(StringInfo buf, int nitems, FmgrInfo *receiveproc, Oid typioparam, int32 typmod, int typlen, bool typbyval, char typalign, Datum *values, bool *nulls, - bool *hasnulls, int32 *nbytes); + bool *hasnulls, int32 *nbytes, bool fixedlen); static void CopyArrayEls(ArrayType *array, Datum *values, bool *nulls, int nitems, int typlen, bool typbyval, char typalign, @@ -1242,7 +1242,7 @@ array_recv(PG_FUNCTION_ARGS) ndim, MAXDIM))); flags = pq_getmsgint(buf, 4); - if (flags != 0 && flags != 1) + if ((flags & ~3) != 0) ereport(ERROR, (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION), errmsg("invalid array flags"))); @@ -1327,7 +1327,7 @@ array_recv(PG_FUNCTION_ARGS) &my_extra->proc, typioparam, typmod, typlen, typbyval, typalign, dataPtr, nullsPtr, - &hasnulls, &nbytes); + &hasnulls, &nbytes, (flags & 2) != 0); if (hasnulls) { dataoffset = ARR_OVERHEAD_WITHNULLS(ndim, nitems); @@ -1390,7 +1390,8 @@ ReadArrayBinary(StringInfo buf, Datum *values, bool *nulls, bool *hasnulls, - int32 *nbytes) + int32 *nbytes, + bool fixedlen) { int i; bool hasnull; @@ -1403,7 +1404,7 @@ ReadArrayBinary(StringInfo buf, char csave; /* Get and check the item length */ - itemlen = pq_getmsgint(buf, 4); + itemlen = fixedlen ? typlen : pq_getmsgint(buf, 4); if (itemlen < -1 || itemlen > (buf->len - buf->cursor)) ereport(ERROR, (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION), @@ -1496,6 +1497,7 @@ array_send(PG_FUNCTION_ARGS) int bitmask; int nitems, i; + int flags; int ndim, *dim; StringInfoData buf; @@ -1535,6 +1537,8 @@ array_send(PG_FUNCTION_ARGS) typbyval = my_extra->typbyval; typalign = my_extra->typalign; + flags = ARR_HASNULL(v) ? 1 : (typlen > 0 ? 2 : 0); + ndim = ARR_NDIM(v); dim = ARR_DIMS(v); nitems = ArrayGetNItems(ndim, dim); @@ -1543,7 +1547,7 @@ array_send(PG_FUNCTION_ARGS) /* Send the array header information */ pq_sendint(&buf, ndim, 4); - pq_sendint(&buf, ARR_HASNULL(v) ? 1 : 0, 4); + pq_sendint(&buf, flags, 4); pq_sendint(&buf, element_type, sizeof(Oid)); for (i = 0; i < ndim; i++) { @@ -1571,7 +1575,8 @@ array_send(PG_FUNCTION_ARGS) itemvalue = fetch_att(p, typbyval, typlen); outputbytes = SendFunctionCall(&my_extra->proc, itemvalue); - pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4); + if ((flags & 2) == 0) + pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4); pq_sendbytes(&buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); pfree(outputbytes);
And here is the matching jdbc driver patch (similar changes need to be done to other drivers too):
inline patch 2: org/postgresql/jdbc2/AbstractJdbc2Array.java (+7/-3 lines) download inline-2.patchIndex: org/postgresql/jdbc2/AbstractJdbc2Array.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Array.java,v retrieving revision 1.29 diff -u -r1.29 AbstractJdbc2Array.java --- org/postgresql/jdbc2/AbstractJdbc2Array.java 30 Sep 2011 10:08:17 -0000 1.29 +++ org/postgresql/jdbc2/AbstractJdbc2Array.java 22 Nov 2011 21:06:15 -0000 @@ -175,7 +175,7 @@ private Object readBinaryArray(int index, int count) throws SQLException { int dimensions = ByteConverter.int4(fieldBytes, 0); - //int flags = ByteConverter.int4(fieldBytes, 4); // bit 0: 0=no-nulls, 1=has-nulls + int flags = ByteConverter.int4(fieldBytes, 4); // bit 0: 0=no-nulls, 1=has-nulls, bit 1: 0=variable-size objects, 2=fixed-size int elementOid = ByteConverter.int4(fieldBytes, 8); int pos = 12; int[] dims = new int[dimensions]; @@ -190,8 +190,12 @@ dims[0] = Math.min(count, dims[0]); } Object arr = java.lang.reflect.Array.newInstance(elementOidToClass(elementOid), dims); + int len = -1; + if ((flags & 2) != 0) { + len = fixedBinaryLengthOfOid(elementOid); + } try { - storeValues((Object[]) arr, elementOid, dims, pos, 0, index); + storeValues((Object[]) arr, elementOid, dims, len, pos, 0, index); } catch (IOException ioe) { @@ -200,18 +204,27 @@ 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 len, 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; - if (len != -1) { - pos += len; + if (len > 0) { + pos += len * (index - 1); + } else { + for (int i = 1; i < index; ++i) { + int l = ByteConverter.int4(fieldBytes, pos); pos += 4; + if (l != -1) { + pos += l; + } } } for (int i = 0; i < dims[thisDimension]; ++i) { - int len = ByteConverter.int4(fieldBytes, pos); pos += 4; - if (len == -1) { - continue; + int l; + if (len > 0) { + l = len; + } else { + l = ByteConverter.int4(fieldBytes, pos); pos += 4; + if (l == -1) { + continue; + } } switch (elementOid) { case Oid.INT2: @@ -232,14 +245,14 @@ case Oid.TEXT: case Oid.VARCHAR: Encoding encoding = connection.getEncoding(); - arr[i] = encoding.decode(fieldBytes, pos, len); + arr[i] = encoding.decode(fieldBytes, pos, l); break; } - pos += len; + pos += l; } } else { for (int i = 0; i < dims[thisDimension]; ++i) { - pos = storeValues((Object[]) arr[i], elementOid, dims, pos, thisDimension + 1, 0); + pos = storeValues((Object[]) arr[i], elementOid, dims, len, pos, thisDimension + 1, 0); } } return pos; @@ -358,6 +371,27 @@ } } + private int fixedBinaryLengthOfOid(int oid) throws SQLException { + switch (oid) { + case Oid.INT2: + return 2; + case Oid.INT4: + return 4; + case Oid.INT8: + return 8; + case Oid.FLOAT4: + return 4; + case Oid.FLOAT8: + return 8; + case Oid.TEXT: + case Oid.VARCHAR: + return -1; + default: + throw org.postgresql.Driver.notImplemented(this.getClass(), + "readBinaryArray(data,oid)"); + } + } + /** * Build {@link ArrayList} from field's string input. As a result * of this method {@link #arrayList} is build. Method can be called Patches: download all (2 files, .tar.gz) ^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: Optimize postgres protocol for fixed size arrays
  2011-11-22 21:47 Optimize postgres protocol for fixed size arrays Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
@ 2011-11-22 21:58 ` ktm@rice.edu <ktm@rice.edu>
  2 siblings, 0 replies; 11+ messages in thread

From: ktm@rice.edu @ 2011-11-22 21:58 UTC (permalink / raw)
  To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; +Cc: pgsql-jdbc; pgsql-hackers@postgresql.org

On Tue, Nov 22, 2011 at 11:47:22PM +0200, Mikko Tiihonen wrote:
> Hi,
> 
> During conversion of the jdbc driver to use binary encoding when receiving array objects from postgres it was noticed
> that for example for int[] arrays the binary encoding is normally 30% to 200% larger in bytes than the text encoding.
> 
> This was of concern to some users with slower links to database. Even though the encoded size was larger the binary
> encoding was still constantly faster (with 50% speed up for float[]).
> 
> Here is a patch that adds a new flag to the protocol that is set when all elements of the array are of same fixed size.
> When the bit is set the 4 byte length is only sent once and not for each element. Another restriction is that the flag
> can only be set when there are no NULLs in the array.
> 

Cool. This would be very useful with the DSPAM binary array driver. Although
the binary is shorter because the values are 8 byte integers, they would be
much shorter without the redundant sizing information. Barring issues:

+1

Regards,
Ken



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

* Re: Optimize postgres protocol for fixed size arrays
  2011-11-22 21:47 Optimize postgres protocol for fixed size arrays Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
@ 2011-11-22 22:46 ` Merlin Moncure <mmoncure@gmail.com>
  2 siblings, 0 replies; 11+ messages in thread

From: Merlin Moncure @ 2011-11-22 22:46 UTC (permalink / raw)
  To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; +Cc: pgsql-jdbc; pgsql-hackers@postgresql.org

On Tue, Nov 22, 2011 at 3:47 PM, Mikko Tiihonen
<mikko.tiihonen@nitorcreations.com> wrote:
> Hi,
>
> During conversion of the jdbc driver to use binary encoding when receiving
> array objects from postgres it was noticed
> that for example for int[] arrays the binary encoding is normally 30% to
> 200% larger in bytes than the text encoding.
>
> This was of concern to some users with slower links to database. Even though
> the encoded size was larger the binary
> encoding was still constantly faster (with 50% speed up for float[]).
>
> Here is a patch that adds a new flag to the protocol that is set when all
> elements of the array are of same fixed size.
> When the bit is set the 4 byte length is only sent once and not for each
> element. Another restriction is that the flag
> can only be set when there are no NULLs in the array.
>
> The postgres part is my first try at the problem and I really need some
> feedback around the detection of fixed size
> elements. I just made a guess and noticed that typlen != 0 seemed to work
> for the basic types I user for testing.
>
> First the postgres patch:
>
> diff --git a/src/backend/utils/adt/arrayfuncs.c
> b/src/backend/utils/adt/arrayfuncs.c
> index bfb6065..970272f 100644
> --- a/src/backend/utils/adt/arrayfuncs.c
> +++ b/src/backend/utils/adt/arrayfuncs.c
> @@ -86,7 +86,7 @@ static void ReadArrayBinary(StringInfo buf, int nitems,
>                                FmgrInfo *receiveproc, Oid typioparam, int32
> typmod,
>                                int typlen, bool typbyval, char typalign,
>                                Datum *values, bool *nulls,
> -                               bool *hasnulls, int32 *nbytes);
> +                               bool *hasnulls, int32 *nbytes, bool
> fixedlen);
>  static void CopyArrayEls(ArrayType *array,
>                         Datum *values, bool *nulls, int nitems,
>                         int typlen, bool typbyval, char typalign,
> @@ -1242,7 +1242,7 @@ array_recv(PG_FUNCTION_ARGS)
>                                                ndim, MAXDIM)));
>
>        flags = pq_getmsgint(buf, 4);
> -       if (flags != 0 && flags != 1)
> +       if ((flags & ~3) != 0)
>                ereport(ERROR,
>
>  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
>                                 errmsg("invalid array flags")));
> @@ -1327,7 +1327,7 @@ array_recv(PG_FUNCTION_ARGS)
>                                        &my_extra->proc, typioparam, typmod,
>                                        typlen, typbyval, typalign,
>                                        dataPtr, nullsPtr,
> -                                       &hasnulls, &nbytes);
> +                                       &hasnulls, &nbytes, (flags & 2) !=
> 0);
>        if (hasnulls)
>        {
>                dataoffset = ARR_OVERHEAD_WITHNULLS(ndim, nitems);
> @@ -1390,7 +1390,8 @@ ReadArrayBinary(StringInfo buf,
>                                Datum *values,
>                                bool *nulls,
>                                bool *hasnulls,
> -                               int32 *nbytes)
> +                               int32 *nbytes,
> +                               bool fixedlen)
>  {
>        int                     i;
>        bool            hasnull;
> @@ -1403,7 +1404,7 @@ ReadArrayBinary(StringInfo buf,
>                char            csave;
>
>                /* Get and check the item length */
> -               itemlen = pq_getmsgint(buf, 4);
> +               itemlen = fixedlen ? typlen : pq_getmsgint(buf, 4);
>                if (itemlen < -1 || itemlen > (buf->len - buf->cursor))
>                        ereport(ERROR,
>
>  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
> @@ -1496,6 +1497,7 @@ array_send(PG_FUNCTION_ARGS)
>        int                     bitmask;
>        int                     nitems,
>                                i;
> +       int                     flags;
>        int                     ndim,
>                           *dim;
>        StringInfoData buf;
> @@ -1535,6 +1537,8 @@ array_send(PG_FUNCTION_ARGS)
>        typbyval = my_extra->typbyval;
>        typalign = my_extra->typalign;
>
> +       flags = ARR_HASNULL(v) ? 1 : (typlen > 0 ? 2 : 0);
> +
>        ndim = ARR_NDIM(v);
>        dim = ARR_DIMS(v);
>        nitems = ArrayGetNItems(ndim, dim);
> @@ -1543,7 +1547,7 @@ array_send(PG_FUNCTION_ARGS)
>
>        /* Send the array header information */
>        pq_sendint(&buf, ndim, 4);
> -       pq_sendint(&buf, ARR_HASNULL(v) ? 1 : 0, 4);
> +       pq_sendint(&buf, flags, 4);
>        pq_sendint(&buf, element_type, sizeof(Oid));
>        for (i = 0; i < ndim; i++)
>        {
> @@ -1571,7 +1575,8 @@ array_send(PG_FUNCTION_ARGS)
>
>                        itemvalue = fetch_att(p, typbyval, typlen);
>                        outputbytes = SendFunctionCall(&my_extra->proc,
> itemvalue);
> -                       pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ,
> 4);
> +                       if ((flags & 2) == 0)
> +                               pq_sendint(&buf, VARSIZE(outputbytes) -
> VARHDRSZ, 4);
>                        pq_sendbytes(&buf, VARDATA(outputbytes),
>                                                 VARSIZE(outputbytes) -
> VARHDRSZ);
>                        pfree(outputbytes);
>
>
>
>
> And here is the matching jdbc driver patch (similar changes need to be done
> to other drivers too):
>
> Index: org/postgresql/jdbc2/AbstractJdbc2Array.java
> ===================================================================
> RCS file:
> /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Array.java,v
> retrieving revision 1.29
> diff -u -r1.29 AbstractJdbc2Array.java
> --- org/postgresql/jdbc2/AbstractJdbc2Array.java        30 Sep 2011 10:08:17
> -0000      1.29
> +++ org/postgresql/jdbc2/AbstractJdbc2Array.java        22 Nov 2011 21:06:15
> -0000
> @@ -175,7 +175,7 @@
>
>     private Object readBinaryArray(int index, int count) throws SQLException
> {
>         int dimensions = ByteConverter.int4(fieldBytes, 0);
> -        //int flags = ByteConverter.int4(fieldBytes, 4); // bit 0:
> 0=no-nulls, 1=has-nulls
> +        int flags = ByteConverter.int4(fieldBytes, 4); // bit 0:
> 0=no-nulls, 1=has-nulls, bit 1: 0=variable-size objects, 2=fixed-size
>         int elementOid = ByteConverter.int4(fieldBytes, 8);
>         int pos = 12;
>         int[] dims = new int[dimensions];
> @@ -190,8 +190,12 @@
>             dims[0] = Math.min(count, dims[0]);
>         }
>         Object arr =
> java.lang.reflect.Array.newInstance(elementOidToClass(elementOid), dims);
> +        int len = -1;
> +        if ((flags & 2) != 0) {
> +            len = fixedBinaryLengthOfOid(elementOid);
> +        }
>         try {
> -            storeValues((Object[]) arr, elementOid, dims, pos, 0, index);
> +            storeValues((Object[]) arr, elementOid, dims, len, pos, 0,
> index);
>         }
>         catch (IOException ioe)
>         {
> @@ -200,18 +204,27 @@
>         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 len, 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;
> -                if (len != -1) {
> -                    pos += len;
> +            if (len > 0) {
> +                pos += len * (index - 1);
> +            } else {
> +                for (int i = 1; i < index; ++i) {
> +                    int l = ByteConverter.int4(fieldBytes, pos); pos += 4;
> +                    if (l != -1) {
> +                        pos += l;
> +                    }
>                 }
>             }
>             for (int i = 0; i < dims[thisDimension]; ++i) {
> -                int len = ByteConverter.int4(fieldBytes, pos); pos += 4;
> -                if (len == -1) {
> -                    continue;
> +                int l;
> +                if (len > 0) {
> +                    l = len;
> +                } else {
> +                    l = ByteConverter.int4(fieldBytes, pos); pos += 4;
> +                    if (l == -1) {
> +                        continue;
> +                    }
>                 }
>                 switch (elementOid) {
>                 case Oid.INT2:
> @@ -232,14 +245,14 @@
>                 case Oid.TEXT:
>                 case Oid.VARCHAR:
>                     Encoding encoding = connection.getEncoding();
> -                    arr[i] = encoding.decode(fieldBytes, pos, len);
> +                    arr[i] = encoding.decode(fieldBytes, pos, l);
>                     break;
>                 }
> -                pos += len;
> +                pos += l;
>             }
>         } else {
>             for (int i = 0; i < dims[thisDimension]; ++i) {
> -                pos = storeValues((Object[]) arr[i], elementOid, dims, pos,
> thisDimension + 1, 0);
> +                pos = storeValues((Object[]) arr[i], elementOid, dims, len,
> pos, thisDimension + 1, 0);
>             }
>         }
>         return pos;
> @@ -358,6 +371,27 @@
>         }
>     }
>
> +    private int fixedBinaryLengthOfOid(int oid) throws SQLException {
> +        switch (oid) {
> +        case Oid.INT2:
> +            return 2;
> +        case Oid.INT4:
> +            return 4;
> +        case Oid.INT8:
> +            return 8;
> +        case Oid.FLOAT4:
> +            return 4;
> +        case Oid.FLOAT8:
> +            return 8;
> +        case Oid.TEXT:
> +        case Oid.VARCHAR:
> +            return -1;
> +        default:
> +            throw org.postgresql.Driver.notImplemented(this.getClass(),
> +                    "readBinaryArray(data,oid)");
> +        }
> +    }
> +
>     /**
>      * Build {@link ArrayList} from field's string input. As a result
>      * of this method {@link #arrayList} is build. Method can be called

+1.   just be advised that this is a significant change to the binary
wire format and will cause headaches to those that are using it (you
are supposed to expect those headaches though -- using the wire format
is analogous to attending a rock concert).  maybe this would be an
appropriate place to discuss a well advertised place in the
documentation that describes changes to the wire format?

libpqtypes users of course will not have to worry about this because
the format change will be handled in the library.

merlin



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

* Re: [JDBC] Optimize postgres protocol for fixed size arrays
  2011-11-22 21:47 Optimize postgres protocol for fixed size arrays Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
@ 2011-11-22 23:41 ` Oliver Jowett <oliver@opencloud.com>
  2011-11-22 23:52   ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Tom Lane <tgl@sss.pgh.pa.us>
  2 siblings, 1 reply; 11+ messages in thread

From: Oliver Jowett @ 2011-11-22 23:41 UTC (permalink / raw)
  To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; +Cc: pgsql-jdbc; pgsql-hackers@postgresql.org

On 23 November 2011 10:47, Mikko Tiihonen
<mikko.tiihonen@nitorcreations.com> wrote:

> Here is a patch that adds a new flag to the protocol that is set when all
> elements of the array are of same fixed size.
> When the bit is set the 4 byte length is only sent once and not for each
> element. Another restriction is that the flag
> can only be set when there are no NULLs in the array.

How does a client detect that this feature is supported?

At a glance the JDBC patch doesn't use it on the send path, but
presumably clients could use this when sending binary-format arrays to
the server - but only if the server understood the format.

(Ideally a pair of settings would be useful here - one to say "the
server understands the new format" and another the client sets to say
"please use the new format" that defaults to off - then you could
avoid confusing old clients, too)

Oliver



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

* Re: [JDBC] Optimize postgres protocol for fixed size arrays
  2011-11-22 21:47 Optimize postgres protocol for fixed size arrays Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
  2011-11-22 23:41 ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Oliver Jowett <oliver@opencloud.com>
@ 2011-11-22 23:52   ` Tom Lane <tgl@sss.pgh.pa.us>
  2011-11-23 16:07     ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Merlin Moncure <mmoncure@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Tom Lane @ 2011-11-22 23:52 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; pgsql-jdbc; pgsql-hackers@postgresql.org

Oliver Jowett <oliver@opencloud.com> writes:
> On 23 November 2011 10:47, Mikko Tiihonen
> <mikko.tiihonen@nitorcreations.com> wrote:
>> Here is a patch that adds a new flag to the protocol that is set when all
>> elements of the array are of same fixed size.

> How does a client detect that this feature is supported?

The only way that anything like this will go in is as part of a protocol
version bump, so discoverability would reduce to knowing which protocol
you're using.  We should file this away as one of the things we might
want to do whenever there's sufficient critical mass for a new wire
protocol version.

Note that COPY BINARY files would be affected too, and so we'd want to
make sure that this sort of change is recognizable from a binary file's
header.

			regards, tom lane



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

* Re: [JDBC] Optimize postgres protocol for fixed size arrays
  2011-11-22 21:47 Optimize postgres protocol for fixed size arrays Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
  2011-11-22 23:41 ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Oliver Jowett <oliver@opencloud.com>
  2011-11-22 23:52   ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Tom Lane <tgl@sss.pgh.pa.us>
@ 2011-11-23 16:07     ` Merlin Moncure <mmoncure@gmail.com>
  2011-11-23 16:36       ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 11+ messages in thread

From: Merlin Moncure @ 2011-11-23 16:07 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Oliver Jowett <oliver@opencloud.com>; Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; pgsql-jdbc; pgsql-hackers@postgresql.org

On Tue, Nov 22, 2011 at 6:52 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Oliver Jowett <oliver@opencloud.com> writes:
>> On 23 November 2011 10:47, Mikko Tiihonen
>> <mikko.tiihonen@nitorcreations.com> wrote:
>>> Here is a patch that adds a new flag to the protocol that is set when all
>>> elements of the array are of same fixed size.
>
>> How does a client detect that this feature is supported?
>
> The only way that anything like this will go in is as part of a protocol
> version bump, so discoverability would reduce to knowing which protocol
> you're using.  We should file this away as one of the things we might
> want to do whenever there's sufficient critical mass for a new wire
> protocol version.
>
> Note that COPY BINARY files would be affected too, and so we'd want to
> make sure that this sort of change is recognizable from a binary file's
> header.

Wire format changes can only be made with a protocol version bump?  Is
this a new policy? In the past they were just made...for example the
money type was bumped to 64 bits.  In the past it was always buyer
beware for binary format consumers.

merlin



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

* Re: [JDBC] Optimize postgres protocol for fixed size arrays
  2011-11-22 21:47 Optimize postgres protocol for fixed size arrays Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
  2011-11-22 23:41 ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Oliver Jowett <oliver@opencloud.com>
  2011-11-22 23:52   ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Tom Lane <tgl@sss.pgh.pa.us>
  2011-11-23 16:07     ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Merlin Moncure <mmoncure@gmail.com>
@ 2011-11-23 16:36       ` Tom Lane <tgl@sss.pgh.pa.us>
  2011-11-24 00:26         ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Tom Lane @ 2011-11-23 16:36 UTC (permalink / raw)
  To: Merlin Moncure <mmoncure@gmail.com>; +Cc: Oliver Jowett <oliver@opencloud.com>; Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; pgsql-jdbc; pgsql-hackers@postgresql.org

Merlin Moncure <mmoncure@gmail.com> writes:
> On Tue, Nov 22, 2011 at 6:52 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> The only way that anything like this will go in is as part of a protocol
>> version bump,

> Wire format changes can only be made with a protocol version bump?  Is
> this a new policy? In the past they were just made...for example the
> money type was bumped to 64 bits.  In the past it was always buyer
> beware for binary format consumers.

Well, (a) our standards have gone up over time, (b) binary protocol is
getting more widely used (in part due to your own efforts), and (c)
money is a third-class stepchild anyway.  I don't think we can get away
with changing the binary representation of such widely used types as
int and float arrays, unless we have some pretty carefully thought
through notion of how the client and server will negotiate what to do.

Now it's possible we could do that without formally calling it a
protocol version change, but I don't care at all for the idea of coming
up with one-off hacks every time somebody decides that some feature is
important enough that they have to have it Right Now instead of waiting
for a sufficient accumulation of reasons to have a protocol flag day.
I think "but we made arrays a bit smaller!" is a pretty lame response
to have to give when somebody complains that Postgres 9.2 broke their
client software.  When we do it, I want to have a *long* list of good
reasons.

BTW, so far as the actual array format is concerned, I don't think
the proposal is acceptable as-is: it renders the received array entirely
unreadable unless the reader knows a-priori what the sender thought the
typlen was.  It would be a lot better if the fixed-length flag meant
that the typlen is given once in the array header rather than once per
element.  I'm not thrilled by the "no nulls" restriction, either,
though I admit I don't have a good idea about avoiding that offhand.

			regards, tom lane



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

* Re: [JDBC] Optimize postgres protocol for fixed size arrays
  2011-11-22 21:47 Optimize postgres protocol for fixed size arrays Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
  2011-11-22 23:41 ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Oliver Jowett <oliver@opencloud.com>
  2011-11-22 23:52   ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Tom Lane <tgl@sss.pgh.pa.us>
  2011-11-23 16:07     ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Merlin Moncure <mmoncure@gmail.com>
  2011-11-23 16:36       ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Tom Lane <tgl@sss.pgh.pa.us>
@ 2011-11-24 00:26         ` Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 11+ messages in thread

From: Oliver Jowett @ 2011-11-24 00:26 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Merlin Moncure <mmoncure@gmail.com>; Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; pgsql-jdbc; pgsql-hackers@postgresql.org

On 24 November 2011 05:36, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Now it's possible we could do that without formally calling it a
> protocol version change, but I don't care at all for the idea of coming
> up with one-off hacks every time somebody decides that some feature is
> important enough that they have to have it Right Now instead of waiting
> for a sufficient accumulation of reasons to have a protocol flag day.
> I think "but we made arrays a bit smaller!" is a pretty lame response
> to have to give when somebody complains that Postgres 9.2 broke their
> client software.  When we do it, I want to have a *long* list of good
> reasons.

Can we get a mechanism for minor protocol changes in this future
version? Something as simple as exchanging a list of protocol features
during the initial handshake (then use only features that are present
on both sides) would be enough. The difficulty of making any protocol
changes at the moment is a big stumbling block.

(You could probably retrofit that to the current protocol version)

Oliver



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

* Re: [JDBC] Optimize postgres protocol for fixed size arrays
@ 2011-11-24 00:36 Kevin Grittner <Kevin.Grittner@wicourts.gov>
  2011-11-24 18:54 ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Kevin Grittner @ 2011-11-24 00:36 UTC (permalink / raw)
  To: oliver@opencloud.com; tgl@sss.pgh.pa.us; +Cc: mmoncure@gmail.com; mikko.tiihonen@nitorcreations.com; pgsql-hackers@postgresql.org; pgsql-jdbc

Oliver Jowett  wrote:
 
> Can we get a mechanism for minor protocol changes in this future
> version? Something as simple as exchanging a list of protocol
> features during the initial handshake (then use only features that
> are present on both sides) would be enough. The difficulty of
> making any protocol changes at the moment is a big stumbling block.
 
I've been thinking the same thing.  Any new protocol should include a
way for each side to publish a list of what it can accept from the
other during initial handshaking.
 
> (You could probably retrofit that to the current protocol version)
 
Perhaps.  It would be great if both sides could recognize the case
where the "feature negotiation" was absent and use a default feature
list for the protocol available on the other end.
 
-Kevin



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

* Re: [JDBC] Optimize postgres protocol for fixed size arrays
  2011-11-24 00:36 Re: [JDBC] Optimize postgres protocol for fixed size arrays Kevin Grittner <Kevin.Grittner@wicourts.gov>
@ 2011-11-24 18:54 ` Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
  2011-11-25 00:20   ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Mikko Tiihonen @ 2011-11-24 18:54 UTC (permalink / raw)
  To: Kevin Grittner <Kevin.Grittner@wicourts.gov>; +Cc: oliver@opencloud.com; tgl@sss.pgh.pa.us; mmoncure@gmail.com; pgsql-hackers@postgresql.org; pgsql-jdbc

On 11/24/2011 02:36 AM, Kevin Grittner wrote:
> Oliver Jowett  wrote:
>
>> Can we get a mechanism for minor protocol changes in this future
>> version? Something as simple as exchanging a list of protocol
>> features during the initial handshake (then use only features that
>> are present on both sides) would be enough. The difficulty of
>> making any protocol changes at the moment is a big stumbling block.
>
> I've been thinking the same thing.  Any new protocol should include a
> way for each side to publish a list of what it can accept from the
> other during initial handshaking.
>
>> (You could probably retrofit that to the current protocol version)
>
> Perhaps.  It would be great if both sides could recognize the case
> where the "feature negotiation" was absent and use a default feature
> list for the protocol available on the other end.

What about a hand-shake protocol based on simple binary-protocol minor
version instead of features. We keep the v3 protocol as is but can
add cumulative conditionally enabled features when we bump the minor
version.

The hand shake requires that the server sends a parameter back with
it's highest supported minor version:
FE=> StartupPacket
<=BE ParameterStatus(binary_minor = 23)

And the client can send any number between 1<=binary_minor back to
enable newer protocol versions and/or limit what the server sends
FE=> Execute(SET binary_minor = 20)

To keep full backwards compatibility:
1) if backend does not send a binary_minor parameter on connection the
    highest supported minor version is assumed to be 0 (current format)
2) the backend assumes the active minor version is 0 unless the
    SET binary_minor is received

I think bumping a minor version is better than feature flags because:
1) the hand shake is simpler and faster
2) coding is easier as all previous features are known to be supported
    and active when implementing feature+1

I'm not exactly sure about the COPY BINARY feature Tom mentioned. But
probably we could prefix the data with the int4 containing the
minor version?

-Mikko



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

* Re: [JDBC] Optimize postgres protocol for fixed size arrays
  2011-11-24 00:36 Re: [JDBC] Optimize postgres protocol for fixed size arrays Kevin Grittner <Kevin.Grittner@wicourts.gov>
  2011-11-24 18:54 ` Re: [JDBC] Optimize postgres protocol for fixed size arrays Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
@ 2011-11-25 00:20   ` Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 11+ messages in thread

From: Oliver Jowett @ 2011-11-25 00:20 UTC (permalink / raw)
  To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; +Cc: Kevin Grittner <Kevin.Grittner@wicourts.gov>; tgl@sss.pgh.pa.us; mmoncure@gmail.com; pgsql-hackers@postgresql.org; pgsql-jdbc

On 25 November 2011 07:54, Mikko Tiihonen
<mikko.tiihonen@nitorcreations.com> wrote:

> <=BE ParameterStatus(binary_minor = 23)
> FE=> Execute(SET binary_minor = 20)

Yeah this was almost exactly what I was thinking about how to retrofit
it, except it might be clearer to have, say, "supported_binary_minor"
(read-only, advertised by the server on startup) vs. "binary_minor"
(read-write, defaults to 0) as otherwise you have special behavior for
just one parameter where the advertised version doesn't actually match
the currently-set version.

Re list vs. always-incrementing minor version, you could just use an
integer and set bits to represent features, which would keep it simple
but also let clients be more selective about which features they
implement (you could support feature 21 and 23 without supporting 22)

Oliver



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


end of thread, other threads:[~2011-11-25 00:20 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-11-22 21:47 Optimize postgres protocol for fixed size arrays Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
2011-11-22 21:58 ` ktm@rice.edu <ktm@rice.edu>
2011-11-22 22:46 ` Merlin Moncure <mmoncure@gmail.com>
2011-11-22 23:41 ` Oliver Jowett <oliver@opencloud.com>
2011-11-22 23:52   ` Tom Lane <tgl@sss.pgh.pa.us>
2011-11-23 16:07     ` Merlin Moncure <mmoncure@gmail.com>
2011-11-23 16:36       ` Tom Lane <tgl@sss.pgh.pa.us>
2011-11-24 00:26         ` Oliver Jowett <oliver@opencloud.com>
2011-11-24 00:36 Re: [JDBC] Optimize postgres protocol for fixed size arrays Kevin Grittner <Kevin.Grittner@wicourts.gov>
2011-11-24 18:54 ` Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
2011-11-25 00:20   ` Oliver Jowett <oliver@opencloud.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