Help us shape the future of Go! Take the 2026 Go Developer Survey

mvcc

package
v0.36.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 12 Imported by: 0

README

Go mvcc SQLite VFS

This package implements the "mvcc" in-memory SQLite VFS.

It has some benefits over the "memdb" VFS:

  • panics do not corrupt a shared database,
  • single-writer not blocked by readers,
  • readers never block,
  • instant snapshots.

mvcc.TestDB is the preferred way to setup an in-memory database for testing when you intend to leverage snapshots, e.g. to setup many independent copies of a database, such as one for each subtest.

Documentation

Overview

Package mvcc implements the "mvcc" SQLite VFS.

The "mvcc" vfs.VFS allows the same in-memory database to be shared among multiple database connections in the same process, as long as the database name begins with "/".

Importing package mvcc registers the VFS:

import _ "github.com/ncruces/go-sqlite3/vfs/mvcc"
Example
package main

import (
	"database/sql"
	_ "embed"
	"fmt"
	"log"

	_ "github.com/ncruces/go-sqlite3/driver"
	"github.com/ncruces/go-sqlite3/vfs/mvcc"
)

//go:embed testdata/test.db
var testDB string

func main() {
	mvcc.Create("test.db", mvcc.NewSnapshot(testDB))

	db, err := sql.Open("sqlite3", "file:/test.db?vfs=mvcc")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	_, err = db.Exec(`INSERT INTO users (id, name) VALUES (3, 'rust')`)
	if err != nil {
		log.Fatal(err)
	}

	rows, err := db.Query(`SELECT id, name FROM users`)
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	for rows.Next() {
		var id, name string
		err = rows.Scan(&id, &name)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s %s\n", id, name)
	}
	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}
}
Output:
0 go
1 zig
2 whatever
3 rust

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(name string, snapshot Snapshot)

Create creates a shared memory database, using a snapshot as its initial contents.

func Delete

func Delete(name string)

Delete deletes a shared memory database.

func TestDB added in v0.29.1

func TestDB(tb testing.TB, snapshot Snapshot, params ...url.Values) string

TestDB creates a shared database from a snapshot for the test to use. The database is automatically deleted when the test and all its subtests complete. Returns a URI filename appropriate to call Open with.

func Test_something(t *testing.T) {
	t.Parallel()
	dsn := mvcc.TestDB(t, snapshot, url.Values{
		"_pragma": {"busy_timeout(1000)"},
	})

	db, err := sql.Open("sqlite3", dsn)
	if err != nil {
		t.Fatal(err)
	}
	defer db.Close()

	// ...
}

Types

type Snapshot

type Snapshot struct {
	*wbt.Tree[int64, string]
}

Snapshot represents a database snapshot.

func NewSnapshot added in v0.29.1

func NewSnapshot(data string) Snapshot

NewSnapshot creates a snapshot from data.

func TakeSnapshot added in v0.29.1

func TakeSnapshot(name string) Snapshot

TakeSnapshot takes a snapshot of a database. Name may be a URI filename.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL