> ## 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.

# Why patch parts of lightweight updates stay for days although merges are running

> A patch part of a lightweight UPDATE is removed only after every data part of its partition has been rewritten. Learn how to find the parts that hold patches and how to apply them.

A patch part created by a [lightweight `UPDATE`](https://clickhouse.com/docs/reference/statements/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`](https://clickhouse.com/docs/reference/statements/alter/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.

```sql theme={null}
CREATE TABLE events
(
    event_date Date,
    id UInt64,
    payload String,
    status String
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(event_date)
ORDER BY id
SETTINGS
    enable_block_number_column = 1,
    enable_block_offset_column = 1,
    max_bytes_to_merge_at_max_space_in_pool = 1000000;

INSERT INTO events
SELECT
    toDate('2026-01-01') + number % 30,
    number,
    hex(sipHash128(number)),
    'new'
FROM numbers(200000);

UPDATE events SET status = 'done' WHERE id = 7;
```

The update creates a patch part in its own partition, named `patch-<hash>-<original partition id>`:

```sql theme={null}
SELECT
    partition_id,
    name,
    rows,
    data_version
FROM system.parts
WHERE database = currentDatabase() AND `table` = 'events' AND active
ORDER BY name;
```

```text theme={null}
   ┌─partition_id──────────────────────────────────┬─name──────────────────────────────────────────────────┬───rows─┬─data_version─┐
1. │ 202601                                        │ 202601_1_1_0                                          │ 200000 │            1 │
2. │ patch-1748dd0ab3aee22c6f8a3df840546a1c-202601 │ patch-1748dd0ab3aee22c6f8a3df840546a1c-202601_3_3_0_2 │      1 │            2 │
   └───────────────────────────────────────────────┴───────────────────────────────────────────────────────┴────────┴──────────────┘
```

Insert more rows into the same partition and let a merge run:

```sql theme={null}
INSERT INTO events VALUES ('2026-01-30', 1000001, 'a', 'new');
INSERT INTO events VALUES ('2026-01-31', 1000002, 'b', 'new');

OPTIMIZE TABLE events;
```

Run the query on `system.parts` again:

```text theme={null}
   ┌─partition_id──────────────────────────────────┬─name──────────────────────────────────────────────────┬───rows─┬─data_version─┐
1. │ 202601                                        │ 202601_1_1_0                                          │ 200000 │            1 │
2. │ 202601                                        │ 202601_4_5_1                                          │      2 │            4 │
3. │ patch-1748dd0ab3aee22c6f8a3df840546a1c-202601 │ patch-1748dd0ab3aee22c6f8a3df840546a1c-202601_3_3_0_2 │      1 │            2 │
   └───────────────────────────────────────────────┴───────────────────────────────────────────────────────┴────────┴──────────────┘
```

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`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/other#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`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/max-bytes#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](https://clickhouse.com/docs/reference/statements/update.md#performance-considerations)).
* Lazy materialization ([`query_plan_optimize_lazy_materialization`](https://clickhouse.com/docs/reference/settings/session-settings/query-plan#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`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/max#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:

```sql theme={null}
SELECT
    database,
    `table`,
    count() AS patch_parts,
    sum(rows) AS rows_in_patches,
    formatReadableSize(sum(data_uncompressed_bytes)) AS uncompressed
FROM system.parts
WHERE active AND startsWith(partition_id, 'patch-')
GROUP BY
    database,
    `table`
ORDER BY sum(data_uncompressed_bytes) DESC;
```

```text theme={null}
   ┌─database─┬─table──┬─patch_parts─┬─rows_in_patches─┬─uncompressed─┐
1. │ default  │ events │           1 │               1 │ 91.00 B      │
   └──────────┴────────┴─────────────┴─────────────────┴──────────────┘
```

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:

```sql theme={null}
WITH patches AS
    (
        SELECT
            replaceRegexpOne(partition_id, '^patch-[0-9a-f]{32}-', '') AS base_partition_id,
            max(data_version) AS newest_patch_version
        FROM system.parts
        WHERE database = currentDatabase() AND `table` = 'events' AND active AND startsWith(partition_id, 'patch-')
        GROUP BY base_partition_id
    )
SELECT
    partition_id,
    name,
    data_version,
    newest_patch_version,
    rows,
    formatReadableSize(bytes_on_disk) AS size
FROM system.parts AS parts
INNER JOIN patches ON parts.partition_id = patches.base_partition_id
WHERE database = currentDatabase() AND `table` = 'events' AND active AND data_version < newest_patch_version
ORDER BY bytes_on_disk DESC;
```

```text theme={null}
   ┌─partition_id─┬─name─────────┬─data_version─┬─newest_patch_version─┬───rows─┬─size─────┐
1. │ 202601       │ 202601_1_1_0 │            1 │                    2 │ 200000 │ 6.91 MiB │
   └──────────────┴──────────────┴──────────────┴──────────────────────┴────────┴──────────┘
```

## 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`](https://clickhouse.com/docs/reference/system-tables/mutations). The example waits for it with `mutations_sync`:

```sql theme={null}
ALTER TABLE events APPLY PATCHES IN PARTITION 202601 SETTINGS mutations_sync = 2;
```

The query on `system.parts` from the example now returns:

```text theme={null}
   ┌─partition_id──────────────────────────────────┬─name──────────────────────────────────────────────────┬───rows─┬─data_version─┐
1. │ 202601                                        │ 202601_1_1_0_6                                        │ 200000 │            6 │
2. │ 202601                                        │ 202601_4_5_1_6                                        │      2 │            6 │
3. │ patch-1748dd0ab3aee22c6f8a3df840546a1c-202601 │ patch-1748dd0ab3aee22c6f8a3df840546a1c-202601_3_3_0_2 │      1 │            2 │
   └───────────────────────────────────────────────┴───────────────────────────────────────────────────────┴────────┴──────────────┘
```

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:

* [`min_age_to_force_merge_seconds`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/min-age#min_age_to_force_merge_seconds) merges parts when every part in the range is older than the given age. With [`min_age_to_force_merge_on_partition_only`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/min-age#min_age_to_force_merge_on_partition_only) it 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_pool` by default (the [`enable_max_bytes_limit_for_min_age_to_force_merge`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/enable#enable_max_bytes_limit_for_min_age_to_force_merge) setting), so they don't rewrite a part that is already at the size limit.
* [`min_partition_age_to_force_merge_seconds`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/min#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`. Use `APPLY PATCHES` for it.

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.

## Related pages

* [Lightweight `UPDATE`](https://clickhouse.com/docs/reference/statements/update)
* [`ALTER TABLE ... APPLY PATCHES`](https://clickhouse.com/docs/reference/statements/alter/apply-patches)
* [Part merges](https://clickhouse.com/docs/concepts/core-concepts/merges.md)
