> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# BEGIN

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

The `BEGIN` <InternalLink path="sql-statements">statement</InternalLink> initiates a <InternalLink path="transactions">transaction</InternalLink>, which either successfully executes all of the statements it contains or none at all.

<Note>
  When running under the default <InternalLink path="demo-serializable">`SERIALIZABLE`</InternalLink> isolation level, your application should <InternalLink path="query-behavior-troubleshooting#transaction-retry-errors">use a retry loop to handle transaction retry errors</InternalLink> that can occur under [contention](performance-best-practices-overview.html#transaction-contention).
</Note>

## Synopsis

<img src="https://mintcdn.com/cockroachlabs/8savutSe7Roc6oQ0/images/sql-diagrams/v26.2/legacy_begin.svg?fit=max&auto=format&n=8savutSe7Roc6oQ0&q=85&s=f0b5fa934dc97bdef4270775751dbf20" alt="legacy_begin syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="655" height="777" data-path="images/sql-diagrams/v26.2/legacy_begin.svg" />

## Required privileges

No <InternalLink path="security-reference/authorization#managing-privileges">privileges</InternalLink> 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

| Parameter                          | Description                                                                                                                                                                                                                                                                                                                                                                                                           |
| ---------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `PRIORITY`                         | If you do not want the transaction to run with `NORMAL` priority, you can set it to `LOW` or `HIGH`.<br /><br />Transactions with higher priority are less likely to need to be retried.<br /><br />For more information, see <InternalLink path="transactions#transaction-priorities">Transactions: Priorities</InternalLink>.<br /><br />**Default**: `NORMAL`                                                      |
| `READ`                             | Set the transaction access mode to `READ ONLY` or `READ WRITE`. The current transaction access mode is also exposed as the <InternalLink path="show-vars">session variable</InternalLink> `transaction_read_only`.<br /><br />**Default**: `READ WRITE`                                                                                                                                                               |
| `AS OF SYSTEM TIME`                | Execute the transaction using the database contents "as of" a specified time in the past.<br /><br /> 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.<br /><br />For more information, see <InternalLink path="as-of-system-time">`AS OF SYSTEM TIME`</InternalLink>. |
| `NOT DEFERRABLE`<br />`DEFERRABLE` | This 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 LEVEL`                  | Set the transaction isolation level. Transactions use `SERIALIZABLE` isolation by default. They can be configured to run at <InternalLink path="read-committed">`READ COMMITTED`</InternalLink> isolation.                                                                                                                                                                                                            |

## Examples

### Begin a transaction

#### Use default settings

Without modifying the `BEGIN` statement, the transaction uses `SERIALIZABLE` isolation and `NORMAL` priority.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> BEGIN;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SAVEPOINT cockroach_restart;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> UPDATE products SET inventory = 0 WHERE sku = '8675309';
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> INSERT INTO orders (customer, sku, status) VALUES (1001, '8675309', 'new');
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> RELEASE SAVEPOINT cockroach_restart;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> COMMIT;
```

<Danger>
  This example assumes you're using client-side retry handling.
</Danger>

#### Change isolation level

You can set the transaction isolation level to <InternalLink path="demo-serializable">`SERIALIZABLE`</InternalLink> or <InternalLink path="read-committed#enable-read-committed-isolation">`READ COMMITTED`</InternalLink>.

If not specified, transactions use the value of the current session's <InternalLink path="session-variables">`default_transaction_isolation`</InternalLink> variable.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED;
```

#### Change priority

You can set a transaction's priority to `LOW` or `HIGH`.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> BEGIN PRIORITY HIGH;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SAVEPOINT cockroach_restart;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> UPDATE products SET inventory = 0 WHERE sku = '8675309';
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> INSERT INTO orders (customer, sku, status) VALUES (1001, '8675309', 'new');
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> RELEASE SAVEPOINT cockroach_restart;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> COMMIT;
```

You can also set a transaction's priority with <InternalLink path="set-transaction">`SET TRANSACTION`</InternalLink>.

This example assumes you're using <InternalLink path="transaction-retry-error-reference#client-side-retry-handling">client-side retry handling</InternalLink>.

### Use the `AS OF SYSTEM TIME` option

You can execute the transaction using the database contents "as of" a specified time in the past.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> BEGIN AS OF SYSTEM TIME '2019-04-09 18:02:52.0+00:00';
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SELECT * FROM orders;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SELECT * FROM products;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> COMMIT;
```

<Tip>
  You can also use the <InternalLink path="set-transaction#use-the-as-of-system-time-option">`SET TRANSACTION`</InternalLink> statement inside the transaction to achieve the same results. This syntax is easier to use from <InternalLink path="install-client-drivers">drivers and ORMs</InternalLink>.
</Tip>

### Begin a transaction with automatic retries

CockroachDB will <InternalLink path="transactions#transaction-retries">automatically retry</InternalLink> 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:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> BEGIN;

> DELETE FROM customers WHERE id = 1;

> DELETE orders WHERE customer = 1;

> COMMIT;
```

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

```go theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
db.Exec(
  "BEGIN;

  DELETE FROM customers WHERE id = 1;

  DELETE orders WHERE customer = 1;

  COMMIT;"
)
```

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

* <InternalLink path="transactions">Transactions</InternalLink>
* <InternalLink path="commit-transaction">`COMMIT`</InternalLink>
* <InternalLink path="savepoint">`SAVEPOINT`</InternalLink>
* <InternalLink path="release-savepoint">`RELEASE SAVEPOINT`</InternalLink>
* <InternalLink path="rollback-transaction">`ROLLBACK`</InternalLink>
