Initializing Datums for use with SPI_execute_plan - Mailing list pgsql-general

From Jack Orenstein
Subject Initializing Datums for use with SPI_execute_plan
Date
Msg-id 7ecd811f0609121857m40db2e70hdb7f121549e286f@mail.gmail.com
Whole thread Raw
Responses Re: Initializing Datums for use with SPI_execute_plan  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
I am trying to write a postgresql extension in C, which uses
SPI_prepare and SPI_execute_plan. What I want to do is to create a
prepared INSERT statement, bind some int, bigint, varchar and
bytea values, and then execute the INSERT.

I'm stuck on how to bind the values. The problem is that the values
being bound are not present as Datums. I don't get them as function
parameters, or from a table. I have data encoded, in a proprietary
format in one bytea column of a table. My function decodes the bytea
yielding the values that I need to bind to the INSERT statement.

int, bigint: From looking at postgres.h, I realize that Datum is an
unsigned long. I'm guessing that I should just be able to assign Datums
carrying ints or bigints, e.g. using Int32GetDatum to cast the int to
a Datum. Is that correct?

varchar: I have a zero-terminated string that I need to turn into a
Datum. Is CStringGetDatum the right thing to use?

bytea: I have an unsigned char* (not zero-terminated). Can I use
PointerGetDatum?

For the varchar and bytea cases, I assume that I simply pfree the
palloc'ed data as usual, after the SPI_execute_plan call.

Jack Orenstein

pgsql-general by date:

Previous
From: "J S B"
Date:
Subject: berkley sockets
Next
From: Francisco Reyes
Date:
Subject: Re: Superuser lost access to particular database