Skip to content

Under Postgres, a revision every usable ClickHouse backup holds can lose its payload, past a horizon no shorter than the backup interval plus 60 minutes - #453

Closed
zlareb1 wants to merge 9 commits into
feat/pg-revision-metadatafrom
feat/pg-revision-payload-clear
Closed

zlareb1 wants to merge 9 commits into
feat/pg-revision-metadatafrom
feat/pg-revision-payload-clear

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Migration 004_revision_delivered_at.sql builds its index inside the migration's transaction. That is safe only while loom.memory_revisions is small. No released build writes the Postgres journal, and every known deployment runs the ClickHouse authority, so it is small wherever 004 runs. This PR therefore has to ship in the same release as 003. If a release carrying 003 is cut first, the index moves to CREATE INDEX CONCURRENTLY before this PR merges.

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 LOOM_STORAGE_AUTHORITY=postgres with LOOM_PG_PAYLOAD_CLEAR_ENABLED, a memory revision keeps only its metadata in Postgres once every usable ClickHouse backup holds it. That happens past a horizon that is never shorter than LOOM_CLICKHOUSE_BACKUP_INTERVAL_MINUTES plus 60. Before restoring a ClickHouse backup, turn the switch off; the steps are in backup and recovery.

Why

loom.memory_revisions.row keeps every version's full row, and ClickHouse's memory_versions holds the same. Since the previous change, the journal keeps a revision's metadata in typed columns, and every reader tolerates a cleared payload. The payload is still needed to write a version back into ClickHouse after a backup restore. So it may go only once every backup a restore may use holds the version.

Design

  • Delivery time. A completion that empties a delivery more than 15 minutes after its revision's commit sets delivered_at on the revision (pg_memory._STAMP_LATE). The stamp is a data-modifying CTE fed from the RETURNING of the existing DELETE and settle UPDATE. A completion sends no more statements than before, and an on-time write never touches the journal. So a revision's delivery time is at most coalesce(delivered_at, committed_at) plus 15 minutes.

  • The pass. It is a worker of its own, pg_payload_clear, off by default (LOOM_PG_PAYLOAD_CLEAR_ENABLED, not LIVE, so a restore of loom.runtime_overrides cannot turn it on). Each pass works in three steps:

    • It reads up to LOOM_PG_DELIVERY_BATCH_ROWS candidates through the new partial index memory_revisions_payload (its predicate is pg_memory.CLEARABLE, word for word). A candidate is past the horizon, owes no delivery, and is not a profile revision.
    • It checks the candidates against loom.memory_versions with sequential consistency: same key and version, same memory_id, same content hash. A redaction keeps the hash.
    • It clears the matches with FOR NO KEY UPDATE SKIP LOCKED, so a row held by a redaction or an erasure is skipped.

    A version ClickHouse lacks is kept and counted (missing_in_clickhouse). A dropped ClickHouse connection is retried within half the pass deadline. The pass resumes from the last pass's position in the process and starts over at the end. It clears nothing until the process has adopted the deployment's overrides (settings_overrides.adopted()).

  • Horizon. The horizon is LOOM_PG_PAYLOAD_RESTORE_HORIZON_MINUTES (LIVE; 0, the default, means twice the backup interval plus 60). It is never under LOOM_CLICKHOUSE_BACKUP_INTERVAL_MINUTES (LIVE, default 1440) plus 60, nor under the insert bound plus 60. The 60 minutes cover a late backup, replica lag and clock skew. The arithmetic and the restore runbook are in docs/operations/backup-and-recovery.md.

  • No new state. There is no new table and no stored high-water mark: row IS NULL already means cleared by this pass, because a redaction scrubs the payload rather than nulling it.

Scope and risk

  • Postgres authority only, and only with the switch on. A profile revision keeps its payload. Knowledge documents are not covered: their old versions already live only in ClickHouse.
  • If a ClickHouse restore skips the runbook, the content of versions committed between the backup and the restore can be lost; their metadata stays.
  • Erasure: unchanged. The saga deletes revisions by scope, and delivered_at holds no content.

Compatibility, rollout and rollback

  • Migration 004 adds a nullable column and a partial index. A build without 004 ignores both.
  • New settings: LOOM_PG_PAYLOAD_CLEAR_ENABLED, LOOM_PG_PAYLOAD_CLEAR_INTERVAL_SECONDS, LOOM_CLICKHOUSE_BACKUP_INTERVAL_MINUTES and LOOM_PG_PAYLOAD_RESTORE_HORIZON_MINUTES. Wire: none. Operator steps: none until the switch is turned on.
  • Rollback: the previous build reads cleared payloads. A cleared payload stays cleared.

Tests

  • tests/unit/test_pg_payload_clear.py:
    • the index predicate equals CLEARABLE;
    • the horizon is clamped to the backup interval and the insert bound, each plus 60;
    • no clearing before the overrides are adopted;
    • only revisions ClickHouse holds are cleared;
    • the cursor resumes after a full batch and resets after a short one;
    • a dropped ClickHouse connection is retried within the pass;
    • a version matches only with its id and hash;
    • a completion's stamp rides its two existing statements.
  • test_pg_authority.py::test_a_revision_delivered_late_is_stamped_and_one_delivered_at_once_is_not (integration): a delivery taken over from a writer that stopped an hour ago is stamped; on-time ones are not.
  • test_pg_authority.py::test_a_revision_past_the_horizon_that_clickhouse_holds_loses_its_payload (integration): past the horizon, both versions of a key are cleared, the redacted one included. Their metadata stays, and memory.history and memory.get answer as before. A revision ClickHouse lacks keeps its payload.

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. Checked: the delivery-time bound on every completion path, the lock order against redaction and erasure, and the restore runbook against the compose deployment.

  • 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.
  • Generated files are regenerated in this PR, not by hand.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version.
  • The frontend builds and its tests pass, if frontend/ 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), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 9 commits November 9, 2026 11:57
…edaction live in typed columns that every journal reader matches on

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…nal written before it, and readers take the field from row then

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…index serves, and a profile revision keeps its payload; the unread tombstone column is gone

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…r clears; the journal keeps no column nothing reads

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…a cleared payload

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…gap test's check reads like its neighbour's

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ad when it sweeps its settled delivery, past LOOM_PG_REVISION_PAYLOAD_MINUTES

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the feat/pg-revision-metadata branch from ba5f06e to 982d7e0 Compare November 9, 2026 19:06
@zlareb1
zlareb1 deleted the branch feat/pg-revision-metadata November 9, 2026 20:20
@zlareb1 zlareb1 closed this Nov 9, 2026
@zlareb1 zlareb1 changed the title Under Postgres, a delivered revision keeps only its metadata past LOOM_PG_REVISION_PAYLOAD_MINUTES Under Postgres, a revision every usable ClickHouse backup holds can lose its payload, past a horizon no shorter than the backup interval plus 60 minutes Nov 9, 2026
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