SPI_exec doesn't return proc context (on 9.1) - Mailing list pgsql-hackers

From Pavel Stehule
Subject SPI_exec doesn't return proc context (on 9.1)
Date
Msg-id AANLkTikoGppOY9FJXKLvjRha0RiWhwPW-cZHhgpwK36c@mail.gmail.com
Whole thread Raw
Responses Re: SPI_exec doesn't return proc context (on 9.1)  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Hello

I am playing with demos for PostgreSQL's Prague Developer Day and I
found a strange behave. SPI_exec should to return to proc context. But
it isn't true. In following demo, I have to play with MemoryContext
when I would to get a correct result. Is it ok?

/** contrib/citext/p2d2.c*/
#include "postgres.h"

#include "executor/spi.h"
#include "utils/builtins.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

Datum p2d2_eval(PG_FUNCTION_ARGS);

PG_FUNCTION_INFO_V1(p2d2_eval);

Datum
p2d2_eval(PG_FUNCTION_ARGS)
{       text *result = NULL;       char *query = text_to_cstring(PG_GETARG_TEXT_P(0));       int             ret;
//      MemoryContext   fcectx = CurrentMemoryContext;
       SPI_connect();
       ret = SPI_exec(query, 0);       if (ret > 0 && SPI_tuptable != NULL)       {               TupleDesc tupdesc =
SPI_tuptable->tupdesc;              SPITupleTable *tuptable = SPI_tuptable;               HeapTuple tuple =
tuptable->vals[0];
               if (tupdesc->natts > 1)                       elog(ERROR, "Query returned more columns");
               if (SPI_processed > 1)                       elog(ERROR, "Query returned more rows");               else
if(SPI_processed == 1)               {                       elog(NOTICE, "received: \"%s\"",
 
SPI_getvalue(tuple, tupdesc, 1));

//                      MemoryContextSwitchTo(fcectx);
                       result = cstring_to_text(SPI_getvalue(tuple,
tupdesc, 1));               }       }
       SPI_finish();       if (result != NULL)               PG_RETURN_TEXT_P(result);       else
PG_RETURN_NULL();
}

Regards

Pavel


pgsql-hackers by date:

Previous
From: Thom Brown
Date:
Subject: Re: Snapshots no longer build
Next
From: Jeff Davis
Date:
Subject: Re: WIP: RangeTypes