Small. Fast. Reliable.
Choose any three.
The SQLite OS Interface or "VFS"
Table Of Contents

1. Introduction

This article describes the SQLite OS portability layer or "VFS" - the module at the bottom of the SQLite implementation stack that provides portability across operating systems.

2. The VFS In Relation To The Rest Of SQLite

The internal organization of the SQLite library can be viewed as the stack of modules shown to the right. The Tokenizer, Parser, and Code Generator components are used to process SQL statements and convert them into executable programs in a virtual machine language or byte code. Roughly speaking, these top three layers implement sqlite3_prepare_v2(). The byte code generated by the top three layers is a prepared statement. The Virtual Machine module is responsible for running the SQL statement byte code. The B-Tree module organizes a database file into multiple key/value stores with ordered keys and logarithmic performance. The Pager module is responsible for loading pages of the database file into memory, for implementing and controlling transactions, and for creating and maintaining the journal files that prevent database corruption following a crash or power failure. The OS Interface is a thin abstraction that provides a common set of routines for adapting SQLite to run on different operating systems. Roughly speaking, the bottom four layers implement sqlite3_step().

This article is about the bottom layer.

The OS Interface - also called the "VFS" - is what makes SQLite portable across operating systems. Whenever any of the other modules in SQLite needs to communicate with the operating system, they invoke methods in the VFS. The VFS then invokes the operating-specific code needed to satisfy the request. Hence, porting SQLite to a new operating system is simply a matter of writing a new OS interface layer or "VFS".

3. Multiple VFSes

The standard SQLite source tree contains built-in VFSes for unix and windows. Alternative VFSes can be added at start-time or run-time using the sqlite3_vfs_register() interface.

Multiple VFSes can be registered at the same time. Each VFS has a unique names. Separate database connections within the same process can be using different VFSes at the same time. For that matter, if a single database connection has multiple database files open using the ATTACH command, then each attached database might be using a different VFS.

3.1. Standard Unix VFSes

Unix builds come with multiple built-in VFSes. The default VFS for unix is called "unix" and is used in most applications. Other VFSes that might be found in unix (depending on compile-time options) include:

  1. unix-dotfile - uses dot-file locking rather than POSIX advisory locks.

  2. unix-excl - obtains and holds an exclusive lock on database files, preventing other processes from accessing the database. Also keeps the wal-index in heap rather than in shared memory.

  3. unix-none - all file locking operations are no-ops.

  4. unix-namedsem - uses named semaphores for file locking. VXWorks only.

The various unix VFSes differ only in the way they handle file locking - they share most of their implementation in common with one another and are all located in the same SQLite source file: os_unix.c. Note that except for "unix" and "unix-excl", the various unix VFSes all use incompatible locking implementations. If two processes are accessing the same SQLite database using different unix VFSes, they may not see each others locks and may end up interfering with one another, resulting in database corruption. The "unix-none" VFS in particular does no locking at all and will easily result in database corruption if used by two or more database connections at the same time. Programmers are encouraged to use only "unix" or "unix-excl" unless there is a compelling reason to do otherwise.

3.2. Standard Windows VFSes

Windows builds also come with multiple built-in VFSes. The default Windows VFS is called "win32" and is used in most applications. Other VFSes that might be found on windows builds include:

  1. win32-longpath - like "win32" except that pathnames can be up to 65534 bytes in length, whereas pathnames max out at 1040 bytes in "win32".

  2. win32-none - all file locking operations are no-ops.

  3. win32-longpath-none - combination of "win32-longpath" and "win32-none" - long pathnames are supported and all lock operations are no-ops.

As with unix, most of the code for the various Windows VFSes is shared.

3.3. Specifying Which VFS To Use

There is always one VFS which is the default VFS. On unix systems, the "unix" VFS comes up as the default and on windows it is "win32". If no other actions are taken, new database connections will make use of the default VFS.

The default VFS can be changed by registering or re-registering the VFS using the sqlite3_vfs_register() interface with a second parameter of 1. Hence, if a (unix) process wants to always use the "unix-nolock" VFS in place of "unix", the following code would work:

sqlite3_vfs_register(sqlite3_vfs_find("unix-nolock"), 1);

An alternate VFS can also be specified as the 4th parameter to the sqlite3_open_v2() function. For example:

int rc = sqlite3_open_v2("demo.db", &db, SQLITE_OPEN_READWRITE, "unix-nolock");

Finally, if URI filenames have been enabled, then the alternative VFS can be specified using the "vfs=" parameter on the URI. This technique works with sqlite3_open(), sqlite3_open16(), sqlite3_open_v2(), and when a new database is ATTACH-ed to an existing database connection. For example:

ATTACH 'file:demo2.db?vfs=unix-none' AS demo2;

The VFS specified by a URI has the highest priority. After that comes a VFS specified as the fourth argument to sqlite3_open_v2(). The default VFS is used if no VFS is specified otherwise.

3.4. VFS Shims

From the point of view of the uppers layers of the SQLite stack, each open database file uses exactly one VFS. But in practice, a particular VFS might just be a thin wrapper around another VFS that does the real work. We call a wrapper VFS a "shim".

A simple example of a shim is the "vfstrace" VFS. This is a VFS (implemented in the test_vfstrace.c source file) that writes a message associated with each VFS method call into a log file, then passes control off to another VFS to do the actual work.

3.5. Other Example VFSes

The following are other VFS implementations available in the public SQLite source tree:

There are other VFS implementations both in the core SQLite source code library and in available extensions. The list above is not meant to be exhaustive but merely representative of the kinds of features that can be realized using the VFS interface.

4. VFS Implementations

A new VFS is implemented by subclassing three objects:

An sqlite3_vfs object defines the name of the VFS and the core methods that implement the interface to the operating system, such as checking for existence of files, deleting files, creating files and opening and for reading and/or writing, converting filenames into their canonical form. The sqlite3_vfs object also contains methods for obtaining randomness from the operating system, for suspending a process (sleeping) and for finding the current date and time.

The sqlite3_file object represents an open file. The xOpen method of sqlite3_vfs constructs an sqlite3_file object when the file is opened. The sqlite3_file keeps track of the state of the file while it is opened.

The sqlite3_io_methods object holds the methods used to interact with an open file. Each sqlite3_file contains a pointer to an sqlite3_io_methods object that is appropriate for the file it represents. The sqlite3_io_methods object contains methods to do things such as read and write from the file, to truncate the file, to flush any changes to persistent storage, to find the size of the file, to lock and unlock the file, and to close file and destroy the sqlite3_file object.

Writing the code for a new VFS involves constructing a subclass for the sqlite3_vfs object and then registering that VFS object using a call to sqlite3_vfs_register(). The VFS implementation also provides subclasses for sqlite3_file and sqlite3_io_methods but those objects are not registered directly with SQLite. Instead, the sqlite3_file object is returned from the xOpen method of sqlite3_vfs and the sqlite3_file object points to an instance of the sqlite3_io_methods object.

This page last modified on 2024-02-22 15:53:45 UTC