<!-- doc/src/sgml/xplang.sgml -->
<chapter id="xplang">
<title>Procedural Languages</title>
<indexterm zone="xplang">
<primary>procedural language</primary>
</indexterm>
<para>
<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</acronym>s). For a function
written in a procedural language, the database server has
no built-in knowledge about how to interpret the function's source
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
could serve as <quote>glue</quote> between
<productname>PostgreSQL</productname> and an existing implementation
of a programming language. The handler itself is a
C language function compiled into a shared object and
loaded on demand, just like any other C function.
</para>
<para>
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"/>).
There are additional procedural languages available that are not
included in the core distribution. <xref linkend="external-projects"/>
has information about finding them. In addition other languages can
be defined by users; the basics of developing a new procedural
language are covered in <xref linkend="plhandler"/>.
</para>
<sect1 id="xplang-install">
<title>Installing Procedural Languages</title>
<para>
A procedural language must be <quote>installed</quote> into each
database where it is to be used. But procedural languages installed in
the database <literal>template1</literal> are automatically available in all
subsequently created databases, since their entries in
<literal>template1</literal> will be copied by <command>CREATE DATABASE</command>.
So the database administrator can
decide which languages are available in which databases and can make
some languages available by default if desired.
</para>
<para>
For the languages supplied with the standard distribution, it is
only necessary to execute <command>CREATE EXTENSION</command>
<replaceable>language_name</replaceable> to install the language into the
current database.
The manual procedure described below is only recommended for
installing languages that have not been packaged as extensions.
</para>
<procedure>
<title>Manual Procedural Language Installation</title>
<para>
A procedural language is installed in a database in five steps,
which must be carried out by a database superuser. In most cases
the required SQL commands should be packaged as the installation script
of an <quote>extension</quote>, so that <command>CREATE EXTENSION</command> can be
used to execute them.
</para>
<step performance="required" id="xplang-install-cr1">
<para>
The shared object for the language handler must be compiled and
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"/>. 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.
</para>
</step>
<step performance="required" id="xplang-install-cr2">
<para>
The handler must be declared with the command
<synopsis>
CREATE FUNCTION <replaceable>handler_function_name</replaceable>()