2006-09-16 02:30:20 +02:00
|
|
|
<!-- $PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.31 2006/09/16 00:30:16 momjian 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>
|
2004-12-30 22:45:37 +01:00
|
|
|
<productname>PostgreSQL</productname> allows user-defined functions
|
|
|
|
to be written in other languages besides SQL and C. These other
|
|
|
|
languages are generically called <firstterm>procedural
|
|
|
|
languages</firstterm> (<acronym>PL</>s). For a function
|
|
|
|
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
|
2004-12-30 22:45:37 +01:00
|
|
|
of a programming language. The handler itself is a
|
2003-04-07 03:29:26 +02:00
|
|
|
C language function compiled into a shared object and
|
2004-12-30 22:45:37 +01:00
|
|
|
loaded on demand, just like any other C function.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
|
2000-04-03 00:59:40 +02:00
|
|
|
<para>
|
2004-12-30 22:45:37 +01:00
|
|
|
There are currently four procedural languages available in the
|
|
|
|
standard <productname>PostgreSQL</productname> distribution:
|
|
|
|
<application>PL/pgSQL</application> (<xref linkend="plpgsql">),
|
|
|
|
<application>PL/Tcl</application> (<xref linkend="pltcl">),
|
|
|
|
<application>PL/Perl</application> (<xref linkend="plperl">), and
|
|
|
|
<application>PL/Python</application> (<xref linkend="plpython">).
|
|
|
|
Other languages can be defined by users.
|
|
|
|
The basics of developing a new procedural language are covered in <xref
|
|
|
|
linkend="plhandler">.
|
2000-04-03 00:59:40 +02:00
|
|
|
</para>
|
|
|
|
|
2004-12-30 04:13:56 +01:00
|
|
|
<para>
|
|
|
|
There are additional procedural languages available that are not
|
|
|
|
included in the core distribution. <xref linkend="external-projects">
|
|
|
|
has information about finding them.
|
|
|
|
</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
|
2004-12-30 22:45:37 +01:00
|
|
|
subsequently created databases, since their entries in
|
|
|
|
<literal>template1</> will be copied by <command>CREATE DATABASE</>.
|
|
|
|
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>
|
2005-09-06 01:50:49 +02:00
|
|
|
For the languages supplied with the standard distribution, it is
|
|
|
|
only necessary to execute <command>CREATE LANGUAGE</>
|
|
|
|
<replaceable>language_name</> to install the language into the
|
|
|
|
current database. Alternatively, the program <xref
|
|
|
|
linkend="app-createlang"> may be used to do this from the shell
|
|
|
|
command line. For example, to install the language
|
2003-04-07 03:29:26 +02:00
|
|
|
<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
|
2005-09-06 01:50:49 +02:00
|
|
|
installing custom languages that <command>CREATE LANGUAGE</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>
|
2004-12-30 22:45:37 +01:00
|
|
|
A procedural language is installed in a database in four steps,
|
2005-09-06 01:50:49 +02:00
|
|
|
which must be carried out by a database superuser. (For languages
|
|
|
|
known to <command>CREATE LANGUAGE</>, the second and third steps
|
|
|
|
can be omitted, because they will be carried out automatically
|
|
|
|
if needed.)
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1999-07-22 17:11:05 +02:00
|
|
|
|
2004-12-30 22:45:37 +01:00
|
|
|
<step performance="required" id="xplang-install-cr1">
|
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
|
2004-12-30 22:45:37 +01:00
|
|
|
functions does; see <xref linkend="dfunc">. Often, the language
|
|
|
|
handler will depend on an external library that provides the actual
|
|
|
|
programming language engine; if so, that must be installed as well.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</step>
|
2001-02-04 16:28:18 +01:00
|
|
|
|
2004-12-30 22:45:37 +01:00
|
|
|
<step performance="required" id="xplang-install-cr2">
|
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
|
|
|
|
2004-12-30 22:45:37 +01:00
|
|
|
<step performance="optional" id="xplang-install-cr3">
|
|
|
|
<para>
|
|
|
|
Optionally, the language handler may provide a <quote>validator</>
|
|
|
|
function that checks a function definition for correctness without
|
|
|
|
actually executing it. The validator function is called by
|
|
|
|
<command>CREATE FUNCTION</> if it exists. If a validator function
|
|
|
|
is provided by the handler, declare it with a command like
|
|
|
|
<synopsis>
|
|
|
|
CREATE FUNCTION <replaceable>validator_function_name</replaceable>(oid)
|
|
|
|
RETURNS void
|
|
|
|
AS '<replaceable>path-to-shared-object</replaceable>'
|
|
|
|
LANGUAGE C;
|
|
|
|
</synopsis>
|
|
|
|
</para>
|
|
|
|
</step>
|
|
|
|
|
|
|
|
<step performance="required" id="xplang-install-cr4">
|
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>
|
2004-12-30 22:45:37 +01:00
|
|
|
HANDLER <replaceable>handler_function_name</replaceable>
|
|
|
|
<optional>VALIDATOR <replaceable>validator_function_name</replaceable></optional> ;
|
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
|
|
|
|
2004-12-30 22:45:37 +01:00
|
|
|
<para>
|
|
|
|
<application>PL/pgSQL</application> has a validator function,
|
|
|
|
so we declare that too:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE FUNCTION plpgsql_validator(oid) RETURNS void AS
|
|
|
|
'$libdir/plpgsql' LANGUAGE C;
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
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
|
2004-12-30 22:45:37 +01:00
|
|
|
HANDLER plpgsql_call_handler
|
|
|
|
VALIDATOR plpgsql_validator;
|
2001-02-04 16:28:18 +01:00
|
|
|
</programlisting>
|
2004-12-30 22:45:37 +01:00
|
|
|
then defines that the previously declared functions
|
2001-02-04 16:28:18 +01:00
|
|
|
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>
|
2005-09-06 01:50:49 +02:00
|
|
|
directory. If <application>Tcl</> 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 and installed if Perl support
|
|
|
|
is configured, and the <application>PL/PythonU</> handler 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>
|