©
This document usesPHP Chinese website manualRelease
A procedural language must be"installed"into each database where it is to be used. But procedural languages installed in the databasetemplate1are automatically available in all subsequently created databases, since their entries intemplate1will be copied byCREATE DATABASE. So the database administrator can decide which languages are available in which databases and can make some languages available by default if he chooses.
For the languages supplied with the standard distribution, it is only necessary to executeCREATE LANGUAGElanguage_nameto install the language into the current database. Alternatively, the programcreatelangcan be used to do this from the shell command line. For example, to install the languagePL/Perlinto the databasetemplate1, use:
createlang plperl template1
The manual procedure described below is only recommended for installing custom languages thatCREATE LANGUAGEdoes not know about.
Manual Procedural Language Installation
A procedural language is installed in a database in five steps, which must be carried out by a database superuser. (For languages known toCREATE LANGUAGE, the second through fourth steps can be omitted, because they will be carried out automatically if needed.)
The shared object for the language handler must be compiled and installed into an appropriate library directory. This works in the same way as building and installing modules with regular user-defined C functions does; see Section 35.9.6. Often, the language handler will depend on an external library that provides the actual programming language engine; if so, that must be installed as well.
The handler must be declared with the command
CREATE FUNCTIONhandler_function_name() RETURNS language_handler AS 'path-to-shared-object' LANGUAGE C;
The special return type oflanguage_handlertells the database system that this function does not return one of the definedSQLdata types and is not directly usable inSQLstatements.
Optionally, the language handler can provide an"inline"handler function that executes anonymous code blocks (DO commands) written in this language. If an inline handler function is provided by the language, declare it with a command like
CREATE FUNCTIONinline_function_name(internal) RETURNS void AS 'path-to-shared-object' LANGUAGE C;
Optionally, the language handler can provide a"validator"function that checks a function definition for correctness without actually executing it. The validator function is called byCREATE FUNCTIONif it exists. If a validator function is provided by the language, declare it with a command like
CREATE FUNCTIONvalidator_function_name(oid) RETURNS void AS 'path-to-shared-object' LANGUAGE C;
The PL must be declared with the command
CREATE [TRUSTED] [PROCEDURAL] LANGUAGElanguage-nameHANDLERhandler_function_name[INLINEinline_function_name] [VALIDATORvalidator_function_name] ;
The optional key wordTRUSTEDspecifies that the language does not grant access to data that the user would not otherwise have. Trusted languages are designed for ordinary database users (those without superuser privilege) and allows them to safely create of functions and trigger procedures. Since PL functions are executed inside the database server, theTRUSTEDflag should only be given for languages that do not allow access to database server internals or the file system. The languagesPL/pgSQL,PL/Tcl, andPL/Perlare considered trusted; the languagesPL/TclU,PL/PerlU, andPL/PythonUare designed to provide unlimited functionality and shouldnotbe marked trusted.
Example 38-1 shows how the manual installation procedure would work with the languagePL/Perl.
Example 38-1. Manual Installation ofPL/Perl
The following command tells the database server where to find the shared object for thePL/Perllanguage's call handler function:
CREATE FUNCTION plperl_call_handler() RETURNS language_handler AS '$libdir/plperl' LANGUAGE C;
PL/Perlhas an inline handler function and a validator function, so we declare those too:
CREATE FUNCTION plperl_inline_handler(internal) RETURNS void AS '$libdir/plperl' LANGUAGE C; CREATE FUNCTION plperl_validator(oid) RETURNS void AS '$libdir/plperl' LANGUAGE C;
The command:
CREATE TRUSTED PROCEDURAL LANGUAGE plperl HANDLER plperl_call_handler INLINE plperl_inline_handler VALIDATOR plperl_validator;
then defines that the previously declared functions should be invoked for functions and trigger procedures where the language attribute isplperl.
In a defaultPostgreSQLinstallation, the handler for thePL/pgSQLlanguage is built and installed into the"library"directory; furthermore, thePL/pgSQLlanguage itself is installed in all databases. IfTclsupport is configured in, the handlers forPL/TclandPL/TclUare built and installed in the library directory, but the language itself is not installed in any database by default. Likewise, thePL/PerlandPL/PerlUhandlers are built and installed if Perl support is configured, and thePL/PythonUhandler is installed if Python support is configured, but these languages are not installed by default.