<!--
$Header: /cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.24 2003/09/20 20:12:05 tgl Exp $
-->

 <chapter id="xplang">
  <title id="xplang-title">Procedural Languages</title>

  <indexterm zone="xplang">
   <primary>procedural language</primary>
  </indexterm>

  <para>
   <productname>PostgreSQL</productname> allows users to add new
   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
   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 special
   C language function compiled into a shared object and
   loaded on demand.
  </para>

  <para>
   Writing a handler for a new procedural language is described in
   <xref linkend="xfunc-plhandler">.  Several procedural languages are
   available in the standard <productname>PostgreSQL</productname>
   distribution, which can serve as examples.
  </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</> are automatically available in all
    subsequently created databases. So the database administrator can
    decide which languages are available in which databases and can make
    some languages available by default if he chooses.
   </para>

   <para>
    For the languages supplied with the standard distribution, the
    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
<programlisting>
createlang plpgsql template1
</programlisting>
    The manual procedure described below is only recommended for
    installing custom languages that <command>createlang</command>
    does not know about.
   </para>

   <procedure>
    <title>
     Manual Procedural Language Installation
    </title>

    <para>
     A procedural language is installed in a database in three steps,
     which must be carried out by a database superuser.  The
     <command>createlang</command> program automates <xref
     linkend="xplang-install-cr1"> and <xref
     linkend="xplang-install-cr2">.
    </para>

    <step performance="required">
     <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">.
     </para>
    </step>

    <step performance="required" id="xplang-install-cr1">
     <para>
      The handler must be declared with the command
<synopsis>
CREATE FUNCTION <replaceable>handler_function_name</replaceable>()
    RETURNS language_handler
    AS '<replaceable>path-to-shared-object</replaceable>'
    LANGUAGE C;
</synopsis>
      The special return type of <type>language_handler</type> tells
      the database system that this function does not return one of
      the defined <acronym>SQL</acronym> data types and is not directly usable
      in <acronym>SQL</acronym> statements.
     </para>
    </step>

    <step performance="required" id="xplang-install-cr2">
     <para>
      The PL must be declared with the command
<synopsis>
CREATE <optional>TRUSTED</optional> <optional>PROCEDURAL</optional> LANGUAGE <replaceable>language-name</replaceable>
    HANDLER <replaceable>handler_function_name</replaceable>;
</synopsis>
      The optional key word <literal>TRUSTED</literal> specifies that
      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>,
      <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.
     </para>
    </step>
   </procedure>

   <para>
    <xref linkend="xplang-install-example"> shows how the manual
    installation procedure would work with the language
    <application>PL/pgSQL</application>.
   </para>

   <example id="xplang-install-example">
    <title>Manual Installation of <application>PL/pgSQL</application></title>

     <para>
      The following command tells the database server where to find the 
      shared object for the <application>PL/pgSQL</application> language's call handler function.

<programlisting>
CREATE FUNCTION plpgsql_call_handler() RETURNS language_handler AS
    '$libdir/plpgsql' LANGUAGE C;
</programlisting>
     </para>

     <para>
      The command
<programlisting>
CREATE TRUSTED PROCEDURAL LANGUAGE plpgsql
    HANDLER plpgsql_call_handler;
</programlisting>
      then defines that the previously declared call handler function
      should be invoked for functions and trigger procedures where the
      language attribute is <literal>plpgsql</literal>.
     </para>
  </example>

   <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
    and installed if Perl support is configured, and <application>PL/PythonU</> is
    installed if Python support is configured.
   </para>

  </sect1>

</chapter>

<!-- Keep this comment at the end of the file
Local variables:
mode:sgml
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
sgml-local-catalogs:("/usr/lib/sgml/catalog")
sgml-local-ecat-files:nil
End:
-->
