navige
navige

Reputation: 2517

Postgresql: definition of own type in C

I'm using a Postgresql 9.2 installation and I'm trying to get an extension (I have programmed) to run.

This code compiles, and I can load it into Postgresql; but every time I try to do an INSERT, the connection to the DB stops. I guess the reason for that is that my in-function is somehow broken.

First to the typedef:

typedef struct tphys {
   char   length[4];
   char   data[1];
} tphys;

As you see, I have a data-type that is meant to be unlimited in size. And now to the in-code:

PG_FUNCTION_INFO_V1(tphys_data_in);
Datum
tphys_data_in(PG_FUNCTION_ARGS)
{
   char *in  = PG_GETARG_CSTRING(0);
   tphys *dest = (tphys *) palloc(VARHDRSZ + VARSIZE(in));

   SET VARSIZE(dest, VARHDRSZ + VARSIZE(in));
   memcpy(VARDATA(dest), in, VARSIZE(in));

   PG_RETURN_POINTER(dest);
}

I've been trying with this code quite some time, and I really don't know where the error is. Do you maybe see it?

Upvotes: 2

Views: 118

Answers (1)

Pavel Stehule
Pavel Stehule

Reputation: 45795

You cannot use VARSIZE macro for cstring type.

char *instr = PG_GETAR_CSTRING(0);
tphys *dest = (phhys *) palloc(VARHDRSZ + strlen(instr));

...

Upvotes: 3

Related Questions