-
-
Save khalefa-phd/ec405946301674a4c0e072cf11a395d6 to your computer and use it in GitHub Desktop.
a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PG_FUNCTION_INFO_V1(addTen); | |
Datum addTen(PG_FUNCTION_ARGS) { | |
int32 i; | |
FuncCallContext *funcctx; | |
int call_cntr; | |
int max_calls; | |
// Code executed only on first call of the function | |
if (SRF_IS_FIRSTCALL()) { | |
MemoryContext oldcontext; | |
// Initializing the function context for cross call persistence | |
funcctx = SRF_FIRSTCALL_INIT(); | |
// Context memory for multi calls | |
oldcontext = MemoryContextSwitchTo(funcctx -> multi_call_memory_ctx); | |
// Getting argument (original integer) | |
if (PG_ARGISNULL(0)) | |
ereport(ERROR, | |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), | |
errmsg("Table cannot be NULL"))); | |
funcctx->user_fctx = (void*)palloc(sizeof(int32)); | |
*((int32*)funcctx->user_fctx) = PG_GETARG_INT32(0); | |
// Alloacting space to save the integer and make it persistent between calls | |
// funcctx->user_fctx = &i; | |
// Maximum number of calls | |
funcctx -> max_calls = 10; | |
MemoryContextSwitchTo(oldcontext); | |
} | |
// Code executed on each call (first one included) | |
// Retrieving values from function context | |
funcctx = SRF_PERCALL_SETUP(); | |
call_cntr = funcctx->call_cntr; | |
max_calls = funcctx -> max_calls; | |
if (call_cntr < max_calls) { | |
int32* temp = funcctx->user_fctx; | |
i = *temp; | |
SRF_RETURN_NEXT(funcctx, i + call_cntr); | |
} else { // Done | |
SRF_RETURN_DONE(funcctx); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment