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.

0204060800–15: Header stringHeader string16–17: Page size1618–18: Write version19–19: Read version20–20: Reserved bytes per page21–21: Max embedded payload fraction22–22: Min embedded payload fraction23–23: Leaf payload fraction24–27: File change counterFile change counte…28–31: Database size in pagesDatabase size in p…32–35: First freelist trunk pageFirst freelist tru…36–39: Freelist pagesFreelist pages40–43: Schema cookieSchema cookie44–47: Schema format numberSchema format numb…48–51: Default page cache sizeDefault page cache…52–55: Largest root b-tree pageLargest root b-tre…56–59: Text encodingText encoding60–63: User versionUser version64–67: Incremental vacuum modeIncremental vacuum…68–71: Application IDApplication ID72–91: Reserved for expansionReserved for expansion72–91: Reserved for expansionReserved for expansion92–95: Version-valid-for numberVersion-valid-for …96–99: SQLite version numberSQLite version num…

All multi-byte numbers in the file are big-endian.

OffsetSizeFieldMeaning
016Header stringThe bytes "SQLite format 3" and a zero byte. Every database file starts with them.
162Page sizeBytes per page: a power of two from 512 to 32768, or 1 meaning 65536.
181Write version1 for the rollback journal, 2 for WAL mode.
191Read version1 for the rollback journal, 2 for WAL mode.
201Reserved bytes per pageSpace at the end of each page kept for extensions such as encryption or checksums. Usually 0.
211Max embedded payload fractionMust be 64.
221Min embedded payload fractionMust be 32.
231Leaf payload fractionMust be 32.
244File change counterIncremented whenever the file is changed outside WAL mode.
284Database size in pagesTrusted only if it is non-zero and the change counter equals the version-valid-for number; otherwise the size comes from the file length.
324First freelist trunk pagePage number of the first freelist trunk page, or 0 if the freelist is empty.
364Freelist pagesTotal number of freelist pages, trunks and leaves.
404Schema cookieIncremented whenever the schema changes, so prepared statements know to recompile.
444Schema format number1 to 4. Format 4, the default since 3.3.0, allows DESC indexes and boolean values stored as serial types 8 and 9.
484Default page cache sizeSuggested cache size, set by the deprecated default_cache_size pragma.
524Largest root b-tree pageNon-zero only in auto-vacuum and incremental-vacuum databases, which have pointer-map pages.
564Text encoding1 is UTF-8, 2 is UTF-16le, 3 is UTF-16be.
604User versionFree for the application: read and set with PRAGMA user_version.
644Incremental vacuum modeNon-zero for incremental vacuum, zero otherwise.
684Application IDIdentifies the application that owns the file: PRAGMA application_id.
7220Reserved for expansionMust be zero.
924Version-valid-for numberThe change counter value when the SQLite version number below was stored.
964SQLite version numberThe 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.

page 2: interiorkeys 40 | 80page 5: leafrows 1 to 40page 6: leafrows 41 to 80page 4: leafrows 81 to 117≤ 40≤ 80right-most child

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

offset 0 (100 on page 1)end of pagecell content area starts hereheadercell pointers →unallocatedcell 3freecell 2cell 1res.

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 typeCell
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.

ValueBytesLength
0001
1277f1
12881 002
16,383ff 7f2
16,38481 80 003
2,097,151ff ff 7f3
268,435,45681 80 80 80 005
34,359,738,36881 80 80 80 80 006
-1ff ff ff ff ff ff ff ff ff9

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.

04header size: 400type 0: NULL01type 1: 8-bit int11type 17: text, 2 bytes2a426f"o"6b"k"record header (4 bytes)record body: the values, in order
Serial typeMeaningBytes in body
0NULL0
18-bit integer1
216-bit integer2
324-bit integer3
432-bit integer4
548-bit integer6
664-bit integer8
764-bit float8
8integer 00
9integer 10
10reserved0
11reserved0
N ≥ 12, evenBLOB(N − 12) / 2
N ≥ 13, oddTEXT, 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:

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.