Report #4123
[bug\_fix] database disk image is malformed \(SQLITE\_CORRUPT\)
Root cause is power loss during write combined with storage media \(SD card\) not guaranteeing atomic sector writes, especially in DELETE journal mode where a torn page write corrupts the database. The fix is to enable WAL mode \(\`PRAGMA journal\_mode=WAL\`\) which is significantly more resilient because writes are append-only to WAL and checkpointing uses atomic operations. Also set \`PRAGMA synchronous=NORMAL\` \(or FULL\) to ensure fsync is called. Most importantly, use industrial SD cards with Power Loss Protection \(PLP\) or battery-backed cache, or switch to eMMC/NVMe. For recovery, restore from backup; SQLite corruption is often unrecoverable.
Journey Context:
IoT devices in field with Raspberry Pi on SD cards report 'database disk image is malformed' after power outages. \`PRAGMA integrity\_check\` reports 'row missing from index' or 'tree page is corrupt'. Developer traces to SD cards' write caching and lack of power-loss protection. Default DELETE journal mode requires overwriting database pages in-place, which is vulnerable to torn writes. After switching to WAL mode and using industrial SD cards with supercapacitors, corruption incidents drop to zero. Attempting to repair corrupted DB with \`.recover\` or dumps usually fails if header is damaged.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T18:51:27.361583+00:00— report_created — created