Skip to main content
The CREATE FUNCTION creates a . The CREATE FUNCTION statement performs a schema change. For more information about how online schema changes work in CockroachDB, see .

Required privileges

  • To create a function, a user must have on the schema of the function. The user must also have privileges on all the objects referenced in the function body.
  • To define a function with a , a user must have USAGE privilege on the user-defined type.
  • To resolve a function, a user must have at least the USAGE privilege on the schema of the function.
  • To call a function, a user must have EXECUTE privilege on the function. By default, the user must also have privileges on all the objects referenced in the function body. However, a SECURITY DEFINER function executes with the privileges of the user that owns the function, not the user that calls it. A SECURITY INVOKER function executes with the privileges of the user that calls the function, thus matching the default behavior.
If you grant EXECUTE privilege as a default privilege at the database level, newly created functions inherit that privilege from the database.

Synopsis

create_func syntax diagram

Parameters

ParameterDescription
routine\_create\_nameThe name of the function.
routine\_paramA comma-separated list of function parameters, specifying the mode, name, and type.
routine\_return\_typeThe type returned by the function: any built-in , user-defined or type, RECORD, TABLE, PL/pgSQL type, , or VOID.
routine\_body\_strThe body of the function. For allowed contents, refer to .

Example of a simple function

The following statement creates a function to compute the square of integers:
The following statement invokes the sq function:

Examples of functions that reference tables

Setup

To follow along, run to start a temporary, in-memory cluster with the sample dataset preloaded:

Create a function that references a table

The following statement defines a function that returns the total number of MovR application users.

Create a function that modifies a table

The following statement defines a function that updates the rules value for a specified row in promo_codes.
Given the promo_codes row:

Create a function that uses a WHERE clause

The following statement defines a function that returns the total revenue for rides taken in European cities.

Create a function that returns a set of results

The following statement defines a function that returns information for all vehicles not in use. The SETOF clause specifies that the function should return each row as the query executes to completion.
RETURNS TABLE also returns a set of results, each formatted as a RECORD type.

Create a function that returns a RECORD type

The following function returns the information for the user that most recently completed a ride. The information is returned as a record, which takes the structure of the row that is retrieved by the selection query. In the function subquery, the latest end_time timestamp is used to determine the most recently completed ride:

Create a function that returns a table

The following function returns information for the last x users that recently completed a ride. The information is returned as a table, which is equivalent to a set of RECORD values. The rows are sorted in order of most recent ride. The RETURNS TABLE clause specifies the column names to output: id, name, city, and end_time. A reads the most recent rides from the rides table.
OUT and INOUT parameters cannot be used with RETURNS TABLE.

Create a function that uses OUT and INOUT parameters

The following statement uses a combination of OUT and INOUT parameters to modify a provided value and output the result. An OUT parameter returns a value, while an INOUT parameter passes an input value and returns a value.
The CREATE FUNCTION statement does not need a RETURN statement because this is added implicitly for a function with OUT parameters:

Create a function that invokes a function

The following statement defines a function that invokes the double_triple example function.

Create a function that uses a loop

The following user-defined function returns the nth integer in the Fibonacci sequence. It uses the syntax to iterate through a simple calculation, and to return an error message if the specified n is negative.

Create a trigger function

A trigger function is a . A trigger function must return type TRIGGER and is written in .
The preceding example modifies a given name value and returns the NEW because it is meant to be executed by a BEFORE trigger. For details, refer to .

Create a SECURITY DEFINER function

The following example defines a function using the SECURITY DEFINER clause. This causes the function to execute with the privileges of the function owner.
  1. Create two roles:
  2. Grant a on the user_promo_codes table to the owner role.
  3. Set your role to owner.
  4. Create a simple SECURITY DEFINER function that reads the contents of user_promo_codes.
  5. Grant the on the get_codes function to the invoker role.
This step is not necessary if the function is defined on the public schema, for which roles automatically have the EXECUTE privilege.
  1. Set your role to invoker.
  2. invoker does not have the privileges to read the user_promo_codes table directly:
  3. As invoker, call the get_codes function to read user_promo_codes, since SECURITY DEFINER is executed with the privileges of the owner role (i.e., SELECT on user_promo_codes).

See also