Skip to content

Under Postgres, the publication pipeline reports its search wait, projection lag, backlog and inserts - #469

Merged
zlareb1 merged 17 commits into
mainfrom
feat/pg-pipeline-metrics
Nov 10, 2026
Merged

zlareb1 merged 17 commits into
mainfrom
feat/pg-pipeline-metrics

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

Under the Postgres authority, Loom reports how long a search waits for earlier writes to reach ClickHouse, how long a revision takes to become searchable, how many deliveries are owed or parked, and how many ClickHouse inserts its publishers make. GET /v1/health?include=postgres gains the same backlog, and the monitoring guide states the pipeline's SLOs and alerts.

Why

Under the Postgres authority a write answers at its commit and becomes searchable once its memory row and search document are in ClickHouse. An operator had no signal for that gap: neither how long a search waited at the barrier (pg_validation.admit), nor how far behind the delivery worker was, nor whether a delivery ClickHouse kept refusing had parked. The request counter also counted a 503 PROJECTION_BEHIND like any other failure, with no code to tell it from an outage.

What changes

  • server/observability/pg_pipeline.py holds the instruments:
    • loom.pg.search_wait_ms: what a read paid at the barrier when ClickHouse was behind, by loom.operation (the op the request invoked), outcome (passed, or deadline when it reached its deadline, LOOM_PG_PROJECTION_WAIT_MS or what a re-entrant pass had left: the op answers 503 PROJECTION_BEHIND, a report that may skip the projection reads on) and traffic class. A read that found nothing behind records nothing, so the histogram is the wait users actually see.
    • loom.pg.projection_lag_ms: from a revision's commit transaction's start to its delivery clearing its last search target (the document landed, or a redelivery settled a revision no longer served unsent), measured on Postgres's clock as the delivery clears and recorded once that commits (pg_memory.complete), by path (the writer's own flush, or a redelivery).
    • loom.pg.backlog (source, state) and loom.pg.oldest_pending_age_s (source): one reading per delivery pass for memory, the audit outbox and knowledge, taken first in the pass by the worker that owns the source. The process that runs the delivery workers exports them (the workers child by default, else the lease holder); a process that runs none has no reading, and one older than two export periods is dropped; loom.pg.backlog_reading_age_s says how old each is.
    • loom.ch.inserts and loom.ch.insert_rows (table): the memory write path's inserts of memory rows and search documents and the audit writers' inserts, under either authority. Memory rows and search documents are committed inserts, a part each; audit rows are batched by default.
    • Every label is a closed set; none names a namespace, a key or a user.
  • loom.requests carries loom.error_code (empty on success), on the counter only: the refusal's own code where it carries one (HTTP reads it from the detail, MCP from the answer); a codeless refusal is HANDLER_HTTP_ERROR on HTTP and its status's code on MCP; so the duration histogram's series still divide by the counter's.
  • GET /v1/health?include=postgres adds postgres.backlog: per source the pending and parked counts up to the cap, capped past it, the oldest one's age and reading_age_s. Each process reads its own, kept LOOM_PG_BACKLOG_HEALTH_CACHE_SECONDS. The block's parked_* fields keep counting in full.
  • New settings: LOOM_PG_BACKLOG_COUNT_CAP (10000), the most deliveries one reading counts per source; past it the reading stops and reports capped, and LOOM_PG_BACKLOG_HEALTH_CACHE_SECONDS (10).
  • docs/operations/monitoring.md gains "Pipeline SLOs" (targets, signals, why readiness does not use them) and the alerts; docs/features/observability/telemetry.md lists the instruments.

Scope and risk

  • The backlog readings are bounded count(*) reads (the pending and the parked counts each LIMIT one past the cap, so capped means more than the cap) on the deliveries tables' partial indexes, once per delivery pass and at most once per cache period per replica for health.
  • The projection lag reads committed_at in the statement that already locks the cleared deliveries; nothing else is added to a write or a search.
  • No change to what is published or when; the instruments only observe.
  • tests/unit/test_pg_durable_reads.py classifies the delivery workers' backlog counts (they feed a gauge and send nothing).

Compatibility, rollout and rollback

  • Wire: postgres.backlog is added to GET /v1/health?include=postgres; loom.requests gains the loom.error_code attribute on failures. Nothing removed.
  • Operator steps: none. Two settings added with defaults.
  • Rolling upgrade: build N-1 emits none of the new instruments; the gauges come from whichever process runs the delivery workers.
  • Rollback: reverting leaves nothing behind.

Tests

  • tests/unit/test_pg_pipeline_metrics.py: a backlog reading dropped after two export periods; projection lag recorded only when a revision's last search target lands, by path, and only once the completion commits; the barrier records a wait only when something was behind, with its outcome and the op; the error code on the counter alone; health reading the backlog itself once per cache period.

  • More unit cases: the gauges export a process's fresh readings only, nothing where it took none or its last went stale; a backlog read that fails is logged and not raised; capped only past the cap; the HTTP failure counter takes a coded HTTPException's own code (tests/unit/test_transport_internal_error_logging.py); an MCP failure counts under the code it answers (a refusal's, a named 5xx's, or INTERNAL_ERROR).

  • tests/integration/test_pg_pipeline_metrics.py (added to the Postgres authority job) runs the three backlog reads against the real queues. Each memory and knowledge case takes its delivery over (advancing lease_token, so the write's own flush cannot clear it), backdates it so the oldest age must reach it, checks the cap, and counts its parked row by the drop when the row is removed; the outbox case starts its row parked and makes it due only inside a transaction it rolls back, the backlog read in it, so no running worker sees it.

  • tests/unit/test_transport_internal_error_logging.py now expects error_code among a failed op's attributes.

  • Local runs: make format-check, make lint and mypy on the changed files pass; the unit files that touch the changed modules pass (6988 tests; one webhook deadline test failed once under load and passes alone 3 of 3, unrelated). The full suite runs in CI.

  • Latency, a small local before/after (100 sequential calls each, two runs, Postgres 16 with a synchronous standby, the local embedder; not a benchmark):

    main this branch
    memory.set p50 504 / 499 ms 498 / 525 ms
    memory.search p50 72 / 79 ms 79 / 79 ms

    Within run-to-run noise.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. The label sets, the read bounds and where the readings are taken were checked against the code paths they observe.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed. Locally: format, lint, mypy on the changed files and the affected unit files; the full suites run in CI.
  • Generated files are regenerated in this PR, not by hand: docs/reference/configuration.md, loom.example.yaml, schemas/loom-settings.schema.json, the settings defaults snapshot.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited: no migration.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version: no plugin file.
  • The frontend builds, if frontend/ changed: not changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s).

🤖 Generated with Claude Code

@zlareb1
zlareb1 force-pushed the feat/pg-pipeline-metrics branch 3 times, most recently from 2497426 to 2593c50 Compare November 10, 2026 14:25
zlareb1 and others added 17 commits November 10, 2026 15:16
…jection lag, backlog and inserts

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…s say what they measure, and health answers each reading's age

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… the instruments, and the deadline alert says a report reads on

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ode test, integration cases count their own rows

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…fence out the writer's flush, precise docs

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… which tables make a part each, settings named by tests, outbox case held from a running worker

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… starts parked, SLO and insert alert that can fire, error-code claim narrowed

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…pped, outbox case due only in a rolled-back transaction

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…m settings, the lock helper back to one job

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…m (the workers child by default); health always reads its own, cached

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…h max, census rename reverted, health backlog simpler

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… target everywhere, a long pass named as a gap, error-code comment

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the feat/pg-pipeline-metrics branch from 2593c50 to c24e436 Compare November 10, 2026 15:24
@zlareb1
zlareb1 merged commit 42794ac into main Nov 10, 2026
38 checks passed
@zlareb1
zlareb1 deleted the feat/pg-pipeline-metrics branch November 10, 2026 16:03
@zlareb1 zlareb1 mentioned this pull request Nov 12, 2026
3 tasks done
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