Explainer
The SQLite file format
A SQLite database is one ordinary file, cut into pages of equal size. Every table and every index is a b-tree made of those pages. This page walks through the layout from the first byte, with diagrams; the file viewer shows the same structures in any database you open.
Written from the official Database File Format document, which is the authority; it is in the public domain, like SQLite itself.
Pages
The file is an array of pages, numbered from 1. The page size is a power of two between 512 and 65536 bytes (4096 by default) and is the same for the whole file. Page N starts at byte (N − 1) × page size. A page is the unit SQLite reads, writes, caches and journals.
Each page has one job: Table interior · Table leaf · Index interior · Index leaf · Overflow · Freelist trunk · Freelist leaf · Pointer map · Lock-byte. Page 1 is special only in that its first 100 bytes are the database header; the rest of it is the root of the sqlite_schema table.
The 100-byte header
All multi-byte numbers in the file are big-endian.
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0 | 16 | Header string | The bytes "SQLite format 3" and a zero byte. Every database file starts with them. |
| 16 | 2 | Page size | Bytes per page: a power of two from 512 to 32768, or 1 meaning 65536. |
| 18 | 1 | Write version | 1 for the rollback journal, 2 for WAL mode. |
| 19 | 1 | Read version | 1 for the rollback journal, 2 for WAL mode. |
| 20 | 1 | Reserved bytes per page | Space at the end of each page kept for extensions such as encryption or checksums. Usually 0. |
| 21 | 1 | Max embedded payload fraction | Must be 64. |
| 22 | 1 | Min embedded payload fraction | Must be 32. |
| 23 | 1 | Leaf payload fraction | Must be 32. |
| 24 | 4 | File change counter | Incremented whenever the file is changed outside WAL mode. |
| 28 | 4 | Database size in pages | Trusted only if it is non-zero and the change counter equals the version-valid-for number; otherwise the size comes from the file length. |
| 32 | 4 | First freelist trunk page | Page number of the first freelist trunk page, or 0 if the freelist is empty. |
| 36 | 4 | Freelist pages | Total number of freelist pages, trunks and leaves. |
| 40 | 4 | Schema cookie | Incremented whenever the schema changes, so prepared statements know to recompile. |
| 44 | 4 | Schema format number | 1 to 4. Format 4, the default since 3.3.0, allows DESC indexes and boolean values stored as serial types 8 and 9. |
| 48 | 4 | Default page cache size | Suggested cache size, set by the deprecated default_cache_size pragma. |
| 52 | 4 | Largest root b-tree page | Non-zero only in auto-vacuum and incremental-vacuum databases, which have pointer-map pages. |
| 56 | 4 | Text encoding | 1 is UTF-8, 2 is UTF-16le, 3 is UTF-16be. |
| 60 | 4 | User version | Free for the application: read and set with PRAGMA user_version. |
| 64 | 4 | Incremental vacuum mode | Non-zero for incremental vacuum, zero otherwise. |
| 68 | 4 | Application ID | Identifies the application that owns the file: PRAGMA application_id. |
| 72 | 20 | Reserved for expansion | Must be zero. |
| 92 | 4 | Version-valid-for number | The change counter value when the SQLite version number below was stored. |
| 96 | 4 | SQLite version number | The SQLITE_VERSION_NUMBER of the library that most recently wrote the file, e.g. 3045001 for 3.45.1. |
B-tree pages
SQLite uses two kinds of b-tree. A table b-tree holds a rowid table: each entry is keyed by a 64-bit rowid, and only the leaves hold row data (strictly, a B+tree). An index b-tree holds an index or a WITHOUT ROWID table: each entry is a record whose leading columns are the key, and interior pages hold entries too. Each kind has interior and leaf pages, which gives the four page types, told apart by the page's first byte: 2 index interior, 5 table interior, 10 index leaf, 13 table leaf.
An interior page holds K keys and K + 1 child pointers: one in each cell (the left child), and the right-most pointer in the page header. Every key in a cell's left child is less than or equal to the cell's key.
Inside a b-tree page
The page header is 8 bytes on leaves and 12 on interior pages: the type byte; the offset of the first freeblock; the number of cells; where the cell content area starts; the count of fragmented free bytes; and, on interior pages, the right-most child. After it comes the cell pointer array, two bytes per cell, sorted by key. Cells themselves are packed from the end of the page backwards, so the array and the content area grow towards each other and the gap between them is the unallocated space. Deleting a cell leaves a freeblock (a linked list inside the page) or, for gaps under four bytes, fragmented bytes.
| Page type | Cell |
|---|---|
| Table leaf (13) | varint payload size, varint rowid, payload, 4-byte first overflow page if it spills |
| Table interior (5) | 4-byte left child page, varint rowid |
| Index leaf (10) | varint payload size, payload, 4-byte first overflow page if it spills |
| Index interior (2) | 4-byte left child page, varint payload size, payload, 4-byte first overflow page if it spills |
Varints
Sizes, rowids and serial types are stored as variable-length integers of one to nine bytes. The first eight bytes each give seven bits, high bit set meaning "another byte follows"; a ninth byte gives all eight of its bits. Small numbers, which are the common case, take one byte.
| Value | Bytes | Length |
|---|---|---|
| 0 | 00 | 1 |
| 127 | 7f | 1 |
| 128 | 81 00 | 2 |
| 16,383 | ff 7f | 2 |
| 16,384 | 81 80 00 | 3 |
| 2,097,151 | ff ff 7f | 3 |
| 268,435,456 | 81 80 80 80 00 | 5 |
| 34,359,738,368 | 81 80 80 80 80 00 | 6 |
| -1 | ff ff ff ff ff ff ff ff ff | 9 |
Records
Row data and index keys are stored as records: a header, then a body. The header is a varint giving the header's own size, then one varint serial type per column. The body is the values, one after another, with nothing between them. A value's serial type says both its type and its size, so values need no separators or length prefixes of their own.
| Serial type | Meaning | Bytes in body |
|---|---|---|
| 0 | NULL | 0 |
| 1 | 8-bit integer | 1 |
| 2 | 16-bit integer | 2 |
| 3 | 24-bit integer | 3 |
| 4 | 32-bit integer | 4 |
| 5 | 48-bit integer | 6 |
| 6 | 64-bit integer | 8 |
| 7 | 64-bit float | 8 |
| 8 | integer 0 | 0 |
| 9 | integer 1 | 0 |
| 10 | reserved | 0 |
| 11 | reserved | 0 |
| N ≥ 12, even | BLOB | (N − 12) / 2 |
| N ≥ 13, odd | TEXT, in the database encoding | (N − 13) / 2 |
In a rowid table, a column declared INTEGER PRIMARY KEY is the rowid itself, so the record stores NULL in its place. A table altered with ADD COLUMN can have records with fewer columns than the table; the missing ones take their default values. Generated columns declared VIRTUAL are not stored at all.
A record does not name its columns. The viewer takes the names from the CREATE TABLE or CREATE INDEX statement in sqlite_schema: a rowid table's record holds its columns in declared order; a WITHOUT ROWID table's holds the primary key columns first; an index entry holds the indexed columns followed by the rowid (or the rest of the primary key).
Overflow pages
When a payload is too big for a page, the first part stays in the cell and the rest goes to a chain of overflow pages. Each overflow page starts with the 4-byte number of the next page in the chain (0 on the last) and holds usable size − 4 bytes of payload. How much stays local depends on the usable size U (page size minus reserved bytes) and the payload size P:
- Table leaf: if P ≤ X = U − 35, everything is local.
- Index pages: X = ((U − 12) × 64 / 255) − 23.
- Otherwise, with M = ((U − 12) × 32 / 255) − 23 and K = M + ((P − M) mod (U − 4)), the local part is K bytes if K ≤ X, else M.
The point of K is to make the last overflow page full, so no space is wasted at the end of the chain.
The freelist
Pages that are no longer used (after a DELETE or DROP) are not given back to the operating system; they go on the freelist, to be reused. The header gives the first trunk page and the total count. Each trunk page holds the number of the next trunk, a count, and an array of leaf page numbers. Leaf pages hold nothing useful. VACUUM rebuilds the file without them.
Pointer-map pages
In an auto-vacuum or incremental-vacuum database (header offset 52 non-zero), page 2 and every (U / 5 + 1)th page after it is a pointer map: five bytes for each following page, giving a type (1 b-tree root, 2 freelist, 3 first overflow page, 4 later overflow page, 5 non-root b-tree page) and the parent page. With this map SQLite can move a page to the end of the file and fix up the one pointer that refers to it, which is how auto-vacuum shrinks the file at commit.
The lock-byte page
The page that contains bytes 1,073,741,824 to 1,073,742,335 (the first byte past one gigabyte) is never used: those bytes are where SQLite takes its file locks on Windows. Only databases bigger than 1 GiB have one.
The write-ahead log
In WAL mode (header bytes 18 and 19 are 2), a transaction does not change the database file. It appends the new versions of the pages it changed to the -wal file: a 32-byte header, then frames of a 24-byte frame header and one page. The frame header gives the page number and, on the last frame of a transaction, the database size after the commit, plus a running checksum. A reader takes each page from the newest committed frame in the WAL, or from the database file if the WAL has none. A checkpoint copies the WAL's pages back into the database file. So a copy of a WAL-mode .db file without its -wal file is the database as of the last checkpoint; the viewer reads both if you drop both.
The rollback journal
In the other journal modes (DELETE, the default, TRUNCATE and PERSIST), a transaction writes the database file in place, so it first saves the original content of each page it will change in the -journal file. The journal starts with a header padded to a disk sector: the magic number d9 d5 05 f9 20 a1 63 d7, the number of records, a random nonce for the checksums, the database size in pages before the transaction, the sector size and the page size. Each record is a 4-byte page number, the page, and a 4-byte checksum (the nonce plus every 200th byte of the page). At commit the journal is deleted, truncated, or has its header zeroed. A journal still there with a valid header is hot: the next connection copies its pages back and truncates the file to its old size, which undoes the unfinished transaction. Drop a database together with its -journal in the viewer to see which pages it would restore.
See it in a real file
Open the sample database in the file viewer: click any page in the map to see its header, cell pointers and records decoded byte by byte.