Overview

Parts are immutable, so changing existing rows means rewriting or masking them. ClickHouse offers heavy mutations (ALTER TABLE ... UPDATE|DELETE), lightweight deletes (DELETE FROM) and, in recent versions, lightweight updates (UPDATE ... SET) through patch parts. They are for corrections, not for OLTP-style workloads (ch-when-not-to-use).

Core concepts

  • ALTER TABLE t UPDATE/DELETE WHERE ... is a mutation: asynchronous, rewrites affected parts (new part names get a mutation version suffix such as all_1_1_0_4). Track in system.mutations (is_done, parts_to_do, latest_fail_reason); SETTINGS mutations_sync = 1|2 waits.
  • Lightweight DELETE FROM marks rows deleted with a hidden mask column; they vanish from queries immediately and are physically removed by later merges.
  • Lightweight UPDATE (newer versions) writes a small patch part that is applied at read time and merged later. It needs the table settings enable_block_number_column = 1 and enable_block_offset_column = 1.
  • Alternatives that avoid updates: insert a new version and use ReplacingMergeTree (ch-replacing-collapsing-engines), or design immutable event data and recompute aggregates.
  • Replicated tables run mutations through Keeper on every replica (ch-replication-keeper).

Worked example

A table of 2,000,000 rows (two parts, 8.5 MiB):

ALTER TABLE mut UPDATE status = 'done' WHERE id = 5 SETTINGS mutations_sync = 2;
-- system.mutations: mutation_3.txt, is_done = 1, parts_to_do = 0
-- system.part_log: MutatePart events appear for the rewritten parts
 
DELETE FROM mut WHERE id < 1000;            -- count() immediately 1,999,000
UPDATE mut SET status = 'light' WHERE id = 7;
-- ERROR NOT_IMPLEMENTED: Lightweight updates are supported only for tables with materialized
-- _block_number column. Run MODIFY SETTING enable_block_number_column = 1
ALTER TABLE mut MODIFY SETTING enable_block_number_column = 1, enable_block_offset_column = 1;
UPDATE mut SET status = 'light' WHERE id = 1500;
SELECT id, status FROM mut WHERE id IN (1500, 1501);    -- 1500 light | 1501 new

After the lightweight update system.parts listed an extra 1-row patch part (patch-..., Compact) next to the two data parts. (The first lightweight update targeted a row that the earlier DELETE had removed, so it changed nothing: always check the filter.)

Pitfalls and production notes

  • Heavy mutations on huge tables rewrite lots of data; avoid frequent small mutations, and check system.mutations for stuck ones.
  • Mutations do not respect transactions; concurrent inserts are not affected.
  • Data-subject deletion (GDPR) needs a plan: lightweight deletes mask rows, but physical removal waits for merges; use OPTIMIZE ... FINAL or ALTER ... DELETE when erasure must be physical.
  • Lightweight update and delete semantics and requirements evolve between versions; check release notes.
  • Compare with in-place updates in PostgreSQL (pg-mvcc) and Oracle (oracle-redo-undo).

Practice questions

  1. Why is ALTER TABLE ... UPDATE called a mutation and why is it asynchronous?
  2. How does a lightweight delete hide a row before it is physically removed?
  3. What setting did the first lightweight update require?

References

  • ClickHouse documentation, ALTER TABLE UPDATE/DELETE; Lightweight DELETE; Lightweight UPDATE

ch-replacing-collapsing-engines · ch-mergetree-parts · ch-ttl-tiered-storage