Skip to content

Feat(dbt): Add support for transaction in dbt pre and post hooks#5480

Merged
themisvaltinos merged 2 commits intomainfrom
themis/dbt_transaction
Oct 7, 2025
Merged

Feat(dbt): Add support for transaction in dbt pre and post hooks#5480
themisvaltinos merged 2 commits intomainfrom
themis/dbt_transaction

Conversation

@themisvaltinos
Copy link
Contributor

This adds support for transaction in dbt pre and post hooks. By default hooks are executed inside of the same transaction as the model being created. Hooks that need to run outside a transaction are for example when executing a VACUUM in Redshift or inserting into an audit table at the start of a run where the insert should persist even if the model fails. Running the hook outside a transaction ensures it won’t be rolled back.

These are set in a dbt project either with the before_begin and after_commit macro:

{{
  config(
    pre_hook=before_begin("SQL-statement"),
    post_hook=after_commit("SQL-statement")
  )
}}

or by setting the transaction flag in a dict:

{{
  config(
    pre_hook={
      "sql": "SQL-statement",
      "transaction": False
    },
    post_hook={
      "sql": "SQL-statement",
      "transaction": False
    }
  )
}}

By default and without setting the transaction the hooks are executed inside the transaction.

Docs: https://docs.getdbt.com/reference/resource-configs/pre-hook-post-hook#transaction-behavior

assert shared_table["hook_name"][2] == "after_commit"
assert shared_table["execution_order"][2] == 3

# the timestamps also should be monotonically increasing for teh same reason
Copy link
Collaborator

Choose a reason for hiding this comment

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

typo: teh

@themisvaltinos themisvaltinos force-pushed the themis/dbt_transaction branch from c094084 to 88121c3 Compare October 6, 2025 19:43
@themisvaltinos themisvaltinos force-pushed the themis/dbt_transaction branch from 88121c3 to 0722793 Compare October 7, 2025 06:47
@themisvaltinos themisvaltinos merged commit 5ffed10 into main Oct 7, 2025
34 of 36 checks passed
@themisvaltinos themisvaltinos deleted the themis/dbt_transaction branch October 7, 2025 07:18
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.

2 participants