Topic 2 of 5 · 5 min read
Branch and scratch databases for agent work
Goal: Plan an agent task so its schema changes run on a branch first and reach production only through a reviewed migration.
Let an agent change a database only on a copy of it, and let the change reach production only as a migration file a person has reviewed. The branch is where the agent is allowed to be wrong.
Check yourself · 1 of 3
What is the main purpose of running an agent's schema change on a database branch first?
Why it matters
Agents are useful for schema work: adding a column, rewriting a slow query, trying an index. They are also confident when wrong. A migration that looks fine can lock a large table, drop a column another service reads, or fail halfway. You want the failure to happen somewhere nobody depends on.
Three places to run agent work
- A database branch. Services such as Neon create a branch from a parent database in moments, with its own endpoint. The agent gets a connection string for the branch and the parent is untouched. Delete the branch when the task ends.
- A restored copy. Restore last night's backup into a scratch instance. It is slower, and it also proves your backups restore.
- A local container. Run PostgreSQL in Docker with the schema loaded from your migrations and a small seed. Cheapest, but it does not show how the change behaves on production-sized data.
How to plan the task
- Create the scratch database and give the agent only that connection string. Production credentials are not in its environment.
- State the task and the exit condition: "add
orders.cancelled_at, backfill fromstatus_history, make existing tests pass". - Have the agent write a migration file, not run ad hoc DDL, so the change is a diff you can read.
- Run the migration on the branch and check it: row counts, the timing, the query plan, and the rollback.
- A person reviews the file and applies it to production through the normal deploy path.
- Delete the branch.
Before and after
Before: "Add a cancelled_at column and backfill it" with the production URL in .env. The agent runs ALTER TABLE and an UPDATE on the live table, and the backfill holds locks during peak traffic.
After: the plan below.
Goal: add orders.cancelled_at and backfill it
1. Create branch agent/cancelled-at from production (schema only)
2. Agent writes migrations/0042_cancelled_at.sql and runs it on the branch
3. Check row counts, timing and EXPLAIN on the branch
4. Human reviews the migration; deploy applies it to production
5. Delete the branch
What goes wrong
- A branch copies production data. Customer emails now live in a scratch database with weaker access. Use schema-only branches or masked data when the task does not need real rows.
- Production credentials in the agent's environment. Then the branch is optional in practice. Keep them out.
- Small data hides the problem. A backfill that takes a second locally can take minutes on the real table. Test on realistic volume.
- Applying the agent's session by hand. Copying statements from a chat to production skips review. Ship the migration file.
Examples
Restored copy as a double test
A team restores last night's backup into a scratch instance for an agent to try an index change. The restore takes two hours, but it also confirms the backup is usable, which a branch would not. Use this when the change is risky or when you have never tested a restore.
Local container for a small change
Adding a nullable column to a small internal table: the agent runs PostgreSQL in Docker, applies all existing migrations, then its new one, and runs the test suite. No cloud cost, no real data. It tells you the SQL is valid, not how long it takes on the real table.
Key points
- Give the agent a branch, restored copy or local container, and keep production credentials out of its environment.
- Have the agent write a migration file so the change is a reviewable diff, not ad hoc statements.
- Check timing, row counts and the query plan on the branch, and prefer production-sized data for backfills.
- A branch copies production data, so use schema-only or masked data when real rows are not needed.
- Delete the scratch database when the task ends.
Common mistakes
- Pointing the agent at production "just to look" and then letting it make the change there.
- Copying SQL out of a chat window into production instead of shipping a migration file.
- Testing only on a tiny seed, so lock time and backfill duration are never seen.
- Leaving branches with copied customer data running after the task is finished.
Check yourself · 2 of 3
An agent created a branch from production to test a migration that touches only the schema. What is a sensible precaution?
Check yourself · 3 of 3
A backfill runs in one second in a local Docker database with 200 seeded rows. Why is that not enough to approve it for production?
Practice items, not counted towards your mastery.
Start here
- PostgreSQL privileges overview (opens in a new tab)DocsFree
GRANT, REVOKE and default privileges: the mechanism that decides what an agent role can touch.
- PostgreSQL: Row security policies (opens in a new tab)DocsFree
How CREATE POLICY limits the rows a role sees, including the owner-bypass rule.
- Supabase: Model Context Protocol (opens in a new tab)DocsFree
The hosted MCP server's project scoping, read-only mode and feature groups.
More for this topic
- Neon: Database branching (opens in a new tab)DocsFree
How copy-on-write branches give an agent a disposable copy of schema and data.