Skip to content

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

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.

1 participant