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

# SET TRANSACTION

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 `SET TRANSACTION` <InternalLink path="sql-statements">statement</InternalLink> sets the transaction priority, access mode, and "as of" timestamp after you <InternalLink path="begin-transaction">`BEGIN`</InternalLink> it but before executing the first statement that manipulates a database.

## Synopsis

<img src="https://mintcdn.com/cockroachlabs/hRoSoqt0mqHbZVjm/images/sql-diagrams/v23.1/set_transaction.svg?fit=max&auto=format&n=hRoSoqt0mqHbZVjm&q=85&s=a6e2c415ccd1e043f731f9a7ff7cd0e4" alt="set_transaction syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="691" height="399" data-path="images/sql-diagrams/v23.1/set_transaction.svg" />

## Required privileges

No <InternalLink path="security-reference/authorization#managing-privileges">privileges</InternalLink> 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 <InternalLink path="transactions#transaction-priorities">Transactions: Priorities</InternalLink>.<br /><br />The current priority is also exposed as the read-only <InternalLink path="show-vars">session variable</InternalLink> `transaction_priority`.<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.                                                                                                                                                                                                                                                                                                                   |

CockroachDB now only supports `SERIALIZABLE` isolation, so transactions can no longer be meaningfully set to any other `ISOLATION LEVEL`. In previous versions of CockroachDB, you could set transactions to `SNAPSHOT` isolation, but that feature has been removed.

## Examples

### Set priority

```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` <InternalLink path="set-vars">session variable</InternalLink>. 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 <InternalLink path="show-vars">session variable</InternalLink> that cannot be set directly.

## See also

* <InternalLink path="set-vars">`SET`</InternalLink>
* <InternalLink path="transactions#transaction-priorities">Transactions: Priority levels</InternalLink>
* <InternalLink path="begin-transaction">`BEGIN`</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>
