summaryrefslogtreecommitdiff
path: root/src/include/funcapi.h
diff options
context:
space:
mode:
authorTom Lane2002-08-29 17:14:33 +0000
committerTom Lane2002-08-29 17:14:33 +0000
commite4186762ffaa4188e16702e8f4f299ea70988b96 (patch)
tree2ee78b84ead113ad17b029743432c24a37a362f9 /src/include/funcapi.h
parent0201dac1c319599aad3feb752d7ee5162200e2c7 (diff)
Adjust nodeFunctionscan.c to reset transient memory context between calls
to the table function, thus preventing memory leakage accumulation across calls. This means that SRFs need to be careful to distinguish permanent and local storage; adjust code and documentation accordingly. Patch by Joe Conway, very minor tweaks by Tom Lane.
Diffstat (limited to 'src/include/funcapi.h')
-rw-r--r--src/include/funcapi.h22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/include/funcapi.h b/src/include/funcapi.h
index 27dbdf20e62..fcfb6acb694 100644
--- a/src/include/funcapi.h
+++ b/src/include/funcapi.h
@@ -9,7 +9,7 @@
*
* Copyright (c) 2002, PostgreSQL Global Development Group
*
- * $Id: funcapi.h,v 1.5 2002/08/29 00:17:06 tgl Exp $
+ * $Id: funcapi.h,v 1.6 2002/08/29 17:14:33 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -101,13 +101,14 @@ typedef struct FuncCallContext
AttInMetadata *attinmeta;
/*
- * memory context used to initialize structure
+ * memory context used for structures which must live for multiple calls
*
- * fmctx is set by SRF_FIRSTCALL_INIT() for you, and used by
- * SRF_RETURN_DONE() for cleanup. It is primarily for internal use
- * by the API.
+ * multi_call_memory_ctx is set by SRF_FIRSTCALL_INIT() for you, and used
+ * by SRF_RETURN_DONE() for cleanup. It is the most appropriate memory
+ * context for any memory that is to be re-used across multiple calls
+ * of the SRF.
*/
- MemoryContext fmctx;
+ MemoryContext multi_call_memory_ctx;
} FuncCallContext;
@@ -160,17 +161,22 @@ extern HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
* {
* FuncCallContext *funcctx;
* Datum result;
+ * MemoryContext oldcontext;
* <user defined declarations>
*
- * if(SRF_IS_FIRSTCALL())
+ * if (SRF_IS_FIRSTCALL())
* {
- * <user defined code>
* funcctx = SRF_FIRSTCALL_INIT();
+ * // switch context when allocating stuff to be used in later calls
+ * oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
+ * <user defined code>
* <if returning composite>
* <obtain slot>
* funcctx->slot = slot;
* <endif returning composite>
* <user defined code>
+ * // return to original context when allocating transient memory
+ * MemoryContextSwitchTo(oldcontext);
* }
* <user defined code>
* funcctx = SRF_PERCALL_SETUP();