Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. pre-hooks and post-hooks

dbt · Macros, Packages & Advanced

pre-hooks and post-hooks

Mediumdbt-34
hookspre-hookpost-hookgrants

Question

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

Solution

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

Example

{{ 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
pre-hook → build model SQL → post-hook

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

PreviousNext