Hello, I've been working just a little with SPI in a few stored functions, this is a model of my SP:
PG_FUNCTION_INFO_V1(myspi);
Datum
myspi(PG_FUNCTION_ARGS)
{
bool isnull;
bytea *val;
Oid *karg;
void *plan;
int res;
ret = SPI_connect();
karg = (Oid *) palloc(sizeof(Oid));
ret = SPI_exec("SELECT bytevalue FROM table1", 1);
if (ret == SPI_OK_SELECT && SPI_processed > 0) {
TupleDesc tupdesc = SPI_tuptable->tupdesc;
SPITupleTable *tuptable = SPI_tuptable;
val = DatumGetByteaP(SPI_getbinval(tuptable->vals[0], tupdesc, 1, &isnull));
karg[0] = SPI_gettypeid(tupdesc, 1);
}
// Here I use and modified the val bytea value, but it stills as a bytea kind of data
// Now I need to return the bytea value to the table
plan = SPI_prepare("UPDATE table1 SET bytevalue=$1", 1, karg);
if(!plan)
elog(ERROR, "I don't know what happened!");
plan = SPI_saveplan(plan);
// This is where the SP and the connection dies!
ret = SPI_execp(plan, val, NULL, 1);
SPI_finnish();
PG_RETURN_INT32(1);
}
Well, the funcion is compiled and it loads correctly using
create or replace function myspi() returns integer as '/usr/lib/pgsql/myspi.so' language 'c';
but when I did a
select myspi();
I get the non-friendly message "The server closed the connection unexpectedly..." blah blah blah... and the !> indicator...
I checked out all the contrib dir (contrib/fulltextindex/fti.c, contrib/tsearch2/dict.c, and similar) and I guess my code is similar to them in that area, what did I do wrong???
Thanks a lot for your help!!!