Catch dbt cost increases before merge
Compile your dbt project on your main branch and on the pull request, dry-run each changed model's compiled SQL from both, and compare the bytes.
Updated · First published
A dbt change can make a model cost more per run even when its diff looks harmless. A BigQuery dry run returns the bytes a query would process, without running it and without a charge. On-demand queries are billed by the bytes they process, so comparing the dry-run bytes of a model's compiled SQL before and after a change gives its cost change, with exceptions: on a clustered table the estimate is an upper bound, an external table can report a lower bound of 0 bytes, and a table with row-level security always reports 0 bytes.
Why does a small dbt change make a model more expensive?#
BigQuery runs the compiled SQL, not the model file, so look for changes to the compiled SQL like these:
- A macro, a
var(), config indbt_project.ymlor schema YAML, or a source definition. The change reaches the compiled SQL of every model that uses it, though none of those models'.sqlfiles changed. The pull request (PR) diff shows the macro, not the models whose compiled SQL it rewrote. - A filter on a partitioning column. Removing it, or rewriting it so BigQuery can't use it to skip partitions, can make the model read every partition of that table. See Removed partition filters.
- The filter that limits an incremental model's run to new rows. When it compares the source's partitioning column with a constant expression and a change drops or widens it, every incremental run reads more partitions of its source (which filters prune).
SELECT *, a new join (especially aCROSS JOIN) or a new table reference. DiffCost's structural analysis flags these because they usually raise cost.
How do I check a dbt change's cost by hand?#
Compile both versions and dry-run each changed model:
- On your main branch, run
dbt compile --target prod, then copy the compiled SQL outsidetarget/:cp -R target/compiled ../compiled_main. - Check out the PR branch, delete
target/compiled/, and run the same command. - Compare the two folders (for example
diff -r ../compiled_main target/compiled) to find the models whose compiled SQL changed. - Dry-run both versions of each of those models, in the BigQuery console or with
bq query --dry_run --nouse_legacy_sql, and compare the bytes.
A few things to know:
- Compile against your production target, so the compiled SQL reads production tables.
- With on-demand pricing, price one run as bytes ÷ 2^40 × your price per TiB (BigQuery's list price is $6.25 per TiB; this ignores BigQuery's 10 MB minimums, per table referenced and per query, and the free 1 TiB a month).
dbt compileisn't a dry run: macros that query the warehouse at compile time, such asrun_query, run real queries, billed to the project in your profile.
That works for one model and one PR. To check every PR, you also need to:
- find every model whose compiled SQL changed, including models changed only through a macro or a var;
- compile the commit the PR merges into as well as the PR, or keep the main branch's compiled manifest (
target/manifest.json) to compare against; - treat estimates BigQuery can't stand behind as unknown, not $0: on an external table, for example, a dry run may report a lower bound of 0 bytes.
Compare BigQuery cost controls weighs a script of your own against the other options.
How do I run this check on every pull request?#
Run it in CI. DiffCost is a GitHub Action that dry-runs and compares each changed dbt model on every PR, for dbt Core; dbt Cloud isn't required. Setup is one job in the PR workflow (see dbt projects):
- It runs
dbt compileon the PR. - It compiles the commit the PR merges into, normally the tip of your main branch: the baseline manifest.
- It runs DiffCost, which compares the two.
DiffCost counts a model as changed when its .sql file's checksum or its compiled SQL differs, so a macro, var or config change marks every model whose compiled SQL it changed. It dry-runs both versions of each changed model in your own BigQuery project and prices the difference per run. It posts one comment on the PR, with a row per changed model and the likely cause, such as a removed partition filter, SELECT * or a new join. For example, this row comes with the finding "Partition filter removed: order_date >= '2024-01-01'":
| Query / Model | Current | This PR | Scan Delta | Cost Change |
|---|---|---|---|---|
fct_daily_revenue |
$2.09 | $9.66 | +1.21 TiB | +$7.57 (↑362%) |
Your SQL goes only to BigQuery, in your project. With an API key, the DiffCost service receives the repository name, PR number, commit SHA, the PR author (only with send_author: true), the .diffcost.yml thresholds and runs per month, whether the run came from a merge queue, and per-run costs, bytes and query counts. It never receives SQL, table or model names, or file paths (Security and data).
For an incremental model, DiffCost prices the SQL that dbt compile produced. When the model's table already exists in the production target, that's usually the incremental run, not a full refresh.
The PR comment is free and needs no DiffCost account. To block a PR that makes a model too expensive, set a threshold: a limit on how much any one query's or model's per-run cost may rise, in $/run or percent (not the PR's total). Merge blocking needs a paid plan or the free trial of the Team plan (Plans and billing), an API key, a threshold, the DiffCost GitHub App, and diffcost/cost-check as a required status check with the App as its source: see Merge blocking. A PR can edit the workflow that runs DiffCost, for example to skip it, so also protect the workflow itself.
Which BigQuery limits stop an expensive dbt run after merge?#
Two BigQuery settings stop an expensive query when it runs: maximum_bytes_billed in your dbt profile, and require_partition_filter on partitioned models. Both act after the change has merged, so they complement a PR check rather than replace it.
maximum_bytes_billed in your dbt profile. It's a field of the BigQuery target in profiles.yml, in bytes, and dbt sets it on every query job it runs with that target. BigQuery estimates a query's bytes before running it, and a query over the limit fails without a charge, so the model fails to build instead of costing more:
my_dbt_project:
target: prod
outputs:
prod:
type: bigquery
method: oauth
project: my-project
dataset: analytics
location: US
threads: 4
maximum_bytes_billed: 1099511627776 # 1 TiB per query, in bytes
One value covers every model in the target, so it has to fit the largest scan any model in the target needs. On clustered tables the estimate is an upper bound, so a query can fail even though its actual bytes billed wouldn't exceed the limit.
require_partition_filter on partitioned models. With this model config set, a query on the model's table must filter its partitioning column, or it fails. Other dbt models and tests that select from the model must then filter it too, or they fail. The config checks that a usable filter exists, not how many partitions the filter keeps: a filter such as >= a date long before your oldest partition still passes, and still reads every partition.
Neither tells a reviewer what a change will cost. Compare BigQuery cost controls puts them side by side with quotas and budgets.
What can't a pull request check catch?#
A PR check compares two versions of the SQL, so it misses cost that doesn't come from a code change:
- Data growth. A source table that grows makes the same SQL scan more, with no PR to compare.
- Run frequency. A PR check prices one run; it doesn't know how often the model runs. DiffCost's monthly figure ("≈$X/mo" in the comment) assumes 30 runs a month unless you set
default_runs_per_monthin.diffcost.yml(Configuration reference). - Full refreshes. When the model's table already exists,
dbt compileusually produces the incremental run, so a change that only makes a full refresh more expensive doesn't show. - Queries outside the repository. Ad-hoc queries and scheduled queries that aren't dbt models or SQL files in the PR aren't priced.
- PRs from forks. Fork PRs get no BigQuery credentials, so
dbt compilecan't reach BigQuery. DiffCost reports in the job summary that it couldn't analyze the PR rather than "no changes", so a fork PR never looks free. They get no API key either, so DiffCost posts nodiffcost/cost-checkstatus: an admin bypasses the required check to merge one (Merge blocking).
Two DiffCost limits: it analyzes one dbt project per PR, and on Editions (slot-based) pricing it reports bytes scanned and a scan multiplier (the PR version's bytes divided by the base version's, for example 4.6x scan) instead of dollars.
Next steps#
- dbt projects: set up the PR workflow that compiles the PR and its baseline.
- BigQuery query cost calculator: what a query costs per run and per month, from its bytes.
- Removed partition filters: why a removed filter can make a query read every partition.
- Compare BigQuery cost controls: PR checks, byte limits, quotas, budgets and job history.
- Reading the PR comment: every row and finding in the report.
Something missing or wrong? Email support@diffcost.dev.