Thread: SPI_exec doesn't return proc context (on 9.1)

SPI_exec doesn't return proc context (on 9.1)

From
Pavel Stehule
Date:
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


Re: SPI_exec doesn't return proc context (on 9.1)

From
Tom Lane
Date:
Pavel Stehule <pavel.stehule@gmail.com> writes:
> 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.

Yes it is.

> In following demo, I have to play with MemoryContext
> when I would to get a correct result. Is it ok?

The commented-out lines of code don't appear to have anything to do with
what you claim the problem is.  What it looks to me like you're doing is
copying the result out to the function's calling context, which is not
either of the SPI contexts established by SPI_connect (and removed by
SPI_finish).

The less crocky way to do that is to use SPI_palloc() for something that
should be allocated in the outer context.
        regards, tom lane


Re: SPI_exec doesn't return proc context (on 9.1)

From
Pavel Stehule
Date:
2011/1/29 Tom Lane <tgl@sss.pgh.pa.us>:
> Pavel Stehule <pavel.stehule@gmail.com> writes:
>> 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.
>
> Yes it is.

ah, I though a savedctx, but it restore procctx, that is child contextof SPI too.

>
>> In following demo, I have to play with MemoryContext
>> when I would to get a correct result. Is it ok?
>
> The commented-out lines of code don't appear to have anything to do with
> what you claim the problem is.  What it looks to me like you're doing is
> copying the result out to the function's calling context, which is not
> either of the SPI contexts established by SPI_connect (and removed by
> SPI_finish).

>
> The less crocky way to do that is to use SPI_palloc() for something that
> should be allocated in the outer context.

I understand. Is there some way, where I can use a cstring_to_text
function? There isn't simple way to get a saveCtx.

some like SPI_copyDatum ... ?

Pavel

>
>                        regards, tom lane
>


Re: SPI_exec doesn't return proc context (on 9.1)

From
Alvaro Herrera
Date:
Excerpts from Pavel Stehule's message of sáb ene 29 16:56:40 -0300 2011:
> 2011/1/29 Tom Lane <tgl@sss.pgh.pa.us>:

> > The less crocky way to do that is to use SPI_palloc() for something that
> > should be allocated in the outer context.
> 
> I understand. Is there some way, where I can use a cstring_to_text
> function? There isn't simple way to get a saveCtx.
> 
> some like SPI_copyDatum ... ?

I wrote such a patch some time ago but never applied it ...
http://archives.postgresql.org/message-id/20091116162531.GA3669@alvh.no-ip.org
I still wonder if it's useful enough to be applied.  Would it solve your
use case?

-- 
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


Re: SPI_exec doesn't return proc context (on 9.1)

From
Tom Lane
Date:
Alvaro Herrera <alvherre@commandprompt.com> writes:
> I wrote such a patch some time ago but never applied it ...
> http://archives.postgresql.org/message-id/20091116162531.GA3669@alvh.no-ip.org
> I still wonder if it's useful enough to be applied.  Would it solve your
> use case?

Needs to be fixed to behave sanely for the typbyval case (as
implemented, it's depending on the caller to check that).
Otherwise, seems pretty reasonable.
        regards, tom lane