Skip to main content
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, where the reason column tells you why each part was detached;
  • the lost_part_count column of system.replicas, which counts parts of replicated tables that no replica could provide.

Detached parts by 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:
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:
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:
Two asynchronous metrics give a rougher number without a query: 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. 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 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 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

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 counter in system.events counts the same event on one server, but it is reset when the server restarts:
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:
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:
DROP DETACHED PART is refused unless the 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 and add the host to the result, replacing default with the name of your cluster: