working
with C strings, you pass the <structname>TupleDesc</structname> to
<function>TupleDescGetAttInMetadata</function>, and then call
<function>BuildTupleFromCStrings</function> for each row. In the case of a
function returning a set of tuples, the setup steps can all be done
once during the first call of the function.
</para>
<para>
Several helper functions are available for setting up the needed
<structname>TupleDesc</structname>. The recommended way to do this in most
functions returning composite values is to call:
<programlisting>
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo,
Oid *resultTypeId,
TupleDesc *resultTupleDesc)
</programlisting>
passing the same <literal>fcinfo</literal> struct passed to the calling function
itself. (This of course requires that you use the version-1
calling conventions.) <varname>resultTypeId</varname> can be specified
as <literal>NULL</literal> or as the address of a local variable to receive the
function's result type OID. <varname>resultTupleDesc</varname> should be the
address of a local <structname>TupleDesc</structname> variable. Check that the
result is <literal>TYPEFUNC_COMPOSITE</literal>; if so,
<varname>resultTupleDesc</varname> has been filled with the needed
<structname>TupleDesc</structname>. (If it is not, you can report an error along
the lines of <quote>function returning record called in context that
cannot accept type record</quote>.)
</para>
<tip>
<para>
<function>get_call_result_type</function> can resolve the actual type of a
polymorphic function result; so it is useful in functions that return
scalar polymorphic results, not only functions that return composites.
The <varname>resultTypeId</varname> output is primarily useful for functions
returning polymorphic scalars.
</para>
</tip>
<note>
<para>
<function>get_call_result_type</function> has a sibling
<function>get_expr_result_type</function>, which can be used to resolve the
expected output type for a function call represented by an expression
tree. This can be used when trying to determine the result type from
outside the function itself. There is also
<function>get_func_result_type</function>, which can be used when only the
function's OID is available. However these functions are not able
to deal with functions declared to return <structname>record</structname>, and
<function>get_func_result_type</function> cannot resolve polymorphic types,
so you should preferentially use <function>get_call_result_type</function>.
</para>
</note>
<para>
Older, now-deprecated functions for obtaining
<structname>TupleDesc</structname>s are:
<programlisting>
TupleDesc RelationNameGetTupleDesc(const char *relname)
</programlisting>
to get a <structname>TupleDesc</structname> for the row type of a named relation,
and:
<programlisting>
TupleDesc TypeGetTupleDesc(Oid typeoid, List *colaliases)
</programlisting>
to get a <structname>TupleDesc</structname> based on a type OID. This can
be used to get a <structname>TupleDesc</structname> for a base or
composite type. It will not work for a function that returns
<structname>record</structname>, however, and it cannot resolve polymorphic
types.
</para>
<para>
Once you have a <structname>TupleDesc</structname>, call:
<programlisting>
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
</programlisting>
if you plan to work with Datums, or:
<programlisting>
AttInMetadata *TupleDescGetAttInMetadata(TupleDesc tupdesc)
</programlisting>
if you plan to work with C strings. If you are writing a function
returning set, you can save the results of these functions in the
<structname>FuncCallContext</structname>