> ## 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 find failed merges and mutations

> Background merges and mutations do not appear in system.query_log. Find their failures in system.part_log, system.text_log, system.mutations and system.replication_queue.

Merges and mutations run in the background, not as queries, so their errors are not in `system.query_log`. Look for them in these places:

| Table | What it shows |
| - | - |
| [`system.part_log`](https://clickhouse.com/docs/reference/system-tables/part_log) | One row per attempt, with the error code and message, for both merges and mutations |
| [`system.text_log`](https://clickhouse.com/docs/reference/system-tables/text_log) | The server log messages of the background threads, including stack traces |
| [`system.mutations`](https://clickhouse.com/docs/reference/system-tables/mutations) | The latest failure of every unfinished mutation |
| [`system.replication_queue`](https://clickhouse.com/docs/reference/system-tables/replication_queue) | For replicated tables, the last exception of every queued merge or mutation |

A failed merge or mutation is retried, so one problem usually produces the same error many times.

## Example setup

This mutation can never finish, because one of the values is not a number:

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

INSERT INTO readings VALUES (1, '10'), (2, '20'), (3, 'n/a');

ALTER TABLE readings UPDATE value = toString(toUInt32(value) * 2) WHERE 1;
```

The `ALTER` returns without an error, because the mutation is executed in the background. The log tables used below are written with a delay of a few seconds. To see fresh rows at once, run `SYSTEM FLUSH LOGS` first.

## system.part\_log

Failed attempts have a non-zero `error`. The event type is `MergeParts` for a merge and `MutatePart` for a mutation:

```sql theme={null}
SELECT
    event_type,
    database,
    table,
    part_name,
    merged_from,
    errorCodeToName(error) AS error_name,
    substring(exception, 1, 200) AS exception
FROM system.part_log
WHERE event_type IN ('MergeParts', 'MutatePart')
    AND error != 0
    AND event_date >= today() - 1
ORDER BY event_time_microseconds DESC
LIMIT 1
FORMAT Vertical;
```

```text theme={null}
Row 1:
──────
event_type:  MutatePart
database:    default
table:       readings
part_name:   all_1_1_0_2
merged_from: ['all_1_1_0']
error_name:  CANNOT_PARSE_TEXT
exception:   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
```

`part_name` is the part that the operation tried to create, and `merged_from` lists its source parts. The `exception` column holds the full message with the stack trace, so the query cuts it.

To get an overview of what is failing and how often, group the same rows:

```sql theme={null}
SELECT
    hostName() AS host,
    database,
    table,
    event_type,
    errorCodeToName(error) AS error_name,
    count() AS failures,
    max(event_time) AS last_failure
FROM system.part_log
WHERE event_type IN ('MergeParts', 'MutatePart')
    AND error != 0
    AND event_date >= today() - 1
GROUP BY ALL
ORDER BY failures DESC;
```

A merge that was cancelled, for example by `SYSTEM STOP MERGES`, is recorded with the error `ABORTED`. That is not a failure.

## system.text\_log

The background threads that run merges and mutations are named `MergeMutate`, so filter the error messages by thread name:

```sql theme={null}
SELECT
    thread_name,
    logger_name,
    substring(message, 1, 200) AS message
FROM system.text_log
WHERE level = 'Error'
    AND thread_name ILIKE '%merge%'
    AND message ILIKE '%exception%'
    AND event_date >= today() - 1
ORDER BY event_time_microseconds DESC
LIMIT 2
FORMAT Vertical;
```

```text theme={null}
Row 1:
──────
thread_name: MergeMutate
logger_name: MergeTreeBackgroundExecutor
message:     Exception while executing background task {880a4bff-c3d2-4440-a1ba-af29e4c9feab::all_1_1_0_2}: Code: 6. DB::Exception: Cannot parse string 'n/a' as UInt32: syntax error at begin of string. Note: there

Row 2:
──────
thread_name: MergeMutate
logger_name: virtual bool DB::MutatePlainMergeTreeTask::executeStep()
message:     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 identifier in braces is the UUID of the table followed by the name of the part. Select the whole `message` to read the stack trace.

## system.mutations

For mutations, the quickest check is the list of unfinished mutations that have failed at least once:

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

```text theme={null}
Row 1:
──────
database:                    default
table:                       readings
mutation_id:                 mutation_2.txt
command:                     (UPDATE value = toString(toUInt32(value) * 2) WHERE 1)
parts_to_do:                 1
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
```

A mutation that cannot succeed keeps being retried and blocks the mutations queued after it. Remove it with [`KILL MUTATION`](https://clickhouse.com/docs/reference/statements/kill#kill-mutation):

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

See also [How to resolve the `TOO_MANY_MUTATIONS` error](https://clickhouse.com/docs/resources/support-center/knowledge-base/troubleshooting/too-many-mutations-error).

## system.replication\_queue

For replicated tables, merges and mutations are entries of the replication queue. A failing entry shows its last error in `last_exception` and the number of attempts in `num_tries`:

```sql theme={null}
SELECT
    database,
    table,
    type,
    new_part_name,
    num_tries,
    last_exception_time,
    last_exception
FROM system.replication_queue
WHERE type IN ('MERGE_PARTS', 'MUTATE_PART') AND last_exception != ''
ORDER BY num_tries DESC;
```

## Notes

* All these tables describe the server that runs the query. On a cluster, or in ClickHouse Cloud, read them from every replica with the [`clusterAllReplicas`](https://clickhouse.com/docs/reference/functions/table-functions/cluster) table function, for example `FROM clusterAllReplicas('default', system.part_log)`.
* `system.part_log` and `system.text_log` exist only if the [`part_log`](https://clickhouse.com/docs/reference/settings/server-settings/settings/other#part_log) and [`text_log`](https://clickhouse.com/docs/reference/settings/server-settings/settings/other#text_log) sections are present in the server configuration. The configuration file shipped with ClickHouse contains both.
