mirror of
https://github.com/postgres/postgres.git
synced 2025-07-03 20:02:46 +03:00
Allow special '$libdir' macro to show up in object file path in CREATE
FUNCTION command. Guard against trying to load a directory. Update documentation some.
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
<!--
|
<!--
|
||||||
$Header: /cvsroot/pgsql/doc/src/sgml/dfunc.sgml,v 1.13 2001/01/20 20:59:28 petere Exp $
|
$Header: /cvsroot/pgsql/doc/src/sgml/dfunc.sgml,v 1.14 2001/05/19 09:01:10 petere Exp $
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<sect2 id="dfunc">
|
<sect2 id="dfunc">
|
||||||
@ -264,7 +264,7 @@ gcc -shared -o foo.so foo.o
|
|||||||
<productname>Postgres</productname>. When specifying the file name
|
<productname>Postgres</productname>. When specifying the file name
|
||||||
to the <command>CREATE FUNCTION</command> command, one must give it
|
to the <command>CREATE FUNCTION</command> command, one must give it
|
||||||
the name of the shared library file (ending in
|
the name of the shared library file (ending in
|
||||||
<filename>.so</filename>) rather than the simple object file.
|
<filename>.so</filename>) rather than the intermediate object file.
|
||||||
|
|
||||||
<note>
|
<note>
|
||||||
<para>
|
<para>
|
||||||
@ -273,21 +273,8 @@ gcc -shared -o foo.so foo.o
|
|||||||
</para>
|
</para>
|
||||||
</note>
|
</note>
|
||||||
|
|
||||||
Paths given to the <command>CREATE FUNCTION</command> command must
|
Refer back to <xref linkend="xfunc-c-dynload"> about where the
|
||||||
be absolute paths (i.e., start with <literal>/</literal>) that refer
|
server expects to find the shared library files.
|
||||||
to directories visible on the machine on which the
|
|
||||||
<productname>Postgres</productname> server is running. Relative
|
|
||||||
paths do in fact work, but are relative to the directory where the
|
|
||||||
database resides (which is generally invisible to the frontend
|
|
||||||
application). Obviously, it makes no sense to make the path
|
|
||||||
relative to the directory in which the user started the frontend
|
|
||||||
application, since the server could be running on a completely
|
|
||||||
different machine! The user id the
|
|
||||||
<productname>Postgres</productname> server runs as must be able to
|
|
||||||
traverse the path given to the <command>CREATE FUNCTION</command>
|
|
||||||
command and be able to read the shared library file. (Making the
|
|
||||||
file or a higher-level directory not readable and/or not executable
|
|
||||||
by the <quote>postgres</quote> user is a common mistake.)
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
|
@ -1,187 +1,151 @@
|
|||||||
<!--
|
<!--
|
||||||
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_function.sgml,v 1.22 2001/04/28 13:59:07 momjian Exp $
|
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_function.sgml,v 1.23 2001/05/19 09:01:10 petere Exp $
|
||||||
Postgres documentation
|
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<refentry id="SQL-CREATEFUNCTION">
|
<refentry id="SQL-CREATEFUNCTION">
|
||||||
<refmeta>
|
<refmeta>
|
||||||
<refentrytitle id="sql-createfunction-title">
|
<refentrytitle>CREATE FUNCTION</refentrytitle>
|
||||||
CREATE FUNCTION
|
|
||||||
</refentrytitle>
|
|
||||||
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
||||||
</refmeta>
|
</refmeta>
|
||||||
|
|
||||||
<refnamediv>
|
<refnamediv>
|
||||||
<refname>
|
<refname>CREATE FUNCTION</refname>
|
||||||
CREATE FUNCTION
|
<refpurpose>Defines a new function</refpurpose>
|
||||||
</refname>
|
|
||||||
<refpurpose>
|
|
||||||
Defines a new function
|
|
||||||
</refpurpose>
|
|
||||||
</refnamediv>
|
</refnamediv>
|
||||||
|
|
||||||
<refsynopsisdiv>
|
<refsynopsisdiv>
|
||||||
<refsynopsisdivinfo>
|
<synopsis>
|
||||||
<date>2000-03-25</date>
|
CREATE FUNCTION <replaceable class="parameter">name</replaceable> ( [ <replaceable class="parameter">argtype</replaceable> [, ...] ] )
|
||||||
</refsynopsisdivinfo>
|
RETURNS <replaceable class="parameter">rettype</replaceable>
|
||||||
<synopsis>
|
AS '<replaceable class="parameter">definition</replaceable>'
|
||||||
CREATE FUNCTION <replaceable class="parameter">name</replaceable> ( [ <replaceable class="parameter">ftype</replaceable> [, ...] ] )
|
|
||||||
RETURNS <replaceable class="parameter">rtype</replaceable>
|
|
||||||
AS <replaceable class="parameter">definition</replaceable>
|
|
||||||
LANGUAGE '<replaceable class="parameter">langname</replaceable>'
|
LANGUAGE '<replaceable class="parameter">langname</replaceable>'
|
||||||
[ WITH ( <replaceable class="parameter">attribute</replaceable> [, ...] ) ]
|
[ WITH ( <replaceable class="parameter">attribute</replaceable> [, ...] ) ]
|
||||||
CREATE FUNCTION <replaceable class="parameter">name</replaceable> ( [ <replaceable class="parameter">ftype</replaceable> [, ...] ] )
|
CREATE FUNCTION <replaceable class="parameter">name</replaceable> ( [ <replaceable class="parameter">argtype</replaceable> [, ...] ] )
|
||||||
RETURNS <replaceable class="parameter">rtype</replaceable>
|
RETURNS <replaceable class="parameter">rettype</replaceable>
|
||||||
AS <replaceable class="parameter">obj_file</replaceable> , <replaceable class="parameter">link_symbol</replaceable>
|
AS '<replaceable class="parameter">obj_file</replaceable>', '<replaceable class="parameter">link_symbol</replaceable>'
|
||||||
LANGUAGE 'langname'
|
LANGUAGE '<replaceable class="parameter">langname</replaceable>'
|
||||||
[ WITH ( <replaceable class="parameter">attribute</replaceable> [, ...] ) ]
|
[ WITH ( <replaceable class="parameter">attribute</replaceable> [, ...] ) ]
|
||||||
</synopsis>
|
</synopsis>
|
||||||
|
|
||||||
<refsect2 id="R2-SQL-CREATEFUNCTION-1">
|
|
||||||
<refsect2info>
|
|
||||||
<date>2000-03-25</date>
|
|
||||||
</refsect2info>
|
|
||||||
<title>
|
|
||||||
Inputs
|
|
||||||
</title>
|
|
||||||
<para>
|
|
||||||
|
|
||||||
<variablelist>
|
|
||||||
<varlistentry>
|
|
||||||
<term><replaceable class="parameter">name</replaceable></term>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
The name of a function to create.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
<varlistentry>
|
|
||||||
<term><replaceable class="parameter">ftype</replaceable></term>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
The data type(s) of the function's arguments, if any.
|
|
||||||
The input types may be base or complex types, or
|
|
||||||
<firstterm>opaque</firstterm>.
|
|
||||||
<literal>Opaque</literal> indicates that the function
|
|
||||||
accepts arguments of a non-SQL type such as <type>char *</type>.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
<varlistentry>
|
|
||||||
<term><replaceable class="parameter">rtype</replaceable></term>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
The return data type.
|
|
||||||
The output type may be specified as a base type, complex type,
|
|
||||||
<option>setof type</option>,
|
|
||||||
or <option>opaque</option>.
|
|
||||||
The <option>setof</option>
|
|
||||||
modifier indicates that the function will return a set of items,
|
|
||||||
rather than a single item.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
<varlistentry>
|
|
||||||
<term><replaceable class="parameter">attribute</replaceable></term>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
An optional piece of information about the function, used for
|
|
||||||
optimization. See below for details.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
<varlistentry>
|
|
||||||
<term><replaceable class="parameter">definition</replaceable></term>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
A string defining the function; the meaning depends on the language.
|
|
||||||
It may be an internal function name, the path to an object file,
|
|
||||||
an SQL query, or text in a procedural language.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
<varlistentry>
|
|
||||||
<term><replaceable class="parameter">obj_file</replaceable> , <replaceable class="parameter">link_symbol</replaceable></term>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
This form of the <command>AS</command> clause is used for
|
|
||||||
dynamically linked, C language functions when the function name in
|
|
||||||
the C language source code is not the same as the name of the SQL
|
|
||||||
function. The string <replaceable
|
|
||||||
class="parameter">obj_file</replaceable> is the name of the file
|
|
||||||
containing the dynamically loadable object, and <replaceable
|
|
||||||
class="parameter">link_symbol</replaceable> is the object's link
|
|
||||||
symbol, that is the name of the function in the C
|
|
||||||
language source code.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
<varlistentry>
|
|
||||||
<term><replaceable class="parameter">langname</replaceable></term>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
May be '<literal>sql</literal>',
|
|
||||||
'<literal>C</literal>', '<literal>internal</literal>',
|
|
||||||
or '<replaceable class="parameter">plname</replaceable>',
|
|
||||||
where '<replaceable class="parameter">plname</replaceable>'
|
|
||||||
is the name of a created procedural language. See
|
|
||||||
<xref linkend="sql-createlanguage" endterm="sql-createlanguage-title">
|
|
||||||
for details.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
</variablelist>
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
|
|
||||||
<refsect2 id="R2-SQL-CREATEFUNCTION-2">
|
|
||||||
<refsect2info>
|
|
||||||
<date>2000-03-25</date>
|
|
||||||
</refsect2info>
|
|
||||||
<title>
|
|
||||||
Outputs
|
|
||||||
</title>
|
|
||||||
<para>
|
|
||||||
|
|
||||||
<variablelist>
|
|
||||||
<varlistentry>
|
|
||||||
<term><computeroutput>
|
|
||||||
CREATE
|
|
||||||
</computeroutput></term>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
This is returned if the command completes successfully.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
</variablelist>
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
</refsynopsisdiv>
|
</refsynopsisdiv>
|
||||||
|
|
||||||
<refsect1 id="R1-SQL-CREATEFUNCTION-1">
|
<refsect1 id="sql-createfunction-description">
|
||||||
<refsect1info>
|
<title>Description</title>
|
||||||
<date>2000-03-25</date>
|
|
||||||
</refsect1info>
|
|
||||||
<title>
|
|
||||||
Description
|
|
||||||
</title>
|
|
||||||
<para>
|
<para>
|
||||||
<command>CREATE FUNCTION</command> allows a
|
<command>CREATE FUNCTION</command> defines a new function.
|
||||||
<productname>Postgres</productname> user
|
|
||||||
to register a function
|
<variablelist>
|
||||||
with the database. Subsequently, this user is considered the
|
<title>Parameters</title>
|
||||||
owner of the function.
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><replaceable class="parameter">name</replaceable></term>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
The name of a function to create. The name need not be unique,
|
||||||
|
because functions may be overloaded, but functions with the
|
||||||
|
same name must have different argument types.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><replaceable class="parameter">argtype</replaceable></term>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
The data type(s) of the function's arguments, if any. The
|
||||||
|
input types may be base or complex types, or
|
||||||
|
<literal>opaque</literal>. <literal>Opaque</literal> indicates
|
||||||
|
that the function accepts arguments of a non-SQL type such as
|
||||||
|
<type>char *</type>.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><replaceable class="parameter">rettype</replaceable></term>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
The return data type. The output type may be specified as a
|
||||||
|
base type, complex type, <literal>setof</literal> type, or
|
||||||
|
<literal>opaque</literal>. The <literal>setof</literal>
|
||||||
|
modifier indicates that the function will return a set of
|
||||||
|
items, rather than a single item. Functions with a declared
|
||||||
|
return type of <literal>opaque</literal> do not return a value.
|
||||||
|
These cannot be called directly; trigger functions make use of
|
||||||
|
this feature.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><replaceable class="parameter">definition</replaceable></term>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
A string defining the function; the meaning depends on the
|
||||||
|
language. It may be an internal function name, the path to an
|
||||||
|
object file, an SQL query, or text in a procedural language.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><replaceable class="parameter">obj_file</replaceable>, <replaceable class="parameter">link_symbol</replaceable></term>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
This form of the <literal>AS</literal> clause is used for
|
||||||
|
dynamically linked C language functions when the function name
|
||||||
|
in the C language source code is not the same as the name of
|
||||||
|
the SQL function. The string <replaceable
|
||||||
|
class="parameter">obj_file</replaceable> is the name of the
|
||||||
|
file containing the dynamically loadable object, and
|
||||||
|
<replaceable class="parameter">link_symbol</replaceable> is the
|
||||||
|
object's link symbol, that is, the name of the function in the C
|
||||||
|
language source code.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><replaceable class="parameter">langname</replaceable></term>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
May be '<literal>sql</literal>', '<literal>C</literal>',
|
||||||
|
'<literal>internal</literal>', or '<replaceable
|
||||||
|
class="parameter">plname</replaceable>', where '<replaceable
|
||||||
|
class="parameter">plname</replaceable>' is the name of a
|
||||||
|
created procedural language. See
|
||||||
|
<xref linkend="sql-createlanguage">
|
||||||
|
for details.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><replaceable class="parameter">attribute</replaceable></term>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
An optional piece of information about the function, used for
|
||||||
|
optimization. See below for details.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
|
</variablelist>
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<refsect2 id="R2-SQL-CREATEFUNCTION-3">
|
<para>
|
||||||
<refsect2info>
|
The user that creates the function becomes the owner of the function.
|
||||||
<date>2000-08-24</date>
|
</para>
|
||||||
</refsect2info>
|
|
||||||
<title>
|
|
||||||
Function Attributes
|
|
||||||
</title>
|
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
The following items may appear in the WITH clause:
|
The following attributes may appear in the WITH clause:
|
||||||
|
|
||||||
<variablelist>
|
<variablelist>
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
@ -214,17 +178,12 @@ CREATE
|
|||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
</variablelist>
|
</variablelist>
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
</refsect2>
|
</refsect1>
|
||||||
|
|
||||||
<refsect2 id="R2-SQL-CREATEFUNCTION-4">
|
<refsect1 id="sql-createfunction-notes">
|
||||||
<refsect2info>
|
<title>Notes</title>
|
||||||
<date>2000-03-25</date>
|
|
||||||
</refsect2info>
|
|
||||||
<title>
|
|
||||||
Notes
|
|
||||||
</title>
|
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
Refer to the chapter in the
|
Refer to the chapter in the
|
||||||
@ -240,7 +199,7 @@ CREATE
|
|||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
The full <acronym>SQL92</acronym> type syntax is allowed for
|
The full <acronym>SQL</acronym> type syntax is allowed for
|
||||||
input arguments and return value. However, some details of the
|
input arguments and return value. However, some details of the
|
||||||
type specification (e.g., the precision field for
|
type specification (e.g., the precision field for
|
||||||
<type>numeric</type> types) are the responsibility of the
|
<type>numeric</type> types) are the responsibility of the
|
||||||
@ -250,7 +209,7 @@ CREATE
|
|||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
<productname>Postgres</productname> allows function "overloading";
|
<productname>Postgres</productname> allows function <firstterm>overloading</firstterm>;
|
||||||
that is, the same name can be used for several different functions
|
that is, the same name can be used for several different functions
|
||||||
so long as they have distinct argument types. This facility must
|
so long as they have distinct argument types. This facility must
|
||||||
be used with caution for internal and C-language functions, however.
|
be used with caution for internal and C-language functions, however.
|
||||||
@ -275,38 +234,45 @@ CREATE
|
|||||||
C-language implementation of each overloaded SQL function.
|
C-language implementation of each overloaded SQL function.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
</refsect2>
|
<para>
|
||||||
|
When repeated <command>CREATE FUNCTION</command> calls refer to
|
||||||
|
the same object file, the file is only loaded once. To unload and
|
||||||
|
reload the file (perhaps during development), use the <xref
|
||||||
|
linkend="sql-load"> command.
|
||||||
|
</para>
|
||||||
|
|
||||||
</refsect1>
|
</refsect1>
|
||||||
|
|
||||||
<refsect1 id="R1-SQL-CREATEFUNCTION-2">
|
|
||||||
<title>
|
<refsect1 id="sql-createfunction-examples">
|
||||||
Usage
|
<title>Examples</title>
|
||||||
</title>
|
|
||||||
<para>
|
<para>
|
||||||
To create a simple SQL function:
|
To create a simple SQL function:
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
CREATE FUNCTION one() RETURNS int4
|
CREATE FUNCTION one() RETURNS integer
|
||||||
AS 'SELECT 1 AS RESULT'
|
AS 'SELECT 1 AS RESULT;'
|
||||||
LANGUAGE 'sql';
|
LANGUAGE 'sql';
|
||||||
SELECT one() AS answer;
|
|
||||||
|
|
||||||
<computeroutput>
|
SELECT one() AS answer;
|
||||||
|
<computeroutput>
|
||||||
answer
|
answer
|
||||||
--------
|
--------
|
||||||
1
|
1
|
||||||
</computeroutput>
|
</computeroutput>
|
||||||
</programlisting>
|
</programlisting>
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
This example creates a C function by calling a routine from a user-created
|
The next example creates a C function by calling a routine from a
|
||||||
shared library. This particular routine calculates a check
|
user-created shared library. This particular routine calculates a
|
||||||
digit and returns TRUE if the check digit in the function parameters
|
check digit and returns TRUE if the check digit in the function
|
||||||
is correct. It is intended for use in a CHECK contraint.
|
parameters is correct. It is intended for use in a CHECK
|
||||||
</para>
|
constraint.
|
||||||
<programlisting>
|
|
||||||
CREATE FUNCTION ean_checkdigit(bpchar, bpchar) RETURNS boolean
|
<programlisting>
|
||||||
|
CREATE FUNCTION ean_checkdigit(char, char) RETURNS boolean
|
||||||
AS '/usr1/proj/bray/sql/funcs.so' LANGUAGE 'c';
|
AS '/usr1/proj/bray/sql/funcs.so' LANGUAGE 'c';
|
||||||
|
|
||||||
CREATE TABLE product (
|
CREATE TABLE product (
|
||||||
@ -316,28 +282,28 @@ CREATE TABLE product (
|
|||||||
eancode char(6) CHECK (eancode ~ '[0-9]{6}'),
|
eancode char(6) CHECK (eancode ~ '[0-9]{6}'),
|
||||||
CONSTRAINT ean CHECK (ean_checkdigit(eanprefix, eancode))
|
CONSTRAINT ean CHECK (ean_checkdigit(eanprefix, eancode))
|
||||||
);
|
);
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
This example creates a function that does type conversion between the
|
This example creates a function that does type conversion between the
|
||||||
user-defined type complex, and the internal type point. The
|
user-defined type complex, and the internal type point. The
|
||||||
function is implemented by a dynamically loaded object that was
|
function is implemented by a dynamically loaded object that was
|
||||||
compiled from C source. For <productname>Postgres</productname> to
|
compiled from C source. For <productname>PostgreSQL</productname> to
|
||||||
find a type conversion function automatically, the sql function has
|
find a type conversion function automatically, the SQL function has
|
||||||
to have the same name as the return type, and so overloading is
|
to have the same name as the return type, and so overloading is
|
||||||
unavoidable. The function name is overloaded by using the second
|
unavoidable. The function name is overloaded by using the second
|
||||||
form of the <command>AS</command> clause in the SQL definition:
|
form of the <command>AS</command> clause in the SQL definition:
|
||||||
</para>
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
CREATE FUNCTION point(complex) RETURNS point
|
CREATE FUNCTION point(complex) RETURNS point
|
||||||
AS '/home/bernie/pgsql/lib/complex.so', 'complex_to_point'
|
AS '/home/bernie/pgsql/lib/complex.so', 'complex_to_point'
|
||||||
LANGUAGE 'c';
|
LANGUAGE 'c';
|
||||||
</programlisting>
|
</programlisting>
|
||||||
<para>
|
|
||||||
The C declaration of the function is:
|
The C declaration of the function could be:
|
||||||
</para>
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
Point * complex_to_point (Complex *z)
|
Point * complex_to_point (Complex *z)
|
||||||
{
|
{
|
||||||
Point *p;
|
Point *p;
|
||||||
@ -348,58 +314,33 @@ Point * complex_to_point (Complex *z)
|
|||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
</para>
|
||||||
|
|
||||||
</refsect1>
|
</refsect1>
|
||||||
|
|
||||||
<refsect1 id="R1-SQL-CREATEFUNCTION-4">
|
|
||||||
<title>
|
|
||||||
Compatibility
|
|
||||||
</title>
|
|
||||||
|
|
||||||
<refsect2 id="R2-SQL-CREATEFUNCTION-5">
|
<refsect1 id="sql-createfunction-compat">
|
||||||
<refsect2info>
|
<title>Compatibility</title>
|
||||||
<date>2000-03-25</date>
|
|
||||||
</refsect2info>
|
|
||||||
<title>
|
|
||||||
SQL92
|
|
||||||
</title>
|
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
<command>CREATE FUNCTION</command> is
|
A <command>CREATE FUNCTION</command> command is defined in SQL99.
|
||||||
a <productname>Postgres</productname> language extension.
|
The <application>PostgreSQL</application> version is similar but
|
||||||
</para>
|
not compatible. The attributes are not portable, neither are the
|
||||||
</refsect2>
|
different available languages.
|
||||||
|
</para>
|
||||||
<refsect2 id="R2-SQL-CREATEFUNCTION-6">
|
|
||||||
<refsect2info>
|
|
||||||
<date>2000-03-25</date>
|
|
||||||
</refsect2info>
|
|
||||||
<title>
|
|
||||||
SQL/PSM
|
|
||||||
</title>
|
|
||||||
<para>
|
|
||||||
|
|
||||||
<note>
|
|
||||||
<para>
|
|
||||||
PSM stands for Persistent Stored Modules. It is a procedural
|
|
||||||
language. SQL/PSM is a standard to enable function extensibility.
|
|
||||||
</para>
|
|
||||||
</note>
|
|
||||||
|
|
||||||
SQL/PSM <command>CREATE FUNCTION</command> has the following syntax:
|
|
||||||
<synopsis>
|
|
||||||
CREATE FUNCTION <replaceable class="parameter">name</replaceable>
|
|
||||||
( [ [ IN | OUT | INOUT ] <replaceable class="parameter">type</replaceable> [, ...] ] )
|
|
||||||
RETURNS <replaceable class="parameter">rtype</replaceable>
|
|
||||||
LANGUAGE '<replaceable class="parameter">langname</replaceable>'
|
|
||||||
ESPECIFIC <replaceable class="parameter">routine</replaceable>
|
|
||||||
<replaceable class="parameter">SQL-statement</replaceable>
|
|
||||||
</synopsis>
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
</refsect1>
|
</refsect1>
|
||||||
|
|
||||||
|
|
||||||
|
<refsect1 id="sql-createfunction-seealso">
|
||||||
|
<title>See Also</title>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
<xref linkend="sql-dropfunction">,
|
||||||
|
<xref linkend="sql-load">,
|
||||||
|
<citetitle>PostgreSQL Programmer's Guide</citetitle>
|
||||||
|
</para>
|
||||||
|
</refsect1>
|
||||||
|
|
||||||
</refentry>
|
</refentry>
|
||||||
|
|
||||||
<!-- Keep this comment at the end of the file
|
<!-- Keep this comment at the end of the file
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<!--
|
<!--
|
||||||
$Header: /cvsroot/pgsql/doc/src/sgml/ref/drop_function.sgml,v 1.11 2001/05/08 17:35:57 momjian Exp $
|
$Header: /cvsroot/pgsql/doc/src/sgml/ref/drop_function.sgml,v 1.12 2001/05/19 09:01:10 petere Exp $
|
||||||
Postgres documentation
|
Postgres documentation
|
||||||
-->
|
-->
|
||||||
|
|
||||||
@ -116,7 +116,7 @@ NOTICE RemoveFunction: Function "<replaceable class="parameter">name</replaceabl
|
|||||||
|
|
||||||
<para>
|
<para>
|
||||||
Refer to
|
Refer to
|
||||||
<xref linkend="sql-createfunction" endterm="sql-createfunction-title">
|
<xref linkend="sql-createfunction">
|
||||||
for information on creating functions.
|
for information on creating functions.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
@ -1,183 +1,54 @@
|
|||||||
<!--
|
<!--
|
||||||
$Header: /cvsroot/pgsql/doc/src/sgml/ref/load.sgml,v 1.7 1999/07/22 15:09:12 thomas Exp $
|
$Header: /cvsroot/pgsql/doc/src/sgml/ref/load.sgml,v 1.8 2001/05/19 09:01:10 petere Exp $
|
||||||
Postgres documentation
|
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<refentry id="SQL-LOAD">
|
<refentry id="SQL-LOAD">
|
||||||
<refmeta>
|
<refmeta>
|
||||||
<refentrytitle id="SQL-LOAD-TITLE">
|
<refentrytitle>LOAD</refentrytitle>
|
||||||
LOAD
|
|
||||||
</refentrytitle>
|
|
||||||
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
||||||
</refmeta>
|
</refmeta>
|
||||||
|
|
||||||
<refnamediv>
|
<refnamediv>
|
||||||
<refname>
|
<refname>LOAD</refname>
|
||||||
LOAD
|
<refpurpose>Loads a shared object file</refpurpose>
|
||||||
</refname>
|
|
||||||
<refpurpose>
|
|
||||||
Dynamically loads an object file
|
|
||||||
</refpurpose>
|
|
||||||
</refnamediv>
|
</refnamediv>
|
||||||
|
|
||||||
<refsynopsisdiv>
|
<refsynopsisdiv>
|
||||||
<refsynopsisdivinfo>
|
<synopsis>
|
||||||
<date>1999-07-20</date>
|
|
||||||
</refsynopsisdivinfo>
|
|
||||||
<synopsis>
|
|
||||||
LOAD '<replaceable class="PARAMETER">filename</replaceable>'
|
LOAD '<replaceable class="PARAMETER">filename</replaceable>'
|
||||||
</synopsis>
|
</synopsis>
|
||||||
|
|
||||||
<refsect2 id="R2-SQL-LOAD-1">
|
|
||||||
<refsect2info>
|
|
||||||
<date>1998-09-01</date>
|
|
||||||
</refsect2info>
|
|
||||||
<title>
|
|
||||||
Inputs
|
|
||||||
</title>
|
|
||||||
<para>
|
|
||||||
<variablelist>
|
|
||||||
<varlistentry>
|
|
||||||
<term><replaceable class="PARAMETER">filename</replaceable></term>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
Object file for dynamic loading.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
</variablelist>
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
|
|
||||||
<refsect2 id="R2-SQL-LOAD-2">
|
|
||||||
<refsect2info>
|
|
||||||
<date>1998-09-24</date>
|
|
||||||
</refsect2info>
|
|
||||||
<title>
|
|
||||||
Outputs
|
|
||||||
</title>
|
|
||||||
<para>
|
|
||||||
|
|
||||||
<variablelist>
|
|
||||||
<varlistentry>
|
|
||||||
<term><computeroutput>
|
|
||||||
LOAD
|
|
||||||
</computeroutput></term>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
Message returned on successful completion.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
<varlistentry>
|
|
||||||
<term><computeroutput>
|
|
||||||
ERROR: LOAD: could not open file '<replaceable class="PARAMETER">filename</replaceable>'
|
|
||||||
</computeroutput></term>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
Message returned if the specified file is not found. The file must be visible
|
|
||||||
<emphasis>to the <productname>Postgres</productname> backend</emphasis>,
|
|
||||||
with the appropriate full path name specified, to avoid this message.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
</variablelist>
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
</refsynopsisdiv>
|
</refsynopsisdiv>
|
||||||
|
|
||||||
<refsect1 id="R1-SQL-LOAD-1">
|
<refsect1 id="sql-load-description">
|
||||||
<refsect1info>
|
<title>Description</title>
|
||||||
<date>1998-09-24</date>
|
|
||||||
</refsect1info>
|
|
||||||
<title>
|
|
||||||
Description
|
|
||||||
</title>
|
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
Loads an object (or ".o") file into the
|
Loads a shared object file into the PostgreSQL backend's address
|
||||||
<productname>Postgres</productname> backend address space. Once a
|
space. If the file had been loaded previously, it is first
|
||||||
file is loaded, all functions in that file can be accessed. This
|
unloaded. This command is primarily useful to unload and reload a
|
||||||
function is used in support of user-defined types and functions.
|
shared object file if it has been changed. To make use of the
|
||||||
</para>
|
shared object, a function needs to be declared using the <xref
|
||||||
|
linkend="sql-createfunction"> command.
|
||||||
<para>
|
|
||||||
If a file is not loaded using
|
|
||||||
<command>LOAD</command>,
|
|
||||||
the file will be loaded automatically the first time the
|
|
||||||
function is called by <productname>Postgres</productname>.
|
|
||||||
<command>LOAD</command>
|
|
||||||
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.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<refsect2 id="R2-SQL-LOAD-3">
|
|
||||||
<refsect2info>
|
|
||||||
<date>1998-09-24</date>
|
|
||||||
</refsect2info>
|
|
||||||
<title>
|
|
||||||
Notes
|
|
||||||
</title>
|
|
||||||
|
|
||||||
<para>
|
|
||||||
Functions in loaded object files should not call functions in other
|
|
||||||
object files loaded through the
|
|
||||||
<command>LOAD</command>
|
|
||||||
command. For example, all functions in file <literal>A</literal> 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 <literal>B</literal>.
|
|
||||||
This is because if <literal>B</literal> is reloaded, the Postgres loader is
|
|
||||||
not able to relocate the calls from the functions in
|
|
||||||
<literal>A</literal> into
|
|
||||||
the new address space of <literal>B</literal>.
|
|
||||||
If <literal>B</literal> is not reloaded, however, there will
|
|
||||||
not be a problem.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
Object files must be compiled to contain position independent code.
|
|
||||||
For example,
|
|
||||||
on DECstations you must use
|
|
||||||
<application>/bin/cc</application>
|
|
||||||
with the <literal>-G 0</literal> option when compiling object files to be
|
|
||||||
loaded.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
Note that if you are porting <productname>Postgres</productname>
|
|
||||||
to a new platform, <command>LOAD</command>
|
|
||||||
will have to work in order to support ADTs.
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
</refsect1>
|
|
||||||
|
|
||||||
<refsect1 id="R1-SQL-LOAD-2">
|
|
||||||
<title>
|
|
||||||
Usage
|
|
||||||
</title>
|
|
||||||
<para>
|
|
||||||
Load the file <filename>/usr/postgres/demo/circle.o</filename>:
|
|
||||||
|
|
||||||
<programlisting>
|
|
||||||
LOAD '/usr/postgres/demo/circle.o'
|
|
||||||
</programlisting>
|
|
||||||
</para>
|
</para>
|
||||||
</refsect1>
|
</refsect1>
|
||||||
|
|
||||||
<refsect1 id="R1-SQL-LOAD-3">
|
<refsect1 id="sql-load-compat">
|
||||||
<title>
|
<title>Compatibility</title>
|
||||||
Compatibility
|
|
||||||
</title>
|
|
||||||
|
|
||||||
<refsect2 id="R2-SQL-LOAD-4">
|
<para>
|
||||||
<refsect2info>
|
<command>LOAD</command> is a <application>PostgreSQL</application>
|
||||||
<date>1998-09-24</date>
|
extension.
|
||||||
</refsect2info>
|
</para>
|
||||||
<title>
|
</refsect1>
|
||||||
SQL92
|
|
||||||
</title>
|
|
||||||
<para>
|
<refsect1>
|
||||||
There is no <command>LOAD</command> in <acronym>SQL92</acronym>.
|
<title>See Also</title>
|
||||||
</para>
|
|
||||||
</refsect2>
|
<para>
|
||||||
|
<xref linkend="sql-createfunction">,
|
||||||
|
<citetitle>PostgreSQL Programmer's Guide</citetitle>
|
||||||
|
</para>
|
||||||
</refsect1>
|
</refsect1>
|
||||||
</refentry>
|
</refentry>
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<!--
|
<!--
|
||||||
$Header: /cvsroot/pgsql/doc/src/sgml/xfunc.sgml,v 1.31 2001/02/15 19:03:35 tgl Exp $
|
$Header: /cvsroot/pgsql/doc/src/sgml/xfunc.sgml,v 1.32 2001/05/19 09:01:10 petere Exp $
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<chapter id="xfunc">
|
<chapter id="xfunc">
|
||||||
@ -29,14 +29,12 @@ $Header: /cvsroot/pgsql/doc/src/sgml/xfunc.sgml,v 1.31 2001/02/15 19:03:35 tgl E
|
|||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
procedural language
|
procedural language
|
||||||
functions (functions written in, for example, PLTCL or PLSQL)
|
functions (functions written in, for example, PL/Tcl or PL/pgSQL)
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
programming
|
C language functions
|
||||||
language functions (functions written in a compiled
|
|
||||||
programming language such as <acronym>C</acronym>)
|
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</itemizedlist>
|
</itemizedlist>
|
||||||
@ -371,16 +369,36 @@ SELECT clean_EMP();
|
|||||||
</sect1>
|
</sect1>
|
||||||
|
|
||||||
<sect1 id="xfunc-c">
|
<sect1 id="xfunc-c">
|
||||||
<title>Compiled (C) Language Functions</title>
|
<title>C Language Functions</title>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
Functions written in C can be compiled into dynamically loadable
|
User-defined functions can be written in C (or a language that can
|
||||||
objects (also called shared libraries), and used to implement user-defined
|
be made compatible with C, such as C++). Such functions are
|
||||||
SQL functions. The first time a user-defined function in a particular
|
compiled into dynamically loadable objects (also called shared
|
||||||
|
libraries) and are loaded by the server on demand. This
|
||||||
|
distinguishes them from internal functions.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
Two different calling conventions are currently used for C functions.
|
||||||
|
The newer "version 1" calling convention is indicated by writing
|
||||||
|
a <literal>PG_FUNCTION_INFO_V1()</literal> 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.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<sect2 id="xfunc-c-dynload">
|
||||||
|
<title>Dynamic Loading</title>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
The first time a user-defined function in a particular
|
||||||
loadable object file is called in a backend session,
|
loadable object file is called in a backend session,
|
||||||
the dynamic loader loads that object file into memory so that the
|
the dynamic loader loads that object file into memory so that the
|
||||||
function can be called. The <command>CREATE FUNCTION</command>
|
function can be called. The <command>CREATE FUNCTION</command>
|
||||||
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
|
information for the function: the name of the loadable
|
||||||
object file, and the C name (link symbol) of the specific function to call
|
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
|
within that object file. If the C name is not explicitly specified then
|
||||||
@ -397,35 +415,82 @@ SELECT clean_EMP();
|
|||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
The string that specifies the object file (the first string in the AS
|
The following algorithm is used to locate the shared object file
|
||||||
clause) should be the <emphasis>full path</emphasis> of the object
|
based on the name given in the <command>CREATE FUNCTION</command>
|
||||||
code file for the function, bracketed by single quote marks. If a
|
command:
|
||||||
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 <command>nm</command> will print all of the link
|
|
||||||
symbols in a dynamically loadable object.
|
|
||||||
|
|
||||||
<note>
|
<orderedlist>
|
||||||
<para>
|
<listitem>
|
||||||
<productname>Postgres</productname> will not compile a function
|
<para>
|
||||||
automatically; it must be compiled before it is used in a CREATE
|
If the name is an absolute file name, the given file is loaded.
|
||||||
FUNCTION command. See below for additional information.
|
</para>
|
||||||
</para>
|
</listitem>
|
||||||
</note>
|
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
If the name starts with the string <literal>$libdir</literal>,
|
||||||
|
that part is replaced by the PostgreSQL library directory,
|
||||||
|
which is determined at build time.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
If the name does not contain a directory part, the file is
|
||||||
|
searched the path specified by the configuration variable
|
||||||
|
<varname>dynamic_library_path</varname>.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
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.)
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</orderedlist>
|
||||||
|
|
||||||
|
If this sequence does not work, the platform-specific shared
|
||||||
|
library file name extension (often <filename>.so</filename>) is
|
||||||
|
appended to the given name and this sequence is tried again. If
|
||||||
|
that fails as well, the load will fail.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<note>
|
||||||
|
<para>
|
||||||
|
The user id the <application>PostgreSQL</application> 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 <quote>postgres</quote> user is a
|
||||||
|
common mistake.
|
||||||
|
</para>
|
||||||
|
</note>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
In any case, the file name that is specified in the
|
||||||
|
<command>CREATE FUNCTION</command> command is recorded literally
|
||||||
|
in the system catalogs, so if the file needs to be loaded again
|
||||||
|
the same procedure is applied.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
Two different calling conventions are currently used for C functions.
|
It is recommended to locate shared libraries either relative to
|
||||||
The newer "version 1" calling convention is indicated by writing
|
<literal>$libdir</literal> or through the dynamic library path.
|
||||||
a <literal>PG_FUNCTION_INFO_V1()</literal> macro call for the function,
|
This simplifies version upgrades if the new installation is at a
|
||||||
as illustrated below. Lack of such a macro indicates an old-style
|
different location.
|
||||||
("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.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
<note>
|
||||||
|
<para>
|
||||||
|
<application>PostgreSQL</application> will not compile a function
|
||||||
|
automatically; it must be compiled before it is used in a CREATE
|
||||||
|
FUNCTION command. See <xref linkend="dfunc"> for additional information.
|
||||||
|
</para>
|
||||||
|
</note>
|
||||||
|
</sect2>
|
||||||
|
|
||||||
<sect2>
|
<sect2>
|
||||||
<title>Base Types in C-Language Functions</title>
|
<title>Base Types in C-Language Functions</title>
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<!--
|
<!--
|
||||||
$Header: /cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.12 2001/02/09 02:20:52 tgl Exp $
|
$Header: /cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.13 2001/05/19 09:01:10 petere Exp $
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<chapter id="xplang">
|
<chapter id="xplang">
|
||||||
@ -130,13 +130,13 @@ CREATE <optional>TRUSTED</optional> <optional>PROCEDURAL</optional> LANGUAGE '<r
|
|||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
CREATE FUNCTION plpgsql_call_handler () RETURNS OPAQUE AS
|
CREATE FUNCTION plpgsql_call_handler () RETURNS OPAQUE AS
|
||||||
'/usr/local/pgsql/lib/plpgsql.so' LANGUAGE 'C';
|
'$libdir/plpgsql' LANGUAGE 'C';
|
||||||
</programlisting>
|
</programlisting>
|
||||||
</para>
|
</para>
|
||||||
</step>
|
</step>
|
||||||
|
|
||||||
<step performance="Required">
|
<step performance="Required">
|
||||||
<para>
|
<para>
|
||||||
The command
|
The command
|
||||||
<programlisting>
|
<programlisting>
|
||||||
CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
|
CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $Header: /cvsroot/pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.49 2001/05/17 17:44:18 petere Exp $
|
* $Header: /cvsroot/pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.50 2001/05/19 09:01:10 petere Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -51,7 +51,7 @@ char * Dynamic_library_path;
|
|||||||
static bool file_exists(const char *name);
|
static bool file_exists(const char *name);
|
||||||
static char * find_in_dynamic_libpath(const char * basename);
|
static char * find_in_dynamic_libpath(const char * basename);
|
||||||
static char * expand_dynamic_library_name(const char *name);
|
static char * expand_dynamic_library_name(const char *name);
|
||||||
|
static char * substitute_libpath_macro(const char * name);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Load the specified dynamic-link library file, and look for a function
|
* Load the specified dynamic-link library file, and look for a function
|
||||||
@ -210,7 +210,7 @@ file_exists(const char *name)
|
|||||||
AssertArg(name != NULL);
|
AssertArg(name != NULL);
|
||||||
|
|
||||||
if (stat(name, &st) == 0)
|
if (stat(name, &st) == 0)
|
||||||
return true;
|
return S_ISDIR(st.st_mode) ? false : true;
|
||||||
else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
|
else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
|
||||||
elog(ERROR, "stat failed on %s: %s", name, strerror(errno));
|
elog(ERROR, "stat failed on %s: %s", name, strerror(errno));
|
||||||
|
|
||||||
@ -234,15 +234,14 @@ file_exists(const char *name)
|
|||||||
* the name. Else (no slash) try to expand using search path (see
|
* the name. Else (no slash) try to expand using search path (see
|
||||||
* find_in_dynamic_libpath below); if that works, return the fully
|
* find_in_dynamic_libpath below); if that works, return the fully
|
||||||
* expanded file name. If the previous failed, append DLSUFFIX and
|
* expanded file name. If the previous failed, append DLSUFFIX and
|
||||||
* try again. If all fails, return NULL. The return value is
|
* try again. If all fails, return NULL.
|
||||||
* palloc'ed.
|
|
||||||
*/
|
*/
|
||||||
static char *
|
static char *
|
||||||
expand_dynamic_library_name(const char *name)
|
expand_dynamic_library_name(const char *name)
|
||||||
{
|
{
|
||||||
bool have_slash;
|
bool have_slash;
|
||||||
char * new;
|
char * new;
|
||||||
size_t len;
|
char * full;
|
||||||
|
|
||||||
AssertArg(name);
|
AssertArg(name);
|
||||||
|
|
||||||
@ -250,28 +249,23 @@ expand_dynamic_library_name(const char *name)
|
|||||||
|
|
||||||
if (!have_slash)
|
if (!have_slash)
|
||||||
{
|
{
|
||||||
char * full;
|
|
||||||
|
|
||||||
full = find_in_dynamic_libpath(name);
|
full = find_in_dynamic_libpath(name);
|
||||||
if (full)
|
if (full)
|
||||||
return full;
|
return full;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (file_exists(name))
|
full = substitute_libpath_macro(name);
|
||||||
return pstrdup(name);
|
if (file_exists(full))
|
||||||
|
return full;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = strlen(name);
|
new = palloc(strlen(name)+ strlen(DLSUFFIX) + 1);
|
||||||
|
|
||||||
new = palloc(len + strlen(DLSUFFIX) + 1);
|
|
||||||
strcpy(new, name);
|
strcpy(new, name);
|
||||||
strcpy(new + len, DLSUFFIX);
|
strcat(new, DLSUFFIX);
|
||||||
|
|
||||||
if (!have_slash)
|
if (!have_slash)
|
||||||
{
|
{
|
||||||
char * full;
|
|
||||||
|
|
||||||
full = find_in_dynamic_libpath(new);
|
full = find_in_dynamic_libpath(new);
|
||||||
pfree(new);
|
pfree(new);
|
||||||
if (full)
|
if (full)
|
||||||
@ -279,8 +273,9 @@ expand_dynamic_library_name(const char *name)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (file_exists(new))
|
full = substitute_libpath_macro(new);
|
||||||
return new;
|
if (file_exists(full))
|
||||||
|
return full;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -288,6 +283,40 @@ expand_dynamic_library_name(const char *name)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static char *
|
||||||
|
substitute_libpath_macro(const char * name)
|
||||||
|
{
|
||||||
|
size_t macroname_len;
|
||||||
|
char * replacement = NULL;
|
||||||
|
|
||||||
|
AssertArg(name != NULL);
|
||||||
|
|
||||||
|
if (strlen(name) == 0 || name[0] != '$')
|
||||||
|
return pstrdup(name);
|
||||||
|
|
||||||
|
macroname_len = strcspn(name + 1, "/") + 1;
|
||||||
|
|
||||||
|
if (strncmp(name, "$libdir", macroname_len)==0)
|
||||||
|
replacement = LIBDIR;
|
||||||
|
else
|
||||||
|
elog(ERROR, "invalid macro name in dynamic library path");
|
||||||
|
|
||||||
|
if (name[macroname_len] == '\0')
|
||||||
|
return replacement;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char * new;
|
||||||
|
|
||||||
|
new = palloc(strlen(replacement) + (strlen(name) - macroname_len) + 1);
|
||||||
|
|
||||||
|
strcpy(new, replacement);
|
||||||
|
strcat(new, name + macroname_len);
|
||||||
|
|
||||||
|
return new;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Search for a file called 'basename' in the colon-separated search
|
* Search for a file called 'basename' in the colon-separated search
|
||||||
* path 'path'. If the file is found, the full file name is returned
|
* path 'path'. If the file is found, the full file name is returned
|
||||||
@ -312,55 +341,26 @@ find_in_dynamic_libpath(const char * basename)
|
|||||||
baselen = strlen(basename);
|
baselen = strlen(basename);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
char * piece;
|
||||||
|
const char * mangled;
|
||||||
|
|
||||||
len = strcspn(p, ":");
|
len = strcspn(p, ":");
|
||||||
|
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
elog(ERROR, "zero length dynamic_library_path component");
|
elog(ERROR, "zero length dynamic_library_path component");
|
||||||
|
|
||||||
/* substitute special value */
|
piece = palloc(len + 1);
|
||||||
if (p[0] == '$')
|
strncpy(piece, p, len);
|
||||||
{
|
piece[len] = '\0';
|
||||||
size_t varname_len = strcspn(p + 1, "/") + 1;
|
|
||||||
const char * replacement = NULL;
|
|
||||||
size_t repl_len;
|
|
||||||
|
|
||||||
if (strncmp(p, "$libdir", varname_len)==0)
|
mangled = substitute_libpath_macro(piece);
|
||||||
replacement = LIBDIR;
|
|
||||||
else
|
|
||||||
elog(ERROR, "invalid dynamic_library_path specification");
|
|
||||||
|
|
||||||
repl_len = strlen(replacement);
|
/* only absolute paths */
|
||||||
|
if (mangled[0] != '/')
|
||||||
|
elog(ERROR, "dynamic_library_path component is not absolute");
|
||||||
|
|
||||||
if (p[varname_len] == '\0')
|
full = palloc(strlen(mangled) + 1 + baselen + 1);
|
||||||
{
|
sprintf(full, "%s/%s", mangled, basename);
|
||||||
full = palloc(repl_len + 1 + baselen + 1);
|
|
||||||
snprintf(full, repl_len + 1 + baselen + 1,
|
|
||||||
"%s/%s", replacement, basename);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
full = palloc(repl_len + (len - varname_len) + 1 + baselen + 1);
|
|
||||||
|
|
||||||
strcpy(full, replacement);
|
|
||||||
strncat(full, p + varname_len, len - varname_len);
|
|
||||||
full[repl_len + (len - varname_len)] = '\0';
|
|
||||||
strcat(full, "/");
|
|
||||||
strcat(full, basename);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* regular case */
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* only absolute paths */
|
|
||||||
if (p[0] != '/')
|
|
||||||
elog(ERROR, "dynamic_library_path component is not absolute");
|
|
||||||
|
|
||||||
full = palloc(len + 1 + baselen + 1);
|
|
||||||
strncpy(full, p, len);
|
|
||||||
full[len] = '/';
|
|
||||||
strcpy(full + len + 1, basename);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DebugLvl > 1)
|
if (DebugLvl > 1)
|
||||||
elog(DEBUG, "find_in_dynamic_libpath: trying %s", full);
|
elog(DEBUG, "find_in_dynamic_libpath: trying %s", full);
|
||||||
@ -368,6 +368,7 @@ find_in_dynamic_libpath(const char * basename)
|
|||||||
if (file_exists(full))
|
if (file_exists(full))
|
||||||
return full;
|
return full;
|
||||||
|
|
||||||
|
pfree(piece);
|
||||||
pfree(full);
|
pfree(full);
|
||||||
if (p[len] == '\0')
|
if (p[len] == '\0')
|
||||||
break;
|
break;
|
||||||
|
Reference in New Issue
Block a user