Skip to content

Commit 822b015

Browse files
committed
Update plhandler.sgml to describe validators and inline handlers for
procedural languages.
1 parent 717fa27 commit 822b015

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

doc/src/sgml/plhandler.sgml

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/plhandler.sgml,v 1.7 2006/09/16 00:30:14 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/plhandler.sgml,v 1.8 2009/10/08 04:41:07 tgl Exp $ -->
22

33
<chapter id="plhandler">
44
<title>Writing A Procedural Language Handler</title>
@@ -13,7 +13,7 @@
1313
the current <quote>version 1</quote> interface for compiled
1414
languages (this includes functions in user-defined procedural languages,
1515
functions written in SQL, and functions using the version 0 compiled
16-
language interface), go through a <firstterm>call handler</firstterm>
16+
language interface) go through a <firstterm>call handler</firstterm>
1717
function for the specific language. It is the responsibility of
1818
the call handler to execute the function in a meaningful way, such
1919
as by interpreting the supplied source text. This chapter outlines
@@ -51,8 +51,7 @@
5151

5252
<para>
5353
It's up to the call handler to fetch the entry of the function from the
54-
system table
55-
<classname>pg_proc</classname> and to analyze the argument
54+
<classname>pg_proc</classname> system catalog and to analyze the argument
5655
and return types of the called function. The <literal>AS</> clause from the
5756
<command>CREATE FUNCTION</command> command for the function will be found
5857
in the <literal>prosrc</literal> column of the
@@ -152,10 +151,71 @@ CREATE LANGUAGE plsample
152151
</programlisting>
153152
</para>
154153

154+
<para>
155+
Although providing a call handler is sufficient to create a minimal
156+
procedural language, there are two other functions that can optionally
157+
be provided to make the language more convenient to use. These
158+
are a <firstterm>validator</firstterm> and an
159+
<firstterm>inline handler</firstterm>. A validator can be provided
160+
to allow language-specific checking to be done during
161+
<xref linkend="sql-createfunction" endterm="sql-createfunction-title">.
162+
An inline handler can be provided to allow the language to support
163+
anonymous code blocks executed via the <xref linkend="sql-do"
164+
endterm="sql-do-title"> command.
165+
</para>
166+
167+
<para>
168+
If a validator is provided by a procedural language, it
169+
must be declared as a function taking a single parameter of type
170+
<type>oid</>. The validator's result is ignored, so it is customarily
171+
declared to return <type>void</>. The validator will be called at
172+
the end of a <command>CREATE FUNCTION</> command that has created
173+
or updated a function written in the procedural language.
174+
The passed-in OID is the OID of the function's <classname>pg_proc</>
175+
row. The validator must fetch this row in the usual way, and do
176+
whatever checking is appropriate. Typical checks include verifying
177+
that the function's argument and result types are supported by the
178+
language, and that the function's body is syntactically correct
179+
in the language. If the validator finds the function to be okay,
180+
it should just return. If it finds an error, it should report that
181+
via the normal <function>ereport()</> error reporting mechanism.
182+
Throwing an error will force a transaction rollback and thus prevent
183+
the incorrect function definition from being committed.
184+
</para>
185+
186+
<para>
187+
Validator functions should typically honor the <xref
188+
linkend="guc-check-function-bodies"> parameter: if it is turned off then
189+
any expensive or context-sensitive checking should be skipped.
190+
In particular, this parameter is turned off by <application>pg_dump</>
191+
so that it can load procedural language functions without worrying
192+
about possible dependencies of the function bodies on other database
193+
objects. (Because of this requirement, the call handler should avoid
194+
assuming that the validator has fully checked the function. The point
195+
of having a validator is not to let the call handler omit checks, but
196+
to notify the user immediately if there are obvious errors in a
197+
<command>CREATE FUNCTION</> command.)
198+
</para>
199+
200+
<para>
201+
If an inline handler is provided by a procedural language, it
202+
must be declared as a function taking a single parameter of type
203+
<type>internal</>. The inline handler's result is ignored, so it is
204+
customarily declared to return <type>void</>. The inline handler
205+
will be called when a <command>DO</> statement is executed specifying
206+
the procedural language. The parameter actually passed is a pointer
207+
to an <structname>InlineCodeBlock</> struct, which contains information
208+
about the <command>DO</> statement's parameters, in particular the
209+
text of the anonymous code block to be executed. The inline handler
210+
should execute this code and return.
211+
</para>
212+
155213
<para>
156214
The procedural languages included in the standard distribution
157-
are good references when trying to write your own call handler.
215+
are good references when trying to write your own language handler.
158216
Look into the <filename>src/pl</> subdirectory of the source tree.
217+
The <xref linkend="sql-createlanguage" endterm="sql-createlanguage-title">
218+
reference page also has some useful details.
159219
</para>
160220

161221
</chapter>

doc/src/sgml/ref/create_language.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_language.sgml,v 1.46 2009/09/22 23:43:37 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_language.sgml,v 1.47 2009/10/08 04:41:07 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -41,7 +41,7 @@ CREATE [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</
4141
<para>
4242
<command>CREATE LANGUAGE</command> effectively associates the
4343
language name with a call handler that is responsible for executing
44-
functions written in the language. Refer to <xref linkend="xplang">
44+
functions written in the language. Refer to <xref linkend="plhandler">
4545
for more information about language call handlers.
4646
</para>
4747

0 commit comments

Comments
 (0)