Skip to content

fix: drop incremental staging tables when a model run fails - #770

Open
ardalan-jalal wants to merge 3 commits into
ClickHouse:mainfrom
ardalan-jalal:fix/723-staging-cleanup
Open

ardalan-jalal wants to merge 3 commits into
ClickHouse:mainfrom
ardalan-jalal:fix/723-staging-cleanup

Conversation

@ardalan-jalal

@ardalan-jalal ardalan-jalal commented Oct 29, 2026 •

Copy link
Copy Markdown

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:

  • 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 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 new pre_model_hook / post_model_hook logic and register_staging_relation / unregister_staging_relation APIs; anything still registered after the model finishes is dropped in post_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.

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
@CLAassistant

CLAassistant commented Oct 29, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

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.
@Flipmonster Flipmonster reopened this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

orphaned __dbt_new_data_<UUID> tables after failed/interrupted delete+insert incremental runs

3 participants