ALTER TABLE … ADD COLUMN adds a new field to an existing ClickHouse table without recreating or locking it.
Add new data points quickly without full-table rebuilds and keep queries backward-compatible.
Use ALTER TABLE table_name ADD COLUMN [IF NOT EXISTS] name type [AFTER col]
. Include AFTER
to control column order.
Add DEFAULT
or MATERIALIZED
expressions to back-fill existing rows on the fly.
Chain statements in a single command: ALTER TABLE … ADD COLUMN …, ADD COLUMN …
. Each clause runs atomically.
Always include IF NOT EXISTS
in migrations, test on a staging cluster, and use AFTER
for predictable schemas.
ALTER TABLE Orders ADD COLUMN IF NOT EXISTS customer_ref String AFTER customer_id;
ALTER TABLE OrderItems ADD COLUMN IF NOT EXISTS discounted_price Decimal(10,2) DEFAULT price;
ALTER TABLE Products
ADD COLUMN IF NOT EXISTS is_active UInt8 DEFAULT 1,
ADD COLUMN IF NOT EXISTS updated_at DateTime DEFAULT now();
Omitting IF NOT EXISTS: causes failures in idempotent deploys. Fix by adding the clause.
Ignoring column order: readers may rely on ordinal positions. Specify AFTER
.
No. ClickHouse performs the alteration asynchronously, so reads and writes continue.
Yes. Use ALTER TABLE … DROP COLUMN [IF EXISTS] col_name
. Note that data is removed irreversibly.