Repository navigation
fix: drop incremental staging tables when a model run fails - #770
Open
ardalan-jalal wants to merge 3 commits into
Open
ardalan-jalal wants to merge 3 commits into
ardalan-jalal wants to merge 3 commits into
Conversation
The delete+insert, microbatch and insert_overwrite strategies create a <model>__dbt_new_data_<invocation_id> staging table and drop it only after their last statement succeeds. When a later statement failed, the table was left behind, and because the next run uses a new invocation id, it was never dropped. Every failed run added another orphaned table. The macros now register the staging relation with the adapter right before creating it. dbt-core calls post_model_hook in a finally block after every model, on the same thread and connection, and the adapter drops any registered relation the materialization did not drop itself. A successful run drops the table as before, so no extra SQL is sent. A failed cleanup drop is logged as a warning and never replaces the original error. Registrations are kept per thread, so concurrent models do not affect each other. Staging table names, generated SQL and ON CLUSTER behaviour are unchanged. Cleanup cannot run if the process is killed (SIGKILL, OOM, pod eviction), and it does not roll back a delete+insert whose DELETE already succeeded. Tests: two integration tests make delete+insert and insert_overwrite fail after the staging table exists and check that it is gone; both fail without this change. Unit tests cover the hook behaviour, including that a failed drop does not raise. The incremental suite passes over HTTP and native, and on the test_replica and test_shard clusters, on ClickHouse 25.6.5.41 and latest. Fixes ClickHouse#723 Related ClickHouse#642
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit ceaf55f. Configure here.
… them create_distributed_local_table drops the shard relation before creating it, which unregistered the staging table too early, so a later failure in distributed_incremental still left it behind. Unregister explicitly after the macro's own final drop instead of on any drop_relation call.
This was referenced Oct 30, 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

The delete+insert, microbatch and insert_overwrite strategies create a _dbt_new_data<invocation_id> staging table and drop it only after their last statement succeeds. When a later statement failed, the table was left behind, and because the next run uses a new invocation id, it was never dropped. Every failed run added another orphaned table.
The macros now register the staging relation with the adapter right before creating it. dbt-core calls post_model_hook in a finally block after every model, on the same thread and connection, and the adapter drops any registered relation the materialization did not drop itself. A successful run drops the table as before, so no extra SQL is sent. A failed cleanup drop is logged as a warning and never replaces the original error. Registrations are kept per thread, so concurrent models do not affect each other. Staging table names, generated SQL and ON CLUSTER behaviour are unchanged.
Cleanup cannot run if the process is killed (SIGKILL, OOM, pod eviction), and it does not roll back a delete+insert whose DELETE already succeeded.
Tests: two integration tests make delete+insert and insert_overwrite fail after the staging table exists and check that it is gone; both fail without this change. Unit tests cover the hook behaviour, including that a failed drop does not raise. The incremental suite passes over HTTP and native, and on the test_replica and test_shard clusters, on ClickHouse 25.6.5.41 and latest.
Fixes #723
Related #642
Summary
Checklist
Delete items not relevant to your PR:
docswas updated with further explanations or tutorialsNote
Medium Risk
Changes incremental materialization cleanup on failure paths; incorrect hook behavior could drop the wrong relation or skip cleanup, but scope is limited to staging tables and normal success paths are unchanged.
Overview
Incremental runs that fail after creating a
__dbt_new_data_*staging table no longer leave orphaned tables behind. The ClickHouse adapter tracks those relations per thread via newpre_model_hook/post_model_hooklogic andregister_staging_relation/unregister_staging_relationAPIs; anything still registered after the model finishes is dropped inpost_model_hook, with failed drops logged as warnings so the original model error remains visible.The delete+insert and insert_overwrite incremental macros register staging tables at creation and unregister them when the materialization drops them successfully, so happy paths behave as before.
Tests add integration coverage for failed delete+insert and insert_overwrite (including distributed delete+insert on cluster), plus unit tests for hook and registration behavior.
Reviewed by Cursor Bugbot for commit 5bb10ac. Bugbot is set up for automated code reviews on this repo. Configure here.