Skip to main content

Merge branches is here

Branches now merge back into their parent in plain SQL: MERGE, REFRESH and RESET DATABASE, plus merge requests for agents.

November 4, 2026 · 5 min readProductReleaseEngineering

With the latest edge build of ScramDB, we're bringing the full lifecycle of git-like data branches. Go build your AgentHub!

Branches, end to end1:30

Until now a branch was a one-way trip. You branched, tested your change, deleted the branch, and then wrote the same change again as a migration for production. Twice the work, and the second time on the database you can't afford to break.

Now branches merge back, in plain SQL. On top of CREATE DATABASE ... CLONE you get three new statements:

  • MERGE DATABASE sends your branch's changes back to its parent.
  • REFRESH DATABASE pulls the parent's latest changes into your branch.
  • RESET DATABASE throws away your branch's changes so you can start over.

Try the risky change on a branch. If it works, merge it. If it doesn't, reset it.

That's GitOps for your data, with no extra tooling: branch, review, merge, roll back, all inside the database. Hand the same loop to an AI agent and you get AgentOps out of the box. It's built for people trying a change before production sees it, for agents doing work a person signs off on, and for A/B tests where two versions run side by side and the winner gets merged.

Branch. Merge. Or reset.

A branch, start to finish
scramdbparentpricing_v2branchCLONEREFRESHRESETMERGE
MERGE DATABASE pricing_v2 INTO scramdb;The branch's rows and schema changes land on the parent in one transaction.

For people: a branch, start to finish​

CLONE makes the branch without copying any data, so a huge parent branches as fast as a tiny one. The branch reads the parent's storage and only pays for the pages it rewrites.

sql
-- connected to scramdb
CREATE TABLE plans (id int PRIMARY KEY, name text NOT NULL, price_cents int NOT NULL);
INSERT INTO plans VALUES (1, 'starter', 900), (2, 'team', 4900), (3, 'business', 19900);

CREATE DATABASE pricing_v2 CLONE scramdb;

Connect to pricing_v2 and make the change you're nervous about. In this example we raise the team plan's price, give it five seats, and add an enterprise tier, which needs a new seats column first:

sql
-- connected to pricing_v2
ALTER TABLE plans ADD COLUMN seats int NOT NULL DEFAULT 1;
UPDATE plans SET price_cents = 5900, seats = 5 WHERE name = 'team';
INSERT INTO plans VALUES (4, 'enterprise', 99900, 50);

Production hasn't moved. Point your tests, your app or your reports at the branch for as long as you like. Once you trust the change, connect back to the parent and ask what a merge would do:

sql
-- connected to scramdb
SELECT * FROM scram.merge_preview('pricing_v2');
text
table | inserted | updated | deleted | conflicts | conflict_detail
--------------+----------+---------+---------+-----------+-------------------------------------------------------------
public.plans | 1 | 1 | 0 | 0 | {"items":[],"listed":0,"schema":["add column seats integer"],"total":0}

The preview counts one inserted row and one updated row, and it lists the seats column it would add. It writes nothing. If that's what you expected, merge:

sql
MERGE DATABASE pricing_v2 INTO scramdb;

Column first, then rows, all inside one transaction, so a failure halfway through leaves the parent exactly as it was.

When both sides changed the same row​

Production doesn't wait for you. While you were testing, somebody in sales dropped the team plan to 39.00 for a promo. Rows are matched by primary key, and a key that changed on both sides since the branch was made is a conflict. By default the merge applies nothing and lists every conflict it found:

text
table | inserted | updated | deleted | conflicts | conflict_detail | committed
--------------+----------+---------+---------+-----------+---------------------------------------+-----------
public.plans | 1 | 0 | 0 | 1 | {"items":["2: both updated"], ...} | f

Nothing was applied. You pick the winner and run it again. Try each policy here:

Row 2, changed on both sideson_conflict
When you branchedteam49.00 
On your branchteam59.005 seats
On productionteam39.00 
Production after the mergeNothing applied
team39.00
The default. The merge lists every conflict ("2: both updated") and the parent stays exactly as it was.MERGE DATABASE pricing_v2 INTO scramdb;

The function route is the interesting one. Your function gets the row as it was when you branched and as each side has it now, and returns the row to keep, or NULL to delete it:

sql
CREATE FUNCTION public.pick(tbl text, key jsonb, base jsonb, branch jsonb, parent jsonb)
RETURNS jsonb LANGUAGE sql AS $$ SELECT coalesce(branch, parent) $$;

MERGE DATABASE pricing_v2 INTO scramdb WITH (on_conflict = 'function:public.pick');

Add dry_run = on to any of these and you get the merge's answer without a single write.

Keeping a branch current​

If a branch stays open for a week, production keeps moving without it. Think of REFRESH DATABASE as a rebase. Run it on the branch to pull in whatever landed on production since you branched; your own edits are kept. The next merge still sends every change you made, before the refresh and after it.

sql
-- connected to pricing_v2
REFRESH DATABASE pricing_v2 FROM scramdb;
REFRESH DATABASE pricing_v2 FROM scramdb WITH (on_conflict = parent);
REFRESH DATABASE pricing_v2 FROM scramdb AT TIMESTAMP '2026-11-03 18:00:00';

With AT TIMESTAMP the refresh stops at that moment, which helps when last night's batch job is exactly what you want to keep out.

Starting over​

Maybe the new pricing fell apart in testing. You could drop the branch and clone a fresh one, but a reset does it in place and the branch keeps its name, so your connection strings still point at it:

sql
-- connected to scramdb
RESET DATABASE pricing_v2;

Your changes are gone and the branch matches the parent again. And if the branch should simply become production, swap the names: ALTER DATABASE app SWAP WITH app_next.

For A/B tests: two branches, one winner​

When your team can't agree on two versions, run both and let the data pick. Make a branch per variant, put one idea on each, and send them the same workload.

Two branches, one winner
scramdbproductionpricing_adroppedpricing_bper-seatMERGE
  1. 1
    CREATE DATABASE pricing_a CLONE scramdb; CREATE DATABASE pricing_b CLONE scramdb;Two branches, each with the full data.
  2. 2
    The same workload against bothA adds a cheaper tier. B moves everyone to per-seat pricing.
  3. 3
    MERGE DATABASE pricing_b INTO scramdb WITH (drop_branch = on);The winner lands on production and its branch is dropped.
  4. 4
    DROP DATABASE pricing_a;Or RESET DATABASE pricing_a for another round.

Each branch has the full data, so whatever you measure (query speed, report totals, how your app behaves) comes from real rows, not a sample. Both branches can take REFRESH DATABASE while the test runs, so neither falls behind production. When the numbers are in, merge the winner and drop the other. Want another round instead? Keep the losing branch and run RESET DATABASE pricing_a. It starts again from production as it is now, winner included.

For agents: AgentOps, a token and not the keys​

Agents try a lot of things, and some of those things are bad ideas, so we give them a branch. Whatever an agent rewrites there, production never sees. The tricky moment comes when it finishes and you actually like the result. Giving it write access to production just to land that result would defeat the point of the branch.

So an agent gets its own branch, its own role and a token to connect with:

sql
CREATE AGENT analyst WITH (clone_from = 'scramdb', quota_bytes = '1 GB',
exclude = 'public.salaries');
CREATE AGENT TOKEN FOR analyst VALID FOR '1 day'; -- shown once

The token is shown once and stored only as a salted hash. The agent's role can connect to its own branch and nothing else, and it can't mint tokens or read the token records. exclude hides a table, or just the rows matching a predicate, from the agent. quota_bytes caps how much the branch may grow. To cut an agent off, DROP AGENT TOKENS FOR analyst kills all of its tokens.

So the agent never merges. It sends a merge request, and you approve it or you don't. That's the whole AgentOps loop, and it ships with the database:

The agent asks, you decide
YouAgent
  1. YouCREATE AGENT analyst WITH (clone_from = 'scramdb');Its own branch and its own role.
  2. YouCREATE AGENT TOKEN FOR analyst VALID FOR '1 day';Shown once, stored only as a salted hash.
  3. AgentConnects to its branch with the tokenIt can rewrite anything there. Production never sees it.
  4. AgentREQUEST MERGE OF DATABASE agent_analyst;A request id and the preview, table by table.
  5. RefusedAPPLY MERGE REQUEST 'mr-...' USING TOKEN '...';Refused (42501). An agent never applies a merge.
  6. YouAPPROVE MERGE REQUEST 'mr-...';A one-time approval token, for this request only.
  7. YouAPPLY MERGE REQUEST 'mr-...' USING TOKEN '<approval_token>';The merge lands on the parent.
  8. YouUNDO MERGE REQUEST 'mr-...';If you change your mind: it takes back only what that merge wrote.

The request comes back with the preview, table by table, including sample rows it would change or delete. The approval token is single use, tied to that one request, and it expires after [agent] approval_token_ttl. If the agent touches the branch after the approval, the apply is refused and it needs a new request. A merge that would delete more than a configured share of a table's rows is refused unless you add ALLOW DESTRUCTIVE. And an undo takes back only what that merge wrote: writes made to the parent after the merge are left alone.

Worth knowing​

  • Primary keys. Rows are matched by primary key, so a table the branch changed needs one. A keyless table is refused.
  • Schema comes along. A merge carries new tables and columns, dropped ones, triggers, policies, grants and sequence settings.
  • Safe retries. merge_id = 'deploy-42' names a merge, so running it again after a dropped connection changes nothing. drop_branch = on drops the branch once the merge commits.
  • Clusters. Merge, refresh and reset run from any node, and the merge commits once on every node.
  • Functions too. Each statement has a function twin for scripts and orchestrators: scram.merge_branch, scram.refresh_branch, scram.reset_branch.

Every form, with every option, is in the SQL reference. The agent side is in Branching for agents.