Skip to content

Guides

Compare BigQuery cost controls

Fail queries over a byte limit, require a partition filter, or set an approximate daily cap with custom quotas. Budgets and job history only report; a pull request check flags costly changes first.

Updated · First published

Which BigQuery cost controls are there?#

The seven compared here act at four moments. Before a change merges: a pull request (PR) check, or your own dry-run script. When a query runs: maximum bytes billed and a required partition filter. Per day: custom quotas. After the fact: Cloud Billing budgets and INFORMATION_SCHEMA.JOBS job history, which only report. Costs here assume on-demand pricing, where BigQuery bills the bytes a query processes.

Control When it acts What it stops What it misses Where you set it
PR check, such as DiffCost (a GitHub Action) Before a change merges A change that raises one query's or model's per-run cost by more than your threshold (a limit on the increase, in $/run or percent), once it's a required check Ad-hoc queries, queries outside the repository, cost that grows with the data Your CI workflow, plus a required status check in branch protection
Your own dry-run script Before a change merges What your script checks Whatever you don't build (see below) Your CI
Maximum bytes billed When a query starts One query whose estimated bytes are over the limit: it fails without a charge A query under the limit that runs often The query job, or a dbt profile
Required partition filter When a query starts A query with no filter usable for partition elimination A filter that keeps every partition A table option, or a dbt model config
Custom quotas Per day, checked when a query starts Queries from a project or user that used up its daily bytes (on-demand only, approximate) Which change raised the bytes: it only counts billed bytes Google Cloud quotas
Cloud Billing budgets After the spend Nothing by default: it sends alerts (some services support a spend cap) It doesn't automatically cap spending Cloud Billing
INFORMATION_SCHEMA.JOBS After queries ran Nothing: it reports Queries that haven't run yet A query in each project

Which control should I use?#

Use several: each covers a moment the others miss.

  1. Before merge: a PR check prices each change while it's in review and, once it's a required check, can block the ones over your threshold.
  2. When a query runs: maximum bytes billed fails a pipeline or dbt job whose estimate is over the limit. A required partition filter fails any query on the table without a usable filter, ad-hoc queries included.
  3. Per day: a custom quota sets an approximate daily cap on the bytes a project or user can query.
  4. After the fact: budgets alert you to the spend; job history shows which jobs used the bytes.

How do I stop an expensive query when it runs?#

Set maximum bytes billed on the query job, or require a partition filter on the table. Both act in production, after the change that made the query expensive has merged: a query they stop is a failed job, such as a scheduled model that doesn't build.

Maximum bytes billed#

BigQuery estimates the bytes a query will read before it runs; if the estimate is over the limit, "the query fails without incurring a charge" (Google). Set it with bq query --maximum_bytes_billed or the API's maximumBytesBilled. In dbt, set maximum_bytes_billed (in bytes) in the BigQuery profile; it applies to every query job dbt runs with that profile:

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
  • The limit applies to one query at a time: a query that grows from 10 GiB to 900 GiB under a 1 TiB limit still runs, every time it's scheduled.
  • On a clustered table the estimate is an upper bound, so "a query on a clustered table can fail, even though the actual bytes billed wouldn't exceed the maximum bytes billed setting" (Google).

Required partition filter#

With require_partition_filter set on a partitioned table, a query fails unless it filters the partitioning column: "There must be at least one predicate that only references a partitioning column" (Google). This also applies to queries on views and materialized views that read the table. In dbt, require_partition_filter: true in a model's config makes every query on the model, other models' and tests' included, filter its partitioning column or fail.

-- analytics.orders is partitioned by order_date.
ALTER TABLE analytics.orders SET OPTIONS (require_partition_filter = true);

-- Fails: no filter on the partitioning column.
SELECT order_date, SUM(revenue) AS revenue FROM analytics.orders GROUP BY order_date;

-- Fails: a partition filter combined with OR isn't sufficient.
SELECT order_date, SUM(revenue) AS revenue FROM analytics.orders
WHERE order_date >= '2024-01-01' OR region = 'EU' GROUP BY order_date;

-- Runs, and reads every partition if the table has none before 2000.
SELECT order_date, SUM(revenue) AS revenue FROM analytics.orders
WHERE order_date >= '2000-01-01' GROUP BY order_date;

The option checks that a usable filter exists, not how many partitions the filter keeps. Removed partition filters explains which filters prune partitions.

Can I cap BigQuery usage per day?#

Yes, approximately, with custom quotas (on-demand pricing only): QueryUsagePerDay caps a project and QueryUsagePerUserPerDay caps each user. Usage is "an accumulation of billed bytes". Once a quota is reached, queries fail with quotaExceeded until it resets at midnight Pacific Time (Google).

  • Google calls the quotas approximate: they aren't designed to strictly limit bytes processed.
  • At the cap, queries of that project or user fail, the important ones too. The quota counts billed bytes; job history shows which jobs used them.

What do budgets and job history show?#

What was spent and which jobs used the bytes, after the fact. A budget doesn't automatically cap spending, and job history only reports.

  • Cloud Billing budgets send alerts. A budget "doesn't automatically cap Google Cloud … usage or spending" (Google). Some services support a spend cap; see Google's page.
  • INFORMATION_SCHEMA.JOBS has "near real-time metadata about all BigQuery jobs in the current project", with history for the past 180 days (Google). On on-demand pricing, its total_bytes_billed column shows the bytes each job was billed for, so you can find the jobs that read the most. DiffCost's Effective cost per TiB on Editions recipe uses the same history for something else: what your Editions slots cost per TiB.

Can I build my own dry-run check in CI?#

Yes. A dry run returns the bytes a query would process, and "you are not charged for performing a dry run" (Google). For a few plain SQL files, a script works: dry-run each changed file and fail above a byte limit. To compare a change with what's merged and gate on it, the script also has to handle:

  • Both versions: dry-run the base commit's version and the PR's, to price the change rather than the new total.
  • dbt: compile both branches; a macro, var or config change alters the compiled SQL of models whose files didn't change.
  • Estimates BigQuery can't stand behind: external tables can report "a lower bound of 0 bytes", and tables with row-level security "always return 0 bytes" (Google): read as cheap, they slip through. On clustered tables the estimate is an upper bound.
  • Reporting and blocking: a PR comment updated on every push, and a required commit status. Any workflow with statuses: write can post a status of the same name, so post it from outside the PR's workflows and pin branch protection to that source.
  • The workflow itself: a PR can edit the workflow that runs the check.

What does DiffCost add as a pull request check?#

DiffCost is a GitHub Action that catches a cost increase, with its likely cause, while the change is in review. It:

  • dry-runs the base and PR versions of each changed query or dbt model in your BigQuery project and posts the per-run cost change as a PR comment, free, with no DiffCost account (Quickstart);
  • compares the compiled SQL of the PR's dbt manifest with one compiled from the commit the PR merges into, so it catches changes from macros, vars and config (dbt projects);
  • flags the likely cause, such as a removed partition filter, SELECT * added or a new JOIN (Structural analysis);
  • marks clustered-table estimates ≤ (at most), and treats estimates BigQuery can't stand behind, such as on external tables or tables with row-level security, as unknown cost, never $0 (Accuracy);
  • with a threshold set, can post a diffcost/cost-check commit status that fails when the PR breaks your 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) or a changed query's cost is unknown.

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.

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).

What doesn't DiffCost cover?#

DiffCost sees only the changes in a PR, so keep the run-time limits too:

  • Queries outside the PR: ad-hoc queries, queries from other tools, and cost that grows with the data after merge.
  • BigQuery and GitHub Actions only: DiffCost runs on a Linux runner with Docker.
  • One DiffCost run per PR: one dbt project, or one set of plain SQL files.
  • PRs from forks: no credentials, so plain SQL gets a structural-only report in the job summary and a dbt project reports that DiffCost couldn't analyze the PR (Workflow rules); no API key either, so no diffcost/cost-check status: an admin bypasses the required check to merge one (Merge blocking).
  • Editions (slot-based) pricing: the comment shows bytes scanned and a scan multiplier instead of dollars (How costs are calculated).
  • The check trusts the workflow: a PR can edit .github/workflows/*.yml to skip DiffCost. Protect it with a CODEOWNERS entry and Require review from Code Owners, or run DiffCost as a required workflow in an organization ruleset (Protect the workflow itself).

Next steps#

Something missing or wrong? Email support@diffcost.dev.