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

# How to resolve the TOO_MANY_MUTATIONS error

> A table rejects new mutations with TOO_MANY_MUTATIONS when its queue of unfinished mutations is full. Find the mutation that blocks the queue, kill it, and undo its schema change.

A table accepts new mutations only while it has fewer than `number_of_mutations_to_throw` unfinished ones (1000 by default). After that, every `ALTER TABLE ... UPDATE`, `ALTER TABLE ... DELETE`, `DELETE FROM` and other mutating `ALTER` fails with `TOO_MANY_MUTATIONS` (code 692). Mutations are applied in the order they were created, so usually one mutation that can never finish is at the head of the queue and everything else waits behind it. Find that mutation in `system.mutations`, remove it with `KILL MUTATION`, and the rest of the queue is processed.

## Example

To keep the example short, the limit is lowered to 5 for this table:

```sql theme={null}
CREATE TABLE readings
(
    id UInt32,
    value String
)
ENGINE = MergeTree
ORDER BY id
SETTINGS number_of_mutations_to_throw = 5;

INSERT INTO readings VALUES (1, '10'), (2, 'n/a'), (100, '7');

ALTER TABLE readings MODIFY COLUMN value UInt32;
```

Changing the type from `String` to `UInt32` rewrites the column with a mutation. The mutation fails on the value `'n/a'`, and the `ALTER` reports it (shortened here):

```text theme={null}
Code: 341. DB::Exception: Exception happened during execution of mutation 'mutation_2.txt' with part 'all_1_1_0' reason: 'Code: 6. DB::Exception: Cannot parse string 'n/a' as UInt32: syntax error at begin of string. ... (CANNOT_PARSE_TEXT) ...'. This error maybe retryable or not. In case of unretryable error, mutation can be killed with KILL MUTATION query ... (UNFINISHED)
```

The statement failed, but two things have already happened: the column type in the table definition is now `UInt32`, and the mutation stays in the queue and is retried again and again. With `alter_sync = 0` the `ALTER` does not wait for the mutation and returns without any error, which makes the problem harder to notice.

Reading the column now fails, because the stored strings are converted on the fly:

```sql theme={null}
SELECT * FROM readings ORDER BY id;
```

```text theme={null}
Code: 6. DB::Exception: Cannot parse string 'n/a' as UInt32: syntax error at begin of string. ... (CANNOT_PARSE_TEXT)
```

Every further mutation is queued behind the failing one, until the limit is reached:

```sql theme={null}
ALTER TABLE readings DELETE WHERE id = 100;
ALTER TABLE readings DELETE WHERE id = 101;
ALTER TABLE readings DELETE WHERE id = 102;
ALTER TABLE readings DELETE WHERE id = 103;
ALTER TABLE readings DELETE WHERE id = 104;
```

```text theme={null}
Code: 692. DB::Exception: Too many unfinished mutations (5) in table default.readings (e6bb1afb-43c1-4c99-b4f2-fca52d33aade). (TOO_MANY_MUTATIONS)
```

The last statement is rejected. So is a lightweight `DELETE FROM readings WHERE id = 105`, and so is an `ALTER TABLE readings MODIFY COLUMN value String` that tries to undo the type change.

## Find the mutation that blocks the queue

Look at the oldest unfinished mutation that has a failure reason:

```sql theme={null}
SELECT
    mutation_id,
    command,
    latest_failed_part,
    latest_fail_error_code_name,
    substring(latest_fail_reason, 1, 200) AS latest_fail_reason
FROM system.mutations
WHERE database = currentDatabase() AND table = 'readings' AND NOT is_done AND latest_fail_reason != ''
ORDER BY create_time, length(mutation_id), mutation_id
LIMIT 1
FORMAT Vertical;
```

```text theme={null}
Row 1:
──────
mutation_id:                 mutation_2.txt
command:                     (MODIFY COLUMN `value` UInt32)
latest_failed_part:          all_1_1_0
latest_fail_error_code_name: CANNOT_PARSE_TEXT
latest_fail_reason:          Code: 6. DB::Exception: Cannot parse string 'n/a' as UInt32: syntax error at begin of string. Note: there are toUInt32OrZero and toUInt32OrNull functions, which returns zero/NULL instead of throwing e
```

The mutations queued behind it can show the same `latest_fail_reason`, because a part has to pass the earlier mutation first. The oldest one is the cause.

If no unfinished mutation has a failure reason, the queue is not blocked by an error: mutations are submitted faster than the table applies them. See [Other causes](#other-causes).

## Fix it

Kill the blocking mutation only:

```sql theme={null}
KILL MUTATION WHERE database = currentDatabase() AND table = 'readings' AND mutation_id = 'mutation_2.txt';
```

`KILL MUTATION` removes the mutation from the queue, but it does not undo a schema change. After a failed `MODIFY COLUMN`, the table definition still has the new type and `SELECT` still fails, so change the column back to the type that the data has:

```sql theme={null}
ALTER TABLE readings MODIFY COLUMN value String;
```

Now the remaining mutations run without further action. In the example, the queued `DELETE WHERE id = 100` has been applied:

```sql theme={null}
SELECT count() AS unfinished
FROM system.mutations
WHERE database = currentDatabase() AND table = 'readings' AND NOT is_done;

SELECT * FROM readings ORDER BY id;
```

```text theme={null}
   ┌─unfinished─┐
1. │          0 │
   └────────────┘
   ┌─id─┬─value─┐
1. │  1 │ 10    │
2. │  2 │ n/a   │
   └────┴───────┘
```

If the blocking mutation is an `UPDATE` or `DELETE` with an expression that fails, killing it is enough: there is no schema change to undo.

`KILL MUTATION WHERE database = ... AND table = ...` without a `mutation_id` kills all unfinished mutations of the table. Use it only if you are ready to submit the other mutations again.

To do the type change properly, first make the data convertible, then change the type:

```sql theme={null}
ALTER TABLE readings UPDATE value = '0' WHERE toUInt32OrNull(value) IS NULL SETTINGS mutations_sync = 1;

ALTER TABLE readings MODIFY COLUMN value UInt32;

SELECT * FROM readings ORDER BY id;
```

```text theme={null}
   ┌─id─┬─value─┐
1. │  1 │    10 │
2. │  2 │     0 │
   └────┴───────┘
```

## Other causes

* **Many small mutations.** An application that sends one `ALTER TABLE ... UPDATE` or `DELETE` per row or per event can fill the queue even though each mutation succeeds. Combine the changes into fewer statements. See [How do I view the number of active or queued mutations?](https://clickhouse.com/docs/resources/support-center/knowledge-base/monitoring-debugging/view-number-of-active-mutations.md) to watch the queue.
* **`Nullable` to non-`Nullable`.** Before ClickHouse 25.12, `MODIFY COLUMN` from a `Nullable` type to a non-`Nullable` type got stuck in the same way when the column contained `NULL`. Since 25.12 such an `ALTER` is rejected unless it has a `DEFAULT` expression, and `NULL` values are replaced with that default, for example `ALTER TABLE t MODIFY COLUMN c String DEFAULT ''`.

## About the limits

* [`number_of_mutations_to_throw`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/number-of#number_of_mutations_to_throw) (default 1000) is the number of unfinished mutations at which new ones are rejected.
* [`number_of_mutations_to_delay`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/number-of#number_of_mutations_to_delay) (default 500) is the number at which each new mutation is delayed for a short time before it is accepted.

Both are `MergeTree` settings that you can change for a table with `ALTER TABLE ... MODIFY SETTING`. Raising them does not make the queue shorter: it only postpones the error, so fix the cause first.

For other ways to find failing mutations, see [How to find failed merges and mutations](https://clickhouse.com/docs/resources/support-center/knowledge-base/troubleshooting/find-failed-merges-and-mutations.md).
