Thread: quick SRF question

quick SRF question

From
Jeremy Drake
Date:
If I have a multi-call SRF and a user_fctx struct allocated in the
multi_call_memory_ctx, and in the if(SRF_IS_FIRSTCALL()) block while still
in the multi_call_memory_ctx I use PG_GETARG_TEXT_P(n) to get an argument
to my function, and stash the result of this in my user_fctx struct, am I
guaranteed that this pointer will remain valid throughout the remaining
calls to this SRF, or should I instead use PG_GETARG_TEXT_P_COPY(n)?

Here is an example of what I am talking about

typedef struct testfunc_ctx {text * txt;
} testfunc_ctx;

Datum testfunc(PG_FUNCTION_ARGS)
{FuncCallContext *funcctx;testfunc_ctx *userctx;MemoryContext oldcontext;
if (SRF_IS_FIRSTCALL()){ funcctx = SRF_FIRSTCALL_INIT(); oldcontext =
MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);userctx = palloc(sizeof(testfunc_ctx));
 
/* XXX does this need to be PG_GETARG_TEXT_P_COPY, or is this ok like this */ userctx->txt = PG_GETARG_TEXT_P(0);
MemoryContextSwitchTo(oldcontext);funcctx->user_fctx = userctx;}
 
funcctx = SRF_PERCALL_SETUP();userctx = funcctx->user_fctx;/* do something with userctx->txt */
if (done) SRF_RETURN_DONE(funcctx);else SRF_RETURN_NEXT(funcctx, result);
}


-- 
The New Testament offers the basis for modern computer coding theory,
in the form of an affirmation of the binary number system.
But let your communication be Yea, yea; nay, nay: forwhatsoever is more than these cometh of evil.    -- Matthew 5:37