2 $PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.63 2004/11/27 21:27:07 petere Exp $
5 <refentry id="SQL-CREATEFUNCTION">
7 <refentrytitle id="SQL-CREATEFUNCTION-TITLE">CREATE FUNCTION</refentrytitle>
8 <refmiscinfo>SQL - Language Statements</refmiscinfo>
12 <refname>CREATE FUNCTION</refname>
13 <refpurpose>define a new function</refpurpose>
16 <indexterm zone="sql-createfunction">
17 <primary>CREATE FUNCTION</primary>
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>'
31 [ WITH ( <replaceable class="parameter">attribute</replaceable> [, ...] ) ]
35 <refsect1 id="sql-createfunction-description">
36 <title>Description</title>
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.
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</>).
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.
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.
72 The user that creates the function becomes the owner of the function.
77 <title>Parameters</title>
82 <term><replaceable class="parameter">name</replaceable></term>
86 The name (optionally schema-qualified) of the function to create.
92 <term><replaceable class="parameter">argname</replaceable></term>
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.
104 <term><replaceable class="parameter">argtype</replaceable></term>
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.
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.
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.
130 <term><replaceable class="parameter">rettype</replaceable></term>
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.
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.
151 <term><replaceable class="parameter">langname</replaceable></term>
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.
166 <term><literal>IMMUTABLE</literal></term>
167 <term><literal>STABLE</literal></term>
168 <term><literal>VOLATILE</literal></term>
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.
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.
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.
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()</>.
211 For additional details see <xref linkend="xfunc-volatility">.
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>
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
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
242 <term><literal><optional>EXTERNAL</optional> SECURITY INVOKER</literal></term>
243 <term><literal><optional>EXTERNAL</optional> SECURITY DEFINER</literal></term>
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.
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.
263 <term><replaceable class="parameter">definition</replaceable></term>
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.
275 <term><literal><replaceable class="parameter">obj_file</replaceable>, <replaceable class="parameter">link_symbol</replaceable></literal></term>
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.
294 <term><replaceable class="parameter">attribute</replaceable></term>
298 The historical way to specify optional pieces of information
299 about the function. The following attributes may appear here:
303 <term><literal>isStrict</></term>
306 Equivalent to <literal>STRICT</literal> or <literal>RETURNS NULL ON NULL INPUT</literal>
312 <term><literal>isCachable</></term>
315 <literal>isCachable</literal> is an obsolete equivalent of
316 <literal>IMMUTABLE</literal>; it's still accepted for
317 backwards-compatibility reasons.
324 Attribute names are not case-sensitive.
333 <refsect1 id="sql-createfunction-notes">
337 Refer to <xref linkend="xfunc"> for further information on writing
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.
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
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.
369 Use <xref linkend="sql-dropfunction"
370 endterm="sql-dropfunction-title"> to remove user-defined
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.
383 To be able to define a function, the user must have the
384 <literal>USAGE</literal> privilege on the language.
389 <refsect1 id="sql-createfunction-examples">
390 <title>Examples</title>
393 Here is a trivial example to help you get started. For more
394 information and examples, see <xref linkend="xfunc">.
396 CREATE FUNCTION add(integer, integer) RETURNS integer
397 AS $$select $1 + $2;$$
400 RETURNS NULL ON NULL INPUT;
405 Increment an integer, making use of an argument name, in
406 <application>PL/pgSQL</application>:
409 CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS '
412 END;' LANGUAGE plpgsql;
418 <refsect1 id="sql-createfunction-compat">
419 <title>Compatibility</title>
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.
430 <refsect1 id="sql-createfunction-seealso">
431 <title>See Also</title>
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">
445 <!-- Keep this comment at the end of the file
450 sgml-minimize-attributes:nil
451 sgml-always-quote-attributes: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