2023-05-31 18:45:45 +01:00
|
|
|
# Go SQLite VFS API
|
|
|
|
|
|
2023-11-09 16:35:45 +00:00
|
|
|
This package implements the SQLite [OS Interface](https://sqlite.org/vfs.html) (aka VFS).
|
2023-05-31 18:45:45 +01:00
|
|
|
|
2024-04-11 13:20:30 +01:00
|
|
|
It replaces the default SQLite VFS with a **pure Go** implementation.
|
2023-05-31 18:45:45 +01:00
|
|
|
|
2024-04-11 13:20:30 +01:00
|
|
|
It also exposes [interfaces](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs#VFS)
|
|
|
|
|
that should allow you to implement your own custom VFSes.
|
|
|
|
|
|
|
|
|
|
Since it is a from scratch reimplementation,
|
|
|
|
|
there are naturally some ways it deviates from the original.
|
|
|
|
|
|
|
|
|
|
The main differences are [file locking](#file-locking) and [WAL mode](write-ahead-logging) support.
|
|
|
|
|
|
|
|
|
|
### File Locking
|
|
|
|
|
|
|
|
|
|
POSIX advisory locks, which SQLite uses on Unix, are
|
2024-04-27 10:32:06 +01:00
|
|
|
[broken by design](https://github.com/sqlite/sqlite/blob/b74eb0/src/os_unix.c#L1073-L1161).
|
2024-04-11 13:20:30 +01:00
|
|
|
|
2024-04-24 01:07:17 +01:00
|
|
|
On Linux and macOS, this module uses
|
2024-04-11 13:20:30 +01:00
|
|
|
[OFD locks](https://www.gnu.org/software/libc/manual/html_node/Open-File-Description-Locks.html)
|
|
|
|
|
to synchronize access to database files.
|
|
|
|
|
OFD locks are fully compatible with POSIX advisory locks.
|
|
|
|
|
|
|
|
|
|
On BSD Unixes, this module uses
|
|
|
|
|
[BSD locks](https://man.freebsd.org/cgi/man.cgi?query=flock&sektion=2).
|
2024-05-04 11:44:54 +01:00
|
|
|
On BSD, macOS, and illumos, these locks are fully compatible with POSIX advisory locks;
|
|
|
|
|
on Linux and z/OS, they are fully functional, but incompatible with POSIX advisory locks.
|
|
|
|
|
However, concurrency is always reduced with BSD locks
|
2024-04-11 13:20:30 +01:00
|
|
|
(`BEGIN IMMEDIATE` behaves the same as `BEGIN EXCLUSIVE`).
|
2024-05-04 11:44:54 +01:00
|
|
|
You can opt into BSD locks with the `sqlite3_flock` build tag.
|
2024-04-11 13:20:30 +01:00
|
|
|
|
|
|
|
|
On Windows, this module uses `LockFileEx` and `UnlockFileEx`,
|
|
|
|
|
like SQLite.
|
|
|
|
|
|
|
|
|
|
On all other platforms, file locking is not supported, and you must use
|
|
|
|
|
[`nolock=1`](https://sqlite.org/uri.html#urinolock)
|
|
|
|
|
(or [`immutable=1`](https://sqlite.org/uri.html#uriimmutable))
|
2024-04-24 15:49:45 +01:00
|
|
|
to open database files.\
|
2024-04-11 13:20:30 +01:00
|
|
|
To use the [`database/sql`](https://pkg.go.dev/database/sql) driver
|
|
|
|
|
with `nolock=1` you must disable connection pooling by calling
|
|
|
|
|
[`db.SetMaxOpenConns(1)`](https://pkg.go.dev/database/sql#DB.SetMaxOpenConns).
|
|
|
|
|
|
|
|
|
|
You can use [`vfs.SupportsFileLocking`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs#SupportsFileLocking)
|
2024-05-04 11:44:54 +01:00
|
|
|
to check if your build supports file locking.
|
2024-04-11 13:20:30 +01:00
|
|
|
|
|
|
|
|
### Write-Ahead Logging
|
|
|
|
|
|
2024-04-24 01:07:17 +01:00
|
|
|
On 64-bit Linux and macOS, this module uses `mmap` to implement
|
2024-04-11 13:20:30 +01:00
|
|
|
[shared-memory for the WAL-index](https://sqlite.org/wal.html#implementation_of_shared_memory_for_the_wal_index),
|
|
|
|
|
like SQLite.
|
|
|
|
|
|
2024-04-13 13:43:24 +01:00
|
|
|
To allow `mmap` to work, each connection needs to reserve up to 4GB of address space.\
|
2024-04-11 13:20:30 +01:00
|
|
|
To limit the amount of address space each connection needs,
|
2024-04-16 17:33:48 +01:00
|
|
|
use [`WithMemoryLimitPages`](../tests/testcfg/testcfg.go).
|
2024-04-11 13:20:30 +01:00
|
|
|
|
2024-05-04 11:44:54 +01:00
|
|
|
On Windows, and with BSD locks, [WAL](https://sqlite.org/wal.html) support is
|
2024-04-11 13:20:30 +01:00
|
|
|
[limited](https://sqlite.org/wal.html#noshm).
|
2024-04-24 15:49:45 +01:00
|
|
|
`EXCLUSIVE` locking mode can be set to create, read, and write WAL databases.\
|
|
|
|
|
To use `EXCLUSIVE` locking mode with the
|
|
|
|
|
[`database/sql`](https://pkg.go.dev/database/sql) driver
|
|
|
|
|
you must disable connection pooling by calling
|
2024-04-11 13:20:30 +01:00
|
|
|
[`db.SetMaxOpenConns(1)`](https://pkg.go.dev/database/sql#DB.SetMaxOpenConns).
|
|
|
|
|
|
2024-05-04 11:44:54 +01:00
|
|
|
On all other platforms, where file locking is not supported, WAL mode is disabled.
|
2024-04-24 15:49:45 +01:00
|
|
|
|
2024-04-11 13:20:30 +01:00
|
|
|
You can use [`vfs.SupportsSharedMemory`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs#SupportsSharedMemory)
|
2024-05-04 11:44:54 +01:00
|
|
|
to check if your build supports shared memory.
|
2024-04-11 13:20:30 +01:00
|
|
|
|
2024-04-16 02:52:37 +01:00
|
|
|
### Batch-Atomic Write
|
|
|
|
|
|
|
|
|
|
On 64-bit Linux, this module supports [batch-atomic writes](https://sqlite.org/cgi/src/technote/714)
|
2024-04-24 01:07:17 +01:00
|
|
|
with the F2FS filesystem.
|
2024-04-16 02:52:37 +01:00
|
|
|
|
2024-05-04 11:44:54 +01:00
|
|
|
### Build Tags
|
2024-04-11 13:20:30 +01:00
|
|
|
|
|
|
|
|
The VFS can be customized with a few build tags:
|
2024-05-04 11:44:54 +01:00
|
|
|
- `sqlite3_flock` forces the use of BSD locks; it can be used on z/OS for locking support,
|
|
|
|
|
and elsewhere to test the BSD locks.
|
2024-04-11 13:20:30 +01:00
|
|
|
- `sqlite3_nosys` prevents importing [`x/sys`](https://pkg.go.dev/golang.org/x/sys);
|
|
|
|
|
disables locking _and_ shared memory on all platforms.
|
2024-04-24 15:49:45 +01:00
|
|
|
- `sqlite3_noshm` disables shared memory on all platforms.
|
2024-05-04 11:44:54 +01:00
|
|
|
|
|
|
|
|
> [!CAUTION]
|
|
|
|
|
> If file locking is incompatible with POSIX advisory locks,
|
|
|
|
|
> accessing databases concurrently with this package and other SQLite implementations
|
|
|
|
|
> is unsafe, and will eventually corrupt data.
|
|
|
|
|
> This will only be the case if you explicitly opt into BSD locks with `sqlite3_flock`.
|
|
|
|
|
> The SQLite [`unix-flock`](https://sqlite.org/compile.html#enable_locking_style) VFS
|
|
|
|
|
> is always compatible with `sqlite3_flock`.
|