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
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers