Postgresql 中文操作指南

CREATE LANGUAGE

CREATE LANGUAGE——定义新的过程语言

CREATE LANGUAGE — define a new procedural language

Synopsis

CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE name
    HANDLER call_handler [ INLINE inline_handler ] [ VALIDATOR valfunction ]
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE name

Description

CREATE LANGUAGE 使用 PostgreSQL 数据库注册新的过程语言。随后,可以用这种新语言定义函数和过程。

CREATE LANGUAGE registers a new procedural language with a PostgreSQL database. Subsequently, functions and procedures can be defined in this new language.

CREATE LANGUAGE 实际上将语言名称与负责执行用该语言编写的函数的处理程序函数关联起来。有关语言处理程序的更多信息,请参考 Chapter 58

CREATE LANGUAGE effectively associates the language name with handler function(s) that are responsible for executing functions written in the language. Refer to Chapter 58 for more information about language handlers.

CREATE OR REPLACE LANGUAGE 将创建一种新语言或替换现有定义。如果语言已存在,那么它的参数将根据命令进行更新,但语言的所有权和权限设置不会更改,并且假设用该语言编写的任何现有函数仍然有效。

CREATE OR REPLACE LANGUAGE will either create a new language, or replace an existing definition. If the language already exists, its parameters are updated according to the command, but the language’s ownership and permissions settings do not change, and any existing functions written in the language are assumed to still be valid.

必须具有 PostgreSQL 超级用户权限才能注册新语言或更改现有语言的参数。但是,一旦创建了语言,就有权将其所有权分配给非超级用户,而后者可以将其删除、更改其权限、重命名或将其分配给新所有者。(但是,不要将基础 C 函数的所有权分配给非超级用户;这会为该用户创建权限升级路径。)

One must have the PostgreSQL superuser privilege to register a new language or change an existing language’s parameters. However, once the language is created it is valid to assign ownership of it to a non-superuser, who may then drop it, change its permissions, rename it, or assign it to a new owner. (Do not, however, assign ownership of the underlying C functions to a non-superuser; that would create a privilege escalation path for that user.)

不提供任何处理程序函数的 CREATE LANGUAGE 形式已过时。为了与旧转储文件保持向后兼容性,它被解释为 CREATE EXTENSION 。如果已将语言打包到同名的扩展中,那么这种方式将会奏效,这是设置过程语言的传统方法。

The form of CREATE LANGUAGE that does not supply any handler function is obsolete. For backwards compatibility with old dump files, it is interpreted as CREATE EXTENSION. That will work if the language has been packaged into an extension of the same name, which is the conventional way to set up procedural languages.

Parameters

  • TRUSTED

    • TRUSTED specifies that the language does not grant access to data that the user would not otherwise have. If this key word is omitted when registering the language, only users with the PostgreSQL superuser privilege can use this language to create new functions.

  • PROCEDURAL

    • This is a noise word.

  • name

    • The name of the new procedural language. The name must be unique among the languages in the database.

  • HANDLER call_handler

    • call_handler is the name of a previously registered function that will be called to execute the procedural language’s functions. The call handler for a procedural language must be written in a compiled language such as C with version 1 call convention and registered with PostgreSQL as a function taking no arguments and returning the language_handler type, a placeholder type that is simply used to identify the function as a call handler.

  • INLINE inline_handler

    • inline_handler is the name of a previously registered function that will be called to execute an anonymous code block (DO command) in this language. If no inline_handler function is specified, the language does not support anonymous code blocks. The handler function must take one argument of type internal, which will be the DO command’s internal representation, and it will typically return void. The return value of the handler is ignored.

  • VALIDATOR valfunction

    • valfunction is the name of a previously registered function that will be called when a new function in the language is created, to validate the new function. If no validator function is specified, then a new function will not be checked when it is created. The validator function must take one argument of type oid, which will be the OID of the to-be-created function, and will typically return void.

    • A validator function would typically inspect the function body for syntactical correctness, but it can also look at other properties of the function, for example if the language cannot handle certain argument types. To signal an error, the validator function should use the ereport() function. The return value of the function is ignored.

Notes

使用 ` DROP LANGUAGE ` 放弃过程语言。

Use DROP LANGUAGE to drop procedural languages.

系统目录 ` pg_language `(请参阅 ` Section 53.29 `)记录有关当前已安装语言的信息。此外,psql 命令 ` \dL ` 列出已安装的语言。

The system catalog pg_language (see Section 53.29) records information about the currently installed languages. Also, the psql command \dL lists the installed languages.

要在过程语言中创建函数,用户必须具有该语言的 ` USAGE ` 权限。默认情况下,` USAGE ` 授予 ` PUBLIC `(即所有人)受信任的语言。如果需要,可以撤销该权限。

To create functions in a procedural language, a user must have the USAGE privilege for the language. By default, USAGE is granted to PUBLIC (i.e., everyone) for trusted languages. This can be revoked if desired.

过程语言属于各个数据库的本地语言。但是,可以将语言安装到 ` template1 ` 数据库中,这将导致它自动在所有随后创建的数据库中可用。

Procedural languages are local to individual databases. However, a language can be installed into the template1 database, which will cause it to be available automatically in all subsequently-created databases.

Examples

创建新过程语言的最小顺序如下:

A minimal sequence for creating a new procedural language is:

CREATE FUNCTION plsample_call_handler() RETURNS language_handler
    AS '$libdir/plsample'
    LANGUAGE C;
CREATE LANGUAGE plsample
    HANDLER plsample_call_handler;

通常将它编写在扩展的创建脚本中,用户可以这样做来安装扩展:

Typically that would be written in an extension’s creation script, and users would do this to install the extension:

CREATE EXTENSION plsample;

Compatibility

` CREATE LANGUAGE ` 是一个 PostgreSQL 扩展。

CREATE LANGUAGE is a PostgreSQL extension.