Initializing Datums for use with SPI_execute_plan

From: "Jack Orenstein" <jack(dot)orenstein(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Initializing Datums for use with SPI_execute_plan
Date: 2006-09-13 01:57:33
Message-ID: 7ecd811f0609121857m40db2e70hdb7f121549e286f@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2006-09-13 03:29:17 Re: INSERT does not finish except if it is carried out a few minutes after the creation of the table
Previous Message Don Parris 2006-09-13 01:15:37 Re: Question About Aggregate Functions