DEV Community

Cover image for Set on_schema_change on every dbt incremental model. Here's what the default cost us.
Vijay Kumar
Vijay Kumar

Posted on Originally published at datafanalytics.com AI-assisted

Set on_schema_change on every dbt incremental model. Here's what the default cost us.

As part of our SAP migration I've been auditing the dbt incremental models I inherited, and one setting keeps coming up: on_schema_change. Most projects never set it. On one of the inherited models, the default had been dropping 18 new columns for six weeks, without a single error, before I found it. In this article I'll show you what the default does, how I found the missing columns, how I fixed it, and the rule I'd now apply to every incremental model. (It's also the post I promised at the end of my last two.)

The short version

  • What happened: 18 new columns were added to an inherited incremental model. The SQL produced them, but on_schema_change was at its default, ignore, so they never reached the production table: 253 columns in the model, 235 in the table, for six weeks.
  • What I changed: set on_schema_change='append_new_columns' on the model and reloaded the affected history, because new columns arrive as NULL for old rows.
  • What I'd make the default: fail on every incremental model, plus a dbt test that the columns a report depends on actually exist.

Some background

I'm an analytics engineer at a leading North American housewares company, and we're moving our SAP reporting onto Snowflake and dbt. This is the same inherited finance model from my first post: an incremental model built from SAP's general-ledger line items, about 1.5 million rows per fiscal period.

A new finance commentary report needed more from it, so 18 columns were added: 12 commentary measures and 6 local and document currency amounts. The model's SQL produced all of them and every scheduled run succeeded.

The report showed blank columns

When the new report came up, those columns were blank.

My first guess was a bug in the logic of the new columns, so that's where I looked first. The logic was fine. The problem was the table. The model produced 253 columns and the production table had 235. The 18 new columns had never made it in.

18 columns built every run, never stored

What dbt does by default

On incremental models, dbt has a setting called on_schema_change. It decides what happens when the model's columns and the existing table's columns don't match. Its default is ignore.

With ignore, dbt doesn't change the table at all. When it inserts the new batch, it uses the columns the table already has. Any extra columns the model produces are simply left out. No warning, no error, a green run.

So for six weeks, every incremental run did exactly what the default says: it built 253 columns and stored 235.

The asymmetry that makes it sneaky

The part I find most useful to remember is that the two common schema changes fail in completely different ways.

  • Adding a column fails silently. The column just never appears in the table.
  • Renaming a column fails loudly. The insert refers to a column the table doesn't have, and Snowflake stops it with invalid identifier.

I've seen both in the same project. Another incremental model had a column renamed, and it broke every pull request's build until it was fixed, so that one was noticed right away. The added columns in the finance model took six weeks and a blank report.

The four options

on_schema_change What happens when the model and table differ
ignore (default) Nothing. New columns are left out of the insert.
fail The run stops with an error, so a person decides what to do.
append_new_columns New columns are added to the table. Removed columns stay.
sync_all_columns New columns are added and removed columns are dropped.

How I fixed it

I set append_new_columns on the model, so new columns get added to the table automatically:

{{
    config(
        materialized='incremental',
        incremental_strategy='delete+insert',
        unique_key=['fiscal_year', 'fiscal_period'],
        on_schema_change='append_new_columns'
    )
}}
Enter fullscreen mode Exit fullscreen mode

There's one catch. append_new_columns adds the column and fills it with NULL for every existing row. It doesn't recompute history. So the new columns were only populated for periods processed after the change. To fill them for the past, I reloaded a bounded range of periods, using the same period-aligned reload I described in the first post, rather than a full refresh. After that, the production table had all the columns, including all 12 commentary measures.

The default I'd pick now

If I were setting one default for every incremental model on a team, I'd choose fail.

append_new_columns fixes the symptom, but it still makes a decision for you, and it leaves history half-filled until someone remembers to reload. With fail, the run stops the first time the model and the table disagree, and a person decides: add the column and reload, or change the model. That's a short, visible interruption instead of six silent weeks.

And I'd pair it with a dbt test that checks the columns a report depends on actually exist in the built table:

-- tests/assert_report_columns_exist.sql
-- Fails if any column the report needs is missing from the built table.
select required.column_name
from (
    select 'COMMENTARY_MEASURE_01' as column_name union all
    select 'COMMENTARY_MEASURE_02'
    -- ...one line per column the report depends on
) as required
left join {{ ref('my_gl_model').database }}.information_schema.columns as c
    on  c.table_schema = '{{ ref('my_gl_model').schema | upper }}'
    and c.table_name   = '{{ ref('my_gl_model').identifier | upper }}'
    and c.column_name  = required.column_name
where c.column_name is null
Enter fullscreen mode Exit fullscreen mode

Any row returned is a column the report expects but the table doesn't have, and the test fails.

To sum up

On incremental models, on_schema_change defaults to ignore, and ignore means new columns can disappear without an error. Set it explicitly on every incremental model. If you use append_new_columns, remember that history comes in as NULL and needs a reload. And remember the asymmetry: renames shout, additions whisper.

If you want to check your own project, any incremental model that doesn't set on_schema_change is running on ignore:

grep -rl "materialized='incremental'" models/ | xargs grep -L "on_schema_change"
Enter fullscreen mode Exit fullscreen mode

This is the fourth post in my series about failures in the data stack where everything is green and the numbers are still wrong.

If your team is looking to implement AI solutions, especially on top of data you need to be able to trust, you can book a free 15-minute AI Quick Audit or message me on LinkedIn.

Top comments (0)