CREATE LANGUAGE 中文man頁面
NAME
CREATE LANGUAGE - 定義一種新的過程語言
SYNOPSIS
CREATE [ TRUSTED ] [ PROCEDURAL ] LANGUAGE name HANDLER call_handler [ VALIDATOR valfunction ]
DESCRIPTION 描述
使用 CREATE LANGUAGE, 一個PostgreSQL 用戶可以在 PostgreSQL里注冊一個新的語言。 因而,函數(shù)和觸發(fā)器過程可以用這種新語言定義。要注冊新語言用戶必須具有 PostgreSQL 超級用戶權(quán)限。
CREATE LANGUAGE 將該語言的名字和一個調(diào)用句柄關(guān)聯(lián)起來,而該調(diào)用句柄負(fù)責(zé)執(zhí)行該語言書寫的函數(shù)。 請參考 ``User-Defined Functions'' 獲取有關(guān)語言調(diào)用句柄的更多信息。
請注意過程語言是對每個獨(dú)立的數(shù)據(jù)庫而言是自己的。 要讓一種語言缺省時可以為所有數(shù)據(jù)庫獲得,那你應(yīng)該把它安裝到 template1 數(shù)據(jù)庫里。
PARAMETERS 參數(shù)
- TRUSTED
- TRUSTED 說明對該語言的調(diào)用句柄是安全的; 也就是說,它不會提供給非特權(quán)用戶任何繞過訪問限制的能力。 如果忽略這個關(guān)鍵字,只有具有 PostgreSQL 超級用戶權(quán)限的人可以使用這個語言創(chuàng)建新的函數(shù)。
- PROCEDURAL
這是個沒有用的字。- name
新的過程化語言的名稱。語言名是大小寫無關(guān)的。 這個名字應(yīng)該在數(shù)據(jù)庫的所有語言中***。
出于向下兼容的原因,這個名字可以用單引號包圍。- HANDLER call_handler
- call_handler 是一個以前注冊過的函數(shù)的名字,該函數(shù)將被調(diào)用來執(zhí)行這門過程語言寫的函數(shù)。 過程語言的調(diào)用句柄必須用一種編譯語言書寫,比如 C,調(diào)用風(fēng)格必須是版本 1 的調(diào)用風(fēng)格, 并且在 PostgreSQL 里注冊為不接受參數(shù)并且返回 language_handler 類型的函數(shù), language_handler 是用于將函數(shù)聲明為調(diào)用句柄的占位符。
- VALIDATOR valfunction
- valfunction 是一個已經(jīng)注冊的函數(shù)的名字, 在用該語言創(chuàng)建新函數(shù)的時候?qū)⒄{(diào)用它來校驗新函數(shù)。如果沒有聲明校驗函數(shù),那么建立新函數(shù)的時候就不會檢查它。 校驗函數(shù)必須接受一個類型為 oid 的參數(shù),它是將要創(chuàng)建的函數(shù)的 OID,并且通常會返回 void。
校驗函數(shù)通常會檢查函數(shù)體,看看看有沒有語法錯誤,但是它也可以查看函數(shù)的其它屬性, 比如該語言是否不能處理某種參數(shù)類型。要發(fā)出一個錯誤,校驗函數(shù)應(yīng)該用 elog() 函數(shù)。 該函數(shù)的返回值將被忽略。
NOTES 注意
這條命令通常不應(yīng)該由用戶直接執(zhí)行。 對于 PostgreSQL 版本里提供的過程語言, 我們應(yīng)該使用 createlang(1) 程序, 它將為我們安裝正確的調(diào)用句柄。 (createlang 也會在內(nèi)部調(diào)用 CREATE LANGUAGE。)
在 PostgreSQL 版本 7.3 之前, 我們必須聲明句柄函數(shù)返回占位類型 opaque,而不是 language_handler。 為了支持裝載舊的轉(zhuǎn)儲文件,CREATE LANGUAGE 還將接受聲明為返回 opaque 的函數(shù), 但是它會發(fā)出一條通知并且把函數(shù)聲明返回類型改為 language_handler。
使用 CREATE FUNCTION [create_function(7)] 命令創(chuàng)建新函數(shù)。
使用 DROP LANGUAGE [drop_language(7)],或者更好是 droplang(1) 程序刪除一個過程語言。
系統(tǒng)表 pg_language (參閱 ``System Catalogs'') 記錄了更多有關(guān)當(dāng)前安裝的過程語言的信息。createlang 也有一個選項列出已安裝的語言。
目前,除了權(quán)限之外,一種過程語言創(chuàng)建之后它的定義就不能再更改。
要使用一種過程語言,用戶必須被賦予 USAGE 權(quán)限。 如果該語言已知是可信的,那么 createlang 程序自動給每個人賦予權(quán)限。
EXAMPLES 例子
下面兩條順序執(zhí)行的命令將注冊一門新的過程語言及其關(guān)聯(lián)的調(diào)用句柄。
CREATE FUNCTION plsample_call_handler() RETURNS language_handler AS '$libdir/plsample' LANGUAGE C; CREATE LANGUAGE plsample HANDLER plsample_call_handler;
COMPATIBILITY 兼容性
CREATE LANGUAGE 是 PostgreSQL 擴(kuò)展。
SEE ALSO 參見
ALTER LANGUAGE [alter_language(7)], CREATE FUNCTION [create_function(l)], DROP LANGUAGE [drop_language(l)], GRANT [grant(l)], REVOKE [revoke(l)], createlang(1), droplang(1)
#p#
NAME
CREATE LANGUAGE - define a new procedural language
SYNOPSIS
CREATE [ TRUSTED ] [ PROCEDURAL ] LANGUAGE name HANDLER call_handler [ VALIDATOR valfunction ]
DESCRIPTION
Using CREATE LANGUAGE, a PostgreSQL user can register a new procedural language with a PostgreSQL database. Subsequently, functions and trigger procedures can be defined in this new language. The user must have the PostgreSQL superuser privilege to register a new language.
CREATE LANGUAGE effectively associates the language name with a call handler that is responsible for executing functions written in the language. Refer to the section called ``User-Defined Functions'' in the documentation for more information about language call handlers.
Note that procedural languages are local to individual databases. To make a language available in all databases by default, it should be installed into the template1 database.
PARAMETERS
- TRUSTED
- TRUSTED specifies that the call handler for the language is safe, that is, it does not offer an unprivileged user any functionality to bypass access restrictions. 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 language name is case insensitive. The name must be unique among the languages in the database.
For backward compatibility, the name may be enclosed by single quotes.
- HANDLER call_handler
- call_handler is the name of a previously registered function that will be called to execute the procedural language 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.
- 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
This command normally should not be executed directly by users. For the procedural languages supplied in the PostgreSQL distribution, the createlang(1) program should be used, which will also install the correct call handler. (createlang will call CREATE LANGUAGE internally.)
In PostgreSQL versions before 7.3, it was necessary to declare handler functions as returning the placeholder type opaque, rather than language_handler. To support loading of old dump files, CREATE LANGUAGE will accept a function declared as returning opaque, but it will issue a notice and change the function's declared return type to language_handler.
Use the CREATE FUNCTION [create_function(7)] command to create a new function.
Use DROP LANGUAGE [drop_language(7)], or better yet the droplang(1) program, to drop procedural languages.
The system catalog pg_language (see the chapter called ``System Catalogs'' in the documentation) records information about the currently installed languages. Also createlang has an option to list the installed languages.
The definition of a procedural language cannot be changed once it has been created, with the exception of the privileges.
To be able to use a procedural language, a user must be granted the USAGE privilege. The createlang program automatically grants permissions to everyone if the language is known to be trusted.
EXAMPLES
The following two commands executed in sequence will register a new procedural language and the associated call handler.
CREATE FUNCTION plsample_call_handler() RETURNS language_handler AS '$libdir/plsample' LANGUAGE C; CREATE LANGUAGE plsample HANDLER plsample_call_handler;
COMPATIBILITY
CREATE LANGUAGE is a PostgreSQL extension.
SEE ALSO
ALTER LANGUAGE [alter_language(7)], CREATE FUNCTION [create_function(l)], DROP LANGUAGE [drop_language(l)], GRANT [grant(l)], REVOKE [revoke(l)], createlang(1), droplang(1)