CREATE FUNCTION v17
Name
CREATE FUNCTION
— Define a new function.
Synopsis
Description
CREATE FUNCTION
defines a new function. CREATE OR REPLACE FUNCTION
either creates a new function or replaces an existing definition.
If you include a schema name, then the function is created in the specified schema. Otherwise it's created in the current schema. The name of the new function can't match any existing function with the same input argument types in the same schema. However, functions of different input argument types can share a name. This is called overloading.
Note
Overloading functions is an EDB Postgres Advanced Server feature. Overloading stored, standalone functions isn't compatible with Oracle databases.
To update the definition of an existing function, use CREATE OR REPLACE FUNCTION
. You can't change the name or argument types of a function this way. (That syntax instead creates a new function.) CREATE OR REPLACE FUNCTION
also doesn't let you change the return type of an existing function. To do that, you must drop the function and create it again. Also, you can change the types of any OUT
parameters only by dropping the function first.
The user that creates the function becomes the owner of the function.
Parameters
name
The identifier of the function.
parameters
A list of formal parameters.
data_type
The data type of the value returned by the function’s RETURN
statement.
declarations
Variable, cursor, type, or subprogram declarations. To include subprogram declarations, declare them after all other variable, cursor, and type declarations.
statements
SPL program statements. The BEGIN - END
block can contain an EXCEPTION
section.
IMMUTABLE
STABLE
VOLATILE
These attributes inform the query optimizer about the behavior of the function. You can specify only one of these attributes.
Use
IMMUTABLE
to indicate that the function can't modify the database and must always reach the same result when given the same argument values. This attribute doesn't perform database lookups and uses only information present in its argument list. If you include this clause, you can immediately replace any call of the function with all-constant arguments with the function value.Use
STABLE
to indicate that the function can't modify the database and that, in a single table scan, it consistently returns the same result for the same argument values. However, its result might change across SQL statements. Use this selection for functions that depend on database lookups, parameter variables (such as the current time zone), and so on.Use
VOLATILE
(the default) to indicate that the function value can change even in a single table scan, so no optimizations can be made. Classify any function that has side effects as volatile, even if its result is predictable. This setting prevent calls from being optimized away.
DETERMINISTIC
DETERMINISTIC
is a synonym for IMMUTABLE
. A DETERMINISTIC
function can't modify the database and always reaches the same result when given the same argument values. It doesn't perform database lookups and uses only information directly present in its argument list. If you include this clause, you can immediately replace any call of the function with all-constant arguments with the function value.
[ NOT ] LEAKPROOF
A LEAKPROOF
function has no side effects and reveals no information about the values used to call the function.
CALLED ON NULL INPUT
RETURNS NULL ON NULL INPUT
STRICT
Use
CALLED ON NULL INPUT
(the default) to call the procedure normally when some of its arguments areNULL
. Check forNULL
values, if necessary, and respond appropriately.Use
RETURNS NULL ON NULL INPUT
orSTRICT
to indicate that the procedure always returnsNULL
when any of its arguments areNULL
. If you use these clauses, the procedure doesn't execute when there areNULL
arguments. Instead, aNULL
result is assumed.
[ EXTERNAL ] SECURITY DEFINER
SECURITY DEFINER
(the default) specifies for the function to execute with the privileges of the user that created it. The optional keyword EXTERNAL
is allowed for SQL conformance.
[ EXTERNAL ] SECURITY INVOKER
Use the SECURITY INVOKER
clause to execute the function with the privileges of the user that calls it. The optional keyword EXTERNAL
is allowed for SQL conformance.
AUTHID DEFINER
AUTHID CURRENT_USER
The
AUTHID DEFINER
clause is a synonym for[EXTERNAL] SECURITY DEFINER
. If you omit theAUTHID
clause or specifyAUTHID DEFINER
, the rights of the function owner determine access privileges to database objects.The
AUTHID CURRENT_USER
clause is a synonym for[EXTERNAL] SECURITY INVOKER
. If you specifyAUTHID CURRENT_USER
, the rights of the current user executing the function determine access privileges.
PARALLEL { UNSAFE | RESTRICTED | SAFE }
The PARALLEL
clause enables the use of parallel sequential scans (parallel mode). A parallel sequential scan uses multiple workers to scan a relation in parallel during a query, in contrast to a serial sequential scan.
When set to
UNSAFE
(the default), the function can't execute in parallel mode. The presence of such a function in a SQL statement forces a serial execution plan.When set to
RESTRICTED
, the function can execute in parallel mode, but the execution is restricted to the parallel group leader. If the qualification for any particular relation has anything that is parallel restricted, that relation isn't chosen for parallelism.When set to
SAFE
, the function can execute in parallel mode with no restriction.
COST execution_cost
execution_cost
is a positive number giving the estimated execution cost for the function, in units of cpu_operator_cost
. If the function returns a set, this is the cost per returned row. Larger values cause the planner to try to avoid evaluating the function more often than needed.
ROWS result_rows
result_rows
is a positive number giving the estimated number of rows for the planner to expect the function to return. This option is allowed only when the function is declared to return a set. The default assumption is 1000
rows.
SET configuration_parameter { TO value | = value | FROM CURRENT }
The SET
clause causes the specified configuration parameter to be set to the specified value when the function is entered and then restored to its prior value when the function exits. SET FROM CURRENT
saves the session's current value of the parameter as the value to apply when the function is entered.
If a SET
clause is attached to a function, then the effects of a SET LOCAL
command executed inside the function for the same variable are restricted to the function. The configuration parameter's prior value is restored at function exit. An ordinary SET
command (without LOCAL
) overrides the SET
clause, similar to a previous SET LOCAL
command. The effects of such a command persist after procedure exit unless the current transaction is rolled back.
PRAGMA AUTONOMOUS_TRANSACTION
PRAGMA AUTONOMOUS_TRANSACTION
is the directive that sets the function as an autonomous transaction.
Note
The STRICT
, LEAKPROOF
, PARALLEL
, COST
, ROWS
and SET
keywords provide extended functionality for EDB Postgres Advanced Server and aren't supported by Oracle.
Notes
EDB Postgres Advanced Server allows function overloading. That is, you can use the same name for several different functions. However, they must have discrete input (IN
, IN OUT
) argument data types.
Examples
The function emp_comp
takes two numbers as input and returns a computed value. The SELECT
command shows the use of the function.
The function sal_range
returns a count of the number of employees whose salary falls in the specified range. The following anonymous block calls the function a number of times using the argument's default values for the first two calls:
This example uses the AUTHID CURRENT_USER
clause and STRICT
keyword in a function declaration:
Include the STRICT
keyword to return NULL
if any input parameter passed is NULL
. If a NULL
value is passed, the function doesn't execute.
The dept_salaries
function executes with the privileges of the role that's calling the function. If the current user doesn't have privileges to perform the SELECT
statement querying the emp
table (to display employee salaries), the function reports an error. To use the privileges associated with the role that defined the function, replace the AUTHID CURRENT_USER
clause with the AUTHID DEFINER
clause.
Other Pragmas (declared in a package specification)
PRAGMA RESTRICT_REFERENCES
EDB Postgres Advanced Server accepts but ignores syntax referencing PRAGMA RESTRICT_REFERENCES
.