Skip to main content
A patch part created by a lightweight 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 low max_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.
The update creates a patch part in its own partition, named patch-<hash>-<original partition id>:
Insert more rows into the same partition and let a merge run:
Run the query on system.parts again:
A merge ran in partition 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_merge setting, 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_version lower than the data_version of 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.
So a steady number of merges on the table doesn’t show that patches are being applied.

What pending patch parts cost

  • SELECT queries 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 make SELECT ... ORDER BY ... LIMIT queries 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 with TOO_LARGE_LIGHTWEIGHT_UPDATES.

Find the parts that hold patches

Count the patch parts per table:
Then list, for one table, the data parts that are older than the newest patch of their partition. These are the parts that keep the patch parts alive:

Apply the patches

To apply a backlog now, run APPLY 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:
The query on system.parts from the example now returns:
Both data parts now have a 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: Each forced merge rewrites the parts it merges completely, so check the size of the parts involved before you enable these settings on a large table.