]> begriffs open source - ai-pg/blob - full-docs/html/plhandler.html
Include links to all subsection html pages, with shorter paths too
[ai-pg] / full-docs / html / plhandler.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter 57. Writing a Procedural Language Handler</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="nls-programmer.html" title="56.2. For the Programmer" /><link rel="next" href="fdwhandler.html" title="Chapter 58. Writing a Foreign Data Wrapper" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">Chapter 57. Writing a Procedural Language Handler</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="nls-programmer.html" title="56.2. For the Programmer">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="internals.html" title="Part VII. Internals">Up</a></td><th width="60%" align="center">Part VII. Internals</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="fdwhandler.html" title="Chapter 58. Writing a Foreign Data Wrapper">Next</a></td></tr></table><hr /></div><div class="chapter" id="PLHANDLER"><div class="titlepage"><div><div><h2 class="title">Chapter 57. Writing a Procedural Language Handler</h2></div></div></div><a id="id-1.10.9.2" class="indexterm"></a><p>
3     All calls to functions that are written in a language other than
4     the current <span class="quote">“<span class="quote">version 1</span>”</span> interface for compiled
5     languages (this includes functions in user-defined procedural languages
6     and functions written in SQL) go through a <em class="firstterm">call handler</em>
7     function for the specific language.  It is the responsibility of
8     the call handler to execute the function in a meaningful way, such
9     as by interpreting the supplied source text.  This chapter outlines
10     how a new procedural language's call handler can be written.
11    </p><p>
12     The call handler for a procedural language is a
13     <span class="quote">“<span class="quote">normal</span>”</span> function that must be written in a compiled
14     language such as C, using the version-1 interface, and registered
15     with <span class="productname">PostgreSQL</span> as taking no arguments
16     and returning the type <code class="type">language_handler</code>.  This
17     special pseudo-type identifies the function as a call handler and
18     prevents it from being called directly in SQL commands.
19     For more details on C language calling conventions and dynamic loading,
20     see <a class="xref" href="xfunc-c.html" title="36.10. C-Language Functions">Section 36.10</a>.
21    </p><p>
22     The call handler is called in the same way as any other function:
23     It receives a pointer to a
24     <code class="structname">FunctionCallInfoBaseData</code> <code class="type">struct</code> containing
25     argument values and information about the called function, and it
26     is expected to return a <code class="type">Datum</code> result (and possibly
27     set the <code class="structfield">isnull</code> field of the
28     <code class="structname">FunctionCallInfoBaseData</code> structure, if it wishes
29     to return an SQL null result).  The difference between a call
30     handler and an ordinary callee function is that the
31     <code class="structfield">flinfo-&gt;fn_oid</code> field of the
32     <code class="structname">FunctionCallInfoBaseData</code> structure will contain
33     the OID of the actual function to be called, not of the call
34     handler itself.  The call handler must use this field to determine
35     which function to execute.  Also, the passed argument list has
36     been set up according to the declaration of the target function,
37     not of the call handler.
38    </p><p>
39     It's up to the call handler to fetch the entry of the function from the
40     <code class="classname">pg_proc</code> system catalog and to analyze the argument
41     and return types of the called function. The <code class="literal">AS</code> clause from the
42     <code class="command">CREATE FUNCTION</code> command for the function will be found
43     in the <code class="literal">prosrc</code> column of the
44     <code class="classname">pg_proc</code> row. This is commonly source
45     text in the procedural language, but in theory it could be something else,
46     such as a path name to a file, or anything else that tells the call handler
47     what to do in detail.
48    </p><p>
49     Often, the same function is called many times per SQL statement.
50     A call handler can avoid repeated lookups of information about the
51     called function by using the
52     <code class="structfield">flinfo-&gt;fn_extra</code> field.  This will
53     initially be <code class="symbol">NULL</code>, but can be set by the call handler to point at
54     information about the called function.  On subsequent calls, if
55     <code class="structfield">flinfo-&gt;fn_extra</code> is already non-<code class="symbol">NULL</code>
56     then it can be used and the information lookup step skipped.  The
57     call handler must make sure that
58     <code class="structfield">flinfo-&gt;fn_extra</code> is made to point at
59     memory that will live at least until the end of the current query,
60     since an <code class="structname">FmgrInfo</code> data structure could be
61     kept that long.  One way to do this is to allocate the extra data
62     in the memory context specified by
63     <code class="structfield">flinfo-&gt;fn_mcxt</code>; such data will
64     normally have the same lifespan as the
65     <code class="structname">FmgrInfo</code> itself.  But the handler could
66     also choose to use a longer-lived memory context so that it can cache
67     function definition information across queries.
68    </p><p>
69     When a procedural-language function is invoked as a trigger, no arguments
70     are passed in the usual way, but the
71     <code class="structname">FunctionCallInfoBaseData</code>'s
72     <code class="structfield">context</code> field points at a
73     <code class="structname">TriggerData</code> structure, rather than being <code class="symbol">NULL</code>
74     as it is in a plain function call.  A language handler should
75     provide mechanisms for procedural-language functions to get at the trigger
76     information.
77    </p><p>
78     A template for a procedural-language handler written as a C extension is
79     provided in <code class="literal">src/test/modules/plsample</code>.  This is a
80     working sample demonstrating one way to create a procedural-language
81     handler, process parameters, and return a value.
82    </p><p>
83     Although providing a call handler is sufficient to create a minimal
84     procedural language, there are two other functions that can optionally
85     be provided to make the language more convenient to use.  These
86     are a <em class="firstterm">validator</em> and an
87     <em class="firstterm">inline handler</em>.  A validator can be provided
88     to allow language-specific checking to be done during
89     <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a>.
90     An inline handler can be provided to allow the language to support
91     anonymous code blocks executed via the <a class="xref" href="sql-do.html" title="DO"><span class="refentrytitle">DO</span></a> command.
92    </p><p>
93     If a validator is provided by a procedural language, it
94     must be declared as a function taking a single parameter of type
95     <code class="type">oid</code>.  The validator's result is ignored, so it is customarily
96     declared to return <code class="type">void</code>.  The validator will be called at
97     the end of a <code class="command">CREATE FUNCTION</code> command that has created
98     or updated a function written in the procedural language.
99     The passed-in OID is the OID of the function's <code class="classname">pg_proc</code>
100     row.  The validator must fetch this row in the usual way, and do
101     whatever checking is appropriate.
102     First, call <code class="function">CheckFunctionValidatorAccess()</code> to diagnose
103     explicit calls to the validator that the user could not achieve through
104     <code class="command">CREATE FUNCTION</code>.  Typical checks then include verifying
105     that the function's argument and result types are supported by the
106     language, and that the function's body is syntactically correct
107     in the language.  If the validator finds the function to be okay,
108     it should just return.  If it finds an error, it should report that
109     via the normal <code class="function">ereport()</code> error reporting mechanism.
110     Throwing an error will force a transaction rollback and thus prevent
111     the incorrect function definition from being committed.
112    </p><p>
113     Validator functions should typically honor the <a class="xref" href="runtime-config-client.html#GUC-CHECK-FUNCTION-BODIES">check_function_bodies</a> parameter: if it is turned off then
114     any expensive or context-sensitive checking should be skipped.  If the
115     language provides for code execution at compilation time, the validator
116     must suppress checks that would induce such execution.  In particular,
117     this parameter is turned off by <span class="application">pg_dump</span> so that it can
118     load procedural language functions without worrying about side effects or
119     dependencies of the function bodies on other database objects.
120     (Because of this requirement, the call handler should avoid
121     assuming that the validator has fully checked the function.  The point
122     of having a validator is not to let the call handler omit checks, but
123     to notify the user immediately if there are obvious errors in a
124     <code class="command">CREATE FUNCTION</code> command.)
125     While the choice of exactly what to check is mostly left to the
126     discretion of the validator function, note that the core
127     <code class="command">CREATE FUNCTION</code> code only executes <code class="literal">SET</code> clauses
128     attached to a function when <code class="varname">check_function_bodies</code> is on.
129     Therefore, checks whose results might be affected by GUC parameters
130     definitely should be skipped when <code class="varname">check_function_bodies</code> is
131     off, to avoid false failures when restoring a dump.
132    </p><p>
133     If an inline handler is provided by a procedural language, it
134     must be declared as a function taking a single parameter of type
135     <code class="type">internal</code>.  The inline handler's result is ignored, so it is
136     customarily declared to return <code class="type">void</code>.  The inline handler
137     will be called when a <code class="command">DO</code> statement is executed specifying
138     the procedural language.  The parameter actually passed is a pointer
139     to an <code class="structname">InlineCodeBlock</code> struct, which contains information
140     about the <code class="command">DO</code> statement's parameters, in particular the
141     text of the anonymous code block to be executed.  The inline handler
142     should execute this code and return.
143    </p><p>
144     It's recommended that you wrap all these function declarations,
145     as well as the <code class="command">CREATE LANGUAGE</code> command itself, into
146     an <em class="firstterm">extension</em> so that a simple <code class="command">CREATE EXTENSION</code>
147     command is sufficient to install the language.  See
148     <a class="xref" href="extend-extensions.html" title="36.17. Packaging Related Objects into an Extension">Section 36.17</a> for information about writing
149     extensions.
150    </p><p>
151     The procedural languages included in the standard distribution
152     are good references when trying to write your own language handler.
153     Look into the <code class="filename">src/pl</code> subdirectory of the source tree.
154     The <a class="xref" href="sql-createlanguage.html" title="CREATE LANGUAGE"><span class="refentrytitle">CREATE LANGUAGE</span></a>
155     reference page also has some useful details.
156    </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="nls-programmer.html" title="56.2. For the Programmer">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="internals.html" title="Part VII. Internals">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="fdwhandler.html" title="Chapter 58. Writing a Foreign Data Wrapper">Next</a></td></tr><tr><td width="40%" align="left" valign="top">56.2. For the Programmer </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> Chapter 58. Writing a Foreign Data Wrapper</td></tr></table></div></body></html>