> ## Documentation Index
> Fetch the complete documentation index at: https://villagesql.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# vsql_branch extension for MySQL

> The vsql_branch extension copies a MySQL database into a branch on the same server, with or without its rows, so you can test a migration on the copy, reset it, and drop it.

`vsql_branch` makes a named copy of a database on the same server, so you can
run a migration or a test suite against the copy, reset it to match the source
again, and drop it when you are done. A branch is an ordinary database, so any
MySQL client or driver connects to it the same way it connects to the source.

Branches in the `vsql_branch` extension work differently from how branches in
the VillageSQL managed database service will work when it launches. A
`vsql_branch` branch is one database on your own server, and you reach it
through the same connection. A VillageSQL managed database service branch will
be a separate server that copies every database of its source, and it has its
own connection endpoint.

| | |
| - | - |
| **Maintainer** | VillageSQL |
| **Source and full documentation** | [villagesql/vsql-branch](https://github.com/villagesql/vsql-branch) |
| **License** | GPL-2.0 |
| **Written in** | Rust |
| **Capability** | Preview |

> [!WARNING]
>
> This extension declares preview capabilities, so
> `vsql_allow_preview_extensions` must be `ON`. Start the server with
> `--vsql_allow_preview_extensions=ON`, or run
> `SET PERSIST vsql_allow_preview_extensions = ON`.

## Install

`vsql_branch` is not bundled with the server, so build it from the repository
first. The build needs the Rust toolchain and `cargo-vsql`
(`cargo install cargo-vsql`). Point `VillageSQL_BUILD_DIR` at your server build
tree and run, from the repository:

```bash theme={null}
export VillageSQL_BUILD_DIR=$HOME/build/villagesql
cargo vsql install
```

`cargo vsql install` builds the extension and copies `vsql_branch.veb` into the
server's `lib/veb` directory. Then install it, and turn on the background worker
that copies, resets, and drops branches:

```sql theme={null}
INSTALL EXTENSION vsql_branch;
SET PERSIST vsql_branch.worker_enabled = ON;
```

Until the worker runs, every function except `mcp_tools` fails:

```
ERROR 3200 (HY000): VDF error in function 'list_branches': vsql_branch: the vsql_branch worker is not running. Run SET PERSIST vsql_branch.worker_enabled = ON
```

`UNINSTALL EXTENSION vsql_branch` deletes the persisted
`vsql_branch.worker_enabled` value, so run the `SET PERSIST` again after you
install the extension again.

## What it adds

Every function except `mcp_tools` takes one JSON string and returns a JSON
string. SQL `NULL` counts as `{}`.

| Function | Arguments | Returns |
| - | - | - |
| `create_branch(json)` | `name`, `src_branch_id`, and optionally `mode` | The new branch and an `operation_id` |
| `list_branches(json)` | none, so pass `NULL` | `{"items": [...]}`, one entry per branch |
| `describe_branch(json)` | `branch_id` | One branch |
| `reset_branch(json)` | `branch_id` | An operation |
| `delete_branch(json)` | `branch_id` | An operation |
| `get_connection_string(json)` | `branch_id`, and optionally `user` | `{"connection_string": "mysql://..."}` |
| `get_operation(json)` | `operation_id` | An operation |
| `mcp_tools()` | | The branch functions as Model Context Protocol tools |

`name` is the branch name and the name of the new database: 1 to 64
characters from A-Z, a-z, 0-9, and `_`. `src_branch_id` is the source database,
which can be any database except the system databases. `mode` is
`schema-only`, the default, which copies every table, view, and trigger without
their rows, or `data`, which copies the rows too. `branch_id` is the branch name.

A SQL function cannot run DDL, so `create_branch`, `reset_branch`, and
`delete_branch` queue the work and return at once. Call `get_operation` until
its `state` is `succeeded` or `failed`.

## Example

Start from a source database:

```sql theme={null}
CREATE DATABASE shop;
CREATE TABLE shop.customers (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL);
CREATE TABLE shop.orders (
  id INT PRIMARY KEY,
  customer_id INT NOT NULL,
  total DECIMAL(10,2) NOT NULL,
  FOREIGN KEY (customer_id) REFERENCES shop.customers (id)
);
INSERT INTO shop.customers VALUES (1, 'Ada'), (2, 'Grace');
INSERT INTO shop.orders VALUES (10, 1, 25.00), (11, 2, 40.00), (12, 1, 12.50);
```

Copy it, rows included, into a branch named `shop_test`. The copy takes a
moment, so wait before you check the operation, and check it again while its
`state` is `pending` or `running`:

```sql theme={null}
SET @r = vsql_branch.create_branch('{"name":"shop_test","src_branch_id":"shop","mode":"data"}');

DO SLEEP(1);

SELECT vsql_branch.get_operation(
  JSON_OBJECT('operation_id', JSON_VALUE(@r, '$.operation_id'))) AS op\G
```

```
*************************** 1. row ***************************
op: {"branch_id":"shop_test","created_at":"2026-12-04T19:12:43Z","failure_message":null,"id":"op_1a242791fba0004","state":"succeeded","type":"branch_create","updated_at":"2026-12-04T19:12:44Z"}
```

Change the branch. The source does not change:

```sql theme={null}
DELETE FROM shop_test.orders WHERE customer_id = 1;

SELECT
  (SELECT COUNT(*) FROM shop.orders)      AS source_orders,
  (SELECT COUNT(*) FROM shop_test.orders) AS branch_orders;
```

```
+---------------+---------------+
| source_orders | branch_orders |
+---------------+---------------+
|             3 |             1 |
+---------------+---------------+
```

Reset the branch to match the source again. `reset_branch` returns the
operation itself, so read its `id`:

```sql theme={null}
SET @r = vsql_branch.reset_branch('{"branch_id":"shop_test"}');

DO SLEEP(1);

SELECT JSON_VALUE(vsql_branch.get_operation(
  JSON_OBJECT('operation_id', JSON_VALUE(@r, '$.id'))), '$.state') AS state;

SELECT COUNT(*) FROM shop_test.orders;
```

```
+-----------+
| state     |
+-----------+
| succeeded |
+-----------+

+----------+
| COUNT(*) |
+----------+
|        3 |
+----------+
```

A branch takes one operation at a time, so wait for one to succeed before you
start the next on the same branch. List the branches with `JSON_TABLE`:

```sql theme={null}
SELECT name, mode, status
FROM JSON_TABLE(vsql_branch.list_branches(NULL), '$.items[*]'
  COLUMNS (name   VARCHAR(64) PATH '$.name',
           mode   VARCHAR(16) PATH '$.mode',
           status VARCHAR(16) PATH '$.status')) AS b;
```

```
+-----------+------+--------+
| name      | mode | status |
+-----------+------+--------+
| shop_test | data | active |
+-----------+------+--------+
```

Drop the branch, check that the operation finished, and drop the source
database:

```sql theme={null}
SET @r = vsql_branch.delete_branch('{"branch_id":"shop_test"}');

DO SLEEP(1);

SELECT JSON_VALUE(vsql_branch.get_operation(
  JSON_OBJECT('operation_id', JSON_VALUE(@r, '$.id'))), '$.state') AS state;

DROP DATABASE shop;
```

```
+-----------+
| state     |
+-----------+
| succeeded |
+-----------+
```

## Limits

* Every account can call the functions, including an account that has only
  `USAGE`. The worker runs its SQL without checking the caller's privileges, so
  such an account can copy any database that is not a system database and reset
  or drop any branch. Install the extension only on a server whose accounts you
  trust with that.
* `delete_branch` drops the database of any row in the table
  `vsql_branches.branches`. An account that can write that table can add
  a row for any database that is not a system database, and after the next
  restart the worker drops that database on request. Grant write access to
  `vsql_branches.branches` only to accounts you trust with that.
* Stored procedures, stored functions, and events are not copied. Tables,
  views, and triggers are, and the worker changes every name of the source
  database in a view or trigger to the branch, including text inside a string
  literal, such as `'see shop.items'`.
* In `data` mode, writes to the source tables wait while the worker copies
  their files, and the worker fails the operation if it waits more than 30
  seconds for a lock. The tables of a data branch can come from different
  moments, so branch an idle source when the tables must agree.
* Operations live in memory, so a restart forgets them.

## See also

* [Managing extensions](https://villagesql.com/docs/en/extensions/managing.md) — how extension settings are read and written
* [Preview capabilities](https://villagesql.com/docs/en/extensions/build/preview-capabilities) — what the preview flag turns on and what it promises
* [Available extensions](https://villagesql.com/docs/en/extensions/catalog) — the full catalog
* [villagesql/vsql-branch](https://github.com/villagesql/vsql-branch) — source, build instructions, and the known limitations


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.