]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/create_function.sgml
7d8d01f4cd36416e7af79d857e17cc5c9d7d1b69
[postgresql] / doc / src / sgml / ref / create_function.sgml
1 <!--
2 $PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.63 2004/11/27 21:27:07 petere Exp $
3 -->
4
5 <refentry id="SQL-CREATEFUNCTION">
6  <refmeta>
7   <refentrytitle id="SQL-CREATEFUNCTION-TITLE">CREATE FUNCTION</refentrytitle>
8   <refmiscinfo>SQL - Language Statements</refmiscinfo>
9  </refmeta>
10
11  <refnamediv>
12   <refname>CREATE FUNCTION</refname>
13   <refpurpose>define a new function</refpurpose>
14  </refnamediv>
15
16  <indexterm zone="sql-createfunction">
17   <primary>CREATE FUNCTION</primary>
18  </indexterm>
19
20  <refsynopsisdiv>
21 <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>
24   { LANGUAGE <replaceable class="parameter">langname</replaceable>
25     | IMMUTABLE | STABLE | VOLATILE
26     | CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
27     | [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
28     | AS '<replaceable class="parameter">definition</replaceable>'
29     | AS '<replaceable class="parameter">obj_file</replaceable>', '<replaceable class="parameter">link_symbol</replaceable>'
30   } ...
31     [ WITH ( <replaceable class="parameter">attribute</replaceable> [, ...] ) ]
32 </synopsis>
33  </refsynopsisdiv>
34   
35  <refsect1 id="sql-createfunction-description">
36   <title>Description</title>
37
38   <para>
39    <command>CREATE FUNCTION</command> defines a new function.
40    <command>CREATE OR REPLACE FUNCTION</command> will either create a
41    new function, or replace an existing definition.
42   </para>
43
44   <para>
45    If a schema name is included, then the function is created in the
46    specified schema.  Otherwise it is created in the current schema.
47    The name of the new function must not match any existing function
48    with the same argument types in the same schema.  However,
49    functions of different argument types may share a name (this is
50    called <firstterm>overloading</>).
51   </para>
52
53   <para>
54    To update the definition of an existing function, use
55    <command>CREATE OR REPLACE FUNCTION</command>.  It is not possible
56    to change the name or argument types of a function this way (if you
57    tried, you would actually be creating a new, distinct function).
58    Also, <command>CREATE OR REPLACE FUNCTION</command> will not let
59    you change the return type of an existing function.  To do that,
60    you must drop and recreate the function.
61   </para>
62
63   <para>
64    If you drop and then recreate a function, the new function is not
65    the same entity as the old; you will break existing rules, views,
66    triggers, etc. that referred to the old function.  Use
67    <command>CREATE OR REPLACE FUNCTION</command> to change a function
68    definition without breaking objects that refer to the function.
69   </para>
70
71   <para>
72    The user that creates the function becomes the owner of the function.
73   </para>
74  </refsect1>
75
76  <refsect1>
77   <title>Parameters</title>
78
79    <variablelist>
80
81     <varlistentry>
82      <term><replaceable class="parameter">name</replaceable></term>
83
84      <listitem>
85       <para>
86        The name (optionally schema-qualified) of the function to create.
87       </para>
88      </listitem>
89     </varlistentry>
90
91     <varlistentry>
92      <term><replaceable class="parameter">argname</replaceable></term>
93
94      <listitem>
95       <para>
96        The name of an argument. Some languages (currently only PL/pgSQL) let
97        you use the name in the function body.  For other languages the
98        argument name is just extra documentation.
99       </para>
100      </listitem>
101     </varlistentry>
102
103     <varlistentry>
104      <term><replaceable class="parameter">argtype</replaceable></term>
105
106      <listitem>
107       <para>
108        The data type(s) of the function's arguments (optionally 
109        schema-qualified), if any. The argument types may be base, complex, or 
110        domains, or copy the type of an existing column.
111       </para>
112       <para>
113        The type of a column is referenced by writing
114        <literal><replaceable
115        class="parameter">tablename</replaceable>.<replaceable
116        class="parameter">columnname</replaceable>%TYPE</literal>;
117        using this can sometimes help make a function independent from
118        changes to the definition of a table.
119       </para>
120       <para>
121        Depending on the implementation language it may also be allowed
122        to specify <quote>pseudotypes</> such as <type>cstring</>.
123        Pseudotypes indicate that the actual argument type is either
124        incompletely specified, or outside the set of ordinary SQL data types.
125       </para>
126      </listitem>
127     </varlistentry>
128
129     <varlistentry>
130      <term><replaceable class="parameter">rettype</replaceable></term>
131
132      <listitem>
133       <para>
134        The return data type (optionally schema-qualified). The return type 
135        may be a base type, complex type, or a domain,
136        or may be specified to copy the type of an existing column. See the description
137        under <literal>argtype</literal> above on how to reference the type
138        of an existing column.
139       </para>
140       <para>
141        Depending on the implementation language it may also be allowed
142        to specify <quote>pseudotypes</> such as <type>cstring</>.
143        The <literal>SETOF</literal>
144        modifier indicates that the function will return a set of
145        items, rather than a single item.
146       </para>
147      </listitem>
148     </varlistentry>
149
150     <varlistentry>
151      <term><replaceable class="parameter">langname</replaceable></term>
152
153      <listitem>
154       <para>
155        The name of the language that the function is implemented in.
156        May be <literal>SQL</literal>, <literal>C</literal>,
157        <literal>internal</literal>, or the name of a user-defined
158        procedural language.  (See also <xref linkend="app-createlang"
159        endterm="app-createlang-title">.)  For backward compatibility,
160        the name may be enclosed by single quotes.
161       </para>
162      </listitem>
163     </varlistentry>
164
165     <varlistentry>
166      <term><literal>IMMUTABLE</literal></term>
167      <term><literal>STABLE</literal></term>
168      <term><literal>VOLATILE</literal></term>
169
170      <listitem>
171       <para>
172        These attributes inform the system whether it is safe to
173        replace multiple evaluations of the function with a single
174        evaluation, for run-time optimization.  At most one choice
175        may be specified.  If none of these appear,
176        <literal>VOLATILE</literal> is the default assumption.
177       </para>
178
179       <para>
180        <literal>IMMUTABLE</literal> indicates that the function always
181        returns the same result when given the same argument values; that
182        is, it does not do database lookups or otherwise use information not
183        directly present in its argument list.  If this option is given,
184        any call of the function with all-constant arguments can be
185        immediately replaced with the function value.
186       </para>
187
188       <para>
189        <literal>STABLE</literal> indicates that within a single table scan
190        the function will consistently
191        return the same result for the same argument values, but that its
192        result could change across SQL statements.  This is the appropriate
193        selection for functions whose results depend on database lookups,
194        parameter variables (such as the current time zone), etc.  Also note
195        that the <function>current_timestamp</> family of functions qualify
196        as stable, since their values do not change within a transaction.
197       </para>
198
199       <para>
200        <literal>VOLATILE</literal> indicates that the function value can
201        change even within a single table scan, so no optimizations can be
202        made.  Relatively few database functions are volatile in this sense;
203        some examples are <literal>random()</>, <literal>currval()</>,
204        <literal>timeofday()</>.  Note that any function that has side-effects
205        must be classified volatile, even if its result is quite predictable,
206        to prevent calls from being optimized away; an example is
207        <literal>setval()</>.
208       </para>
209
210       <para>
211        For additional details see <xref linkend="xfunc-volatility">.
212       </para>
213      </listitem>
214     </varlistentry>
215
216     <varlistentry>
217      <term><literal>CALLED ON NULL INPUT</literal></term>
218      <term><literal>RETURNS NULL ON NULL INPUT</literal></term>
219      <term><literal>STRICT</literal></term>
220
221      <listitem>
222       <para>
223        <literal>CALLED ON NULL INPUT</literal> (the default) indicates
224        that the function will be called normally when some of its
225        arguments are null.  It is then the function author's
226        responsibility to check for null values if necessary and respond
227        appropriately.
228       </para>
229
230       <para>
231        <literal>RETURNS NULL ON NULL INPUT</literal> or
232        <literal>STRICT</literal> indicates that the function always
233        returns null whenever any of its arguments are null.  If this
234        parameter is specified, the function is not executed when there
235        are null arguments; instead a null result is assumed
236        automatically.
237       </para>
238      </listitem>
239     </varlistentry>
240
241    <varlistentry>
242     <term><literal><optional>EXTERNAL</optional> SECURITY INVOKER</literal></term>
243     <term><literal><optional>EXTERNAL</optional> SECURITY DEFINER</literal></term>
244
245     <listitem>
246      <para>
247       <literal>SECURITY INVOKER</literal> indicates that the function
248       is to be executed with the privileges of the user that calls it.
249       That is the default.  <literal>SECURITY DEFINER</literal>
250       specifies that the function is to be executed with the
251       privileges of the user that created it.
252      </para>
253
254      <para>
255       The key word <literal>EXTERNAL</literal> is present for SQL
256       conformance but is optional since, unlike in SQL, this feature
257       does not only apply to external functions.
258      </para>
259     </listitem>
260    </varlistentry>
261
262     <varlistentry>
263      <term><replaceable class="parameter">definition</replaceable></term>
264
265      <listitem>
266       <para>
267        A string constant defining the function; the meaning depends on the
268        language.  It may be an internal function name, the path to an
269        object file, an SQL command, or text in a procedural language.
270       </para>
271      </listitem>
272     </varlistentry>
273
274     <varlistentry>
275      <term><literal><replaceable class="parameter">obj_file</replaceable>, <replaceable class="parameter">link_symbol</replaceable></literal></term>
276
277      <listitem>
278       <para>
279        This form of the <literal>AS</literal> clause is used for
280        dynamically loadable C language functions when the function name
281        in the C language source code is not the same as the name of
282        the SQL function. The string <replaceable
283        class="parameter">obj_file</replaceable> is the name of the
284        file containing the dynamically loadable object, and
285        <replaceable class="parameter">link_symbol</replaceable> is the
286        function's link symbol, that is, the name of the function in the C
287        language source code.  If the link symbol is omitted, it is assumed
288        to be the same as the name of the SQL function being defined.
289       </para>
290      </listitem>
291     </varlistentry>
292
293     <varlistentry>
294      <term><replaceable class="parameter">attribute</replaceable></term>
295
296      <listitem>
297       <para>
298        The historical way to specify optional pieces of information
299        about the function.  The following attributes may appear here:
300
301       <variablelist>
302        <varlistentry>
303         <term><literal>isStrict</></term>
304         <listitem>
305          <para>
306           Equivalent to <literal>STRICT</literal> or <literal>RETURNS NULL ON NULL INPUT</literal>
307          </para>
308         </listitem>
309        </varlistentry>
310
311        <varlistentry>
312         <term><literal>isCachable</></term>
313         <listitem>
314          <para>
315           <literal>isCachable</literal> is an obsolete equivalent of
316           <literal>IMMUTABLE</literal>; it's still accepted for
317           backwards-compatibility reasons.
318          </para>
319         </listitem>
320        </varlistentry>
321
322       </variablelist>
323
324       Attribute names are not case-sensitive.
325      </para>
326     </listitem>
327    </varlistentry>
328
329    </variablelist>
330
331  </refsect1>
332
333  <refsect1 id="sql-createfunction-notes">
334   <title>Notes</title>
335
336    <para>
337     Refer to <xref linkend="xfunc"> for further information on writing
338     functions.
339    </para>
340
341    <para>
342     The full <acronym>SQL</acronym> type syntax is allowed for
343     input arguments and return value. However, some details of the
344     type specification (e.g., the precision field for
345     type <type>numeric</type>) are the responsibility of the
346     underlying function implementation and are silently swallowed
347     (i.e., not recognized or
348     enforced) by the <command>CREATE FUNCTION</command> command.
349    </para>
350
351    <para>
352     <productname>PostgreSQL</productname> allows function
353     <firstterm>overloading</firstterm>; that is, the same name can be
354     used for several different functions so long as they have distinct
355     argument types.  However, the C names of all functions must be
356     different, so you must give overloaded C functions different C
357     names (for example, use the argument types as part of the C
358     names).
359    </para>
360
361    <para>
362     When repeated <command>CREATE FUNCTION</command> calls refer to
363     the same object file, the file is only loaded once.  To unload and
364     reload the file (perhaps during development), use the <xref
365     linkend="sql-load" endterm="sql-load-title"> command.
366    </para>
367
368    <para>
369     Use <xref linkend="sql-dropfunction"
370     endterm="sql-dropfunction-title"> to remove user-defined
371     functions.
372    </para>
373
374    <para>
375     It is often helpful to use dollar quoting (see <xref
376     linkend="sql-syntax-dollar-quoting">) to write the function definition
377     string, rather than the normal single quote syntax.  Without dollar
378     quoting, any single quotes or backslashes in the function definition must
379     be escaped by doubling them.
380    </para>
381
382    <para>
383     To be able to define a function, the user must have the
384     <literal>USAGE</literal> privilege on the language.
385    </para>
386
387  </refsect1>
388
389  <refsect1 id="sql-createfunction-examples">
390   <title>Examples</title>
391
392   <para>
393    Here is a trivial example to help you get started.  For more
394    information and examples, see <xref linkend="xfunc">.
395 <programlisting>
396 CREATE FUNCTION add(integer, integer) RETURNS integer
397     AS $$select $1 + $2;$$
398     LANGUAGE SQL
399     IMMUTABLE
400     RETURNS NULL ON NULL INPUT;
401 </programlisting>
402   </para>
403
404   <para>
405    Increment an integer, making use of an argument name, in
406    <application>PL/pgSQL</application>:
407
408 <programlisting>
409 CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS '
410         BEGIN
411                 RETURN i + 1;
412         END;' LANGUAGE plpgsql;
413 </programlisting>
414   </para>
415  </refsect1>
416
417  
418  <refsect1 id="sql-createfunction-compat">
419   <title>Compatibility</title>
420
421   <para>
422    A <command>CREATE FUNCTION</command> command is defined in SQL:1999 and later.
423    The <productname>PostgreSQL</productname> version is similar but
424    not fully compatible.  The attributes are not portable, neither are the
425    different available languages.
426   </para>
427  </refsect1>
428
429
430  <refsect1 id="sql-createfunction-seealso">
431   <title>See Also</title>
432
433   <para>
434    <xref linkend="sql-alterfunction" endterm="sql-alterfunction-title">,
435    <xref linkend="sql-dropfunction" endterm="sql-dropfunction-title">,
436    <xref linkend="sql-grant" endterm="sql-grant-title">,
437    <xref linkend="sql-load" endterm="sql-load-title">,
438    <xref linkend="sql-revoke" endterm="sql-revoke-title">,
439    <xref linkend="app-createlang">
440   </para>
441  </refsect1>
442
443 </refentry>
444
445 <!-- Keep this comment at the end of the file
446 Local variables:
447 mode:sgml
448 sgml-omittag:nil
449 sgml-shorttag:t
450 sgml-minimize-attributes:nil
451 sgml-always-quote-attributes:t
452 sgml-indent-step:1
453 sgml-indent-data:t
454 sgml-parent-document:nil
455 sgml-default-dtd-file:"../reference.ced"
456 sgml-exposed-tags:nil
457 sgml-local-catalogs:("/usr/lib/sgml/catalog")
458 sgml-local-ecat-files:nil
459 End:
460 -->