11.\" This is -*-nroff-*-
22.\" XXX standard disclaimer belongs here....
3- .\" $Header: /cvsroot/pgsql/src/man/Attic/create_function.l,v 1.10 1998/06/24 13:21:24 momjian Exp $
3+ .\" $Header: /cvsroot/pgsql/src/man/Attic/create_function.l,v 1.11 1999/05/20 02:44:53 tgl Exp $
44.TH "CREATE FUNCTION" SQL 11/05/95 PostgreSQL PostgreSQL
55.SH "NAME"
66create function - define a new function
@@ -9,8 +9,9 @@ create function - define a new function
99\fB create function \fP function_name
1010\fB ( \fP [type1 {, type-n}]\fB ) \fP
1111\fB returns \fP type-r
12- \fB as \fP {'/full/path/to/objectfile' | 'sql-queries'}
13- \fB language \fP {'c'\ 'sql'\ 'internal'\ 'plname'}
12+ \fB as \fP { '/full/path/to/objectfile' | 'sql-queries' |
13+ 'builtin-function-name' | 'pl-program-text' }
14+ \fB language \fP { 'c' | 'sql' | 'internal' | 'plname' }
1415.fi
1516.SH "DESCRIPTION"
1617With this command, a Postgres user can register a function with Postgres.
3536.IR " plname"
3637is the language name of a created procedural language. See
3738create_language(l) for details.)
38- (The
39- .IR " arg is"
40- clause may be left out if the function has no arguments, or
39+ (The argument list
40+ may be left out if the function has no arguments, or
4141alternatively the argument list may be left empty.)
4242The input types may be base or complex types, or
4343.IR opaque .
@@ -54,32 +54,43 @@ modifier indicates that the function will return a set of items,
5454rather than a single item.
5555The
5656.IR as
57- clause of the command is treated differently for C and SQL
58- functions, as explained below.
57+ clause of the command is treated differently depending on the language,
58+ as explained below.
59+ .SH "INTERNAL FUNCTIONS"
60+ Internal functions are functions written in C which have been statically
61+ linked into the postgres backend process. The
62+ .BR as
63+ clause gives the C-language name of the function, which need not be the
64+ same as the name being declared for SQL use. (For reasons of backwards
65+ compatibility, an empty
66+ .BR as
67+ string is accepted as meaning that the C-language function name is the
68+ same as the SQL name.) Normally, all internal functions present in the
69+ backend are declared as SQL functions during database initialization,
70+ but a user could use
71+ .BR " create function"
72+ to create additional alias names for an internal function.
5973.SH "C FUNCTIONS"
6074Functions written in C can be defined to Postgres, which will dynamically
61- load them into its address space. The loading happens either using
75+ load them into its address space. The
76+ .IR as
77+ clause gives the full path name of the object file that contains the
78+ function. This file is loaded either using
6279.IR load(l)
6380or automatically the first time the function is necessary for
6481execution. Repeated execution of a function will cause negligible
6582additional overhead, as the function will remain in a main memory
6683cache.
67- .PP
68- Internal functions are functions written in C which have been statically
69- linked into the postgres backend process. The
70- .BR as
71- clause must still be specified when defining an internal function but
72- the contents are ignored.
7384.SH "Writing C Functions"
74- The body of a C function following
85+ For a C function, the string following
7586.BR as
7687should be the
7788.BR " FULL PATH"
78- of the object code(.o file) for the function, bracketed by quotation
89+ of the object code file for the function, bracketed by quotation
7990marks. (Postgres will not compile a function automatically - it must
8091be compiled before it is used in a
81- .BR " define function"
82- command.)
92+ .BR " create function"
93+ command. See below for additional information. )
8394.PP
8495C functions with base type arguments can be written in a
8596straightforward fashion. The C equivalents of built-in Postgres types
@@ -297,7 +308,7 @@ on. If an argument is complex, then a \*(lqdot\*(rq notation may be
297308used to access attributes of the argument (e.g.\*( lq $1.emp\*( rq ), or
298309to invoke functions via a nested-dot syntax.
299310.SH "PL FUNCTIONS"
300- Procedural languages aren'tbuiltin to Postgres. They are offered
311+ Procedural languages aren'tbuilt into Postgres. They are offered
301312by loadable modules. Please refer to the documentation for the
302313PL in question for details about the syntax and how the
303314.IR " as"
@@ -400,10 +411,11 @@ A function may also have the same name as an attribute. In the case
400411that there is an ambiguity between a function on a complex type and
401412an attribute of the complex type, the attribute will always be used.
402413.SH "RESTRICTIONS"
403- The name of the C function must be a legal C function name, and the
404- name of the function in C code must be exactly the same as the name
405- used in
406- .BR " create function" .
414+ For functions written in C, the SQL name declared in
415+ .BR " create function"
416+ must be exactly the same as the actual name of the function in the
417+ C code (hence it must be a legal C function name).
418+ .PP
407419There is a subtle implication of this restriction: while the
408420dynamic loading routines in most operating systems are more than
409421happy to allow you to load any number of shared libraries that
@@ -422,6 +434,14 @@ define a set of C functions with different names and then define
422434a set of identically-named SQL function wrappers that take the
423435appropriate argument types and call the matching C function.
424436.PP
437+ Another solution is not to use dynamic loading, but to link your
438+ functions into the backend statically and declare them as INTERNAL
439+ functions. Then, the functions must all have distinct C names but
440+ they can be declared with the same SQL names (as long as their
441+ argument types differ, of course). This way avoids the overhead of
442+ an SQL wrapper function, at the cost of more effort to prepare a
443+ custom backend executable.
444+ .PP
425445.IR opaque
426446cannot be given as an argument to a SQL function.
427447.SH "BUGS"