Skip to content
Source
Sign inSign up

Move a class of Durable Objects to Postgres

Site administration

Move a class of Durable Objects to Postgres

Some classes of Durable Objects kept their state in each object, and only that object read and wrote it. The forge moved them to Postgres, one class at a time. One class is left: the CI runs. Its flag picks where a new run starts, and each old run moves by itself once it ended, or as it is while it waits for an approval. This page tells the operator how to flip that flag, move the old runs and sort the run objects that no run names, and how the other classes moved.

21 min read

How the flag works:

  • The flag objects.ci_run in site_settings tells where a new CI run starts: object (in its CiRunObject) or postgres. Each Worker reads the flags again within 5 seconds, with no deploy.
  • A run never changes its home with the flag: a run that started in its object stays there until it moves, and a run that started on Postgres stays there after a flip back. The row of each run in ci_runs says home = 'postgres' when the run is on Postgres.
  • For 10 minutes after a flip to postgres, the start of a run that has no row in ci_runs yet first asks the run's CiRunObject. A run that started in its object just before the flip gets its row a few seconds later, and a start of it that comes again in that time stays in its object. Each ask leaves an empty CiRunObject. After the 10 minutes, a run with no row starts on Postgres, and no object is called.
  • The script object-move calls the route POST /_gitsrv/object-move of the web Worker with the gitd secret. The route runs five commands: status, flip, flip-back, audit and orphans. The script calls a command again until each Worker reads the flag, or until the audit is done. It prints one line for each call: the time, the class, the command, the HTTP status and the answer.

You need:

  • The gitd secret in ~/gitd-secret, from step 1 of the git servers setup. The script reads it from the file, so the secret is not on the command line.
  • psql, and PG_URL as in Delete the analytics of a person, for steps 3, 4 and 6.
  • For step 5, the login of wrangler on your computer (pnpm -C apps/web exec wrangler login), or a Cloudflare token that reads the Durable Objects of the account in CLOUDFLARE_API_TOKEN.
  • A go for the flip, and a go for the moves.

Run the commands from the root of the Source repository, in Git Bash or a Linux shell.

1. Read the flag

pnpm -C apps/web object-move ~/gitd-secret ci_run status

The answer has "mode":"object" or "mode":"postgres".

2. Flip the flag

  1. Flip the class:

    pnpm -C apps/web object-move ~/gitd-secret ci_run flip

    The script calls the route again until each Worker reads the flag, about 6 seconds. Then the answer has "mode":"postgres".

  2. Watch the first runs that start after the flip: a run of a pull request and a run of the default branch. Each one plans, gets its slots, runs its jobs and ends, and its row says home = 'postgres':

    psql "$PG_URL" -c "SELECT status, count(*) FROM ci_runs WHERE home = 'postgres' GROUP BY status"

    Then rerun a failed job of a run on Postgres, and cancel another one.

  3. If the runs on Postgres have a problem that you cannot correct, flip back:

    pnpm -C apps/web object-move ~/gitd-secret ci_run flip-back

    New runs start in their objects again. The runs that started on Postgres stay there, and each read and write of a run follows its home.

3. Move the old runs

The route does not move the CI runs. Each run moves by itself, once it ended, or while it waits for an approval. While the flag objects.ci_run is postgres and the site setting ci.run_moves is on, the sweep "CI run moves" of the CI cron tries up to 50 old runs each minute. One sweep runs at a time, under the work lease ci-run-moves: a sweep that starts while another one runs does nothing. Each minute it starts one page further in the list of the runs left, so a run that does not come to rest keeps no other run waiting.

  1. Check that new runs start on Postgres:

    psql "$PG_URL" -c "SELECT value FROM site_settings WHERE key = 'objects.ci_run'"
    psql "$PG_URL" -c "SELECT count(*) FROM ci_runs WHERE home = 'postgres'"

    The flag must be postgres. While it is not, the sweep moves nothing.

  2. Turn the moves on. Each Worker reads the setting within 60 seconds:

    psql "$PG_URL" -c "INSERT INTO site_settings (key, value, updated_at) VALUES ('ci.run_moves', 'true', 0) ON CONFLICT (key) DO UPDATE SET value = excluded.value"
  3. To stop the moves, set ci.run_moves to false. A run that moved stays on Postgres: no run moves back.

How a run moves:

  • Its CiRunObject puts its fence up. From then on the object refuses each write of the run, and the caller tries again.
  • The sweep reads the rows of the run from the object. The run must be at rest: it ended (completed or cancelled) or waits for a person to approve it (waiting_approval: an untrusted run that no person approved, so none of its jobs started), no job of it runs, no slot of it holds a lease or owes a release, and the index has its last version. Else the fence comes down, and a later sweep tries again.
  • In one transaction, the run's row gets home = 'postgres', the next version, the run that started it (parent_run_id, null when that run is gone) and moved_at, the time of the move. Its cells, slots, audit trail and fixups go to Postgres, with the record of each of its jobs (the jobs row, its results and the steps of its log). The same transaction counts the rows of each table. When a count differs, nothing is written, the run stays in its fenced object, and a later sweep tries again.
  • Each column of the object's tables moves or stays in the object for a reason: MOVED_COLUMNS and LEFT_COLUMNS in apps/web/src/server/jobs/ci-run-move.ts name each one, and a test fails for a column that neither names.

To count the runs that moved:

psql "$PG_URL" -c "SELECT count(*) FROM ci_runs WHERE moved_at IS NOT NULL"

A run that waits for a person to approve it moves as it is: it keeps waiting_approval, and an approval after the move starts it on Postgres, as a run that started there. The merge box and the checks of its pull request read it on Postgres after the move, and show what they showed before it. A run that runs, such as a run whose job waits at an approval gate, stays in its object until it ended. The fenced run objects keep their rows, and the job objects their records, until the cleanup of the classes.

4. Watch the moves

  1. Watch the old runs that are left go down to 0:

    psql "$PG_URL" -c "SELECT count(*) FROM ci_runs WHERE home IS NULL AND version > 0 AND status IN ('completed', 'cancelled')"

    The cron logs moved and waiting for each minute that it moved runs. A run that waits is not at rest yet: it moves at a later sweep.

  2. Watch the locks of the runs on the analytics server (Sign in to the server). Count the holds of the lock of a run in each 5 minutes. Find the slow parts of a repository tells what each timing measures:

    clickhouse-client --password "$(cat /root/.clickhouse-admin)" --query "SELECT toStartOfFiveMinutes(at) AS at5, count() AS holds, round(sum(ms) / 1000, 1) AS held_s, max(ms) AS max_ms, countIf(ok = 0) AS failed FROM analytics.timings WHERE kind = 'lock_hold' AND name LIKE 'ci_runs %' AND at >= now() - INTERVAL 1 HOUR GROUP BY at5 ORDER BY at5"
  3. Read the new issues in Sentry. The issue alert: a lock was held more than 100 seconds in 5 minutes names the lock in its breadcrumbs (lock, heldMs, windowStart), as the key of what it locks.

5. Sort the run objects that no run names

The moves and their checks start from the table ci_runs. A CiRunObject that no row of it names is outside them, and the delete of the class would delete its rows with it. On 2026-12-02, 6,178 run objects held data, and the index named about 5,920 of them. The command orphans sorts the others. It only reads.

  1. Run the sort:

    pnpm -C apps/web object-move ~/gitd-secret ci_run orphans

    The script lists the run objects with stored data from the Cloudflare API, with the login of wrangler or CLOUDFLARE_API_TOKEN. It never prints the token. CLOUDFLARE_ACCOUNT_ID picks an account other than the one in apps/web/wrangler.runner.jsonc. Then it sends the ids of the objects to the route, 200 in each call, and prints one line for each call.

  2. Read the last line, the sort. objects is the number of run objects with data. named is the number that a run of the index names: the moves and their checks handle those. Each other object gets one kind:

    KindWhat the object holdsProblem
    tombstoneOnly the tombstone of a deleted run: an admin deleted it, or its retention ended.No
    deletingA deleted run whose delete has steps left, such as its cells and the records of its jobs.No
    cut-createThe context or the cells of a create that stopped before it stored the run. No run was made.No
    emptyNothing in its tables.No
    gone-repositoryA run of a repository that was deleted.No
    deleted-on-postgresA run that moved to Postgres and was deleted there later.No
    live-repositoryA run of a repository that is here, which no row of the index names ("index":"none"), or only a failed plan ("index":"version 0").Yes
    misnamedA run whose id names another object.Yes
    rows-without-runSlots, audit entries or fixups, with no run and no tombstone.Yes

    Each problem names its object, and its run and repository, or its tables. The sort holds no value of a run. An object that the sort could not read is in failed: not-an-id for an id that is not a run object, and call for a call that failed. Run the sort again for those.

  3. The delete of the class CiRunObject waits for a sort with "clean":true: no problem and no failed object. The script exits with 0 only then.

Run the sort before the CI run retention ends for the runs that moved. The tombstone of a run deleted on Postgres goes with its retention, and then a run that moved and was deleted later looks like a live-repository problem.

6. Audit the moved runs

Before the class CiRunObject is deleted, check that Postgres holds each run that moved as its objects hold it. Each run object keeps its rows behind its fence, and each job object its record, so the audit can compare them.

  1. Check that no run is left in its object that has not ended. The sweep moves a run that waits for a person to approve it (waiting_approval) as it is: it keeps its state, and a person approves it on Postgres, as a run that started there. Such a run has no end time, so it comes first in the list of the sweep. Nothing is cancelled or run again: the showcase (@TanStack/showcase) stays archived, and the guided tour still shows the runs of its pull request #4 as runs that wait for an approval. This lists the runs that have not ended in their objects, with the state of their pull requests (open, closed or merged). The state comes from item_index: row level security holds the role of PG_URL to the repository of its session, which psql does not set, so a join with repo.items finds no row.

    psql "$PG_URL" -c "SELECT r.id, p.name, r.pull_number, i.state, r.workflow, r.status FROM ci_runs r JOIN repos p ON p.id = r.repo_id LEFT JOIN item_index i ON i.repo_id = r.repo_id AND i.number = r.pull_number WHERE r.home IS NULL AND r.version > 0 AND r.status NOT IN ('completed', 'cancelled')"

    A run on the list moves once the sweep finds it at rest: a run that waits for an approval at the next sweep, and a run that runs, such as a run whose job waits at an approval gate, once it ended.

  2. Wait until no old run is left in its object:

    psql "$PG_URL" -c "SELECT count(*) FROM ci_runs WHERE home IS NULL AND version > 0"
  3. Run the audit:

    pnpm -C apps/web object-move ~/gitd-secret ci_run audit

    Each call audits up to 25 runs, 4 at once, and starts no run after 25 seconds. It answers "waiting":"audit" with the count of the runs audited so far, and the script calls again at once, until the audit is done. Its last line has the outcome.

    A deploy during the audit costs a wait, not the audit. A call that fails (an answer of 5xx, a cut connection, or no answer within 60 seconds) loses nothing: the script calls again after 10 seconds, and stops only after 5 failed calls in a row. A deploy of the runner Worker restarts the objects: the audit reads a run that a restart stopped again after 2 seconds, up to 3 tries, before the run counts as failed.

What the audit compares, for each run that a move put on Postgres (a run with moved_at):

  • The row of the run in ci_runs, and its cells, slots, audit trail and fixups, with the rows that its object keeps, column by column. The run that started it (parent_run_id) is the object's, or null when that run is not on Postgres, as the move writes it. A test checks that the audit compares each column that the move writes.
  • The record of each of its jobs (the jobs row, ci_job_results and ci_job_log_steps), with the record that the job's object keeps.
  • A run that changed on Postgres after its move, such as a run that ran again, has a higher version. For such a run, the audit checks only that each row of its object is there.

It reads each page of runs on Postgres in one snapshot, so a run that retention deletes while the audit runs is compared as it was.

The audit writes on purpose to each run's object: before it reads the object, it puts up the object's fence again. A moved run keeps its fence up until the class is deleted, and a fence that is up keeps the time it went up, so this changes nothing for a run that the move fenced. A fence that goes up at or after the start of the audit was down: something took it down after the move, and the object could take writes. The audit counts such a run as unfenced. Its fence is up again after the audit: find out what wrote to it, then run the audit again.

How to read the outcome:

  • clean is true when no row differs, no run was unfenced or failed, and no run was left in its object when the audit started or when it ended (leftAtStart and leftAtEnd). It is the gate of the delete of the class.
  • tables counts, for each table, the rows of the objects (rows), and the rows that are missing on Postgres, extra on Postgres, or changed.
  • problems lists the first 50 problems: the run, the table, the key of the row, and the column that changed, or a run that was unfenced, or a run whose audit failed. It shows no value and no error text, because a value can be the text of a private repository. Read a value with psql, and the error of a failed run in Workers Logs (CI run audit: the audit of a run failed). The audit goes on with the other runs after a run fails.
  • newer counts the runs that changed after their move, unfenced the runs whose fence was down, and failed the runs whose audit threw.

The route answers the same outcome again for 10 minutes after the audit ends, so an answer that the network lost is not lost. A call after that starts a new audit. An audit that did not end goes on from where it stands while calls keep it: it starts over when no call came for an hour (the updated_at of its row), or when the flag objects.ci_run changed since it started. Its age does not count, because the audit of a day of moves takes hours. The audit changes no run. It keeps where it stands in the row ci_run_audit of site_settings.

Done

New CI runs start on Postgres, and each old run moves there once it ended, or as it is while it waits for an approval. The reads and the writes of a run follow its home. The run objects that no run names are sorted, with no problem.

Size the locks of the class

An object runs one call at a time, in memory. On Postgres, each change of a class locks a row, or takes a lease, and holds it across the round trips of its caller. So size each lock before a class takes production's load on Postgres. On 2026-12-01, the CI queue locks, nearly all of them the TanStack account's, were held up to 59 s of each minute, from 17 CI runs of one repository. Every caller of the lock waited, and their waits slowed every page of the site.

  1. Count the round trips of each change of the class under its lock, in a test, as apps/web/test/repo-write-statements.test.ts does.

  2. Find the calls of the class's busiest object in a busy minute of the last week:

    clickhouse-client --password "$(cat /root/.clickhouse-admin)" --query "SELECT toStartOfMinute(at) AS minute, repo_id, count() AS calls FROM analytics.timings WHERE kind = 'rpc' AND name LIKE 'CiRunObject.%' AND at >= now() - INTERVAL 7 DAY GROUP BY minute, repo_id ORDER BY calls DESC LIMIT 10"

    The calls of an object with no repository have an empty repo_id. Then the count of the whole class is the most that one of its objects can have.

  3. Multiply the calls by the round trips of their changes, and by about 5 ms a round trip for a caller next to the database, or 70 ms for a caller far from it. Keep each lock under 20 s of a minute. Over that, change the code before the flip: hold the lock for one message (repoWriteBatch, or SQL that makes the change in one statement), or read with no lock.

  4. Time each lock and lease with countLock and the key of what it locks (lockKey in apps/web/src/server/timings.ts). The timings consumer alerts the team when one lock is held more than 100 s in 5 minutes (step 4).

The moves that ended

The other classes moved on 2026-12-01 and 2026-12-02, each with a go, and each class is deleted with its objects (deleted_classes). The flags of these classes stay in site_settings, and no code reads them. The rows of their moves stay in object_moves, as the record of each move.

Most of them moved with a copy. The route froze the class (its flag refused the writes), put up the fence of each listed object, copied the tables of each object to Postgres in pages, checked each copy (the count and the checksum of the rows of each table), and then flipped the flag to postgres. A rollback copied the state back the same way. Before the delete of a class, an audit compared each row of each object with Postgres: the same, newer, or gone for a reason. That code went with the last class that it moved.

The classes of P3

  • app_card, the cards of the apps on pull requests: moved first, at about 03:00 UTC, to repo.app_cards, repo.app_card_sections and repo.app_card_files, and AppCardObject is deleted (v16 of apps/web/wrangler.jsonc).
  • account, the state of each person, org or app bot: the screening state, the PR chat tokens, the activity counts, the seen state, the "Needs attention" items, the "Import everything" run and the org MCP connections. It moved at 03:54 UTC to screening_items, account_screening, org_mcp_connections, import_runs, import_run_items, pr_chat_days, activity_days, seen_repos, seen_items and attention_items, and AccountObject is deleted (v18). The flag of a person is in account_flags, and the ban in users.
  • webhook and app, in one freeze at 08:13 UTC: the webhooks of each repository, their deliveries and the ids of the events that they took to repo.webhooks, repo.webhook_deliveries and repo.webhook_seen, and the deliveries of the apps, their hour windows and the purge targets of the deleted apps to app_deliveries, app_send_windows and app_purge_targets. WebhookObject and AppObject are deleted (v17).
  • coordinator, the background work of each repository, had no copy: only its flag moved it, at 06:20 UTC. Since then, after each change, the writer relays the outbox itself and arms the due work (repo-data/relay.ts, repo-data/due.ts). RepoCoordinator is deleted (v19).

A known gap: an app bot keeps no burst flag on Postgres. The flag is in account_flags, which has a key to users, and a bot has no row there. The record that finds a burst of a bot still holds that burst, but the next record of the bot finds no flag. Revisit it when apps are used.

The CI queue group moved

The CI queue group was two classes of the runner Worker. The CiQueueObject of each account kept its slot requests and its money: the usage, the billing periods, the credits, the overage reports and the model spend. The CiAuthorObject of each fork author kept its container seconds and its factory runs. The class ci_queue moved them on 2026-12-01 at 08:30 UTC to the tables ci_accounts, ci_slot_requests, ci_authors, ci_author_days, factory_author_days, factory_malicious_items, ci_pair_runtime, ci_pair_stop_receipts, ci_pair_runtime_final and the billing tables (billing_usage_events, billing_usage_lines, billing_periods, billing_credit_grants, billing_overage_reports, billing_model_months, billing_model_receipts and billing_model_acks), and its audit was clean. The classes CiQueueObject and CiAuthorObject went with all their objects (v12 of wrangler.runner.jsonc).

On Postgres, the ledger reads billing_accounts itself: there is no copy of the billing to send, and a billing change cuts the billing period in its own transaction. A grant reaches its holder only from a grant pass: a ci.pass message asks for one, and the sweep "CI passes" of the CI cron runs each pass that is due or that a lost message never ran. The daily sweep "CI trims" drops the usage ids older than 62 days, and the lines and the reports of overage older than 13 months.

The CI cache indexes and the CI surfaces moved

The class ci_cache moved the cache index of each repository to its tables on Postgres on 2026-12-01, and CiCacheObject went with all its objects (v9 of wrangler.runner.jsonc). Each CI workflow had one CiSurfaceObject on each pull request: the card that its jobs write. The class ci_surface moved the surfaces to ci_surfaces, ci_surface_replaced_runs, ci_surface_sections, ci_surface_files and ci_surface_clicks on 2026-12-01, and CiSurfaceObject went with all its objects (v10). On Postgres, the files of the old runs of a surface, and the uploads that a Worker left for an hour, go to ci_purges, and the task "CI purges" of the CI cron deletes their bytes.

The factory items

The factory items are on Postgres since 2026-12-01. A factory item is the state of the software factory on one issue or pull request: its runs, verdicts, pipeline, the ids of the events it handled, and its needs-info timer. The default branch of a repository (branch), its dependency updates (deps) and its CI cache agent (ci-cache) are items too. A skill store has items as a repository does, under the repository id skill:<store id>. An item is one row of factory_items and its rows in the other factory_* tables. The run lists and the spend of a repository read factory_runs and factory_spend_days.

The items did not move in one freeze: the script had commands of their own for it, and a no-loss check before the old classes went. That code is gone with the move. The old objects, FactoryItemObject and the run indexes of the repositories (FactoryRepoObject), went with all their storage (deleted_classes, v11 of wrangler.runner.jsonc), after the no-loss check found every one of their rows on Postgres (2026-12-01).

How the items work in Postgres:

  • A page that changes an item waits up to 10 seconds for the other work of the item, then answers 503. A queue message about a busy item tries again 30 seconds later.
  • The minute cron task factory due claims the items that are due, and the queue gitflare-factory-events runs their due work.
  • The end of a run sends its effects to the same queue. If the message is lost, the item is due a minute after the end, and its due work runs the effects.
  • An agent workflow run keeps its state on Postgres (agent_runs and its tables), and the change that moves its status writes its row of factory_runs and its spend (apps/web/src/server/factory/run-reports.ts). See Move the agent workflow runs.

Move the agent workflow runs

The agent workflow runs moved from their AgentRunObjects to Postgres (agent_runs and its tables) on 2026-12-02, on the model of the CI runs:

  1. The list of the objects with data from the Cloudflare API, and a rehearsal on staging that copied and checked each run (1,101 runs, all the same).
  2. The flip of the flag objects.agent_runs at 13:19 UTC: from then on new runs started on Postgres.
  3. The sweep "agent run moves" of the minute cron, while the site setting factory.agent_run_moves was on: it moved each old run once it was at rest, from 14:20 to 14:47 UTC.
  4. The check of each moved object against Postgres: 1,102 runs the same (9,771 rows), 0 mismatches, and 6 objects that held no run.

Every run is on Postgres now. Every caller reaches a run through agentRun (apps/web/src/server/factory/agent-run-router.ts), which reads no flag and reaches no object. The flag, the sweep, the site setting and the commands of the moves (agent_run_moves) went when the move ended. Their rows stay in site_settings and object_moves (classes agent_run and agent_run_check) as its record. The class AgentRunObject went with its fenced objects and all their storage (deleted_classes, v13 of wrangler.runner.jsonc).