Trouble with bytea in SPI... - Mailing list pgsql-general

From Cristian Prieto
Subject Trouble with bytea in SPI...
Date
Msg-id 029d01c5af65$50b2cce0$6500a8c0@gt.ClickDiario.local
Whole thread Raw
Responses Re: Trouble with bytea in SPI...
List 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!!!

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Performance question (FOR loop)
Next
From: Michael Fuhr
Date:
Subject: Re: Trouble with bytea in SPI...