Skip to content

Lesson 1 of 9 · 22m

One File, Many Structures: Pages, B-Trees, and the Database Image

Why SQLite is a real database engine in a single file, how pages and B-trees organize data, and what that means for reads, writes, and copying files.

SQLite is often described as a toy because it is embedded and serverless. That is backwards. It is a complete relational database engine — SQL parser, query planner, transaction manager, crash recovery — that stores the database as a carefully versioned disk image instead of behind a network daemon.

The database is a sequence of pages

Every SQLite database file is divided into fixed-size pages, usually 4096 bytes on modern systems:

PRAGMA page_size;       -- commonly 4096
PRAGMA page_count;      -- total pages in the main database file
PRAGMA freelist_count;  -- unused pages available for reuse

Tables and indexes are B-trees built from those pages. Interior pages point to child pages; leaf pages contain records. A rowid table is a B-tree keyed by a signed 64-bit integer rowid. If you do not declare one explicitly, SQLite gives you a hidden rowid column.

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  email TEXT NOT NULL UNIQUE,
  display_name TEXT NOT NULL
);

SELECT rowid, id FROM users LIMIT 3;

INTEGER PRIMARY KEY is special: it becomes an alias for rowid, so lookups by id are direct B-tree probes. Other primary keys create an index but do not replace the rowid unless the table is declared WITHOUT ROWID.

Records are compact, typed byte sequences

SQLite uses manifest typing. Columns have affinities (INTEGER, TEXT, REAL, BLOB, NUMERIC), but individual values carry their own type. That flexibility is useful and dangerous: SQLite will happily compare or store values in ways stricter engines reject.

CREATE TABLE measurements (
  id INTEGER PRIMARY KEY,
  sensor TEXT NOT NULL,
  value REAL NOT NULL,
  captured_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now'))
);

Prefer ISO-8601 UTC text, integer Unix epoch milliseconds, or real Julian day values consistently. Do not mix all three in one column and expect time queries to be understandable.

The file is not a backup format while hot

A database file can be copied safely only when no writer can modify it. In rollback-journal mode there may also be a -journal file; in WAL mode there may be -wal and -shm files containing committed data not yet merged into the main database. Copying only app.db while WAL is active can lose data or capture an inconsistent image.

Use SQLite's backup API, .backup in the CLI, or stop writes first:

sqlite3 app.db ".backup 'app-backup.db'"

Field notes

  • INTEGER PRIMARY KEY means rowid alias; PRIMARY KEY on TEXT does not.
  • VACUUM rebuilds the file and reclaims freelist space, but temporarily needs roughly the database size in free disk space.
  • PRAGMA integrity_check reads every page structure, not every semantic fact; corruption can still hide in application-level invariants.
  • Never edit a database file with a hex editor, filesystem-level dedupe, or copy-on-write snapshot trick unless you understand journal state cold.

Was this lesson helpful?

Sign in to rate