Skip to content

perf(db): stop worker heartbeats from re-queuing every thread - #1336

Merged
nixuuu merged 1 commit into
devfrom
fix/worker-heartbeat-fanout
Nov 27, 2026
Merged

nixuuu merged 1 commit into
devfrom
fix/worker-heartbeat-fanout

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 27, 2026

Copy link
Copy Markdown
Member

Problem

On the production gateway (0.11.0-nightly.14), Postgres wrote ~285 GB in 6 days. Measured with pg_stat_*:

6 days since stats reset 60 s sample
thread_replication_clock UPDATEs (1744 rows) 51.2 M 7238 (~120/s)
all other tables far fewer < 1500
WAL 80 GB (6.3 M full-page images) 9.5 MB
commits 46 M (~86/s)

Cause: a worker heartbeat runs UPDATE workers SET last_seen_at=now() about every 20 s. The constraint trigger computer_worker_change (0049) treats every UPDATE of workers as a metadata change, which has no thread_id. Each heartbeat therefore:

  1. runs thread_replication_changed over every thread on that worker and its forks. That is 904 and 581 clock updates for the two largest workers, in one transaction.
  2. counts as relevant for every watched thread in threadWatchRevision, so every thread watch and the BFF warmer re-read.
  3. makes mirrors revalidate.

Nothing reads last_seen_at from the change log: ui.Thread only carries worker_id, and worker lists get changes through the overview.

Change

  • Migration 0223_worker_heartbeat_changes splits the trigger. computer_worker_change keeps INSERT and DELETE. A new computer_worker_update fires on UPDATE only when anything other than last_seen_at changed.
  • docs/linked-computers.md described 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

  • New case in packages/db/test/notice-replication.test.ts: a last_seen_at-only update records no workers change and leaves the thread's replication clock unchanged. A rename still records the change and moves the clock. Without the migration the case fails (5 workers changes recorded).
  • notice-replication and schema tests: 31 pass. local-thread-liveness, thread-read-cost, ui-thread-deltas and thread-mirror-deltas via check:tmp: 29 pass.
  • oxlint on edited files is clean. CI runs the full gate.

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_files is off on that server, so the query isn't identified yet.

🤖 Generated with Claude Code

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
nixuuu enabled auto-merge (squash) November 27, 2026 11:44
@nixuuu
nixuuu merged commit 2984d6a into dev Nov 27, 2026
24 checks passed
@nixuuu
nixuuu deleted the fix/worker-heartbeat-fanout branch November 27, 2026 11:50
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant