Trouble with bytea in SPI...

From: "Cristian Prieto" <cristian(at)clickdiario(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Trouble with bytea in SPI...
Date: 2005-09-02 02:23:31
Message-ID: 029d01c5af65$50b2cce0$6500a8c0@gt.ClickDiario.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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!!!

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2005-09-02 03:05:35 Re: Trouble with bytea in SPI...
Previous Message Christopher Kings-Lynne 2005-09-02 01:16:59 Re: PL/pgSQL: EXCEPTION NOSAVEPOINT