CREATE FUNCTION 中文man頁(yè)面
NAME
CREATE FUNCTION - 定義一個(gè)新函數(shù)
SYNOPSIS
CREATE [ OR REPLACE ] FUNCTION name ( [ argtype [, ...] ] ) RETURNS rettype { LANGUAGE langname | IMMUTABLE | STABLE | VOLATILE | CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT | [EXTERNAL] SECURITY INVOKER | [EXTERNAL] SECURITY DEFINER | AS 'definition' | AS 'obj_file', 'link_symbol' } ... [ WITH ( attribute [, ...] ) ]
DESCRIPTION 描述
CREATE FUNCTION 定義一個(gè)新的函數(shù)。 CREATE OR REPLACE FUNCTION 將要么創(chuàng)建一個(gè)新函數(shù), 要么替換現(xiàn)有的定義。
如果包含了一個(gè)模式名,那么函數(shù)就在指定的模式中創(chuàng)建。否則它會(huì)在當(dāng)前模式中創(chuàng)建。 新函數(shù)的名字不能和同一個(gè)模式中的任何帶有同樣參數(shù)類型的函數(shù)同名。 不過,參數(shù)類型不同的函數(shù)可以同名(這叫做重載)。
要更新一個(gè)現(xiàn)有函數(shù)的定義,使用 CREATE OR REPLACE FUNCTION。 我們不能用這個(gè)方法修改一個(gè)函數(shù)的名字或者參數(shù)類型(如果你這么干,那么你就會(huì)創(chuàng)建一個(gè)新的,不同的函數(shù))。 同樣,CREATE OR REPLACE FUNCTION 也不會(huì)允許你修改一個(gè)現(xiàn)有函數(shù)的返回類型。 要做這些事情,你必須刪除并重新創(chuàng)建函數(shù)。
如果你刪除然后重建一個(gè)函數(shù),新函數(shù)和舊的將是不同的實(shí)體;你會(huì)破壞現(xiàn)有規(guī)則,視圖,觸發(fā)器等等。 使用 CREATE OR REPLACE FUNCTION 可以在不破壞引用該函數(shù)的對(duì)象的前提下修改函數(shù)定義。
創(chuàng)建這個(gè)函數(shù)的用戶成為函數(shù)的所有者。
PARAMETERS 參數(shù)
- name
要?jiǎng)?chuàng)建的函數(shù)名字。- argtype
該函數(shù)的數(shù)據(jù)類型(可以有模式修飾)。如果有的話,參數(shù)類型可以是基本類型,也可以是復(fù)合類型,域類型,或者和一個(gè)現(xiàn)有字段相同的類型。
一個(gè)字段的類型是用 tablename.columnname%TYPE 表示的;使用這個(gè)東西可以幫助函數(shù)獨(dú)立于表定義的修改。
根據(jù)實(shí)現(xiàn)語(yǔ)言的不同,我們還可以在這上面聲明 "偽類型", 比如 cstring。偽類型表示實(shí)際的參數(shù)類型要么是沒有完整地聲明,要么是在普通的 SQL 數(shù)據(jù)類型之外。- rettype
返回?cái)?shù)據(jù)類型。輸出類型可以聲明為一個(gè)基本類型,復(fù)合類型,域類型, 或者從現(xiàn)有字段拷貝。參閱上面 argtype 的描述獲取如何引用一個(gè)現(xiàn)有類型的信息。
根據(jù)實(shí)現(xiàn)語(yǔ)言的不同,我們還可以在這上面聲明 "偽類型", 比如 cstring。 SETOF 修飾詞表示該函數(shù)將返回一套條目, 而不是一條條目。- langname
用以實(shí)現(xiàn)函數(shù)的語(yǔ)言的名字。 可以是 SQL,C, internal,或者是用戶定義的過程語(yǔ)言名字。 (又見 createlang。 ) 為了保持向下兼容,該名字可以用單引號(hào)包圍。- IMMUTABLE
- STABLE
- VOLATILE
這些屬性告訴系統(tǒng)把對(duì)該函數(shù)的多次調(diào)用替換成一次是否安全。 主要用于運(yùn)行時(shí)優(yōu)化。 至少應(yīng)該聲明一個(gè)選擇。如果任何一個(gè)都沒有出現(xiàn),那么 VOLATILE 是缺省假設(shè)。IMMUTABLE 表示該函數(shù)在給出同樣的參數(shù)值時(shí)總是返回相同的結(jié)果; 也就是說,它不做數(shù)據(jù)庫(kù)查找或者是使用那些并沒有直接出現(xiàn)在其參數(shù)列表里面的信息。 如果給出這個(gè)選項(xiàng),那么任何帶著全部是常量參數(shù)對(duì)該函數(shù)的調(diào)用都將立即替換為該函數(shù)的值。
STABLE 表示在一次表掃描里,對(duì)相同參數(shù)值, 該函數(shù)將穩(wěn)定返回相同的值,但是它的結(jié)果可能在不同 SQL 語(yǔ)句之間變化。 這個(gè)選項(xiàng)對(duì)那些結(jié)果倚賴數(shù)據(jù)庫(kù)查找,參數(shù)變量(比如當(dāng)前時(shí)區(qū)),等等的函數(shù)是很合適的。 還要注意 current_timestamp 族函數(shù)是 stable (穩(wěn)定)的,因?yàn)樗鼈兊闹翟谝淮问聞?wù)中不會(huì)變化。
VOLATILE 表示該函數(shù)值甚至可以在一次表掃描內(nèi)改變, 因此不會(huì)做任何優(yōu)化。很少數(shù)據(jù)庫(kù)函數(shù)在這個(gè)概念上是易變的; 一些例子是 random(),currval(), timeofday()。請(qǐng)注意任何有副作用的函數(shù)都必需列為易變類, 即使其結(jié)果相當(dāng)有規(guī)律也應(yīng)該這樣,這樣才能避免它被優(yōu)化;一個(gè)例子就是 setval()。
- CALLED ON NULL INPUT
- RETURNS NULL ON NULL INPUT
- STRICT
- CALLED ON NULL INPUT (缺?。┍砻髟摵瘮?shù)在自己的某些參數(shù)是空值的時(shí)候還是可以按照正常的方式調(diào)用。 剩下的事情是函數(shù)的作者必須負(fù)責(zé)檢查空值以及相應(yīng)地做出反應(yīng)。
RETURNS NULL ON NULL INPUT 或 STRICT 表明如果它的任何參數(shù)是 NULL,此函數(shù)總是返回 NULL。 如果聲明了這個(gè)參數(shù),則如果存在 NULL 參數(shù)時(shí)不會(huì)執(zhí)行該函數(shù); 而只是自動(dòng)假設(shè)一個(gè) NULL 結(jié)果。
- [EXTERNAL] SECURITY INVOKER
- [EXTERNAL] SECURITY DEFINER
- SECURITY INVOKER 表明該函數(shù)將帶著調(diào)用它的用戶的權(quán)限執(zhí)行。 這是缺省。SECURITY DEFINER 聲明該函數(shù)將以創(chuàng)建它的用戶的權(quán)限執(zhí)行。
關(guān)鍵字 EXTERNAL 的目的是和 SQL 兼容, 但是我們和 SQL 不同的是,這個(gè)特性不僅僅適用于外部的函數(shù), 所以它是可選的。 - definition
一個(gè)定義函數(shù)的字串;含義取決于語(yǔ)言。它可以是一個(gè)內(nèi)部函數(shù)名字, 一個(gè)指向某個(gè)目標(biāo)文件的路徑,一個(gè) SQL 查詢,或者一個(gè)用過程語(yǔ)言寫的文本。- obj_file, link_symbol
這個(gè)形式的 AS 子句用于在函數(shù)的 C 源文件里的函數(shù)名字和 SQL 函數(shù)的名字不同的時(shí)候可動(dòng)態(tài)裝載 C 語(yǔ)言函數(shù)。 字串 obj_file 是包含可動(dòng)態(tài)裝載的對(duì)象的文件名,而 link_symbol 是函數(shù)的鏈接符號(hào),也就是該函數(shù)在 C 源文件里的名字。 如果省略了鏈接符號(hào),那么就假設(shè)它和被定義的 SQL 函數(shù)同名。- attribute
歷史遺留的函數(shù)可選信息。下面的屬性可以在此出現(xiàn):- isStrict
等效于 STRICT 或者 RETURNS NULL ON NULL INPUT- isCachable
- isCachable 是 IMMUTABLE 的過時(shí)的等效物;不過出于向下兼容,我們?nèi)匀唤邮芩?
屬性名是大小寫無(wú)關(guān)的。
NOTES 注意
請(qǐng)參閱 ``User-Defined Functions'' 獲取更多關(guān)于書寫函數(shù)的信息。
我們?cè)试S你將完整的 SQL 類型語(yǔ)法用于輸入?yún)?shù)和返回值。 不過,有些類型聲明的細(xì)節(jié)(比如,numeric 類型的精度域)是由下層函數(shù)實(shí)現(xiàn)負(fù)責(zé)的, 并且會(huì)被 CREATE FUNCTION 命令悄悄地吞掉。 (也就是說,不再被識(shí)別或強(qiáng)制)。
PostgreSQL 允許函數(shù)重載; 也就是說,同一個(gè)函數(shù)名可以用于幾個(gè)不同的函數(shù), 只要它們的參數(shù)可以區(qū)分它們。不過,所有函數(shù)的 C 名字必須不同, 也就是說你必須給予重載的 C 函數(shù)不同的 C 名字(比如,使用參數(shù)類型作為 C 名字的一部分)。
如果重復(fù)調(diào)用 CREATE FUNCTION,并且都指向同一個(gè)目標(biāo)文件, 那么該文件只裝載一次。要卸載和恢復(fù)裝載該文件(可能是在開發(fā)過程中),你可以使用 LOAD [load(7)] 命令。
使用 DROP FUNCTION 刪除一個(gè)用戶定義函數(shù)。
函數(shù)定義里面的任何單引號(hào)或者反斜杠都必須用寫雙份的方式逃逸。
要能定義函數(shù),用戶必須對(duì)該語(yǔ)言有 USAGE 權(quán)限。
EXAMPLES 例子
這里是一個(gè)簡(jiǎn)單的例子,用于幫助你開始掌握這個(gè)命令。 更多信息和例子,參閱 ``User-Defined Functions''。
CREATE FUNCTION add(integer, integer) RETURNS integer AS 'select $1 + $2;' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT;
COMPATIBILITY 兼容性
在 SQL99 里的確定義了一個(gè)CREATE FUNCTION PostgreSQL 的和它類似但是不兼容。 這個(gè)屬性是不可移植的,可以使用的不同語(yǔ)言也是如此。
SEE ALSO 參見
ALTER FUNCTION [alter_function(7)], DROP FUNCTION [drop_function(7)], GRANT [grant(7)], LOAD [load(7)], REVOKE [revoke(7)], createlang(1)
#p#
NAME
CREATE FUNCTION - define a new function
SYNOPSIS
CREATE [ OR REPLACE ] FUNCTION name ( [ argtype [, ...] ] ) RETURNS rettype { LANGUAGE langname | IMMUTABLE | STABLE | VOLATILE | CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT | [EXTERNAL] SECURITY INVOKER | [EXTERNAL] SECURITY DEFINER | AS 'definition' | AS 'obj_file', 'link_symbol' } ... [ WITH ( attribute [, ...] ) ]
DESCRIPTION
CREATE FUNCTION defines a new function. CREATE OR REPLACE FUNCTION will either create a new function, or replace an existing definition.
If a schema name is included, then the function is created in the specified schema. Otherwise it is created in the current schema. The name of the new function must not match any existing function with the same argument types in the same schema. However, functions of different argument types may share a name (this is called overloading).
To update the definition of an existing function, use CREATE OR REPLACE FUNCTION. It is not possible to change the name or argument types of a function this way (if you tried, you'd just be creating a new, distinct function). Also, CREATE OR REPLACE FUNCTION will not let you change the return type of an existing function. To do that, you must drop and recreate the function.
If you drop and then recreate a function, the new function is not the same entity as the old; you will break existing rules, views, triggers, etc. that referred to the old function. Use CREATE OR REPLACE FUNCTION to change a function definition without breaking objects that refer to the function.
The user that creates the function becomes the owner of the function.
PARAMETERS
- name
- The name of a function to create.
- argtype
- The data type(s) of the function's arguments (optionally schema-qualified), if any. The argument types may be base, complex, or domain types, or copy the type of an existing column.
The type of a column is referenced by writing tablename.columnname%TYPE; using this can sometimes help make a function independent from changes to the definition of a table.
Depending on the implementation language it may also be allowed to specify ``pseudotypes'' such as cstring. Pseudotypes indicate that the actual argument type is either incompletely specified, or outside the set of ordinary SQL data types.
- rettype
- The return data type (optionally schema-qualified). The return type may be specified as a base, complex, or domain type, or may copy the type of an existing column. See the description under argtype above on how to reference the type of an existing column.
Depending on the implementation language it may also be allowed to specify ``pseudotypes'' such as cstring. The SETOF modifier indicates that the function will return a set of items, rather than a single item.
- langname
- The name of the language that the function is implemented in. May be SQL, C, internal, or the name of a user-defined procedural language. (See also createlang [createlang(1)].) For backward compatibility, the name may be enclosed by single quotes.
- IMMUTABLE
- STABLE
- VOLATILE
- These attributes inform the system whether it is safe to replace multiple evaluations of the function with a single evaluation, for run-time optimization. At most one choice should be specified. If none of these appear, VOLATILE is the default assumption.
IMMUTABLE indicates that the function always returns the same result when given the same argument values; that is, it does not do database lookups or otherwise use information not directly present in its argument list. If this option is given, any call of the function with all-constant arguments can be immediately replaced with the function value.
STABLE indicates that within a single table scan the function will consistently return the same result for the same argument values, but that its result could change across SQL statements. This is the appropriate selection for functions whose results depend on database lookups, parameter variables (such as the current time zone), etc. Also note that the current_timestamp family of functions qualify as stable, since their values do not change within a transaction.
VOLATILE indicates that the function value can change even within a single table scan, so no optimizations can be made. Relatively few database functions are volatile in this sense; some examples are random(), currval(), timeofday(). Note that any function that has side-effects must be classified volatile, even if its result is quite predictable, to prevent calls from being optimized away; an example is setval().
- CALLED ON NULL INPUT
- RETURNS NULL ON NULL INPUT
- STRICT
- CALLED ON NULL INPUT (the default) indicates that the function will be called normally when some of its arguments are null. It is then the function author's responsibility to check for null values if necessary and respond appropriately.
RETURNS NULL ON NULL INPUT or STRICT indicates that the function always returns null whenever any of its arguments are null. If this parameter is specified, the function is not executed when there are null arguments; instead a null result is assumed automatically.
- [EXTERNAL] SECURITY INVOKER
- [EXTERNAL] SECURITY DEFINER
- SECURITY INVOKER indicates that the function is to be executed with the privileges of the user that calls it. That is the default. SECURITY DEFINER specifies that the function is to be executed with the privileges of the user that created it.
The key word EXTERNAL is present for SQL conformance but is optional since, unlike in SQL, this feature does not only apply to external functions.
- definition
- A string defining the function; the meaning depends on the language. It may be an internal function name, the path to an object file, an SQL command, or text in a procedural language.
- obj_file, link_symbol
- This form of the AS clause is used for dynamically loadable C language functions when the function name in the C language source code is not the same as the name of the SQL function. The string obj_file is the name of the file containing the dynamically loadable object, and link_symbol is the function's link symbol, that is, the name of the function in the C language source code. If the link symbol is omitted, it is assumed to be the same as the name of the SQL function being defined.
- attribute
- The historical way to specify optional pieces of information about the function. The following attributes may appear here:
- isStrict
- Equivalent to STRICT or RETURNS NULL ON NULL INPUT
- isCachable
- isCachable is an obsolete equivalent of IMMUTABLE; it's still accepted for backwards-compatibility reasons.
Attribute names are not case-sensitive.
NOTES
Refer to the section called ``User-Defined Functions'' in the documentation for further information on writing functions.
The full SQL type syntax is allowed for input arguments and return value. However, some details of the type specification (e.g., the precision field for type numeric) are the responsibility of the underlying function implementation and are silently swallowed (i.e., not recognized or enforced) by the CREATE FUNCTION command.
PostgreSQL allows function overloading; that is, the same name can be used for several different functions so long as they have distinct argument types. However, the C names of all functions must be different, so you must give overloaded C functions different C names (for example, use the argument types as part of the C names).
When repeated CREATE FUNCTION calls refer to the same object file, the file is only loaded once. To unload and reload the file (perhaps during development), use the LOAD [load(7)] command.
Use DROP FUNCTION to remove user-defined functions.
Any single quotes or backslashes in the function definition must be escaped by doubling them.
To be able to define a function, the user must have the USAGE privilege on the language.
EXAMPLES
Here is a trivial example to help you get started. For more information and examples, see the section called ``User-Defined Functions'' in the documentation.
CREATE FUNCTION add(integer, integer) RETURNS integer AS 'select $1 + $2;' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT;
COMPATIBILITY
A CREATE FUNCTION command is defined in SQL99. The PostgreSQL version is similar but not fully compatible. The attributes are not portable, neither are the different available languages.
SEE ALSO
ALTER FUNCTION [alter_function(7)], DROP FUNCTION [drop_function(7)], GRANT [grant(7)], LOAD [load(7)], REVOKE [revoke(7)], createlang(1)