CREATE FUNCTION 中文man頁面
NAME
CREATE 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 定義一個新的函數。 CREATE OR REPLACE FUNCTION 將要么創建一個新函數, 要么替換現有的定義。
如果包含了一個模式名,那么函數就在指定的模式中創建。否則它會在當前模式中創建。 新函數的名字不能和同一個模式中的任何帶有同樣參數類型的函數同名。 不過,參數類型不同的函數可以同名(這叫做重載)。
要更新一個現有函數的定義,使用 CREATE OR REPLACE FUNCTION。 我們不能用這個方法修改一個函數的名字或者參數類型(如果你這么干,那么你就會創建一個新的,不同的函數)。 同樣,CREATE OR REPLACE FUNCTION 也不會允許你修改一個現有函數的返回類型。 要做這些事情,你必須刪除并重新創建函數。
如果你刪除然后重建一個函數,新函數和舊的將是不同的實體;你會破壞現有規則,視圖,觸發器等等。 使用 CREATE OR REPLACE FUNCTION 可以在不破壞引用該函數的對象的前提下修改函數定義。
創建這個函數的用戶成為函數的所有者。
PARAMETERS 參數
- name
要創建的函數名字。- argtype
該函數的數據類型(可以有模式修飾)。如果有的話,參數類型可以是基本類型,也可以是復合類型,域類型,或者和一個現有字段相同的類型。
一個字段的類型是用 tablename.columnname%TYPE 表示的;使用這個東西可以幫助函數獨立于表定義的修改。
根據實現語言的不同,我們還可以在這上面聲明 "偽類型", 比如 cstring。偽類型表示實際的參數類型要么是沒有完整地聲明,要么是在普通的 SQL 數據類型之外。- rettype
返回數據類型。輸出類型可以聲明為一個基本類型,復合類型,域類型, 或者從現有字段拷貝。參閱上面 argtype 的描述獲取如何引用一個現有類型的信息。
根據實現語言的不同,我們還可以在這上面聲明 "偽類型", 比如 cstring。 SETOF 修飾詞表示該函數將返回一套條目, 而不是一條條目。- langname
用以實現函數的語言的名字。 可以是 SQL,C, internal,或者是用戶定義的過程語言名字。 (又見 createlang。 ) 為了保持向下兼容,該名字可以用單引號包圍。- IMMUTABLE
- STABLE
- VOLATILE
這些屬性告訴系統把對該函數的多次調用替換成一次是否安全。 主要用于運行時優化。 至少應該聲明一個選擇。如果任何一個都沒有出現,那么 VOLATILE 是缺省假設。IMMUTABLE 表示該函數在給出同樣的參數值時總是返回相同的結果; 也就是說,它不做數據庫查找或者是使用那些并沒有直接出現在其參數列表里面的信息。 如果給出這個選項,那么任何帶著全部是常量參數對該函數的調用都將立即替換為該函數的值。
STABLE 表示在一次表掃描里,對相同參數值, 該函數將穩定返回相同的值,但是它的結果可能在不同 SQL 語句之間變化。 這個選項對那些結果倚賴數據庫查找,參數變量(比如當前時區),等等的函數是很合適的。 還要注意 current_timestamp 族函數是 stable (穩定)的,因為它們的值在一次事務中不會變化。
VOLATILE 表示該函數值甚至可以在一次表掃描內改變, 因此不會做任何優化。很少數據庫函數在這個概念上是易變的; 一些例子是 random(),currval(), timeofday()。請注意任何有副作用的函數都必需列為易變類, 即使其結果相當有規律也應該這樣,這樣才能避免它被優化;一個例子就是 setval()。
- CALLED ON NULL INPUT
- RETURNS NULL ON NULL INPUT
- STRICT
- CALLED ON NULL INPUT (缺省)表明該函數在自己的某些參數是空值的時候還是可以按照正常的方式調用。 剩下的事情是函數的作者必須負責檢查空值以及相應地做出反應。
RETURNS NULL ON NULL INPUT 或 STRICT 表明如果它的任何參數是 NULL,此函數總是返回 NULL。 如果聲明了這個參數,則如果存在 NULL 參數時不會執行該函數; 而只是自動假設一個 NULL 結果。
- [EXTERNAL] SECURITY INVOKER
- [EXTERNAL] SECURITY DEFINER
- SECURITY INVOKER 表明該函數將帶著調用它的用戶的權限執行。 這是缺省。SECURITY DEFINER 聲明該函數將以創建它的用戶的權限執行。
關鍵字 EXTERNAL 的目的是和 SQL 兼容, 但是我們和 SQL 不同的是,這個特性不僅僅適用于外部的函數, 所以它是可選的。 - definition
一個定義函數的字串;含義取決于語言。它可以是一個內部函數名字, 一個指向某個目標文件的路徑,一個 SQL 查詢,或者一個用過程語言寫的文本。- obj_file, link_symbol
這個形式的 AS 子句用于在函數的 C 源文件里的函數名字和 SQL 函數的名字不同的時候可動態裝載 C 語言函數。 字串 obj_file 是包含可動態裝載的對象的文件名,而 link_symbol 是函數的鏈接符號,也就是該函數在 C 源文件里的名字。 如果省略了鏈接符號,那么就假設它和被定義的 SQL 函數同名。- attribute
歷史遺留的函數可選信息。下面的屬性可以在此出現:- isStrict
等效于 STRICT 或者 RETURNS NULL ON NULL INPUT- isCachable
- isCachable 是 IMMUTABLE 的過時的等效物;不過出于向下兼容,我們仍然接受它。
屬性名是大小寫無關的。
NOTES 注意
請參閱 ``User-Defined Functions'' 獲取更多關于書寫函數的信息。
我們允許你將完整的 SQL 類型語法用于輸入參數和返回值。 不過,有些類型聲明的細節(比如,numeric 類型的精度域)是由下層函數實現負責的, 并且會被 CREATE FUNCTION 命令悄悄地吞掉。 (也就是說,不再被識別或強制)。
PostgreSQL 允許函數重載; 也就是說,同一個函數名可以用于幾個不同的函數, 只要它們的參數可以區分它們。不過,所有函數的 C 名字必須不同, 也就是說你必須給予重載的 C 函數不同的 C 名字(比如,使用參數類型作為 C 名字的一部分)。
如果重復調用 CREATE FUNCTION,并且都指向同一個目標文件, 那么該文件只裝載一次。要卸載和恢復裝載該文件(可能是在開發過程中),你可以使用 LOAD [load(7)] 命令。
使用 DROP FUNCTION 刪除一個用戶定義函數。
函數定義里面的任何單引號或者反斜杠都必須用寫雙份的方式逃逸。
要能定義函數,用戶必須對該語言有 USAGE 權限。
EXAMPLES 例子
這里是一個簡單的例子,用于幫助你開始掌握這個命令。 更多信息和例子,參閱 ``User-Defined Functions''。
CREATE FUNCTION add(integer, integer) RETURNS integer AS 'select $1 + $2;' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT;
COMPATIBILITY 兼容性
在 SQL99 里的確定義了一個CREATE FUNCTION PostgreSQL 的和它類似但是不兼容。 這個屬性是不可移植的,可以使用的不同語言也是如此。
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)