03 Data ownership
Status: Current Last verified: 604c95a
One PostgreSQL database holds every table; each table has one owner that writes it, and the others only read it. The owner's schema holds the table (ADR 0004, schemas and roles). Columns, keys and indexes: generated/schema.md. Isolation between accounts: MULTI_USER_ARCHITECTURE.md.
Where data lives
| Kind | Where | Why |
|---|---|---|
| Repository contents (raw) | The user's storage only: their git remote, folder or bucket | The platform never needs the code; keeping it out is the strongest privacy property. |
| Worker decision state (intermediate) | worker_repo_state (self-hosted), or a SQLite file on a linked machine | About one row per repository; the dashboard and the schedule read it. |
| Runs, results, logs (processed) | The tables below | What the dashboard, the assistant and the MCP server show. |
| Embeddings (derived) | embedding_* | Rebuilt from the rows they index; nothing is lost when they are dropped. |
Owners
| Schema | Tables | Written by | Read by |
|---|---|---|---|
identity | accounts, agents, api_tokens, agent_link_requests, audit_events | API (src/platform/tenancy) | API, Observatory (account state, through identity.accounts_v1) |
runs | backup_runs, backup_results, backup_run_errors, backup_result_errors, execution_logs, analytics_snapshots | The worker through src/platform/runstore: directly when self-hosted, through the API's /v1 intake when linked | API, Observatory (through the insights API since B7a; its grants go in B7b) |
runs | worker_repo_state, worker_run_lease | The worker (runstore), or the API's /v1 intake for a linked worker's lease | API, the worker |
runs | backup_run_requests | Queued by the runs service (POST /api/requests, which the MCP server calls too) through runstore.QueueRequest: one pending request per account and target (a unique index, 000022) and the manual-run limit of the account's plan; claimed and finished by the worker | API |
runs | account_versions | Triggers on backup_runs and backup_run_requests (000021), never code: a counter per account of the changes a reader notices | The runs service (/v1/schedule's ETag), insights (whether a rollup is current) |
runs | outbox | runstore.CompleteRun (the API's intake, or a self-hosted worker as ghb_worker) and the abandoned-run reaper: run.completed and run.failed, in the run's transaction (000023, ADR 0005); the runs service's missed-run check: run.missed, once per account and missed slot (ADR 0017) | The runs service's relay, which publishes them as jobs; notify, through the view runs.events_v1 (000041) |
insights | account_rollups | Insights (src/platform/rollups): recomputed by the insights.rollup job after a run ends, or when a read finds the row behind account_versions (000024) | The API's dashboard, metrics and repository count |
runs | backup_fixes, backup_run_fixes, backup_fix_commits | The runs service (POST and PATCH /api/fixes, runstore.RecordFix), for the MCP server, the Observatory and the dashboard; a repeated Idempotency-Key answers with the stored fix (000028) | API, Observatory (through the insights API since B7a; its grants go in B7b) |
observatory | ai_chat_sessions, ai_session_metadata, ai_chat_messages, ai_tool_calls, ai_tool_call_args, ai_tool_call_errors, ai_tool_confirmations, ai_reports, ai_openrouter_credentials, investigations, investigation_errors | Observatory. ai_reports keeps recipients, sent_at and error_message only for old binaries: nothing writes them, and 000043 cleared the addresses, send times and mail errors of reports sent before ADR 0020 | Observatory |
observatory | embedding_generations, embedding_chunks, embedding_chunk_metadata, embedding_jobs, embedding_job_errors | Observatory, from its own background jobs (ADR 0013): a job holds the text to embed, read through the insights API, and its account until the chunk has it (000029). Identity deletes an account's jobs and chunks with the account, the API's retention deletes chunks whose source row is gone, and the Observatory hourly deletes jobs of accounts that no longer exist | Observatory |
runs | research_consent | The runs service (/api/me/research, ADR 0012); for one release also a trigger mirroring writes of identity.accounts.research_opt_in_at (000026) | runstore (the intake and a self-hosted worker, FOR SHARE while storing samples) |
identity | outbox | Identity: account.deleted with the account's deletion (000027) | Identity's relay, which publishes it as runs.account_deleted, insights.account_deleted and notify.account_deleted jobs |
notify | channels, rules, rule_channels | notify (/api/alerts, ADR 0017): an account's alert channels (a Slack, Discord or webhook URL and a webhook's signing secret, sealed) and its rules (event types to channels, quiet hours, a minimum interval), 000040; 000042 deleted the e-mail channels and their address and confirmation columns (ADR 0020) | notify |
notify | events, deliveries | notify's jobs: the events it alerts on, copied from the producer's view, and one delivery per event and channel (UNIQUE (event_id, channel_id)) with its status and attempts, 000040 | notify (GET /api/alerts/deliveries) |
identity | revocations | Triggers on identity's tables only, in the transaction that revokes a token or an agent or disables or deletes an account (000025); rows older than a day are pruned by the same trigger | Every API process, through identity.revocations_v1 |
runs | research_samples | runstore (the /v1 intake or a self-hosted worker), only for an account that opted in; deleted by the API on opt-out and by retention | Nobody through an API; research queries only |
identity | waitlist_signups | API (POST /public/waitlist) | The owner, by SQL |
billing | plans, cohorts, prices | Migrations (000031 seeds the launch catalogue, 000033 gives the founding cohort 500 seats); a price change is a new row, never an update of one somebody paid (ADR 0018) | Billing (GET /public/billing/plans, checkouts) |
billing | customers, subscriptions, payments, invoices, refunds, document_sequences, provider_refs, webhook_events, entitlement_versions | Billing (src/backend/github-backup-api/billing): checkouts and the payment providers' webhooks, verified and recorded once per provider event id (000030) | Billing; identity reads the entitlement through billing.entitlements_v1 |
billing | outbox | Billing: billing.subscription_activated, subscription_renewed, payment_failed, subscription_ended, entitlement_changed and renewal_due, in the transaction of the change (000030) | Billing's relay, which publishes billing.entitlement_changed as identity.entitlement_changed jobs |
public | schema_migrations | The migration runner (src/platform/dbmigrate) | Every Go service at startup |
platform_cache, platform_rate_limits, platform_locks, platform_jobs | The PostgreSQL adapters of the ports (src/platform/ports/pgports), in any Go service whose setting picks them: today RATELIMIT_BACKEND=postgres in the API (the MCP server has no database and counts in process instead). The cache and the counters are UNLOGGED: a crash empties them | The same services |
insights.account_rollups is derived like the embeddings: no foreign keys, and deleting a row only makes the next read rebuild it.
notify's tables have no foreign keys to identity.accounts either: a trigger (notify.require_account, like runs.require_account) refuses a row of an account that does not exist.
Events name at most 100 failed repositories and never reach platform_jobs: a job carries the event's id, its type, the account and the run. Published events are deleted after 7 days, unpublished ones after 30 (and each write prunes its own account's), so a database that no runs service relays stays bounded.
observatory.embedding_indexing_checkpoints exists (migration 000002 created it) but nothing reads or writes it. Removing it takes a new migration, which nobody has written yet.
Every tenant table carries account_id BIGINT NOT NULL with no default, and every query filters by the caller's account. Child tables (errors, metadata, tool-call arguments) are reached only through their scoped parent.
Deleting an account crosses the owners by event (ADR 0004, 0005): identity deletes its rows and, until B7b, the Observatory's (its embedding jobs by account_id since B7a) in one transaction with an account.deleted event in identity.outbox (000027); the runs service deletes the account's runs rows (in batches, with a second sweep 5 minutes later for writes still in flight), insights its rollup and notify its channels, rules, events and deliveries (with a second sweep too) when their jobs run. Each consumer first checks that the account is gone from identity.accounts_v1, since any role may insert a job. Since 000027 runs has no foreign keys to identity.accounts: a trigger (runs.require_account, owner's rights, no lock) refuses a row of a missing account with the same error a key gave.
Schemas and roles
Migration 000019 moved every table into the schema of the service that writes it: identity, runs and observatory (insights holds the rollups since 000024, notify the alerts since 000040). public keeps schema_migrations, the platform_* tables of the ports, and a view with the old name of every moved table, so a query that names no schema still works. The Go services (the API, the MCP server, the worker, through runstore, tenancy and the ports) name every table's schema, and their tests as a service role run with an empty search_path, so a query that does not fails there; the Observatory still goes through the views until B7, and a later migration drops them. The views are security_invoker: the caller's grants on the table decide.
Runs publishes runs.events_v1 (000041): its outbox's events, envelope and data, by event_id, read by notify to say what a failed or missed run was. Like identity's views it runs with its owner's rights, and roles.sql allows reads only.
Billing publishes billing.entitlements_v1 (000030, 000033): what an account's subscriptions grant now, the plan (pro, or none without a paid period: there is no free plan, ADR 0022) and until when, with the account's entitlement version. Identity reads it to set identity.accounts.plan (ADR 0018).
Identity publishes three read-only views: identity.accounts_v1 (an account) and identity.token_lookup_v1 (a token with its account's and agent's state), migration 000020, and identity.revocations_v1 (tokens and agents revoked and accounts disabled or deleted in the last day, 000025); every other service reads accounts, tokens and revocations only through them, with tenancy.Resolver. They run with their owner's rights, so a reader needs only SELECT on the view, and a column grant limits what it sees: no service outside identity reads the email, since nothing sends mail (ADR 0020). A change a reader would notice is a new _v2 view beside the first.
src/platform/dbmigrate/roles.sql gives each service a database role limited to its own schema and the few reads and writes it still makes in another, each listed below with the pull request that removes it. It runs after the migrations of every run and describes the whole state, so a grant taken out of the file is revoked by the next run.
| Role | Login | May |
|---|---|---|
ghb_identity, ghb_runs, ghb_insights, ghb_notify, ghb_billing | no | Groups: write their schema (ghb_insights reads runs, and writes insights; ghb_notify writes notify and reads runs.events_v1; ghb_billing reads three columns of runs.backup_runs for the refund rule; ghb_identity reads billing.entitlements_v1) |
ghb_api | yes | The bundle (github-backup-api): member of the five groups; reads and deletes the Observatory's rows (account deletion, retention) |
ghb_identity_svc | yes | github-backup-identity: member of ghb_identity, which deletes an account's rows in observatory until B7b; runs and insights delete theirs on account.deleted |
ghb_runs_svc | yes | github-backup-runs: member of ghb_runs; reads identity's views (sessions, tokens, revocations, an account's plan for retention and limits); writes research consent and its audit events (temporary INSERT on identity.audit_events); deletes orphan embedding chunks (until B7b) |
ghb_insights_svc | yes | github-backup-insights: member of ghb_insights (reads runs); reads identity's views, revocations included |
ghb_notify_svc | yes | github-backup-notify: member of ghb_notify; reads identity's views, revocations included, like insights |
ghb_billing_svc | yes | github-backup-billing: member of ghb_billing; reads identity's views, revocations included |
ghb_worker | yes | Member of ghb_runs (all of runs, fixes and requests included); reads and locks research consent in runs.research_consent but cannot change it; reads none of identity's tables; never the schema itself |
ghb_observatory | yes | Writes observatory; reads identity's account view (id, disabled); claims its kinds from public.platform_jobs; may still read the runs tables and the fixes (B7b removes it), but since B7a reads runs and fixes only through the insights API and records fixes through the runs service |
ghb_mcp | retired | Since B6 the MCP server has no database URL. migrate no longer creates the role and strips the grants of one that exists; drop it (06) |
Grants that cross a schema are temporary, and the pull request that moves the access behind an API or an event removes them: the runs service's INSERT on identity.audit_events for consent's audit events (until identity records them from an event), the trigger mirroring identity.accounts.research_opt_in_at into runs (one release), the Observatory's reads of runs (unused since B7a; B7b removes them), identity's deletes in observatory, the runs service's deletes of an account's embedding jobs and its chunk deletes (B7b, which needs the owner's decisions on the deletion promise and on old chunks). The services that authenticate requests outside identity (runs, insights) read identity's views and write only the two columns that record a token's use; the worker, which shares ghb_runs, reads no tokens. The exact list, by role and table, is src/platform/dbmigrate/testdata/role_grants.golden (table, column, schema and database privileges), which a test compares with the database.
Without the roles nothing changes: every service keeps using the owner's URL. migrate --roles creates them (without a login, so an operator enables each one: 06 operations). A service role with more power than its grants (SUPERUSER, CREATEROLE, CREATEDB, BYPASSRLS, REPLICATION, or another role's membership) stops the run; a missing one is skipped with a warning.
Retention
The API deletes in batches of 1,000 rows (SKIP LOCKED), hourly, and only while the database is already in use (jobs.Periodic), so it never wakes a database that scaled to zero.
| Data | Kept |
|---|---|
execution_logs | 30 days |
| Runs, with their results, errors, logs and snapshots | 365 days, for every account with a plan or without one (ADR 0022) |
agent_link_requests | 7 days after they expired |
Embedding chunks whose log, result or run (run_failure) is gone | deleted |
observatory.run_failed jobs no Observatory claimed | 30 days while waiting (runs' retention, so a deployment without an Observatory keeps the queue bounded) |
Text queued in observatory.embedding_jobs | until its chunk is stored (then NULL); jobs of a deleted account at once, with the account, and hourly for any queued after |
research_samples | 365 days, for every account; all of an account's samples go when it opts out |
runs.outbox | 7 days after the relay published an event, 30 days if it never did (every account, the owner's included) |
identity.revocations | a day (a deleted account's id stays that long, nothing else of it) |
billing.webhook_events | the delivery record for good (it makes a redelivery a no-op); the body 90 days |
| Billing's payments, invoices and refunds | for good, the account's deletion included: tax law keeps them (in India 8 years from the financial year's end); the billing details used for new invoices go with the account |
identity.outbox | 7 days after the relay published an event; until then, however long it waits (a lost account.deleted would leave the account's history behind) |
notify.events with their deliveries, and channel tests | 30 days, for every account, the owner's included: a record of what was sent, not the backup history |
platform_rate_limits (keys hold client addresses and account ids), platform_cache | Until they expire (a counter when its window ends), then deleted a batch at a time as the adapters run |
platform_locks, dead platform_jobs | A day after they expired; 30 days after the job died |
| The deployment owner's account | never pruned, apart from research samples |
Deletion and export
| Request | Deletes or returns | Code |
|---|---|---|
DELETE /api/me | At once: the account, its identity rows and, until B7b, its Observatory rows (the queued embedding text included), with an account.deleted event (the answer lists the services still to delete as pending). Within about 12 hours (the runners pass at least that often without traffic): its runs rows, its rollup and its alert channels, rules and deliveries, by their jobs | Identity, db/account_deletion.db.go; runs, db/runs_deletion.db.go; insights, rollups; notify, notify/deletion.go |
DELETE /sessions/{id} | The chat's messages, investigations, tool calls (with arguments and errors), confirmations, and the embedding chunks and jobs cut from the messages and investigations | Observatory, delete_session |
DELETE /sessions/{id}/messages/{message_id} | The same, for one turn | Observatory, delete_session_message |
GET /api/me/export | The account row and identity's tables as JSON (version 2), never token hashes | Identity, db/account_export.db.go |
GET /api/runs/export | The account's backup history (runs, results, logs, snapshots, fixes, requests, worker state, research consent and samples) as JSON | Runs, db/account_export.db.go |
GET /me/export | The assistant tables as JSON, never the OpenRouter key | Observatory, data/account_export.py |
Chats deleted before this existed left their investigations behind (unlinked from the session); they go when the account is deleted.
Migrations
- The migrations live in
src/platform/dbmigrate/migrations/, numbered000000,000001and up, embedded in the Go binaries. A run is one transaction under an advisory lock (pg_advisory_xact_lock), so instances starting together queue up and a failed run keeps nothing. Only versions are recorded (schema_migrations), not file contents. - Who applies them: the API at startup by default;
github-backup-api migrateas a pre-deploy job (ghb deployon a self-hosted machine, or any deployment withMIGRATE_ON_START=false);github-backup-worker migrate(make migrate) by hand. A backup run never migrates. - Every Go service refuses a database that records a version it does not embed (
dbmigrate.ErrDatabaseAhead); the API withMIGRATE_ON_START=falsealso refuses one that lacks its versions. - Each change is additive first: add the new column or table, ship the code that writes both, migrate the data, and remove the old shape in a later release. The previous release must keep working on the new schema, because its processes keep serving while the new release migrates and starts (a service checks the schema only when it starts). It cannot start again on that schema, so a release that migrated is fixed forward or the database is restored (06 operations).
- Guards: role defaults from
000014(statement_timeout2 min,lock_timeout30 s,idle_in_transaction_session_timeout10 min); the runner lifts the first two for itself and gives each migration 30 s to get a table lock.000019takes the locks of every table it moves at once without queueing and retries within those 30 s, so it never holds one while it waits; if the tables stay busy it fails (SQLSTATE 55P03) having changed nothing.CREATE INDEXlocks its table even when the index exists, so a migration checks first (as000000and000016do). - Migrations that already ran anywhere are never edited, apart from comments; a fix is a new migration.
- Since
000019, a migration names the schema of every table it changes (runs.backup_runs), and of every table a catalog check looks at (table_schema = 'runs', notcurrent_schema(), which now holds only the views;TestCatalogChecksStayInCurrentSchema). A view's columns are fixed when it is created: a migration that adds a column recreates the view (CREATE OR REPLACE VIEW ... WITH (security_invoker = true), leaving outWITHresets it), and one that drops or retypes a column drops the view first. - A new table gets its grants in
roles.sql, in the same pull request;TestRoleGrantMatrixfails until the reviewed list includes them.