Why does sqlite3_rsync sometimes create a malformed target database in WAL mode?
08:28 23 Mar 2026

I am synchronizing a SQLite database between two embedded devices using sqlite3_rsync. Device A continuously reads and writes to a WAL‑mode SQLite database mydata.sqlite. Periodically, another process copies changes from device A → B using:

std::system("sqlite3_rsync mydata.sqlite target.sqlite");

On device B, the target database becomes corrupted at irregular intervals and I get on all subsequent rsync attempts the following error:

ERROR: SQL statement [ATTACH %Q AS 'replica'] failed: database disk image is malformed
Databases were not synced due to errors

When I run an integrity check on the target database on device B with:

std::system("sqlite3 target.sqlite \"PRAGMA integrity_check;\"");

I get the following error:

Error: in prepare, database disk image is malformed (11)

The source database on A always stays intact. If I delete the corrupted database on B, the next sync recreates it correctly. The issue occurs sporadically.

c++ database sqlite rsync data-corruption