Keep four layers of data, and erase the raw layer once the core has accepted a load
Superseded in part by ADR-0046: the order of the steps of "The way of a load", as a load is now marked accepted before its rows are deleted, and by ADR-0047: "the DAG of maintenance runs amcheck from contrib" of "The protection of the core", as the maintenance runs no amcheck. The rest of this record stays accepted.
Context and Problem Statement
Today the raw tables of dlt stay for good, the staging models are views, and each mart is a table that dbt rebuilds whole on every run (ADR-0004, ADR-0005, ADR-0025). That fits the demo, a warehouse of 211 MB, of which the raw tables take 117 MB. It does not fit the limit of one server, about 147 million rows of statistics over 36 months (ADR-0040): a full rebuild on every run is out of the question at that size, a delete of merge without keys walks a whole table, and a failed test of dbt only stops what lies below a table that it has already written.
The maintainer set the frame in October 2026, before v0.5 and with no deployment that holds real data yet: strict layers, with data that flows only downstream and checks between the layers; raw data that arrives in temporary tables; a normalized core that keeps the data for good; wide views for the people and the agents who read it; and loads that are incremental from the first day.
Every source on this subject keeps a raw copy, because it can be replayed: Kimball's persistent staging, Data Vault, the bronze layer of a medallion, Fivetran, dbt. Their argument leans on a source that still holds the data, and an ad platform does not: Google Ads and Meta keep daily data for 37 months, Microsoft for 36, VK for 12, and LinkedIn for 6. Past that, the warehouse holds the only copy, whichever way the raw layer goes.
Which layers does the warehouse have, what does each keep, and what protects the data once the raw layer is gone?
Decision Drivers
- Data flows one way, and people, Metabase and agents read only the top of it.
- The core keeps all that a replay would need: every non-zero row and every loaded field, in the units of the source.
- A bad row stops as early as it can, and one bad account does not stop the others: with 200 accounts, one expired token must not leave 199 without fresh data.
- Data older than the window of an API exists only in the core, so no step of the pipeline may be able to damage it by mistake.
- No new long-running service, and nothing outside the official image of PostgreSQL (ADR-0003) without a reason stated in a record.
- Each decision can be checked: the sizes and the times below are measured, and what is not measured says so.
Considered Options
For the layers:
- Four layers, of which two are stored: a temporary raw layer, views over it, a permanent core and views over the core
- The raw layer kept for good in the warehouse
- A temporary raw layer with a cold archive of the files of dlt
- A stored clean layer between the raw layer and the core
- The layout of today, with the marts as tables that dbt rebuilds
For a load that fails its checks:
- The account goes to quarantine, and the others go on
- The whole run stops
Decision Outcome
Chosen options: "Four layers, of which two are stored" and "The account goes to quarantine, and the others go on", because the core and the journal of restatements cover what a raw copy is for, the checks count rows and sums before anything is erased, and a raw copy on the same server does not protect against the loss of that server.
In practice this means:
-
The layers.
Layer Schema What it holds How long Owner Who reads it L0 raw raw_<source>The rows of an API as dlt typed them, only loads that the core has not accepted, each under its _dlt_load_idUntil the core accepts the load loadertransformerL1 clean stagingViews of dbt, one for each raw table: types, the names of the common vocabulary, amounts as numeric(18,6), statuses, natural keysNot stored transformerdbt L2 core coreNarrow facts by grain, dimensions with versions, rates by day, the journals of loads and of restatements (ADR-0036) For good A role for the migrations; transformerowns its snapshotsdbt, opsL3 presentation martsViews of one grain under the names of today's marts (ADR-0037) Not stored transformerMetabase, MCP Toolbox, people Service opselementary, views of the journals, the health of the pipeline By retention (ADR-0044) transformerMetabase (Ops), Airflow -
The flow. L0 to L1 to L2 to L3.
opsreads every layer and writes only itself. Nothing writes upstream: no post-hook changes a source, and no view ofmartsreads L0. - The way of a load. dlt writes the window of each account into L0, and one run of dbt accepts it into the core (ADR-0038, ADR-0039). A task after that run deletes the rows of the accepted loads, only when the run has succeeded, and marks them accepted in
core.loads. It is idempotent: a repeat finds nothing. A load that a failed run did not accept stays, and the next window starts from the last accepted day of the account, so nothing is lost. -
The gates. The marts are views, so what reaches the core is visible at once, and no gate can stand between L2 and L3 for its rows. The hard checks therefore run on L1, before the write.
Boundary What it checks How When it fails G0, source to L0 The type of each column, the retries of HTTP, a whole package schema_contractwithdata_type: freeze, the HTTP client of dlt,_dlt_loadsThe task fails and Airflow retries it; L0 is not accepted, and the next window is wider G0a, against the platform Cost, clicks and impressions by account and day, from a separate request at the level of the account, against the loaded rows of campaigns, within the tolerance of the platform One more request for each account and load, kept in core.platform_totals_dailyThe account goes to quarantine G1, L0 to L1, before the write A whole load ( status = 0), keys that are unique and not null, a known account for each fact, a currency of ISO 4217, a time zone of IANA, at most six decimal places in an amount, counters of zero or more, dates inside the windowThe view int_g1_failures, with one row for each account and reason, read by the modelcore.account_quarantine; tests of dbt for the schema and for shared codeThe account goes to quarantine; a test of the schema stops the run G1a, against a collapse An empty window of an account that had spend, or a sum that fell more than a threshold, against the core A comparison before the write The account goes to quarantine: a window replaces whole days, so a lost token would otherwise erase them G2, L1 to L2, during and after the write PK, NOT NULL, CHECK and a foreign key only to the registry of accounts, on the tables of the core; equal sums of spend, clicks and conversions in L1 and L2 by account and day; the share of entities with no row of a dimension Constraints, and singular tests of dbt A constraint rolls back the transaction of the model; a failed sum leaves the run red and L0 in place, and Ops shows the difference by account G3, L2 to L3 An aggregate table, where there is one, equal to its view; a rate for every day and currency of a fact; a description for each column Singular tests of dbt errorfor sums,warnfor descriptionsSoft signals Spikes and drops of spend and conversions, missing days, more clicks than impressions, ad groups above their campaign by more than a cent Anomaly tests of elementary and generic tests at warnA row in Ops; nothing stops -
Quarantine. An account that fails G0a, G1 or G1a leaves this acceptance: the core does not take its window, L0 keeps its rows, its data stays as it was, and Ops,
./agoranomos statusand the alerts name the reason (ADR-0045). Only an error of the schema or of shared code stops a whole run. More than half of the accounts of a source in quarantine means a failure of the platform or of the connector, so that source stops and the others go on. An error of the API on one account is caught on that account and written to the journal of the load. - The protection of the core.
- A role for the migrations owns the schema
coreand its tables. On the facts,transformerhas SELECT, INSERT and DELETE, and on the dimensions and journals UPDATE too, with no DROP and no TRUNCATE. This is more than a guard against--full-refresh: when a type in a model is wider than the type of the table, dbt runsdrop column ... cascadeby itself, and only the lack of the right stops it. - A partition older than the window of restatements gets a row trigger that refuses DELETE. Only
data forgetand a repeated load of history lift it, with a setting inside their own transaction. INSERT stays allowed, so the first history of a new account goes through. - The journal of restatements keeps the earlier values of each changed row for 13 months, so a bad load can be rolled back from it.
data_checksumsstay on, and the DAG of maintenance runsamcheckfrom contrib.
- A role for the migrations owns the schema
- The roles.
loaderownsraw_*and writes the journal of a load there, and dbt lifts it intocore.loads, so that the loader writes nothing into the core; readingcore.loadsto compute a window is control, not a flow of data.transformerownsstaging,martsandops, readsraw_*, and has incoreUSAGE, CREATE for its snapshots and the rights above.metabase_readerreadsmartsand the views ofops, andai_readerreadsmartsonly (ADR-0043). The views run with the rights of their owner, so neither needs a right oncore.
What this does not cover, and what is accepted:
| What happens | What saves the data |
|---|---|
| Rows are lost in processing | The check of sums stops the cleanup |
| A wrong unit, currency or status | A fix in the core, which holds the values of the source |
| An error found while the API still serves the data | A reload from the API |
| A field that was never loaded | Nothing: a raw copy would not have it either |
| An error that passes the checks and is found after the window of the API | Nothing, without an archive |
| A lost server | Only a copy of a backup outside the server (ADR-0044) |
The other options were not chosen for these reasons:
- The raw layer kept for good is the double of the core for a case that a copy on the same server does not cover, and it needs merges or deletes over tables that keep growing. At the limit, a compressed archive would take about 6 to 9 GB over 36 months. It can be added later with no loss for the last 37 months, while the APIs still serve them.
- A cold archive of the files of dlt would add a volume and a rotation for the same narrow case. The rows of a failed load stay in L0 until it is accepted, which is what the analysis of an incident needs.
- A stored clean layer would write and delete each row twice for nothing: dbt builds the core from the views of L1 in one transaction for each model, and the tests of the views run before that.
- The marts as tables are measured in ADR-0036.
- Stopping the whole run leaves every account without fresh data because of one of them.
Consequences
- Good, because the warehouse has one place that holds the data for good, and the layers around it can be rebuilt from the core or the API.
- Good, because the storage and the work grow with the data that changes, not with the whole history.
- Good, because one expired token costs one account, and says so.
- Good, because a mistake in a model cannot drop a table of the core or delete a closed month.
- Bad, because this departs from every source on the subject. An error that passes all the checks and is found after the window of an API cannot be fixed.
- Bad, because the core becomes the one thing to protect: the gates and the backups are its only defense.
- Bad, because a sum that fails after the write leaves the wrong rows visible until a repeat. It is a net for an error in the write itself, not the main line of defense.
- Bad, because the accounts in quarantine have older data than the rest, and a dashboard that sums them does not show it by itself: Ops and the alerts do.
- Bad, because a schema with a role for the migrations asks more of a contributor than a table that dbt creates.
More Information
- Implementation status, as of November 2026: in place for v0.5, but for the parts that the end of this item names. On the side of the connector, an error of the API on one account of Google Ads is caught at the account, written to the journal of the load with the status failed and the text of the error, and the load takes the other accounts and fails at the end with the list (
airflow/dags/agoranomos_pipelines/google_ads.py). The protection of the core stands on its tables, of which the pin of the transform writescore.loads, and each run of the transform the dimensions, the registries, the facts and the journals of restatements, by incremental models of dbt that can neither rebuild nor change a table (ADR-0036): the migrations ofpostgres/migrationscreate them asmigrator, which owns the schemacoreand every table in it but the snapshots of the versions of the entities, whichtransformercreates and owns there;transformerhas SELECT, INSERT and DELETE on the facts, and UPDATE too on the dimensions and the journals, with no TRUNCATE and no DROP; and a row trigger on each fact, which every partition of the fact takes, refuses the DELETE and the UPDATE of a row of a month that ended more than 92 days before the current date, unless the settingagoranomos.thawholds the ID of the current transaction, while INSERT stays allowed (postgres/migrations/0005_create_the_facts.sql); the model of each fact sets it in the transaction of its write only when the write is to replace a row of a frozen month, as when a load of history comes again, as a new history of the demo does (dbt/macros/facts.sql).loadermay readcore.loads, andmetabase_readerandai_readerhave no right incore. In L0, each load of a fact table lies under its own_dlt_load_id, with its journal, a task before the tasks of dbt pins the whole loads intocore.loads, and the views of L1 read the newest pinned load of each account and day (ADR-0038, ADR-0046). Quarantine is in place too, for G0a, G1 and G1a: before it writes the core, each run of the transform checks the data of each account that it reads instaging.int_g1_failures, one statement for each kind of data (dbt/macros/quarantine.sql), and an account that fails a check stays out of every dimension and every fact, and so of the marts, which are views over them, and has its load, reason and window incore.account_quarantine; G0a compares, for each account and day of a load of Google Ads, the impressions, clicks and spend of its campaigns with the totals of the account that the connector asks for in a request of their own at the level of the account (customer_stats), within the share of the platform inplatform_totals_toleranceof thevars, zero for Google, and each run writes those totals intocore.platform_totals_dailyfor the days of its windows, while the demo has none; G1a compares the spend of the campaigns of each load, and the sessions of each load of web analytics, with the core; a source with more than half of its pinned accounts in quarantine stops by a filter in SQL, and two tests warn, as the viewops.ops_quarantineand its card in the Ops collection do. G2 is in place too: after the write of each fact, tests of dbt compare its sums by account and day over the windows with those of staging, and the share of its rows whose entity has no row of a dimension withdimensionless_shareof thevars, 0.001, each an error. Of G3, a test checks after the write of the rates of the core that each day of the windows of the facts has a rate, an error, and an account whose currency has no rate goes to quarantine (ADR-0042). The acceptance and the erasure of L0 are in place too, in the order of ADR-0046: once every task of dbt of a run of the transform has succeeded, the taskacceptmarks as accepted incore.loads, astransformer, the loads that the run read, by load and account, but those of the accounts in quarantine, and the taskclean_rawdeletes their rows from L0, asloader, by load and account, and the rows of a load that did not end once it is a day old; a run with a failed or a skipped task of dbt accepts and erases nothing, so that a failed G2 stops the cleanup (airflow/dags/agoranomos_pipelines/acceptance.py). The marts are views over the core (ADR-0037). Not built: of the soft signals, the tests of anomalies of elementary, while the generic tests atwarnare in place;./agoranomos status, which does not name the accounts in quarantine, as Ops and the alerts do (ADR-0045); anddata forget, planned for v0.9 (ADR-0044). No load has reached a real Google Ads account yet, so the tolerance of G0a stays unchecked, as below. - The facts behind this record:
- Measured on real dbt and dlt on 31 October 2026, with PostgreSQL 18.6, dbt-core 1.12.6, dbt-postgres 1.11.0 and dlt 1.31.0, on a small project:
transformerwithout ownership gets a refusal on TRUNCATE, DROP of a table or a partition, DROP COLUMN and DROP SCHEMA; a duplicate key in a load rolls back the fact and the journal together;dbt build --full-refreshwithfull_refresh: falseleaves the table, its partitions and its rows as they were; with a healthy account, an account with a lost token, one with a bad currency and an empty day, only the healthy account changed; the row trigger refuses a DELETE of rows and lets an INSERT through; more than half of the accounts in quarantine stopped the run before the write of the fact. - Run on 1 November 2026 with the same versions, on two sources that share one fact and three of the four accounts of one source in quarantine:
- A test at
errorabove the fact stops the write, but of every source, the healthy one too:dbt buildskips all that lies below a failed test, and one table is one model. - A filter in SQL did what this record asks for, in a green run. It leaves a source with more than half of its accounts in quarantine out of the dimensions, the fact and the days of the pre-hook, so that source stopped with its healthy account and the other went on. Exactly half stopped nothing, and the next healthy load brought the source back.
- A failed test below the fact also lets the other source through, but a red run holds back the acceptance and the cleanup of the healthy source. So the sign of a stopped source is Ops and the alerts, not a red run.
- One load can be accepted for some accounts and wait in quarantine for others, so acceptance and the cleanup go by load and account, not by load.
- A test at
- Measured on 1 November 2026 with 1 million rows of the shape of
ad_stats, on Docker Desktop on a Mac, in five repeats under a limit of 3 GiB, with the settings of ADR-0040:COPYtook 1.88 s into a logged table, with 345 MiB of WAL, and 1.68 s into anUNLOGGEDone, with none; under 384 MiB, 2.08 s and 1.81 s. Through dlt, the step of the load took 2.30 s against 2.05 s of about 46 s in all. SoCOPYdoes not run into the WAL, and the raw tables stay logged.- dlt takes a table made beforehand as
UNLOGGEDand keeps it so, but any process of PostgreSQL that dies empties that table while_dlt_loadskeepsstatus = 0. The next run then reads empty windows: G1a stops an account with spend in the core, and an account with none is accepted with no rows and no sign. - Two other costs are larger, and neither risks the data: the
UNIQUEindex that dlt puts on_dlt_idtook 1.77 s and 86 MiB of WAL for each million rows, and aDELETEof accepted rows wrote 420 MiB of WAL, against about 30 KiB for aTRUNCATE.
- In one agency's production warehouse, a failure of reading by account lost up to 22% of the spend of a day with no sign. A check inside the warehouse cannot see that, so G0a asks the platform separately.
- Kimball: "Reports cannot access data from the staging area", and a staging area may be truncated with each process or preserved (The Data Warehouse ETL Toolkit, error event schema). Databricks keeps bronze to enable "reprocessing and auditing" (Medallion architecture), and Roelant Vos calls a persistent staging area an archive for regeneration (Data Vault versus the Persistent Staging Area).
- Google Ads keeps hourly, daily and weekly data for 37 months since 1 June 2026, and advises an export for anything longer (help, blog). Meta gives at most 37 months (insights).
dbt buildskips what lies below a failed test (build), so the tests protect the nodes below and not the table that was just written. On PostgreSQL, a row stops at the constraints of a table.
- Measured on real dbt and dlt on 31 October 2026, with PostgreSQL 18.6, dbt-core 1.12.6, dbt-postgres 1.11.0 and dlt 1.31.0, on a small project:
- Not checked yet, and to be checked before or with the code:
- The tolerance of G0a on each platform, which needs a live account: Yandex rounds by row, and TikTok cuts decimal places.
- The threshold of G1a, and the threshold of the share of entities with no row of a dimension.
- The time of
COPYinto a logged table on a cloud disk: the times above come from a Mac, while the volume of WAL does not depend on the disk.
- Related decisions: at acceptance this record adds the schema
coreto "Layers" of ADR-0005, and replaces the failure of a whole run on one account of ADR-0032, where an account goes to quarantine instead. The model is in ADR-0036, the views in ADR-0037, the load in ADR-0038 and the orchestration in ADR-0039. - Revisit this record when a real error passes the gates and is found after the window of an API, or when a platform offers a longer history.