ROLLBACK

On this page Carat arrow pointing down

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

When using client-side transaction retries, use ROLLBACK TO SAVEPOINT cockroach_restart to handle a transaction that needs to be retried (identified via the 40001 error code or retry 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 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:

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

Retry a transaction

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

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
On this page

Yes No