Postgresql 中文操作指南

42.1. Installing Procedural Languages #

过程语言必须“安装”到要使用它的每个数据库中。但是 template1 数据库中安装的过程语言在所有后续创建的数据库中都会自动可用,因为 template1 中各自的条目将被 CREATE DATABASE 复制。因此,数据库管理员可以决定哪些语言可在哪些数据库中使用,并且可以在需要时使某些语言默认可用。

A procedural language must be “installed” into each database where it is to be used. But procedural languages installed in the database template1 are automatically available in all subsequently created databases, since their entries in template1 will be copied by CREATE DATABASE. So the database administrator can decide which languages are available in which databases and can make some languages available by default if desired.

对于标准发行版中提供的语言,只需要执行 CREATE EXTENSION language_name 将语言安装到当前数据库中。仅建议对未打包为扩展的语言使用下面描述的手动程序进行安装。

For the languages supplied with the standard distribution, it is only necessary to execute CREATE EXTENSION language_name to install the language into the current database. The manual procedure described below is only recommended for installing languages that have not been packaged as extensions.

Manual Procedural Language Installation

数据库中的过程语言通过五步来安装,该过程必须由数据库超级用户执行。在大多数情况下,必需的 SQL 命令应打包为“扩展”的安装脚本,以便 CREATE EXTENSION 可以用来执行它们。

A procedural language is installed in a database in five steps, which must be carried out by a database superuser. In most cases the required SQL commands should be packaged as the installation script of an “extension”, so that CREATE EXTENSION can be used to execute them.

Example 42.1显示了手动安装过程如何与 PL/Perl 语言一起使用。

Example 42.1 shows how the manual installation procedure would work with the language PL/Perl.

Example 42.1. Manual Installation of PL/Perl

Example 42.1. Manual Installation of PL/Perl

以下命令告诉数据库服务器在何处找到 PL/Perl 语言的调用处理程序函数的共享对象:

The following command tells the database server where to find the shared object for the PL/Perl language’s call handler function:

CREATE FUNCTION plperl_call_handler() RETURNS language_handler AS
    '$libdir/plperl' LANGUAGE C;

PL/Perl 具有一个内联处理程序功能和一个验证程序,因此我们也声明了这些内容:

PL/Perl has 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 STRICT;

CREATE FUNCTION plperl_validator(oid) RETURNS void AS
    '$libdir/plperl' LANGUAGE C STRICT;

该命令:

The command:

CREATE TRUSTED LANGUAGE plperl
    HANDLER plperl_call_handler
    INLINE plperl_inline_handler
    VALIDATOR plperl_validator;

然后定义之前声明的函数应对将语言属性设为 plperl 的函数和过程进行调用。

then defines that the previously declared functions should be invoked for functions and procedures where the language attribute is plperl.

在默认的 PostgreSQL 安装中,PL/pgSQL 语言的处理程序被构建并安装到“library”目录中;此外,PL/pgSQL 语言本身也被安装在所有数据库中。如果配置了 Tcl 支持,PL/Tcl 和 PL/TclU 的处理程序将被构建并安装在 library 目录中,但语言本身在任何数据库中默认情况下并不会安装。同样,如果配置了 Perl 支持,PL/Perl 和 PL/PerlU 的处理程序将被构建并安装,并且如果配置了 Python 支持,PL/PythonU 的处理程序也将被安装,但默认情况下不会安装这些语言。

In a default PostgreSQL installation, the handler for the PL/pgSQL language is built and installed into the “library” directory; furthermore, the PL/pgSQL language itself is installed in all databases. If Tcl support is configured in, the handlers for PL/Tcl and PL/TclU are built and installed in the library directory, but the language itself is not installed in any database by default. Likewise, the PL/Perl and PL/PerlU handlers are built and installed if Perl support is configured, and the PL/PythonU handler is installed if Python support is configured, but these languages are not installed by default.