Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-testing > by-pkgid > bab02a23fa9f3df8d66a9a3231b50245 > files > 403

postgresql8.3-docs-8.3.6-2mdv2008.1.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Writing A Procedural Language Handler</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 8.3.6 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Internals"
HREF="internals.html"><LINK
REL="PREVIOUS"
TITLE="For the Programmer"
HREF="nls-programmer.html"><LINK
REL="NEXT"
TITLE="Genetic Query Optimizer"
HREF="geqo.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2009-02-03T04:34:16"></HEAD
><BODY
CLASS="CHAPTER"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 8.3.6 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="nls-programmer.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="nls.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="geqo.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="geqo.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="PLHANDLER"
></A
>Chapter 48. Writing A Procedural Language Handler</H1
><A
NAME="AEN75513"
></A
><P
>    All calls to functions that are written in a language other than
    the current <SPAN
CLASS="QUOTE"
>"version 1"</SPAN
> interface for compiled
    languages (this includes functions in user-defined procedural languages,
    functions written in SQL, and functions using the version 0 compiled
    language interface), go through a <I
CLASS="FIRSTTERM"
>call handler</I
>
    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.
   </P
><P
>    The call handler for a procedural language is a
    <SPAN
CLASS="QUOTE"
>"normal"</SPAN
> function that must be written in a compiled
    language such as C, using the version-1 interface, and registered
    with <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> as taking no arguments
    and returning the type <TT
CLASS="TYPE"
>language_handler</TT
>.  This
    special pseudotype identifies the function as a call handler and
    prevents it from being called directly in SQL commands.
   </P
><P
>    The call handler is called in the same way as any other function:
    It receives a pointer to a
    <TT
CLASS="STRUCTNAME"
>FunctionCallInfoData</TT
> <TT
CLASS="TYPE"
>struct</TT
> containing
    argument values and information about the called function, and it
    is expected to return a <TT
CLASS="TYPE"
>Datum</TT
> result (and possibly
    set the <TT
CLASS="STRUCTFIELD"
>isnull</TT
> field of the
    <TT
CLASS="STRUCTNAME"
>FunctionCallInfoData</TT
> structure, if it wishes
    to return an SQL null result).  The difference between a call
    handler and an ordinary callee function is that the
    <TT
CLASS="STRUCTFIELD"
>flinfo-&gt;fn_oid</TT
> field of the
    <TT
CLASS="STRUCTNAME"
>FunctionCallInfoData</TT
> 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.
   </P
><P
>    It's up to the call handler to fetch the entry of the function from the
    system table
    <CODE
CLASS="CLASSNAME"
>pg_proc</CODE
> and to analyze the argument
    and return types of the called function. The <TT
CLASS="LITERAL"
>AS</TT
> clause from the
    <TT
CLASS="COMMAND"
>CREATE FUNCTION</TT
> command for the function will be found
    in the <TT
CLASS="LITERAL"
>prosrc</TT
> column of the
    <CODE
CLASS="CLASSNAME"
>pg_proc</CODE
> 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.
   </P
><P
>    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
    <TT
CLASS="STRUCTFIELD"
>flinfo-&gt;fn_extra</TT
> field.  This will
    initially be <TT
CLASS="SYMBOL"
>NULL</TT
>, but can be set by the call handler to point at
    information about the called function.  On subsequent calls, if
    <TT
CLASS="STRUCTFIELD"
>flinfo-&gt;fn_extra</TT
> is already non-<TT
CLASS="SYMBOL"
>NULL</TT
>
    then it can be used and the information lookup step skipped.  The
    call handler must make sure that
    <TT
CLASS="STRUCTFIELD"
>flinfo-&gt;fn_extra</TT
> is made to point at
    memory that will live at least until the end of the current query,
    since an <TT
CLASS="STRUCTNAME"
>FmgrInfo</TT
> data structure could be
    kept that long.  One way to do this is to allocate the extra data
    in the memory context specified by
    <TT
CLASS="STRUCTFIELD"
>flinfo-&gt;fn_mcxt</TT
>; such data will
    normally have the same lifespan as the
    <TT
CLASS="STRUCTNAME"
>FmgrInfo</TT
> itself.  But the handler could
    also choose to use a longer-lived memory context so that it can cache
    function definition information across queries.
   </P
><P
>    When a procedural-language function is invoked as a trigger, no arguments
    are passed in the usual way, but the
    <TT
CLASS="STRUCTNAME"
>FunctionCallInfoData</TT
>'s
    <TT
CLASS="STRUCTFIELD"
>context</TT
> field points at a
    <TT
CLASS="STRUCTNAME"
>TriggerData</TT
> structure, rather than being <TT
CLASS="SYMBOL"
>NULL</TT
>
    as it is in a plain function call.  A language handler should
    provide mechanisms for procedural-language functions to get at the trigger
    information.
   </P
><P
>    This is a template for a procedural-language handler written in C:
</P><PRE
CLASS="PROGRAMLISTING"
>#include "postgres.h"
#include "executor/spi.h"
#include "commands/trigger.h"
#include "fmgr.h"
#include "access/heapam.h"
#include "utils/syscache.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"

PG_FUNCTION_INFO_V1(plsample_call_handler);

Datum
plsample_call_handler(PG_FUNCTION_ARGS)
{
    Datum          retval;

    if (CALLED_AS_TRIGGER(fcinfo))
    {
        /*
         * Called as a trigger procedure
         */
        TriggerData    *trigdata = (TriggerData *) fcinfo-&gt;context;

        retval = ...
    }
    else
    {
        /*
         * Called as a function
         */

        retval = ...
    }

    return retval;
}</PRE
><P>
    Only a few thousand lines of code have to be added instead of the
    dots to complete the call handler.
   </P
><P
>    After having compiled the handler function into a loadable module
    (see <A
HREF="xfunc-c.html#DFUNC"
>Section 34.9.6</A
>), the following commands then
    register the sample procedural language:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION plsample_call_handler() RETURNS language_handler
    AS '<TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
>'
    LANGUAGE C;
CREATE LANGUAGE plsample
    HANDLER plsample_call_handler;</PRE
><P>
   </P
><P
>    The procedural languages included in the standard distribution
    are good references when trying to write your own call handler.
    Look into the <TT
CLASS="FILENAME"
>src/pl</TT
> subdirectory of the source tree.
   </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="nls-programmer.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="geqo.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>For the Programmer</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="internals.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Genetic Query Optimizer</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>