Skip to main content
The BEGIN initiates a , which either successfully executes all of the statements it contains or none at all.
When running under the default isolation level, your application should that can occur under .

Synopsis

legacy_begin syntax diagram

Required privileges

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

Aliases

In CockroachDB, the following are aliases for the BEGIN statement:
  • BEGIN TRANSACTION
  • START TRANSACTION

Parameters

ParameterDescription
PRIORITYIf you do not want the transaction to run with NORMAL priority, you can set it to LOW or HIGH. Transactions with higher priority are less likely to need to be retried. For more information, see . Default: NORMAL
READSet the transaction access mode to READ ONLY or READ WRITE. The current transaction access mode is also exposed as the transaction\_read\_only. Default: READ WRITE
AS OF SYSTEM TIMEExecute the transaction using the database contents “as of” a specified time in the past. The AS OF SYSTEM TIME clause can be used only when the transaction is read-only. If the transaction contains any writes, or if the READ WRITE mode is specified, an error will be returned. For more information, see .
NOT DEFERRABLE DEFERRABLEThis clause is supported for compatibility with PostgreSQL. NOT DEFERRABLE is a no-op and the default behavior for CockroachDB. DEFERRABLE returns an unimplemented error.
ISOLATION LEVELSet the transaction isolation level. Transactions use SERIALIZABLE isolation by default. They can be configured to run at isolation.

Examples

Begin a transaction

Use default settings

Without modifying the BEGIN statement, the transaction uses SERIALIZABLE isolation and NORMAL priority.
This example assumes you’re using .

Change isolation level

You can set the transaction isolation level to or . If not specified, transactions use the value of the current session’s variable.

Change priority

You can set a transaction’s priority to LOW or HIGH.
You can also set a transaction’s priority with . This example assumes you’re using .

Use the AS OF SYSTEM TIME option

You can execute the transaction using the database contents “as of” a specified time in the past.
You can also use the statement inside the transaction to achieve the same results. This syntax is easier to use from .

Begin a transaction with automatic retries

CockroachDB will all transactions that contain both BEGIN and COMMIT in the same batch. Batching is controlled by your driver or client’s behavior, but means that CockroachDB receives all of the statements as a single unit, instead of a number of requests. From the perspective of CockroachDB, a transaction sent as a batch looks like this:
However, in your application’s code, batched transactions are often just multiple statements sent at once. For example, in Go, this transaction would sent as a single batch (and automatically retried):
Issuing statements this way signals to CockroachDB that you do not need to change any of the statement’s values if the transaction doesn’t immediately succeed, so it can continually retry the transaction until it’s accepted.

See also