Skip to content

Commit

Permalink
Update pre-hook-post-hook.md (#4393)
Browse files Browse the repository at this point in the history
clarify transactions not supported per [slack
thread](https://dbt-labs.slack.com/archives/C05FWBP9X1U/p1695951000263009)
  • Loading branch information
mirnawong1 authored Nov 8, 2023
2 parents cfca7c4 + 5e0f763 commit 050ae3e
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions website/docs/reference/resource-configs/pre-hook-post-hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,17 @@ If multiple instances of any hooks are defined, dbt will run each hook using the


### Transaction behavior
If you're using an adapter that makes use of transactions (namely Postgres or Redshift), it's worth noting that by default hooks are executed inside of the same transaction as your model being created.
If you're using an adapter that uses transactions (namely Postgres or Redshift), it's worth noting that by default hooks are executed inside of the same transaction as your model being created.

There may be occasions where you need to run these hooks _outside_ of a transaction, for example:
* You want to run a `VACUUM` in a `post-hook`, however this cannot be executed within a transaction ([Redshift docs](https://docs.aws.amazon.com/redshift/latest/dg/r_VACUUM_command.html#r_VACUUM_usage_notes))
* You want to insert a record into an audit <Term id="table" /> at the start of a run, and do not want that statement rolled back if the model creation fails.
* You want to run a `VACUUM` in a `post-hook`, however, this cannot be executed within a transaction ([Redshift docs](https://docs.aws.amazon.com/redshift/latest/dg/r_VACUUM_command.html#r_VACUUM_usage_notes))
* You want to insert a record into an audit <Term id="table" /> at the start of a run and do not want that statement rolled back if the model creation fails.

To achieve this, you can use one of the following syntaxes. (Note: You should NOT use this syntax if using a database where dbt does not use transactions by default, including Snowflake, BigQuery, and Spark/Databricks.)
To achieve this behavior, you can use one of the following syntaxes:
- Important note: Do not use this syntax if you are using a database where dbt does not support transactions. This includes databases like Snowflake, BigQuery, and Spark or Databricks.

<Tabs>
<TabItem value="beforebegin" label="Use before_begin and after_commit">

#### Config block: use the `before_begin` and `after_commit` helper macros

Expand All @@ -200,6 +204,9 @@ select ...
```

</File>
</TabItem>

<TabItem value="dictionary" label="Use a dictionary">

#### Config block: use a dictionary
<File name='models/<modelname>.sql'>
Expand All @@ -224,6 +231,10 @@ select ...

</File>

</TabItem>

<TabItem value="dbt_project.yml" label="Use dbt_project.yml">

#### `dbt_project.yml`: Use a dictionary

<File name='dbt_project.yml'>
Expand All @@ -242,3 +253,5 @@ models:
```

</File>
</TabItem>
</Tabs>

0 comments on commit 050ae3e

Please sign in to comment.