docs/en/sql-reference/transactions.md
import ExperimentalBadge from '@theme/badges/ExperimentalBadge'; import CloudNotSupportedBadge from '@theme/badges/CloudNotSupportedBadge';
This is transactional (ACID) if the inserted rows are packed and inserted as a single block (see Notes):
insert_quorum setting), and ClickHouse can ask the OS to sync the filesystem data on the storage media (controlled by the fsync_after_insert setting).Same as Case 1 above, with this detail:
Same as Case 1 above, with this detail:
Same as Case 1 above, with this detail:
async_insert is enabled and wait_for_async_insert is set to 1 (the default), but if wait_for_async_insert is set to 0, then atomicity is not ensured.max_insert_block_size rows (~1 000 000 by default) or less then min_chunk_bytes_for_parallel_parsing bytes (10 MB by default) in case of parallel parsing is used (enabled by default)max_block_size, max_insert_block_size, min_insert_block_size_rows, min_insert_block_size_bytes, preferred_block_size_bytes, etc)In addition to the functionality described at the top of this document, ClickHouse has experimental support for transactions, commits, and rollback functionality.
config.d/transactions.xml:
<clickhouse>
<allow_experimental_transactions>1</allow_experimental_transactions>
</clickhouse>
UNKNOWN_FUNCTION exceptions caused by typos.These examples are with a single node ClickHouse server with ClickHouse Keeper enabled.
<clickhouse>
<allow_experimental_transactions>1</allow_experimental_transactions>
</clickhouse>
:::note See the deployment documentation for details on deploying ClickHouse server and a proper quorum of ClickHouse Keeper nodes. The configuration shown here is for experimental purposes. :::
<clickhouse replace="true">
<logger>
<level>debug</level>
<log>/var/log/clickhouse-server/clickhouse-server.log</log>
<errorlog>/var/log/clickhouse-server/clickhouse-server.err.log</errorlog>
<size>1000M</size>
<count>3</count>
</logger>
<display_name>node 1</display_name>
<listen_host>0.0.0.0</listen_host>
<http_port>8123</http_port>
<tcp_port>9000</tcp_port>
<zookeeper>
<node>
<host>clickhouse-01</host>
<port>9181</port>
</node>
</zookeeper>
<keeper_server>
<tcp_port>9181</tcp_port>
<server_id>1</server_id>
<log_storage_path>/var/lib/clickhouse/coordination/log</log_storage_path>
<snapshot_storage_path>/var/lib/clickhouse/coordination/snapshots</snapshot_storage_path>
<coordination_settings>
<operation_timeout_ms>10000</operation_timeout_ms>
<session_timeout_ms>30000</session_timeout_ms>
<raft_logs_level>information</raft_logs_level>
</coordination_settings>
<raft_configuration>
<server>
<id>1</id>
<hostname>clickhouse-keeper-01</hostname>
<port>9234</port>
</server>
</raft_configuration>
</keeper_server>
</clickhouse>
Issue a BEGIN TRANSACTION or START TRANSACTION followed by a ROLLBACK to verify that experimental transactions are enabled, and that ClickHouse Keeper is enabled as it is used to track transactions.
BEGIN TRANSACTION
Ok.
:::tip
If you see the following error, then check your configuration file to make sure that allow_experimental_transactions is set to 1 (or any value other than 0 or false).
Code: 48. DB::Exception: Received from localhost:9000.
DB::Exception: Transactions are not supported.
(NOT_IMPLEMENTED)
You can also check ClickHouse Keeper by issuing
echo ruok | nc localhost 9181
ClickHouse Keeper should respond with imok.
:::
ROLLBACK
Ok.
:::tip Creation of tables is not transactional. Run this DDL query outside of a transaction. :::
CREATE TABLE mergetree_table
(
`n` Int64
)
ENGINE = MergeTree
ORDER BY n
Ok.
BEGIN TRANSACTION
Ok.
INSERT INTO mergetree_table FORMAT Values (10)
Ok.
SELECT *
FROM mergetree_table
┌──n─┐
│ 10 │
└────┘
:::note You can query the table from within a transaction and see that the row was inserted even though it has not yet been committed. :::
Verify that the transaction is rolled back:
ROLLBACK
Ok.
SELECT *
FROM mergetree_table
Ok.
0 rows in set. Elapsed: 0.002 sec.
BEGIN TRANSACTION
Ok.
INSERT INTO mergetree_table FORMAT Values (42)
Ok.
COMMIT
Ok. Elapsed: 0.002 sec.
SELECT *
FROM mergetree_table
┌──n─┐
│ 42 │
└────┘
You can inspect transactions by querying the system.transactions table, but note that you cannot query that
table from a session that is in a transaction. Open a second clickhouse client session to query that table.
SELECT *
FROM system.transactions
FORMAT Vertical
Row 1:
──────
tid: (33,61,'51e60bce-6b82-4732-9e1d-b40705ae9ab8')
tid_hash: 11240433987908122467
elapsed: 210.017820947
is_readonly: 1
state: RUNNING
See this meta issue to find much more extensive tests and to keep up to date with the progress.