Version control for Postgres schemas: branch, diff, merge, then apply the result on a real database. Built for the case where main holds ~5GB of data and a naive ALTER TABLE ... TYPE would take ACCESS EXCLUSIVE for minutes.
The hosted demo is a small seed (free-tier Postgres cannot hold 5GB). The 5GB path is local Compose or a connection string you bring — that is first-class, not an afterthought.
- Branch a tracked schema instantly. Branches are real Postgres schemas (
dbgit_br_<name>) cloned withCREATE TABLE … LIKE … INCLUDING ALL. DDL only. Optional sample rows. The 5GB stays onmain. - Edit with real SQL on the branch.
search_pathis pinned so unqualifiedALTER TABLE orderscannot hit production. DDL onmainis refused; production changes go through merge. - Diff using catalog identity (
pg_class.oid,pg_attribute.attnum), not name heuristics. A rename is a rename. Drop then re-add of the same name is drop+add. - Three-way merge against a true lowest common ancestor. Criss-cross histories take the deepest base rather than inventing a virtual schema.
- Plan the apply: widen-in-place when Postgres can do it without a rewrite; otherwise expand/contract (shadow column, dual-write trigger, batched backfill, atomic rename swap).
SET NOT NULLgoes throughCHECK … NOT VALIDthenVALIDATE. Indexes buildCONCURRENTLY. FKs are addedNOT VALID. - Apply online. Short
lock_timeout, jittered retry, named blockers frompg_stat_activity. The number the UI compares is max blocking lock time, not total runtime.VALIDATE/CREATE INDEX CONCURRENTLYscan the table under SHARE UPDATE EXCLUSIVE — they do not count as blocking writes.
docker compose up --buildOpen http://localhost:3000. Compose starts Postgres 17, creates dbgit_meta and dbgit_demo, seeds 50k orders, registers the demo target, then runs the app and a long-lived worker.
# genuine ~5GB table (orders ≈ 40 million rows). Seed takes a while.
SEED_ROWS=40000000 docker compose up --buildNeeds Node 20+ and Postgres 12+ (17 is what was spiked and tested).
cp .env.example .env
# point META_DATABASE_URL and DEMO_DATABASE_URL at Postgres
# create the two databases first:
# createdb dbgit_meta && createdb dbgit_demo
npm install
npx tsx scripts/setup.ts # meta schema + seed + register demo
npm run worker # long-lived executor, separate terminal
npm run dev # http://localhost:3000The home page accepts a connection string. dbgit will:
- Check it can connect and
CREATEschemas (branches are schemas). - Snapshot whatever is in the tracked schema (default
public) asmain. - Never copy your data into a branch.
Use this to point a reviewer laptop at a 5GB database you already have. dbgit's metadata lives in META_DATABASE_URL, which is a different database on purpose: dropping a branch cannot drop the history that describes it.
Connection strings are stored encrypted when DBGIT_SECRET is set.
- Seed or point at a table around 5GB (
SEED_ROWS=40000000on the bundledorderstable, or your own). - Create a branch — should return in seconds regardless of table size.
- On the branch, run
alter table orders alter column amount type bigint;and commit. - Review merge into
main. The plan should show expand/contract (shadow column, dual-write, backfill, swap), not a table rewrite. Compare naive max lock (minutes) vs dbgit max lock (milliseconds). - Apply. On the job page, click Drive live traffic. Reads and writes should keep succeeding; p99 should not pin to a lock wait. Abort is cooperative: compensating steps drop the trigger and shadow column.
Expected order of magnitude on a modest machine (network-attached storage is slower):
| Step | 50k rows | ~5GB / 40M rows |
|---|---|---|
| Create branch | < 1s | < 1s (DDL only) |
Naive ALTER TYPE lock |
seconds | minutes, ACCESS EXCLUSIVE |
| dbgit backfill | a few seconds | minutes, no write-blocking lock |
| Swap / rename | milliseconds | milliseconds |
Physical identity is (oid, attnum). Postgres never reuses a dropped attnum; a column dropped and re-added under the same name gets a new number. After CREATE TABLE LIKE, new OIDs are rebound to the source logical ids by name, which is only safe at clone time. After that, diffs key on logical id.
Measured on Postgres 17 (see spike/RESULTS.md):
attnumsurvivesRENAMEandALTER TYPEpg_class.oidsurvives a heap rewrite (relfilenodechanges)varcharwiden / totextis rewrite-free;int→bigintrewritesSET NOT NULLwith a proven CHECK is ~O(1); without it, a full scan under ACCESS EXCLUSIVE
npm test # unit + property tests, no Postgres
TEST_DATABASE_URL=postgres://… npm run test:integrationIntegration tests create and drop their own databases. They cover rename vs drop+add on a real catalog, lock_timeout vs the lock queue, backfill catching writes behind the cursor, and expand/contract under concurrent inserts.
The assignment asks for a public URL. The hosted demo is a small seed (Neon free tier cannot hold 5GB). The 5GB claim is exercised locally as above.
-
Create a Neon project with two databases:
dbgit_metaanddbgit_demo. -
Vercel project from this repo. Environment:
META_DATABASE_URL=postgres://…/dbgit_meta?sslmode=require DEMO_DATABASE_URL=postgres://…/dbgit_demo?sslmode=require DBGIT_SECRET=<random 32+ chars> -
After first deploy, seed once from a laptop that can reach Neon:
META_DATABASE_URL=… DEMO_DATABASE_URL=… SEED_ROWS=50000 npx tsx scripts/setup.ts
There is no long-lived worker on Vercel. The job page ticks POST /api/targets/:id/jobs/:id from the browser, which drives the executor a request at a time. That is enough for the 50k-row demo; a 5GB backfill needs the Docker worker (or any always-on process running npm run worker).
Hobby-plan functions time out around a minute. The executor is a resumable state machine: each tick continues from the last committed backfill cursor.
src/core/ IR, diff, three-way merge, commit DAG, migration planner (pure)
src/db/ pg_catalog introspect, metadata store, online executor
src/services branch / commit / merge / DDL / workload
src/app/ Next.js UI + API
src/worker/ long-lived job runner
spike/ catalog-identity measurements against Postgres 17
See decisions.md for why it is built this way.