DB::Exception: Cannot alter column 'col' because it is used in the primary key
0 reputation · 02 Dec 2020, 03:22 UTC
Attempting to Modify a Primary Key Column
When executing ALTER TABLE t MODIFY COLUMN col TYPE String on a MergeTree‑family table that already defines col in its primary key, ClickHouse throws the error DB::Exception: Cannot alter column 'col' because it is used in the primary key. The operation is rejected before any data mutation occurs.
Constraints and Uncertainty
- Primary key columns determine the data layout of MergeTree parts; changing them would require rewriting all parts.
- Current ClickHouse releases (up to 23.x) do not support in‑place primary key mutation.
- Documentation lists this as a limitation, but no version has removed it.
The absence of a clear path to alter primary key columns raises a design question: should ClickHouse allow primary key changes through ALTER TABLE with automatic data reorganization, or is recreating the table the only viable approach?
Unresolved Questions
- What would be the implications of adding support for primary key mutation in MergeTree tables, in terms of performance, consistency, and storage requirements?
- Are there any experimental or beta releases that already implement a safe, incremental rewrite of parts when a primary key column is altered?
- Could a hybrid approach—such as a two‑phase migration that first reindexes new parts and then drops old ones—be integrated into the existing
ALTER TABLEframework?