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.
transformerdoes not own the tables of the core (ADR-0035).
Considered Options
For the run of dbt:
- One
dbt buildinExecutionMode.WATCHER - One task for each node with
TestBehavior.BUILD, as today
For the views:
- An override of the materialization
viewfor the schemamarts, 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
AssetAnyof every source and of the rates, withmax_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, withtrigger_rule = all_successand retries, not a callback (ADR-0035). - The views. The project overrides the materialization
viewformarts: when the view exists andrebuild_viewswas not passed, the model does nothing, and its tests and its tasks in Cosmos stay../agoranomos upruns the build withrebuild_views, in a new one-shot service astransformer, becausemetadata-exportruns with no network. The views of L1 are recreated as usual, and they must be: the macrorelation_or_emptyputs 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 functionSECURITY DEFINERof the role of the migrations, sotransformergets no ownership. Inside areCREATE TABLE ... (LIKE ...), a CHECK andATTACH, becausePARTITION OFtakes ACCESS EXCLUSIVE on the parent and would queue the dashboards, andATTACHtakes only SHARE UPDATE EXCLUSIVE. The function grantsMAINTAINon the new partition. - It runs
ANALYZE ONLYon 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 andcore.loads(ADR-0044). - It runs
amcheck, records the used and free disk in Ops, and once a month compares the core withcore.platform_totals_dailyfor the last 90 days.
- It creates the partitions of two months ahead through
- Retries. The DAGs of the loads get
retrieswithretry_exponential_backoffand adagrun_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
--excluderemoves 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,
ANALYZEand 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
viewformarts, withrebuild_views(dbt/macros/marts_view.sql), and so is the one-shot servicemarts-init, which builds the views at./agoranomos upwithrebuild_views, astransformer, afterdb-initand beforemetabase-init: onedbt buildthat 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 martsand without tests where the snapshot that the views read exists, and with--select +martsand 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), asmax_active_runsdoes 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: withairflow-initandmetadata-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 throughcore.ensure_partition, runsANALYZE ONLYon the parents of the facts andVACUUM (FREEZE)on each month of a fact that has left the window of restatements and changed since its last freeze, and checks both inpg_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 throughcore.drop_partition, and leaves a month that its lock timeout of 2 seconds refuses to the next run; it cleans_dlt_loads, elementary andcore.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 inops.ops_disk, the seconds of each step inops.ops_maintenance_steps, and once a month the comparison of the core withcore.platform_totals_dailyover 90 days inops.ops_core_totals; and it deletes the Variablescosmos_xcom_backup__*of the runs older than 90 days (airflow/dags/agoranomos_pipelines/maintenance.py). It runs noamcheck, which ADR-0047 leaves out. And so is the cleanup of L0, a task after the tasks of dbt of the transform, with the ruleall_success, the default, and two retries, which follows the acceptance of ADR-0046 (clean_rawinairflow/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, inExecutionMode.LOCALwithTestBehavior.BUILD. The maintainer decided on 12 November 2026 that v0.5 keeps them, and that the switch toExecutionMode.WATCHERwaits 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.WATCHERis stable since Cosmos 1.15.0, and runs onedbt buildfor 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 buildrecreates each view with a new oid;--exclude config.materialized:viewremoves their tests too; with the override of eight lines, the oid stays, the tests run,--vars rebuild_viewsrebuilds, and the descriptions stay. A partition through a functionSECURITY DEFINERwas created by a role without ownership, throughPARTITION OFin that check;ANALYZE ONLYworks withMAINTAINon the parent, andVACUUMof a partition needsMAINTAINon 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=4and the memory limits of the stack:ExecutionMode.WATCHERwithInvocationMode.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 buildruns. WithTestBehavior.AFTER_EACHa model with tests is a group of two sensors,runandtest, so a project of the size of the demo, 84 models and 588 tests, has 169 tasks.TestBehavior.BUILDfails the import of the DAG in this mode, withNotImplementedError. - A daily run keeps the oid of a view of
martsand runs its tests;rebuild_views, through theoperator_argsof Cosmos or on the command line of dbt, gives a new oid. A view ofmartsdirectly over a view of L1 gets a new oid on every run, as the rebuild of L1 drops it bycascade. - 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_failedalthough dbt built them;should_detach_multiple_parents_testsdoes 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 = 1holds: 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_timeoutalso 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_doneof Cosmos keeps that task from running after a failed test or a failedon-run-endhook. - 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.BUILDand 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.
- The nodes appear as tasks, and one
ATTACHandDETACH CONCURRENTLYtake SHARE UPDATE EXCLUSIVE, and a DEFAULT partition prevents the second (ALTER TABLE). The official image has contrib and nopg_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_accountand eight readers ofpgbenchon a view over the fact, in three runs of each case unless it says otherwise:core.ensure_partitionthroughATTACHworks as a role without ownership, and takes SHARE UPDATE EXCLUSIVE on the parent. With only the readers it took 12 ms against 13 ms forPARTITION OF, and the longest read beside it took 23 ms against 35 ms.- Behind a query of 15 seconds,
ATTACHtook 24 ms and no reader waited, whilePARTITION OFwaited 11.8 seconds with all eight readers behind it. DuringANALYZE ONLYof the parent,ATTACHwaited 0.5 seconds with no reader behind it, andPARTITION OFstopped all eight for 1.0 to 2.3 seconds. - With the foreign key,
ATTACHalso takes SHARE ROW EXCLUSIVE ondim_account, asPARTITION OFdoes. 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 CONCURRENTLYcannot run in a function or a procedure (SQLSTATE 25001), so a role without ownership cannot reach it throughSECURITY DEFINER. Run by the owner, it waits out a query of 15 seconds and the readers go on.DROPof a detached partition of a fact then takes ACCESS EXCLUSIVE ondim_account, and all eight readers stood behind that query for 11.7 seconds, as they do behind a plainDROPof the partition. A partition of a journal has no foreign key:DROP TABLEin a functionSECURITY DEFINER, with alock_timeoutof 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 ONLYworks withMAINTAINon the parent alone, in about 0.9 seconds. WithoutMAINTAIN,ANALYZEandVACUUMskip 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_retryis 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 buildwith elementary.
- The watcher mode on the real project, with the hooks of elementary and started by
- 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.