Back to Clickhouse

Apply patches from lightweight updates

docs/en/sql-reference/statements/alter/apply-patches.md

26.4.1.1-new2.2 KB
Original Source

import BetaBadge from '@theme/badges/BetaBadge';

<BetaBadge/>
sql
ALTER TABLE [db.]table [ON CLUSTER cluster] APPLY PATCHES [IN PARTITION partition_id]

The command manually triggers the physical materialization of patch parts created by lightweight UPDATE statements. It forcefully applies pending patches to the data parts by rewriting only the affected columns.

:::note

  • It only works for tables in the MergeTree family (including replicated tables).
  • This is a mutation operation and executes asynchronously in the background. :::

When to use APPLY PATCHES {#when-to-use}

:::tip Generally, you should not need to use APPLY PATCHES :::

Patch parts are normally applied automatically during merges when the apply_patches_on_merge setting is enabled (default). However, you may want to manually trigger patch application in these scenarios:

  • To reduce the overhead of applying patches during SELECT queries
  • To consolidate multiple patch parts before they accumulate
  • To prepare data for backup or export with patches already materialized
  • When apply_patches_on_merge is disabled and you want to control when patches are applied

Examples {#examples}

Apply all pending patches for a table:

sql
ALTER TABLE my_table APPLY PATCHES;

Apply patches only for a specific partition:

sql
ALTER TABLE my_table APPLY PATCHES IN PARTITION '2024-01';

Combine with other operations:

sql
ALTER TABLE my_table APPLY PATCHES, UPDATE column = value WHERE condition;

Monitoring patch application {#monitor}

You can monitor the progress of patch application using the system.mutations table:

sql
SELECT * FROM system.mutations
WHERE table = 'my_table' AND command LIKE '%APPLY PATCHES%';

See also {#see-also}