1999-07-22 15:09:15 +00:00
|
|
|
<!--
|
2001-09-03 12:57:50 +00:00
|
|
|
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_language.sgml,v 1.16 2001/09/03 12:57:49 petere Exp $
|
1999-07-22 15:09:15 +00:00
|
|
|
Postgres documentation
|
|
|
|
-->
|
|
|
|
|
1999-07-06 17:16:42 +00:00
|
|
|
<refentry id="SQL-CREATELANGUAGE">
|
|
|
|
<refmeta>
|
1999-07-22 15:09:15 +00:00
|
|
|
<refentrytitle id="sql-createlanguage-title">
|
1998-05-13 05:34:00 +00:00
|
|
|
CREATE LANGUAGE
|
1999-07-06 17:16:42 +00:00
|
|
|
</refentrytitle>
|
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
|
|
|
<refnamediv>
|
|
|
|
<refname>
|
1998-05-13 05:34:00 +00:00
|
|
|
CREATE LANGUAGE
|
1999-07-06 17:16:42 +00:00
|
|
|
</refname>
|
|
|
|
<refpurpose>
|
2001-09-03 12:57:50 +00:00
|
|
|
define a new procedural language
|
1999-07-06 17:16:42 +00:00
|
|
|
</refpurpose>
|
1998-12-29 02:24:47 +00:00
|
|
|
</refnamediv>
|
1999-07-06 17:16:42 +00:00
|
|
|
<refsynopsisdiv>
|
|
|
|
<refsynopsisdivinfo>
|
1999-07-22 15:09:15 +00:00
|
|
|
<date>1999-07-20</date>
|
1999-07-06 17:16:42 +00:00
|
|
|
</refsynopsisdivinfo>
|
|
|
|
<synopsis>
|
2001-08-13 21:34:54 +00:00
|
|
|
CREATE [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">langname</replaceable>
|
1998-09-16 14:43:12 +00:00
|
|
|
HANDLER <replaceable class="parameter">call_handler</replaceable>
|
1999-07-06 17:16:42 +00:00
|
|
|
</synopsis>
|
1998-05-13 05:34:00 +00:00
|
|
|
|
1999-07-06 17:16:42 +00:00
|
|
|
<refsect2 id="R2-SQL-CREATELANGUAGE-1">
|
|
|
|
<refsect2info>
|
|
|
|
<date>1998-09-09</date>
|
|
|
|
</refsect2info>
|
|
|
|
<title>
|
1998-05-13 05:34:00 +00:00
|
|
|
Inputs
|
1999-07-06 17:16:42 +00:00
|
|
|
</title>
|
|
|
|
<para>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term>TRUSTED</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<function> TRUSTED</function> specifies that the call handler for
|
|
|
|
the language is safe; that is, it offers an unprivileged user
|
|
|
|
no functionality to bypass access restrictions. If
|
|
|
|
this keyword is omitted when registering the language,
|
|
|
|
only users with the <productname>Postgres</productname>
|
|
|
|
superuser privilege can use
|
2000-08-24 23:36:29 +00:00
|
|
|
this language to create new functions.
|
1999-07-06 17:16:42 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">langname</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the new procedural language.
|
|
|
|
The language name is case insensitive. A procedural
|
|
|
|
language cannot override one of the built-in languages of
|
|
|
|
<productname>Postgres</productname>.
|
|
|
|
</para>
|
2001-08-13 21:34:54 +00:00
|
|
|
<para>
|
|
|
|
For backward compatibility, the name may be enclosed by single
|
|
|
|
quotes.
|
|
|
|
</para>
|
1999-07-06 17:16:42 +00:00
|
|
|
</listitem>
|
1999-07-22 15:09:15 +00:00
|
|
|
</varlistentry>
|
|
|
|
|
1999-07-06 17:16:42 +00:00
|
|
|
<varlistentry>
|
|
|
|
<term>HANDLER <replaceable class="parameter">call_handler</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<replaceable class="parameter">call_handler</replaceable> is the name
|
|
|
|
of a previously
|
|
|
|
registered function that will be called to execute the PL
|
|
|
|
procedures.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
1999-07-22 15:09:15 +00:00
|
|
|
|
1998-12-29 02:24:47 +00:00
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
|
1999-07-06 17:16:42 +00:00
|
|
|
</refsect2>
|
1998-05-13 05:34:00 +00:00
|
|
|
|
1999-07-06 17:16:42 +00:00
|
|
|
<refsect2 id="R2-SQL-CREATELANGUAGE-2">
|
|
|
|
<refsect2info>
|
|
|
|
<date>1998-09-09</date>
|
|
|
|
</refsect2info>
|
|
|
|
<title>
|
1998-05-13 05:34:00 +00:00
|
|
|
Outputs
|
1999-07-06 17:16:42 +00:00
|
|
|
</title>
|
|
|
|
<para>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><computeroutput>
|
|
|
|
CREATE
|
|
|
|
</computeroutput></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
This message is returned if the language is successfully
|
|
|
|
created.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term><computeroutput>
|
|
|
|
ERROR: PL handler function <replaceable class="parameter">funcname</replaceable>() doesn't exist
|
|
|
|
</computeroutput></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
1998-12-29 02:24:47 +00:00
|
|
|
This error is returned if the function
|
|
|
|
<replaceable class="parameter">funcname</replaceable>()
|
|
|
|
is not found.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
1999-07-06 17:16:42 +00:00
|
|
|
</variablelist>
|
1998-12-29 02:24:47 +00:00
|
|
|
</para>
|
1999-07-06 17:16:42 +00:00
|
|
|
</refsect2>
|
|
|
|
</refsynopsisdiv>
|
|
|
|
|
|
|
|
<refsect1 id="R1-SQL-CREATELANGUAGE-1">
|
|
|
|
<refsect1info>
|
|
|
|
<date>1998-09-09</date>
|
|
|
|
</refsect1info>
|
|
|
|
<title>
|
1998-05-13 05:34:00 +00:00
|
|
|
Description
|
1999-07-06 17:16:42 +00:00
|
|
|
</title>
|
|
|
|
<para>
|
1998-09-16 14:43:12 +00:00
|
|
|
Using <command>CREATE LANGUAGE</command>, a
|
1999-07-06 17:16:42 +00:00
|
|
|
<productname>Postgres</productname> user can register
|
1998-09-16 14:43:12 +00:00
|
|
|
a new language with <productname>Postgres</productname>.
|
1999-07-06 17:16:42 +00:00
|
|
|
Subsequently, functions and
|
1998-05-13 05:34:00 +00:00
|
|
|
trigger procedures can be defined in this new language.
|
1998-09-16 14:43:12 +00:00
|
|
|
The user must have the <productname>Postgres</productname>
|
1999-07-06 17:16:42 +00:00
|
|
|
superuser privilege to
|
1998-05-13 05:34:00 +00:00
|
|
|
register a new language.
|
1999-07-06 17:16:42 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<refsect2 id="R2-SQL-CREATELANGUAGE-3">
|
|
|
|
<refsect2info>
|
|
|
|
<date>1998-09-09</date>
|
|
|
|
</refsect2info>
|
|
|
|
<title>
|
1998-05-13 05:34:00 +00:00
|
|
|
Writing PL handlers
|
1999-07-06 17:16:42 +00:00
|
|
|
</title>
|
2000-05-29 01:59:17 +00:00
|
|
|
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
In <productname>Postgres</productname> 7.1 and later, call handlers
|
2000-11-20 20:36:57 +00:00
|
|
|
must adhere to the "version 1" function manager interface, not the
|
|
|
|
old-style interface.
|
2000-05-29 01:59:17 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
|
1999-07-06 17:16:42 +00:00
|
|
|
<para>
|
1998-05-13 05:34:00 +00:00
|
|
|
The call handler for a procedural language must be written
|
2000-10-05 19:48:34 +00:00
|
|
|
in a compiled language such as C and registered with
|
1998-09-16 14:43:12 +00:00
|
|
|
<productname>Postgres</productname> as a function taking
|
1999-07-06 17:16:42 +00:00
|
|
|
no arguments and returning the
|
2000-05-29 01:59:17 +00:00
|
|
|
<type>opaque</type> type, a placeholder for unspecified or undefined types.
|
1998-05-13 05:34:00 +00:00
|
|
|
This prevents the call handler from being
|
|
|
|
called directly as a function from queries.
|
2000-05-29 01:59:17 +00:00
|
|
|
(However, arguments may be supplied in the actual call when a
|
|
|
|
PL function in the language offered by the handler is to be executed.)
|
1998-05-13 05:34:00 +00:00
|
|
|
</para>
|
2000-05-29 01:59:17 +00:00
|
|
|
|
1998-05-13 05:34:00 +00:00
|
|
|
<para>
|
2000-11-20 20:36:57 +00:00
|
|
|
The call handler is called in the same way as any other
|
2000-05-29 01:59:17 +00:00
|
|
|
function: it receives a pointer to a FunctionCallInfoData struct
|
|
|
|
containing argument values and information about the called function,
|
|
|
|
and it is expected to return a Datum result (and possibly set the
|
|
|
|
<literal>isnull</literal> field of the FunctionCallInfoData struct,
|
|
|
|
if it wishes to return an SQL NULL result). The difference between
|
|
|
|
a call handler and an ordinary callee function is that the
|
|
|
|
<literal>flinfo->fn_oid</literal> field of the FunctionCallInfoData
|
|
|
|
struct will contain the OID of the PL function to be called, not of
|
|
|
|
the call handler itself. The call handler must use this field to
|
|
|
|
determine which function to execute. Also, the passed argument list
|
|
|
|
has been set up according to the declaration of the target PL function,
|
|
|
|
not of the call handler.
|
1999-07-06 17:16:42 +00:00
|
|
|
</para>
|
2000-05-29 01:59:17 +00:00
|
|
|
|
1998-05-13 05:34:00 +00:00
|
|
|
<para>
|
|
|
|
It's up to the call handler to fetch the
|
|
|
|
<filename>pg_proc</filename> entry and
|
|
|
|
to analyze the argument and return types of the called
|
1998-09-16 14:43:12 +00:00
|
|
|
procedure. The AS clause from the
|
1998-05-13 05:34:00 +00:00
|
|
|
<command>CREATE FUNCTION</command> of
|
|
|
|
the procedure will be found in the <literal>prosrc</literal>
|
|
|
|
attribute of the
|
1998-09-16 14:43:12 +00:00
|
|
|
<filename>pg_proc</filename> table entry. This may be the
|
1998-05-13 05:34:00 +00:00
|
|
|
source text in the procedural
|
|
|
|
language itself (like for PL/Tcl), a pathname to a
|
2000-10-05 19:48:34 +00:00
|
|
|
file, or anything else that tells the call handler what to
|
1998-05-13 05:34:00 +00:00
|
|
|
do in detail.
|
|
|
|
</para>
|
2000-05-29 01:59:17 +00:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Often, the same function is called many times per SQL statement.
|
|
|
|
A call handler can avoid repeated lookups of information about the
|
|
|
|
called function by using the <literal>flinfo->fn_extra</literal> field.
|
|
|
|
This will initially be NULL, but can be set by the call handler to
|
|
|
|
point at information about the PL function. On subsequent calls,
|
|
|
|
if <literal>flinfo->fn_extra</literal> is already non-NULL then it
|
|
|
|
can be used and the information lookup step skipped. The call handler
|
|
|
|
must be careful that <literal>flinfo->fn_extra</literal> is made to
|
|
|
|
point at memory that will live at least until the end of the current
|
|
|
|
query, since an FmgrInfo data structure could be kept that long.
|
2000-08-24 23:36:29 +00:00
|
|
|
One way to do this is to allocate the extra data in the memory context
|
|
|
|
specified by <literal>flinfo->fn_mcxt</literal>; such data will
|
|
|
|
normally have the same lifespan as the FmgrInfo itself. But the handler
|
|
|
|
could also choose to use a longer-lived context so that it can cache
|
|
|
|
function definition information across queries.
|
2000-05-29 01:59:17 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
When a PL function is invoked as a trigger, no explicit arguments
|
|
|
|
are passed, but the FunctionCallInfoData's
|
|
|
|
<literal>context</literal> field points at a TriggerData node,
|
|
|
|
rather than being NULL as it is in a plain function call.
|
|
|
|
A PL handler should provide mechanisms for PL functions to get
|
|
|
|
at the trigger information.
|
|
|
|
</para>
|
1998-12-29 02:24:47 +00:00
|
|
|
</refsect2>
|
1998-05-13 05:34:00 +00:00
|
|
|
|
1999-07-06 17:16:42 +00:00
|
|
|
<refsect2 id="R2-SQL-CREATELANGUAGE-4">
|
|
|
|
<refsect2info>
|
|
|
|
<date>1998-09-09</date>
|
|
|
|
</refsect2info>
|
|
|
|
<title>
|
1998-05-13 05:34:00 +00:00
|
|
|
Notes
|
1999-07-06 17:16:42 +00:00
|
|
|
</title>
|
|
|
|
<para>
|
1998-09-16 14:43:12 +00:00
|
|
|
Use <command>CREATE FUNCTION</command>
|
1998-05-13 05:34:00 +00:00
|
|
|
to create a function.
|
|
|
|
</para>
|
|
|
|
<para>
|
1998-09-16 14:43:12 +00:00
|
|
|
Use <command>DROP LANGUAGE</command> to drop procedural languages.
|
1998-05-13 05:34:00 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Refer to the table <filename>pg_language</filename>
|
|
|
|
for further information:
|
2000-03-26 18:32:30 +00:00
|
|
|
<programlisting>
|
|
|
|
<computeroutput>
|
|
|
|
Table "pg_language"
|
|
|
|
Attribute | Type | Modifier
|
|
|
|
---------------+---------+----------
|
|
|
|
lanname | name |
|
|
|
|
lanispl | boolean |
|
|
|
|
lanpltrusted | boolean |
|
|
|
|
lanplcallfoid | oid |
|
|
|
|
lancompiler | text |
|
1998-09-16 14:43:12 +00:00
|
|
|
|
2000-08-24 23:36:29 +00:00
|
|
|
lanname | lanispl | lanpltrusted | lanplcallfoid | lancompiler
|
|
|
|
-------------+---------+--------------+---------------+-------------
|
|
|
|
internal | f | f | 0 | n/a
|
|
|
|
C | f | f | 0 | /bin/cc
|
|
|
|
sql | f | f | 0 | postgres
|
2000-03-26 18:32:30 +00:00
|
|
|
</computeroutput>
|
|
|
|
</programlisting>
|
1999-07-22 15:09:15 +00:00
|
|
|
</para>
|
1998-05-13 05:34:00 +00:00
|
|
|
|
1999-07-22 15:09:15 +00:00
|
|
|
<para>
|
2000-08-24 23:36:29 +00:00
|
|
|
The call handler for a procedural language must normally be written
|
2000-11-20 20:36:57 +00:00
|
|
|
in C and registered as 'internal' or 'C' language, depending
|
2000-08-24 23:36:29 +00:00
|
|
|
on whether it is linked into the backend or dynamically loaded.
|
2000-11-20 20:36:57 +00:00
|
|
|
The call handler cannot use the old-style 'C' function interface.
|
1999-07-22 15:09:15 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
At present, the definitions for a procedural language cannot be
|
|
|
|
changed once they have been created.
|
1998-05-13 05:34:00 +00:00
|
|
|
</para>
|
|
|
|
</refsect2>
|
|
|
|
</refsect1>
|
1999-07-06 17:16:42 +00:00
|
|
|
|
|
|
|
<refsect1 id="R1-SQL-CREATELANGUAGE-6">
|
|
|
|
<title>
|
1998-05-13 05:34:00 +00:00
|
|
|
Usage
|
1999-07-06 17:16:42 +00:00
|
|
|
</title>
|
|
|
|
<para>
|
2000-10-05 19:48:34 +00:00
|
|
|
This is a template for a PL handler written in C:
|
1999-07-06 17:16:42 +00:00
|
|
|
<programlisting>
|
|
|
|
#include "executor/spi.h"
|
|
|
|
#include "commands/trigger.h"
|
|
|
|
#include "utils/elog.h"
|
2000-05-29 01:59:17 +00:00
|
|
|
#include "fmgr.h"
|
1999-07-06 17:16:42 +00:00
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "utils/syscache.h"
|
|
|
|
#include "catalog/pg_proc.h"
|
|
|
|
#include "catalog/pg_type.h"
|
1998-05-13 05:34:00 +00:00
|
|
|
|
2000-11-20 20:36:57 +00:00
|
|
|
PG_FUNCTION_INFO_V1(plsample_call_handler);
|
|
|
|
|
1999-07-06 17:16:42 +00:00
|
|
|
Datum
|
2000-05-29 01:59:17 +00:00
|
|
|
plsample_call_handler(PG_FUNCTION_ARGS)
|
1999-07-06 17:16:42 +00:00
|
|
|
{
|
|
|
|
Datum retval;
|
1998-05-13 05:34:00 +00:00
|
|
|
|
2000-05-29 01:59:17 +00:00
|
|
|
if (CALLED_AS_TRIGGER(fcinfo))
|
|
|
|
{
|
1999-07-06 17:16:42 +00:00
|
|
|
/*
|
2000-05-29 01:59:17 +00:00
|
|
|
* Called as a trigger procedure
|
1999-07-06 17:16:42 +00:00
|
|
|
*/
|
2000-05-29 01:59:17 +00:00
|
|
|
TriggerData *trigdata = (TriggerData *) fcinfo->context;
|
1998-05-13 05:34:00 +00:00
|
|
|
|
1999-07-06 17:16:42 +00:00
|
|
|
retval = ...
|
|
|
|
} else {
|
|
|
|
/*
|
2000-05-29 01:59:17 +00:00
|
|
|
* Called as a function
|
1999-07-06 17:16:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
retval = ...
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
1998-05-13 05:34:00 +00:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Only a few thousand lines of code have to be added instead
|
1998-09-16 14:43:12 +00:00
|
|
|
of the dots to complete the PL call handler.
|
1999-07-06 17:16:42 +00:00
|
|
|
See <command>CREATE FUNCTION</command> for information on how to compile
|
|
|
|
it into a loadable module.
|
|
|
|
</para>
|
1998-05-13 05:34:00 +00:00
|
|
|
<para>
|
|
|
|
The following commands then register the sample procedural
|
1998-09-16 14:43:12 +00:00
|
|
|
language:
|
1998-12-29 02:24:47 +00:00
|
|
|
<programlisting>
|
1999-07-06 17:16:42 +00:00
|
|
|
CREATE FUNCTION plsample_call_handler () RETURNS opaque
|
1998-09-16 14:43:12 +00:00
|
|
|
AS '/usr/local/pgsql/lib/plsample.so'
|
2001-08-13 21:34:54 +00:00
|
|
|
LANGUAGE C;
|
|
|
|
CREATE LANGUAGE plsample
|
|
|
|
HANDLER plsample_call_handler;
|
1998-12-29 02:24:47 +00:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
1999-07-06 17:16:42 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 id="R1-SQL-CREATELANGUAGE-7">
|
|
|
|
<title>
|
1998-05-13 05:34:00 +00:00
|
|
|
Compatibility
|
1999-07-06 17:16:42 +00:00
|
|
|
</title>
|
|
|
|
|
|
|
|
<refsect2 id="R2-SQL-CREATELANGUAGE-5">
|
|
|
|
<refsect2info>
|
|
|
|
<date>1998-09-09</date>
|
|
|
|
</refsect2info>
|
|
|
|
<title>
|
1998-05-13 05:34:00 +00:00
|
|
|
SQL92
|
1999-07-06 17:16:42 +00:00
|
|
|
</title>
|
1999-07-22 15:09:15 +00:00
|
|
|
|
1999-07-06 17:16:42 +00:00
|
|
|
<para>
|
1999-07-22 15:09:15 +00:00
|
|
|
<command>CREATE LANGUAGE</command>
|
|
|
|
is a <productname>Postgres</productname> extension.
|
1998-09-22 15:48:03 +00:00
|
|
|
There is no <command>CREATE LANGUAGE</command> statement in
|
1999-07-06 17:16:42 +00:00
|
|
|
<acronym>SQL92</acronym>.
|
|
|
|
</para>
|
1998-05-13 05:34:00 +00:00
|
|
|
</refsect2>
|
|
|
|
</refsect1>
|
1999-07-06 17:16:42 +00:00
|
|
|
</refentry>
|
1998-05-13 05:34:00 +00:00
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
1999-07-06 17:16:42 +00:00
|
|
|
sgml-omittag:nil
|
1998-05-13 05:34:00 +00:00
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"../reference.ced"
|
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:"/usr/lib/sgml/catalog"
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|