11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.64 2005/01/04 00:39:53 tgl Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.65 2005/03/31 22:45:59 tgl Exp $
33-->
44
55<refentry id="SQL-CREATEFUNCTION">
@@ -19,8 +19,9 @@ $PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.64 2005/01/04 00:39
1919
2020 <refsynopsisdiv>
2121<synopsis>
22- CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
23- RETURNS <replaceable class="parameter">rettype</replaceable>
22+ CREATE [ OR REPLACE ] FUNCTION
23+ <replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
24+ [ RETURNS <replaceable class="parameter">rettype</replaceable> ]
2425 { LANGUAGE <replaceable class="parameter">langname</replaceable>
2526 | IMMUTABLE | STABLE | VOLATILE
2627 | CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
@@ -57,7 +58,9 @@ CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable>
5758 tried, you would actually be creating a new, distinct function).
5859 Also, <command>CREATE OR REPLACE FUNCTION</command> will not let
5960 you change the return type of an existing function. To do that,
60- you must drop and recreate the function.
61+ you must drop and recreate the function. (When using <literal>OUT</>
62+ parameters, that means you can't change the names or types of any
63+ <literal>OUT</> parameters except by dropping the function.)
6164 </para>
6265
6366 <para>
@@ -88,14 +91,28 @@ CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable>
8891 </listitem>
8992 </varlistentry>
9093
94+ <varlistentry>
95+ <term><replaceable class="parameter">argmode</replaceable></term>
96+
97+ <listitem>
98+ <para>
99+ The mode of an argument: either <literal>IN</>, <literal>OUT</>,
100+ or <literal>INOUT</>. If omitted, the default is <literal>IN</>.
101+ </para>
102+ </listitem>
103+ </varlistentry>
104+
91105 <varlistentry>
92106 <term><replaceable class="parameter">argname</replaceable></term>
93107
94108 <listitem>
95109 <para>
96110 The name of an argument. Some languages (currently only PL/pgSQL) let
97111 you use the name in the function body. For other languages the
98- argument name is just extra documentation.
112+ name of an input argument is just extra documentation. But the name
113+ of an output argument is significant, since it defines the column
114+ name in the result row type. (If you omit the name for an output
115+ argument, the system will choose a default column name.)
99116 </para>
100117 </listitem>
101118 </varlistentry>
@@ -137,6 +154,13 @@ CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable>
137154 Depending on the implementation language it may also be allowed
138155 to specify <quote>pseudotypes</> such as <type>cstring</>.
139156 </para>
157+ <para>
158+ When there are <literal>OUT</> or <literal>INOUT</> parameters,
159+ the <literal>RETURNS</> clause may be omitted. If present, it
160+ must agree with the result type implied by the output parameters:
161+ <literal>RECORD</> if there are multiple output parameters, or
162+ the same type as the single output parameter.
163+ </para>
140164 <para>
141165 The <literal>SETOF</literal>
142166 modifier indicates that the function will return a set of
@@ -361,6 +385,16 @@ CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable>
361385 names).
362386 </para>
363387
388+ <para>
389+ Two functions are considered the same if they have the same names and
390+ <emphasis>input</> argument types, ignoring any <literal>OUT</>
391+ parameters. Thus for example these declarations conflict:
392+ <programlisting>
393+ CREATE FUNCTION foo(int) ...
394+ CREATE FUNCTION foo(int, out text) ...
395+ </programlisting>
396+ </para>
397+
364398 <para>
365399 When repeated <command>CREATE FUNCTION</command> calls refer to
366400 the same object file, the file is only loaded once. To unload and
@@ -393,7 +427,7 @@ CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable>
393427 <title>Examples</title>
394428
395429 <para>
396- Hereis a trivialexample to help you get started. For more
430+ Hereare some trivialexamples to help you get started. For more
397431 information and examples, see <xref linkend="xfunc">.
398432<programlisting>
399433CREATE FUNCTION add(integer, integer) RETURNS integer
@@ -407,13 +441,34 @@ CREATE FUNCTION add(integer, integer) RETURNS integer
407441 <para>
408442 Increment an integer, making use of an argument name, in
409443 <application>PL/pgSQL</application>:
410-
411444<programlisting>
412445CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS $$
413446 BEGIN
414447 RETURN i + 1;
415448 END;
416449$$ LANGUAGE plpgsql;
450+ </programlisting>
451+ </para>
452+
453+ <para>
454+ Return a record containing multiple output parameters:
455+ <programlisting>
456+ CREATE FUNCTION dup(in int, out f1 int, out f2 text)
457+ AS $$ SELECT $1, CAST($1 AS text) || ' is text' $$
458+ LANGUAGE SQL;
459+
460+ SELECT * FROM dup(42);
461+ </programlisting>
462+ You can do the same thing more verbosely with an explicitly named
463+ composite type:
464+ <programlisting>
465+ CREATE TYPE dup_result AS (f1 int, f2 text);
466+
467+ CREATE FUNCTION dup(int) RETURNS dup_result
468+ AS $$ SELECT $1, CAST($1 AS text) || ' is text' $$
469+ LANGUAGE SQL;
470+
471+ SELECT * FROM dup(42);
417472</programlisting>
418473 </para>
419474 </refsect1>
@@ -428,6 +483,13 @@ $$ LANGUAGE plpgsql;
428483 not fully compatible. The attributes are not portable, neither are the
429484 different available languages.
430485 </para>
486+
487+ <para>
488+ For compatibility with some other database systems,
489+ <replaceable class="parameter">argmode</replaceable> can be written
490+ either before or after <replaceable class="parameter">argname</replaceable>.
491+ But only the first way is standard-compliant.
492+ </para>
431493 </refsect1>
432494
433495