Overview
Transactions in CockroachDB provide:- Atomicity: All statements succeed together or fail together
- Consistency: Database constraints are maintained
- Isolation: Concurrent transactions don’t interfere with each other
- Durability: Committed data is permanently stored
SERIALIZABLE isolation, the highest isolation level, ensuring that concurrent transactions appear to execute in a serial order.
Transaction Syntax
Transactions are initiated withBEGIN and completed with COMMIT or ROLLBACK.
- Basic Transaction
- With Savepoint
- Rollback
Transaction Statements
Initiate a transaction with optional priority, access mode, timestamp, or isolation level.
Commit the current transaction, making all changes permanent.
Abort the transaction and discard all changes.
Create a savepoint for nested transactions.
Commit a nested transaction.
Roll back to a savepoint, discarding changes made after it.
Set transaction properties after BEGIN.
Isolation Levels
CockroachDB supports two isolation levels:SERIALIZABLE (Default)
SERIALIZABLE isolation provides the strongest consistency guarantees. Transactions appear to execute in a serial order, preventing all anomalies.
Under
SERIALIZABLE isolation, transactions may encounter retry errors during contention. Implement retry logic in your application.READ COMMITTED
READ COMMITTED isolation provides weaker consistency but avoids transaction retry errors. Each statement sees a snapshot of data committed before the statement began.
Transaction Priorities
You can set transaction priority to influence retry behavior during contention.Default priority for most transactions.
Use for background or less critical transactions.
Use for critical transactions that should be prioritized.
Transaction Retries
Transactions may need to retry due to contention with concurrent transactions.Automatic Retries
CockroachDB automatically retries:- Single statements (implicit transactions)
- Batched statements where results fit in the 16 KiB buffer
Client-Side Retries
ForSERIALIZABLE transactions that span multiple statements, implement retry logic in your application:
Python Example
READ COMMITTED transactions do not require client-side retry handling.Error Handling
Handle these error types in your application:| Error Type | Code | Description |
|---|---|---|
| Transaction Retry | 40001 | Transaction failed due to contention. Retry the entire transaction. |
| Ambiguous Error | 40003 | Transaction state is unknown. Verify whether it committed. |
| SQL Error | Various | Statement failed due to constraint violation, syntax error, etc. |
- Retry Error
- Ambiguous Error
- SQL Error
BEGIN.Nested Transactions
Use savepoints to create nested transactions that can be rolled back independently.| Transaction Statement | Effect | Savepoint Statement | Effect |
|---|---|---|---|
COMMIT | Commit entire transaction | RELEASE SAVEPOINT | Commit nested transaction |
ROLLBACK | Discard entire transaction | ROLLBACK TO SAVEPOINT | Discard nested transaction |
Batched Statements
Send multiple statements as a single batch for automatic retry:Go Example
Transaction Limits
You can limit the number of rows read or written in a transaction to prevent large transactions from impacting cluster performance.Best Practices
- Keep transactions short: Long transactions increase contention and retry likelihood
- Use appropriate isolation: Choose
READ COMMITTEDif you don’t need full serializability - Implement retry logic: Always handle retry errors in
SERIALIZABLEtransactions - Use savepoints wisely: Nested transactions enable fine-grained rollback
- Batch when possible: Send related statements as a batch for automatic retries
- Monitor contention: Use the DB Console to identify and resolve transaction contention
AS OF SYSTEM TIME
Read historical data using theAS OF SYSTEM TIME clause:
- Consistent backups
- Time-travel queries
- Reducing contention on read-only transactions