Rollbacks to savepoints over DDL statements are only supported if you’re rolling back to a savepoint created at the beginning of the transaction.
Synopsis
Required privileges
No are required to create a savepoint. However, privileges are required for each statement within a transaction.Parameters
| Parameter | Description |
|---|---|
| name | The name of the savepoint. can use any name for the savepoint. default to using the name cockroach\_restart, but this can be customized using a session variable. For more information, see . |
Savepoints and row locks
CockroachDB supports exclusive row locks.- In PostgreSQL, row locks are released/cancelled upon .
- In CockroachDB, row locks are preserved upon .
Savepoints and high priority transactions
(for either regular savepoints or “restart savepoints” defined withcockroach_restart) causes a “feature not supported” error after a DDL statement in a , in order to avoid a transaction deadlock.
Examples
The examples below use the following table:Basic usage
To establish a savepoint inside a transaction:Due to the ,
SAVEPOINT foo and SAVEPOINT Foo define the same savepoint, whereas SAVEPOINT "Foo" defines another.(1,1) and (3,3) into the table, but not (2,2):
Savepoints for nested transactions
Transactions can be nested using named savepoints. and can both refer to a savepoint “higher” in the nesting hierarchy. When this occurs, all of the savepoints “under” the nesting are automatically released / rolled back too. Specifically:- When a previous savepoint is rolled back, the statements entered after that savepoint are also rolled back.
- When a previous savepoint is released, it commits; the statements entered after that savepoint are also committed.
Multi-level rollback with ROLLBACK TO SAVEPOINT
Savepoints can be arbitrarily nested, and rolled back to the outermost level so that every subsequent statement is rolled back.
For example, this transaction does not insert anything into the table. Both INSERTs are rolled back:
Multi-level commit with RELEASE SAVEPOINT
Changes committed by releasing a savepoint commit all of the statements entered after that savepoint.
For example, the following transaction inserts both (2,2) and (4,4) into the table when it releases the outermost savepoint:
Multi-level rollback and commit in the same transaction
Changes partially committed by a savepoint release can be rolled back by an outer savepoint. For example, the following transaction inserts only value(5, 5). The values (6,6) and (7,7) are rolled back.
Error recovery in nested transactions with ROLLBACK TO SAVEPOINT
If ROLLBACK TO SAVEPOINT is used after a database error, it can also cancel the error state of the transaction. Database errors move a transaction (or nested transaction) into an “Aborted” state. In this state, the transaction will not execute any further SQL statements.
You can use ROLLBACK TO SAVEPOINT to recover from a logical error in a nested transaction. Logical errors include:
- Unique index error (duplicate row)
- Failed foreign key constraint check (row does not exist in referenced table)
- Mistakes in queries (reference a column that does not exist)
SHOW TRANSACTION STATUS statement as shown below.
For example:
Savepoint name visibility
The name of a savepoint that was rolled back over is no longer visible afterward. For example, in the transaction below, the name “bar” is not visible after it was rolled back over:Savepoints and prepared statements
Prepared statements (PREPARE / EXECUTE) are not transactional. Therefore, prepared statements are not invalidated upon savepoint rollback. As a result, the prepared statement was saved and executed inside the transaction, despite the rollback to the prior savepoint:
Savepoints for client-side transaction retries
A savepoint defined with the namecockroach_restart is a “retry savepoint” and is used to implement . For more information, see .
The example below shows basic usage of a retry savepoint.
SAVEPOINT for client-side transaction retries must also include functions to execute retries with .
Note that you can customize the retry savepoint name to something other than cockroach_restart with a session variable if you need to.
Customizing the retry savepoint name
Set theforce_savepoint_restart to true to enable using a custom name for the .
Once this variable is set, the statement will accept any name for the retry savepoint, not just cockroach_restart. In addition, it causes every savepoint name to be equivalent to cockroach_restart, therefore disallowing the use of .
This feature exists to support applications that want to use the , but cannot customize the name of savepoints to be cockroach_restart. For example, this may be necessary because you are using an ORM that requires its own names for savepoints.
Showing savepoint status
Use the statement to see how many savepoints are active in the current transaction:is_initial_savepoint column will be true if the savepoint is the outermost savepoint in the transaction.

