dbt incremental models on ClickHouse: strategies, tests
append, delete+insert, insert_overwrite and microbatch in dbt-clickhouse: what each one runs, where late-arriving rows slip through, and a test that proves you match a full rebuild.
For most dbt incremental models on ClickHouse, pick delete+insert when rows change after they land, append when they never do, and insert_overwrite or microbatch when the model is an aggregate you can recompute one day at a time. The reason is that ClickHouse has no MERGE statement, so the dbt-clickhouse adapter fakes an upsert in one of three ways: it rebuilds the whole table and swaps it in (the default, called legacy), it runs a lightweight DELETE for the changed keys and inserts the new rows (delete+insert), or it replaces whole partitions (insert_overwrite). Each has a different cost and a different way of going wrong. We ran every example below against ClickHouse 26.10 with dbt-core 1.12.6 and dbt-clickhouse 1.10.3, and checked the SQL the adapter generated in logs/dbt.log. The output tables show what each strategy really does with an updated row and a row that arrives late, and the last section is a dbt test that fails whenever the incremental table no longer matches a full rebuild.
How does a dbt incremental model work on ClickHouse?
The dbt side is the same as on any warehouse. You set materialized='incremental' and guard the “only new rows” filter with is_incremental(). According to the dbt docs, is_incremental() is true only when the target table already exists, the model is materialized as incremental, and the run was not started with --full-refresh. The first run, and any full refresh, builds the whole table.
-- models/orders_inc.sql
{{ config(
materialized='incremental',
incremental_strategy='delete+insert',
unique_key='order_id',
order_by='order_id'
) }}
select order_id, customer_id, status, amount, order_date, updated_at
from {{ source('raw', 'orders') }}
{% if is_incremental() %}
where updated_at > (select max(updated_at) from {{ this }})
{% endif %}
The ClickHouse side is what changes. A ClickHouse table is a set of immutable parts, and the engine is built for large inserts, not row-level edits. So the adapter has to translate “make the target look like target plus these changes” into operations ClickHouse is good at. The incremental_strategy setting picks the translation, and order_by, partition_by and engine decide the physical table (the adapter creates a plain MergeTree if you do not set an engine). If the order_by choice is new to you, our guide to the ClickHouse ORDER BY and primary key covers it.
The five dbt-clickhouse incremental strategies
The dbt-clickhouse materializations docs list these strategies. The “Runs” column is what we saw in the adapter’s SQL log.
| Strategy | Runs | Handles updated rows | Safe if the run fails halfway | Main cost |
|---|---|---|---|---|
| legacy (default) | Builds a new table with old unchanged rows plus new rows, then EXCHANGE TABLES | Yes, needs unique_key | Yes, the original table survives a failure | Copies the whole table every run |
append | Plain INSERT into the target | No, updates become duplicates | Mostly, it is one insert | None beyond the insert |
delete+insert | Lightweight DELETE ... WHERE key IN (new keys), then INSERT | Yes, needs unique_key | No, it edits the target directly | A delete mutation every run |
insert_overwrite (experimental) | Builds a staging table, then ALTER TABLE ... REPLACE PARTITION per partition | Yes, at partition level | Yes, the target is untouched until the insert succeeds | Recomputes whole partitions |
microbatch | delete+insert once per time batch | Yes, per batch | Per batch | Needs dbt-core 1.9+ and an event_time column |
The old inserts_only setting is deprecated in favour of append. A profile setting, use_lw_deletes: true, makes delete+insert the default instead of legacy.
append: fast, and wrong as soon as a row changes
append inserts whatever the model returns and does nothing else. That is ideal for immutable events (page views, clicks, log lines) and wrong for anything with a status that changes. We loaded three orders, ran the model, then changed order 2 from placed to refunded and added two more orders. After the second run, the append model held both versions of order 2:
order_id | status | amount | updated_at
1 | placed | 50 | 2026-10-20 09:00:00
2 | refunded | 80 | 2026-10-25 08:00:00
2 | placed | 80 | 2026-10-21 10:00:00
3 | placed | 20 | 2026-10-22 11:00:00
4 | placed | 40 | 2026-10-25 12:00:00
5 | placed | 99 | 2026-10-25 13:00:00
sum(amount) over this table counts order 2 twice. You can keep append for changing data by landing into a ReplacingMergeTree and deduplicating at read time, but then every query needs FINAL or an argMax pattern, which our post on ReplacingMergeTree deduplication covers in detail. If the model has a real primary key and rows change, a strategy that deletes is usually simpler.
delete+insert: the practical upsert
With the same data, the delete+insert and legacy models both ended up with one row per order and order 2 as refunded. The difference is how they got there. delete+insert loaded the new rows into a temporary table and ran:
delete from analytics.orders_inc
where (order_id) in (select order_id from analytics.orders_inc__dbt_new_data_<uuid>)
followed by an insert of those rows. That is a lightweight DELETE: ClickHouse marks the rows as deleted in a hidden _row_exists column, queries skip them right away, and the rows are physically removed in a later merge. Only the parts that contain changed keys are rewritten, instead of a copy of the whole table, which is why it beats legacy on large tables.
Know the trade-offs before you switch:
- It is not atomic. The adapter docs say plainly that a failure between the delete and the insert can leave the table in an invalid state. Rerun the model after any failure, and keep a full refresh in your back pocket.
- It needs
allow_nondeterministic_mutations. The adapter turns it on when it can. If your dbt user is not allowed to change settings, a model that relies on the default strategy falls back to legacy, and a model that setsdelete+insertexplicitly fails at run time. - Projections block it by default. ClickHouse rejects lightweight deletes on tables with projections unless you change the projection mode setting, so check this before you add a projection to an incremental model. Our comparison of materialized views and projections explains what projections cost you.
- Many small deletes add up. Each run queues a mutation, and mutations run one after another. Hourly runs on a large table are fine; a run every minute deserves a load test first.
incremental_predicates (honoured only by delete+insert) lets you narrow the delete, for example to the last 7 days of a partition key, so ClickHouse touches fewer parts.
Legacy: the safe default that copies everything
The default strategy builds a fresh table containing every unchanged row plus the new and changed rows, then swaps it in with EXCHANGE TABLES. It is correct and safe to interrupt, because the original table is untouched until the swap. It is also a full copy of the table on every run, which defeats the purpose of an incremental model once the table is big. Treat it as the starting point for small models and move to delete+insert or insert_overwrite when run times grow.
insert_overwrite: replace whole partitions
insert_overwrite is the natural fit for aggregates like daily revenue, where “update the last few days” means “recompute those days”. It requires partition_by, builds a staging table from the model’s query, and replaces each partition it found there. The SQL the adapter ran for us:
alter table analytics.daily_revenue
replace partition id '20261025'
from analytics.daily_revenue__dbt_new_data_<uuid>
settings allow_replace_partition_from_empty_source = 1
Two things follow from that. Only partitions present in the new data are replaced, so a partition the query does not return is left as it was. And the partition key is your unit of recomputation, so partition by day or month, not by something fine-grained that would create thousands of parts (our post on the too many parts error shows why). The adapter still marks this strategy experimental and notes it is not fully functional with distributed materializations.
Late-arriving data: the bug most incremental models share
Here is the model most teams write first:
-- models/daily_revenue.sql
{{ config(
materialized='incremental',
incremental_strategy='insert_overwrite',
partition_by='order_date',
order_by='order_date'
) }}
select order_date, count() as orders, sum(amount) as revenue
from {{ source('raw', 'orders') }}
{% if is_incremental() %}
where order_date >= today() - 3
{% endif %}
group by order_date
It recomputes the last three days. Then order 5 arrives on 25 October with an order_date of 21 October: a delayed sync, a backfilled CSV, a mobile client that was offline. The filter never looks at 21 October again, so the row is never counted. In our run the incremental table reported 1 order and 80 in revenue for that day, while a full rebuild reported 2 orders and 179. Nothing failed, and nothing will warn you.
The filter is on the wrong column. Event time decides which partition a row belongs to; load time (updated_at, or the ingestion timestamp your pipeline writes) decides whether the row is new. Select the partitions that received new rows, then recompute them completely:
{% if is_incremental() %}
where order_date in (
select distinct order_date
from {{ source('raw', 'orders') }}
where updated_at > now() - interval 3 day
)
{% endif %}
With this version, a row with order_date 20 October that landed today caused the 20 October partition to be recomputed, and the table matched a full rebuild. The three-day window now only has to cover the gap between dbt runs plus some slack, not how late your data can be. Because insert_overwrite replaces whole partitions, recomputing a partition twice is harmless.
The same idea works with delete+insert on row-level models: filter on load time, never on event time.
microbatch: built-in lookback for time-series models
dbt-core 1.9 added the microbatch strategy, and dbt-clickhouse implements it on top of delete+insert. You declare the event-time column, a batch size and a lookback, and dbt runs one batch per period, filtering every upstream ref or source that also declares an event_time:
sources: # models/sources.yml
- name: raw
schema: raw
tables:
- name: orders
config:
event_time: ordered_at
-- models/daily_revenue_mb.sql
{{ config(
materialized='incremental',
incremental_strategy='microbatch',
event_time='order_day',
begin='2026-10-01',
batch_size='day',
lookback=3,
unique_key='order_day',
order_by='order_day'
) }}
select toStartOfDay(ordered_at) as order_day, count() as orders, sum(amount) as revenue
from {{ source('raw', 'orders') }}
group by order_day
On 26 October this ran four batches, 23 to 26 October. A row for 24 October that arrived late was picked up. A row for 12 October that arrived on the same day was not, because it fell outside the lookback, and we had to backfill it explicitly:
dbt run -s daily_revenue_mb --event-time-start 2026-10-12 --event-time-end 2026-10-13
Two ClickHouse details cost us time. First, with the versions above, an event_time on a Date column failed, because dbt filters with a timestamp string such as '2026-10-23 00:00:00' and ClickHouse refused to compare it with a Date. Use a DateTime column. Second, dbt filters the source with select * from ..., and ClickHouse leaves MATERIALIZED columns out of select *, so an event_time defined as a materialized column was not found. A regular column with a DEFAULT works.
Microbatch makes the lookback explicit, which is its main advantage. It does not remove the problem: anything later than the lookback still needs a backfill, and the next test is how you find out.
How do you test that an incremental model matches a full rebuild?
Every incremental model is a claim that “previous result plus increments” equals “recompute from scratch”. Test that claim directly. A dbt singular test returns failing rows, so compare the incremental table with the same query run in full over a recent window, in both directions:
-- tests/daily_revenue_matches_full_rebuild.sql
-- Any returned row is a failure.
with
incremental as (
select order_date, orders, revenue
from {{ ref('daily_revenue') }}
where order_date >= today() - 30
),
full_rebuild as (
select order_date, count() as orders, sum(amount) as revenue
from {{ source('raw', 'orders') }}
where order_date >= today() - 30
group by order_date
)
select 'only_in_incremental' as side, * from (
select * from incremental except select * from full_rebuild
)
union all
select 'only_in_full_rebuild' as side, * from (
select * from full_rebuild except select * from incremental
)
Against the naive daily revenue model, this failed with 6 rows: four for two days whose totals differed, and two for days that existed only in the full rebuild. After dbt run --full-refresh -s daily_revenue it passed. A few rules make it reliable:
- Compare both directions.
EXCEPTone way only misses rows that exist on one side and not the other. - Bound the window. Thirty days of a fact table is cheap to recompute; the whole history may not be. Run a full-history comparison weekly or before releases.
- Round floating-point sums.
sum()overFloat64can differ in the last digits between runs. UseDecimalfor money, or compareround(revenue, 2). - Mind data that lands mid-test. If ingestion runs while the test does, add
and updated_at < <run start>to both sides, or run the test right after the model. - Schedule full refreshes anyway. A periodic
--full-refreshputs the table back in a known state, and the test tells you how much it had drifted. A test that never fails after full refreshes is evidence your incremental logic is right.
This sits next to the uniqueness and freshness checks in our list of data-quality tests every warehouse needs. For an incremental model with a unique_key, add a unique test on that key as well: it is the cheapest way to catch an append model that should have been delete+insert.
Which incremental strategy should you choose?
| Your model | Strategy | Watch out for |
|---|---|---|
| Immutable events, never updated | append | Retries that insert the same batch twice |
| Rows with a key that change (orders, tickets, accounts) | delete+insert | Not atomic; mutation queue on very frequent runs |
| Daily or monthly aggregates | insert_overwrite | Experimental; filter by load time, not event time |
| Time series with known lateness | microbatch | Lookback is a guess; use DateTime for event_time |
| Small table, or you are unsure | legacy (default) | Full copy every run |
Whatever you pick, write the full-rebuild test on day one. Changing the strategy later is easy when you can prove the new one produces the same table.
Where Goni fits
If you would rather not maintain a dbt project next to a separate BI tool, Goni is one option. Full disclosure: this is our product. It is a full data platform built around a ClickHouse warehouse, with ELT connectors, a dbt-like YAML semantic layer that compiles to ClickHouse SQL with column-level tests and source freshness checks, and BI on top: a SQL IDE, dashboards, alerts, and AI Q&A grounded in the defined metrics. The honest drawbacks: ClickHouse is the only warehouse it queries, it is not open source, and it is younger than dbt, so a team with hundreds of mature dbt models should weigh the migration carefully. Our post on dbt alternatives for ClickHouse compares the wider field. You can try it with the 14-day free trial.
FAQ
Which incremental strategies does dbt-clickhouse support?
The adapter supports the default legacy strategy, append, delete+insert, insert_overwrite and microbatch. Legacy rebuilds and swaps the table, append only inserts, delete+insert uses lightweight deletes, insert_overwrite replaces partitions and is marked experimental, and microbatch runs delete+insert per time batch and needs dbt-core 1.9 or later.
Does ClickHouse support MERGE for dbt incremental models?
No. ClickHouse has no MERGE statement, so the dbt-clickhouse adapter emulates upserts. It either rebuilds the table and swaps it with EXCHANGE TABLES, deletes changed keys with a lightweight DELETE before inserting, or replaces whole partitions with REPLACE PARTITION. Pick the strategy whose cost matches your table size and update pattern.
Why does my dbt incremental model on ClickHouse have duplicate rows?
Usually because it uses append, or the inserts_only setting, on data where rows change. append inserts the new version of a row without removing the old one. Switch to delete+insert with a unique_key, or land into a ReplacingMergeTree and deduplicate at read time with FINAL or argMax.
How do I handle late-arriving data in an incremental model?
Filter new rows by load time, such as updated_at or an ingestion timestamp, not by event time. Then recompute every partition or key those rows touch. With microbatch, set a lookback that covers normal lateness and backfill older gaps with --event-time-start and --event-time-end.
How often should I full-refresh an incremental model?
There is no universal interval. Run a test that compares the incremental table with a full rebuild over a recent window on every run, and a full-history comparison weekly. If the tests never fail, full refreshes can be rare; if they do fail, run dbt run --full-refresh on the model and fix the filter.