Pin the loads before the transform, and accept them before the cleanup
Superseded in part by ADR-0051: "the load with the largest _dlt_load_id" of "What L1 reads", as a load of the kind window or history now comes before one of the kind intraday, whatever their load IDs. The rest of this record stays accepted.
Context and Problem Statement
ADR-0038 removes the race between a load and the transform by pinning finished loads, ADR-0035 erases L0 once the core has accepted a load, and ADR-0039 gives the order inside a run. On 1 November 2026 these ran on throwaway stands, with real parallel loads. The design holds, and pinning needs no lock, but four of its wordings do not survive:
- ADR-0038 pins "in
on-run-start". That hook fires on everydbt build,dbt runanddbt test, so a pin lasts one call of dbt and not one run of the DAG. With one task for each node (ADR-0028), which stays until the watcher mode comes, a load that ended between two tasks reached the fact and not the dimension. Adbt testby hand pinned too. - ADR-0038 says that L1 sees "the newest for each account". After a failed run, a short window of a newer load dropped the days that only the older load held. A cleanup then removed them from L0, and the next window started past them, so they were gone with a green run.
- ADR-0035 deletes the rows and then marks the load accepted, and ADR-0038 gives an emptied table a
TRUNCATE. A failure between the first two left a pinned load with no rows, and the next run read its window as empty days and deleted them from the core. ATRUNCATEafter a separate check lost 400 of 6,720 rows of a load in progress, which still ended whole in_dlt_loads. - ADR-0039 runs "the tests of the sources" first. Such a test stops the first
up, when a table of L0 does not exist yet, and it reads past the pin: the rows of a killed load stopped a run.
Two more follow from the same checks. DETACH CONCURRENTLY cannot run in a function or a procedure (SQLSTATE 25001), so a role without ownership cannot reach it through SECURITY DEFINER, while ADR-0039 detaches and drops the partitions of journals. And ADR-0044 cleans core.loads by partition, while a pin needs a key that its on conflict can use, with no date in it.
An accepted record keeps its decision, so these changes need a record of their own. Where is a load pinned, which load does L1 read, in what order is a load accepted and erased, and where do the checks of L0 run?
Decision Drivers
- One run of the DAG reads one set of loads, from its first model to its last, however many calls of dbt it makes.
- A green run never loses a day.
- No lock between a load and the transform, as ADR-0038 wants.
- A failure between any two steps loses nothing, and a repeat finishes the work.
- It works with one task for each node and with the watcher mode, as the first comes before the second.
Considered Options
For where a load is pinned:
- One task before the tasks of dbt, in a run of the DAG only
on-run-start, as ADR-0038 sayson-run-startwith the watcher mode, and a lock until then
For what L1 reads:
- The newest load for each account and day, among the pinned and the accepted, with rows from the pinned only
- The newest load for each account, as ADR-0038 says
- The newest load for each account and day, among the pinned only
For the order of the cleanup:
- Mark, delete, then
TRUNCATEunder a lock in one transaction - Delete, then mark, as ADR-0035 says
- Mark and delete in one transaction
For the checks of L0:
- No test on a raw table: the checks run on the views of L1
- The tests of the sources first, as ADR-0039 says
- The tests of the sources with a
whereon the pinned loads
Decision Outcome
Chosen options: "One task before the tasks of dbt", "the newest load for each account and day, among the pinned and the accepted", "mark, delete, then TRUNCATE under a lock", and "no test on a raw table", because each of them held on the stand where the option it replaces lost data or stopped a run.
- The pin. One task before the tasks of dbt in the DAG of the transform runs
dbt run-operationon a macro that pins the finished loads. Only a run of the DAG pins: dbt by hand reads what is pinned and pins nothing. The task stays once the watcher mode comes, so that the rule is one. - What L1 reads. For each account and day of a window, the load with the largest
_dlt_load_id, compared as a number, among the pinned and the accepted, and rows of L0 only from the pinned. So a short window of a newer load keeps the other days of an older one, and an older load that ends after a newer one was accepted does not replace its days. A row of the journal with a failed account has no window, and L1 passes over it. core.loads. One row for each load and account, as one load can be accepted for some accounts and wait in quarantine for others, with the statuspinnedoracceptedand theload_skthat the facts refer to (ADR-0036). It has no partitions, as its key holds no date and it holds a few hundred thousand rows at the limit: the DAG of maintenance deletes the rows past 13 months.- The acceptance and the cleanup. After a run that succeeded, in this order, each step repeatable:
transformermarks as accepted what this run or an earlier one pinned, by load and account, with no account in quarantine and no stopped source. A load pinned later stays pinned, as no run has read it.loaderdeletes the rows of what is accepted, by load and account.loadertakesLOCK TABLE ... IN ACCESS EXCLUSIVE MODE, checks that a table is empty and runsTRUNCATE, in one transaction. A table that thelock_timeoutdoes not reach waits for the next cleanup. Where a table holds only accepted rows,TRUNCATEreplaces the delete.
- Unfinished loads.
loaderremoves the rows of a load older than a cut, with no row in_dlt_loadsor incore.loads. The cut is no shorter than thedagrun_timeoutof a load. - The checks of L0. No test on a raw table. The order inside a run stays as ADR-0039 gives it, without the tests of the sources: the checks of the data live on the views of L1, which is G1 of ADR-0035.
-
The stop of a source. ADR-0035 stops a source with more than half of its accounts in quarantine. A filter in SQL does it, as a test at
errorabove a fact that all sources share stops the healthy ones too. A test atwarn, the view of the quarantine and the alerts of ADR-0045 tell a person. -
The partitions of journals.
core.drop_partition,SECURITY DEFINERand for journals only, drops a month with a plainDROP TABLEand alock_timeoutof 2 seconds. A refusal costs a retry of the DAG. Journals have no foreign key, so the drop touches no dimension that a dashboard reads.
The other options were not chosen for these reasons:
on-run-startleaves the race while each node has its task, and lets adbt testby hand move the set in the middle of a run.on-run-startwith a lock until the watcher mode adds the lock that ADR-0038 removes, for a stretch of the work that no release would ship, and leaves the case of dbt by hand.- The newest load for each account loses days with a green run.
- The newest load among the pinned only lets an older load that ended late replace days that the core has accepted.
- Delete, then mark can read a load with no rows as empty days, and only G1a stands between that and an empty core.
- Mark and delete in one transaction is not possible:
transformermarks,loaderdeletes, and neither gets the right of the other (ADR-0035). - The tests of the sources with a
wherestill fail while a table does not exist, and they check what L1 checks anyway.
Consequences
- Good, because the race is gone while each node has its task, and not only from the watcher mode on.
- Good, because the checks that passed on the stand are the ones that the code will take: each of the three ways of losing data has a repeatable case.
- Good, because a failure between two steps of the cleanup leaves rows that L1 no longer reads, and a repeat removes them.
- Good, because a person may run dbt by hand on a running stack.
- Bad, because a stopped source is not a red run, so its sign is Ops and the alerts.
- Bad, because a red run holds back the acceptance and the cleanup of the healthy sources too, until a run succeeds.
- Bad, because a new column in a raw table still waits for the open transaction of a model, and fails after the
lock_timeout: "no lock to wait on" of ADR-0038 holds for rows. The retries of the DAGs of the loads cover it. - Bad, because the
LOCK TABLEof the cleanup waits for a file of a load in progress, up to thelock_timeout. - Bad, because the
DROP TABLEof a journal takes ACCESS EXCLUSIVE on its parent, so a write to the journal may wait up to 2 seconds. - Neutral: the DAG of the transform has one task more.
Confirmation
A stack test puts a load of the demo between two tasks of dbt, and not only inside one, and the result of the run does not change, while the next run takes the load. Another fails the cleanup between its steps and repeats it. The code review checks that no test reads a raw table.
More Information
- Implementation status, as of November 2026: the pin, what L1 reads, the acceptance and the cleanup are in place, with one task of dbt for each node, as the watcher mode is deferred to v0.6.0 (ADR-0039). The task
pinof the DAG of the transform, after the settings and before every other task of dbt, runsdbt run-operation pin_loads(dbt/macros/pin_loads.sql) astransformer: in one transaction, oneinsert ... selectfor each source copies intocore.loadsthe rows of the journal_agoranomos_loadsof each load that_dlt_loadshas withstatus = 0, withon conflict do nothing, and passes over a source whose tables do not exist; thencore.ensure_partitioncreates the partitions of the facts for the months of the pinned windows, and those of the journals of restatements for the current month and the next, in UTC, each in a transaction of its own. The macro refuses to run withoutDBT_JOB_RUN_ID, which the DAG sets, so dbt by hand pins nothing. L1 reads, for each account and day, the newest load among the pinned and the accepted ofcore.loads, with the load ids compared as numbers, rows only from the pinned, and of the accepted only those newer than the oldest pinned load of the account (pinned_loadsindbt/macros/newest_load.sql), through the tablestaging.stg_loads__newest_days, which each run of the transform fills and analyzes before the staging models of the facts. Once every task of dbt of the run has succeeded, by the ruleall_success, the taskacceptmarks asacceptedincore.loads, astransformer, withaccepted_at, the rows that this run or an earlier one pinned, up to the latestpinned_atof the loads of the days of the run (staging.stg_loads__newest_days), but every row of an account ofstaging.int_g1_failures, the accounts of a stopped source among them, and in the same transaction takes the days of the accepted loads out ofstaging.int_window_days, whose model joins the pinned loads only, so that dbt by hand after the run writes no day from rows that L0 no longer holds. Then the taskclean_raw, asloader, takes each table of each raw schema with a journal of loads, with its child tables, underLOCK TABLE ... IN ACCESS EXCLUSIVE MODEwith alock_timeoutof 10 seconds, and runsTRUNCATEwhen no row stays, and else, or when the lock does not come, deletes byDELETEthe rows of the accepted loads, by load and account, the account of each table as its staging model reads it, or by whole load for a table that names none, and the rows of a load with no row in_dlt_loadsand none incore.loadswhose_dlt_load_idis more than a day old, longer than thedagrun_timeoutof every DAG of the loads; it leaves_agoranomos_loadsand the tables of dlt alone. Each task retries twice, a minute apart (airflow/dags/agoranomos_pipelines/acceptance.py). Stack tests check that a run accepts what it read and leaves no row of it in L0, that a repeat of either step finds nothing, that an account in quarantine keeps its rows and its pinned load while the other account of the load loses its rows, that a run after the cleanup keeps every entity, fact and day, that a failure between the two steps loses nothing, and that a load in progress loses no row while the cleanup waits for its lock (tests/stack/test_raw_cleanup.py). The stop of a source is in place: a source with more than half of the accounts of its pinned loads in quarantine stops by a filter in SQL, and a test warns (dbt/macros/quarantine.sql, ADR-0035). A stack test lets a load of the demo end after the pin of a run and runs its tasks of dbt again: they do not read the load, and the next run does (tests/stack/test_demo_transform.py). No test reads a raw table. The DAGs of the loads of the demo and of the rates of the ECB retry a task twice with a wait that doubles (retry_exponential_backoffof 2.0, a multiplier in Airflow 3.3.2), which a new column that waited past the lock timeout needs, and end a run within an hour (dagrun_timeout), the shortest cut of the cleanup of unfinished loads; the DAG of Google Ads retries in the same way, after 10 minutes and then 20, and ends a run within seven hours, its three tries of up to two hours and the waits between them, the cut of the cleanup of its unfinished loads. The DAG of maintenance deletes the accepted rows ofcore.loadspast 13 months, but those that staging or the next window of an account reads, and none whose row_dlt_loadsstill holds, which the pin would take again; and it drops the months of the journals of restatements past 13 months throughcore.drop_partition, tries a refused month four times, 15 seconds apart, and then leaves it to the run of the next day, with no failure (airflow/dags/agoranomos_pipelines/maintenance.py). - The facts behind this record: the runs of 1 November 2026, with dlt 1.31.0, dbt-core 1.12.6, dbt-postgres 1.11.0 and PostgreSQL 18.6, are in "More Information" of ADR-0035, ADR-0036, ADR-0038 and ADR-0039. The numbers in the context above are from those runs.
DbtRunOperationLocalOperatorexists in Cosmos 1.15.1. A load id is a time in seconds as text, with fractions of different lengths, so it compares as a number. - Not checked yet, and to be checked with the code:
- A task before the group of the watcher mode: the stand ran a task after it, and dbt by hand for the pin; the stack tests run the task before the tasks of one node each.
- The bound of L1 on the accepted loads, to those newer than the oldest pinned load of the account, as a bound by overlapping windows would unroll about 180 windows of 90 days for each account (#191): it ran on a throwaway warehouse with loads written by hand, and the stack tests, which accept the loads of each run, let no older load end after a newer one was accepted.
- Related decisions: at acceptance this record replaces, in ADR-0038, "in
on-run-start" and "the newest for each account" of "Pinning", and "a table that a cleanup has emptied gets aTRUNCATE"; in ADR-0035, the order of "The way of a load"; in ADR-0039, "the tests of the sources" in the order of "The transform" and "detaches and" in "The DAG of maintenance"; and in ADR-0044, "By partition" forcore.loads. The rest of those records stands. - Revisit this record when a release of Cosmos or dbt gives a pin that holds across the calls of one run, or when a load needs to be accepted in part of a window.