ClickHouse / ClickHouse/ClickHouse
Serialization error after INSERT when using implicit_transaction
- Dominant language
- C++
- Stars
- 49.9k
- Forks
- 9k
- Avg merge
- 21h 32m
- Merged PRs (30d)
- 515
Description
**Describe what's wrong**
I'm testing experimental transactional support and I stumbled across bizarre behavior regarding `INSERT` atomicity.
I properly configured `allow_experimental_transactions` and all statements are executed using:
```
SET implicit_transaction = true, throw_on_unsupported_query_inside_transaction = false;
```
After inserting a larger batch of data I want to ALTER the data by executing the statement that is using the data from the INSERT statement. It results in `Serialization error: part all_1221_1221_0 is locked by transaction 10837109814998401029' exception.
If I wait some time before proceeding with the `ALTER` statement (like 30 seconds) no exception is raised. The same applies if I change `min_insert_block_size_rows` to a number that is higher that the count of the inserted rows (`set min_insert_block_size_rows = 10000000001`) or if I execute `BEGIN TRANSACTION` at the beginning of the SQL block (committing it after).
**Does it reproduce on recent release?**
Yes. Tested on 22.11.1.1360.
**How to reproduce**
```
SET implicit_transaction = true, throw_on_unsupported_query_inside_transaction = false;
DROP TABLE IF EXISTS t
Query id: 61a12db2-aa8a-49a9-b498-d56124d0c3ae
Ok.
0 rows in set. Elapsed: 0.270 sec.
CREATE TABLE t
(
`id` int,
`c1` int
)
ENGINE = MergeTree
ORDER BY id
Query id: c29f60ec-e0eb-4357-9164-98611afc016c
Ok.
0 rows in set. Elapsed: 0.009 sec.
INSERT INTO t (id, c1) SELECT
number,
number % 30
FROM numbers(1000000000)
Query id: 072a822f-caa7-4621-9b61-3941229a30ea
Ok.
0 rows in set. Elapsed: 15.563 sec. Processed 1.00 billion rows, 8.00 GB (64.25 million rows/s., 514.04 MB/s.)
ALTER TABLE t
DELETE WHERE c1 IN (
SELECT DISTINCT c1
FROM t
WHERE (c1 % 17) = 0
)
SETTINGS mutations_sync = 2
Query id: 9495b60d-18b3-48c4-8384-76b6e1d24f9a
Ok.
0 rows in set. Elapsed: 6.717 sec.
Received exception from server (version 22.11.1):
Code: 341. DB::Exception: Received from localhost:9000. DB::Exception: Exception happened during execution of mutation 'mutation_1239.txt' with part 'all_1221_1221_0' reason: 'Serialization error: part all_1221_1221_0 is locked by transaction 10837109814998401029'. This error maybe retryable or not. In case of unretryable error, mutation can be killed with KILL MUTATION query. (UNFINISHED)
```
When I query for that transaction I see:
```
SELECT *
FROM system.transactions_info_log
WHERE tid_hash = 10837109814998401029
LIMIT 1
FORMAT Vertical
Query id: 0e8808de-11dc-4400-9d41-ff9ce251d497
Row 1:
──────
type: Commit
event_date: 2022-11-23
event_time: 2022-11-23 17:20:58.556038
thread_id: 17219
query_id:
tid: (138,35,'6b4435e1-7fe0-4c34-bab4-742c1ba9791c')
tid_hash: 10837109814998401029
csn: 144
database:
table:
uuid: 00000000-0000-0000-0000-000000000000
part:
```
**Expected behavior**
`INSERT` should commit all changes (when no `BEGIN TRANSACTION` was fired) and there shouldn't be any active transaction that is blocking newly created parts.
Contributor guide
Assessment
This issue has not been assessed yet.