Skip to content

Support safe concurrent dbt runs #420

@BentsiLeviav

Description

@BentsiLeviav

Support safe concurrent dbt runs

Description

There have been multiple issues related to concurrent dbt runs in ClickHouse, where one run attempts to query an intermediate/temp table that has already been dropped by a previous run.

Several PRs have attempted to fix this by appending invocation_id or UUIDs to table names or by implementing conditional drops based on specific criteria. However, each change was applied to different parts of the codebase based on individual use cases, resulting in an inconsistent and partial solution.

Proposed Solution

Introduce dedicated macros, clickhouse__make_intermediate_relation and clickhouse__make_temp_relation, that would:

  1. Include a prefix parameter with a default value using invocation_id, ensuring unique table names for each dbt run.
  2. Introduce a feature flag (disabled by default) that allows users to opt into this behavior. When enabled, invocation_id will be automatically appended to intermediate and temp table names.
  3. Standardize the handling of temp and intermediate relations across the dbt-clickhouse adapter, avoiding ad-hoc solutions in different parts of the code.

✅ Benefits

  • Prevents table name collisions in parallel dbt runs.
  • Ensures a consistent approach to naming intermediate and temp tables.
  • Provides an opt-in feature flag for backward compatibility.
  • Reduces the need for future patches addressing the same issue in different ways.

❌ Potential Downsides

  • Increased risk of hanging tables:
    • Currently, if a temp/intermediate table is not dropped, it is often cleaned up in the next run. With this change, the risk of orphaned tables may increase.
    • Potential solution: Implement a cleanup mechanism that drops stale intermediate/temp tables based on their creation time or last usage.

This is not a heavy lift, but I Would love to get the community's opinion on it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions