Repository navigation
perf(db): stop worker heartbeats from re-queuing every thread - #1336
Merged
Merged
Conversation
Each worker heartbeat writes workers.last_seen_at, and every UPDATE of workers was a metadata change for the whole owner. The replication trigger re-queued the head of every thread on that worker, every thread watch revalidated and mirrors resynced. On one production gateway that was about 120 clock updates per second (51 million in six days) for a column nothing reads from the change log. Migration 0223 ignores updates that only move last_seen_at. Inserts, deletes and every other worker change still count. Co-Authored-By: Claude Opus 5.5 <[email protected]>
nixuuu
enabled auto-merge (squash)
November 27, 2026 11:44
nixuuu
added a commit
that referenced
this pull request
Nov 27, 2026
## Problem PostgreSQL's default `checkpoint_timeout` is 5 minutes. After each checkpoint, the first change to a page writes the whole page to WAL. On the production gateway these full-page images made up most of the WAL volume. The pages came from the tables that grow and shrink all the time: `thread_events`, `thread_replication_journal` and `thread_items`, which receive transcript events while retention deletes old rows. Measured on that gateway after the #1336 fix (same workload, values per 1000 WAL records): | | default (5 min, 1 GB, no compression) | 15 min, 4 GB, lz4 (30 min sample) | |---|---|---| | full-page images | 296 | ~105 (−65%) | | WAL bytes | 1.83 MB | 0.60 MB (−67%) | Almost all of the gain comes from the longer checkpoint interval. Bytes per full-page image barely changed with lz4, most likely because most of these pages are already-compressed TOAST data. ## Change - **Desktop (local mode).** `apps/desktop/src/runtime/database.ts` starts PostgreSQL with `checkpoint_timeout=15min`. - `max_wal_size` stays at the default 1 GB, so crash recovery on a laptop stays short. - Compression is left off, because an `@embedded-postgres` build is not guaranteed to include lz4 and the server would fail to start without it. - **Compose.** `deploy/docker-compose.yml` runs `postgres` with `checkpoint_timeout=15min`, `max_wal_size=4GB` and `wal_compression=lz4`. - **Docs.** `docs/desktop.md` and `docs/deploy.md` describe the settings, their cost (up to ~4 GB of WAL files on disk, longer recovery after a crash) and how to apply them to an own PostgreSQL with `ALTER SYSTEM` + `pg_reload_conf()` or to an existing Compose container. ## Verification - `postgres:17` with these flags reports `checkpoint_timeout=900s`, `max_wal_size=4096MB` and `wal_compression=lz4`. - `docker compose config` renders the command. - The production gateway runs the same three settings via `ALTER SYSTEM`; the numbers above come from there. - oxlint on the desktop file is clean. - Not run: a desktop local-mode start with the `@embedded-postgres` 18.7 binaries. `checkpoint_timeout` is a core setting accepted by every build. CI runs the full gate. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
This was referenced Nov 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On the production gateway (0.11.0-nightly.14), Postgres wrote ~285 GB in 6 days. Measured with
pg_stat_*:thread_replication_clockUPDATEs (1744 rows)Cause: a worker heartbeat runs
UPDATE workers SET last_seen_at=now()about every 20 s. The constraint triggercomputer_worker_change(0049) treats every UPDATE ofworkersas a metadata change, which has nothread_id. Each heartbeat therefore:thread_replication_changedover every thread on that worker and its forks. That is 904 and 581 clock updates for the two largest workers, in one transaction.threadWatchRevision, so every thread watch and the BFF warmer re-read.Nothing reads
last_seen_atfrom the change log:ui.Threadonly carriesworker_id, and worker lists get changes through the overview.Change
0223_worker_heartbeat_changessplits the trigger.computer_worker_changekeeps INSERT and DELETE. A newcomputer_worker_updatefires on UPDATE only when anything other thanlast_seen_atchanged.docs/linked-computers.mddescribed heartbeat revisions as expected behaviour; it now describes the new rule.The trigger dates from #27 and has always behaved this way, so this is not linked as a fix.
Verification
packages/db/test/notice-replication.test.ts: alast_seen_at-only update records noworkerschange and leaves the thread's replication clock unchanged. A rename still records the change and moves the clock. Without the migration the case fails (5workerschanges recorded).notice-replicationandschematests: 31 pass.local-thread-liveness,thread-read-cost,ui-thread-deltasandthread-mirror-deltasviacheck:tmp: 29 pass.Not addressed here: Postgres reports 71 GB of temporary files (22.5k files) over the same period. A 4-minute sample saw none, so they come in bursts, likely from periodic maintenance.
log_temp_filesis off on that server, so the query isn't identified yet.🤖 Generated with Claude Code