sqlmesh icon indicating copy to clipboard operation
sqlmesh copied to clipboard

Bug: `INDIRECT_NON_BREAKING` snapshots in dev environments write to a table nobody reads, causing repeated re-backfills

Open gandeevanraghuramandd opened this issue 6 months ago • 4 comments

When a snapshot is categorized as INDIRECT_NON_BREAKING, dev environment runs (sqlmesh run dev) repeatedly backfill the same intervals on every invocation. Completed work is never recognized.

The root cause is an inconsistency in how three subsystems treat INDIRECT_NON_BREAKING snapshots. These snapshots are is_representative=True but is_deployable=False, and different parts of the codebase use different predicates:

Decision Predicate used Result Effect
Which table does the dev view point to? is_representative points to prod table i.e sqlmesh__<schema>.<model>__<version> View reads from prod table
Which intervals are checked for missing work? is_representative intervals (prod ledger) Missing work computed against prod
Where is the evaluated data written? is_deployable sqlmesh__<schema>.<model>__<dev_version>__dev (dev table, __dev suffix) Data written to dev table
Where is completed work recorded? is_deployable dev_intervals (dev ledger) Completions written to dev ledger

Because missing-interval detection reads intervals but successful evaluation writes to dev_intervals, completed work is never visible to the next run.

Reproduction

  1. Have a model B that depends on model A.
  2. Make a non-breaking change to A and apply a plan. B gets categorized as INDIRECT_NON_BREAKING.
  3. Both prod and dev environments now point to the same snapshot of B.
  4. Run sqlmesh run dev --ignore-cron. It backfills some intervals for B.
  5. Run sqlmesh run dev --ignore-cron again immediately. It backfills the same intervals.
  6. Repeat indefinitely -- same result every time.

Code references

INDIRECT_NON_BREAKING is unconditionally non-deployable - DeployabilityIndex.create() in sqlmesh/core/snapshot/definition.py:

if (
    snapshot.is_forward_only
    or snapshot.is_indirect_non_breaking   # <-- unconditional
    or is_forward_only_model
    or has_auto_restatement
    or not is_valid_start
):
    this_deployable = False

But it is marked as representative - same method, immediately below:

if not snapshot.is_paused or (
    snapshot.is_indirect_non_breaking and snapshot.intervals
):
    representative_shared_version_ids.add(node)

Missing-interval detection uses is_representative - Snapshot.missing_intervals() in sqlmesh/core/snapshot/definition.py:

intervals = (
    self.intervals if deployability_index.is_representative(self) else self.dev_intervals
)

Interval recording uses is_deployable - PlanEvaluator in sqlmesh/core/plan/evaluator.py:

is_deployable = deployability_index.is_deployable(snapshot)
# ...
intervals=intervals if is_deployable else [],
dev_intervals=intervals if not is_deployable else [],

View promotion uses is_representative - SnapshotEvaluator._promote_snapshot() in sqlmesh/core/snapshot/evaluator.py:

table_name = snapshot.table_name(deployability_index.is_representative(snapshot))

Impact

  • Dev runs are never idempotent for affected models. Every run re-processes the same intervals.
  • The dev table (__dev suffix) receives data that no view ever points to. The work is wasted.
  • CI pipelines using sqlmesh run dev will always perform full backfills regardless of prior runs.
  • This is not version-specific; the same code pattern exists across multiple SQLMesh releases.

Expected behavior

For a given snapshot, these three decisions should be consistent:

  1. The table the dev view points to,
  2. The interval ledger checked for missing work, and
  3. The interval ledger updated after successful work

should all refer to the same physical table (either prod or dev), not a mix of both.

Environment

  • SQLMesh version: 0.234.1 (also confirmed in 0.231.1 source)
  • Engine: DuckDB (but the issue is engine-independent; it's in the core snapshot/plan logic)

Note: AI was used to both triage this bug and to document the issue. However, the code paths and conclusions were manually inspected and validated.

gandeevanraghuramandd avatar May 13 '26 22:05 gandeevanraghuramandd

Roping some people together here (@gandeevanraghuramandd , @davem-bis , @StuffbyYuki )

So the diagnosis is right. run checks missing work on the prod table/ledger, then writes a __dev table and ticks a different ledger, so the next run never sees that work.

We end up with a situation where we have one physical table, and two different snapshots. First snapshot is the prod version. Second snapshot is because the parent changed, causing the model to be INDIRECT_NON_BREAKING. The problem is the second snapshot gets its own __dev ledger for runs that it writes to even though it still shares the physical table with the prod snapshot.

“Make all three decisions consistent” can go two ways:

  1. We create another physical table, a __dev table, that fixes this because the second snapshot now has a physical table to associate with the __dev ledger.
  2. Since the second snapshot currently is pointed at the same physical table as the prod snapshot, we just remove the __dev ledger and have it also share the prod ledger.

If we go route 2, that fixes the infinite loop and keeps dev cheap: we don’t build extra physical tables for unchanged downstream models. Caveat: those INDIRECT_NON_BREAKING models still point at prod’s physical tables, so they are only as fresh as prod. run dev, even with --ignore-cron, will not give them a private, ahead-of-prod copy.

@gandeevanraghuramandd -- does that fix your CI? The problem is run reloading the same days forever, right? For INDIRECT_NON_BREAKING we’d stop building __dev and stop ticking the __dev ledger. run would use prod’s days and skip those models.

P.S. Sorry @davem-bis -- I just realized with your PR this might introduce more compute costs for users. So maybe we flip the approach. Right now you're working on approach 1

cmgoffena13 avatar Sep 03 '26 03:09 cmgoffena13

Not a problem @cmgoffena13 - I appreciate this is quite a nuanced bit of logic so happy with lots of discussion on it.

Regarding option 2, is there ever a scenario where the non-dev physical table can end up being deployable, or more crucially can it ever end up being in both the prod and a dev environment?

I'm thinking of whether having an INDIRECT_NON_BREAKING model build to the non-dev physical table could put the prod environment out of sync with regards to what intervals have been run for different tables? Presumably this could happen when the upstream non-breaking model would is promoted, but at that point the plan should pick up any missing intervals.

davem-bis avatar Sep 04 '26 15:09 davem-bis

@davem-bis

  1. The non-dev physical table is already in both prod and dev because the snapshot is is_representative. Both views read that table. There is no __dev table the environment is supposed to increment, which is why we should skip the snapshot instead of evaluating it.

  2. Your concern is the right question. It depends on the way we do the change. With run I think we'd just skip if its non-prod and its pointed at a prod physical table. That way we never end up with a dev run actually affecting the prod ledger. It won't create a __dev ledger either, so no infinite loop. I see route 2 being a change to the run command specifically and it actually becomes a lot simpler then.

I'm looking at Scheduler.merged_missing_intervals in sqlmesh/core/scheduler.py. Really looking at lines 185 - 188, snapshots_to_intervals

So logically, we just want to not include snapshots to run that are: is_representative and not is_deployable

This means if a snapshot is is_representative -> pointed at prod physical table, not is_deployable -> this environment is not allowed to write to this table. We skip.

Today run evaluates these anyway, which creates a __dev table and ticks the dev_intervals, creating the misalignment. If we drop the snapshots there, they never get evaluated.

INDIRECT_NON_BREAKING is never deployable. Prod run declares all prod snapshots are is_deployable, so no worries there.

cmgoffena13 avatar Sep 05 '26 02:09 cmgoffena13

I think that makes sense to me.

The only thing I can of against it is that it won't run the indirect non breaking model outside of the prod environment using the new physical non-breaking model that will get promoted, so you could theoretically argue that the model hasn't been run in anger before it's planned into prod. I don't think that's a showstopper as the non-breaking model shouldn't, well, break!

Unless anyone else throws an opinion in I'll start dev / testing that soon.

davem-bis avatar Sep 07 '26 15:09 davem-bis