Home Explore Blog CI



postgresql

1st chunk of `doc/src/sgml/plhandler.sgml`
9fcc69198303978ef8aa8a4783eb7f386d31421f3e70e95d0000000100000fa2
<!-- doc/src/sgml/plhandler.sgml -->

 <chapter id="plhandler">
   <title>Writing a Procedural Language Handler</title>

   <indexterm zone="plhandler">
    <primary>procedural language</primary>
    <secondary>handler for</secondary>
   </indexterm>

   <para>
    All calls to functions that are written in a language other than
    the current <quote>version 1</quote> interface for compiled
    languages (this includes functions in user-defined procedural languages
    and functions written in SQL) go through a <firstterm>call handler</firstterm>
    function for the specific language.  It is the responsibility of
    the call handler to execute the function in a meaningful way, such
    as by interpreting the supplied source text.  This chapter outlines
    how a new procedural language's call handler can be written.
   </para>

   <para>
    The call handler for a procedural language is a
    <quote>normal</quote> function that must be written in a compiled
    language such as C, using the version-1 interface, and registered
    with <productname>PostgreSQL</productname> as taking no arguments
    and returning the type <type>language_handler</type>.  This
    special pseudo-type identifies the function as a call handler and
    prevents it from being called directly in SQL commands.
    For more details on C language calling conventions and dynamic loading,
    see <xref linkend="xfunc-c"/>.
   </para>

   <para>
    The call handler is called in the same way as any other function:
    It receives a pointer to a
    <structname>FunctionCallInfoBaseData</structname> <type>struct</type> containing
    argument values and information about the called function, and it
    is expected to return a <type>Datum</type> result (and possibly
    set the <structfield>isnull</structfield> field of the
    <structname>FunctionCallInfoBaseData</structname> structure, if it wishes
    to return an SQL null result).  The difference between a call
    handler and an ordinary callee function is that the
    <structfield>flinfo-&gt;fn_oid</structfield> field of the
    <structname>FunctionCallInfoBaseData</structname> structure will contain
    the OID of the actual function to be called, not of the call
    handler itself.  The call handler must use this field to determine
    which function to execute.  Also, the passed argument list has
    been set up according to the declaration of the target function,
    not of the call handler.
   </para>

   <para>
    It's up to the call handler to fetch the entry of the function from the
    <classname>pg_proc</classname> system catalog and to analyze the argument
    and return types of the called function. The <literal>AS</literal> clause from the
    <command>CREATE FUNCTION</command> command for the function will be found
    in the <literal>prosrc</literal> column of the
    <classname>pg_proc</classname> row. This is commonly source
    text in the procedural language, but in theory it could be something else,
    such as a path name to a file, or anything else that tells the call handler
    what to do in detail.
   </para>

   <para>
    Often, the same function is called many times per SQL statement.
    A call handler can avoid repeated lookups of information about the
    called function by using the
    <structfield>flinfo-&gt;fn_extra</structfield> field.  This will
    initially be <symbol>NULL</symbol>, but can be set by the call handler to point at
    information about the called function.  On subsequent calls, if
    <structfield>flinfo-&gt;fn_extra</structfield> is already non-<symbol>NULL</symbol>
    then it can be used and the information lookup step skipped.  The
    call handler must make sure that
    <structfield>flinfo-&gt;fn_extra</structfield> is made to point at
    memory that will live at least until the end of the current query,
    since an <structname>FmgrInfo</structname> data structure could be
    kept that long.  One way to do this is to allocate the extra data

Title: Writing a Procedural Language Handler
Summary
This chapter explains how to write a call handler for a new procedural language in PostgreSQL, including the responsibilities of the call handler, its registration, and how it is called and executes functions written in the procedural language.