CREATE FUNCTION creates a function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | CREATE [ OR REPLACE ] FUNCTION function_name ( [ { argname [ argmode ] argtype [ { DEFAULT | := | = } expression ]} [, ...] ] ) [ RETURNS rettype [ DETERMINISTIC ] | RETURNS TABLE ( { column_name column_type } [, ...] )] LANGUAGE lang_name [ {IMMUTABLE | STABLE | VOLATILE } | {SHIPPABLE | NOT SHIPPABLE} | WINDOW | [ NOT ] LEAKPROOF | {CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT } | {[ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER | AUTHID DEFINER | AUTHID CURRENT_USER} | {fenced | not fenced} | {PACKAGE} | COST execution_cost | ROWS result_rows | SET configuration_parameter { {TO | =} value | FROM CURRENT }} ][...] { AS 'definition' | AS 'obj_file', 'link_symbol' } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | CREATE [ OR REPLACE ] FUNCTION function_name ( [ { argname [ argmode ] argtype [ { DEFAULT | := | = } expression ] } [, ...] ] ) RETURN rettype [ DETERMINISTIC ] [ {IMMUTABLE | STABLE | VOLATILE } | {SHIPPABLE | NOT SHIPPABLE} | {PACKAGE} | {FENCED | NOT FENCED} | [ NOT ] LEAKPROOF | {CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT } | {[ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER | AUTHID DEFINER | AUTHID CURRENT_USER } | COST execution_cost | ROWS result_rows | SET configuration_parameter { {TO | =} value | FROM CURRENT ][...] { IS | AS } plsql_body / |
Indicates the name of the function to create (optionally schema-qualified).
Value range: a string. It must comply with the naming convention.
Indicates the name of a function parameter.
Value range: a string. It must comply with the naming convention.
Indicates the mode of a parameter.
Value range: IN, OUT, IN OUT, INOUT, and VARIADIC. The default value is IN. Only the parameter of OUT mode can be followed by VARIADIC. The parameters of OUT and INOUT cannot be used in function definition of RETURNS TABLE.
VARIADIC specifies parameters of array types.
Indicates the data types of the function's parameters.
Indicates the default expression of a parameter.
Indicates the return data type.
When there is OUT or IN OUT parameter, the RETURNS clause can be omitted. If the clause exists, it must be the same as the result type indicated by the output parameter. If there are multiple output parameters, the value is RECORD. Otherwise, the value is the same as the type of a single output parameter.
The SETOF modifier indicates that the function will return a set of items, rather than a single item.
The adaptation oracle SQL syntax. You are not advised to use it.
Specifies the column name.
Specifies the column type.
Specifies a string constant defining the function; the meaning depends on the language. It can be an internal function name, a path pointing to a target file, a SQL query, or text in a procedural language.
Indicates the name of the language that is used to implement the function. It can be SQL, internal, or the name of user-defined process language. To ensure downward compatibility, the name can use single quotation marks. Contents in single quotation marks must be capitalized.
Indicates that the function is a window function. The WINDOW attribute cannot be changed when the function definition is replaced.
For a user-defined window function, the value of LANGUAGE can only be internal, and the referenced internal function must be a window function.
Indicates that the function always returns the same result if the parameter values are the same.
If the input argument of the function is a constant, the function value is calculated at the optimizer stage. The advantage is that the expression value can be obtained as early as possible, so the cost estimation is more accurate and the execution plan generated is better.
A user-defined IMMUTABLE function is automatically pushed down to DNs for execution, which may cause potential risks. If a function is defined as IMMUTABLE but the function execution process is in fact not IMMUTABLE, serious problems such as result errors may occur. Therefore, exercise caution when defining the IMMUTABLE attribute for a function.
Examples:
To prevent possible problems, you can set behavior_compat_options to check_function_conflicts in the database to check definition conflicts. This method can identify the 1 and 2 scenarios described above.
Indicates that the function cannot modify the database, and that within a single table scan it will consistently return the same result for the same parameter values, but that its result varies by SQL statements.
Indicates that the function value can change even within a single table scan, so no optimizations can be made.
NOT SHIPPABLE
Indicates whether the function can be pushed down to DNs for execution.
Exercise caution when defining the SHIPPABLE attribute for a function. SHIPPABLE means that the entire function will be pushed down to DNs for execution. If the attribute is incorrectly set, serious problems such as result errors may occur.
Similar to the IMMUTABLE attribute, the SHIPPABLE attribute has use restrictions. The function cannot contain factors that do not allow the function to be pushed down for execution. If a function is pushed down to a single DN for execution, the function's calculation logic will depend only on the data set of the DN.
Examples:
Indicates that the function has no side effects. LEAKPROOF can be set only by the system administrator.
Declares that some parameters of the function can be invoked in normal mode if the parameter values are NULL. This parameter can be omitted.
STRICT
Indicates that the function always returns NULL whenever any of its parameters are NULL. If this parameter is specified, the function is not executed when there are NULL parameters; instead a NULL result is returned automatically.
The usage of RETURNS NULL ON NULL INPUT is the same as that of STRICT.
The keyword EXTERNAL is allowed for SQL conformance, but it is optional since, unlike in SQL, this feature applies to all functions not only external ones.
AUTHID CURRENT_USER
Indicates that the function is to be executed with the permissions of the user that calls it. This parameter can be omitted.
SECURITY INVOKER and AUTHID CURRENT_USER have the same functions.
AUTHID DEFINER
Specifies that the function is to be executed with the permissions of the user that created it.
The usage of AUTHID DEFINER is the same as that of SECURITY DEFINER.
NOT FENCED
(Effective only for C functions) Specifies whether functions are executed in fenced mode. In NOT FENCED mode, a function is executed in a CN or DN process. In FENCED mode, a function is executed in a new fork process, which does not affect CN or DN processes.
Application scenarios:
A positive number giving the estimated execution cost for the function.
The unit of execution_cost is cpu_operator_cost.
Value range: A positive number.
Estimates the number of rows returned by the function. This is only allowed when the function is declared to return a set.
Value range: A positive number. The default is 1000 rows.
Sets a specified database session parameter to a specified value. If the value is DEFAULT or RESET, the default setting is used in the new session. OFF closes the setting.
Value range: a string
Specifies the default value.
Uses the value of configuration_parameter of the current session.
(Used for C functions) Specifies the absolute path of the dynamic library using obj_file and the link symbol (function name in C programming language) of the function using link_symbol.
Indicates the PL/SQL stored procedure body.
When the function is creating users, the log will record unencrypted passwords. You are not advised to do it.
Define the function as SQL query.
1 2 3 4 5 | CREATE FUNCTION func_add_sql(integer, integer) RETURNS integer AS 'select $1 + $2;' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT; |
Add an integer by parameter name using PL/pgSQL.
1 2 3 4 5 | CREATE OR REPLACE FUNCTION func_increment_plsql(i integer) RETURNS integer AS $$ BEGIN RETURN i + 1; END; $$ LANGUAGE plpgsql; |
Return the RECORD type.
1 2 3 4 5 6 7 8 9 | CREATE OR REPLACE FUNCTION compute(i int, out result_1 bigint, out result_2 bigint) returns SETOF RECORD as $$ begin result_1 = i + 1; result_2 = i * 10; return next; end; $$language plpgsql; |
Get a record containing multiple output parameters.
1 2 3 4 | CREATE FUNCTION func_dup_sql(in int, out f1 int, out f2 text) AS $$ SELECT $1, CAST($1 AS text) || ' is text' $$ LANGUAGE SQL; SELECT * FROM func_dup_sql(42); |
Calculate the sum of two integers and get the result. If the input is null, null will be returned.
1 2 3 4 5 6 | CREATE FUNCTION func_add_sql2(num1 integer, num2 integer) RETURN integer AS BEGIN RETURN num1 + num2; END; / |
Create an overloaded function with the PACKAGE attribute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | CREATE OR REPLACE FUNCTION package_func_overload(col int, col2 int) return integer package as declare col_type text; begin col := 122; dbms_output.put_line('two int parameters ' || col2); return 0; end; / CREATE OR REPLACE FUNCTION package_func_overload(col int, col2 smallint) return integer package as declare col_type text; begin col := 122; dbms_output.put_line('two smallint parameters ' || col2); return 0; end; / |