+ linkend="sql-createfunction"> command.
+
+
+
+
Compatibility
- If a file is not loaded using
- LOAD,
- the file will be loaded automatically the first time the
- function is called by
Postgres.
- LOAD
- can also be used to reload an object file if it has been edited and
- recompiled. Only objects created from C language files are supported
- at this time.
+ extension.
+
-
-
- 1998-09-24
-
-
- Notes
-
- Functions in loaded object files should not call functions in other
- object files loaded through the
- LOAD
- command. For example, all functions in file A should
- call each other, functions in the standard or math libraries, or in
- Postgres itself. They should not call functions defined in a different
- loaded file B.
- This is because if B is reloaded, the Postgres loader is
- not able to relocate the calls from the functions in
- A into
- the new address space of B.
- If B is not reloaded, however, there will
- not be a problem.
-
- Object files must be compiled to contain position independent code.
- For example,
- on DECstations you must use
- with the -G 0 option when compiling object files to be
- loaded.
-
- Note that if you are porting
Postgres
- to a new platform, LOAD
- will have to work in order to support ADTs.
-
-
-
+
+
See Also
-
-
- Usage
-
- Load the file /usr/postgres/demo/circle.o:
-
-LOAD '/usr/postgres/demo/circle.o'
-
+ ,
+ PostgreSQL Programmer's Guide
-
-
-
- Compatibility
-
-
-
-
- 1998-09-24
-
-
- SQL92
-
- There is no
LOAD in
SQL92.
-
-
-
procedural language
- functions (functions written in, for example, PLTCL or PLSQL)
+ functions (functions written in, for example, PL/Tcl or PL/pgSQL)
- programming
- language functions (functions written in a compiled
- programming language such as
C)
+ C language functions
-
Compiled (C) Language Functions
+
C Language Functions
- Functions written in C can be compiled into dynamically loadable
- objects (also called shared libraries), and used to implement user-defined
- SQL functions. The first time a user-defined function in a particular
+ User-defined functions can be written in C (or a language that can
+ be made compatible with C, such as C++). Such functions are
+ compiled into dynamically loadable objects (also called shared
+ libraries) and are loaded by the server on demand. This
+ distinguishes them from internal functions.
+
+
+ Two different calling conventions are currently used for C functions.
+ The newer "version 1" calling convention is indicated by writing
+ a PG_FUNCTION_INFO_V1() macro call for the function,
+ as illustrated below. Lack of such a macro indicates an old-style
+ ("version 0") function. The language name specified in CREATE FUNCTION
+ is 'C' in either case. Old-style functions are now deprecated
+ because of portability problems and lack of functionality, but they
+ are still supported for compatibility reasons.
+
+
+
+
Dynamic Loading
+
+ The first time a user-defined function in a particular
loadable object file is called in a backend session,
the dynamic loader loads that object file into memory so that the
function can be called. The CREATE FUNCTION
- for a user-defined function must therefore specify two pieces of
+ for a user-defined C function must therefore specify two pieces of
information for the function: the name of the loadable
object file, and the C name (link symbol) of the specific function to call
within that object file. If the C name is not explicitly specified then
- The string that specifies the object file (the first string in the AS
- clause) should be the full path of the object
- code file for the function, bracketed by single quote marks. If a
- link symbol is given in the AS clause, the link symbol should also be
- bracketed by single quote marks, and should be exactly the
- same as the name of the function in the C source code. On Unix systems
- the command nm will print all of the link
- symbols in a dynamically loadable object.
+ The following algorithm is used to locate the shared object file
+ based on the name given in the CREATE FUNCTION
+ command:
+
+
+
+ If the name is an absolute file name, the given file is loaded.
+
+
+
+
+ If the name starts with the string $libdir,
+ that part is replaced by the PostgreSQL library directory,
+ which is determined at build time.
+
+
+
+
+ If the name does not contain a directory part, the file is
+ searched the path specified by the configuration variable
+ dynamic_library_path.
+
+
+
+
+ Otherwise (the file was not found in the path, or it contains a
+ non-absolute directory part), the dynamic loader will try to
+ take the name as given, which will most likely fail. (It is
+ unreliable to depend on the current working directory.)
+
+
+
+
+ If this sequence does not work, the platform-specific shared
+ library file name extension (often .so) is
+ appended to the given name and this sequence is tried again. If
+ that fails as well, the load will fail.
+
-
-
Postgres will not compile a function
- automatically; it must be compiled before it is used in a CREATE
- FUNCTION command. See below for additional information.
-
-
+
+ The user id the
PostgreSQL server runs
+ as must be able to traverse the path to the file you intend to
+ load. Making the file or a higher-level directory not readable
+ and/or not executable by the postgres
user is a
+ common mistake.
+
+
+
+ In any case, the file name that is specified in the
+ CREATE FUNCTION command is recorded literally
+ in the system catalogs, so if the file needs to be loaded again
+ the same procedure is applied.
- Two different calling conventions are currently used for C functions.
- The newer "version 1" calling convention is indicated by writing
- a PG_FUNCTION_INFO_V1() macro call for the function,
- as illustrated below. Lack of such a macro indicates an old-style
- ("version 0") function. The language name specified in CREATE FUNCTION
- is 'C' in either case. Old-style functions are now deprecated
- because of portability problems and lack of functionality, but they
- are still supported for compatibility reasons.
+ It is recommended to locate shared libraries either relative to
+ $libdir or through the dynamic library path.
+ This simplifies version upgrades if the new installation is at a
+ different location.
+
+
PostgreSQL will not compile a function
+ automatically; it must be compiled before it is used in a CREATE
+ FUNCTION command. See for additional information.
+
+
+
+
Base Types in C-Language Functions