Skip to content

Commit group

Heyang Zhou edited this page Aug 20, 2022 · 17 revisions

Warning

This feature has been removed in the latest version. Might be added back in a future version if a nicer API is designed.

Introduction

Commit group is a mechanism to atomically commit or rollback multiple transactions spanning different databases. Databases in different connections and ATTACH-ed databases in the same connection both "just work".

Usage

Note that all the SQL queries between mv_commitgroup_begin and mv_commitgroup_{commit,rollback} (inclusively) must run on the same thread, because internally mvsqlite uses thread-local state to determine the relationship between transactions and commit groups.

-- Attach the databases.
ATTACH db1 as db1;
ATTACH db2 as db2;

-- Start the commit group.
-- Everything needs to happen on the same thread from now on.
SELECT mv_commitgroup_begin();

-- optional, disable database locks (this is safe)
--
-- this makes transactions faster (one fewer RTT), and enables consistent point-in-time view
-- across all databases
SELECT mv_commitgroup_lock_disable();

-- Run arbitrary transactions on an arbitrary set of databases (subject to SQLite attach limit)
BEGIN;
INSERT INTO db2.t1 SELECT * from db1.t1;

COMMIT; -- this doesn't actually commit, but just appends to the commit group

SELECT mv_commitgroup_commit(); -- or
SELECT mv_commitgroup_rollback();
-- the same-thread constraint ends here

mv_commitgroup_commit will return one of the three types of values:

  • NULL: When there is nothing to commit.
  • conflict (as a string): When a conflict occurred and no changes are committed.
  • A 20-character hex string: The commit version, when commit succeeded.
Clone this wiki locally