SPI_exec doesn't return proc context (on 9.1)

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: SPI_exec doesn't return proc context (on 9.1)
Date: 2011-01-29 18:39:54
Message-ID: AANLkTikoGppOY9FJXKLvjRha0RiWhwPW-cZHhgpwK36c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeff Davis 2011-01-29 18:52:09 Re: WIP: RangeTypes
Previous Message Thom Brown 2011-01-29 18:35:52 Re: Snapshots no longer build