Warning:
Cockroach Labs will stop providing Assistance Support for this version on November 1, 2020. Prior to that date, upgrade to a more recent version to continue receiving support. For more details, see the Release Support Policy.

The ROLLBACK statement aborts the current transaction, discarding all updates made by statements included in the transaction.

When using advanced client-side transaction retries, use ROLLBACK TO SAVEPOINT to handle a transaction that needs to be retried (identified via the 40001 error code or restart transaction string in the error message), and then re-execute the statements you want the transaction to contain.

Synopsis

ROLLBACK TO SAVEPOINT cockroach_restart

Required privileges

No privileges are required to rollback a transaction. However, privileges are required for each statement within a transaction.

Parameters

Parameter Description
TO SAVEPOINT cockroach_restart If using advanced client-side transaction retries, retry the transaction. You should execute this statement when a transaction returns a 40001 / retry transaction error.

Example

Rollback a transaction

Typically, an application conditionally executes rollbacks, but we can see their behavior by using ROLLBACK instead of COMMIT directly through SQL:

copy
icon/buttons/copy
> SELECT * FROM accounts;
+----------+---------+
|   name   | balance |
+----------+---------+
| Marciela |    1000 |
+----------+---------+
copy
icon/buttons/copy
> BEGIN;
copy
icon/buttons/copy
> UPDATE accounts SET balance = 2500 WHERE name = 'Marciela';
copy
icon/buttons/copy
> ROLLBACK;
copy
icon/buttons/copy
> SELECT * FROM accounts;
+----------+---------+
|   name   | balance |
+----------+---------+
| Marciela |    1000 |
+----------+---------+

Retry a transaction

To use advanced client-side transaction retries, an application must execute ROLLBACK TO SAVEPOINT after detecting a 40001 / retry transaction error:

copy
icon/buttons/copy
> ROLLBACK TO SAVEPOINT cockroach_restart;

For examples of retrying transactions in an application, check out the transaction code samples in our Build an App with CockroachDB tutorials.

See also



Yes No