Skip to content
MSO CloudDocumentation

ADR 0055 — Panel read model for record datasets

On this page
  • Status: Accepted (2026-10-20). Operator approved the new table and this ADR on 2026-10-20.
  • Date: 2026-10-20
  • Owners: Platform (tenant dataset plane).
  • Related: ADR 0038 D2 (RLS, ACL and backup cohorts of the dataset family), ADR 0040 D4 (materialise at rollup, never at read), ADR 0042 (packs as data), ADR 0044 D5/D6 (binding cache and its invalidation), §82 (brand-partitioned writes), §121 (a runtime writes only what its role is granted).

Context#

agg_org_dataset_period stores one (dim_key, value_key) per cell. A request that filters on one dimension and groups on another cannot be answered from it; the resolver reports the filter as ignored. Records carry only period_month, so there is no week or day grain. Dashboards that cross several dimensions at once (entity x group x item x week, or a day window around a campaign date) need every dimension of a record in one place.

Decisions#

D1 — One generic table, org_dataset_panel_rows

One row per record of a record dataset whose pack declares a panel block:

Column Type Meaning
id uuid Row identity.
org_id, brand_id, dataset_id uuid, NOT NULL Tenancy and owning dataset; org_id and dataset_id cascade.
period_month varchar(7), NOT NULL YYYY-MM of the record.
period_sub varchar(10), NULL W1..W5 or YYYY-MM-DD inside period_month; NULL = month grain. CHECK on the shape.
dims jsonb, NOT NULL dim_key -> value_key, every dimension of the record except the sub-period field. A NULL value is an unresolved source cell.
measures jsonb, NOT NULL measure_key -> integer for the panel's declared measures. A key is absent when the source did not carry it; never a fabricated 0.
schema_version integer The dataset's schema_version at write time.
is_sample boolean Copied from the record.
created_at timestamptz One value per rewrite.

Index: (org_id, brand_id, dataset_id, period_month). No GIN on dims: a reader scans one (org, brand, dataset) partition, bounded by D3.

measures is jsonb keyed by measure, not an array with a key list on the dataset: the key list would be a second source of truth that drifts on a pack upgrade, and the reader idiom already in use (PanelRow.m in @yng/metrics/dataset-panel) is a keyed record.

The table is generic. Possible consumers are the sales tracker (entity x group x item x week), HR (department x grade x location x month), finance (cost centre x account x scenario x month) and Content Intelligence corpora with several facets per row. Nothing in the table, the writer or the projection names a vertical, a dimension or a customer.

D2 — The declaration is pack data: datasets[].panel

A record dataset in a pack may carry a strict block (Zod: datasetPanelSpecSchema, @yng/metrics/panel-projection; field references checked in apps/web/src/server/dataset-packs/pack-schema.ts):

{
  "readModel": "panel",
  "subPeriodField": "week",
  "subPeriodKind": "week_of_month",
  "roles": { "entity": "shop", "group": "category", "item": "item_id" },
  "measures": ["value", "units"],
  "chatQuestions": [
    {
      "id": "top_groups",
      "view": "breakdown",
      "request": { "by": "category" },
      "vi": "...",
      "en": "...",
      "requires": { "views": ["breakdown"], "dims": ["category"] }
    }
  ]
}
  • subPeriodField names a declared dimension; subPeriodKind is week_of_month or day. Both or neither. A day value must be a calendar date inside the record's period_month.
  • roles maps free role names to declared dimensions. Readers and chat look a role up; they never hardcode a dimension key. A role may not name the sub-period field.
  • measures lists declared measure fields. Only listed measures are projected.
  • chatQuestions are suggested questions: view is a reader view key (the reader owns the closed set and drops a question whose view it does not serve), request is a partial view request the reader validates, requires.dims names roles or declared dimensions.
  • A derived dataset may not declare a panel.

The block is persisted where the whole pack already is: org_dataset_packs.content. No column on org_datasets and no config table. The writer reads the block from the org's active pack rows by dataset_key; two active packs declaring different blocks for one dataset is a refusal (D4).

D3 — Writer: full replace per (org, brand, dataset), in the rollup executor

refreshDatasetPanel (apps/worker/src/processors/dataset-panel-projection.ts) runs after the cell rollup of every dataset.rollup.refresh and once after a dataset.backfill. In one runWithRlsContext(org, brand) transaction it deletes the partition and inserts one row per record with a period_month, built by the pure projectPanelRows. A dataset without a panel block gets the delete only, so a dropped block leaves no stale projection. The regrain purge deletes the dataset's panel rows in the same transaction as its records and cells. The same records always produce the same rows (idempotent).

Row cap: 500,000 records per (org, brand, dataset) (ORG_DATASET_PANEL_ROW_CAP), counted before the rewrite.

Only yng_worker writes the table (SELECT, INSERT, DELETE); yng_app holds SELECT. The archive role carries it as portable.

D4 — Refusals are permanent and leave the partition empty

PanelProjectionRefusal is thrown for: more records than the cap (row_cap); a record with two values for one dimension, a missing sub-period or one of the wrong shape (unprojectable_records); two active packs with different blocks (conflicting_spec); a stored block that no longer parses (invalid_spec). The rewrite rolls back, a second statement empties the partition, and processDatasetJob routes the job to the DLQ on the first attempt. A reader then finds no rows and abstains instead of serving a projection older than the records.

D5 — Read-time aggregation is a recorded exception to ADR 0040 D4

A panel reader groups and filters org_dataset_panel_rows at read time inside RLS. This is the one exception to "materialise at rollup, never at read", bounded by:

  • one (org, brand, dataset) partition per read, capped by D3;
  • formulas stay server-side in @yng/metrics/dataset-panel*; no formula in SQL or in the client;
  • the reader applies the compensation gate (datasetMeasureReadDecision) to every measure key it returns, exactly as the cell resolver does;
  • one projection read per filter state, cached by the binding cache (ADR 0044 D5) under (org, brand, datasetVersion, filterHash) plus the viewer leg. datasetVersion combines the dataset's schema_version and the partition's created_at, so a rewrite changes the key.

agg_org_dataset_period stays the read path for every non-panel binding.

Consequences#

  • A refresh rewrites the whole partition, not only the refreshed periods. At the cap this is one transaction of up to 500,000 inserts; a dataset that needs more uses a narrower grain or a second dataset.
  • The panel reader and its widgets, the loader that installs a panel pack, and the chat catalogue that serves chatQuestions are separate changes that build on this table.