Skip to content

Run one dbt build in the watcher mode of Cosmos, with a DAG of maintenance

Superseded in part by ADR-0046: "the tests of the sources" in the order of "The transform", as no test reads a raw table, and "detaches and" in "The DAG of maintenance", as a month of a journal is dropped with no detach, and by ADR-0047: "It runs amcheck" in "The DAG of maintenance", as the maintenance runs no amcheck. The rest of this record stays accepted.

Context and Problem Statement

ADR-0028 builds each node with its tests in one Airflow task. Today that is 84 tasks, each a dbt process of its own (LOCAL, SUBPROCESS, TestBehavior.BUILD), two at a time, and 160 seconds for 657 nodes of the demo: about 4 seconds of a slot for each task, nearly all of it orchestration. The transform also starts only on the assets of the demo or of Google Ads: the rates do not start it.

The architecture of ADR-0035 adds what no DAG does yet: a cleanup of the raw layer after a run, the partitions of each new month, ANALYZE of the partitioned parents, the retention of journals. And a run must not rebuild the views: a rebuild takes ACCESS EXCLUSIVE on the name of a view, queues behind a long query of Metabase and blocks every reader behind it.

How does Airflow run dbt, what keeps the views out of the daily run, and what does the upkeep of the warehouse?

Decision Drivers

  • A run is visible in Airflow, node by node, and a failed test stops what lies below it.
  • The cost of a run goes into dbt, not into starting processes.
  • A daily run keeps the tests of the views: they are the gate G1.
  • Upkeep runs by itself, with no operator, and each step can be repeated.
  • transformer does not own the tables of the core (ADR-0035).

Considered Options

For the run of dbt:

  • One dbt build in ExecutionMode.WATCHER
  • One task for each node with TestBehavior.BUILD, as today

For the views:

  • An override of the materialization view for the schema marts, which rebuilds only on demand
  • A list of nodes for each dbt build, repeated in Cosmos
  • The standard behavior, with a lock on every run

For the upkeep:

  • One DAG of maintenance, with idempotent tasks
  • Callbacks on the DAGs that exist
  • A new service

Decision Outcome

Chosen options: "One dbt build in ExecutionMode.WATCHER", "An override of the materialization view" and "One DAG of maintenance", because one process does the work of 84 and the nodes stay visible, eight lines of a macro keep one build of the whole project with its tests, and a DAG is what the stack already runs and watches.

In practice this means:

  • The transform. One DAG for all the sources, started by AssetAny of every source and of the rates, with max_active_runs = 1, as two builds must not overlap. The nodes appear as tasks in Airflow, and dbt runs once. The order inside the run comes from the graph: the tests of the sources, the views of L1 and their tests, the checks by account and the quarantine, the dimensions and registries, the facts with the journal of restatements, the check of sums, and aggregates with their tests.
  • The cleanup of L0. A task after dbt build, with trigger_rule = all_success and retries, not a callback (ADR-0035).
  • The views. The project overrides the materialization view for marts: when the view exists and rebuild_views was not passed, the model does nothing, and its tests and its tasks in Cosmos stay. ./agoranomos up runs the build with rebuild_views, in a new one-shot service as transformer, because metadata-export runs with no network. The views of L1 are recreated as usual, and they must be: the macro relation_or_empty puts in an empty query while a table does not exist, and only the next build reads the table.
  • The DAG of maintenance, once a day.
    • It creates the partitions of two months ahead through core.ensure_partition, a function SECURITY DEFINER of the role of the migrations, so transformer gets no ownership. Inside are CREATE TABLE ... (LIKE ...), a CHECK and ATTACH, because PARTITION OF takes ACCESS EXCLUSIVE on the parent and would queue the dashboards, and ATTACH takes only SHARE UPDATE EXCLUSIVE. The function grants MAINTAIN on the new partition.
    • It runs ANALYZE ONLY on the parents of the facts, which autovacuum does not analyze.
    • It runs VACUUM (FREEZE) on a month that has left the window of restatements.
    • It detaches and drops partitions of journals past their retention, and cleans _dlt_loads, elementary and core.loads (ADR-0044).
    • It runs amcheck, records the used and free disk in Ops, and once a month compares the core with core.platform_totals_daily for the last 90 days.
  • Retries. The DAGs of the loads get retries with retry_exponential_backoff and a dagrun_timeout.

The other options were not chosen for these reasons:

  • One task for each node spends about 8 to 9 seconds on each call in the benchmark of Astronomer: 32 minutes against 5.5 for 185 models. Its one gain, a retry of one node, matters little here: a repeat of the whole build is safe, as each window is replaced whole.
  • A list of nodes for each build has to be repeated in Cosmos, and --exclude removes the tests of the views with them, which is the gate G1.
  • The standard behavior gives each view a new oid every day, by a rename and drop view ... cascade, under a lock.
  • Callbacks run only on the DAGs that have them, and a failure of a callback is hard to see. A new service costs memory that the budget does not have (ADR-0022).

Consequences

  • Good, because the time of a run goes to the work: dbt starts once.
  • Good, because partitions, ANALYZE and retention need no operator, and a failure shows up as a failed DAG.
  • Good, because a dashboard is not blocked by a daily run.
  • Good, because a new asset of a source or of the rates starts the transform with no change of code.
  • Bad, because the watcher mode is young: stable since Cosmos 1.15.0, as of October 2026.
  • Bad, because a retry of the producer task does not start dbt again, so a failed run takes a new run of the DAG.
  • Bad, because a project with its own materialization carries eight lines that an upgrade of dbt can break.
  • Bad, because the budget has no room left: the new one-shot service of 256 MiB brings the sum of the limits to 7,680 of 7,680 MiB in server mode.

More Information

  • Implementation status, as of November 2026: in place for v0.5, but for the watcher mode, which the maintainer deferred to v0.6.0, as the end of this item says; the trigger on any asset is in place, and so is the override of the materialization view for marts, with rebuild_views (dbt/macros/marts_view.sql), and so is the one-shot service marts-init, which builds the views at ./agoranomos up with rebuild_views, as transformer, after db-init and before metabase-init: one dbt build that loads the seeds of the base package first, as the views carry the rows of the seed of the platforms in their text, with --select marts and without tests where the snapshot that the views read exists, and with --select +marts and its tests where it does not, as on a new warehouse (metabase/marts_init.py). It holds an advisory lock of PostgreSQL, exclusive, through its build, which each model, seed and snapshot of a run of the transform takes, shared, in the transaction in which it writes, and the pin through all of its transactions (dbt/macros/marts_init.sql), as max_active_runs does not reach a service outside Airflow. Its limit of 384 MiB, by the formula of the system requirements from its peak of 184 MiB on a new warehouse, fits the budget of 7,680 MiB of ADR-0022 as ADR-0048 counts the one-shot jobs: with airflow-init and metadata-export, which can run beside it, the limits of the LocalExecutor add up to 7,424 MiB in local mode and to 7,552 MiB in server mode; and so is the DAG of maintenance, agoranomos_maintenance, once a day, as the record says it, with the retention of ADR-0044: it makes the partitions of the current month and the two after it through core.ensure_partition, runs ANALYZE ONLY on the parents of the facts and VACUUM (FREEZE) on each month of a fact that has left the window of restatements and changed since its last freeze, and checks both in pg_stat_all_tables, as without MAINTAIN they skip a table with a WARNING and succeed; it drops the months of the journals of restatements older than 13 months through core.drop_partition, and leaves a month that its lock timeout of 2 seconds refuses to the next run; it cleans _dlt_loads, elementary and core.loads; it records the used and the free disk, the bytes of the partitions of the window and an estimate of the free space in them in ops.ops_disk, the seconds of each step in ops.ops_maintenance_steps, and once a month the comparison of the core with core.platform_totals_daily over 90 days in ops.ops_core_totals; and it deletes the Variables cosmos_xcom_backup__* of the runs older than 90 days (airflow/dags/agoranomos_pipelines/maintenance.py). It runs no amcheck, which ADR-0047 leaves out. And so is the cleanup of L0, a task after the tasks of dbt of the transform, with the rule all_success, the default, and two retries, which follows the acceptance of ADR-0046 (clean_raw in airflow/dags/agoranomos_demo_transform.py, airflow/dags/agoranomos_pipelines/acceptance.py). The rest of this record is not built: the transform runs one task of dbt for each node, in ExecutionMode.LOCAL with TestBehavior.BUILD. The maintainer decided on 12 November 2026 that v0.5 keeps them, and that the switch to ExecutionMode.WATCHER waits for the measurements of the transform on the long-running stand, in v0.6.0 (#203): in Cosmos 1.16.0 a watcher sensor that falls back to a run of its own builds its command of dbt from the flags in the XCom of the producer, which the producer restores from its task store and from a Variable, as below, and the roles Op and User of Airflow may write them, against the rule of ADR-0027 that nothing that the role Op can edit decides what a task of dbt runs. The transform starts on the asset of the rates of the ECB, on that of Google Ads, and on the five assets of the demo as one condition (airflow/dags/agoranomos_pipelines/assets.py): as five conditions, one load of the demo would start a run after its first platform, which would read tables whose days the other loads still replace, and one more run after them. The pinning of ADR-0046 is in place, which lets the five become conditions of their own; they are still one.
  • The facts behind this record:
    • ExecutionMode.WATCHER is stable since Cosmos 1.15.0, and runs one dbt build for the whole project, with tasks marked by the events of dbt (watcher mode). The benchmark is on that page, and the numbers of the demo are from October 2026.
    • Run on 31 October 2026: a daily dbt build recreates each view with a new oid; --exclude config.materialized:view removes their tests too; with the override of eight lines, the oid stays, the tests run, --vars rebuild_views rebuilds, and the descriptions stay. A partition through a function SECURITY DEFINER was created by a role without ownership, through PARTITION OF in that check; ANALYZE ONLY works with MAINTAIN on the parent, and VACUUM of a partition needs MAINTAIN on the partition itself.
    • Run on 1 November 2026, with Airflow 3.3.2, Cosmos 1.15.1, dbt-core 1.12.6, dbt-postgres 1.11.0 and PostgreSQL 18.6, the LocalExecutor, AIRFLOW_PARALLELISM=4 and the memory limits of the stack: ExecutionMode.WATCHER with InvocationMode.SUBPROCESS, on a small dbt project with a handful of rows, loaded from a manifest (ADR-0027) and with the override of eight lines. The two work together:
      • The nodes appear as tasks, and one dbt build runs. With TestBehavior.AFTER_EACH a model with tests is a group of two sensors, run and test, so a project of the size of the demo, 84 models and 588 tests, has 169 tasks. TestBehavior.BUILD fails the import of the DAG in this mode, with NotImplementedError.
      • A daily run keeps the oid of a view of marts and runs its tests; rebuild_views, through the operator_args of Cosmos or on the command line of dbt, gives a new oid. A view of marts directly over a view of L1 gets a new oid on every run, as the rebuild of L1 drops it by cascade.
      • A failed test of a view stops its descendants in dbt and in Airflow. A test of two parents that dbt skipped also fails the test sensor of the other parent, whose descendants then show as upstream_failed although dbt built them; should_detach_multiple_parents_tests does not change that.
      • A retry or a clear of the producer task does not start dbt build: the task restores the states from the Airflow Variable and ends as skipped, and the sensors read them. After a failed test the run stays failed, and so does a clear of the failed sensors with their downstream tasks (#3023); a new run of the DAG repairs it. When dbt itself was killed and the producer had retries, the sensors with no state ran their nodes themselves, one process of dbt for each and outside the pool of the producer, and the run ended in success. A failed run leaves its Variable behind (#2823).
      • max_active_runs = 1 holds: a second run, queued at once, started after the first had ended, and the two builds did not overlap. After a SIGKILL of the task process of the producer, the task stayed running until its process of dbt had gone.
      • execution_timeout also limits a sensor that waits: with 20 seconds on the sensors and a build of 48 seconds, a sensor failed and the run with it, while the producer succeeded.
      • A task group with a task after it works, with the pool on the producer alone: the gate dbt_producer_watcher_done of Cosmos keeps that task from running after a failed test or a failed on-run-end hook.
      • At the size of the demo, over empty tables, so the cost of the orchestration alone, a run took 36 and 39 seconds in the watcher mode, with 8.8 seconds of dbt, against 239 seconds in the local mode with TestBehavior.BUILD and two slots. The container of the tasks peaked at 878 and 842 MiB of 1,280, and the triggerer at 421 MiB of 512. The failures of the sensors on the stand of ADR-0027 did not come back in 35 runs.
    • ATTACH and DETACH CONCURRENTLY take SHARE UPDATE EXCLUSIVE, and a DEFAULT partition prevents the second (ALTER TABLE). The official image has contrib and no pg_partman.
    • Run on 1 November 2026, on PostgreSQL 18.6 under a limit of 1 GiB, with a fact of 123 monthly partitions and 7 million rows, a foreign key to dim_account and eight readers of pgbench on a view over the fact, in three runs of each case unless it says otherwise:
      • core.ensure_partition through ATTACH works as a role without ownership, and takes SHARE UPDATE EXCLUSIVE on the parent. With only the readers it took 12 ms against 13 ms for PARTITION OF, and the longest read beside it took 23 ms against 35 ms.
      • Behind a query of 15 seconds, ATTACH took 24 ms and no reader waited, while PARTITION OF waited 11.8 seconds with all eight readers behind it. During ANALYZE ONLY of the parent, ATTACH waited 0.5 seconds with no reader behind it, and PARTITION OF stopped all eight for 1.0 to 2.3 seconds.
      • With the foreign key, ATTACH also takes SHARE ROW EXCLUSIVE on dim_account, as PARTITION OF does. It waits for an open write to the dimension, while the readers go on, and two facts that create partitions in open transactions wait for each other, so a pre-hook that creates one belongs outside the transaction of its model.
      • The CHECK changes nothing on an empty table, 11.0 ms against 10.7 ms. It pays on a table filled beforehand: 2.4 million rows attached in 1.1 to 3.1 seconds without it and in 0.2 to 0.3 seconds with it, in two runs.
      • A backfill of a quarter in one transaction, into three old partitions at the density of the limit, wrote 7.36 million rows in 90 seconds, in four runs. The 99th percentile of the readers rose from 21 to 25 ms, no reader waited for a lock behind the backfill, and no count of the quarter saw a part of it.
      • DETACH CONCURRENTLY cannot run in a function or a procedure (SQLSTATE 25001), so a role without ownership cannot reach it through SECURITY DEFINER. Run by the owner, it waits out a query of 15 seconds and the readers go on. DROP of a detached partition of a fact then takes ACCESS EXCLUSIVE on dim_account, and all eight readers stood behind that query for 11.7 seconds, as they do behind a plain DROP of the partition. A partition of a journal has no foreign key: DROP TABLE in a function SECURITY DEFINER, with a lock_timeout of 2 seconds, took 5 ms and the readers of the fact did not notice. Behind a reader of the journal it gave up after 2 seconds, and a writer of the journal waited that long.
      • ANALYZE ONLY works with MAINTAIN on the parent alone, in about 0.9 seconds. Without MAINTAIN, ANALYZE and VACUUM skip the table with a WARNING and succeed.
  • Not checked yet, and to be checked before or with the code:
    • The watcher mode on the real project, with the hooks of elementary and started by AssetAny: the stand of 1 November 2026 ran small projects, started by hand.
    • The copy of the states in an Airflow Variable also holds the flags of the command of the producer, and a sensor that falls back builds its command from them (read in the source of Cosmos 1.15.1, not run). The Op role may edit a Variable, which a driver of ADR-0027 rules out. Cosmos 1.16.0, released on 3 November 2026 and in the stack since 7 November, keeps the copy in the task store of the producer on Airflow 3.3 and later while enable_watcher_reliable_retry is on, as it is by default. The callbacks of a retry and of a failure write the Variable only when the last write to the task store failed, and only with the entries of the row of that write, and a producer that runs again still reads the Variable, whose entries win over those of the task store (cosmos/operators/_watcher/xcom.py). The REST API of Airflow 3.3.2 lets whoever may edit the task instances of a DAG write their task store, which with the FAB provider 3.9.0 is the role User and the roles above it, and the sensors read the flags from an XCom of the producer, which the Op role may write through that API too (read in the sources on 7 November 2026, not run).
    • The memory of a task and of the database under a real dbt build with elementary.
  • Related decisions: at acceptance this record supersedes ADR-0028 and replaces "each model is retried and observed in Airflow" of ADR-0005. The manifest of ADR-0027 stays.
  • Revisit this record when the watcher mode shows a limit in practice, or when a run needs to retry one node.