Storage Format

tursodatabase/turso/.claude/skills/storage-format

作者 tursodatabaseff97ec42cdef無授權條款24K 個星標收錄於 2026年10月9日更新於 2026年10月8日儲存庫今天更新

SQLite file format, B-trees, pages, cells, overflow, freelist that is used in tursodb

AI 產生的概覽

介紹 Turso 所用 SQLite 磁碟檔案格式的參考指南,涵蓋 B 樹、頁、儲存格、溢位頁與空閒清單。

功能
此技能是一份參考文件,說明 SQLite 資料庫檔案格式:100 位元組資料庫標頭、頁類型、資料表與索引 B 樹結構、儲存格與記錄編碼、溢位頁鏈以及空閒清單。它也將這些結構對應到 Turso 的實作檔案,並列出用來檢查資料庫的 PRAGMA 指令。它產出的是說明內容,而非檔案或程式碼。
適用情境
當你需要理解或說明 SQLite 或 Turso 如何在磁碟上儲存資料時使用,例如讀取原始頁、偵錯儲存層行為,或解讀標頭欄位與頁類型。
執行需求
不需要指令碼或工具,僅為說明與參考資料。偵錯範例假定已安裝 Rust 工具鏈與 tursodb 執行檔,參考連結指向外部文件。

Storage Format Guide

Database File Structure

┌─────────────────────────────┐│ Page 1: Header + Schema     │  ← First 100 bytes = DB header├─────────────────────────────┤│ Page 2..N: B-tree pages     │  ← Tables and indexes│            Overflow pages   ││            Freelist pages   │└─────────────────────────────┘

Page size: power of 2, 512-65536 bytes. Default 4096.

Database Header (First 100 Bytes)

OffsetSizeField
016Magic: "SQLite format 3\0"
162Page size (big-endian)
181Write format version (1=rollback, 2=WAL)
191Read format version
244Change counter
284Database size in pages
324First freelist trunk page
364Total freelist pages
404Schema cookie
564Text encoding (1=UTF8, 2=UTF16LE, 3=UTF16BE)

All multi-byte integers: big-endian.

Page Types

FlagTypePurpose
0x02Interior indexIndex B-tree internal node
0x05Interior tableTable B-tree internal node
0x0aLeaf indexIndex B-tree leaf
0x0dLeaf tableTable B-tree leaf
-OverflowPayload exceeding cell capacity
-FreelistUnused pages (trunk or leaf)

B-tree Structure

Two B-tree types:

  • Table B-tree: 64-bit rowid keys, stores row data
  • Index B-tree: Arbitrary keys (index columns + rowid)
Interior page:  [ptr0] key1 [ptr1] key2 [ptr2] ...                   │         │         │                   ▼         ▼         ▼               child     child     child               pages     pages     pages
Leaf page:     key1:data  key2:data  key3:data ...

Page 1 always root of sqlite_schema table.

Cell Format

Table Leaf Cell

[payload_size: varint] [rowid: varint] [payload] [overflow_ptr: u32?]

Table Interior Cell

[left_child_page: u32] [rowid: varint]

Index Cells

Similar but key is arbitrary (columns + rowid), not just rowid.

Record Format (Payload)

[header_size: varint] [type1: varint] [type2: varint] ... [data1] [data2] ...

Serial types:

TypeMeaning
0NULL
1-41/2/3/4 byte signed int
56 byte signed int
68 byte signed int
7IEEE 754 float
8Integer 0
9Integer 1
≥12 evenBLOB, length=(N-12)/2
≥13 oddText, length=(N-13)/2

Overflow Pages

When payload exceeds threshold, excess stored in overflow chain:

[next_page: u32] [data...]

Last page has next_page=0.

Freelist

Linked list of trunk pages, each containing leaf page numbers:

Trunk: [next_trunk: u32] [leaf_count: u32] [leaf_pages: u32...]

Turso Implementation

Key files:

  • core/storage/sqlite3_ondisk.rs - On-disk format, PageType enum
  • core/storage/btree.rs - B-tree operations (large file)
  • core/storage/pager.rs - Page management
  • core/storage/buffer_pool.rs - Page caching

Debugging Storage

bash
# Integrity checkcargo run --bin tursodb test.db "PRAGMA integrity_check;"
# Page countcargo run --bin tursodb test.db "PRAGMA page_count;"
# Freelist infocargo run --bin tursodb test.db "PRAGMA freelist_count;"

References

來源與署名

來源:tursodatabase/turso位於.claude/skills/storage-format提交ff97ec4

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架