head	1.38;
access;
symbols
	REL9_0_0:1.38
	REL9_1_ALPHA1:1.38
	REL9_0_RC1:1.38
	REL9_0_BETA4:1.38
	REL9_0_STABLE:1.38.0.2
	REL9_0_BETA3:1.38
	REL9_0_BETA2:1.38
	REL7_4_29:1.25
	REL8_0_25:1.28
	REL8_1_21:1.29
	REL8_2_17:1.32
	REL8_3_11:1.34
	REL8_4_4:1.34
	REL9_0_BETA1:1.37
	REL9_0_ALPHA5_BRANCH:1.36.0.6
	REL9_0_ALPHA5:1.36
	REL7_4_28:1.25
	REL8_0_24:1.28
	REL8_1_20:1.29
	REL8_2_16:1.32
	REL8_3_10:1.34
	REL8_4_3:1.34
	REL9_0_ALPHA4:1.36
	REL9_0_ALPHA4_BRANCH:1.36.0.4
	REL8_5_ALPHA3:1.36
	REL8_5_ALPHA3_BRANCH:1.36.0.2
	REL7_4_27:1.25
	REL8_0_23:1.28
	REL8_1_19:1.29
	REL8_2_15:1.32
	REL8_3_9:1.34
	REL8_4_2:1.34
	REL8_5_ALPHA2:1.35
	REL8_5_ALPHA2_BRANCH:1.35.0.2
	REL7_4_26:1.25
	REL8_0_22:1.28
	REL8_1_18:1.29
	REL8_2_14:1.32
	REL8_3_8:1.34
	REL8_4_1:1.34
	REL8_5_ALPHA1:1.34
	REL8_5_ALPHA1_BRANCH:1.34.0.6
	REL8_4_STABLE:1.34.0.4
	REL8_4_0:1.34
	REL8_4_RC2:1.34
	REL8_4_RC1:1.34
	REL8_4_BETA2:1.34
	REL8_4_BETA1:1.34
	REL7_4_25:1.25
	REL8_0_21:1.28
	REL8_1_17:1.29
	REL8_2_13:1.32
	REL8_3_7:1.34
	REL7_4_24:1.25
	REL8_0_20:1.28
	REL8_1_16:1.29
	REL8_2_12:1.32
	REL8_3_6:1.34
	REL7_4_23:1.25
	REL8_0_19:1.28
	REL8_1_15:1.29
	REL8_2_11:1.32
	REL8_3_5:1.34
	REL7_4_22:1.25
	REL8_0_18:1.28
	REL8_1_14:1.29
	REL8_2_10:1.32
	REL8_3_4:1.34
	REL7_4_21:1.25
	REL8_0_17:1.28
	REL8_1_13:1.29
	REL8_2_9:1.32
	REL8_3_3:1.34
	REL7_4_20:1.25
	REL8_0_16:1.28
	REL8_1_12:1.29
	REL8_2_8:1.32
	REL8_3_2:1.34
	REL8_2_7:1.32
	REL8_3_1:1.34
	REL8_3_STABLE:1.34.0.2
	REL8_3_0:1.34
	REL8_3_RC2:1.34
	REL7_3_21:1.19
	REL7_4_19:1.25
	REL8_0_15:1.28
	REL8_1_11:1.29
	REL8_2_6:1.32
	REL8_3_RC1:1.34
	REL8_3_BETA4:1.34
	REL8_3_BETA3:1.34
	REL8_3_BETA2:1.34
	REL8_3_BETA1:1.34
	REL7_3_20:1.19
	REL7_4_18:1.25
	REL8_0_14:1.28
	REL8_1_10:1.29
	REL8_2_5:1.32
	REL7_3_19:1.19
	REL7_4_17:1.25
	REL8_0_13:1.28
	REL8_1_9:1.29
	REL8_2_4:1.32
	REL8_0_12:1.28
	REL8_1_8:1.29
	REL8_2_3:1.32
	REL7_3_18:1.19
	REL7_4_16:1.25
	REL8_0_11:1.28
	REL8_1_7:1.29
	REL8_2_2:1.32
	REL8_0_10:1.28
	REL8_1_6:1.29
	REL8_2_1:1.32
	REL7_4_15:1.25
	REL7_3_17:1.19
	REL8_2_STABLE:1.32.0.2
	REL8_2_0:1.32
	REL8_2_RC1:1.32
	REL8_2_BETA3:1.31
	REL8_2_BETA2:1.31
	REL8_1_5:1.29
	REL8_0_9:1.28
	REL7_4_14:1.25
	REL7_3_16:1.19
	REL8_2_BETA1:1.31
	REL7_3_15:1.19
	REL7_4_13:1.25
	REL8_0_8:1.28
	REL8_1_4:1.29
	REL7_3_14:1.19
	REL7_4_12:1.25
	REL8_0_7:1.28
	REL8_1_3:1.29
	REL7_3_13:1.19
	REL7_4_11:1.25
	REL8_0_6:1.28
	REL8_1_2:1.29
	REL7_3_12:1.19
	REL7_4_10:1.25
	REL8_0_5:1.28
	REL8_1_1:1.29
	REL8_1_STABLE:1.29.0.2
	REL8_1_0:1.29
	REL8_1_0RC1:1.29
	REL8_1_0BETA4:1.29
	REL8_1_0BETA3:1.29
	REL7_3_11:1.19
	REL7_4_9:1.25
	REL8_0_4:1.28
	REL8_1_0BETA2:1.29
	REL8_1_0BETA1:1.28
	REL7_2_8:1.17
	REL7_3_10:1.19
	REL7_4_8:1.25
	REL8_0_3:1.28
	REL8_0_2:1.28
	REL7_2_7:1.17
	REL7_3_9:1.19
	REL7_4_7:1.25
	REL8_0_1:1.28
	REL8_0_STABLE:1.28.0.4
	REL8_0_0:1.28.0.2
	REL8_0_0RC5:1.28
	REL8_0_0RC4:1.28
	REL8_0_0RC3:1.28
	REL8_0_0RC2:1.26
	REL8_0_0RC1:1.26
	REL8_0_0BETA5:1.26
	REL8_0_0BETA4:1.26
	REL7_4_6:1.25
	REL7_3_8:1.19
	REL7_2_6:1.17
	REL8_0_0BETA3:1.26
	REL8_0_0BETA2:1.26
	REL7_2_5:1.17
	REL7_4_5:1.25
	REL7_3_7:1.19
	REL7_4_4:1.25
	REL8_0_0BETA1:1.26
	REL7_4_3:1.25
	REL7_4_2:1.25
	REL7_3_6:1.19
	REL7_4_1:1.25
	REL7_3_5:1.19
	REL7_4:1.25
	REL7_4_RC2:1.25
	REL7_4_STABLE:1.25.0.2
	REL7_4_RC1:1.25
	REL7_4_BETA5:1.24
	REL7_4_BETA4:1.24
	REL7_4_BETA3:1.23
	REL7_4_BETA2:1.22
	WIN32_DEV:1.22.0.2
	REL7_4_BETA1:1.22
	REL7_3_4:1.19
	REL7_3_2:1.19
	REL7_2_4:1.17
	REL7_3_STABLE:1.19.0.2
	REL7_2_3:1.17
	REL7_2_STABLE:1.17.0.2
	REL7_2:1.17
	REL7_2_RC2:1.17
	REL7_2_RC1:1.17
	REL7_2_BETA5:1.17
	REL7_2_BETA4:1.16
	REL7_2_BETA3:1.15
	REL7_2_BETA2:1.15
	REL7_2_BETA1:1.15
	REL7_1_2:1.12
	REL7_1_STABLE:1.12.0.2
	REL7_1_BETA:1.10
	REL7_1_BETA3:1.10
	REL7_1_BETA2:1.10
	REL7_1:1.12
	REL7_0_PATCHES:1.8.0.2
	REL7_0:1.8
	REL6_5_PATCHES:1.4.0.2
	REL6_5:1.4
	REL6_4:1.3.0.2;
locks; strict;
comment	@# @;


1.38
date	2010.05.30.02.23.09;	author momjian;	state Exp;
branches;
next	1.37;

1.37
date	2010.04.03.07.22.56;	author petere;	state Exp;
branches;
next	1.36;

1.36
date	2009.12.19.01.49.02;	author tgl;	state Exp;
branches;
next	1.35;

1.35
date	2009.09.22.23.43.37;	author tgl;	state Exp;
branches;
next	1.34;

1.34
date	2007.02.01.00.28.18;	author momjian;	state Exp;
branches;
next	1.33;

1.33
date	2007.01.31.20.56.20;	author momjian;	state Exp;
branches;
next	1.32;

1.32
date	2006.11.20.17.42.16;	author neilc;	state Exp;
branches;
next	1.31;

1.31
date	2006.09.16.00.30.16;	author momjian;	state Exp;
branches;
next	1.30;

1.30
date	2006.03.10.19.10.50;	author momjian;	state Exp;
branches;
next	1.29;

1.29
date	2005.09.05.23.50.48;	author tgl;	state Exp;
branches;
next	1.28;

1.28
date	2004.12.30.21.45.37;	author tgl;	state Exp;
branches;
next	1.27;

1.27
date	2004.12.30.03.13.56;	author tgl;	state Exp;
branches;
next	1.26;

1.26
date	2003.11.29.19.51.38;	author pgsql;	state Exp;
branches;
next	1.25;

1.25
date	2003.10.22.22.28.10;	author tgl;	state Exp;
branches;
next	1.24;

1.24
date	2003.09.20.20.12.05;	author tgl;	state Exp;
branches;
next	1.23;

1.23
date	2003.08.31.17.32.21;	author petere;	state Exp;
branches;
next	1.22;

1.22
date	2003.06.30.18.31.42;	author tgl;	state Exp;
branches
	1.22.2.1;
next	1.21;

1.21
date	2003.04.07.01.29.26;	author petere;	state Exp;
branches;
next	1.20;

1.20
date	2003.01.31.00.10.51;	author tgl;	state Exp;
branches;
next	1.19;

1.19
date	2002.09.21.18.32.54;	author petere;	state Exp;
branches;
next	1.18;

1.18
date	2002.08.22.00.01.40;	author tgl;	state Exp;
branches;
next	1.17;

1.17
date	2002.01.07.02.29.14;	author petere;	state Exp;
branches;
next	1.16;

1.16
date	2001.11.21.06.09.45;	author thomas;	state Exp;
branches;
next	1.15;

1.15
date	2001.09.10.21.58.47;	author petere;	state Exp;
branches;
next	1.14;

1.14
date	2001.08.13.21.34.51;	author petere;	state Exp;
branches;
next	1.13;

1.13
date	2001.05.19.09.01.10;	author petere;	state Exp;
branches;
next	1.12;

1.12
date	2001.02.09.02.20.52;	author tgl;	state Exp;
branches;
next	1.11;

1.11
date	2001.02.04.15.28.18;	author petere;	state Exp;
branches;
next	1.10;

1.10
date	2000.11.04.21.06.37;	author momjian;	state Exp;
branches;
next	1.9;

1.9
date	2000.09.29.20.21.34;	author petere;	state Exp;
branches;
next	1.8;

1.8
date	2000.04.02.22.59.40;	author tgl;	state Exp;
branches;
next	1.7;

1.7
date	2000.03.31.03.27.42;	author thomas;	state Exp;
branches;
next	1.6;

1.6
date	2000.03.30.22.13.30;	author thomas;	state Exp;
branches;
next	1.5;

1.5
date	99.07.22.15.11.05;	author thomas;	state Exp;
branches;
next	1.4;

1.4
date	98.12.29.02.24.21;	author thomas;	state Exp;
branches;
next	1.3;

1.3
date	98.10.30.19.37.18;	author thomas;	state Exp;
branches;
next	1.2;

1.2
date	98.10.25.00.29.01;	author thomas;	state Exp;
branches;
next	1.1;

1.1
date	98.10.21.05.30.16;	author thomas;	state Exp;
branches;
next	;

1.22.2.1
date	2003.09.07.04.36.44;	author momjian;	state Exp;
branches;
next	;


desc
@@


1.38
log
@Clarify the meaning of "trusted language" in the documentation.
@
text
@<!-- $PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.37 2010/04/03 07:22:56 petere Exp $ -->

 <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</>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</> are automatically available in all
    subsequently created databases, since their entries in
    <literal>template1</> will be copied by <command>CREATE DATABASE</>.
    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, 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"> can be used to do this from the shell
    command line.  For example, to install the language
    <application>PL/Perl</application> into the database
    <literal>template1</>, use:
<programlisting>
createlang plperl template1
</programlisting>
    The manual procedure described below is only recommended for
    installing custom languages that <command>CREATE LANGUAGE</command>
    does not know about.
   </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.  (For languages
     known to <command>CREATE LANGUAGE</>, the second through fourth steps
     can be omitted, because they will be carried out automatically
     if needed.)
    </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>()
    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="optional" id="xplang-install-cr3">
     <para>
      Optionally, the language handler can provide an <quote>inline</>
      handler function that executes anonymous code blocks
      (<xref linkend="sql-do"> commands)
      written in this language.  If an inline handler function
      is provided by the language, declare it with a command like
<synopsis>
CREATE FUNCTION <replaceable>inline_function_name</replaceable>(internal)
    RETURNS void
    AS '<replaceable>path-to-shared-object</replaceable>'
    LANGUAGE C;
</synopsis>
     </para>
    </step>

    <step performance="optional" id="xplang-install-cr4">
     <para>
      Optionally, the language handler can 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 language, 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-cr5">
     <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>
    <optional>INLINE <replaceable>inline_function_name</replaceable></optional>
    <optional>VALIDATOR <replaceable>validator_function_name</replaceable></optional> ;
</synopsis>
      The optional key word <literal>TRUSTED</literal> specifies that
      the language does not grant access to data that the user would
      not otherwise have.  Trusted languages are designed for ordinary
      database users (those without superuser privilege) and allows them
      to safely create of 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/Perl</application>.
   </para>

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

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

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

     <para>
      <application>PL/Perl</application> has an inline handler function
      and a validator function, so we declare those too:

<programlisting>
CREATE FUNCTION plperl_inline_handler(internal) RETURNS void AS
    '$libdir/plperl' LANGUAGE C;

CREATE FUNCTION plperl_validator(oid) RETURNS void AS
    '$libdir/plperl' LANGUAGE C;
</programlisting>
     </para>

     <para>
      The command:
<programlisting>
CREATE TRUSTED PROCEDURAL LANGUAGE plperl
    HANDLER plperl_call_handler
    INLINE plperl_inline_handler
    VALIDATOR plperl_validator;
</programlisting>
      then defines that the previously declared functions
      should be invoked for functions and trigger procedures where the
      language attribute is <literal>plperl</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; furthermore, the <application>PL/pgSQL</application> language
    itself is installed in all databases.
    If <application>Tcl</> support is configured in, the handlers for
    <application>PL/Tcl</> and <application>PL/TclU</> are built and installed
    in the library directory, but the language itself is not installed in any
    database by default.
    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 installed if Python support is
    configured, but these languages are not installed by default.
   </para>

  </sect1>

</chapter>
@


1.37
log
@Remove unnecessary xref endterm attributes and title ids

The endterm attribute is mainly useful when the toolchain does not support
automatic link target text generation for a particular situation.  In  the
past, this was required by the man page tools for all reference page links,
but that is no longer the case, and it now actually gets in the way of
proper automatic link text generation.  The only remaining use cases are
currently xrefs to refsects.
@
text
@d1 1
a1 1
<!-- $PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.36 2009/12/19 01:49:02 tgl Exp $ -->
d154 4
a157 2
      ordinary database users that have no superuser privileges should
      be allowed to use this language to create functions and trigger
@


1.36
log
@Adjust some more places in the documentation to match the fact that
plpgsql is now installed by default.
@
text
@d1 1
a1 1
<!-- $PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.35 2009/09/22 23:43:37 tgl Exp $ -->
d4 1
a4 1
  <title id="xplang-title">Procedural Languages</title>
d116 1
a116 1
      (<xref linkend="sql-do" endterm="sql-do-title"> commands)
@


1.35
log
@Implement the DO statement to support execution of PL code without having
to create a function for it.

Procedural languages now have an additional entry point, namely a function
to execute an inline code block.  This seemed a better design than trying
to hide the transient-ness of the code from the PL.  As of this patch, only
plpgsql has an inline handler, but probably people will soon write handlers
for the other standard PLs.

In passing, remove the long-dead LANCOMPILER option of CREATE LANGUAGE.

Petr Jelinek
@
text
@d1 1
a1 1
<!-- $PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.34 2007/02/01 00:28:18 momjian Exp $ -->
d36 2
a37 2
   has information about finding them. In addition other languages can 
   be defined by users; the basics of developing a new procedural 
d62 1
a62 1
    <application>PL/pgSQL</application> into the database
d65 1
a65 1
createlang plpgsql template1
d176 1
a176 1
    <application>PL/pgSQL</application>.
d180 1
a180 1
    <title>Manual Installation of <application>PL/pgSQL</application></title>
d183 3
a185 2
      The following command tells the database server where to find the 
      shared object for the <application>PL/pgSQL</application> language's call handler function.
d188 2
a189 2
CREATE FUNCTION plpgsql_call_handler() RETURNS language_handler AS
    '$libdir/plpgsql' LANGUAGE C;
d194 1
a194 1
      <application>PL/pgSQL</application> has an inline handler function
d198 2
a199 2
CREATE FUNCTION plpgsql_inline_handler(internal) RETURNS void AS
    '$libdir/plpgsql' LANGUAGE C;
d201 2
a202 2
CREATE FUNCTION plpgsql_validator(oid) RETURNS void AS
    '$libdir/plpgsql' LANGUAGE C;
d209 4
a212 4
CREATE TRUSTED PROCEDURAL LANGUAGE plpgsql
    HANDLER plpgsql_call_handler
    INLINE plpgsql_inline_handler
    VALIDATOR plpgsql_validator;
d216 1
a216 1
      language attribute is <literal>plpgsql</literal>.
d224 10
a233 6
    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
    installed if Python support is configured.
@


1.34
log
@Consistenly use colons before '<programlisting>' blocks, where
appropriate.
@
text
@d1 1
a1 1
<!-- $PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.33 2007/01/31 20:56:20 momjian Exp $ -->
d78 1
a78 1
     A procedural language is installed in a database in four steps,
d80 1
a80 1
     known to <command>CREATE LANGUAGE</>, the second and third steps
d114 16
d134 1
a134 1
      is provided by the handler, declare it with a command like
d144 1
a144 1
    <step performance="required" id="xplang-install-cr4">
d150 1
d193 2
a194 2
      <application>PL/pgSQL</application> has a validator function,
      so we declare that too:
d197 3
d210 1
@


1.33
log
@Update documentation on may/can/might:

Standard English uses "may", "can", and "might" in different ways:

        may - permission, "You may borrow my rake."

        can - ability, "I can lift that log."

        might - possibility, "It might rain today."

Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice.  Similarly, "It may crash" is better stated, "It might crash".

Also update two error messages mentioned in the documenation to match.
@
text
@d1 1
a1 1
<!-- $PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.32 2006/11/20 17:42:16 neilc Exp $ -->
d63 1
a63 1
    <literal>template1</>, use
d186 1
a186 1
      The command
@


1.32
log
@Overhaul the "External Projects" chapter in the manual. Replace
the variabelist with a more concise table, add a URL for each project,
remove some orphaned projects, add PL/Py, and various other changes.
Initial patch from Robert Treat, subsequent work by Neil Conway.
@
text
@d1 1
a1 1
<!-- $PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.31 2006/09/16 00:30:16 momjian Exp $ -->
d60 1
a60 1
    linkend="app-createlang"> may be used to do this from the shell
d114 1
a114 1
      Optionally, the language handler may provide a <quote>validator</>
@


1.31
log
@Remove emacs info from footer of SGML files.
@
text
@d1 1
a1 1
<!-- $PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.30 2006/03/10 19:10:50 momjian Exp $ -->
a33 6
   Other languages can be defined by users.
   The basics of developing a new procedural language are covered in <xref
   linkend="plhandler">.
  </para>

  <para>
d36 3
a38 1
   has information about finding them.
@


1.30
log
@Make $PostgreSQL CVS tags consistent for SGML files.
@
text
@d1 1
a1 1
<!-- $PostgreSQL: pgsql/doc/src/sgml/advanced.sgml,v 1.49 2005/04/09 03:52:43 momjian Exp $ -->
a216 17

<!-- 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:
-->
@


1.29
log
@Implement a preliminary 'template' facility for procedural languages,
as per my recent proposal.  For now the template data is hard-wired in
proclang.c --- this should be replaced later by a new shared system
catalog, but we don't want to force initdb during 8.1 beta.  This change
lets us cleanly load existing dump files even if they contain outright
wrong information about a PL's support functions, such as a wrong path
to the shared library or a missing validator function.  Also, we can
revert the recent kluges to make pg_dump dump PL support functions that
are stored in pg_catalog.
While at it, I removed the code in pg_regress that replaced $libdir
with a hardcoded path for temporary installations.  This is no longer
needed given our support for relocatable installations.
@
text
@d1 1
a1 3
<!--
$PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.28 2004/12/30 21:45:37 tgl Exp $
-->
@


1.28
log
@More minor updates and copy-editing.
@
text
@d2 1
a2 1
$PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.27 2004/12/30 03:13:56 tgl Exp $
d62 6
a67 4
    For the languages supplied with the standard distribution, the
    program <xref linkend="app-createlang"> may be used to install the
    language instead of carrying out the details by hand.  For
    example, to install the language
d74 1
a74 1
    installing custom languages that <command>createlang</command>
d85 4
a88 3
     which must be carried out by a database superuser.  The
     <command>createlang</command> program automates all but <xref
     linkend="xplang-install-cr1">.
d208 5
a212 4
    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 <application>PL/PythonU</> is
@


1.27
log
@More updates and copy-editing.  Rearrange order of sections a little bit
to put more widely useful info before less widely useful info.
@
text
@d2 1
a2 1
$PostgreSQL: pgsql/doc/src/sgml/xplang.sgml,v 1.26 2003/11/29 19:51:38 pgsql Exp $
d13 5
a17 5
   <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
d24 1
a24 1
   of a programming language.  The handler itself is a special
d26 1
a26 1
   loaded on demand.
d30 9
a38 4
   Writing a handler for a new procedural language is described in
   <xref linkend="plhandler">.  Several procedural languages are
   available in the core <productname>PostgreSQL</productname>
   distribution, which can serve as examples.
d54 3
a56 1
    subsequently created databases. So the database administrator can
d63 1
a63 1
    program <command>createlang</command> may be used to install the
d82 1
a82 1
     A procedural language is installed in a database in three steps,
d84 2
a85 3
     <command>createlang</command> program automates <xref
     linkend="xplang-install-cr1"> and <xref
     linkend="xplang-install-cr2">.
d88 1
a88 1
    <step performance="required">
d93 3
a95 1
      functions does; see <xref linkend="dfunc">.
d99 1
a99 1
    <step performance="required" id="xplang-install-cr1">
d115 17
a131 1
    <step performance="required" id="xplang-install-cr2">
d136 2
a137 1
    HANDLER <replaceable>handler_function_name</replaceable>;
d179 10
d192 2
a193 1
    HANDLER plpgsql_call_handler;
d195 1
a195 1
      then defines that the previously declared call handler function
d205 1
a205 1
    directory. If <application>Tcl/Tk</> support is configured in, the handlers for
@


1.26
log
@
$Header: -> $PostgreSQL Changes ...
@
text
@d2 1
a2 1
$PostgreSQL: /cvsroot/pgsql-server/doc/src/sgml/xplang.sgml,v 1.25 2003/10/22 22:28:10 tgl Exp $
d32 1
a32 1
   available in the standard <productname>PostgreSQL</productname>
d36 6
@


1.25
log
@Move docs about index cost estimation functions and writing a procedural
language handler to the 'Internals' area, per my proposal of yesterday.
Clean up the trigger documentation a bit.  Push SPI chapter to the end
of its part, and reorder the Internals chapters into what seems a more
sensible order (at the moment anyway).
@
text
@d2 1
a2 1
$Header: /cvsroot/pgsql-server/doc/src/sgml/xplang.sgml,v 1.24 2003/09/20 20:12:05 tgl Exp $
@


1.24
log
@Fix a batch of speling misteaks identified by Peter's spell-checker tool.
@
text
@d2 1
a2 1
$Header: /cvsroot/pgsql-server/doc/src/sgml/xplang.sgml,v 1.23 2003/08/31 17:32:21 petere Exp $
d31 1
a31 1
   <xref linkend="xfunc-plhandler">.  Several procedural languages are
@


1.23
log
@Add/edit index entries.
@
text
@d2 1
a2 1
$Header: /cvsroot/pgsql-server/doc/src/sgml/xplang.sgml,v 1.22 2003/06/30 18:31:42 tgl Exp $
d71 1
a71 1
     <command>createlang</command> programm automates <xref
@


1.22
log
@Rename plpython to plpythonu, and update documentation to reflect its
now-untrusted status.
@
text
@d2 1
a2 1
$Header: /cvsroot/pgsql-server/doc/src/sgml/xplang.sgml,v 1.21 2003/04/07 01:29:26 petere Exp $
d7 4
@


1.22.2.1
log
@Update this branch to match CVS head, includes WIN32 improvements.
@
text
@d2 1
a2 1
$Header: /cvsroot/pgsql-server/doc/src/sgml/xplang.sgml,v 1.23 2003/08/31 17:32:21 petere Exp $
a6 4

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


1.21
log
@General editing
@
text
@d2 1
a2 1
$Header: /cvsroot/pgsql-server/doc/src/sgml/xplang.sgml,v 1.20 2003/01/31 00:10:51 tgl Exp $
d112 8
a119 7
      <application>PL/Tcl</application>,
      <application>PL/Perl</application>, and
      <application>PL/Python</application> are known to be trusted;
      the languages <application>PL/TclU</application> and
      <application>PL/PerlU</application> are designed to provide
      unlimited functionality and should <emphasis>not</emphasis> be
      marked trusted.
d162 1
a162 1
    and installed if Perl support is configured, and <application>PL/Python</> is
@


1.20
log
@Fix typo, per Neil Conway.
@
text
@d2 1
a2 1
$Header: /cvsroot/pgsql-server/doc/src/sgml/xplang.sgml,v 1.19 2002/09/21 18:32:54 petere Exp $
a7 3
  <sect1 id="xplang-intro">
   <title>Introduction</title>

d21 1
a21 1
   programming language function compiled into a shared object and
a30 1
  </sect1>
d38 1
a38 1
    the template1 database are automatically available in all
d40 1
a40 1
    decide which languages are available in which databases, and can make
d46 5
a50 3
    shell script <filename>createlang</filename> may be used instead
    of carrying out the details by hand.  For example, to install <application>PL/pgSQL</application>
    into the template1 database, use
d55 1
a55 1
    installing custom languages that <filename>createlang</filename>
d65 5
a69 2
     A procedural language is installed in the database in three
     steps, which must be carried out by a database superuser.
d85 4
a88 3
CREATE FUNCTION <replaceable>handler_function_name</replaceable> ()
    RETURNS LANGUAGE_HANDLER AS
    '<replaceable>path-to-shared-object</replaceable>' LANGUAGE C;
d90 2
a91 2
      The special return type of <type>LANGUAGE_HANDLER</type> tells
      the database that this function does not return one of
d104 1
a104 1
      The optional key word <literal>TRUSTED</literal> tells whether
d124 3
a126 11
    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/Python</> is
    installed if Python support is configured.  The
    <filename>createlang</filename> script automates <xref
    linkend="xplang-install-cr1"> and <xref
    linkend="xplang-install-cr2"> described above.
d129 1
a129 1
   <example>
d137 1
a137 1
CREATE FUNCTION plpgsql_call_handler () RETURNS LANGUAGE_HANDLER AS
d153 11
@


1.19
log
@Add more appropriate markup.
@
text
@d2 1
a2 1
$Header: /cvsroot/pgsql-server/doc/src/sgml/xplang.sgml,v 1.18 2002/08/22 00:01:40 tgl Exp $
d115 1
a115 1
      unlimited functionality should <emphasis>not</emphasis> be
@


1.18
log
@Add a bunch of pseudo-types to replace the behavior formerly associated
with OPAQUE, as per recent pghackers discussion.  I still want to do some
more work on the 'cstring' pseudo-type, but I'm going to commit the bulk
of the changes now before the tree starts shifting under me ...
@
text
@d2 1
a2 1
$Header: /cvsroot/pgsql-server/doc/src/sgml/xplang.sgml,v 1.17 2002/01/07 02:29:14 petere Exp $
d125 4
a128 4
    directory. If Tcl/Tk support is configured in, the handlers for
    PL/Tcl and PL/TclU are also built and installed in the same
    location.  Likewise, the PL/Perl and PL/PerlU handlers are built
    and installed if Perl support is configured, and PL/Python is
@


1.17
log
@Editorial review
@
text
@d2 1
a2 1
$Header: /cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.16 2001/11/21 06:09:45 thomas Exp $
d85 1
a85 1
    RETURNS OPAQUE AS
d88 1
a88 1
      The special return type of <type>OPAQUE</type> tells
d143 1
a143 1
CREATE FUNCTION plpgsql_call_handler () RETURNS OPAQUE AS
@


1.16
log
@Deprecate 'current' date/time constant.
Purge "Postgres" in favor of "PostgreSQL" in docs. ref/ not yet done.
@
text
@d2 1
a2 1
$Header: /cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.15 2001/09/10 21:58:47 petere Exp $
d8 3
d29 4
a32 4
   Writing a handler for a new procedural language is outside the
   scope of this manual, although some information is provided in
   the CREATE LANGUAGE reference page.  Several procedural languages are
   available in the standard <productname>PostgreSQL</productname> distribution.
d34 1
d80 1
a80 1
    <step performance="required">
d95 1
a95 1
    <step performance="required">
d102 15
a116 10
      The optional key word <token>TRUSTED</token> tells
      whether 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 backend, the <acronym>TRUSTED</acronym>
      flag should only be given for
      languages that do not allow access to database backends
      internals or the file system. The languages <application>PL/pgSQL</application>,
      <application>PL/Tcl</application>, and <application>PL/Perl</application> are known to be trusted; the language <application>PL/TclU</application>
      should <emphasis>not</emphasis> be marked trusted.
d122 11
a132 7
    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 Tcl/Tk support is configured
    in, the handlers for PL/Tcl and PL/TclU are also built and installed in
    the same location.  Likewise, the PL/Perl handler is built and installed
    if Perl support is configured.  The <filename>createlang</filename>
    script automates the two CREATE steps described above.
d135 2
a136 2
   <procedure>
    <title>Example</title>
a137 1
    <step performance="required">
d139 1
a139 1
      The following command tells the database where to find the 
a146 1
    </step>
a147 1
    <step performance="Required">
d158 1
a158 2
    </step>
   </procedure>
@


1.15
log
@Markup and spell-check run over Programmer's Guide (rather incomplete still).
@
text
@d2 1
a2 1
$Header: /home/projects/pgsql/cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.14 2001/08/13 21:34:51 petere Exp $
d9 1
a9 1
   <productname>Postgres</productname> allows users to add new
d19 1
a19 1
   <productname>Postgres</productname> and an existing implementation
d29 1
a29 1
   available in the standard <productname>Postgres</productname> distribution.
d113 1
a113 1
    In a default <productname>Postgres</productname> installation, the
@


1.14
log
@Make LANCOMPILER clause in CREATE LANGUAGE optional.  Allow "identifier"
syntax for language names (instead of 'string').

createlang now handles the case where a second language uses the same call
handler as an already installed language (e.g., plperl/plperlu).

droplang now handles the reverse case, i.e., dropping a language where
the call handler is still used by another language.  Moreover, droplang
can now be used to drop any user-defined language, not just the supplied
ones.
@
text
@d2 1
a2 1
$Header: /home/projects/pgsql/cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.13 2001/05/19 09:01:10 petere Exp $
d47 1
a47 1
    of carrying out the details by hand.  For example, to install PL/pgSQL
d105 2
a106 2
      internals or the filesystem. The languages PL/pgSQL,
      PL/Tcl, and PL/Perl are known to be trusted; the language PL/TclU
d114 1
a114 1
    handler for the PL/pgSQL language is built and installed into the
d128 1
a128 1
      shared object for the PL/pgSQL language's call handler function.
d146 1
a146 1
      language attribute is 'plpgsql'.
@


1.13
log
@Allow special '$libdir' macro to show up in object file path in CREATE
FUNCTION command.  Guard against trying to load a directory.  Update
documentation some.
@
text
@d2 1
a2 1
$Header: /home/projects/pgsql/cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.12 2001/02/09 02:20:52 tgl Exp $
d82 1
a82 1
    '<replaceable>path-to-shared-object</replaceable>' LANGUAGE 'C';
d95 2
a96 3
CREATE <optional>TRUSTED</optional> <optional>PROCEDURAL</optional> LANGUAGE '<replaceable>language-name</replaceable>'
    HANDLER <replaceable>handler_function_name</replaceable>
    LANCOMPILER '<replaceable>description</replaceable>';
d132 1
a132 1
    '$libdir/plpgsql' LANGUAGE 'C';
d141 2
a142 3
CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
    HANDLER plpgsql_call_handler
    LANCOMPILER 'PL/pgSQL';
@


1.12
log
@Restructure discussion of PL installation to emphasize createlang as the
recommended install procedure, rather than mentioning it as an afterthought.
@
text
@d2 1
a2 1
$Header: /home/projects/pgsql/cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.11 2001/02/04 15:28:18 petere Exp $
d133 1
a133 1
    '/usr/local/pgsql/lib/plpgsql.so' LANGUAGE 'C';
d139 1
a139 1
    <para>
@


1.11
log
@Move PL docs to programmer's guide, "storage" chapter to admin guide,
clean up some things in the affected areas.
@
text
@d2 1
a2 1
$Header: /home/projects/pgsql/cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.10 2000/11/04 21:06:37 momjian Exp $
d18 1
a18 1
   could serve as a <quote>glue</quote> between
d27 3
a29 2
   scope of this manual.  Several procedural languages are available
   in the <productname>Postgres</productname> distribution.
d35 22
d59 1
a59 1
     Procedural Language Installation
d64 1
a64 5
     steps.  A procedural language must be installed into each
     database where it is to be used.  Procedural languages defined in
     the template1 database are automatically available in all
     subsequently created databases. So the administrator can decide
     which languages are available by default.
d70 3
a72 3
      installed.  This works in the same way as building and
      installing modules with regular user-defined C functions does;
      see <xref linkend="dfunc">.
d106 3
a108 2
      internals or the filesystem. The languages PL/pgSQL and
      PL/Tcl are known to be trusted.
d115 1
a115 1
    handler for the PL/pgSQL is built and installed into the
d117 4
a120 2
    in, the handler for PL/Tcl is also built and installed in the same
    location.
a151 25

   <para>
    For the languages supplied with the standard distribution, the
    shell script <filename>createlang</filename> can be used instead
    of carrying out the details manually.  To install PL/pgSQL into
    the template1 database, use
<programlisting>
createlang plpgsql template1
</programlisting>
   </para>

     <para>
      PL handler functions have a special call interface that is
      different from regular C language functions. One of the arguments
      given to the handler is the object ID in the <filename>pg_proc</filename>
      tables entry for the function that should be executed.
      The handler examines various system catalogs to analyze the
      functions call arguments and it's return data type. The source
      text of the functions body is found in the prosrc attribute of
      <literal>pg_proc</literal>.
      Due to this, PL functions
      can be overloaded like SQL language functions. There can be
      multiple different PL functions having the same function name,
      as long as the call arguments differ.
     </para>
@


1.10
log
@more procedural doc cleanup
@
text
@d2 1
a2 1
$Header: /home/projects/pgsql/cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.9 2000/09/29 20:21:34 petere Exp $
d9 5
a13 4
   <productname>Postgres</productname> supports
   the definition of procedural languages.
   In the case of a function or trigger
   procedure defined in a procedural language, the database has
d15 8
a22 5
   text. Instead, the task is passed to
   a handler that knows the details of the language. The
   handler itself is a special programming language function
   compiled into a shared object
   and loaded on demand.
d26 3
a28 2
   Writing a handler for a new procedural language (PL)
   is outside the scope of this manual. 
d40 6
a45 4
     A procedural language is installed in the database in three steps.
     (For the languages supplied with the standard distribution, the
     shell script <filename>createlang</filename> can be used instead
     of carrying out the details manually.)
d48 1
a48 1
    <step performance="Required">
d50 4
a53 6
      The shared object for the language handler
      must be compiled and installed. By default the
      handler for PL/pgSQL is built and installed into the
      database library directory. If Tcl/Tk support is
      configured in, the handler for PL/Tcl is also built
      and installed in the same location.
d56 2
a57 1
    <step performance="Required">
d60 1
a60 1
      <programlisting>
d63 3
a65 3
    '<filename>path-to-shared-object</filename>' LANGUAGE 'C';
      </programlisting>
      The special return type of <acronym>OPAQUE</acronym> tells
d67 1
a67 1
      the defined <acronym>SQL</acronym> datatypes and is not directly usable
d71 2
a72 1
    <step performance="Required">
d75 2
a76 2
      <programlisting>
CREATE [ TRUSTED ] [ PROCEDURAL ] LANGUAGE '<replaceable>language-name</replaceable>'
d79 2
a80 2
      </programlisting>
      The optional keyword <acronym>TRUSTED</acronym> tells
d86 1
a86 1
      languages that don't allow access to database backends
d93 8
d103 2
a104 1
    <step performance="Required">
d109 1
a109 1
      <programlisting>
d112 1
a112 1
      </programlisting>
d119 1
a119 1
      <programlisting>
d123 4
a126 1
      </programlisting>
d128 12
a141 5
      then defines that the previously declared call handler
      function should be invoked for functions and trigger procedures
      where the language attribute is 'plpgsql'.
     </para>
     <para>
d155 1
a155 8
     <para>
      Procedural languages defined in the <filename>template1</filename>
      database are automatically defined in all subsequently created
      databases. So the database administrator can decide which
      languages are available by default.
     </para>
    </step>
   </procedure>
a157 1
 <!-- **** End of PL installation **** -->
@


1.9
log
@Add `id' attributes to all `chapter' and `sect1' tags, to generate useful
names for the HTML files (e.g., not x4856.htm).
@
text
@d2 1
a2 1
$Header: /home/projects/pgsql/cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.8 2000/04/02 22:59:40 tgl Exp $
d69 1
a69 1
CREATE [ TRUSTED ] PROCEDURAL LANGUAGE '<replaceable>language-name</replaceable>'
@


1.8
log
@Copy-editing, mostly.
@
text
@d2 1
a2 1
$Header: /usr/local/cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.7 2000/03/31 03:27:42 thomas Exp $
d26 1
a26 1
  <sect1>
@


1.7
log
@Minor markup changes. Refer to (".../catalog") in the emacs hints.
@
text
@d2 1
a2 1
$Header$
d13 2
a14 2
   no builtin knowlege how to interpret the functions source
   text. Instead, the calls are passed into
d21 5
d36 3
a49 4
     <para>
      Writing a handler for a new procedural language (PL)
      is outside the scope of this manual. 
     </para>
d60 2
a61 2
      the database, that this function does not return one of
      the defined base- or composite types and is not directly usable
d74 2
a75 2
      if ordinary database users that have no superuser
      privileges can use this language to create functions
d77 3
a79 2
      executed inside the database backend it should only be used for
      languages that don't gain access to database backends
d91 1
a91 1
      shared object for the PL/pgSQL languages call handler function.
d124 1
a124 1
      Due to this, in contrast to C language functions, PL functions
@


1.6
log
@Split off language-specific docs to their own chapters.
Add PL/perl docs from Mark Hollomon.
Put language-specific docs into the User's Guide, and move the rest
 to the Programmer's Guide.
@
text
@d1 4
d149 1
a149 1
sgml-local-catalogs:("/usr/lib/sgml/CATALOG")
@


1.5
log
@Complete merge of all old man page information.
lisp.sgml is a placeholder for Eric Marsden's upcoming contribution.
catalogs.sgml is not yet marked up or integrated.
 It should perhaps become an appendix.
@
text
@d4 12
a15 4
<!-- **********
     * General information about procedural language support
     **********
-->
d17 2
a18 13
<para>
    Beginning with the release of version 6.3,
    <productname>Postgres</productname> supports
    the definition of procedural languages.
    In the case of a function or trigger
    procedure defined in a procedural language, the database has
    no builtin knowlege how to interpret the functions source
    text. Instead, the calls are passed into
    a handler that knows the details of the language. The
    handler itself is a special programming language function
    compiled into a shared object
    and loaded on demand.
</para>
d20 1
a20 9
<!-- **********
     * Installation of procedural languages
     **********
-->

<sect1>
<title>Installing Procedural Languages</title>

<procedure>
d22 1
a22 1
    Procedural Language Installation
d26 1
a26 1
    A procedural language is installed in the database in three steps.
a27 46
	<step performance="Required">
	    <para>
	        The shared object for the language handler
		must be compiled and installed. By default the
		handler for PL/pgSQL is built and installed into the
		database library directory. If Tcl/Tk support is
		configured in, the handler for PL/Tcl is also built
		and installed in the same location.
	    </para>
	    <para>
	        Writing a handler for a new procedural language (PL)
		is outside the scope of this manual. 
	    </para>
	</step>
	<step performance="Required">
	    <para>
	        The handler must be declared with the command
		<programlisting>
    CREATE FUNCTION <replaceable>handler_function_name</replaceable> () RETURNS OPAQUE AS
        '<filename>path-to-shared-object</filename>' LANGUAGE 'C';
		</programlisting>
		The special return type of <acronym>OPAQUE</acronym> tells
		the database, that this function does not return one of
		the defined base- or composite types and is not directly usable
		in <acronym>SQL</acronym> statements.
	    </para>
	</step>
	<step performance="Required">
	    <para>
	    	The PL must be declared with the command
		<programlisting>
    CREATE [ TRUSTED ] PROCEDURAL LANGUAGE '<replaceable>language-name</replaceable>'
        HANDLER <replaceable>handler_function_name</replaceable>
	LANCOMPILER '<replaceable>description</replaceable>';
		</programlisting>
		The optional keyword <acronym>TRUSTED</acronym> tells
		if ordinary database users that have no superuser
		privileges can use this language to create functions
		and trigger procedures. Since PL functions are
		executed inside the database backend it should only be used for
		languages that don't gain access to database backends
		internals or the filesystem. The languages PL/pgSQL and
		PL/Tcl are known to be trusted.
	    </para>
	</step>
</procedure>
d29 49
a77 1
<procedure>
d80 9
a88 8
    <para>
        The following command tells the database where to find the 
	shared object for the PL/pgSQL languages call handler function.
    </para>
    <programlisting>
    CREATE FUNCTION plpgsql_call_handler () RETURNS OPAQUE AS
        '/usr/local/pgsql/lib/plpgsql.so' LANGUAGE 'C';
    </programlisting>
d93 33
a125 32
        The command
    </para>
    <programlisting>
    CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
        HANDLER plpgsql_call_handler
        LANCOMPILER 'PL/pgSQL';
    </programlisting>
    <para>
        then defines that the previously declared call handler
	function should be invoked for functions and trigger procedures
	where the language attribute is 'plpgsql'.
    </para>
    <para>
        PL handler functions have a special call interface that is
	different from regular C language functions. One of the arguments
	given to the handler is the object ID in the <filename>pg_proc</filename>
	tables entry for the function that should be executed.
        The handler examines various system catalogs to analyze the
	functions call arguments and it's return data type. The source
	text of the functions body is found in the prosrc attribute of
	<filename>pg_proc</filename>.
	Due to this, in contrast to C language functions, PL functions
	can be overloaded like SQL language functions. There can be
	multiple different PL functions having the same function name,
	as long as the call arguments differ.
    </para>
    <para>
        Procedural languages defined in the <filename>template1</filename>
	database are automatically defined in all subsequently created
	databases. So the database administrator can decide which
	languages are available by default.
    </para>
d127 2
a128 1448
</procedure>
</sect1> <!-- **** End of PL installation **** -->

<!-- **********
     * The procedural language PL/pgSQL
     **********
-->

<sect1>
<title>PL/pgSQL</title>

<para>
    PL/pgSQL is a loadable procedural language for the
    <productname>Postgres</productname> database system.
</para>

<para>
    This package was originally written by Jan Wieck.
</para>

<!-- **** PL/pgSQL overview **** -->

<sect2>
<title>Overview</title>

<para>
    The design goals of PL/pgSQL were to create a loadable procedural
    language that
    <itemizedlist>
    <listitem>
	<para>
        can be used to create functions and trigger procedures,
	</para>
    </listitem>
    <listitem>
	<para>
        adds control structures to the <acronym>SQL</acronym> language,
	</para>
    </listitem>
    <listitem>
	<para>
        can perform complex computations,
	</para>
    </listitem>
    <listitem>
	<para>
        inherits all user defined types, functions and operators,
	</para>
    </listitem>
    <listitem>
	<para>
        can be defined to be trusted by the server,
	</para>
    </listitem>
    <listitem>
	<para>
        is easy to use.
	</para>
    </listitem>
    </itemizedlist>
</para>
<para>
    The PL/pgSQL call handler parses the functions source text and
    produces an internal binary instruction tree on the first time, the
    function is called by a backend. The produced bytecode is identified
    in the call handler by the object ID of the function. This ensures,
    that changing a function by a DROP/CREATE sequence will take effect
    without establishing a new database connection. 
</para>
<para>
    For all expressions and <acronym>SQL</acronym> statements used in
    the function, the PL/pgSQL bytecode interpreter creates a
    prepared execution plan using the SPI managers SPI_prepare() and
    SPI_saveplan() functions. This is done the first time, the individual
    statement is processed in the PL/pgSQL function. Thus, a function with
    conditional code that contains many statements for which execution
    plans would be required, will only prepare and save those plans
    that are really used during the entire lifetime of the database
    connection.
</para>
<para>
    Except for input-/output-conversion and calculation functions
    for user defined types, anything that can be defined in C language
    functions can also be done with PL/pgSQL. It is possible to
    create complex conditional computation functions and later use
    them to define operators or use them in functional indices.
</para>
</sect2>

<!-- **** PL/pgSQL Description **** -->

<sect2>
<title>Description</title>

<!-- **** PL/pgSQL structure **** -->

<sect3>
<title>Structure of PL/pgSQL</title>

<para>
    The PL/pgSQL language is case insensitive. All keywords and
    identifiers can be used in mixed upper- and lowercase.
</para>
<para>
    PL/pgSQL is a block oriented language. A block is defined as

<programlisting>
    [&lt;&lt;label&gt;&gt;]
    [DECLARE
        <replaceable>declarations</replaceable>]
    BEGIN
        <replaceable>statements</replaceable>
    END;
</programlisting>

    There can be any number of subblocks in the statement section
    of a block. Subblocks can be used to hide variables from outside a
    block of statements. The variables
    declared in the declarations section preceding a block are
    initialized to their default values every time the block is entered,
    not only once per function call.
</para>
  
<para>
    It is important not to misunderstand the meaning of BEGIN/END for
    grouping statements in PL/pgSQL and the database commands for
    transaction control. Functions and trigger procedures cannot
    start or commit transactions and <productname>Postgres</productname>
    does not have nested transactions.
</para>
</sect3>

<!-- **** PL/pgSQL comments **** -->

<sect3>
<title>Comments</title>

<para>
    There are two types of comments in PL/pgSQL. A double dash '--'
    starts a comment that extends to the end of the line. A '/*'
    starts a block comment that extends to the next occurence of '*/'.
    Block comments cannot be nested, but double dash comments can be
    enclosed into a block comment and a double dash can hide
    the block comment delimiters '/*' and '*/'.
</para>
</sect3>

<!-- **** PL/pgSQL declarations **** -->

<sect3>
<title>Declarations</title>

<para>
    All variables, rows and records used in a block or it's
    subblocks must be declared in the declarations section of a block
    except for the loop variable of a FOR loop iterating over a range
    of integer values. Parameters given to a PL/pgSQL function are
    automatically declared with the usual identifiers $n.
    The declarations have the following syntax:
</para>
<variablelist>

<varlistentry>
<term>
<replaceable>name</replaceable> [ CONSTANT ]
<replaceable>>typ</replaceable>> [ NOT NULL ] [ DEFAULT | :=
	<replaceable>value</replaceable> ];
</term>
<listitem>
<para>
    Declares a variable of the specified base type. If the variable
    is declared as CONSTANT, the value cannot be changed. If NOT NULL
    is specified, an assignment of a NULL value results in a runtime
    error. Since the default value of all variables is the
    <acronym>SQL</acronym> NULL value, all variables declared as NOT NULL
    must also have a default value specified.
</para>
<para>
    The default value is evaluated ever time the function is called. So
    assigning '<replaceable>now</replaceable>' to a variable of type
    <replaceable>datetime</replaceable> causes the variable to have the
    time of the actual function call, not when the function was
    precompiled into it's bytecode.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
<replaceable>name</replaceable> <replaceable>class</replaceable>%ROWTYPE;
</term>
<listitem>
<para>
    Declares a row with the structure of the given class. Class must be
    an existing table- or viewname of the database. The fields of the row
    are accessed in the dot notation. Parameters to a function can
    be composite types (complete table rows). In that case, the
    corresponding identifier $n will be a rowtype, but it
    must be aliased using the ALIAS command described below. Only the user
    attributes of a table row are accessible in the row, no Oid or other
    system attributes (hence the row could be from a view and view rows
    don't have useful system attributes).
</para>
<para>
    The fields of the rowtype inherit the tables fieldsizes 
    or precision for char() etc. data types.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
<replaceable>name</replaceable> RECORD;
</term>
<listitem>
<para>
    Records are similar to rowtypes, but they have no predefined structure.
    They are used in selections and FOR loops to hold one actual
    database row from a SELECT operation. One and the same record can be
    used in different selections. Accessing a record or an attempt to assign
    a value to a record field when there is no actual row in it results
    in a runtime error.
</para>
<para>
    The NEW and OLD rows in a trigger are given to the procedure as
    records. This is necessary because in <productname>Postgres</productname>
    one and the same trigger procedure can handle trigger events for
    different tables.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
<replaceable>name</replaceable> ALIAS FOR $n;
</term>
<listitem>
<para>
    For better readability of the code it is possible to define an alias
    for a positional parameter to a function.
</para>
<para>
    This aliasing is required for composite types given as arguments to
    a function. The dot notation $1.salary as in SQL functions is not
    allowed in PL/pgSQL.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
</term>
<listitem>
<para>
    Change the name of a variable, record or row. This is useful
    if NEW or OLD should be referenced by another name inside a 
    trigger procedure.
</para>
</listitem>
</varlistentry>

</variablelist>
</sect3>

<!-- **** PL/pgSQL data types **** -->

<sect3>
<title>Data Types</title>

<para>
    The type of a varible can be any of the existing basetypes of
    the database. <replaceable>type</replaceable> in the declarations
    section above is defined as:
</para>
<para>
    <itemizedlist>
    <listitem>
        <para>
	<productname>Postgres</productname>-basetype
	</para>
    </listitem>
    <listitem>
        <para>
	<replaceable>variable</replaceable>%TYPE
	</para>
    </listitem>
    <listitem>
        <para>
	<replaceable>class.field</replaceable>%TYPE
	</para>
    </listitem>
    </itemizedlist>
</para>
<para>
    <replaceable>variable</replaceable> is the name of a variable,
previously declared in the 
    same function, that is visible at this point.
</para>
<para>
    <replaceable>class</replaceable> is the name of an existing table
    or view where <replaceable>field</replaceable> is the name of
    an attribute.
</para>
<para>
    Using the <replaceable>class.field</replaceable>%TYPE
    causes PL/pgSQL to lookup the attributes definitions at the
    first call to the funciton during the lifetime of a backend.
    Have a table with a char(20) attribute and some PL/pgSQL functions
    that deal with it's content in local variables. Now someone
    decides that char(20) isn't enough, dumps the table, drops it,
    recreates it now with the attribute in question defined as
    char(40) and restores the data. Ha - he forgot about the
    funcitons. The computations inside them will truncate the values
    to 20 characters. But if they are defined using the
    <replaceable>class.field</replaceable>%TYPE
    declarations, they will automagically handle the size change or
    if the new table schema defines the attribute as text type.
</para>
</sect3>

<!-- **** PL/pgSQL expressions **** -->

<sect3>
<title>Expressions</title>

<para>
    All expressions used in PL/pgSQL statements are processed using
    the backends executor. Expressions which appear to contain
constants may in fact require run-time evaluation (e.g. 'now' for the
datetime type) so
it is impossible for the PL/pgSQL parser
    to identify real constant values other than the NULL keyword. All
    expressions are evaluated internally by executing a query
    <programlisting>
    SELECT <replaceable>expression</replaceable>
    </programlisting>
    using the SPI manager. In the expression, occurences of variable
    identifiers are substituted by parameters and the actual values from
    the variables are passed to the executor in the parameter array. All
    expressions used in a PL/pgSQL function are only prepared and
    saved once.
</para>
<para>
    The type checking done by the <productname>Postgres</productname>
    main parser has some side
    effects to the interpretation of constant values. In detail there
    is a difference between what the two functions

    <programlisting>
    CREATE FUNCTION logfunc1 (text) RETURNS datetime AS '
        DECLARE
            logtxt ALIAS FOR $1;
        BEGIN
            INSERT INTO logtable VALUES (logtxt, ''now'');
            RETURN ''now'';
        END;
    ' LANGUAGE 'plpgsql';
    </programlisting>

    and

    <programlisting>
    CREATE FUNCTION logfunc2 (text) RETURNS datetime AS '
        DECLARE
            logtxt ALIAS FOR $1;
            curtime datetime;
        BEGIN
            curtime := ''now'';
            INSERT INTO logtable VALUES (logtxt, curtime);
            RETURN curtime;
        END;
    ' LANGUAGE 'plpgsql';
    </programlisting>

    do. In the case of logfunc1(), the <productname>Postgres</productname>
    main parser
    knows when preparing the plan for the INSERT, that the string 'now'
    should be interpreted as datetime because the target field of logtable
    is of that type. Thus, it will make a constant from it at this time
    and this constant value is then used in all invocations of logfunc1()
    during the lifetime of the backend. Needless to say that this isn't what the
    programmer wanted.
</para>
<para>
    In the case of logfunc2(), the <productname>Postgres</productname> 
    main parser does not know
    what type 'now' should become and therefor it returns a datatype of
    text containing the string 'now'. During the assignment
    to the local variable curtime, the PL/pgSQL interpreter casts this
    string to the datetime type by calling the text_out() and datetime_in()
    functions for the conversion.
</para>
<para>
    This type checking done by the <productname>Postgres</productname> main
    parser got implemented after PL/pgSQL was nearly done.
    It is a difference between 6.3 and 6.4 and affects all functions
    using the prepared plan feature of the SPI manager.
    Using a local
    variable in the above manner is currently the only way in PL/pgSQL to get
    those values interpreted correctly.
</para>
<para>
    If record fields are used in expressions or statements, the data types of
    fields should not change between calls of one and the same expression.
    Keep this in mind when writing trigger procedures that handle events
    for more than one table.
</para>
</sect3>

<!-- **** PL/pgSQL statements **** -->

<sect3>
<title>Statements</title>

<para>
    Anything not understood by the PL/pgSQL parser as specified below
    will be put into a query and sent down to the database engine
    to execute. The resulting query should not return any data.
</para>

<variablelist>

<varlistentry>
<term>
Assignment
</term>
<listitem>
<para>
    An assignment of a value to a variable or row/record field is
    written as
    <programlisting>
    <replaceable>identifier</replaceable> := <replaceable>expression</replaceable>;
    </programlisting>
    If the expressions result data type doesn't match the variables
    data type, or the variable has a size/precision that is known
    (as for char(20)), the result value will be implicitly casted by
    the PL/pgSQL bytecode interpreter using the result types output- and
    the variables type input-functions. Note that this could potentially
    result in runtime errors generated by the types input functions.
</para>
<para>
    An assignment of a complete selection into a record or row can
    be done by
    <programlisting>
    SELECT <replaceable>expressions</replaceable> INTO <replaceable>target</replaceable> FROM ...;
    </programlisting>
    <replaceable>target</replaceable> can be a record, a row variable or a
    comma separated list of variables and record-/row-fields.
</para>
<para>
    if a row or a variable list is used as target, the selected values
    must exactly match the structure of the target(s) or a runtime error
    occurs. The FROM keyword can be followed by any valid qualification,
    grouping, sorting etc. that can be given for a SELECT statement.
</para>
<para>
    There is a special variable named FOUND of type bool that can be used
    immediately after a SELECT INTO to check if an assignment had success.

    <programlisting>
    SELECT * INTO myrec FROM EMP WHERE empname = myname;
    IF NOT FOUND THEN
        RAISE EXCEPTION ''employee % not found'', myname;
    END IF;
    </programlisting>

    If the selection returns multiple rows, only the first is moved
    into the target fields. All others are silently discarded.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
Calling another function
</term>
<listitem>
<para>
    All functions defined in a <productname>Prostgres</productname>
    database return a value. Thus, the normal way to call a function
    is to execute a SELECT query or doing an assignment (resulting
    in a PL/pgSQL internal SELECT). But there are cases where someone
    isn't interested int the functions result.
    <programlisting>
    PERFORM <replaceable>query</replaceable>
    </programlisting>
    executes a 'SELECT <replaceable>query</replaceable>' over the
    SPI manager and discards the result. Identifiers like local
    variables are still substituted into parameters.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
Returning from the function
</term>
<listitem>
<para>
    <programlisting>
    RETURN <replaceable>expression</replaceable>
    </programlisting>
    The function terminates and the value of <replaceable>expression</replaceable>
    will be returned to the upper executor. The return value of a function
    cannot be undefined. If control reaches the end of the toplevel block
    of the function without hitting a RETURN statement, a runtime error
    will occur.
</para>
<para>
    The expressions result will be automatically casted into the
    functions return type as described for assignments.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
Aborting and messages
</term>
<listitem>
<para>
    As indicated in the above examples there is a RAISE statement that
    can throw messages into the <productname>Postgres</productname>
    elog mechanism.
    <programlisting>
    RAISE <replaceable class="parameter">level</replaceable>
	 <replaceable class="parameter">r">for</replaceable>le>'' [,
	  <replaceable class="parameter">identifier</replaceable> [...]];
    </programlisting>
    Inside the format, <quote>%</quote> is used as a placeholder for the
    subsequent comma-separated identifiers. Possible levels are
    DEBUG (silently suppressed in production running databases), NOTICE 
    (written into the database log and forwarded to the client application)
    and EXCEPTION (written into the database log and aborting the transaction).
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
Conditionals
</term>
<listitem>
<para>
    <programlisting>
    IF <replaceable>expression</replaceable> THEN
        <replaceable>statements</replaceable>
    [ELSE
        <replaceable>statements</replaceable>]
    END IF;
    </programlisting>
    The <replaceable>expression</replaceable> must return a value that
    at least can be casted into a boolean type.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
Loops
</term>
<listitem>
<para>
    There are multiple types of loops.
    <programlisting>
    [&lt;&lt;label&gt;&gt;]
    LOOP
        <replaceable>statements</replaceable>
    END LOOP;
    </programlisting>
    An unconditional loop that must be terminated explicitly
    by an EXIT statement. The optional label can be used by
    EXIT statements of nested loops to specify which level of
    nesting should be terminated.
    <programlisting>
    [&lt;&lt;label&gt;&gt;]
    WHILE <replaceable>expression</replaceable> LOOP
        <replaceable>statements</replaceable>
    END LOOP;
    </programlisting>
    A conditional loop that is executed as long as the evaluation
    of <replaceable>expression</replaceable> is true.
    <programlisting>
    [&lt;&lt;label&gt;&gt;]
    FOR <replaceable>name</replaceable> IN [ REVERSE ]
<replaceable>le>express</replaceable>le> .. <replaceable>expression</replaceable> LOOP
        <replaceable>statements</replaceable>
    END LOOP;
    </programlisting>
    A loop that iterates over a range of integer values. The variable
    <replaceable>name</replaceable> is automatically created as type
    integer and exists only inside the loop. The two expressions giving
    the lower and upper bound of the range are evaluated only when entering
    the loop. The iteration step is always 1.
    <programlisting>
    [&lt;&lt;label&gt;&gt;]
    FOR <replaceable>record | row</replaceable> IN <replaceable>select_clause</replaceable> LOOP
        <replaceable>statements</replaceable>
    END LOOP;
    </programlisting>
    The record or row is assigned all the rows resulting from the select
    clause and the statements executed for each. If the loop is terminated
    with an EXIT statement, the last assigned row is still accessible 
    after the loop.
    <programlisting>
    EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replaceable> ];
    </programlisting>
    If no <replaceable>label</replaceable> given,
 the innermost loop is terminated and the
    statement following END LOOP is executed next.
 If <replaceable>label</replaceable> is given, it
    must be the label of the current or an upper level of nested loop
    blocks. Then the named loop or block is terminated and control
    continues with the statement after the loops/blocks corresponding
    END.
</para>
</listitem>
</varlistentry>

</variablelist>

</sect3>

<!-- **** PL/pgSQL trigger procedures **** -->

<sect3>
<title>Trigger Procedures</title>

<para>
    PL/pgSQL can be used to define trigger procedures. They are created
    with the usual CREATE FUNCTION command as a function with no
    arguments and a return type of OPAQUE.
</para>
<para>
    There are some <productname>Postgres</productname> specific details
    in functions used as trigger procedures.
</para>
<para>
    First they have some special variables created automatically in the 
    toplevel blocks declaration section. They are
</para>

<variablelist>

<varlistentry>
<term>
    NEW
</term>
<listitem>
<para>
    Datatype RECORD; variable holding the new database row on INSERT/UPDATE
    operations on ROW level triggers.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
    OLD
</term>
<listitem>
<para>
    Datatype RECORD; variable holding the old database row on UPDATE/DELETE
    operations on ROW level triggers.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
    TG_NAME
</term>
<listitem>
<para>
    Datatype name; variable that contains the name of the trigger actually
    fired.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
    TG_WHEN
</term>
<listitem>
<para>
    Datatype text; a string of either 'BEFORE' or 'AFTER' depending on the
    triggers definition.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
    TG_LEVEL
</term>
<listitem>
<para>
    Datatype text; a string of either 'ROW' or 'STATEMENT' depending on the
    triggers definition.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
    TG_OP
</term>
<listitem>
<para>
    Datatype text; a string of 'INSERT', 'UPDATE' or 'DELETE' telling
    for which operation the trigger is actually fired.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
    TG_RELID
</term>
<listitem>
<para>
    Datatype oid; the object ID of the table that caused the
    trigger invocation.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
    TG_RELNAME
</term>
<listitem>
<para>
    Datatype name; the name of the table that caused the trigger
    invocation.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
    TG_NARGS
</term>
<listitem>
<para>
    Datatype integer; the number of arguments given to the trigger
    procedure in the CREATE TRIGGER statement.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
    TG_ARGV[]
</term>
<listitem>
<para>
    Datatype array of text; the arguments from the CREATE TRIGGER statement.
    The index counts from 0 and can be given as an expression. Invalid
    indices (&lt; 0 or &gt;= tg_nargs) result in a NULL value.
</para>
</listitem>
</varlistentry>

</variablelist>

<para>
    Second they must return either NULL or a record/row containing
    exactly the structure of the table the trigger was fired for.
    Triggers fired AFTER might always return a NULL value with no
    effect. Triggers fired BEFORE signal the trigger manager
    to skip the operation for this actual row when returning NULL.
    Otherwise, the returned record/row replaces the inserted/updated
    row in the operation. It is possible to replace single values directly
    in NEW and return that or to build a complete new record/row to
    return.
</para>
</sect3>

<!-- **** PL/pgSQL exceptions **** -->

<sect3>
<title>Exceptions</title>

<para>
    <productname>Postgres</productname> does not have a very smart
    exception handling model. Whenever the parser, planner/optimizer
    or executor decide that a statement cannot be processed any longer,
    the whole transaction gets aborted and the system jumps back
    into the mainloop to get the next query from the client application.
</para>
<para>
    It is possible to hook into the error mechanism to notice that this
    happens. But currently it's impossible to tell what really
    caused the abort (input/output conversion error, floating point
    error, parse error). And it is possible that the database backend
    is in an inconsistent state at this point so returning to the upper
    executor or issuing more commands might corrupt the whole database.
    And even if, at this point the information, that the transaction
    is aborted, is already sent to the client application, so resuming
    operation does not make any sense.
</para>
<para>
    Thus, the only thing PL/pgSQL currently does when it encounters
    an abort during execution of a function or trigger
    procedure is to write some additional DEBUG level log messages
    telling in which function and where (line number and type of
    statement) this happened.
</para>
</sect3>
</sect2>

<!-- **** PL/pgSQL Examples **** -->

<sect2>
<title>Examples</title>

<para>
Here are only a few functions to demonstrate how easy PL/pgSQL
functions can be written. For more complex examples the programmer
might look at the regression test for PL/pgSQL.
</para>

<para>
One painful detail of writing functions in PL/pgSQL is the handling
of single quotes. The functions source text on CREATE FUNCTION must
be a literal string. Single quotes inside of literal strings must be
either doubled or quoted with a backslash. We are still looking for
an elegant alternative. In the meantime, doubling the single qoutes
as in the examples below should be used. Any solution for this
in future versions of <productname>Postgres</productname> will be
upward compatible.
</para>

<sect3>
<title>Some Simple PL/pgSQL Functions</title>

<para>
    The following two PL/pgSQL functions are identical to their
    counterparts from the C language function discussion.

    <programlisting>
    CREATE FUNCTION add_one (int4) RETURNS int4 AS '
        BEGIN
            RETURN $1 + 1;
        END;
    ' LANGUAGE 'plpgsql';
    </programlisting>

    <programlisting>
    CREATE FUNCTION concat_text (text, text) RETURNS text AS '
        BEGIN
            RETURN $1 || $2;
        END;
    ' LANGUAGE 'plpgsql';
    </programlisting>
</para>

</sect3>

<sect3>
<title>PL/pgSQL Function on Composite Type</title>

<para>
    Again it is the PL/pgSQL equivalent to the example from
    The C functions.

    <programlisting>
    CREATE FUNCTION c_overpaid (EMP, int4) RETURNS bool AS '
        DECLARE
            emprec ALIAS FOR $1;
            sallim ALIAS FOR $2;
        BEGIN
            IF emprec.salary ISNULL THEN
                RETURN ''f'';
            END IF;
            RETURN emprec.salary > sallim;
        END;
    ' LANGUAGE 'plpgsql';
    </programlisting>
</para>

</sect3>

<sect3>
<title>PL/pgSQL Trigger Procedure</title>

<para>
    This trigger ensures, that any time a row is inserted or updated
    in the table, the current username and time are stamped into the
    row. And it ensures that an employees name is given and that the
    salary is a positive value.

    <programlisting>
    CREATE TABLE emp (
        empname text,
        salary int4,
        last_date datetime,
        last_user name);

    CREATE FUNCTION emp_stamp () RETURNS OPAQUE AS
        BEGIN
            -- Check that empname and salary are given
            IF NEW.empname ISNULL THEN
                RAISE EXCEPTION ''empname cannot be NULL value'';
            END IF;
            IF NEW.salary ISNULL THEN
                RAISE EXCEPTION ''% cannot have NULL salary'', NEW.empname;
            END IF;

            -- Who works for us when she must pay for?
            IF NEW.salary < 0 THEN
                RAISE EXCEPTION ''% cannot have a negative salary'', NEW.empname;
            END IF;

            -- Remember who changed the payroll when
            NEW.last_date := ''now'';
            NEW.last_user := getpgusername();
            RETURN NEW;
        END;
    ' LANGUAGE 'plpgsql';

    CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
        FOR EACH ROW EXECUTE PROCEDURE emp_stamp();
    </programlisting>
</para>

</sect3>

</sect2>

</sect1>

<!-- **********
     * The procedural language PL/Tcl
     **********
-->

<sect1>
<title>PL/Tcl</title>

<para>
    PL/Tcl is a loadable procedural language for the
    <productname>Postgres</productname> database system
    that enables the Tcl language to be used to create functions and
    trigger-procedures.
</para>

<para>
    This package was originally written by Jan Wieck.
</para>

<!-- **** PL/Tcl overview **** -->

<sect2>
<title>Overview</title>

<para>
    PL/Tcl offers most of the capabilities a function
    writer has in the C language, except for some restrictions.
</para>
<para>
    The good restriction is, that everything is executed in a safe
    Tcl-interpreter. In addition to the limited command set of safe Tcl, only
    a few commands are available to access the database over SPI and to raise
    messages via elog(). There is no way to access internals of the
    database backend or gaining OS-level access under the permissions of the
    <productname>Postgres</productname> user ID like in C.
    Thus, any unprivileged database user may be
    permitted to use this language.
</para>
<para>
    The other, internal given, restriction is, that Tcl procedures cannot
    be used to create input-/output-functions for new data types.
</para>
<para>
    The shared object for the PL/Tcl call handler is automatically built
    and installed in the <productname>Postgres</productname>
    library directory if the Tcl/Tk support is specified
    in the configuration step of the installation procedure.
</para>
</sect2>

<!-- **** PL/Tcl description **** -->

<sect2>
<title>Description</title>

<sect3>
<title><productname>Postgres</productname> Functions and Tcl Procedure Names</title>

<para>
    In <productname>Postgres</productname>, one and the 
    same function name can be used for
    different functions as long as the number of arguments or their types
    differ. This would collide with Tcl procedure names. To offer the same
    flexibility in PL/Tcl, the internal Tcl procedure names contain the object
    ID of the procedures pg_proc row as part of their name. Thus, different
    argtype versions of the same <productname>Postgres</productname> 
    function are different for Tcl too.
</para>

</sect3>

<sect3>
<title>Defining Functions in PL/Tcl</title>

<para>
    To create a function in the PL/Tcl language, use the known syntax

    <programlisting>
    CREATE FUNCTION <replaceable>funcname</replaceable>
 <replaceable>ceable>argumen</replaceable>ceable>) RETURNS
       <replaceable>returntype</replaceable> AS '
        # PL/Tcl function body
    ' LANGUAGE 'pltcl';
    </programlisting>

    When calling this function in a query, the arguments are given as
    variables $1 ... $n to the Tcl procedure body. So a little max function
    returning the higher of two int4 values would be created as:

    <programlisting>
    CREATE FUNCTION tcl_max (int4, int4) RETURNS int4 AS '
        if {$1 > $2} {return $1}
	return $2
    ' LANGUAGE 'pltcl';
    </programlisting>

    Composite type arguments are given to the procedure as Tcl arrays.
    The element names
    in the array are the attribute names of the composite
    type. If an attribute in the actual row
    has the NULL value, it will not appear in the array! Here is
    an example that defines the overpaid_2 function (as found in the
    older <productname>Postgres</productname> documentation) in PL/Tcl

    <programlisting>
    CREATE FUNCTION overpaid_2 (EMP) RETURNS bool AS '
        if {200000.0 < $1(salary)} {
            return "t"
        }
        if {$1(age) < 30 && 100000.0 < $1(salary)} {
            return "t"
        }
        return "f"
    ' LANGUAGE 'pltcl';
    </programlisting>
</para>

</sect3>

<sect3>
<title>Global Data in PL/Tcl</title>

<para>
    Sometimes (especially when using the SPI functions described later) it
    is useful to have some global status data that is held between two
    calls to a procedure. 
    All PL/Tcl procedures executed in one backend share the same
    safe Tcl interpreter.
    To help protecting PL/Tcl procedures from side effects,
    an array is made available to each procedure via the upvar
    command. The global name of this variable is the procedures internal
    name and the local name is GD.
</para>
</sect3>

<sect3>
<title>Trigger Procedures in PL/Tcl</title>

<para>
    Trigger procedures are defined in <productname>Postgres</productname>
    as functions without
    arguments and a return type of opaque. And so are they in the PL/Tcl
    language.
</para>
<para>
    The informations from the trigger manager are given to the procedure body
    in the following variables:
</para>
<variablelist>

<varlistentry>
<term><replaceable class="Parameter">
$TG_name
</replaceable></term>
<listitem>
<para>
    The name of the trigger from the CREATE TRIGGER statement.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><replaceable class="Parameter">
$TG_relid
</replaceable></term>
<listitem>
<para>
    The object ID of the table that caused the trigger procedure
    to be invoked.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><replaceable class="Parameter">
$TG_relatts
</replaceable></term>
<listitem>
<para>
    A Tcl list of the tables field names prefixed with an empty list element.
    So looking up an element name in the list with the lsearch Tcl command
    returns the same positive number starting from 1 as the fields are numbered
    in the pg_attribute system catalog.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><replaceable class="Parameter">
$TG_when
</replaceable></term>
<listitem>
<para>
    The string BEFORE or AFTER depending on the event of the trigger call.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><replaceable class="Parameter">
$TG_level
</replaceable></term>
<listitem>
<para>
    The string ROW or STATEMENT depending on the event of the trigger call.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><replaceable class="Parameter">
$TG_op
</replaceable></term>
<listitem>
<para>
    The string INSERT, UPDATE or DELETE depending on the event of the 
    trigger call.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><replaceable class="Parameter">
$NEW
</replaceable></term>
<listitem>
<para>
    An array containing the values of the new table row on INSERT/UPDATE
    actions, or empty on DELETE.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><replaceable class="Parameter">
$OLD
</replaceable></term>
<listitem>
<para>
    An array containing the values of the old table row on UPDATE/DELETE
    actions, or empty on INSERT.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><replaceable class="Parameter">
$GD
</replaceable></term>
<listitem>
<para>
    The global status data array as described above.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><replaceable class="Parameter">
$args
</replaceable></term>
<listitem>
<para>
    A Tcl list of the arguments to the procedure as given in the
    CREATE TRIGGER statement. The arguments are also accessible as $1 ... $n
    in the procedure body.
</para>
</listitem>
</varlistentry>

</variablelist>

<para>
    The return value from a trigger procedure is one of the strings OK or SKIP,
    or a list as returned by the 'array get' Tcl command. If the return value
    is OK, the normal operation (INSERT/UPDATE/DELETE) that fired this trigger
    will take place. Obviously, SKIP tells the trigger manager to silently
    suppress the operation. The list from 'array get' tells PL/Tcl
    to return a modified row to the trigger manager that will be inserted instead
    of the one given in $NEW (INSERT/UPDATE only). Needless to say that all
    this is only meaningful when the trigger is BEFORE and FOR EACH ROW.
</para>
<para>
    Here's a little example trigger procedure that forces an integer value
    in a table to keep track of the # of updates that are performed on the
    row. For new row's inserted, the value is initialized to 0 and then
    incremented on every update operation:

    <programlisting>
    CREATE FUNCTION trigfunc_modcount() RETURNS OPAQUE AS '
        switch $TG_op {
            INSERT {
                set NEW($1) 0
            }
            UPDATE {
                set NEW($1) $OLD($1)
                incr NEW($1)
            }
            default {
                return OK
            }
        }
        return [array get NEW]
    ' LANGUAGE 'pltcl';

    CREATE TABLE mytab (num int4, modcnt int4, desc text);

    CREATE TRIGGER trig_mytab_modcount BEFORE INSERT OR UPDATE ON mytab
        FOR EACH ROW EXECUTE PROCEDURE trigfunc_modcount('modcnt');
    </programlisting>

</para>
</sect3>

<sect3>
<title>Database Access from PL/Tcl</title>

<para>
    The following commands are available to access the database from
    the body of a PL/Tcl procedure:
</para>

<variablelist>

<varlistentry>
<term>
elog <replaceable>level</replaceable> <replaceable>msg</replaceable>
</term>
<listitem>
<para>
    Fire a log message. Possible levels are NOTICE, WARN, ERROR,
    FATAL, DEBUG and NOIND
    like for the elog() C function.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
quote <replaceable>string</replaceable>
</term>
<listitem>
<para>
    Duplicates all occurences of single quote and backslash characters.
    It should be used when variables are used in the query string given
    to spi_exec or spi_prepare (not for the value list on spi_execp).
    Think about a query string like

    <programlisting>
    "SELECT '$val' AS ret"
    </programlisting>

    where the Tcl variable val actually contains "doesn't". This would result
    in the final query string

    <programlisting>
    "SELECT 'doesn't' AS ret"
    </programlisting>

    what would cause a parse error during spi_exec or spi_prepare.
    It should contain

    <programlisting>
    "SELECT 'doesn''t' AS ret"
    </programlisting>

    and has to be written as

    <programlisting>
    "SELECT '[ quote $val ]' AS ret"
    </programlisting>
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
spi_exec ?-count <replaceable>n</replaceable>? ?-array
<replaceable>>nam</replaceable>>?<replaceable>e>que</replaceable>e> ?<replaceable>loop-body</replaceable>?
</term>
<listitem>
<para>
    Call parser/planner/optimizer/executor for query.
    The optional -count value tells spi_exec the maximum number of rows
    to be processed by the query.
</para>
<para>
    If the query is
    a SELECT statement and the optional loop-body (a body of Tcl commands
    like in a foreach statement) is given, it is evaluated for each
    row selected and behaves like expected on continue/break. The values
    of selected fields are put into variables named as the column names. So a

    <programlisting>
    spi_exec "SELECT count(*) AS cnt FROM pg_proc"
    </programlisting>

    will set the variable $cnt to the number of rows in the pg_proc system
    catalog. If the option -array is given, the column values are stored
    in the associative array named 'name' indexed by the column name
    instead of individual variables.

    <programlisting>
    spi_exec -array C "SELECT * FROM pg_class" {
        elog DEBUG "have table $C(relname)"
    }
    </programlisting>

    will print a DEBUG log message for every row of pg_class. The return value
    of spi_exec is the number of rows affected by query as found in
    the global variable SPI_processed.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
spi_prepare <replaceable>query</replaceable> <replaceable>typelist</replaceable>
</term>
<listitem>
<para>
    Prepares AND SAVES a query plan for later execution. It is a bit different
    from the C level SPI_prepare in that the plan is automatically copied to the
    toplevel memory context. Thus, there is currently no way of preparing a
    plan without saving it.
</para>
<para>
    If the query references arguments, the type names must be given as a Tcl
    list. The return value from spi_prepare is a query ID to be used in
    subsequent calls to spi_execp. See spi_execp for a sample.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
spi_exec ?-count <replaceable>n</replaceable>? ?-array
<replaceable>>nam</replaceable>>? ?-nulls<replaceable>e>s</replaceable>e><replaceable>le>qu</replaceable>le<replaceable>ble>value</replaceable>ble>? ?<replaceable>loop-body</replaceable>?
</term>
<listitem>
<para>
    Execute a prepared plan from spi_prepare with variable substitution.
    The optional -count value tells spi_execp the maximum number of rows
    to be processed by the query.
</para>
<para>
    The optional value for -nulls is a string of spaces and 'n' characters
    telling spi_execp which of the values are NULL's. If given, it must
    have exactly the length of the number of values.
</para>
<para>
    The queryid is the ID returned by the spi_prepare call.
</para>
<para>
    If there was a typelist given to spi_prepare, a Tcl list of values of
    exactly the same length must be given to spi_execp after the query. If
    the type list on spi_prepare was empty, this argument must be omitted.
</para>
<para>
    If the query is a SELECT statement, the same as described for spi_exec
    happens for the loop-body and the variables for the fields selected.
</para>
<para>
    Here's an example for a PL/Tcl function using a prepared plan:

    <programlisting>
    CREATE FUNCTION t1_count(int4, int4) RETURNS int4 AS '
        if {![ info exists GD(plan) ]} {
            # prepare the saved plan on the first call
            set GD(plan) [ spi_prepare \\
                    "SELECT count(*) AS cnt FROM t1 WHERE num &gt;= \\$1 AND num &lt;= \\$2" \\
                    int4 ]
        }
        spi_execp -count 1 $GD(plan) [ list $1 $2 ]
        return $cnt
    ' LANGUAGE 'pltcl';
    </programlisting>

    Note that each backslash that Tcl should see must be doubled in
    the query creating the function, since the main parser processes
    backslashes too on CREATE FUNCTION.
    Inside the query string given to spi_prepare should
    really be dollar signs to mark the parameter positions and to not let
    $1 be substituted by the value given in the first function call.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
Modules and the unknown command
</term>
<listitem>
<para>
    PL/Tcl has a special support for things often used. It recognizes two
    magic tables, pltcl_modules and pltcl_modfuncs.
    If these exist, the module 'unknown' is loaded into the interpreter
    right after creation. Whenever an unknown Tcl procedure is called,
    the unknown proc is asked to check if the procedure is defined in one
    of the modules. If this is true, the module is loaded on demand.
    To enable this behavior, the PL/Tcl call handler must be compiled
    with -DPLTCL_UNKNOWN_SUPPORT set.
</para>
<para>
    There are support scripts to maintain these tables in the modules
    subdirectory of the PL/Tcl source including the source for the
    unknown module that must get installed initially.
</para>
</listitem>
</varlistentry>

</variablelist>

</sect3>
d130 1
a130 2
</sect2>
</sect1>
d135 1
a135 1
mode: sgml
d145 1
a145 1
sgml-local-catalogs:"/usr/lib/sgml/CATALOG"
@


1.4
log
@Clean up to ensure tag completion as required by the newest versions
 of Norm's Modular Style Sheets and jade/docbook.
From Vince Vielhaber <vev@@michvhf.com>.
@
text
@d1 2
a2 2
<Chapter Id="xplang">
<Title>Procedural Languages</Title>
d9 1
a9 1
<Para>
d11 1
a11 1
    <ProductName>Postgres</ProductName> supports
d21 1
a21 1
</Para>
d28 2
a29 2
<Sect1>
<Title>Installing Procedural Languages</Title>
d31 2
a32 2
<Procedure>
    <Title>
d34 1
a34 1
    </Title>
d39 2
a40 2
	<Step Performance="Required">
	    <Para>
d47 2
a48 2
	    </Para>
	    <Para>
d51 4
a54 4
	    </Para>
	</Step>
	<Step Performance="Required">
	    <Para>
d56 5
a60 5
		<ProgramListing>
    CREATE FUNCTION <Replaceable>handler_function_name</Replaceable> () RETURNS OPAQUE AS
        '<Filename>path-to-shared-object</Filename>' LANGUAGE 'C';
		</ProgramListing>
		The special return type of <Acronym>OPAQUE</Acronym> tells
d63 5
a67 5
		in <Acronym>SQL</Acronym> statements.
	    </Para>
	</Step>
	<Step Performance="Required">
	    <Para>
d69 6
a74 6
		<ProgramListing>
    CREATE [ TRUSTED ] PROCEDURAL LANGUAGE '<Replaceable>language-name</Replaceable>'
        HANDLER <Replaceable>handler_function_name</Replaceable>
	LANCOMPILER '<Replaceable>description</Replaceable>';
		</ProgramListing>
		The optional keyword <Acronym>TRUSTED</Acronym> tells
d82 8
a89 8
	    </Para>
	</Step>
</Procedure>

<Procedure>
    <Title>Example</Title>
    <Step Performance="Required">
    <Para>
d92 2
a93 2
    </Para>
    <ProgramListing>
d96 2
a97 2
    </ProgramListing>
    </Step>
d99 2
a100 2
    <Step Performance="Required">
    <Para>
d102 2
a103 2
    </Para>
    <ProgramListing>
d107 2
a108 2
    </ProgramListing>
    <Para>
d112 2
a113 2
    </Para>
    <Para>
d116 1
a116 1
	given to the handler is the object ID in the <FileName>pg_proc</FileName>
d121 1
a121 1
	<FileName>pg_proc</FileName>.
d126 3
a128 3
    </Para>
    <Para>
        Procedural languages defined in the <FileName>template1</FileName>
d132 4
a135 4
    </Para>
    </Step>
</Procedure>
</Sect1> <!-- **** End of PL installation **** -->
d142 2
a143 2
<Sect1>
<Title>PL/pgSQL</Title>
d145 1
a145 1
<Para>
d147 2
a148 2
    <ProductName>Postgres</ProductName> database system.
</Para>
d150 1
a150 1
<Para>
d152 1
a152 1
</Para>
d156 2
a157 2
<Sect2>
<Title>Overview</Title>
d159 1
a159 1
<Para>
d162 3
a164 3
    <ItemizedList>
    <ListItem>
	<Para>
d166 9
a174 9
	</Para>
    </ListItem>
    <ListItem>
	<Para>
        adds control structures to the <Acronym>SQL</Acronym> language,
	</Para>
    </ListItem>
    <ListItem>
	<Para>
d176 4
a179 4
	</Para>
    </ListItem>
    <ListItem>
	<Para>
d181 4
a184 4
	</Para>
    </ListItem>
    <ListItem>
	<Para>
d186 4
a189 4
	</Para>
    </ListItem>
    <ListItem>
	<Para>
d191 5
a195 5
	</Para>
    </ListItem>
    </ItemizedList>
</Para>
<Para>
d202 3
a204 3
</Para>
<Para>
    For all expressions and <Acronym>SQL</Acronym> statements used in
d213 2
a214 2
</Para>
<Para>
d220 2
a221 2
</Para>
</Sect2>
d225 2
a226 2
<Sect2>
<Title>Description</Title>
d230 2
a231 2
<Sect3>
<Title>Structure of PL/pgSQL</Title>
d233 1
a233 1
<Para>
d236 2
a237 2
</Para>
<Para>
d240 1
a240 1
<ProgramListing>
d247 1
a247 1
</ProgramListing>
d255 1
a255 1
</Para>
d257 1
a257 1
<Para>
d261 1
a261 1
    start or commit transactions and <ProductName>Postgres</ProductName>
d263 2
a264 2
</Para>
</Sect3>
d268 2
a269 2
<Sect3>
<Title>Comments</Title>
d271 1
a271 1
<Para>
d278 2
a279 2
</Para>
</Sect3>
d283 2
a284 2
<Sect3>
<Title>Declarations</Title>
d286 1
a286 1
<Para>
d293 2
a294 2
</Para>
<VariableList>
d296 8
a303 6
<VarListEntry>
<Term>
<Replaceable>name</Replaceable> [ CONSTANT ] <Replaceable>type</Replaceable> [ NOT NULL ] [ DEFAULT | := <Replaceable>value</Replaceable> ];
</Term>
<ListItem>
<Para>
d308 1
a308 1
    <Acronym>SQL</Acronym> NULL value, all variables declared as NOT NULL
d310 2
a311 2
</Para>
<Para>
d313 2
a314 2
    assigning '<Replaceable>now</Replaceable>' to a variable of type
    <Replaceable>datetime</Replaceable> causes the variable to have the
d317 10
a326 10
</Para>
</ListItem>
</VarListEntry>

<VarListEntry>
<Term>
<Replaceable>name</Replaceable> <Replaceable>class</Replaceable>%ROWTYPE;
</Term>
<ListItem>
<Para>
d336 2
a337 2
</Para>
<Para>
d340 10
a349 10
</Para>
</ListItem>
</VarListEntry>

<VarListEntry>
<Term>
<Replaceable>name</Replaceable> RECORD;
</Term>
<ListItem>
<Para>
d356 2
a357 2
</Para>
<Para>
d359 1
a359 1
    records. This is necessary because in <ProductName>Postgres</ProductName>
d362 10
a371 10
</Para>
</ListItem>
</VarListEntry>

<VarListEntry>
<Term>
<Replaceable>name</Replaceable> ALIAS FOR $n;
</Term>
<ListItem>
<Para>
d374 2
a375 2
</Para>
<Para>
d379 10
a388 10
</Para>
</ListItem>
</VarListEntry>

<VarListEntry>
<Term>
RENAME <Replaceable>oldname</Replaceable> TO <Replaceable>newname</Replaceable>;
</Term>
<ListItem>
<Para>
d392 3
a394 3
</Para>
</ListItem>
</VarListEntry>
d396 2
a397 2
</VariableList>
</Sect3>
d401 2
a402 2
<Sect3>
<Title>Data Types</Title>
d404 1
a404 1
<Para>
d406 1
a406 1
    the database. <Replaceable>type</Replaceable> in the declarations
d408 22
a429 22
</Para>
<Para>
    <ItemizedList>
    <ListItem>
        <Para>
	<ProductName>Postgres</ProductName>-basetype
	</Para>
    </ListItem>
    <ListItem>
        <Para>
	<Replaceable>variable</Replaceable>%TYPE
	</Para>
    </ListItem>
    <ListItem>
        <Para>
	<Replaceable>class.field</Replaceable>%TYPE
	</Para>
    </ListItem>
    </ItemizedList>
</Para>
<Para>
    <Replaceable>variable</Replaceable> is the name of a variable,
d432 4
a435 4
</Para>
<Para>
    <Replaceable>class</Replaceable> is the name of an existing table
    or view where <Replaceable>field</Replaceable> is the name of
d437 3
a439 3
</Para>
<Para>
    Using the <Replaceable>class.field</Replaceable>%TYPE
d449 1
a449 1
    <Replaceable>class.field</Replaceable>%TYPE
d452 2
a453 2
</Para>
</Sect3>
d457 2
a458 2
<Sect3>
<Title>Expressions</Title>
d460 1
a460 1
<Para>
d468 3
a470 3
    <ProgramListing>
    SELECT <Replaceable>expression</Replaceable>
    </ProgramListing>
d476 2
a477 2
</Para>
<Para>
d483 1
a483 1
    <ProgramListing>
d492 1
a492 1
    </ProgramListing>
d496 1
a496 1
    <ProgramListing>
d507 1
a507 1
    </ProgramListing>
d509 1
a509 1
    do. In the case of logfunc1(), the <ProductName>Postgres</ProductName>
d517 3
a519 3
</Para>
<Para>
    In the case of logfunc2(), the <ProductName>Postgres</ProductName> 
d526 3
a528 3
</Para>
<Para>
    This type checking done by the <ProductName>Postgres</ProductName> main
d535 2
a536 2
</Para>
<Para>
d541 2
a542 2
</Para>
</Sect3>
d546 2
a547 2
<Sect3>
<Title>Statements</Title>
d549 1
a549 1
<Para>
d553 1
a553 1
</Para>
d555 1
a555 1
<VariableList>
d557 2
a558 2
<VarListEntry>
<Term>
d560 3
a562 3
</Term>
<ListItem>
<Para>
d565 3
a567 3
    <ProgramListing>
    <Replaceable>identifier</Replaceable> := <Replaceable>expression</Replaceable>;
    </ProgramListing>
d574 2
a575 2
</Para>
<Para>
d578 4
a581 4
    <ProgramListing>
    SELECT <Replaceable>expressions</Replaceable> INTO <Replaceable>target</Replaceable> FROM ...;
    </ProgramListing>
    <Replaceable>target</Replaceable> can be a record, a row variable or a
d583 2
a584 2
</Para>
<Para>
d589 2
a590 2
</Para>
<Para>
d594 1
a594 1
    <ProgramListing>
d599 1
a599 1
    </ProgramListing>
d603 3
a605 3
</Para>
</ListItem>
</VarListEntry>
d607 2
a608 2
<VarListEntry>
<Term>
d610 4
a613 4
</Term>
<ListItem>
<Para>
    All functions defined in a <ProductName>Prostgres</ProductName>
d618 4
a621 4
    <ProgramListing>
    PERFORM <Replaceable>query</Replaceable>
    </ProgramListing>
    executes a 'SELECT <Replaceable>query</Replaceable>' over the
d624 3
a626 3
</Para>
</ListItem>
</VarListEntry>
d628 2
a629 2
<VarListEntry>
<Term>
d631 7
a637 7
</Term>
<ListItem>
<Para>
    <ProgramListing>
    RETURN <Replaceable>expression</Replaceable>
    </ProgramListing>
    The function terminates and the value of <Replaceable>expression</Replaceable>
d642 2
a643 2
</Para>
<Para>
d646 3
a648 3
</Para>
</ListItem>
</VarListEntry>
d650 2
a651 2
<VarListEntry>
<Term>
d653 3
a655 3
</Term>
<ListItem>
<Para>
d657 1
a657 1
    can throw messages into the <ProductName>Postgres</ProductName>
d659 5
a663 3
    <ProgramListing>
    RAISE <replaceable class="parameter">level</replaceable> ''<replaceable class="parameter">format</replaceable>'' [, <replaceable class="parameter">identifier</replaceable> [...]];
    </ProgramListing>
d669 3
a671 3
</Para>
</ListItem>
</VarListEntry>
d673 2
a674 2
<VarListEntry>
<Term>
d676 5
a680 5
</Term>
<ListItem>
<Para>
    <ProgramListing>
    IF <Replaceable>expression</Replaceable> THEN
d685 2
a686 2
    </ProgramListing>
    The <Replaceable>expression</Replaceable> must return a value that
d688 3
a690 3
</Para>
</ListItem>
</VarListEntry>
d692 2
a693 2
<VarListEntry>
<Term>
d695 3
a697 3
</Term>
<ListItem>
<Para>
d699 1
a699 1
    <ProgramListing>
d704 1
a704 1
    </ProgramListing>
d709 1
a709 1
    <ProgramListing>
d711 1
a711 1
    WHILE <Replaceable>expression</Replaceable> LOOP
d714 1
a714 1
    </ProgramListing>
d716 2
a717 2
    of <Replaceable>expression</Replaceable> is true.
    <ProgramListing>
d719 2
a720 1
    FOR <Replaceable>name</Replaceable> IN [ REVERSE ] <Replaceable>expression</Replaceable> .. <Replaceable>expression</Replaceable> LOOP
d723 1
a723 1
    </ProgramListing>
d725 1
a725 1
    <Replaceable>name</Replaceable> is automatically created as type
d729 1
a729 1
    <ProgramListing>
d731 1
a731 1
    FOR <Replaceable>record | row</Replaceable> IN <Replaceable>select_clause</Replaceable> LOOP
d734 1
a734 1
    </ProgramListing>
d739 4
a742 4
    <ProgramListing>
    EXIT [ <Replaceable>label</Replaceable> ] [ WHEN <Replaceable>expression</Replaceable> ];
    </ProgramListing>
    If no <Replaceable>label</Replaceable> given,
d745 1
a745 1
 If <Replaceable>label</Replaceable> is given, it
d750 3
a752 3
</Para>
</ListItem>
</VarListEntry>
d754 1
a754 1
</VariableList>
d756 1
a756 1
</Sect3>
d760 2
a761 2
<Sect3>
<Title>Trigger Procedures</Title>
d763 1
a763 1
<Para>
d767 3
a769 3
</Para>
<Para>
    There are some <ProductName>Postgres</ProductName> specific details
d771 2
a772 2
</Para>
<Para>
d775 1
a775 1
</Para>
d777 1
a777 1
<VariableList>
d779 2
a780 2
<VarListEntry>
<Term>
d782 3
a784 3
</Term>
<ListItem>
<Para>
d787 3
a789 3
</Para>
</ListItem>
</VarListEntry>
d791 2
a792 2
<VarListEntry>
<Term>
d794 3
a796 3
</Term>
<ListItem>
<Para>
d799 3
a801 3
</Para>
</ListItem>
</VarListEntry>
d803 2
a804 2
<VarListEntry>
<Term>
d806 3
a808 3
</Term>
<ListItem>
<Para>
d811 3
a813 3
</Para>
</ListItem>
</VarListEntry>
d815 2
a816 2
<VarListEntry>
<Term>
d818 3
a820 3
</Term>
<ListItem>
<Para>
d823 3
a825 3
</Para>
</ListItem>
</VarListEntry>
d827 2
a828 2
<VarListEntry>
<Term>
d830 3
a832 3
</Term>
<ListItem>
<Para>
d835 3
a837 3
</Para>
</ListItem>
</VarListEntry>
d839 2
a840 2
<VarListEntry>
<Term>
d842 3
a844 3
</Term>
<ListItem>
<Para>
d847 3
a849 3
</Para>
</ListItem>
</VarListEntry>
d851 2
a852 2
<VarListEntry>
<Term>
d854 3
a856 3
</Term>
<ListItem>
<Para>
d859 3
a861 3
</Para>
</ListItem>
</VarListEntry>
d863 2
a864 2
<VarListEntry>
<Term>
d866 3
a868 3
</Term>
<ListItem>
<Para>
d871 3
a873 3
</Para>
</ListItem>
</VarListEntry>
d875 2
a876 2
<VarListEntry>
<Term>
d878 3
a880 3
</Term>
<ListItem>
<Para>
d883 3
a885 3
</Para>
</ListItem>
</VarListEntry>
d887 2
a888 2
<VarListEntry>
<Term>
d890 3
a892 3
</Term>
<ListItem>
<Para>
d896 3
a898 3
</Para>
</ListItem>
</VarListEntry>
d900 1
a900 1
</VariableList>
d902 1
a902 1
<Para>
d912 2
a913 2
</Para>
</Sect3>
d917 2
a918 2
<Sect3>
<Title>Exceptions</Title>
d920 2
a921 2
<Para>
    <ProductName>Postgres</ProductName> does not have a very smart
d926 2
a927 2
</Para>
<Para>
d937 2
a938 2
</Para>
<Para>
d944 3
a946 3
</Para>
</Sect3>
</Sect2>
d950 2
a951 2
<Sect2>
<Title>Examples</Title>
d953 1
a953 1
<Para>
d957 1
a957 1
</Para>
d959 1
a959 1
<Para>
d966 1
a966 1
in future versions of <ProductName>Postgres</ProductName> will be
d968 1
a968 1
</Para>
d970 2
a971 2
<Sect3>
<Title>Some Simple PL/pgSQL Functions</Title>
d973 1
a973 1
<Para>
d977 1
a977 1
    <ProgramListing>
d983 1
a983 1
    </ProgramListing>
d985 1
a985 1
    <ProgramListing>
d991 2
a992 2
    </ProgramListing>
</Para>
d994 1
a994 1
</Sect3>
d996 2
a997 2
<Sect3>
<Title>PL/pgSQL Function on Composite Type</Title>
d999 1
a999 1
<Para>
d1003 1
a1003 1
    <ProgramListing>
d1015 2
a1016 2
    </ProgramListing>
</Para>
d1018 1
a1018 1
</Sect3>
d1020 2
a1021 2
<Sect3>
<Title>PL/pgSQL Trigger Procedure</Title>
d1023 1
a1023 1
<Para>
d1029 1
a1029 1
    <ProgramListing>
d1060 2
a1061 2
    </ProgramListing>
</Para>
d1063 1
a1063 1
</Sect3>
d1065 1
a1065 1
</Sect2>
d1067 1
a1067 1
</Sect1>
d1074 2
a1075 2
<Sect1>
<Title>PL/Tcl</Title>
d1077 1
a1077 1
<Para>
d1079 1
a1079 1
    <ProductName>Postgres</ProductName> database system
d1082 1
a1082 1
</Para>
d1084 1
a1084 1
<Para>
d1086 1
a1086 1
</Para>
d1090 2
a1091 2
<Sect2>
<Title>Overview</Title>
d1093 1
a1093 1
<Para>
d1096 2
a1097 2
</Para>
<Para>
d1103 1
a1103 1
    <ProductName>Postgres</ProductName> user ID like in C.
d1106 2
a1107 2
</Para>
<Para>
d1110 2
a1111 2
</Para>
<Para>
d1113 1
a1113 1
    and installed in the <ProductName>Postgres</ProductName>
d1116 2
a1117 2
</Para>
</Sect2>
d1121 2
a1122 2
<Sect2>
<Title>Description</Title>
d1124 2
a1125 2
<Sect3>
<Title><ProductName>Postgres</ProductName> Functions and Tcl Procedure Names</Title>
d1127 2
a1128 2
<Para>
    In <ProductName>Postgres</ProductName>, one and the 
d1134 1
a1134 1
    argtype versions of the same <ProductName>Postgres</ProductName> 
d1136 1
a1136 1
</Para>
d1138 1
a1138 1
</Sect3>
d1140 2
a1141 2
<Sect3>
<Title>Defining Functions in PL/Tcl</Title>
d1143 1
a1143 1
<Para>
d1146 4
a1149 2
    <ProgramListing>
    CREATE FUNCTION <Replaceable>funcname</Replaceable> (<Replaceable>argument-types</Replaceable>) RETURNS <Replaceable>returntype</Replaceable> AS '
d1152 1
a1152 1
    </ProgramListing>
d1158 1
a1158 1
    <ProgramListing>
d1163 1
a1163 1
    </ProgramListing>
d1171 1
a1171 1
    older <ProductName>Postgres</ProductName> documentation) in PL/Tcl
d1173 1
a1173 1
    <ProgramListing>
d1183 2
a1184 2
    </ProgramListing>
</Para>
d1186 1
a1186 1
</Sect3>
d1188 2
a1189 2
<Sect3>
<Title>Global Data in PL/Tcl</Title>
d1191 1
a1191 1
<Para>
d1201 2
a1202 2
</Para>
</Sect3>
d1204 2
a1205 2
<Sect3>
<Title>Trigger Procedures in PL/Tcl</Title>
d1207 2
a1208 2
<Para>
    Trigger procedures are defined in <ProductName>Postgres</ProductName>
d1212 2
a1213 2
</Para>
<Para>
d1216 2
a1217 2
</Para>
<VariableList>
d1219 2
a1220 2
<VarListEntry>
<Term><Replaceable class="Parameter">
d1222 3
a1224 3
</Replaceable></Term>
<ListItem>
<Para>
d1226 3
a1228 3
</Para>
</ListItem>
</VarListEntry>
d1230 2
a1231 2
<VarListEntry>
<Term><Replaceable class="Parameter">
d1233 3
a1235 3
</Replaceable></Term>
<ListItem>
<Para>
d1238 3
a1240 3
</Para>
</ListItem>
</VarListEntry>
d1242 2
a1243 2
<VarListEntry>
<Term><Replaceable class="Parameter">
d1245 3
a1247 3
</Replaceable></Term>
<ListItem>
<Para>
d1252 3
a1254 3
</Para>
</ListItem>
</VarListEntry>
d1256 2
a1257 2
<VarListEntry>
<Term><Replaceable class="Parameter">
d1259 3
a1261 3
</Replaceable></Term>
<ListItem>
<Para>
d1263 3
a1265 3
</Para>
</ListItem>
</VarListEntry>
d1267 2
a1268 2
<VarListEntry>
<Term><Replaceable class="Parameter">
d1270 3
a1272 3
</Replaceable></Term>
<ListItem>
<Para>
d1274 3
a1276 3
</Para>
</ListItem>
</VarListEntry>
d1278 2
a1279 2
<VarListEntry>
<Term><Replaceable class="Parameter">
d1281 3
a1283 3
</Replaceable></Term>
<ListItem>
<Para>
d1286 3
a1288 3
</Para>
</ListItem>
</VarListEntry>
d1290 2
a1291 2
<VarListEntry>
<Term><Replaceable class="Parameter">
d1293 3
a1295 3
</Replaceable></Term>
<ListItem>
<Para>
d1298 3
a1300 3
</Para>
</ListItem>
</VarListEntry>
d1302 2
a1303 2
<VarListEntry>
<Term><Replaceable class="Parameter">
d1305 3
a1307 3
</Replaceable></Term>
<ListItem>
<Para>
d1310 3
a1312 3
</Para>
</ListItem>
</VarListEntry>
d1314 2
a1315 2
<VarListEntry>
<Term><Replaceable class="Parameter">
d1317 3
a1319 3
</Replaceable></Term>
<ListItem>
<Para>
d1321 3
a1323 3
</Para>
</ListItem>
</VarListEntry>
d1325 2
a1326 2
<VarListEntry>
<Term><Replaceable class="Parameter">
d1328 3
a1330 3
</Replaceable></Term>
<ListItem>
<Para>
d1334 3
a1336 3
</Para>
</ListItem>
</VarListEntry>
d1338 1
a1338 1
</VariableList>
d1340 1
a1340 1
<Para>
d1349 2
a1350 2
</Para>
<Para>
d1356 1
a1356 1
    <ProgramListing>
d1377 1
a1377 1
    </ProgramListing>
d1379 2
a1380 2
</Para>
</Sect3>
d1382 2
a1383 2
<Sect3>
<Title>Database Access from PL/Tcl</Title>
d1385 1
a1385 1
<Para>
d1388 1
a1388 1
</Para>
d1390 1
a1390 1
<VariableList>
d1392 6
a1397 6
<VarListEntry>
<Term>
elog <Replaceable>level</Replaceable> <Replaceable>msg</Replaceable>
</Term>
<ListItem>
<Para>
d1401 10
a1410 10
</Para>
</ListItem>
</VarListEntry>

<VarListEntry>
<Term>
quote <Replaceable>string</Replaceable>
</Term>
<ListItem>
<Para>
d1416 1
a1416 1
    <ProgramListing>
d1418 1
a1418 1
    </ProgramListing>
d1423 1
a1423 1
    <ProgramListing>
d1425 1
a1425 1
    </ProgramListing>
d1430 1
a1430 1
    <ProgramListing>
d1432 1
a1432 1
    </ProgramListing>
d1436 1
a1436 1
    <ProgramListing>
d1438 12
a1449 11
    </ProgramListing>
</Para>
</ListItem>
</VarListEntry>

<VarListEntry>
<Term>
spi_exec ?-count <Replaceable>n</Replaceable>? ?-array <Replaceable>name</Replaceable>? <Replaceable>query</Replaceable> ?<Replaceable>loop-body</Replaceable>?
</Term>
<ListItem>
<Para>
d1453 2
a1454 2
</Para>
<Para>
d1461 1
a1461 1
    <ProgramListing>
d1463 1
a1463 1
    </ProgramListing>
d1470 1
a1470 1
    <ProgramListing>
d1474 1
a1474 1
    </ProgramListing>
d1479 10
a1488 10
</Para>
</ListItem>
</VarListEntry>

<VarListEntry>
<Term>
spi_prepare <Replaceable>query</Replaceable> <Replaceable>typelist</Replaceable>
</Term>
<ListItem>
<Para>
d1493 2
a1494 2
</Para>
<Para>
d1498 11
a1508 10
</Para>
</ListItem>
</VarListEntry>

<VarListEntry>
<Term>
spi_exec ?-count <Replaceable>n</Replaceable>? ?-array <Replaceable>name</Replaceable>? ?-nulls <Replaceable>str</Replaceable>? <Replaceable>query</Replaceable> ?<Replaceable>valuelist</Replaceable>? ?<Replaceable>loop-body</Replaceable>?
</Term>
<ListItem>
<Para>
d1512 2
a1513 2
</Para>
<Para>
d1517 2
a1518 2
</Para>
<Para>
d1520 2
a1521 2
</Para>
<Para>
d1525 2
a1526 2
</Para>
<Para>
d1529 2
a1530 2
</Para>
<Para>
d1533 1
a1533 1
    <ProgramListing>
d1544 1
a1544 1
    </ProgramListing>
d1552 3
a1554 3
</Para>
</ListItem>
</VarListEntry>
d1556 2
a1557 2
<VarListEntry>
<Term>
d1559 3
a1561 3
</Term>
<ListItem>
<Para>
d1570 2
a1571 2
</Para>
<Para>
d1575 28
a1602 13
</Para>
</ListItem>
</VarListEntry>

</VariableList>

</Sect3>

</Sect2>
</Sect1>


</Chapter>
@


1.3
log
@Minor editing and markup changes as a result of preparing the Postscript
 documentation for v6.4.
Bigger updates to the installation instructions (install and config).
@
text
@d31 1
a31 2
<Para>
    <Procedure>
d38 1
a38 1

d84 3
a86 3
    </Procedure>
<Para>
    <Procedure>
d134 1
a134 1
    </Procedure>
a369 1
<Para>
a385 1
<Para>
@


1.2
log
@Small adjustments in markup getting ready for hardcopy release.
Jan did a nice job of initial markup so it was pretty easy!
@
text
@d2 1
a2 1
<Title>Extending <Acronym>SQL</Acronym>: Procedural languages</Title>
@


1.1
log
@Programming Language info from Jan Wieck.
Not yet marked up or included in a document, but it should be checked in
 to be safe.
@
text
@d10 2
a11 1
    Since version 6.3 <ProductName>Postgres</ProductName> supports
d29 1
a29 1
<Title>Installing procedural languages</Title>
d34 4
d39 1
a39 1
    </Title>
d71 1
a71 1
    CREATE [TRUSTED] PROCEDURAL LANGUAGE '<Replaceable>language-name</Replaceable>'
d161 1
a161 1
    The design rules of PL/pgSQL where to create a loadable procedural
d186 1
a186 1
        can be defined trusted,
d236 1
a236 1
    identifiers can be used in upper-/lowercase mixed.
d244 1
a244 1
        -- declarations]
d246 1
a246 1
        -- statements
d299 1
a299 1
<Replaceable>name</Replaceable> [CONSTANT] <Replaceable>type</Replaceable> [NOT NULL] [DEFAULT | := <Replaceable>value</Replaceable>];
d322 1
a322 1
<Replaceable>name</Replaceable> <Replaceable>class</Replaceable>&percnt;ROWTYPE;
d403 1
a403 1
<Title>Data types</Title>
d419 1
a419 1
	<Replaceable>variable</Replaceable>&percnt;TYPE
d424 1
a424 1
	<Replaceable>class.field</Replaceable>&percnt;TYPE
d430 3
a432 2
    <Replaceable>variable</Replaceable> is the name of a previously in the 
    same function declared variable that is visible at this point.
d440 1
a440 1
    Using the <Replaceable>class.field</Replaceable>&percnt;TYPE
d450 1
a450 1
    <Replaceable>class.field</Replaceable>&percnt;TYPE
d463 4
a466 3
    the backends executor. Since even a constant looking expression
    can have a totally different meaning for a particular data type
    (as 'now' for datetime), it is impossible for the PL/pgSQL parser
d472 1
a472 1
    over the SPI manager. In the expression, occurences of variable
d479 2
a480 1
    The type checking done by the postgres main parser has some side
d564 1
a564 1
    An assignment of a value to a varable or row/record field is
d598 1
a598 1
        RAISE EXCEPTION ''employee &percnt; not found'', myname;
d661 1
a661 1
    RAISE level ''format'' [, identifier [...]];
d663 3
a665 3
    Inside the format, &percnt; is used as a placeholder for the
    following, comma separated identifiers. Possible levels are
    DEBUG (silently suppressed in productional running databases), NOTICE 
d667 1
a667 1
    and EXCEPTION (written into the database log and aborting the transaction). 
d680 1
a680 1
        -- statements
d682 1
a682 1
        -- statements]
d701 1
a701 1
        -- statements
d711 1
a711 1
        -- statements
d718 2
a719 2
    FOR <Replaceable>name</Replaceable> IN [REVERSE] <Replaceable>expression</Replaceable> .. <Replaceable>expression</Replaceable> LOOP
        -- statements
d730 1
a730 1
        -- statements
d738 1
a738 1
    EXIT [label] [WHEN <Replaceable>expression</Replaceable>];
d740 4
a743 2
    If no label given, the innermost loop is terminated and the
    statement following END LOOP is executed next. If label is given, it
d759 1
a759 1
<Title>Trigger procedures</Title>
d969 1
a969 1
<Title>Some simple PL/pgSQL functions</Title>
d995 1
a995 1
<Title>PL/pgSQL function on composite type</Title>
d1019 1
a1019 1
<Title>PL/pgSQL trigger procedure</Title>
d1041 1
a1041 1
                RAISE EXCEPTION ''&percnt; cannot have NULL salary'', NEW.empname;
d1046 1
a1046 1
                RAISE EXCEPTION ''&percnt; cannot have a negative salary'', NEW.empname;
d1112 2
a1113 2
    owners library directory if the Tcl/Tk support is specified
    in the configure run.
d1123 1
a1123 1
<Title><ProductName>Postgres</ProductName> functions and Tcl procedure names</Title>
d1139 1
a1139 1
<Title>Defining functions in PL/Tcl</Title>
d1185 1
a1185 1
<Title>Global data in PL/Tcl</Title>
d1201 1
a1201 1
<Title>Trigger procedures in PL/Tcl</Title>
d1379 1
a1379 1
<Title>Database access from PL/Tcl</Title>
d1433 1
a1433 1
    "SELECT '[quote $val]' AS ret"
d1529 1
a1529 1
        if {![info exists GD(plan)]} {
d1531 1
a1531 1
            set GD(plan) [spi_prepare \\
d1533 1
a1533 1
                    int4]
d1535 1
a1535 1
        spi_execp -count 1 $GD(plan) [list $1 $2]
@
