Skip to main content
A user-defined function (UDF) is a named function defined at the database level that can be called in queries and other contexts. CockroachDB supports invoking UDFs in SELECT, FROM, and WHERE clauses of .

Overview

The basic components of a user-defined function are a name, list of arguments, return type, volatility, language, and function body.
  • An argument has a mode and a type. CockroachDB supports the IN argument mode. The type can be a built-in type, , or implicit record type. CockroachDB does not support default values for arguments.
  • The return type can be a built-in , user-defined , , implicit record type, or VOID.
    • Preceding a type with SETOF indicates that a set, or multiple rows, may be returned. For an example, see .
    • VOID indicates that there is no return type and NULL will always be returned.
  • The indicates whether the function has side effects. VOLATILE and NOT LEAKPROOF are the default.
    • Annotate a function with side effects with VOLATILE. This also prevents the from pre-evaluating the function.
    • A STABLE or IMMUTABLE function does not mutate data.
    • LEAKPROOF indicates that a function has no side effects and that it communicates nothing that depends on its arguments besides the return value (i.e., it cannot throw an error that depends on the value of its arguments). You must precede LEAKPROOF with IMMUTABLE, and only IMMUTABLE can be set to LEAKPROOF. NOT LEAKPROOF is allowed with any other volatility.
    • Non-VOLATILE functions can be optimized through inlining. For more information, see Create an inlined UDF.
  • The language specifies the language of the function body. CockroachDB supports the language SQL.
  • The function body:
    • Can reference arguments by name or by their ordinal in the function definition with the syntax $1.
    • Can be enclosed in a single line with single quotes '' or multiple lines with $$.
    • Can reference tables.
    • Can reference only the SELECT statement.

Examples

Create a UDF

The following is a UDF that returns the sum of two integers:
Where:
  • name: add
  • arguments: a of type INT, b of type INT
  • return type: INT
  • volatility: IMMUTABLE LEAKPROOF
  • language: SQL
  • function body: 'SELECT a + b'
Alternatively, you could define this function as:
Or as:
For more examples of UDF creation, see .

View a UDF definition

To view the definition for the add() function:
If you do not specify a schema for the function add when you create it, the default schema is public:

Invoke a UDF

You invoke a UDF like a . To invoke the add() function:

Create an inlined UDF

When possible, the will improve a function’s performance by inlining the UDF within the query plan. The UDF must have the following attributes:
  • It is labeled as IMMUTABLE, STABLE, or LEAKPROOF (i.e., non-VOLATILE).
  • It has a single statement.
  • It is not a .
  • Its arguments are only variable or constant expressions.
  • It is not a .
The following example demonstrates how inlining improves a UDF’s performance.
  1. Create tables a and b:
  2. Insert a value (10) into 1000 rows in a and 1 row in b:
  3. Create a VOLATILE function foo_v() and a STABLE function foo_s():
    Each function returns a specified value from table b.
  4. View the query plan when foo_v() (the VOLATILE function) is used in a selection query to retrieve equal values from table a:
    The query takes 77ms to execute because the function is invoked for each row scanned in table a.
  5. View the query plan when using foo_s() (the STABLE function) instead:
    The query takes only 4ms to execute because the function is inlined and transformed to a with an equality comparison (a) = (b), which has much less overhead than invoking a function for each row scanned in table a.

Known limitations

Limitations on use of UDFs

User-defined functions are not currently supported in:
  • Expressions (column, index, constraint) in tables.
  • Views.
  • Other user-defined functions.

Limitations on expressions allowed within UDFs

The following are not currently allowed within the body of a UDF:
  • Mutation statements such as INSERT, UPDATE, DELETE, and UPSERT.
  • CTEs (common table expressions).
  • References to other user-defined functions.

See also