2000-03-31 05:27:42 +02:00
|
|
|
<!--
|
2003-10-23 00:28:10 +02:00
|
|
|
$Header: /cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.25 2003/10/22 22:28:10 tgl Exp $
|
2000-03-31 05:27:42 +02:00
|
|
|
-->
|
|
|
|
|
1999-07-22 17:11:05 +02:00
|
|
|
<chapter id="xplang">
|
|
|
|
<title id="xplang-title">Procedural Languages</title>
|
1998-10-21 07:30:16 +02:00
|
|
|
|
2003-08-31 19:32:24 +02:00
|
|
|
<indexterm zone="xplang">
|
|
|
|
<primary>procedural language</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
2000-03-31 00:13:30 +02:00
|
|
|
<para>
|
2001-11-21 07:09:45 +01:00
|
|
|
<productname>PostgreSQL</productname> allows users to add new
|
2001-02-04 16:28:18 +01:00
|
|
|
programming languages to be available for writing functions and
|
|
|
|
procedures. These are called <firstterm>procedural
|
|
|
|
languages</firstterm> (PL). In the case of a function or trigger
|
|
|
|
procedure written in a procedural language, the database server has
|
2000-04-03 00:59:40 +02:00
|
|
|
no built-in knowledge about how to interpret the function's source
|
2001-02-04 16:28:18 +01:00
|
|
|
text. Instead, the task is passed to a special handler that knows
|
|
|
|
the details of the language. The handler could either do all the
|
|
|
|
work of parsing, syntax analysis, execution, etc. itself, or it
|
2001-02-09 03:20:52 +01:00
|
|
|
could serve as <quote>glue</quote> between
|
2001-11-21 07:09:45 +01:00
|
|
|
<productname>PostgreSQL</productname> and an existing implementation
|
2001-02-04 16:28:18 +01:00
|
|
|
of a programming language. The handler itself is a special
|
2003-04-07 03:29:26 +02:00
|
|
|
C language function compiled into a shared object and
|
2001-02-04 16:28:18 +01:00
|
|
|
loaded on demand.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
|
2000-04-03 00:59:40 +02:00
|
|
|
<para>
|
2002-01-07 03:29:15 +01:00
|
|
|
Writing a handler for a new procedural language is described in
|
2003-10-23 00:28:10 +02:00
|
|
|
<xref linkend="plhandler">. Several procedural languages are
|
2002-01-07 03:29:15 +01:00
|
|
|
available in the standard <productname>PostgreSQL</productname>
|
|
|
|
distribution, which can serve as examples.
|
2000-04-03 00:59:40 +02:00
|
|
|
</para>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="xplang-install">
|
2000-03-31 00:13:30 +02:00
|
|
|
<title>Installing Procedural Languages</title>
|
|
|
|
|
2001-02-09 03:20:52 +01:00
|
|
|
<para>
|
|
|
|
A procedural language must be <quote>installed</quote> into each
|
|
|
|
database where it is to be used. But procedural languages installed in
|
2003-04-07 03:29:26 +02:00
|
|
|
the database <literal>template1</> are automatically available in all
|
2001-02-09 03:20:52 +01:00
|
|
|
subsequently created databases. So the database administrator can
|
2003-04-07 03:29:26 +02:00
|
|
|
decide which languages are available in which databases and can make
|
2001-02-09 03:20:52 +01:00
|
|
|
some languages available by default if he chooses.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
For the languages supplied with the standard distribution, the
|
2003-04-07 03:29:26 +02:00
|
|
|
program <command>createlang</command> may be used to install the
|
|
|
|
language instead of carrying out the details by hand. For
|
|
|
|
example, to install the language
|
|
|
|
<application>PL/pgSQL</application> into the database
|
|
|
|
<literal>template1</>, use
|
2001-02-09 03:20:52 +01:00
|
|
|
<programlisting>
|
|
|
|
createlang plpgsql template1
|
|
|
|
</programlisting>
|
|
|
|
The manual procedure described below is only recommended for
|
2003-04-07 03:29:26 +02:00
|
|
|
installing custom languages that <command>createlang</command>
|
2001-02-09 03:20:52 +01:00
|
|
|
does not know about.
|
|
|
|
</para>
|
|
|
|
|
2000-03-31 00:13:30 +02:00
|
|
|
<procedure>
|
1999-07-22 17:11:05 +02:00
|
|
|
<title>
|
2001-02-09 03:20:52 +01:00
|
|
|
Manual Procedural Language Installation
|
1999-07-22 17:11:05 +02:00
|
|
|
</title>
|
1998-10-25 02:29:01 +02:00
|
|
|
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
A procedural language is installed in a database in three steps,
|
|
|
|
which must be carried out by a database superuser. The
|
2003-09-20 22:12:05 +02:00
|
|
|
<command>createlang</command> program automates <xref
|
2003-04-07 03:29:26 +02:00
|
|
|
linkend="xplang-install-cr1"> and <xref
|
|
|
|
linkend="xplang-install-cr2">.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1999-07-22 17:11:05 +02:00
|
|
|
|
2001-02-04 16:28:18 +01:00
|
|
|
<step performance="required">
|
2000-03-31 00:13:30 +02:00
|
|
|
<para>
|
2001-02-04 16:28:18 +01:00
|
|
|
The shared object for the language handler must be compiled and
|
2001-02-09 03:20:52 +01:00
|
|
|
installed into an appropriate library directory. This works in the same
|
|
|
|
way as building and installing modules with regular user-defined C
|
|
|
|
functions does; see <xref linkend="dfunc">.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</step>
|
2001-02-04 16:28:18 +01:00
|
|
|
|
2002-01-07 03:29:15 +01:00
|
|
|
<step performance="required" id="xplang-install-cr1">
|
2000-03-31 00:13:30 +02:00
|
|
|
<para>
|
|
|
|
The handler must be declared with the command
|
2001-02-04 16:28:18 +01:00
|
|
|
<synopsis>
|
2003-04-07 03:29:26 +02:00
|
|
|
CREATE FUNCTION <replaceable>handler_function_name</replaceable>()
|
|
|
|
RETURNS language_handler
|
|
|
|
AS '<replaceable>path-to-shared-object</replaceable>'
|
|
|
|
LANGUAGE C;
|
2001-02-04 16:28:18 +01:00
|
|
|
</synopsis>
|
2003-04-07 03:29:26 +02:00
|
|
|
The special return type of <type>language_handler</type> tells
|
|
|
|
the database system that this function does not return one of
|
2001-02-04 16:28:18 +01:00
|
|
|
the defined <acronym>SQL</acronym> data types and is not directly usable
|
2000-03-31 00:13:30 +02:00
|
|
|
in <acronym>SQL</acronym> statements.
|
|
|
|
</para>
|
|
|
|
</step>
|
2001-02-04 16:28:18 +01:00
|
|
|
|
2002-01-07 03:29:15 +01:00
|
|
|
<step performance="required" id="xplang-install-cr2">
|
2000-03-31 00:13:30 +02:00
|
|
|
<para>
|
|
|
|
The PL must be declared with the command
|
2001-02-04 16:28:18 +01:00
|
|
|
<synopsis>
|
2001-08-13 23:34:54 +02:00
|
|
|
CREATE <optional>TRUSTED</optional> <optional>PROCEDURAL</optional> LANGUAGE <replaceable>language-name</replaceable>
|
|
|
|
HANDLER <replaceable>handler_function_name</replaceable>;
|
2001-02-04 16:28:18 +01:00
|
|
|
</synopsis>
|
2003-04-07 03:29:26 +02:00
|
|
|
The optional key word <literal>TRUSTED</literal> specifies that
|
2002-01-07 03:29:15 +01:00
|
|
|
ordinary database users that have no superuser privileges should
|
|
|
|
be allowed to use this language to create functions and trigger
|
|
|
|
procedures. Since PL functions are executed inside the database
|
|
|
|
server, the <literal>TRUSTED</literal> flag should only be given
|
|
|
|
for languages that do not allow access to database server
|
|
|
|
internals or the file system. The languages
|
|
|
|
<application>PL/pgSQL</application>,
|
2003-06-30 20:31:42 +02:00
|
|
|
<application>PL/Tcl</application>, and
|
|
|
|
<application>PL/Perl</application>
|
|
|
|
are considered trusted; the languages
|
|
|
|
<application>PL/TclU</application>,
|
|
|
|
<application>PL/PerlU</application>, and
|
|
|
|
<application>PL/PythonU</application>
|
|
|
|
are designed to provide unlimited functionality and should
|
|
|
|
<emphasis>not</emphasis> be marked trusted.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</step>
|
|
|
|
</procedure>
|
|
|
|
|
2001-02-04 16:28:18 +01:00
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
<xref linkend="xplang-install-example"> shows how the manual
|
|
|
|
installation procedure would work with the language
|
|
|
|
<application>PL/pgSQL</application>.
|
2001-02-04 16:28:18 +01:00
|
|
|
</para>
|
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<example id="xplang-install-example">
|
2002-01-07 03:29:15 +01:00
|
|
|
<title>Manual Installation of <application>PL/pgSQL</application></title>
|
2001-02-04 16:28:18 +01:00
|
|
|
|
2000-03-31 00:13:30 +02:00
|
|
|
<para>
|
2002-01-07 03:29:15 +01:00
|
|
|
The following command tells the database server where to find the
|
2001-09-10 23:58:47 +02:00
|
|
|
shared object for the <application>PL/pgSQL</application> language's call handler function.
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2001-02-04 16:28:18 +01:00
|
|
|
<programlisting>
|
2003-04-07 03:29:26 +02:00
|
|
|
CREATE FUNCTION plpgsql_call_handler() RETURNS language_handler AS
|
2001-08-13 23:34:54 +02:00
|
|
|
'$libdir/plpgsql' LANGUAGE C;
|
2001-02-04 16:28:18 +01:00
|
|
|
</programlisting>
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
1998-10-21 07:30:16 +02:00
|
|
|
|
2001-05-19 11:01:10 +02:00
|
|
|
<para>
|
2000-03-31 00:13:30 +02:00
|
|
|
The command
|
2001-02-04 16:28:18 +01:00
|
|
|
<programlisting>
|
2001-08-13 23:34:54 +02:00
|
|
|
CREATE TRUSTED PROCEDURAL LANGUAGE plpgsql
|
|
|
|
HANDLER plpgsql_call_handler;
|
2001-02-04 16:28:18 +01:00
|
|
|
</programlisting>
|
|
|
|
then defines that the previously declared call handler function
|
|
|
|
should be invoked for functions and trigger procedures where the
|
2001-09-10 23:58:47 +02:00
|
|
|
language attribute is <literal>plpgsql</literal>.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
2002-01-07 03:29:15 +01:00
|
|
|
</example>
|
2001-02-04 16:28:18 +01:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<para>
|
|
|
|
In a default <productname>PostgreSQL</productname> installation,
|
|
|
|
the handler for the <application>PL/pgSQL</application> language
|
|
|
|
is built and installed into the <quote>library</quote>
|
|
|
|
directory. If <application>Tcl/Tk</> support is configured in, the handlers for
|
|
|
|
<application>PL/Tcl</> and <application>PL/TclU</> are also built and installed in the same
|
|
|
|
location. Likewise, the <application>PL/Perl</> and <application>PL/PerlU</> handlers are built
|
2003-06-30 20:31:42 +02:00
|
|
|
and installed if Perl support is configured, and <application>PL/PythonU</> is
|
2003-04-07 03:29:26 +02:00
|
|
|
installed if Python support is configured.
|
|
|
|
</para>
|
|
|
|
|
2000-03-31 00:13:30 +02:00
|
|
|
</sect1>
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
</chapter>
|
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
2000-03-31 00:13:30 +02:00
|
|
|
mode:sgml
|
1999-07-22 17:11:05 +02:00
|
|
|
sgml-omittag:nil
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"./reference.ced"
|
|
|
|
sgml-exposed-tags:nil
|
2000-03-31 05:27:42 +02:00
|
|
|
sgml-local-catalogs:("/usr/lib/sgml/catalog")
|
1999-07-22 17:11:05 +02:00
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|