> ## 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 monitor ClickHouse for broken and lost data parts

> Watch the reason column of system.detached_parts for parts that the server set aside as broken, and lost_part_count in system.replicas for parts that no replica has.

When ClickHouse finds a data part of a `MergeTree` table that it cannot use, it does not delete the part: it moves the part to the `detached` directory of the table and records why. Monitor two things:

* [`system.detached_parts`](https://clickhouse.com/docs/reference/system-tables/detached_parts), where the `reason` column tells you why each part was detached;
* the `lost_part_count` column of [`system.replicas`](https://clickhouse.com/docs/reference/system-tables/replicas), which counts parts of replicated tables that no replica could provide.

## Detached parts by reason

```sql theme={null}
SELECT
    database,
    table,
    reason,
    count() AS parts,
    formatReadableSize(sum(bytes_on_disk)) AS size
FROM system.detached_parts
GROUP BY
    database,
    table,
    reason
ORDER BY
    database,
    table,
    reason;
```

The following output comes from a test table where one partition was detached with `ALTER TABLE ... DETACH PARTITION` and one part had a truncated file when the table was loaded:

```text theme={null}
   ┌─database─┬─table──┬─reason──────────┬─parts─┬─size──────┐
1. │ default  │ events │                 │     1 │ 12.11 KiB │
2. │ default  │ events │ broken-on-start │     1 │ 5.57 KiB  │
   └──────────┴────────┴─────────────────┴───────┴───────────┘
```

An empty `reason` means that a user detached the part, which is not a problem. The reasons `attaching`, `deleting` and `tmp-fetch` are not a problem either: they mark a directory that an `ATTACH PART`, `DROP DETACHED PART` or `FETCH PARTITION` query is working on, and the prefix is gone when the query ends. Every other value means that the server detached the part itself. For an alert, count only those:

```sql theme={null}
SELECT count() AS unexpected_detached_parts
FROM system.detached_parts
WHERE coalesce(reason, 'unknown') NOT IN ('', 'attaching', 'deleting', 'tmp-fetch');
```

```text theme={null}
   ┌─unexpected_detached_parts─┐
1. │                         1 │
   └───────────────────────────┘
```

`reason` is `NULL` for a directory in `detached` whose name is not a valid part name, so the query uses `coalesce` to count such directories too. To see the parts themselves:

```sql theme={null}
SELECT
    database,
    table,
    name,
    reason,
    disk,
    modification_time
FROM system.detached_parts
WHERE coalesce(reason, 'unknown') NOT IN ('', 'attaching', 'deleting', 'tmp-fetch')
ORDER BY modification_time DESC;
```

Two asynchronous metrics give a rougher number without a query: [`NumberOfDetachedParts`](https://clickhouse.com/docs/reference/system-tables/asynchronous_metrics#numberofdetachedparts) is the total number of detached parts and `NumberOfDetachedByUserParts` is the number detached by users. Their difference is not the same as the result of the alert query: it includes the directories of operations in progress, and it leaves out directories whose name is not a valid part name.

## What the reasons mean

The `reason` is the prefix of the directory name in `detached`, for example `broken-on-start_202602_2_2_0`.

| `reason` | Meaning |
| - | - |
| empty | A user detached the part with `ALTER TABLE ... DETACH PARTITION` or `DETACH PART`. |
| `broken-on-start` | The part could not be loaded when the table was started, for example because a file is missing or has the wrong size. |
| `broken` | Replicated table: the part failed a check while the server was running. The replica fetches a healthy copy from another replica. |
| `covered-by-broken` | Replicated table: a part covered by a broken part, detached together with it. |
| `unexpected`, `ignored` | Replicated table: a part that exists on disk but is not registered in ClickHouse Keeper, or an outdated part that was set aside. |
| `noquorum` | Replicated table: a part written by an insert that did not reach its quorum. |
| `clone` | Replicated table: a local part that the source replica does not have, saved when a lost replica was restored from another replica. |
| `merge-not-byte-identical`, `mutate-not-byte-identical` | Replicated table with the `detach_not_byte_identical_parts` setting: the result of a merge or mutation that differs from the result on other replicas. |
| `broken-from-backup` | A broken part found in a backup by `RESTORE` with the `restore_broken_parts_as_detached` setting. |

The prefixes `attaching`, `deleting` and `tmp-fetch` mark directories of operations that are in progress, which is why the alert query skips them.

What happens to the data depends on the engine:

* A non-replicated `MergeTree` table has no other copy. The rows of a detached part are missing from query results; to get them back, restore the data from a backup or insert it again. The server checks the parts of such a table when it loads the table. A part that is damaged later stays in the table, queries that read the damaged data fail, and [`CHECK TABLE`](https://clickhouse.com/docs/reference/statements/check-table) reports the part.
* A `ReplicatedMergeTree` table downloads the part from another replica. If no replica has it, the part is lost: the server replaces it with an empty part, logs `Part ... is lost forever.` and increments `lost_part_count`.

If more parts of one table are broken at startup than [`max_suspicious_broken_parts`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/max-suspicious-broken-parts#max_suspicious_broken_parts) allows, the server does not detach them: loading the table fails with the `TOO_MANY_UNEXPECTED_DATA_PARTS` exception, so that you can investigate first.

## Lost parts of replicated tables

```sql theme={null}
SELECT
    database,
    table,
    lost_part_count
FROM system.replicas
WHERE lost_part_count > 0;
```

`lost_part_count` is the number of parts lost by all replicas of the table since the table was created. It is stored in ClickHouse Keeper, so it survives restarts, and the stored value never decreases: alert on an increase, not on a non-zero value. The column is available since ClickHouse 23.4.

The server reads the value from ClickHouse Keeper each time you query the column and shows `0` when it cannot, that is while `is_session_expired` is `1` or `zookeeper_exception` is not empty. After a short outage of ClickHouse Keeper the column returns from `0` to the stored value, which is not a new loss. Ignore the samples taken in that state (`WHERE NOT is_session_expired AND zookeeper_exception = ''`), and compare a sample with the highest value seen before, not with the previous sample.

The [`ReplicatedDataLoss`](https://clickhouse.com/docs/reference/system-tables/events#replicateddataloss) counter in `system.events` counts the same event on one server, but it is reset when the server restarts:

```sql theme={null}
SELECT
    event,
    value
FROM system.events
WHERE event IN ('ReplicatedDataLoss', 'ReplicatedPartChecksFailed');
```

On a healthy server this query returns no rows: `system.events` lists only the counters that are greater than zero.

## Check a table on demand

`CHECK TABLE` verifies the checksums of every part of a table and returns one row per part:

```sql theme={null}
CHECK TABLE events
SETTINGS check_query_single_value_result = 0;
```

```text theme={null}
   ┌─part_path────┬─is_passed─┬─message─┐
1. │ 202601_1_1_0 │         1 │         │
   └──────────────┴───────────┴─────────┘
```

A damaged part has `is_passed = 0` and the exception text, such as `CHECKSUM_DOESNT_MATCH`, in `message`. The query reads all the data of the table, so run it on demand and not as a periodic check.

## Remove detached parts

The server does not use detached parts, but they keep occupying disk space. Once you no longer need a detached part, remove it:

```sql theme={null}
ALTER TABLE events
    DROP DETACHED PART 'broken-on-start_202602_2_2_0'
SETTINGS allow_drop_detached = 1;
```

[`DROP DETACHED PART`](https://clickhouse.com/docs/reference/statements/alter/partition#drop-detached-partitionpart) is refused unless the [`allow_drop_detached`](https://clickhouse.com/docs/reference/settings/session-settings/allow#allow_drop_detached) setting is enabled.

## Monitor all servers of a cluster

`system.detached_parts` and `system.events` describe the server that runs the query. To cover every replica, read them through [`clusterAllReplicas`](https://clickhouse.com/docs/reference/functions/table-functions/cluster) and add the host to the result, replacing `default` with the name of your cluster:

```sql theme={null}
SELECT
    hostName() AS host,
    count() AS unexpected_detached_parts
FROM clusterAllReplicas('default', system.detached_parts)
WHERE coalesce(reason, 'unknown') NOT IN ('', 'attaching', 'deleting', 'tmp-fetch')
GROUP BY host;
```
