postgresql-9.1.1 PQescapeByteaConn() generating incorrectly quoted output.

From: Graham Murray <gmurray(at)webwayone(dot)co(dot)uk>
To: Postgres general mailing list <pgsql-general(at)postgresql(dot)org>
Subject: postgresql-9.1.1 PQescapeByteaConn() generating incorrectly quoted output.
Date: 2011-11-02 08:26:20
Message-ID: 1320222380.17705.44.camel@gmdev.webwayone.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Since upgrading test systems to postgresql 9.1, I am seeing some inserts
to bytea fields giving errors such as "ERROR: invalid byte sequence for
encoding "UTF8": 0xf9" Where the insert is from a C program using libpq
and is of the form "insert into xxx values(E'%s')" where the value is
the return of PQescapeByteaConn();

I noticed that with postgresql 9.0, the return string was of the form
"\\x...." but with postgresql 9.1 it is "\x..."

I can work around this by specifying "E'\\%s" in the format string to
generate the query, but this will only work with postgrseql 9.1.

The following program illustrates the issue.

#include <stdio.h>
#include <libpq-fe.h>
#include <sys/types.h>

int main(int argc, char *argv[])
{
PGconn *conn;
struct
{
u_int64_t byte1;
u_int64_t byte2;
} bindata;

char *enc;
size_t elen;

conn = PQconnectdb("");

bindata.byte1=0x0102030405060708;
bindata.byte2=0x090a0b0c0d0e0f10;

enc = PQescapeByteaConn(conn, (unsigned char *)&bindata, sizeof(bindata), &elen);

printf("Server version %d\nEncoded string = %s\n", PQserverVersion(conn), enc);
PQfreemem(enc);
exit(0);
}

Running the above program with postgres 9.0 & 9.1 generates the
following output.

graham(at)gmdev ~ $ ./byteatest
Server version 90101
Encoded string = \x0807060504030201100f0e0d0c0b0a09
graham(at)gmdev ~ $ ./byteatest
Server version 90005
Encoded string = \\x0807060504030201100f0e0d0c0b0a09

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Chris Dumoulin 2011-11-02 11:22:09 select where not exists returning multiple rows?
Previous Message adamaltman 2011-11-02 07:28:56 Re: installation problems on OSX Lion