"unsupported format code: 36" in prepared statement with libpq-fe 9.2.5

From: "Raymond C(dot) Rodgers" <sinful622(at)gmail(dot)com>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: "unsupported format code: 36" in prepared statement with libpq-fe 9.2.5
Date: 2014-01-09 15:25:43
Message-ID: 52CEBF77.5030503@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

Hello,

I haven't been working with libpq-fe for long (only the last month or
so) but I've had the good fortune of being able to figure out and solve
most problems I've encountered within a few minutes to an hour, but this
one is different. I have a prepared statement that is inserting data
into 12 of the 13 columns my table has (1 UUID, 4 numerics, 1 bigint, 2
ints, 1 boolean, 2 varchars, and 1 of 2 timestamps are being inserted).
This is the first time I've attempted to insert more than 8 columns at
once, but I'm pretty sure the number of columns isn't the problem I'm
hitting. At least, I don't think it is. Some of the vital details:
Fedora 18 (64-bit), PostgreSQL and libpq-fe version 9.2.5, code written
in C++ compiled with gcc version 4.7.2 20121109. The errors are being
pulled from the pg_logs directory for the appropriate day. (I'm using
"tail -f" to watch as they occur.)

I've prepared the statement with code similar to the following (lines
broken up for slight readability improvement); DB is an PGconn pointer
that has already been connected to the database by this point:

Oid dozenFieldTypes[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
PQprepare(DB,"insertData","insert into mytable (data1, data2, data3,
data4, data5,
data6, data7, data8 ,data9, data10, data11, data12)
values ($1, $2::double precision,
$3::double precision, $4::double precision, $5::double
precision,$6,$7,$8,
$9::int,$10::int, $11::boolean,$12::timestamp with time
zone)",12,dozenFieldType);

The above is how the query currently exists after hours of banging my
head on this "unsupported format code: 36" error; I've tried casting
each for the inputs as the appropriate type in addition to trying to let
PostgreSQL/libpq-fe figure it out completely. As you can see, I'm
somewhere in the middle of the two at the moment. Nonetheless, I don't
get the error on the creation of the prepared statement, though I am
curious if the numbering above 9 for the parameters should be in decimal
or hexadecimal. I've only tried decimal at this point because it seems
logical that it would continue in decimal and I've found no indications
otherwise in the documentation. Of course, I also haven't found anything
in the documentation that states that a dozen parameters can be used.

I'm hitting the error when I try to execute the statement using code
similar to the following; only my variable and custom function names
have been changed:

const char *value[12];
char dt12[30];
memset(dt12,0,30);
// convert timestamp from time_t to string
struct tm time_s;
memset(&time_s,0,sizeof(time_s));
gmtime_r(&data12,&time_s);
strftime(dt12,29,"%Y-%m-%d %H:%M:%S GMT",&time_s);

int32_t dt9 = bswap_32(data9),dt10 = bswap_32(data10);
int64_t dt6 = bswap_64(atol(data6));
// prepDouble is a custom function to swap bytes appropriately
// for doubles; proven to work properly elsewhere in code
double dt2 = prepDouble(data2),dt3 = prepDouble(data3),
dt4 = prepDouble(data4), dt5 = prepDouble(data5);
value[0] = data1; // UUID stored as string (char *)
value[1] = (char*)&dt2;
value[2] = (char*)&dt3;
value[3] = (char*)&dt4;
value[4] = (char*)&dt5;
value[5] = (char*)&dt6;
value[6] = data7; //string (char*) taken as is
value[7] = data8; //string (char*) taken as is
value[8] = (char*)&dt9;
value[9] = (char*)&dt10;
const char *dt11 = (char*)&data11; // boolean tried as int,
string, and in original bool format, made no difference in error
value[10] = dt11;
value[11] = dt12;
int32_t format[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
int32_t length[12] = {(int32_t) strlen(data1), sizeof(dt2),
sizeof(dt3), sizeof(dt4), sizeof(dt5),
sizeof(dt6), (int32_t) strlen(data7), (int32_t)
strlen(data8), sizeof(dt9), sizeof(dt10),
sizeof(data11), (int32_t) strlen(dt12)};
PQexecPrepared(DB,"insertData",12,value,format,length,1);

Once the PQexecPrepared() has executed, I'm getting the following in the
PostgreSQL log:

ERROR: unsupported format code: 36
STATEMENT: insert into mytable
(data1,data2,data3,data4,data5,data6,data7,data8,data9,data10,data11,data12)
values ($1,$2::double precision,$3::double precision,$4::double
precision,$5::double
precision,$6,$7,$8,$9::int,$10::int,$11::boolean,$12::timestamp with
time zone)

So far, I haven't been able to determine precisely what "unsupported
format code: 36" refers to. I've tried playing with the resultFormat
arameter in the PQexecPrepared() call, changing the casting types of
just about every data item, and in the case of the boolean, using an
intermediate variable of a different type and making the conversion, to
rule it out. There *are* two data types that I haven't tried using in
libpq-fe before now, though I'm sure one is fine: UUID (data1) and
int64_t/bigint (data6). I'm pretty sure that the UUID is fine since it's
being stored in the program as a simple string (char *) and I got an
error when I adjusted the prepared statement to cast the value as
varchar(36). (The error basically said that it was a UUID value and that
I need to change the casting.) Data6, on the other hand, is an unknown
factor. While I've been inserting 64-bit data types (doubles) into the
database easily enough, this is the first time I've tried working with a
long/int64_t/bigint and the database. I presume that since the bswap_32
from byteswap.h works properly, that it's sibling bswap_64 is also
working properly. As you might notice, data6 is being converted from a
string to int64_t via atol(), and I've checked to make sure that's
working properly.

So, that pretty much explains where I am getting stuck. I haven't been
able to find out what "unsupported format code: 36" is referring to,
there doesn't seem to be any documentation covering that error, and I
think I've done all I can to try to resolve it without having good
information about what is causing the error.

I'd greatly appreciate any help I can get to resolve this error, and
document it for posterity. :-)

Best regards and thank you,
Raymond

Responses

Browse pgsql-interfaces by date

  From Date Subject
Next Message Tom Lane 2014-01-09 15:56:18 Re: "unsupported format code: 36" in prepared statement with libpq-fe 9.2.5
Previous Message Devrim GÜNDÜZ 2013-12-23 15:02:11 Re: pgxs installation issue