dbt · Macros, Packages & Advanced

pre-hooks and post-hooks

Mediumhookspre-hookpost-hookgrants

Interview question

What are pre-hooks and post-hooks in dbt?

How to answer

The short answer

Hooks are SQL statements dbt runs before (pre-hook) or after (post-hook) building a model (or at project/operation scope).

Example

SQL
{{ config(
    pre_hook="alter session set query_tag = 'dbt_fct_orders'",
    post_hook=[
      "grant select on {{ this }} to role reporter",
      "analyze table {{ this }}"
    ]
) }}

select * from {{ ref('stg_orders') }}

Or in dbt_project.yml for a folder of models.

Common uses

  • Grants / row-access policies after create
  • ALTER SESSION / query tags for warehouse monitoring
  • Vacuum/analyze/optimize statements (adapter-specific)
  • Temporary setup needed for a model build

Cautions

  • Hooks are easy to overuse; prefer warehouse roles/IaC for permanent permissions when possible
  • Hook failures fail the model run
  • Keep them idempotent and environment-aware

Interview tip: "Hooks are for operational SQL around a model. Core business transforms stay in the main SELECT."