Skip to content

feat: Add CockroachDB Distributed SQL Backend With Hash-Sharded Keys - #11

Merged
ThomasHartDev merged 2 commits into
mainfrom
thomas/feat/cockroach
Sep 4, 2026
Merged

feat: Add CockroachDB Distributed SQL Backend With Hash-Sharded Keys#11
ThomasHartDev merged 2 commits into
mainfrom
thomas/feat/cockroach

Conversation

@ThomasHartDev

Copy link
Copy Markdown
Owner

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.

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
ThomasHartDev merged commit c403084 into main Sep 4, 2026
@ThomasHartDev
ThomasHartDev deleted the thomas/feat/cockroach branch September 4, 2026 19:34
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.

Add a CockroachDB distributed SQL backend (same SQL, hash-sharded keys, hotspots)

1 participant