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@gmdev ~ $ ./byteatest
Server version 90101
Encoded string = \x0807060504030201100f0e0d0c0b0a09
graham@gmdev ~ $ ./byteatest
Server version 90005
Encoded string = \\x0807060504030201100f0e0d0c0b0a09