1 /*-------------------------------------------------------------------------
4 * Utility and convenience functions for fmgr functions that return
5 * sets and/or composite types.
7 * Copyright (c) 2002, PostgreSQL Global Development Group
10 * $Header: /cvsroot/pgsql/src/backend/utils/fmgr/funcapi.c,v 1.5 2002/08/30 19:56:49 tgl Exp $
12 *-------------------------------------------------------------------------
17 #include "catalog/pg_type.h"
18 #include "utils/syscache.h"
23 * Create an empty FuncCallContext data structure
24 * and do some other basic Multi-function call setup
28 init_MultiFuncCall(PG_FUNCTION_ARGS)
30 FuncCallContext *retval;
33 * Bail if we're called in the wrong context
35 if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
36 elog(ERROR, "function called in context that does not accept a set result");
38 if (fcinfo->flinfo->fn_extra == NULL)
43 * Allocate suitably long-lived space and zero it
45 retval = (FuncCallContext *)
46 MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
47 sizeof(FuncCallContext));
48 MemSet(retval, 0, sizeof(FuncCallContext));
51 * initialize the elements
53 retval->call_cntr = 0;
54 retval->max_calls = 0;
56 retval->user_fctx = NULL;
57 retval->attinmeta = NULL;
58 retval->multi_call_memory_ctx = fcinfo->flinfo->fn_mcxt;
61 * save the pointer for cross-call use
63 fcinfo->flinfo->fn_extra = retval;
65 else /* second and subsequent calls */
67 elog(ERROR, "init_MultiFuncCall may not be called more than once");
69 /* never reached, but keep compiler happy */
79 * Do Multi-function per-call setup
82 per_MultiFuncCall(PG_FUNCTION_ARGS)
84 FuncCallContext *retval = (FuncCallContext *) fcinfo->flinfo->fn_extra;
87 * Clear the TupleTableSlot, if present. This is for safety's sake:
88 * the Slot will be in a long-lived context (it better be, if the
89 * FuncCallContext is pointing to it), but in most usage patterns the
90 * tuples stored in it will be in the function's per-tuple context.
91 * So at the beginning of each call, the Slot will hold a dangling
92 * pointer to an already-recycled tuple. We clear it out here. (See
93 * also the definition of TupleGetDatum() in funcapi.h!)
95 if (retval->slot != NULL)
96 ExecClearTuple(retval->slot);
103 * Clean up after init_MultiFuncCall
106 end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx)
108 /* unbind from fcinfo */
109 fcinfo->flinfo->fn_extra = NULL;
112 * Caller is responsible to free up memory for individual
113 * struct elements other than att_in_funcinfo and elements.
115 if (funcctx->attinmeta != NULL)
116 pfree(funcctx->attinmeta);