1
1
.\" This is -*-nroff-*-
2
2
.\" 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 $
4
4
.TH "CREATE FUNCTION" SQL 11/05/95 PostgreSQL PostgreSQL
5
5
.SH "NAME"
6
6
create function - define a new function
@@ -9,8 +9,9 @@ create function - define a new function
9
9
\fB create function \fP function_name
10
10
\fB ( \fP [type1 {, type-n}]\fB ) \fP
11
11
\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' }
14
15
.fi
15
16
.SH "DESCRIPTION"
16
17
With this command, a Postgres user can register a function with Postgres.
35
36
.IR " plname"
36
37
is the language name of a created procedural language. See
37
38
create_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
41
41
alternatively the argument list may be left empty.)
42
42
The input types may be base or complex types, or
43
43
.IR opaque .
@@ -54,32 +54,43 @@ modifier indicates that the function will return a set of items,
54
54
rather than a single item.
55
55
The
56
56
.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.
59
73
.SH "C FUNCTIONS"
60
74
Functions 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
62
79
.IR load(l)
63
80
or automatically the first time the function is necessary for
64
81
execution. Repeated execution of a function will cause negligible
65
82
additional overhead, as the function will remain in a main memory
66
83
cache.
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.
73
84
.SH "Writing C Functions"
74
- The body of a C function following
85
+ For a C function, the string following
75
86
.BR as
76
87
should be the
77
88
.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
79
90
marks. (Postgres will not compile a function automatically - it must
80
91
be compiled before it is used in a
81
- .BR " define function"
82
- command.)
92
+ .BR " create function"
93
+ command. See below for additional information. )
83
94
.PP
84
95
C functions with base type arguments can be written in a
85
96
straightforward 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
297
308
used to access attributes of the argument (e.g. \*( lq $1.emp\*( rq ), or
298
309
to invoke functions via a nested-dot syntax.
299
310
.SH "PL FUNCTIONS"
300
- Procedural languages aren't builtin to Postgres. They are offered
311
+ Procedural languages aren't built into Postgres. They are offered
301
312
by loadable modules. Please refer to the documentation for the
302
313
PL in question for details about the syntax and how the
303
314
.IR " as"
@@ -400,10 +411,11 @@ A function may also have the same name as an attribute. In the case
400
411
that there is an ambiguity between a function on a complex type and
401
412
an attribute of the complex type, the attribute will always be used.
402
413
.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
407
419
There is a subtle implication of this restriction: while the
408
420
dynamic loading routines in most operating systems are more than
409
421
happy 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
422
434
a set of identically-named SQL function wrappers that take the
423
435
appropriate argument types and call the matching C function.
424
436
.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
425
445
.IR opaque
426
446
cannot be given as an argument to a SQL function.
427
447
.SH "BUGS"
0 commit comments