Skip to main content
New in v23.2: A stored procedure is a database object consisting of or statements that can be issued with a single statement. This allows complex logic to be executed repeatedly within the database, which can improve performance and mitigate security risks. Both stored procedures and are types of routines. However, they differ in the following ways:
  • Functions return a value, and procedures do not return a value.
  • Procedures must be invoked using a statement. Functions can be invoked in nearly any context, such as SELECT, FROM, and WHERE clauses, expressions, and expressions.
  • Functions have settings, and procedures do not.

Structure

A stored procedure consists of a name, optional parameters, language, and procedure body.
  • Each parameter can be a supported , , or the PL/pgSQL REFCURSOR type, when .
  • LANGUAGE specifies the language of the function body. CockroachDB supports the languages and .
  • The procedure body:
    • Can be enclosed in single or dollar ( $$ ) quotes. Dollar quotes are easier to use than single quotes, which require that you escape other single quotes that are within the procedure body.
    • Must conform to a if written in PL/pgSQL.
For details, see .

Examples

Setup

To follow along, run to start a temporary, in-memory cluster with the sample dataset preloaded:
For more examples of stored procedure creation, see .

Create a stored procedure using PL/pgSQL

The following stored procedure removes a specified number of earliest rides in vehicle_location_histories. It uses the syntax to iterate through the rows, to return notice and error messages, and REFCURSOR to define a cursor that fetches the next rows to be affected by the procedure.
Open a :
Call the stored procedure, specifying 5 rows to delete and a rides_left cursor name:
Use the cursor to fetch the 3 earliest remaining rows in vehicle_location_histories:
If the procedure is called again, these rows will be the first 3 to be deleted.

Example details

The example works as follows: defines a stored procedure called delete_earliest_histories with an INT and a REFCURSOR parameter.
LANGUAGE specifies as the language for the stored procedure.
DECLARE specifies the that are used in the procedure body.
BEGIN and END in the procedure body.
The following statement if vehicle_location_histories has fewer rows than the number specified with num_deletions. If the exception is raised within an open transaction, the transaction will abort.
The following loop deletes rows iteratively from vehicle_location_histories, stopping when the number of loops reaches the num_deletions value. The DELETE... RETURNING... INTO statement assigns column values from each deleted row into separate variables. For more information about assigning variables, see . Finally, the statement reports these values for each deleted row.
The OPEN statement for all remaining rows in vehicle_location_histories, sorted by timestamp. After calling the procedure in an open transaction, the cursor can be used to fetch rows from the table.

Alter a stored procedure

The following statement renames the to delete_histories:

Known limitations

  • Stored procedures cannot call other stored procedures or .
  • Stored procedures do not support OUT and INOUT argument modes.
  • (e.g., CREATE TABLE, CREATE INDEX ) cannot be used within a stored procedure body.
  • cannot be run within stored procedures.
Also refer to the .

See also