Home Explore Blog CI



postgresql

41th chunk of `doc/src/sgml/xfunc.sgml`
9448d69a1bd892e02c249136da912a20bac840ce8b504f0c0000000100000fa2
 void *user_fctx;

    /*
     * OPTIONAL pointer to struct containing attribute type input metadata
     *
     * attinmeta is for use when returning tuples (i.e., composite data types)
     * and is not used when returning base data types. It is only needed
     * if you intend to use BuildTupleFromCStrings() to create the return
     * tuple.
     */
    AttInMetadata *attinmeta;

    /*
     * memory context used for structures that must live for multiple calls
     *
     * 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 reused across multiple calls
     * of the SRF.
     */
    MemoryContext multi_call_memory_ctx;

    /*
     * OPTIONAL pointer to struct containing tuple description
     *
     * tuple_desc is for use when returning tuples (i.e., composite data types)
     * and is only needed if you are going to build the tuples with
     * heap_form_tuple() rather than with BuildTupleFromCStrings().  Note that
     * the TupleDesc pointer stored here should usually have been run through
     * BlessTupleDesc() first.
     */
    TupleDesc tuple_desc;

} FuncCallContext;
</programlisting>
    </para>

    <para>
     The macros to be used by an <acronym>SRF</acronym> using this
     infrastructure are:
<programlisting>
SRF_IS_FIRSTCALL()
</programlisting>
     Use this to determine if your function is being called for the first or a
     subsequent time. On the first call (only), call:
<programlisting>
SRF_FIRSTCALL_INIT()
</programlisting>
     to initialize the <structname>FuncCallContext</structname>. On every function call,
     including the first, call:
<programlisting>
SRF_PERCALL_SETUP()
</programlisting>
     to set up for using the <structname>FuncCallContext</structname>.
    </para>

    <para>
     If your function has data to return in the current call, use:
<programlisting>
SRF_RETURN_NEXT(funcctx, result)
</programlisting>
     to return it to the caller.  (<literal>result</literal> must be of type
     <type>Datum</type>, either a single value or a tuple prepared as
     described above.)  Finally, when your function is finished
     returning data, use:
<programlisting>
SRF_RETURN_DONE(funcctx)
</programlisting>
     to clean up and end the <acronym>SRF</acronym>.
    </para>

    <para>
     The memory context that is current when the <acronym>SRF</acronym> is called is
     a transient context that will be cleared between calls.  This means
     that you do not need to call <function>pfree</function> on everything
     you allocated using <function>palloc</function>; it will go away anyway.  However, if you want to allocate
     any data structures to live across calls, you need to put them somewhere
     else.  The memory context referenced by
     <structfield>multi_call_memory_ctx</structfield> is a suitable location for any
     data that needs to survive until the <acronym>SRF</acronym> is finished running.  In most
     cases, this means that you should switch into
     <structfield>multi_call_memory_ctx</structfield> while doing the
     first-call setup.
     Use <literal>funcctx-&gt;user_fctx</literal> to hold a pointer to
     any such cross-call data structures.
     (Data you allocate
     in <structfield>multi_call_memory_ctx</structfield> will go away
     automatically when the query ends, so it is not necessary to free
     that data manually, either.)
    </para>

    <warning>
     <para>
      While the actual arguments to the function remain unchanged between
      calls, if you detoast the argument values (which is normally done
      transparently by the
      <function>PG_GETARG_<replaceable>xxx</replaceable></function> macro)
      in the transient context then the detoasted copies will be freed on
      each cycle. Accordingly, if you keep references to such values in
      your <structfield>user_fctx</structfield>, you must either copy

Title: FuncCallContext Structure Details and SRF Macros
Summary
This section details the fields within the `FuncCallContext` structure used by set-returning functions (SRFs) in ValuePerCall mode. It describes `tuple_desc`, which is used for returning composite data types, and then presents macros like `SRF_IS_FIRSTCALL()`, `SRF_FIRSTCALL_INIT()`, `SRF_PERCALL_SETUP()`, `SRF_RETURN_NEXT()`, and `SRF_RETURN_DONE()` for managing the SRF's lifecycle. It emphasizes the importance of using the `multi_call_memory_ctx` memory context for data structures that need to persist across SRF calls, and storing pointers to these structures in `user_fctx`. A warning is given about detoasting argument values in the transient context, as these copies are freed between calls, and references to them stored in `user_fctx` must be copied to a safe memory context.