ROLLBACK

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 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

ROLLBACKTOSAVEPOINTcockroach_restart

Required privileges

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

Parameters

ParameterDescription
TO SAVEPOINT cockroach_restartIf 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:

  1. > SELECT * FROM accounts;
  1. +----------+---------+
  2. | name | balance |
  3. +----------+---------+
  4. | Marciela | 1000 |
  5. +----------+---------+
  1. > BEGIN;
  1. > UPDATE accounts SET balance = 2500 WHERE name = 'Marciela';
  1. > ROLLBACK;
  1. > SELECT * FROM accounts;
  1. +----------+---------+
  2. | name | balance |
  3. +----------+---------+
  4. | Marciela | 1000 |
  5. +----------+---------+

Retry a transaction

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

  1. > 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

Was this page helpful?
YesNo