Skip to content

chore(database): CockroachDB proof of concept (do not merge) - #3633

Draft
fforootd wants to merge 15 commits into
mainfrom
claude/project-thread-u615w4
Draft

fforootd wants to merge 15 commits into
mainfrom
claude/project-thread-u615w4

Conversation

@fforootd

@fforootd fforootd commented Dec 3, 2026

Copy link
Copy Markdown
Member

Context

  • Proof of concept for running nextgen on CockroachDB (multi-region cells). Not meant to merge as is.

Summary

  • The Postgres dialect detects CockroachDB from the crdb_version startup parameter and then:
    • migrates a fresh database from one baseline (migration/cockroach/baseline.sql: the final schema plus the seeded authz catalog, generated from a migrated Postgres by generate.sh + transform.py), records every goose version, and locks migrations with a lease row instead of an advisory lock;
    • writes events without the advisory lock and bounds "settled" events with a fixed 5 s margin (known gap, see event.go);
    • retries transactions that fail with SQLSTATE 40001.
  • Portable fixes that also run on Postgres: RETURNING on data-modifying CTEs, jsonb_array_elements instead of a typed jsonb_to_recordset column list, ::TEXT on authz arguments compared to each other.
  • The cloud launcher serves a preview from CLOUD_CRDB_DATABASE_URL when it is set (branch-scoped only). vercel.json runs the server in fra1 and iad1 on this branch to measure latency from both regions.

Validation

  • ZITADEL_TEST_POSTGRES_URL=<local CockroachDB v26.1> go test -tags postgres_integration ./internal/storage/stmttest/: 331 of 335 top-level tests pass. Left: 3 event-cursor tests (the margin above) and TestUserPasswordStatements_HistoryUnderConcurrentChanges.
  • ZITADEL_TEST_POSTGRES_URL=<local Postgres 18> go test -tags postgres_integration ./internal/storage/stmttest/ ./internal/storage/dialect/postgres/...: all pass.
  • go test ./internal/storage/dialect/... ./cmd/cloud-launcher/: pass.

Release notes / changeset

  • No changeset required — no shipped behavior changed.

Notes

  • Next on this branch: regional-by-row tables (rows follow the user's region with a project policy override, platform tables GLOBAL), a 3-region local simulation, and latency numbers from the preview.

Generated by Claude Code

CockroachDB POC for the cell architecture. The Postgres dialect now
detects CockroachDB from the crdb_version startup parameter and:

- migrates a fresh database from one baseline (the final schema plus the
  seeded authz catalog, dumped from Postgres and transformed by
  migration/cockroach/transform.py), records every goose version, and
  locks migrations with a lease row instead of an advisory lock;
- writes events without the advisory lock and bounds "settled" events
  with a fixed 5 s margin (a known gap, documented in event.go);
- retries transactions that fail with SQLSTATE 40001.

Portable fixes that also run on Postgres: RETURNING on data-modifying
CTEs, jsonb_array_elements instead of a typed jsonb_to_recordset column
list, and ::TEXT on authz arguments compared to each other.

The cloud launcher takes CLOUD_CRDB_DATABASE_URL on a preview and then
serves from it directly. vercel.json runs the server in fra1 and iad1 on
this branch only, to measure latency from both regions.

Storage contract tests on CockroachDB v26.1: 331 of 335 pass. The 4 left
are the event cursor margin and one concurrency test. Postgres: all pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu
@fforootd fforootd self-assigned this Dec 3, 2026
@vercel

vercel Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
cloud Building Building Preview Dec 4, 2026 12:14am UTC

Request Review

Schema-change jobs on a multi-region cluster take seconds each: the
statement-by-statement baseline ran over 11 minutes on a simulated
3-region cluster and stalled the preview build. In one transaction the
new tables need no jobs and the baseline takes about a minute. Triggers
follow the commit (declarative schema changer only). An unfinished
baseline (no versions recorded) is dropped and redone; the migration
lock moves to the public schema for that.

Adds a latency benchmark (build tag crdb_bench) for the sign-in path
from each region's gateway.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu

fforootd commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

full-pr fails on one test only: TestVercelJSONKeepsTheServerInFrankfurt (apps/api/cmd/cloud-launcher/main_test.go:675). This PR deliberately runs the server function in fra1 and iad1 so that the CockroachDB proof of concept can measure latency from both regions. The guard is correct for PlanetScale, so the test stays as it is. The region change has to be reverted before anything from this branch merges. Everything else in test:postgres and test:unit passes.


Generated by Claude Code

regional.sql classifies the tables: end-user data (users, credentials,
sessions, tokens, consents, events, ...) is REGIONAL BY ROW on a
home_region column that defaults to the gateway's region, and a project
policy (projects.data_region) overrides it through a BEFORE INSERT
trigger. Configuration and catalogs are GLOBAL. regionalize.py turns
baseline.sql into a setup that creates every table with its locality in
one transaction (about 2.5 minutes on a simulated 3-region cluster,
where converting existing tables took about a minute per table).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu
Generated IDs can carry a region code in the first random character of
their ULID (ZITADEL_ID_REGION). place_ids.py builds a schema where each
per-user table derives its home region from that code, and keeps unique
attribute values, hashed, in a GLOBAL table maintained by a trigger. The
dialect reads unique lookups from that table when it exists. The latency
bench gains an email lookup and per-gateway region codes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu
… sin1

With ZITADEL_CRDB_REGIONS set, the migration makes the database span
those regions and applies baseline_regional.sql (place_ids.py output with
region placeholders), replacing a single-region schema. IDs take their
region code from the Vercel region (ZITADEL_ID_REGIONS). With
ZITADEL_CRDB_PROBE set, /api/__crdb_probe runs the sign-in path inside the
function and reports its timings, also for users homed in other regions.

This deployment pins the server to sin1 to probe from Asia.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu
Checks are joined and looked up by their session or auth attempt, so they
take their region from it. The unique index on a session's token ID made
every token issue check all regions; the ID is minted, so the index is a
plain one on CockroachDB. The migration records the hash of the regional
baseline and replaces a schema made from another one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu
A check's home_region follows its session or auth attempt, but CockroachDB
cannot derive it from session_id or auth_attempt_id and searched every
region. On the regional schema the statements now name the parent's region.
Local three-region bench: get session 150 ms -> 2.5 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu
…al, restricted placement

- withTransaction re-runs a transaction it opened after a serialization
  failure on CockroachDB; savepoints leave the retry to the outer one.
  The retry backoff gets jitter.
- authz_assignments take their region from principal_id, which every
  authorization check reads them by, so the check stays in one region.
- The database gets PLACEMENT RESTRICTED: a regional row keeps all its
  replicas in its home region.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu
CockroachDB Cloud closes crdb_internal to SQL users, which failed the
preview migration. Setting the policy again is a no-op.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu
- Turn generic plans off on CockroachDB connections. A generic plan is made
  without the placeholder values, so it cannot compute a row's home region
  from its id and reads every region's partition; under plan_cache_mode=auto
  a statement switches to it after five runs (locally: 196 ms instead of
  66 ms for one user's attributes).
- Make authz_assignments, authz_membership_edges and resource_scope_index
  GLOBAL. Every check reads them by columns that do not hold the region, so
  a check read every region several times (p50 ~770 ms on the preview).
  The change runs as an in-place step after the baseline, so the existing
  schema and its data stay.
- Tag telemetry with cloud.region from VERCEL_REGION.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu
…tions

- resource_scope_index goes back to REGIONAL BY ROW. Every new session and
  user writes a row there, and as a GLOBAL table each such write waited out
  the commit (create session 0.2 s -> 0.6 s from iad1). Its reads name the
  resource, whose id holds the region.
- CockroachDB connections end a transaction left idle for 30 s. A function
  frozen mid-transaction kept its schema lease and stalled the locality
  change of the last deploy for over ten minutes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156sXA5XcDpDy6mbvfGCSfu

This branch was successfully deployed

1 active (outdated) deployment
Preview — 12566f57 Deployed Dec 4, 2026 by vercel[bot]
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.

2 participants