Repository navigation
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 - #457
Merged
Conversation
zlareb1
force-pushed
the
feat/pg-revision-payload-clear
branch
14 times, most recently
from
November 12, 2026 03:33
0ad348c to
c550f34
Compare
…ose its payload, past a horizon no shorter than the backup interval plus 60 minutes Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
zlareb1
force-pushed
the
feat/pg-revision-payload-clear
branch
from
November 12, 2026 04:15
c550f34 to
aa80427
Compare
zlareb1
marked this pull request as ready for review
November 12, 2026 04:17
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.
Migration
010_revision_delivered_at.sqlbuilds its index inside the migration's transaction. That is safe only whileloom.memory_revisionsis small. No released build writes the Postgres journal, and every known deployment runs the ClickHouse authority, so it is small wherever 010 runs. This PR therefore has to ship in the release that first carries the Postgres authority (Postgres migrations 001 onward). If a release carrying them is cut first, the index moves toCREATE INDEX CONCURRENTLYbefore this PR merges.Changelog category (leave one):
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=postgreswithLOOM_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 thanLOOM_CH_BACKUP_INTERVAL_MINUTESplus 60. Before restoring a ClickHouse backup, turn the switch off; the steps are in backup and recovery.Why
loom.memory_revisions.rowkeeps every version's full row, and ClickHouse'smemory_versionsholds the same. The journal keeps a revision's metadata in typed columns (003_revision_metadata.sql), and every reader tolerates a cleared payload: the readers outsidepg_memory(the idempotency replay and erasure's consolidated-row lookup) readcoalesce(memory_key, row->>'memory_key'), and the typedmemory_keysurvives a clear. 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_aton 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 mostcoalesce(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 ofloom.runtime_overridescannot turn it on). Each batch works in three steps:LOOM_PG_DELIVERY_BATCH_ROWScandidates through the new partial indexmemory_revisions_payload(its predicate ispg_memory.CLEARABLE, word for word). A candidate is past the horizon, owes no delivery, and is not a profile revision.loom.memory_versionswith sequential consistency: same key and version, samememory_id, same content hash. A redaction keeps the hash.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 pass works through batches until one comes back short or half its deadline has gone, so clearing keeps up with a write rate above one batch per interval. A ClickHouse read that got no answer (ch_errors.is_connection_failure) or met a state that clears by itself (is_passing_state) is retried within the same budget. Past the budget, the first batch fails the pass. A later batch is noted as the pass's failure and ends it with what it cleared, its cursor before that batch. The pass checks for a stop before each batch's read and again before its clear. The next pass, in any process or colour, resumes from where this one stopped (its cursor inloom.worker_status), and a pass that reaches the end starts over. It clears nothing until the process has adopted the deployment's overrides (settings_overrides.adopted()): before that it would clear under the horizon and backup interval it started with, not an operator's override of them.Horizon. The horizon is
LOOM_PG_PAYLOAD_RESTORE_HORIZON_MINUTES(LIVE; 0, the default, means twice the backup interval plus 60). It is never underLOOM_CH_BACKUP_INTERVAL_MINUTES(LIVE, default 1440) plus 60, nor under the insert bound plus 60. The 60 minutes cover replica lag and clock skew, which a backup's start time does not show; a late backup shows in its start time, which the runbook's backup choice checks. The arithmetic and the restore runbook are indocs/operations/backup-and-recovery.md.Nothing records what was cleared. The only new state is
delivered_atand the pass's resume cursor inloom.worker_status; there is no new table:row IS NULLalready means cleared by this pass, because a redaction scrubs the payload rather than nulling it.Scope and risk
memory_currentrow (the same version), soloom reconcilerestores every live head ClickHouse lacks. A superseded cleared revision is left out of the holds (counting only what was held), and a delivery that reaches one anyway (held as a pass cleared it) parks at its claim withlast_error"payload cleared, nothing to send", so the delivery pipeline keeps running.delivered_atholds no content.Compatibility, rollout and rollback
memory_revisions_payload, a fifth index onloom.memory_revisionsbeside four (its key is a timestamp and the primary key's three columns). A late stamp or a clear changes an indexed column, so neither is a HOT update; the stamp never updates an on-time commit; only a clear does, once. With the switch off the index holds every non-profile revision and never shrinks; with it on, a cleared revision leaves it. What the entry costs a commit is not measured here: that takes a load run, which this PR does not include.LOOM_PG_PAYLOAD_CLEAR_ENABLED,LOOM_PG_PAYLOAD_CLEAR_INTERVAL_SECONDS,LOOM_CH_BACKUP_INTERVAL_MINUTESandLOOM_PG_PAYLOAD_RESTORE_HORIZON_MINUTES. Wire: none. Operator steps: none until the switch is turned on.loom reconciledo not. A cleared payload stays cleared. The previous build completes late deliveries unstamped, so turn the switch off before rolling back, and on again no sooner than one horizon after the new build serves alone again (rolling-upgrade rule 43). The previous build's delivery cannot send a cleared revision, so once a clearing pass has run,loom reconcileruns only with a build that carries 010 and the deployment starts on that build after it (with Helm, the release's image is set to it for the reconcile Job and the restart), a rollback waits until the new build owes no claimable delivery for a cleared revision (a query inupgrades.mdreturns 0), and while the previous build serves a retry of parked deliveries leaves the cleared revisions out (postgres.mdgives the clause; rule 43).docs/operations/backup-and-recovery.md's "Restoring one store" steps open with this PR's clearing stop and backup choice.docs/operations/upgrades.mdnames the switch's wait in the upgrade procedure (step 8) and in the rollback.Tests
tests/unit/test_pg_payload_clear.py:CLEARABLE;BackendUnavailableError) and a busy pool (OverloadedError) are retried within the pass; a read past its deadline (BackendTimeoutError) and a refusal are not;DELETEinpostgres.mdnames thelast_errortext the code writes;pg_payload_clear_enabledand paced bypg_payload_clear_interval_seconds.tests/unit/test_restore_reconcile.py:test_heads_that_hold_leaves_out_are_not_counted_and_are_warned_of:pg_memory.holdreturns how many it held, andhold_heads_behindcounts only those and warns of the heads it could not hold.test_pg_authority.py(integration):test_a_revision_delivered_late_is_stamped_and_one_delivered_at_once_is_not: a delivery taken over from a writer that stopped an hour ago is stamped through the settle arm, replacing an older stamp (the arm a redelivery after a restore takes); on-time ones are not.test_a_writer_finishing_its_own_delivery_late_stamps_it: the same through the DELETE arm, also replacing an older stamp.test_the_clearing_read_walks_its_index_in_order: with sequential scans, bitmap scans and sorts priced out,clearable_revisions' plan is an index scan ofmemory_revisions_payloadwith no Sort node.test_a_cleared_head_is_sent_from_its_head_row_and_a_cleared_older_version_parks:holdholds a cleared head and leaves out a cleared superseded version; the head's claim carries itsmemory_currentrow, field for field and at its version, and a delivery owed for the superseded one parks at its claim with its reason; the head's claim then publishes into a ClickHouse whoseloom.memorylacks the key, settles, and the head reads back at its version.test_an_unstamped_revision_waits_out_the_late_delivery_allowance: an unstamped revision whose commit is the horizon plus 5 minutes old is not offered; at the horizon plus 20 minutes it is.test_a_revision_past_the_horizon_that_clickhouse_holds_loses_its_payload:delivered_at,request_idand the audit row among them), andmemory.historyanswers as before;memory.historyserves that version scrubbed (value, reason, time and hash) with every other field and version as before;The candidate read uses the index.
EXPLAIN (ANALYZE, BUFFERS)ofclearable_revisions' statement, on a local journal with 100,000 synthetic revisions past the horizon (one org, 50 namespaces; deleted after), shows an orderedIndex Scan using memory_revisions_payload, itsIndex Condholding the horizon cut and the cursor's row comparison, and aNested Loop Anti Joinonmemory_deliveries_pkey. A batch of 500 took 0.8 ms and 1,180 shared buffers, so a batch's cost does not grow with the backlog. The rows and the read, with the default horizon (2,940 minutes) and an empty cursor:Local:
008_cells.sql) with the file renumbered 009, from baa5825 on main 32bd5f7, at c550f34 squashed onto main 28de00a (which took 009 for009_cell_probe.sql) with the file renumbered 010, and from aa80427 on main c19346c.test_deeply_nested_hidden_html_parses_in_bounded_timefails on this host on main too, in code this PR does not touch; it is the one failure in every full unit run below.test_pg_authority.py,test_pg_durable_publish.py,test_pg_pipeline_metrics.py) 150 passed.test_pg_authority.py142 passed.test_readiness.py,test_pg_payload_clear.py,test_pg_durable_reads.py,test_doc_links.pyandtest_public_copy.py299 passed.clearable_revisions' source text and addstest_the_clearing_read_walks_its_index_in_orderin its place; both changed integration tests passed.test_pg_payload_clear.py24 passed.test_pg_*and clearing and reconcile unit files 1,772 passed.test_doc_links.py,test_public_copy.py,test_storage_layout_rows.py,test_pg_payload_clear.pyand thetest_settings*files passed. The full unit suite at af2e7ab ran under host memory pressure: 33,776 passed and 32 failed, almost all clickhouse-localMEMORY_LIMIT_EXCEEDED. Run again alone, the six files with failures (test_aliases_verified_email_sql.py,test_delete_version_sql.py,test_profile_redaction.py,test_projection_repair_cursor.py,test_sdk_set_coalescer.py,test_signup_name_in_use.py) 263 passed; the remaining one is the host'stest_deeply_nested_hidden_html_parses_in_bounded_time.test_pg_*and clearing and reconcile unit files 1,975 passed.test_pg_payload_clear.py25 passed, andtest_a_revision_past_the_horizon_that_clickhouse_holds_loses_its_payloadpassed against a server on that head.test_pg_payload_clear.py25 passed, andtest_an_unstamped_revision_waits_out_the_late_delivery_allowancepassed against a server on that head.test_pg_payload_clear.pyandtest_doc_links.py241 passed.postgres.mdandparked_count):test_doc_links.pyandtest_public_copy.py221 passed, andmake format lintpassed.make format lintpassed; the full unit suite 33,844 passed and the host's one known failure (test_deeply_nested_hidden_html_parses_in_bounded_time). The commits after it change docs, one docstring, one log message and two unit test files.parked_countdocstring):make format lint typecheckpassed, and the prose check onpostgres.mdis clean.postgres.mdparagraph):test_doc_links.pyandtest_public_copy.py221 passed, and the prose check is clean.test_doc_links.py,test_public_copy.pyandtest_agent_instructions.py425 passed, the prose check is clean, and the rollback query runs on Postgres 16.test_doc_links.pyandtest_public_copy.py221 passed, the prose check is clean, and the query runs on Postgres 16.DELETEonly a parked one, and reconcile holds are released by finishingloom reconcile):test_doc_links.pyandtest_public_copy.py221 passed, the prose check is clean, and the query and theDELETE(rolled back) run on Postgres 16.PAYLOAD_CLEAREDto the rollback check and the operatorDELETE):test_pg_payload_clear.py,test_doc_links.pyandtest_public_copy.py247 passed;make format lintand the prose check pass.test_pg_payload_clear.py,test_doc_links.pyandtest_public_copy.py247 passed;make format lintand the prose check pass; the query runs on Postgres 16.postgres.md):test_pg_payload_clear.py,test_doc_links.pyandtest_public_copy.py247 passed;make format lintand the prose check pass; the retry with its clause runs on Postgres 16 (rolled back).NOT EXISTSanti-join, and the rollback and restore steps name which build runs reconcile and starts after it):test_pg_payload_clear.py,test_doc_links.pyandtest_public_copy.py247 passed; the prose check passes; the retry with the clause runs on Postgres 16 (rolled back) and plans as a nested-loop anti join.test_doc_links.pyandtest_public_copy.py221 passed, and the prose check passes.test_doc_links.pyandtest_public_copy.py221 passed, and the prose check passes.test_doc_links.pyandtest_public_copy.py221 passed, and the prose check passes.test_restore_reconcile.py,test_doc_links.pyandtest_public_copy.py263 passed;make format lintand the prose check pass.make format lint typecheckpassed, and the settings, worker, census, ratchet, snapshot, inventory, storage layout, cell, router, readiness, doc, copy,test_pg_*and clearing and reconcile unit files 1,853 passed. On a fresh Postgres and ClickHouse, the three integration files 153 passed, and the full unit suite 34,065 passed with the host's one known failure (test_deeply_nested_hidden_html_parses_in_bounded_time).make format lint typecheckpassed, and the settings, worker, census, ratchet, snapshot, inventory, storage layout, cell, router, readiness, doc, copy,test_pg_*and clearing and reconcile unit files 2,213 passed.run_pass) is covered by the unit tests.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 typecheckandmake test-unitpass; the integration suite passes if server behaviour changed.main; no merged migration is edited.frontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code