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.
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.
These statements are part of an experimental feature set.
Enable experimental transaction support by adding this setting to your configuration:For more information see the page Transactional (ACID) support.
Syntax
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 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 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 runSELECT, 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 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 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 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:Query
Response
Query
Response