Repository navigation
Under Postgres, a publisher reads another session's commit durably before sending what it built from it to ClickHouse - #456
Merged
Conversation
zlareb1
force-pushed
the
fix/pg-publish-durable-only
branch
4 times, most recently
from
November 10, 2026 03:59
1bcf99d to
7e6d60d
Compare
…us standby holds it A commit whose standby wait was cancelled is visible on the primary alone; the delivery workers published it, and a failover then left ClickHouse with rows of a revision the new primary never had. The publishers' claims, and the reads after a claim that send another session's commit, now run in pg.durable_transaction(), whose commit carries a logical message and waits for the standbys on the connection that read the rows. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
… carry the logical message Review: an empty synchronous_standby_names no longer refuses the publishers alone (it cost availability without the guarantee; the docs state a strict manager is needed); a claim that wrote rows needs no message; one predicate for a newer head copy; decision.record's extra standby round trip is named. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…redicate A commit's standby wait is outside the statement timeout, so an UPDATE claim already waited for the standbys and its client timeout raised; the transaction around it changed nothing. Review nits: newer_copy is not a journal read, the role test checks it runs as that role, a decision delete's extra round trip is named. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…redicates say what they answer, the lost-connection test says what it pins Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…s the GRANT; the head-copy path has an integration regression Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…he statement timeout; the lease check after a durable read says so; timing tests on a fake clock; the head-copy test claims only what it checks Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
A commit's standby wait is outside the statement timeout, so SET LOCAL statement_timeout = 0 changed nothing and cost a round trip on every write. The boot's grant names the text overload on every Postgres version; one comment, one helper moved. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…instead of two Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…he key's head is newer than its claim, a delete or other content as much as a copy Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
… read durably too Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…ain and durable reads disagree Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…ach claim's own revision: an erasure and a redaction in place too Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…the detached proxy Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
A redaction keeps the content hash, so the flush's refactored copy check took a redacted head for a copy and would build the writer's document, with the redacted text, onto it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…cases (a copy built onto, a failed durable read), the docs give the reason before the risk Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…ound trip is its own bullet, one contextlib import Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…; the lease tests use the shared clock fake and flush helper Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…py leaves redaction to embedded; tests use real rows On a server in recovery the logical message cannot take an xid (XX000), checked on the standby; it now expires the pool and answers 503 as a write's 25006 does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…entory's pg_delivery reason covers every head that is not the claim Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…ion tests check the 503 and its cause; a test pins that the usual flush reads no head durably Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…iting it out once per namespace 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]>
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…loors test reads Retry-After Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…and pg_jobs is in the durable-read inventory Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…rojection 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]>
zlareb1
force-pushed
the
fix/pg-publish-durable-only
branch
from
November 10, 2026 04:26
4371665 to
24c9deb
Compare
…_set's reason its dedup read 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]>
…lands Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…witch state included Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…ent names the driver states it covers Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
This was referenced Nov 10, 2026
Merged
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.
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 the Postgres authority, the delivery worker decides a claimed search document from a durable read whenever the key's head is not the claimed revision, a deleted decision's record retirement reads its writers durably, and a knowledge job whose projection lags is sent again only from a durable read, so a failover never leaves ClickHouse with a row, or without one, because of a commit the new primary does not have.
Why
A commit whose synchronous-standby wait is cancelled (a client timeout, a cancel) is committed on the primary alone and visible to every session there. After a failover the new primary never had that revision and reuses its
seq. ClickHouse must not keep rows of it.The delivery claims were already safe: a claim is an UPDATE of the rows it claims, its commit waits for the standbys, the server's statement timeout does not cover that wait (checked on Postgres 16), and a client timeout raises, so nothing claimed is sent.
docs/operations/postgres.mdsaid otherwise ("the delivery worker publishes it"), and the integration file now pins the claims' behaviour. Four paths were not safe:_still_headsand the writer flush's_served_documents, sends that head's row, another session's commit, read without a commit of its own;stalelist instead, which the worker decides again);ingest_run._takefrom a plain read, can be one a cut-off save left on the primary alone; after a failoverpg_jobs.adoptbrings it back from ClickHouse.The fix rests on one Postgres fact: a row becomes visible only after its own commit's standby wait has ended, so a later commit that writes WAL and returns from its own wait covers every row its transaction read.
The three publisher claims stay single statements: the memory delivery worker's
claim_due, the audit outbox'sclaim_outboxand the knowledge delivery worker'sclaim_due. Each is an UPDATE of the rows it claims, so its commit waits for the standbys. The server's statement timeout does not cover a commit's standby wait (checked on Postgres 16: an INSERT under a one-second timeout waited for an absent standby until the client gave up), and the client's command timeout raises, so nothing claimed is sent.Some reads after a claim send another session's commit:
_still_headsand the writer flush's_served_documents;ingest_run._takeprojects again, which it sends only when a durable read (pg_jobs.get(..., durable=True)) returns the same version.These are read again in
pg.durable_transaction(), and the decision is made from that read. Its last statement writes a transactional logical message (pg_logical_emit_message(true, 'loom.durable', '')), so its COMMIT waits for the standbys although the transaction wrote nothing else (a transaction that only reads, or only takes an xid, writes no WAL and does not wait). The read and the commit share one connection, so a failover in between fails the commit instead of passing on another primary. A writer's flush otherwise sends only its own commits, which already returned from their wait, so the usual flush gains no round trip.A lease already spent before the durable read counts as the send's own, as before (
pg_lease.check_lease); one the standby wait itself ended stops the pass as a Postgres outage, releasing the claims uncounted (pg_lease.check_lease_after_durable_read).The window on
mainis narrow: a publish's own commits before the head read (its claim, its renewal, its rows' completion) each wait for the standbys, so only a copy cut off between the last of them and the head read was sent.Boot refuses a role that may not write the message, checked in a rolled-back transaction so a missing standby does not stall boot, and names the
GRANTthat fixes it.A durable read on a demoted primary (its logical message cannot take an xid in recovery, SQLSTATE XX000) counts as the read-only outage a write's 25006 is: the pool is expired and the caller gets a 503.
pg.transaction()no longer runsSET LOCAL statement_timeout = 0before its COMMIT: Postgres stops the statement timeout before it commits, so the statement changed nothing and cost a round trip on every write. The integration case for a cut-off commit (outcome_unknownunder a one-second timeout) passes without it.Not refused: an empty
synchronous_standby_names. A non-strict HA manager empties it when its standby drops, and every commit is then the primary's alone, writes included; refusing only the publishers cost availability without restoring the guarantee. The docs state that the guarantee needs a strict manager, asLOOM_PG_DURABILITY=localalready says for its own case.Rejected alternatives:
pg_current_wal_lsn()withpg_stat_replication.flush_lsn. Read at a pass's start, it misses commits that become visible before the claim. It also reimplementsFIRST n/ANY ncounting, reads NULL for roles that cannot seepg_stat_replication, and has to poll.Scope and risk
Touches the two head re-reads, the decision-record retirement, the knowledge job re-projection in
ingest_run._take, the boot check and the docs. Reads served to callers are unchanged: a read of Postgres can still see a commit whose standby wait was cancelled. That is Postgres's own visibility, and this change only keeps such commits out of ClickHouse.bulk_set's dedup against another session's unpublished keeper belongs to that class and is not changed here, including the usage event it records for the keeper's id.tests/unit/test_pg_durable_reads.pypins every module that calls a read ofpg_memory,pg_documentsorpg_jobs, with why its use is safe. A new caller fails until it is classified there.With the standby detached:
LOOM_PG_STATEMENT_TIMEOUT_MSplus one second, then the pass ends as it does during a Postgres outage.LOOM_PG_DELIVERY_LEASE_SECONDSends after the standby is back, so searches in those namespaces wait for it at the barrier.LOOM_PG_COMMIT_LOCK_TIMEOUT_MS, then answers its retryable 503, as its own commit would.The guarantee holds when a failover promotes a standby of the synchronous set that holds the highest flush position, which a strict HA manager does. The docs state this condition.
Change-data-capture consumers of the Postgres database receive one transactional logical message with the prefix
loom.durableper durable re-read.decision.recordchecks its writers durably after its record lands, and a delete of a decision retires its record durably in its flush, so each waits for one more standby round trip.Compatibility, rollout and rollback
pg_logical_emit_message(its text overload; Postgres 17 added a fourth argument with a default). It is executable by PUBLIC on Postgres 16, 17 and 18 unless revoked, and boot refuses a role that may not, naming the grant.Tests
tests/unit/test_pg_durable_reads.py:stale;test_a_job_is_projected_again_only_from_a_durable_read);tests/integration/test_pg_durable_publish.py, in the Postgres authority job on 16 and 18 (primary plus synchronous standby, workers off). It pointssynchronous_standby_namesat an absent standby and checks:outcome_unknown;POSTGRES_UNAVAILABLE, not the driver's error;memory.setcut off at its commit wait raises at its claim while the standby lacks it, so no pass sends it and it is not in ClickHouse, then is published once the standby is back.main's plain read serves the copy (checked: the test fails there withDID NOT RAISE, its call adapted tomain's one-argument_still_heads).test_pg_durable_reads,test_pg_authority,test_pg_batch,test_pg_memory_reads,test_knowledge_pg_deliveryand the others that useserver.core.pg: 2262 tests).tests/integration/test_pg_durable_publish.pypasses 6 of 6 against a local server on Postgres 16 with a synchronous standby. The rest of the Postgres authority integration files run in this PR's CI.pg.transaction()raised the driver'sInterfaceError(a 500). asyncpg detaches the pooled connection's proxy when the connection is lost, so theterminate()andis_closed()calls on it raised, replacing the outage. They now treat a detached proxy as closed, and the caller gets 503POSTGRES_UNAVAILABLE. CI then showed the same loss surfacing as asyncpg'sInternalClientError(its protocol caught mid-switch when the server ended the connection between two statements). Now anInternalClientErrorof any kind is a 503 outage for every Postgres call, and insidetransaction()androlled_back()any error on a connection that reads closed is 503 too, the commit's markedoutcome_unknown.is_outagehas no retry loop of its own; a delivery worker releases a claim uncounted on a 503, so only a driver error that repeated on the same row could keep it from parking, and a stale schema cache clears on the next try.Two test fixes outside the durable read itself:
live_decision_writersand thepg_getfake intests/unit/test_knowledge_ingest.pynow take the newdurablekeyword.tests/integration/test_knowledge_retrieval_floors.pynow waits out a 429. Its corpus is ingested back to back. The new integration file moves this test into another CI shard, and there those ingests drain the namespace's ingest bucket.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. Each Postgres claim was checked against the commit path (a commit becomes visible after its standby wait; a transaction that writes no WAL does not wait).
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. Locally: format and lint on the changed files, the affected unit files and the new integration file; the full suites run in CI.main; no merged migration is edited: no migration.frontend/changed: not changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.git commit -s).🤖 Generated with Claude Code