You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: skip no-op deletes in delete+insert incremental strategy - #788
Replaces #370 with a narrower scope: skip lightweight deletes that would not delete anything, and leave the delete itself unchanged.
Thanks to @Magicbeanbuyer for the original idea and PR, credited as co-author.
Context
delete+insert runs DELETE FROM ... WHERE (unique_key) IN (SELECT unique_key FROM <new data>). By default, ClickHouse executes this as a mutation over every part of the table. Parts without matches are not rewritten, but each part is still checked, and a mutation cannot touch a part while it is being merged. Since 26.9, mutations on ReplicatedMergeTree skip partitions the condition rules out (ClickHouse#110968), but that only helps when the partition key can be derived from the unique key.
The adapter sets lightweight_deletes_sync=3 (dbclient.py), so the run waits for the mutation, and therefore for any ongoing merges, even when no row matches.
Change
Before the delete, check whether any existing row would be deleted, and skip the delete if not:
select1from<existing_relation>where (<unique_key>) [global] in (select<unique_key>from<new data>) <incremental_predicates>limit1
the check uses the same predicates as the delete, including the microbatch time window.
distributed models need global in; plain in fails on multi-shard clusters with DISTRIBUTED_IN_JOIN_SUBQUERY_DENIED.
a null incremental_predicates config is now treated as an empty list.
Cost and why there's no flag
This adds one query per run.
No match: the query replaces a mutation, and the mutation's wait on merges, with one read of roughly what the mutation would have checked part by part.
Matches: limit 1 stops at the first match, so the overhead is small compared to the delete that follows.
This also addresses the concern in this review. models whose increments always overlap pay the least, so I don't think a flag is worth the extra configuration.
Tests
New integration tests for regular and distributed models, checked against system.mutations: no delete when no keys match, one delete once they do. Both fail against main. Passing on a single node, test_replica and test_shard.
Checklist
Delete items not relevant to your PR:
Unit and integration tests covering the common scenarios were added
A changelog entry was added following Updating the Changelog (user-facing, concise, PR link)
For significant changes, documentation in docs was updated with further explanations or tutorials
Note
Medium Risk
Changes core incremental materialization behavior for delete+insert/microbatch runs (extra pre-delete query, conditional delete), though the delete SQL itself is unchanged when matches exist.
Overview delete+insert now runs a cheap LIMIT 1 existence check (same unique_key, incremental predicates, and global in on distributed models) before issuing a lightweight delete; when nothing would be removed, it logs and skips the mutation while still inserting new rows.
Incremental macros also treat a null predicates / incremental_predicates config as an empty list, and build predicate SQL one clause per line so a trailing -- on one predicate cannot comment out the next.
Docs and the changelog note the skip behavior; integration tests cover no-match vs match paths (local and distributed, via system.mutations) and predicates that include inline comments.
Reviewed by Cursor Bugbot for commit f596da9. Bugbot is set up for automated code reviews on this repo. Configure here.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Joining predicates on one line lets SQL comments swallow subsequent filters, potentially causing DELETE to remove rows outside the intended scope, including microbatch time windows.
Optimizes dbt-clickhouse’s delete+insert incremental strategy by checking for matching rows before issuing a lightweight delete, avoiding unnecessary mutations.
Changes:
Adds an existence check using incremental predicates and GLOBAL IN for distributed models.
Treats null predicate configurations as empty lists.
Adds local and distributed mutation-count tests, documentation, and a changelog entry.
The reason will be displayed to describe this comment to others. Learn more.
Distributed skip check can miss rows
High Severity
The no-op probe reads existing_relation through the Distributed engine (one replica per shard), while the delete still runs ON CLUSTER against the local tables. A lagging replica can make the probe see no match even though local rows would have been deleted. The insert then proceeds and duplicates unique keys. insert_quorum is off unless cluster_mode or database_engine: Replicated is set, so this is the default distributed cluster shape.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The new probe can fail on multi-shard models under the default distributed_product_mode='deny' when incremental predicates contain distributed subqueries.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Use global mode for distributed subqueries in the probe
On a multi-shard model with the default distributed_product_mode = 'deny', an incremental predicate such as id IN (SELECT id FROM allowed_keys_distributed) makes the new probe fail with DISTRIBUTED_IN_JOIN_SUBQUERY_DENIED. The DELETE evaluates this predicate against the local table, but the probe reads the distributed table and only makes the generated key condition GLOBAL IN. Handle distributed subqueries in user predicates for the probe too, for example with a probe-specific distributed_product_mode = 'global' setting, while leaving the DELETE unchanged.
canbekley
changed the title
Skip no-op deletes in delete+insert incremental strategy
feat: skip no-op deletes in delete+insert incremental strategy
Nov 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces #370 with a narrower scope: skip lightweight deletes that would not delete anything, and leave the delete itself unchanged.
Thanks to @Magicbeanbuyer for the original idea and PR, credited as co-author.
Context
delete+insertrunsDELETE FROM ... WHERE (unique_key) IN (SELECT unique_key FROM <new data>). By default, ClickHouse executes this as a mutation over every part of the table. Parts without matches are not rewritten, but each part is still checked, and a mutation cannot touch a part while it is being merged. Since 26.9, mutations onReplicatedMergeTreeskip partitions the condition rules out (ClickHouse#110968), but that only helps when the partition key can be derived from the unique key.The adapter sets
lightweight_deletes_sync=3(dbclient.py), so the run waits for the mutation, and therefore for any ongoing merges, even when no row matches.Change
Before the delete, check whether any existing row would be deleted, and skip the delete if not:
DISTRIBUTED_IN_JOIN_SUBQUERY_DENIED.Cost and why there's no flag
This adds one query per run.
This also addresses the concern in this review. models whose increments always overlap pay the least, so I don't think a flag is worth the extra configuration.
Tests
New integration tests for regular and distributed models, checked against system.mutations: no delete when no keys match, one delete once they do. Both fail against
main. Passing on a single node, test_replica and test_shard.Checklist
Delete items not relevant to your PR:
docswas updated with further explanations or tutorialsNote
Medium Risk
Changes core incremental materialization behavior for delete+insert/microbatch runs (extra pre-delete query, conditional delete), though the delete SQL itself is unchanged when matches exist.
Overview
delete+insert now runs a cheap
LIMIT 1existence check (sameunique_key, incremental predicates, andglobal inon distributed models) before issuing a lightweight delete; when nothing would be removed, it logs and skips the mutation while still inserting new rows.Incremental macros also treat a null
predicates/incremental_predicatesconfig as an empty list, and build predicate SQL one clause per line so a trailing--on one predicate cannot comment out the next.Docs and the changelog note the skip behavior; integration tests cover no-match vs match paths (local and distributed, via
system.mutations) and predicates that include inline comments.Reviewed by Cursor Bugbot for commit f596da9. Bugbot is set up for automated code reviews on this repo. Configure here.