|
2 | 2 |
|
3 | 3 | This package implements the SQLite [OS Interface](https://sqlite.org/vfs.html) (aka VFS).
|
4 | 4 |
|
5 |
| -It replaces the default SQLite VFS with a pure Go implementation. |
| 5 | +It replaces the default SQLite VFS with a **pure Go** implementation. |
6 | 6 |
|
7 |
| -It also exposes interfaces that should allow you to implement your own custom VFSes. |
| 7 | +It also exposes [interfaces](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs#VFS) |
| 8 | +that should allow you to implement your own custom VFSes. |
| 9 | + |
| 10 | +Since it is a from scratch reimplementation, |
| 11 | +there are naturally some ways it deviates from the original. |
| 12 | + |
| 13 | +The main differences are [file locking](#file-locking) and [WAL mode](write-ahead-logging) support. |
| 14 | + |
| 15 | +### File Locking |
| 16 | + |
| 17 | +POSIX advisory locks, which SQLite uses on Unix, are |
| 18 | +[broken by design](https://sqlite.org/src/artifact/2e8b12?ln=1073-1161). |
| 19 | + |
| 20 | +On Linux, macOS and illumos, this module uses |
| 21 | +[OFD locks](https://www.gnu.org/software/libc/manual/html_node/Open-File-Description-Locks.html) |
| 22 | +to synchronize access to database files. |
| 23 | +OFD locks are fully compatible with POSIX advisory locks. |
| 24 | + |
| 25 | +On BSD Unixes, this module uses |
| 26 | +[BSD locks](https://man.freebsd.org/cgi/man.cgi?query=flock&sektion=2). |
| 27 | +On BSD Unixes, BSD locks are fully compatible with POSIX advisory locks. |
| 28 | +However, concurrency is reduced with BSD locks |
| 29 | +(`BEGIN IMMEDIATE` behaves the same as `BEGIN EXCLUSIVE`). |
| 30 | + |
| 31 | +On Windows, this module uses `LockFileEx` and `UnlockFileEx`, |
| 32 | +like SQLite. |
| 33 | + |
| 34 | +On all other platforms, file locking is not supported, and you must use |
| 35 | +[`nolock=1`](https://sqlite.org/uri.html#urinolock) |
| 36 | +(or [`immutable=1`](https://sqlite.org/uri.html#uriimmutable)) |
| 37 | +to open database files. |
| 38 | + |
| 39 | +To use the [`database/sql`](https://pkg.go.dev/database/sql) driver |
| 40 | +with `nolock=1` you must disable connection pooling by calling |
| 41 | +[`db.SetMaxOpenConns(1)`](https://pkg.go.dev/database/sql#DB.SetMaxOpenConns). |
| 42 | + |
| 43 | +You can use [`vfs.SupportsFileLocking`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs#SupportsFileLocking) |
| 44 | +to check if your platform supports file locking. |
| 45 | + |
| 46 | +### Write-Ahead Logging |
| 47 | + |
| 48 | +On 64-bit Linux, macOS and illumos, this module uses `mmap` to implement |
| 49 | +[shared-memory for the WAL-index](https://sqlite.org/wal.html#implementation_of_shared_memory_for_the_wal_index), |
| 50 | +like SQLite. |
| 51 | + |
| 52 | +To allow `mmap` to work, each connection needs to reserve a lot of address space.\ |
| 53 | +To limit the amount of address space each connection needs, |
| 54 | +use [`WithMemoryLimitPages`](../tests/parallel/parallel_test.go#L21). |
| 55 | + |
| 56 | +On all other platforms, [WAL](https://sqlite.org/wal.html) support is |
| 57 | +[limited](https://sqlite.org/wal.html#noshm). |
| 58 | + |
| 59 | +To work around that limitation, SQLite is [patched](sqlite3/locking_mode.patch) |
| 60 | +to automatically use `EXCLUSIVE` locking mode for WAL databases on such platforms. |
| 61 | + |
| 62 | +Because connection pooling is incompatible with `EXCLUSIVE` locking mode, |
| 63 | +to use the [`database/sql`](https://pkg.go.dev/database/sql) driver |
| 64 | +with WAL mode databases you should disable connection pooling by calling |
| 65 | +[`db.SetMaxOpenConns(1)`](https://pkg.go.dev/database/sql#DB.SetMaxOpenConns). |
| 66 | + |
| 67 | +You can use [`vfs.SupportsSharedMemory`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs#SupportsSharedMemory) |
| 68 | +to check if your platform supports shared memory. |
| 69 | + |
| 70 | +### Build tags |
| 71 | + |
| 72 | +The VFS can be customized with a few build tags: |
| 73 | +- `sqlite3_flock` forces the use of BSD locks; it can be used on macOS to test the BSD locking implementation. |
| 74 | +- `sqlite3_nosys` prevents importing [`x/sys`](https://pkg.go.dev/golang.org/x/sys); |
| 75 | + disables locking _and_ shared memory on all platforms. |
| 76 | +- `sqlite3_noshm` disables shared memory on all platforms. |
0 commit comments