ClickHouse and dbt - A Gift from the Community
The dbt-clickhouse adapter, started by community contributor Dmitriy Sokolov and later taken over as a vendor-supported plugin, maps dbt's view, table, incremental and ephemeral materializations onto ClickHouse features. The default incremental strategy builds a temporary table of changed rows, copies all unchanged rows into a new table and atomically swaps it in, which duplicates the whole dataset on every run. A newer delete+insert strategy uses ClickHouse 22.8's experimental lightweight deletes (tombstones removed during merges) to write only the changed rows and performs significantly better. Its caveats: it needs the experimental setting enabled, and because it modifies the target table directly with no transaction, a failure can leave the model in an invalid state.
Related posts
- ClickHouse Newsletter April 2023: Lightweight Deletes
ClickHouse mediumAdept
ClickHouse 23.3 makes lightweight deletes (DELETE FROM with a row condition) generally available and the preferred way to remove data, and also adds trace visualization to the Grafana plugin and parallel replicas that split a shard's data across replicas. Lightweight deletes simplify incremental updates of denormalized, JOIN-materialized tables. Instead of copying the whole target table while excluding changed ids and then swapping it in with EXCHANGE, changed rows go into a temporary table, are deleted from the target by id, and are re-inserted. The dbt-clickhouse plugin now recommends this approach for incremental materializations.
- Handling Updates and Deletes in ClickHouse
ClickHouse mediumAdept
ClickHouse offers several ways to modify mostly-immutable analytical data, each with different costs. Lightweight Deletes (DELETE FROM) write a _row_exists mask and rely on background merges to physically remove rows, which makes them cheap but leaves space reclamation non-deterministic. ALTER UPDATE/DELETE mutations rewrite whole parts, so they are heavy but guarantee removal, which matters for compliance. Cheaper alternatives cover specific cases: swapping in a new column with a DEFAULT for whole-table updates, Join engine plus joinGet for join-based updates, dropping partitions or using TTL for bulk and time-based deletion, and CollapsingMergeTree sign rows for frequent row-level updates.
- How Spyne simplified their CDC pipeline with ClickPipes and ClickHouse Cloud
ClickHouse mediumAdept
Spyne replaced a self-managed Debezium → Kafka → ClickHouse Kafka-engine/materialized-view CDC pipeline with ClickPipes on ClickHouse Cloud. The move eliminated 1–1.5 hours of manual work per table onboarding and fixed schema drift, where new source columns were silently dropped by DDL-locked materialized views. It also brought tradeoffs: snapshotting a new table pauses the whole pipe, so critical tables were isolated into dedicated pipes. ReplacingMergeTree's eventual consistency required adding FINAL to queries at extra CPU cost, and binlog loss forces full table resyncs instead of targeted partial ones.
- Postgres to ClickHouse: Data Modeling Tips
ClickHouse mediumAdept
Postgres tables replicated to ClickHouse via PeerDB land in ReplacingMergeTree tables where updates and deletes arrive as versioned inserts (_peerdb_version, _peerdb_is_deleted), so duplicates persist until background merges run. Deduplicate at query time with FINAL, argMax, ROW_NUMBER window functions or views wrapping them, and hide deleted rows with row policies. Pick ordering keys from frequently filtered columns in ascending cardinality, keep PRIMARY KEY a prefix of ORDER BY, and remember ClickHouse's primary key is a non-unique sparse index rather than a B-tree. Columns need explicit Nullable types, and ordering keys can be changed via materialized views or by predefining and swapping target tables.
- Postgres to ClickHouse: Data Modeling Tips V2
ClickHouse mediumAdept
Postgres CDC into ClickHouse (via PeerDB or ClickPipes) uses logical decoding and maps tables to ReplacingMergeTree. Updates and deletes arrive as versioned inserts, so queries can see duplicate or deleted rows until background merges run. The guide shows several ways to get Postgres-consistent results: the FINAL modifier or setting with _peerdb_is_deleted filters, row policies, views, refreshable materialized views, and tuning force-merge settings. It also covers custom ordering keys, recommending immutable columns like tenant_id and setting REPLICA IDENTITY to include them, plus JOIN optimization and denormalization.