Data Models

For a contributor reading a service’s schema: pick the service page below; the tables every service shares (event_outbox, event_inbox, scheduler_runs, idempotency_keys, _sqlx_migrations) are described once on this page and only pointed at from the service pages. Canopy follows ADR-001: every service owns its own PostgreSQL database, no Postgres-level foreign key crosses a service boundary, and cross-service consistency is the orchestrator’s job (canopy-eligibility for program dispatch, canopy-applications for case lineage).

Per-service pages

canopy-portal has no database (ADR-026) and canopy-exchange owns only the shared event_outbox / event_inbox pair; neither has a page.

Shared message-queue tables (single home)

The event_outbox and event_inbox schema is single-sourced in crates/canopy-mq/outbox-migrations/ and copied verbatim into every DB-backed service’s migrations/ by cargo xtask outbox-migrations --write; cargo xtask outbox-migrations --check (the default action, run by cargo xtask validate) fails on drift, so one drainer and one janitor work everywhere (ADR-018, ADR-039). Per-service data-model pages list the eight generated files as one inventory line and point here.

The generated migration files

Eight files, applied in timestamp order alongside the service’s own:

  • 20260508000000_create_event_outbox.sql — event_outbox + event_outbox_unpublished_idx

  • 20260516000000_create_event_inbox.sql — event_inbox + event_inbox_unprocessed_idx (#433)

  • 20260518004851_event_outbox_lease_columns.sql — claimed_at, claimed_by + event_outbox_lease_idx

  • 20260713000000_event_outbox_hold.sql — hold_operation_id, hold_generation + event_outbox_held_idx (ADR-039)

  • 20260720000000_event_inbox_parking.sql — parked_at, park_reason, park_min_schema, queue_name + idx_event_inbox_parked (#1089)

  • 20260803000000_event_outbox_v7_pk_default.sql — drops the gen_random_uuid() default on event_outbox.id; every writer binds a UUID v7 (#1173)

  • 20260815000000_event_outbox_claim_order_idx.sql — event_outbox_claim_order_idx (#1201)

  • 20260904000000_event_outbox_parking.sql — parked_at, park_reason + event_outbox_parked_idx (#1230)

event_outbox

The transactional outbox: a producer writes the row in the caller’s transaction (Publisher::publish_tx) and the in-process OutboxDrainer (crates/canopy-mq/src/outbox_drainer.rs) flushes unpublished rows to RabbitMQ.

Columns

id (UUID v7, app-supplied, no default), routing_key, payload JSONB, enqueued_at, published_at (NULL until drained), attempts, last_error, claimed_at / claimed_by (drainer lease), hold_operation_id / hold_generation (ADR-039 hold key; NULL for every ordinary event), parked_at / park_reason (#1230 terminal state).

Row state (derived, never a status column)

published_at NOT NULL → published (janitor-owned); parked_at NOT NULL AND published_at IS NULL → parked (operator-owned); otherwise pending / claimable (drainer-owned). A held row (hold_operation_id NOT NULL) is skipped by the drainer until Publisher::release_held or Publisher::drop_held.

Partial indexes

event_outbox_unpublished_idx ((enqueued_at) WHERE published_at IS NULL), event_outbox_lease_idx ((claimed_at NULLS FIRST, enqueued_at) WHERE published_at IS NULL), event_outbox_held_idx ((hold_operation_id, hold_generation) WHERE hold_operation_id IS NOT NULL AND published_at IS NULL), event_outbox_claim_order_idx ((attempts, enqueued_at) WHERE published_at IS NULL — the claim order), event_outbox_parked_idx ((enqueued_at) WHERE parked_at IS NOT NULL AND published_at IS NULL).

Parking

a row whose publish fails for a row-culpable reason is parked once attempts reaches CANOPY_MQ_DRAINER_MAX_ATTEMPTS (default 10, outbox_drainer.rs); the parked count is a gauge and a /readyz surface; an operator replays it through POST /v1/admin/events/replay (crates/canopy-api/src/admin.rs), which unparks and resets attempts.

Retention

the janitor deletes published rows older than CANOPY_MQ_OUTBOX_RETENTION_DAYS (default 7, outbox_drainer.rs).

event_inbox

The consumer-side twin (#433): the Subscriber inserts (event_id, routing_key, payload) with ON CONFLICT DO NOTHING inside the handler’s transaction, so a redelivered envelope (same UUID v7 event_id) is acked without re-invoking the handler.

Columns

event_id (PK, the envelope id), routing_key, payload JSONB, enqueued_at, processed_at (NULL until the handler commits), attempts, last_error, parked_at / park_reason / park_min_schema / queue_name (#1089 parked state).

Row state (derived)

processed_at NOT NULL → processed; parked_at NOT NULL AND processed_at IS NULL → parked (a handler that does not recognise the delivery — unknown event type during a rolling deploy, or a schema_version above what the binary supports — parks it, and the per-subscriber unpark scanner re-offers it until a binary that understands it processes it); otherwise pending / in flight. A failing handler leaves processed_at NULL and bumps attempts; the subscriber nacks until its max_attempts, then routes to the queue’s .dlq on the canopy.dlq exchange (20260516000000_create_event_inbox.sql header).

Partial indexes

event_inbox_unprocessed_idx ((enqueued_at) WHERE processed_at IS NULL), idx_event_inbox_parked ((queue_name, enqueued_at) WHERE parked_at IS NOT NULL AND processed_at IS NULL).

Retention

the inbox janitor deletes processed rows older than the service’s inbox_retention_days (default 7) and never an unprocessed or parked row; documented once in Event-Delivery Protocol › Inbox retention.

scheduler_runs (single home)

Present in every service that runs a fenced daily scheduler (appeals, applications, enrollment, renewals). It is the wall-clock window fence behind canopy_db::window_fence::run_daily_fenced (#1211): columns (job_name, window_start DATE, started_at, completed_at), PK (job_name, window_start). A probe claims the (job, UTC-day) window with INSERT … ON CONFLICT DO NOTHING; the single winner runs the tick and every other probe — any replica, any boot time, any restart — skips. Rows are the fence, not history: a failed tick deletes its row so a later probe retries the window; a successful one is stamped completed_at; growth is one row per job per day with no automated pruning. Single-sourced in crates/canopy-db/scheduler-migrations/20260905000000_create_scheduler_runs.sql and parity-gated by the same cargo xtask outbox-migrations family gate.

idempotency_keys (single home)

Present in every service database as the fleet’s idempotent-POST cache, and the one table whose DDL is not a file in the service’s migrations/ directory: canopy_db::ensure_idempotency_schema owns it (#1463), run under pg_advisory_xact_lock from both migration arms — the in-process migration phase of canopy_api::bootstrap and the deploy-time canopy-manage migrate apply. It is deliberately not a sqlx::migrate! migration: each service runs its own migrator against _sqlx_migrations, and a second migrator’s rows would be rejected by the first.

Base shape: cache_key (PK), response_status, response_body, response_content_type, created_at, with idx_idempotency_keys_created_at. The single-flight expand phase (ADR-016 expand/contract) adds state (default completed), request_hash, claim_id, lease_expires_at, response_headers and replayable, makes response_status and response_body nullable for a pending claim, and adds its state invariants as NOT VALID CHECK constraints so no full-table ACCESS EXCLUSIVE scan is taken on a compliance-gated table. Source: crates/canopy-db/src/idempotency_schema.rs.

Retention: canopy-api’s hourly cleanup deletes, 1 000 rows per statement, every row the claim treats as expired: created over 24 hours ago, and not a pending claim whose lease is still live. Terminal (completed, failed) rows and abandoned pending rows go, while a handler running past the window keeps its row (#2580; run_cleanup_batches in crates/canopy-api/src/idempotency.rs).

_sqlx_migrations

sqlx’s own migration-history table, present in every service; see Migrations for the forward-only contract (ADR-016).

Cross-service references

A cross-service FK drawn in a per-service ERD (for example caps_applications.household_id → canopy-persons.households.id) is application-level only — no Postgres FOREIGN KEY exists. The orchestrator supplies real IDs; a program DB cannot verify existence in another DB (ADR-025 for the validators that check it).

Currency

Migration SQL is the source of truth; a page is updated in the MR that changes the schema.

Edit this page · default