Skip to content
Divyansh-chaudharyPublic

About

ZAMP assignment

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

dbgit

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.

What it does

  • Branch a tracked schema instantly. Branches are real Postgres schemas (dbgit_br_<name>) cloned with CREATE TABLE … LIKE … INCLUDING ALL. DDL only. Optional sample rows. The 5GB stays on main.
  • Edit with real SQL on the branch. search_path is pinned so unqualified ALTER TABLE orders cannot hit production. DDL on main is 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 NULL goes through CHECK … NOT VALID then VALIDATE. Indexes build CONCURRENTLY. FKs are added NOT VALID.
  • Apply online. Short lock_timeout, jittered retry, named blockers from pg_stat_activity. The number the UI compares is max blocking lock time, not total runtime. VALIDATE / CREATE INDEX CONCURRENTLY scan the table under SHARE UPDATE EXCLUSIVE — they do not count as blocking writes.

One-command local run

docker compose up --build

Open 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 --build

Local without Compose

Needs 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:3000

Bring your own database

The home page accepts a connection string. dbgit will:

  1. Check it can connect and CREATE schemas (branches are schemas).
  2. Snapshot whatever is in the tracked schema (default public) as main.
  3. 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.

The reviewer's 5GB test

  1. Seed or point at a table around 5GB (SEED_ROWS=40000000 on the bundled orders table, or your own).
  2. Create a branch — should return in seconds regardless of table size.
  3. On the branch, run alter table orders alter column amount type bigint; and commit.
  4. 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).
  5. 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

How identity works

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):

  • attnum survives RENAME and ALTER TYPE
  • pg_class.oid survives a heap rewrite (relfilenode changes)
  • varchar widen / to text is rewrite-free; int → bigint rewrites
  • SET NOT NULL with a proven CHECK is ~O(1); without it, a full scan under ACCESS EXCLUSIVE

Tests

npm test                     # unit + property tests, no Postgres
TEST_DATABASE_URL=postgres://… npm run test:integration

Integration 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.

Deploy

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.

Vercel + Neon

  1. Create a Neon project with two databases: dbgit_meta and dbgit_demo.

  2. 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>
    
  3. 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.

Layout

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.

About

ZAMP assignment

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages