Re: SPI_execute_with_args call

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Yuriy Rusinov <yrusinov(at)gmail(dot)com>
Cc: POSTGRES <pgsql-general(at)postgresql(dot)org>
Subject: Re: SPI_execute_with_args call
Date: 2013-05-03 14:42:53
Message-ID: 23898.1367592173@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Yuriy Rusinov <yrusinov(at)gmail(dot)com> writes:
> In C-function I do
> size_t nr_ins = strlen ("insert into rand_state (state_rand) values ($1);");
> char * r_sql = (char *) palloc (nr_ins + 1);
> strncpy (r_sql, "insert into rand_state (state_rand) values ($1);", nr_ins);

This is a hard, error-prone, and ultimately incorrect way to do
pstrdup() --- you're not ensuring that the new string is
null-terminated.

> Datum * val = PointerGetDatum (randBuf);

Didn't your compiler give you a warning about that? PointerGetDatum
produces a Datum, not a pointer to a Datum. You'd need something more
like

Datum val[1];
val[0] = PointerGetDatum (randBuf);

This is assuming that randBuf is even of the right format to be a bytea
value, which is unclear from your extract.

> const char * nulls = "NULL";

And that's just wrong. Personally I'd just pass NULL to
SPI_execute_with_args since you don't have any null values to pass, but
if you don't want to do that you'd need something more like

char nulls[1];
nulls[0] = ' ';

(hmm, it looks like the SPI documentation leaves something to be desired
here --- the SPI_execute_with_args page, at least, isn't explaining the
convention for elements of the nulls[] array)

> Could you give some work examples for SPI_execute_with_args because I
> didn't find them in documentation.

A quick grep says there's a usage in src/pl/plpgsql/src/pl_exec.c

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message pradeep singh 2013-05-03 15:30:12 Re: Curious why planner can't handle NOT IN
Previous Message Tom Lane 2013-05-03 14:29:30 Re: Curious why planner can't handle NOT IN