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

# BEGIN TRANSACTION

> Documentation for the transaction control statements BEGIN TRANSACTION, COMMIT, ROLLBACK and SET TRANSACTION SNAPSHOT.

Introduced in: v22.4.0

[Experimental feature](https://clickhouse.com/docs/reference/settings/beta-and-experimental-features.md#experimental-features)

[Not supported in ClickHouse Cloud](https://clickhouse.com/docs/products/cloud/guides/cloud-compatibility.md#list-of-unsupported-features)

The transaction control statements `BEGIN TRANSACTION`, `COMMIT`, `ROLLBACK` and `SET TRANSACTION SNAPSHOT` group several queries over `MergeTree` tables into one transaction.
The queries of a transaction read one consistent snapshot of the changes that other transactions commit.
Writes outside transactions are not isolated: an `INSERT`, `TRUNCATE TABLE` or `ALTER TABLE ... DELETE` that runs outside a transaction is visible to the next query of a running transaction, so a stable view requires the writers to use transactions as well, for example with [`implicit_transaction`](https://clickhouse.com/docs/reference/settings/session-settings/implicit#implicit_transaction).
Other transactions see its changes only after `COMMIT`, and `ROLLBACK` discards them.
Queries that run outside a transaction can see the changes of a running transaction before it commits.

> [!NOTE]
>
> These statements are part of an experimental feature set.
> Enable experimental transaction support by adding this setting to your [configuration](https://clickhouse.com/docs/concepts/features/configuration/server-config/configuration-files.md):
>
> ```xml theme={null}
> <clickhouse>
>     <allow_experimental_transactions>1</allow_experimental_transactions>
> </clickhouse>
> ```
>
> For more information see the page [Transactional (ACID) support](https://clickhouse.com/docs/concepts/features/operations/insert/transactions.md#transactions-commit-and-rollback).

## Syntax

```sql theme={null}
BEGIN TRANSACTION
START TRANSACTION
COMMIT
ROLLBACK
SET TRANSACTION SNAPSHOT snapshot
```

A transaction belongs to a session, and a session has at most one transaction.
Over HTTP, send every query of a transaction with the same `session_id`: a request without `session_id` runs in its own session, which ends with the request.
A transaction that is still running when its session ends is rolled back.

## BEGIN TRANSACTION

Starts a transaction in the current session. `START TRANSACTION` is an alias, introduced in v24.3.0.

Nested transactions are not supported: `BEGIN TRANSACTION` inside a transaction throws an `INVALID_TRANSACTION` exception.

## COMMIT

Commits the current transaction and makes its changes visible to other transactions.
`COMMIT` without a current transaction throws an `INVALID_TRANSACTION` exception.

If a query inside the transaction fails, the transaction is rolled back. A query that cannot be parsed, for example because of a syntax error, or whose query parameters are missing or invalid is not executed and leaves the transaction running, so a later `COMMIT` still commits it.
Until `ROLLBACK` ends a rolled-back transaction, `COMMIT` and the other queries that follow throw an `INVALID_TRANSACTION` exception. Only `ROLLBACK`, `SET TRANSACTION SNAPSHOT` and the `EXPLAIN` queries other than `EXPLAIN ANALYZE` still run.

The [`wait_changes_become_visible_after_commit_mode`](https://clickhouse.com/docs/reference/settings/session-settings/other#wait_changes_become_visible_after_commit_mode) setting controls what `COMMIT` waits for.

## ROLLBACK

Rolls back the current transaction and discards its changes.
`ROLLBACK` without a current transaction throws an `INVALID_TRANSACTION` exception.

## SET TRANSACTION SNAPSHOT

Changes the snapshot that the current transaction reads to the commit sequence number (CSN) `snapshot`, for example a value that [`transactionLatestSnapshot`](https://clickhouse.com/docs/reference/functions/regular-functions/other-functions#transactionLatestSnapshot) returned in an earlier transaction.
The transaction then sees its own changes, the data written outside transactions and the changes of the transactions committed up to that CSN.
It reads only the parts that the server still keeps, and it does not protect them from removal: once no running transaction started before a merge, a mutation or another query removed a part, the server can delete that part, and an earlier snapshot then misses its rows.
Do not rely on `SET TRANSACTION SNAPSHOT` for repeatable reads of historical data.
`SET TRANSACTION SNAPSHOT` without a current transaction throws an `INVALID_TRANSACTION` exception.

The values from `0` to `32` are reserved and throw an `INVALID_TRANSACTION` exception, except two:
`1` makes only the data written outside transactions and the transaction's own changes visible, and `3` makes all the parts of a table visible, including the parts of transactions that have not committed yet and the parts that transactions removed or rolled back.

## Queries inside a transaction

A transaction can run `SELECT`, `INSERT`, `DELETE`, `OPTIMIZE`, `TRUNCATE TABLE`, `SET` and `EXPLAIN` queries, and the `ALTER TABLE` mutations and partition operations.
Other queries, for example `CREATE TABLE`, `UPDATE` or `ALTER TABLE ... ADD COLUMN`, throw a `NOT_IMPLEMENTED` exception, and so do queries over tables that do not support transactions, for example `ReplicatedMergeTree` or `Memory` tables, or `system.transactions`.

Asynchronous inserts are not supported inside a transaction, and [`async_insert`](https://clickhouse.com/docs/reference/settings/session-settings/async-insert#async_insert) is enabled by default, so an `INSERT ... VALUES` or `INSERT ... SELECT` inside a transaction throws a `NOT_IMPLEMENTED` exception unless `async_insert` is disabled, for example with `SET async_insert = 0`.

With [`throw_on_unsupported_query_inside_transaction`](https://clickhouse.com/docs/reference/settings/session-settings/throw-on#throw_on_unsupported_query_inside_transaction) set to `0`, some of these queries run outside the transaction instead of throwing, so `ROLLBACK` does not undo them: for example, a `CREATE TABLE` or an `INSERT` into a `Memory` table.

With [`implicit_transaction`](https://clickhouse.com/docs/reference/settings/session-settings/implicit#implicit_transaction) enabled, each query that runs outside a transaction runs in its own transaction, which is committed when the query succeeds and rolled back when it fails.

## Examples

Roll back an insert:

```sql title="Query" theme={null}
CREATE TABLE t (n UInt64) ENGINE = MergeTree ORDER BY n;
SET async_insert = 0;

BEGIN TRANSACTION;
INSERT INTO t VALUES (1);
SELECT count() FROM t;
ROLLBACK;
SELECT count() FROM t;
```

```text title="Response" theme={null}
┌─count()─┐
│       1 │
└─────────┘
┌─count()─┐
│       0 │
└─────────┘
```

Commit an insert:

```sql title="Query" theme={null}
BEGIN TRANSACTION;
INSERT INTO t VALUES (2);
COMMIT;
SELECT * FROM t;
```

```text title="Response" theme={null}
┌─n─┐
│ 2 │
└───┘
```

## Related content

* [Transactional (ACID) support](https://clickhouse.com/docs/concepts/features/operations/insert/transactions.md)
* [`system.transactions`](https://clickhouse.com/docs/reference/system-tables/transactions)
* [`KILL TRANSACTION`](https://clickhouse.com/docs/reference/statements/kill#kill-transaction)
* [`EXPLAIN CURRENT TRANSACTION`](https://clickhouse.com/docs/reference/statements/explain#explain-current-transaction)
