Repository navigation
Description
What happened?
After a folder database was created fresh (the folder was removed and re-added), the initial sync from one peer ran at about 4 files/s and ~0.35 MB/s for hours, with Syncthing at ~200% CPU. The network path to the peer measured 50–80 MB/s, and the peer was idle.
Running ANALYZE on the folder database (with Syncthing stopped) brought it to ~100 files/s right away: 3,084 files completed in the next 30 seconds, and the remaining backlog finished within minutes.
Folder: about 83k files and 17k directories (6.1 GB), mostly small files. The folder database was ~200 MB, and its files table had ~411k rows (90k local, 321k from the one remote device).
Where the time went
A CPU profile from /rest/debug/cpuprof during the slow phase put 88% of CPU time in the copier:
sendReceiveFolder.pullerIteration.func2
→ copierRoutine → copyBlock → copyBlockFromFolder
→ sqlite.iterStructs[BlockMapEntry] (AllLocalBlocksWithHash)
The syncthing_db_operations_total metric showed AllLocalBlocksWithHash at roughly one call per file. Its syncthing_db_operation_seconds_total stayed small, because that metric doesn't include the time spent iterating the rows.
Root cause: no sqlite_stat1, so the planner drives the query from files
The folder database had no sqlite_stat1 table at all. Without statistics, the query in AllLocalBlocksWithHash is planned as a walk over every local file, probing blocks for each one:
SELECT f.blocklist_hash as blocklisthash, b.idx as blockindex, b.offset, b.size, n.name as filename FROM files f
INNER JOIN file_names n ON f.name_idx = n.idx
LEFT JOIN blocks b ON f.blocklist_hash = b.blocklist_hash
WHERE f.device_idx = {{.LocalDeviceIdx}} AND b.hash = ?| Query plan (first step) | Time per lookup | |
|---|---|---|
No sqlite_stat1 |
SEARCH f USING INDEX files_device_name (device_idx=?) |
~45–56 ms |
After ANALYZE |
SEARCH b USING PRIMARY KEY (hash=?), then SEARCH f USING INDEX files_blocklist_hash_only |
~0.4 ms |
That's about 130× slower per block, and it gets worse as the folder grows, because the scan covers every local file. The puller can't get ahead of the copier, so this caps the whole sync.
Why a new folder database has no statistics
ANALYZE only runs in the periodic maintenance (tidy). The "last maintenance" timestamp is stored once in the main database (internalMeta on sdb, read at L71–72), not per folder. So:
- A folder database created while Syncthing has run maintenance recently (adding a folder, re-adding one, or deleting a single
folder.*.db) gets no statistics for up to--db-maintenance-interval, which is 8h by default. That window is exactly when the initial sync of the whole folder happens. This is what happened here. - On a completely fresh install (no
main.db), maintenance runs about a minute after start, while the folder database is still almost empty. On a second device of ours this produced statistics taken at ~52k rows of an eventual ~418k. The plan happened to still be correct there, but the statistics describe a database very different from the real one.
Possible fixes
-
Make the query independent of statistics. On a copy of the affected database with
sqlite_stat1dropped, forcing the join order withCROSS JOINgets the good plan even without statistics:SELECT f.blocklist_hash, b.idx, b.offset, b.size, n.name FROM blocks b CROSS JOIN files f ON f.blocklist_hash = b.blocklist_hash INNER JOIN file_names n ON f.name_idx = n.idx WHERE b.hash = ? AND f.device_idx = {{.LocalDeviceIdx}}
Variant (no statistics) First plan step Time Current query SEARCH f USING INDEX files_device_name42 ms INNER JOINinstead ofLEFT JOINSEARCH f USING INDEX files_device_name41 ms FROM blocksfirst,INNER JOINSEARCH f USING INDEX files_device_name42 ms FROM blocks CROSS JOIN filesSEARCH b USING PRIMARY KEY (hash=?)0.27 ms I tested these with the sqlite3 CLI 3.54.0. Syncthing v2.1.6 bundles 3.53.4, so I'd expect the same result but haven't confirmed it with that exact version.
-
Collect statistics when a folder database is new, for example
ANALYZE(orPRAGMA optimize) once after the first full index has been received or the first scan completes. Or track the maintenance time per folder database, so a new one is due right away.
Related: #10902 is the same class of problem (a planner-dependent full walk) on a different query (neededGlobalFilesLocal).
Disclosure: the investigation and this write-up were done with help from Claude (Anthropic's AI assistant). All measurements, profiles and query plans above come from my own devices.
Syncthing version
v2.1.6 "Hafnium Hornet" (go1.27.1 darwin-arm64), bundled SQLite 3.53.4. The query and the maintenance logic are unchanged in v2.1.7 and on main at ed38528.
Platform & operating system
macOS 27.2 (Apple Silicon). The peer is the Linux Docker image, v2.1.7.
Browser version
n/a
Relevant log output
# No errors or warnings were logged; the log only shows "Synced file" entries at a low rate.
# Diagnostic commands, for anyone checking their own database:
# Are there statistics?
sqlite3 folder.XXXX.db "SELECT count(*) FROM sqlite_master WHERE name='sqlite_stat1';"
# Which plan does the block lookup get? (first line: SEARCH b = good, SEARCH f = bad)
sqlite3 folder.XXXX.db "EXPLAIN QUERY PLAN SELECT 1 FROM files f JOIN file_names n ON f.name_idx = n.idx LEFT JOIN blocks b ON f.blocklist_hash = b.blocklist_hash WHERE f.device_idx = 1 AND b.hash = x'00';"
# Workaround (with Syncthing stopped)
sqlite3 folder.XXXX.db "ANALYZE;"
Activity
sspilleman commentedon Oct 29, 2026
A refinement on the fresh-install case, after re-reading
periodic()indb_service.go:tidy()(soANALYZE) when the local sequence equalslastSuccessfulGCSeq.sqlite_stat1at all until the next run, 8h later. Restarts don't reset that timer, since it's stored inmain.db.In my tests, statistics taken on an empty or partly filled folder database still gave the good plan. The bad plan only appears with no statistics at all.
sspilleman commentedon Oct 29, 2026
Basically my understanding is the following. My installation was a fresh new client. I understand that by default only after eight hours this is resolved. In practice that’s usually too late, because the slow part is the initial sync
bt90 commentedon Oct 29, 2026
Sounds plausible
moraouf11 commentedon Nov 1, 2026
can I work on this fix ? I look at it and I plan to do as mentioned, using the cross join, as it seems the easier and most direct fix for this.