feat: Add CockroachDB Distributed SQL Backend With Hash-Sharded Keys - #11
Merged
Conversation
Reuse the Postgres schema and feed SQL, then encode range hotspots, hash sharding, and SERIALIZABLE retries on 40001.
ALTER PRIMARY KEY USING HASH on users left a unique secondary index that FKs still need, so users stay on a plain PK. posts still hash-shard, then drop posts_id_key so inserts do not append to an unhashed unique.
ThomasHartDev
force-pushed
the
thomas/feat/cockroach
branch
from
September 4, 2026 19:34
2fbedd2 to
91d9bd0
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.
Closes #10
Cockroach speaks Postgres, so CockroachStore reuses the same schema and LATERAL feed SQL. The new work is the layout: Keyspace models range splits, sequential keys pin writes to the rightmost range, and USING HASH scatters posts inserts and the global created_at index. Users stay on a plain TEXT PK because follows and posts reference users(id). After ALTER PRIMARY KEY on posts, the leftover unique on posts(id) is dropped so inserts do not keep appending to an unhashed unique. Follows and the author timeline stay ordered because those queries are prefix scans.
Default isolation is SERIALIZABLE. The store retries SQLSTATE 40001 with a short exponential backoff and leaves constraint errors alone. Tests run the shared contract on PGlite plus hotspot, layout, migrate(sql, true), and retry cases. On a real cluster, migrate(sql, true) applies CRDB_HASH_STATEMENTS against a pg Pool on Cockroach's Postgres wire (26257). CockroachStore.create() migrates without hash layout so PGlite can boot.