NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›

Branching

A branch is a database made as a consistent copy of another database, its parent, that then goes its own way. Use one to try a migration against real data, to give a developer or a test run a private copy of production, or to recover a database as it was at a past point. A branch can be refreshed from its parent, swapped in for it, compared with it, and merged back into it.

The copy is taken while the parent keeps taking writes. On filesystems that share blocks (btrfs, XFS, ZFS 2.2 and later, bcachefs) each file is cloned, which is nearly free whatever its size; elsewhere (ext4, NTFS) the files are copied in full.

Who can run these statements

Every branch statement except SHOW BRANCHES needs a superuser (42501 otherwise); SHOW BRANCH DIFF too, because it reads rows of databases the session may have no account in. The statements that change something are refused inside a transaction block (25001) and on a read-only session (25006), like CREATE DATABASE.

Run them like any other statement, from psql, a PostgreSQL driver or the Branches tab in Studio.

Statements at a glance

StatementWhat it does
CREATE BRANCH dev [FROM prod]Makes dev as a copy of prod as of now. FROM defaults to the session's database.
CREATE BRANCH dev FROM prod AS OF TIMESTAMP '…'A copy of prod at a past point, from the S3 backup. Also AS OF LSN n and AS OF SNAPSHOT 'id'.
CREATE BRANCH dev FROM prod WITHOUT BASEKeeps no merge base: half the disk space, but the branch cannot be merged.
CREATE DATABASE dev [WITH] TEMPLATE [=] prodPostgreSQL's spelling of the same copy. TEMPLATE template0 / template1 is a plain empty CREATE DATABASE.
SHOW BRANCHESLists branches, in tree order.
SHOW BRANCH DIFF dev [INTO prod]What a merge would do, per table, without doing it.
MERGE BRANCH dev [INTO prod] [ON CONFLICT FAIL | BRANCH WINS | PARENT WINS] [ALLOW DROP]Applies what dev changed since it was made (or last merged) to its parent, in one transaction.
RESET BRANCH devCopies the parent again, as of now. Everything written on dev is discarded.
PROMOTE BRANCH devSwaps dev and its parent by name. The parent's old contents stay, as branch dev.
DROP BRANCH [IF EXISTS] devDrops the branch and its merge base. Refused for a database that is not a branch.

Names follow the usual identifier rules: unquoted names are folded to lower case, and a name in double quotes is kept as written.

Make a branch

sql
CREATE BRANCH staging FROM prod;
text
NOTICE:  branch "staging" of "prod" made by reflink at LSN 48213, with a merge base
CREATE BRANCH

A branch is an ordinary database. Connect to it by name, for example psql -d staging; its roles, sequences, indexes and partitions came with it. Its contents are the parent as of the last commit the copy captured, shown as parent_lsn in SHOW BRANCHES.

Before a full copy (on a filesystem without block sharing), the server checks there is room for the copy, its merge base and 256 MiB to spare, and refuses with 53100 if not.

PostgreSQL tools that clone with a template work too:

sql
CREATE DATABASE staging TEMPLATE prod;

A branch at a past point

With S3 backups configured on the server, a branch can be made from the backup instead of the live database:

sql
CREATE BRANCH before_migration FROM prod AS OF TIMESTAMP '2026-11-19 10:00:00+00';
CREATE BRANCH at_commit        FROM prod AS OF LSN 918273;
CREATE BRANCH from_snapshot    FROM prod AS OF SNAPSHOT 'snap-20261119T020000Z-b57e1f06';

This is the way to look at, or pull rows back from, a database as it was without restoring the whole server. Know the costs:

  • The whole snapshot is downloaded (it holds every database), plus the log bundles up to the point.
  • TIMESTAMP resolves to the last log upload at or before that instant, so it is as precise as the backup's log flush interval. LSN and SNAPSHOT are exact.
  • Tables that were on a key/value store come back as of the snapshot's copy.

Without backups configured, AS OF is refused.

See what you have

sql
SHOW BRANCHES;
ColumnMeaning
branchThe branch's name.
parentThe database it was made from.
parent_existsWhether that database still exists.
pointThe point it was copied at: now, lsn <n>, time <timestamp> or snapshot <id>.
parent_lsnThe parent's last commit the copy holds.
methodHow the files were copied: reflink (cloned), copy (copied in full) or mixed. A promoted parent shows promote.
created_at, merged_atWhen it was made and last merged.
size_bytes, base_bytesSize of the branch and of its merge base.
sessionsOpen sessions on it.
depth, path, children, viaWhere it sits in the tree: path reads like prod/dev/feature; via records branches it was re-parented past.

Compare and merge

First look at what a merge would do:

sql
SHOW BRANCH DIFF staging;

It returns one row per table with a change: table_name, change (schema or data), inserts, updates, deletes, conflicts and detail. Statements that shape no table (views, functions, procedures, triggers, sequences, types, domains, comments) appear as one (schema objects) row.

Then merge:

sql
MERGE BRANCH staging;                              -- into its parent, fail on any conflict
MERGE BRANCH staging INTO prod ON CONFLICT BRANCH WINS;
MERGE BRANCH staging ON CONFLICT PARENT WINS ALLOW DROP;

A merge returns the same rows as the diff, for what it applied. It runs as one transaction on the parent, as ordinary SQL, so constraints, foreign keys, indexes and triggers apply. After it commits, the branch's merge point moves forward, so a second merge offers only later changes. Writes to the branch made while a merge is running go to the next merge.

How changes are decided

A merge is three-way: it compares the branch, the parent, and the branch as it was made (its merge base).

Schema. For each table, the columns and indexes are compared:

  • changed only on the branch: the branch's DDL is replayed on the parent;
  • changed the same way on both: nothing to do;
  • changed differently on both: a conflict, which always refuses the merge;
  • a dropped table or column needs ALLOW DROP (2BP01 otherwise). A column renamed on the branch counts as a drop and an add.

The DDL replayed is what the branch actually ran: every successful CREATE, ALTER, DROP and COMMENT on a branch is recorded (inside a transaction block, only once the block commits). Views, functions, procedures, triggers, sequences, types and domains travel as the DDL that made them.

Rows, compared as text as a client reads them:

  • In a table with a primary key or a unique key, row by row on the key (a key with repeated NULLs falls back to the next rule): unchanged on the branch is skipped, changed only on the branch is applied, changed the same on both is skipped, and anything else is a conflict.
  • In a table without such a key, as a multiset of whole rows. A keyless table whose columns the branch changed is a conflict.
  • In a table keyed on one SERIAL column (or DEFAULT nextval(…)), both sides hand out the same numbers to different rows. A row only the branch has is inserted without its key, takes the parent's next number, and every merged foreign key that points at it is rewritten to the new number. Later merges remember the mapping.

Conflicts

ClauseBehaviour
ON CONFLICT FAIL (default)Any conflict rolls the whole merge back with 40001 and names up to 20 of the conflicts.
ON CONFLICT BRANCH WINSThe branch's version of a conflicting row is written.
ON CONFLICT PARENT WINSThe parent's version is kept.

Schema conflicts refuse the merge whatever the clause. Each side is read in one REPEATABLE READ snapshot, so a write over a parent row that another session changed during the merge is refused, not lost.

What a merge does not carry

  • Privileges (GRANT / REVOKE), roles, and a sequence's current position.
  • The parent's role catalogue and internal tables are never merged as rows.
  • Generated columns are not written; the parent computes them.

The parent's side of a merge runs as the role that issued MERGE BRANCH, so relations its replayed DDL creates are owned by that role.

Refresh, swap, drop

Refresh a branch to the parent's current state, discarding everything written on it:

sql
RESET BRANCH staging;

The new copy is built first, then swapped in; if it will not open, the old one is put back.

Swap a branch in for its parent, for example after rehearsing a migration on it:

sql
PROMOTE BRANCH staging;
text
NOTICE:  "staging" is now "prod"; the previous "prod" is kept as branch "staging"
PROMOTE BRANCH

The old parent becomes a branch without a merge base. PROMOTE is refused for the default database (the one in the data directory itself) and while either side has open sessions.

Drop a branch and its merge base:

sql
DROP BRANCH IF EXISTS staging;

DROP DATABASE staging does the same.

Branches of branches

A branch can itself be a parent: CREATE BRANCH feature FROM dev. Merges go one level at a time: MERGE BRANCH feature merges into dev, and MERGE BRANCH dev then carries it on to prod.

  • Dropping a middle branch moves each branch made from it to the dropped branch's parent, keeping its merge base. A later merge into the new parent carries only the child's own changes. SHOW BRANCHES records the route in via. Rows an earlier merge into the dropped branch renumbered are no longer matched by their new keys.
  • Promoting a middle branch moves its own branches with it.
  • Merges go into a branch's own parent; there is no merge between siblings.

Disk usage

Each branch with a merge base keeps a frozen copy of itself as it was made. On ext4 or NTFS that is a second full copy; on a block-sharing filesystem it is close to free. WITHOUT BASE skips it at the cost of not being able to merge. SHOW BRANCHES gives size_bytes and base_bytes.

Merge bases live under a hidden .rdb_branch_bases directory in the data directory. A branch being built sits in a hidden .branching-<name> directory, which is removed at start-up if a build was interrupted.

Backups and replication

  • S3 snapshots include branches but not their merge bases; a restored branch comes back without one, and RESET BRANCH makes a new one. See Backup and restore.
  • A branch is not replicated: it is the node's own. Creating one on a replicating node says so with a warning. See Replication.

Limits

  • A server running without the write-ahead log cannot make branches.
  • Partitions of a partitioned table placed on a key/value store cannot be branched; the error names the stores. Other tables on a key/value store are copied out of it and are ordinary tables on the branch. In-memory tables come over empty, with a warning.
  • A table with no key, or one whose key may hold NULLs, is read whole into memory during a diff or merge, as is a SERIAL-keyed table whose rows reference each other. Keyed tables without nullable key columns are compared 20,000 rows at a time.
  • A cycle of references among rows new on the branch, in a SERIAL-keyed table, is refused.
  • DDL that cannot be rolled back inside a transaction is refused by a merge rather than applied outside its atomicity, except CREATE INDEX, which a rolled-back merge may leave behind.
  • DDL run on the branch by a path that did not record it is refused by a merge rather than merged around.
  • The identity column form GENERATED … AS IDENTITY does not exist in this engine; SERIAL is the only sequence key a merge renumbers.
✓ page rendered in 229 µs axum + tera + htmx · no other javascript