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

# SET TRANSACTION

The `SET TRANSACTION` [statement](https://docs.cockroachlabs.com/docs/v24.2/sql-statements) sets the transaction priority, access mode, "as of" timestamp, and isolation level. These are applied after you [`BEGIN`](https://docs.cockroachlabs.com/docs/v24.2/begin-transaction) the transaction and before executing the first statement that manipulates a database.

Cockroach Labs recommends leaving the transaction priority at the default setting in almost all cases. Changing the transaction priority to `HIGH` in particular can lead to difficult-to-debug interactions with other transactions executing on the system.

If you are setting a transaction priority to avoid [contention](https://docs.cockroachlabs.com/docs/v24.2/performance-best-practices-overview#transaction-contention) or [hot spots](https://docs.cockroachlabs.com/docs/v24.2/performance-best-practices-overview#hot-spots), or to [get better query performance](https://docs.cockroachlabs.com/docs/v24.2/make-queries-fast), it is usually a sign that you need to update your [schema design](https://docs.cockroachlabs.com/docs/v24.2/schema-design-database) and/or review the data access patterns of your workload.

## Synopsis

![set\_transaction syntax diagram](https://mintcdn.com/cockroachlabs/O8LdfYQ-dz7oGPVh/images/sql-diagrams/v24.2/set_transaction.svg?fit=max\&auto=format\&n=O8LdfYQ-dz7oGPVh\&q=85\&s=cd4473df9e3fc15a6021a0548add0ca4)

## Required privileges

No [privileges](https://docs.cockroachlabs.com/docs/v24.2/security-reference/authorization#managing-privileges) are required to set the transaction priority. However, privileges are required for each statement within a 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`. Transactions with higher priority are less likely to need to be retried. For more information, see [Transactions: Priorities](https://docs.cockroachlabs.com/docs/v24.2/transactions#transaction-priorities). The current priority is also exposed as the read-only [session variable](https://docs.cockroachlabs.com/docs/v24.2/show-vars) `transaction_priority`. **Default**: `NORMAL` |
| `READ` | Set the transaction access mode to `READ ONLY` or `READ WRITE`. The current transaction access mode is also exposed as the [session variable](https://docs.cockroachlabs.com/docs/v24.2/show-vars) `transaction_read_only`. **Default**: `READ WRITE` |
| `AS OF SYSTEM TIME` | Execute 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 [`AS OF SYSTEM TIME`](https://docs.cockroachlabs.com/docs/v24.2/as-of-system-time). |
| `NOT DEFERRABLE` `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 [`READ COMMITTED`](https://docs.cockroachlabs.com/docs/v24.2/read-committed) isolation. This clause only takes effect if specified at the beginning of the transaction. |

## Examples

### Set isolation level

You can set the transaction isolation level to [`SERIALIZABLE`](https://docs.cockroachlabs.com/docs/v24.2/demo-serializable) or [`READ COMMITTED`](https://docs.cockroachlabs.com/docs/v24.2/read-committed#enable-read-committed-isolation).

If not specified, transactions use the value of the current session's [`default_transaction_isolation`](https://docs.cockroachlabs.com/docs/v24.2/session-variables#default-transaction-isolation) variable.

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

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

### Set priority

> [!CAUTION]
>
> This example assumes you're using client-side retry handling.

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

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SET TRANSACTION 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;
```

### 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;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SET TRANSACTION 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;
```

### Set the default transaction priority for a session

To set the default transaction priority for all transactions in a session, use the `default_transaction_priority` [session variable](https://docs.cockroachlabs.com/docs/v24.2/set-vars). For example:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SET default_transaction_priority 'high';
```

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

```
  transaction_priority
------------------------
  high
```

Note that `transaction_priority` is a read-only [session variable](https://docs.cockroachlabs.com/docs/v24.2/show-vars) that cannot be set directly.

## See also

* [`SET`](https://docs.cockroachlabs.com/docs/v24.2/set-vars)
* [Transactions: Priority levels](https://docs.cockroachlabs.com/docs/v24.2/transactions#transaction-priorities)
* [`BEGIN`](https://docs.cockroachlabs.com/docs/v24.2/begin-transaction)
* [`COMMIT`](https://docs.cockroachlabs.com/docs/v24.2/commit-transaction)
* [`SAVEPOINT`](https://docs.cockroachlabs.com/docs/v24.2/savepoint)
* [`RELEASE SAVEPOINT`](https://docs.cockroachlabs.com/docs/v24.2/release-savepoint)
* [`ROLLBACK`](https://docs.cockroachlabs.com/docs/v24.2/rollback-transaction)
