1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter46.Writing A Procedural Language Handler</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="internals.html" title="PartVII.Internals">
<link rel="prev" href="nls-programmer.html" title="45.2.For the Programmer">
<link rel="next" href="geqo.html" title="Chapter47.Genetic Query Optimizer">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" lang="en" id="plhandler">
<div class="titlepage"><div><div><h2 class="title">
<a name="plhandler"></a>Chapter46.Writing A Procedural Language Handler</h2></div></div></div>
<a name="id841099"></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 <em class="firstterm">call handler</em>
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 <code class="type">language_handler</code>. 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
<code class="structname">FunctionCallInfoData</code> <code class="type">struct</code> containing
argument values and information about the called function, and it
is expected to return a <code class="type">Datum</code> result (and possibly
set the <code class="structfield">isnull</code> field of the
<code class="structname">FunctionCallInfoData</code> structure, if it wishes
to return an SQL null result). The difference between a call
handler and an ordinary callee function is that the
<code class="structfield">flinfo->fn_oid</code> field of the
<code class="structname">FunctionCallInfoData</code> 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 <code class="literal">AS</code> clause from the
<code class="command">CREATE FUNCTION</code> command for the function will be found
in the <code class="literal">prosrc</code> 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
<code class="structfield">flinfo->fn_extra</code> field. This will
initially be <code class="symbol">NULL</code>, but can be set by the call handler to point at
information about the called function. On subsequent calls, if
<code class="structfield">flinfo->fn_extra</code> is already non-<code class="symbol">NULL</code>
then it can be used and the information lookup step skipped. The
call handler must make sure that
<code class="structfield">flinfo->fn_extra</code> is made to point at
memory that will live at least until the end of the current query,
since an <code class="structname">FmgrInfo</code> data structure could be
kept that long. One way to do this is to allocate the extra data
in the memory context specified by
<code class="structfield">flinfo->fn_mcxt</code>; such data will
normally have the same lifespan as the
<code class="structname">FmgrInfo</code> 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
<code class="structname">FunctionCallInfoData</code>'s
<code class="structfield">context</code> field points at a
<code class="structname">TriggerData</code> structure, rather than being <code class="symbol">NULL</code>
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->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" title="32.9.6.Compiling and Linking Dynamically-Loaded Functions">Section32.9.6, “Compiling and Linking Dynamically-Loaded Functions”</a>), the following commands then
register the sample procedural language:
</p>
<pre class="programlisting">CREATE FUNCTION plsample_call_handler() RETURNS language_handler
AS '<em class="replaceable"><code>filename</code></em>'
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 <code class="filename">src/pl</code> subdirectory of the source tree.
</p>
</div></body>
</html>
|