<!-- doc/src/sgml/xfunc.sgml -->
<sect1 id="xfunc">
<title>User-Defined Functions</title>
<indexterm zone="xfunc">
<primary>function</primary>
<secondary>user-defined</secondary>
</indexterm>
<para>
<productname>PostgreSQL</productname> provides four kinds of
functions:
<itemizedlist>
<listitem>
<para>
query language functions (functions written in
<acronym>SQL</acronym>) (<xref linkend="xfunc-sql"/>)
</para>
</listitem>
<listitem>
<para>
procedural language functions (functions written in, for
example, <application>PL/pgSQL</application> or <application>PL/Tcl</application>)
(<xref linkend="xfunc-pl"/>)
</para>
</listitem>
<listitem>
<para>
internal functions (<xref linkend="xfunc-internal"/>)
</para>
</listitem>
<listitem>
<para>
C-language functions (<xref linkend="xfunc-c"/>)
</para>
</listitem>
</itemizedlist>
</para>
<para>
Every kind
of function can take base types, composite types, or
combinations of these as arguments (parameters). In addition,
every kind of function can return a base type or
a composite type. Functions can also be defined to return
sets of base or composite values.
</para>
<para>
Many kinds of functions can take or return certain pseudo-types
(such as polymorphic types), but the available facilities vary.
Consult the description of each kind of function for more details.
</para>
<para>
It's easiest to define <acronym>SQL</acronym>
functions, so we'll start by discussing those.
Most of the concepts presented for <acronym>SQL</acronym> functions
will carry over to the other types of functions.
</para>
<para>
Throughout this chapter, it can be useful to look at the reference
page of the <link linkend="sql-createfunction"><command>CREATE
FUNCTION</command></link> command to
understand the examples better. Some examples from this chapter
can be found in <filename>funcs.sql</filename> and
<filename>funcs.c</filename> in the <filename>src/tutorial</filename>
directory in the <productname>PostgreSQL</productname> source
distribution.
</para>
</sect1>
<sect1 id="xproc">
<title>User-Defined Procedures</title>
<indexterm zone="xproc">
<primary>procedure</primary>
<secondary>user-defined</secondary>
</indexterm>
<para>
A procedure is a database object similar to a function.
The key differences are:
<itemizedlist>
<listitem>
<para>
Procedures are defined with
the <link linkend="sql-createprocedure"><command>CREATE
PROCEDURE</command></link> command, not <command>CREATE
FUNCTION</command>.
</para>
</listitem>
<listitem>
<para>
Procedures do not return a function value; hence <command>CREATE
PROCEDURE</command> lacks a <literal>RETURNS</literal> clause.
However, procedures can instead return data to their callers via
output parameters.
</para>
</listitem>
<listitem>
<para>
While a function is called as part of a query or DML command, a
procedure is called in isolation using
the <link linkend="sql-call"><command>CALL</command></link> command.
</para>
</listitem>
<listitem>
<para>
A procedure can commit or roll back transactions during its
execution (then automatically beginning a new transaction), so long
as the invoking <command>CALL</command> command is not part of an
explicit transaction block. A function cannot do that.
</para>
</listitem>
<listitem>
<para>
Certain function attributes, such as strictness, don't apply to
procedures. Those attributes control how the function is
used in a query, which isn't relevant to procedures.
</para>
</listitem>
</itemizedlist>
</para>
<para>
The explanations in the