Repository navigation
Under Postgres, the dataset catalog lives in loom.knowledge_datasets, and an approval commits with the source's head - #465
Merged
Conversation
zlareb1
force-pushed
the
feat/knowledge-pg-datasets
branch
9 times, most recently
from
November 10, 2026 07:06
4cffacf to
deda7f7
Compare
8 of 9 tasks
zlareb1
force-pushed
the
feat/knowledge-pg-datasets
branch
2 times, most recently
from
November 10, 2026 11:10
edefd89 to
f2a2236
Compare
… and an approval commits with the source's head Every catalog write (an ingest's publish, an approval, a metadata edit's carry) commits to loom.knowledge_datasets over an older version only, then projects into loom.datasets. knowledge.dataset_get, knowledge.query and a publish's checks read the catalog there. An approval commits in the transaction that holds the source's head, only while the head is the document version reviewed and the catalog still names the snapshot reviewed. Deletes remove Postgres's catalog rows first; the erasure step and census cover the table. During a rolling upgrade a read also takes the catalog rows an older build wrote to ClickHouse. Related: #157 Signed-off-by: zlareb1 <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ion, and only rows Postgres wrote are projected pg_datasets.upsert returns the rows it wrote and insert_catalog projects only those; approve raises inside its transaction on a refusal so nothing commits and no standby wait can turn the refusal into a 503; forget deletes inside pg.transaction so a lost commit answer is outcome_unknown. The durable-reads inventory and the storage-authority failure table say so. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…ollback is tested pg_datasets.upsert writes every row through one INSERT ... SELECT FROM unnest and returns the keys the ON CONFLICT let through, instead of a round trip per row inside the job's lock. The rollback test covers the erasure-evidence refusal; the storage-authority failure row and the forget comment read plainly. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…ause _UPSERT and _APPROVE are built from _INSERT and _ON_CONFLICT, so a column is named once beside _args; _UPSERT's RETURNING lists the key in _args' order. A test docstring takes the forget comment's wording. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…taset snapshot the restore lost A dataset's rows live in ClickHouse alone while its catalog row is in Postgres, so a snapshot published or approved after the restore point reads with no rows. The reconcile lists every source whose catalog row names a snapshot written after its lower bound (a snapshot id is its write time), to upload and ingest again, and the backup page says so and names the catalog and jobs among what Postgres holds. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
… says so in its dry run A snapshot id is drawn when its rows' write begins, so a long publish across the restore point slipped past a filter on it; the row's version is drawn after the last rows land. The dry run prints the sources to ingest again, both restore procedures carry the step, and the page says rows may be missing, not that none are. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
zlareb1
force-pushed
the
feat/knowledge-pg-datasets
branch
from
November 10, 2026 13:14
f2a2236 to
7a3b9ad
Compare
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.
Related: #157
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 storage authority, the knowledge dataset catalog (which snapshot serves each dataset, and its schema approval) is kept in Postgres.
knowledge.dataset_approvecommits only while the document and snapshot it reviewed still serve.Why
The issue puts schema approvals and active serving pointers in Postgres. For datasets, both live in one place: the catalog row (
loom.datasets, aReplacingMergeTree(version)row per source, dataset and document version). That row says which snapshot ofloom.dataset_rowsserves the dataset, and carries its schema and approval.Under the Postgres authority, document heads, deliveries and ingestion jobs are already in Postgres, but this catalog was still read from ClickHouse. That had two problems:
This PR moves the catalog's authority into Postgres:
loom.knowledge_datasets(Postgres migration007_knowledge_datasets.sql) holds one row per (source, document version, dataset): the completeloom.datasetsrow, with the snapshot id, version and uploader beside it.datasets_repo.insert_catalogwrites an ingest's publish and a metadata edit's carry; an approval commits throughpg_datasets.approve(below) and projects withdatasets_repo.project. Under Postgres it commits each row over an older version only, asReplacingMergeTree(version)decides. Then it inserts the rows that commit wrote intoloom.datasets(a row kept out by a newer version is not projected), best effort with a warning when that fails, for a build that predates the table and a rollback to one (the erasure deletesloom.datasetsby scope, and counts Postgres's copy through its own census row). A dataset's rows are still written before the catalog row that names them.datasets_repo.at_version) reads Postgres, and the rows only ClickHouse holds:knowledge.dataset_get,knowledge.query, an approval's read and its recheck under the claim, a publish's and a metadata edit's carry, and a publish's check that no approval landed.pg_datasets.approve) under the namespace's lock that a document commit shares and an erasure holds alone, reading the source's headFOR SHARE. The write lands unless the head has moved past the document version reviewed (a later version, or that version deleted), an erasure removed that document (itsdoc_idinloom.erased_documents), or a write of the catalog row followed the one the approver read (WHERE version <= <read>); a row Postgres does not hold yet (an older build published it) is inserted. A source with no head (written before its namespace had heads) is approved too. Otherwise the transaction rolls back, so a refusal never waits for the standby, and it answers 409PRECONDITION_FAILEDwith nothing written, and the retype's own rows are deleted, as they are when the transaction failed without committing (a lock timeout, a refused connection). A commit whose answer was lost keeps them, since it may have landed (pg.may_have_committed). The source's claim still keeps an approval apart from an ingest's publish, which checks that no approval landed while it ran.forgetandforget_publishedremove Postgres's catalog rows first, each in a transaction, so a lost commit answer isoutcome_unknown. Then they delete the ClickHouse catalog rows and the snapshots' rows, using the dataset ids Postgres named as well as ClickHouse's, so a lost projection leaves no rows behind.forget_publishedalso keeps the snapshots Postgres's later rows serve.pg_knowledge.deletedeletes the scope's catalog rows together with its heads, deliveries and jobs, the catalog rows last. A user erasure also locks the namespaces holding the catalog rows Postgres holds of the user's (_lock_user_knowledge), so an approval of a headless source's row there waits for it; an approval adding a first Postgres row elsewhere is caught by the residue census. The table gets aPG_CENSUSrow and joinsPG_IN_USE_TABLES. A publish's catalog write reads its job's Postgres row in the same transaction (pg_datasets.upsert(job=…)): once an erasure deleted the job it writes nothing (JobErased, which the run takes asJobGone), and an erasure that comes after waits for it and deletes what it wrote. A run whose job another run took (the job still held, under a new token) is stopped by itsalivecheck before each catalog write, as the run's reads and saves are, not by this job read. A metadata edit's carry, a publish of a job only ClickHouse holds and an approval of a dataset only ClickHouse held have no Postgres row to read; one landing during the erasure is found by the residue census and the saga runs again.loom reconcile --restored clickhouselists, in its dry run too, each source with a catalog row written after its lower bound, to upload and ingest again (pg_datasets.sources_written_since). It filters on the row'sversion, which is drawn once the snapshot's last rows are inserted; the snapshot id is drawn when the write begins, so a long publish across the point would slip past it. A row an approval or a metadata edit wrote over surviving rows is named too.docs/operations/backup-and-recovery.mdadds the step to both restore procedures, and names the ingestion jobs and the dataset catalog among what Postgres holds.pg_datasets.storable), read and approved as onmain, rather than fail the write.dataset_rowbuilds its times to the millisecond, asDateTime64(3)keeps them, so a row read from Postgres compares equal to its ClickHouse copy.Alternatives considered:
loom.knowledge_heads), with the catalog left in ClickHouse. Rejected: an approval changes a dataset's schema and snapshot without a new document version. The head would then hold a copy of every dataset's catalog row, andknowledge_headswould be rewritten for a schema change.Also corrected:
docs/operations/postgres.mdsaidknowledge.searchandknowledge.querykeep serving during a Postgres outage. Search fails closed under this authority since the heads check was added, andknowledge.querynow reads the catalog there.Scope and risk
This PR touches the dataset catalog's store, the approval's switch, a publish's catalog write (fenced on its Postgres job), the Postgres knowledge erasure step and its census, and the output of
loom reconcile --restored clickhouse. It leaves alone:loom.dataset_rows), which stay in ClickHouse: written once per snapshot and never revised;dataset_row's millisecond times, which is whatDateTime64(3)stores anyway.Risks:
loom.datasetsone version behind until the row's next write. Reads under Postgres go to Postgres. An older build reads the lagging version meanwhile.knowledge.querymakes two per dataset). The ClickHouse read stays as it was, not sequentially consistent: a row an older build wrote that a replica has not caught up with is served on its next read; until then Postgres's older row serves, so a dataset the older build retyped can read empty for that window, and an approval over it still outranks it.knowledge.dataset_get,knowledge.dataset_approveandknowledge.queryanswer 503POSTGRES_UNAVAILABLEwhen Postgres cannot be reached under this authority.docs/operations/postgres.mdsays so.Compatibility, rollout and rollback
docs/architecture/rolling-upgrades.md, after main's rule 41; upgrade step 12): N-1 writesloom.datasetsalone. While it serves beside N:loom.datasetsrows alone and leave N's catalog rows in Postgres, so each is asked again with N (upgrade step 12, which now names both 005 and 007).loom.datasets, so N-1 serves it, except a row whose best-effort projection failed: N-1 reads that key a version behind until its next write.loom.knowledge_datasetsstays and is unused.007is the next free Postgres number onmain, after Under Postgres, loom reconcile runs again in a restored store the erasures and redactions the other records past the restore point #460's006_erasure_ledger.sql; the rolling-upgrade rule is 42, after erasure.status returns the erasure's receipt: every substrate its latest census counted, and the purge it applied #462's rule 41. The branch is based onmainec05d4c, which adds Under Postgres, loom reconcile runs again in a restored store the erasures and redactions the other records past the restore point #460 (the erasure ledger, whose per-step mark the catalog's erasure step now records too), Under Postgres, knowledge.inspect counts the ingestion jobs ClickHouse holds at an older version #466, and Code-index extractors and PR text read a file in linear time #463, Memory search's key-match arm splits a key at whitespace before stemming it #475 and A tracker source that lacks a feature column reads it as a default, so its PR-file reads no longer fail #477 (which share no files with this change).Tests
tests/unit/test_knowledge_datasets.pyadds eight cases under Postgres:loom.datasetsstill names;JobErasedending the run asJobGone;tests/unit/test_knowledge_erasure_pg.py: the knowledge erasure deletes the scope's catalog rows after the jobs, counted in what it removed.tests/unit/test_restore_reconcile.py: a ClickHouse restore prints the sources to ingest again, read from the reconcile's lower bound, and its dry run prints them too.tests/unit/test_knowledge_pg_datasets.py(13 tests):pg_datasets.forgetdeletes in a transaction;forgetdeletes Postgres's rows before ClickHouse's and deletes the rows of every dataset either store names;forget_publishedkeeps the snapshots Postgres's later rows serve and deletes the rows of every dataset either store names.LOOM_STORAGE_AUTHORITY=postgreson their own ClickHouse and Postgres, migrated from empty through007(pg_datasets.upsertwrites a batch in one statement and returns only the rows it wrote):tests/integration/test_knowledge_pg_datasets.py(11 tests):test_knowledge_query_live.pyandtest_knowledge_ingest_live.pygive 18 passed. They publish CSV, Parquet and XLSX datasets, approve them over the server and query them. Postgres's statistics forloom.knowledge_datasetsshowed 47 inserts and 28 updates across that run.test_knowledge*.py,test_*dataset*.py,test_erasure*.pyandtest_document*.py: 189 passed, 85 skipped. Three memory-plane tests that expect the ClickHouse authority fail, as onmain.pg_datasetsis registered intests/unit/test_pg_durable_reads.py: its writes are each its own transaction; of its two reads,at_versionreaches ClickHouse only as part of a write the caller commits, andsources_written_since(the restore reconcile's) only prints the sources to ingest again and sends nothing. Under Postgres, a publisher reads another session's commit durably before sending what it built from it to ClickHouse #456's and this PR's nearest unit files give 805 passed; the catalog, live, jobs and heads integration files give 77 passed.007):make format,make lintandmake typecheckpass, and the analyzer shapes check passes, since the ClickHouse statements are unchanged. The overlap's unit files (this PR's, Under Postgres, loom reconcile runs again in a restored store the erasures and redactions the other records past the restore point #460's reconcile and ledger files, Under Postgres, knowledge.inspect counts the ingestion jobs ClickHouse holds at an older version #466's inspect file, the durable-reads inventory, doc links and public copy) pass. Against a server on a ClickHouse and Postgres migrated from empty (Postgres001–007),test_knowledge_pg_datasets.py,test_knowledge_pg_jobs.py,test_knowledge_pg_heads.py,test_knowledge_ingest_live.py,test_knowledge_query_live.pyandtest_restore_reconcile.pypass. The full unit suite passes in 16 parts (-n 4 -p randomly, the slow parts split further andtest_profile_redaction.pyrun serially) on the head before Memory search's key-match arm splits a key at whitespace before stemming it #475 and A tracker source that lacks a feature column reads it as a default, so its PR-file reads no longer fail #477, which touch no file of this change or its tests; on the head that merges, the files those two change and this change's nearest files pass.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 author checks the design against the storage-authority and rolling-upgrade rules and reviews each change.
I can explain every line of this diff and how it interacts with the rest of Loom.
Checklist
make format-check,make lintandmake typecheckpass, andmake test-unitpasses in 16 parts (see Tests for the head); the integration suite passes if server behaviour changed.docs/reference/configuration.mdand thedocs/reference/tables (python scripts/codegen/docs_reference.py), the reach manifest,schemas/schema.sql(make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).main; no merged migration is edited.hooks/,lib/,bin/,skills/,.mcp.json, the Codexplugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.npm --prefix frontend run build) and its tests pass, iffrontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.tests/unit/test_public_copy.py).git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code