UPDATE is removed only after every data part of its partition that is older than the update has been rewritten by a merge or a mutation. Merges of other parts, even in the same partition, don’t count. If the partition contains a part that background merges no longer select, typically one large old part, its patch parts stay until you rewrite that part, for example with ALTER TABLE ... APPLY PATCHES.
Example
The table below has a lowmax_bytes_to_merge_at_max_space_in_pool, so that the first part is too large to be merged again. On a real table the same role is played by a part that has grown close to the default limit, or that is much larger than the other parts of its partition.
patch-<hash>-<original partition id>:
system.parts again:
202601, but it combined the two new parts. The large part 202601_1_1_0 was not rewritten, so the patch part is still there. Queries are correct in the meantime, because SELECT applies the patch on read: SELECT status FROM events WHERE id = 7 returns done.
Why this happens
- A patch is written into a data part only when that part itself is rewritten: by a merge that includes it (the
apply_patches_on_mergesetting, enabled by default) or by a mutation. - A background task removes a patch part when no active data part of the partition has a
data_versionlower than thedata_versionof the patch. Parts inserted after the update are new enough. Every older part has to be rewritten, including parts in which the update changed no rows. - Background merges work within one partition, merge large parts less often than small ones, and don’t select a part when the merge would exceed
max_bytes_to_merge_at_max_space_in_pool. A large old part can therefore stay untouched for a long time while the smaller parts next to it keep merging.
What pending patch parts cost
SELECTqueries have to apply the patches on read. While a table has patch parts, projections are not used, and skipping indexes are not used for parts with pending patches (see Performance considerations).- Lazy materialization (
query_plan_optimize_lazy_materialization) is not used for a table that has patch parts, which can makeSELECT ... ORDER BY ... LIMITqueries over wide tables noticeably more expensive. - When the uncompressed size of all patch parts of a table reaches
max_uncompressed_bytes_in_patches(30 GiB by default), further lightweight updates are rejected withTOO_LARGE_LIGHTWEIGHT_UPDATES.
Find the parts that hold patches
Count the patch parts per table:Apply the patches
To apply a backlog now, runAPPLY PATCHES for the table or for one partition. It is a mutation that rewrites only the affected columns. It runs in the background, and you can follow it in system.mutations. The example waits for it with mutations_sync:
system.parts from the example now returns:
data_version above the version of the patch. The applied patch part is still listed as active for a short time, until the background cleanup removes it. Judge the backlog by the data_version of the data parts, as in the query above, and not by the number of patch parts.
OPTIMIZE TABLE events PARTITION 202601 FINAL applies the patches too, but it rewrites all columns of all parts of the partition.
To put an upper bound on the age of patches without manual statements, force merges of old parts:
min_age_to_force_merge_secondsmerges parts when every part in the range is older than the given age. Withmin_age_to_force_merge_on_partition_onlyit applies only to a whole partition, so a partition that keeps receiving inserts is never merged this way.- Since ClickHouse 26.2 these forced merges respect
max_bytes_to_merge_at_max_space_in_poolby default (theenable_max_bytes_limit_for_min_age_to_force_mergesetting), so they don’t rewrite a part that is already at the size limit. min_partition_age_to_force_merge_seconds, available since ClickHouse 26.9, forces merges in partitions that no longer receive inserts.- A partition that consists of a single data part is not merged by
min_age_to_force_merge_seconds. UseAPPLY PATCHESfor it.