Skip to content

Pulling crawls on a new folder database: no SQLite statistics until first maintenance, so AllLocalBlocksWithHash scans every local file per block #10947

Open

Description

@sspilleman

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

  1. Make the query independent of statistics. On a copy of the affected database with sqlite_stat1 dropped, forcing the join order with CROSS JOIN gets 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_name 42 ms
    INNER JOIN instead of LEFT JOIN SEARCH f USING INDEX files_device_name 41 ms
    FROM blocks first, INNER JOIN SEARCH f USING INDEX files_device_name 42 ms
    FROM blocks CROSS JOIN files SEARCH 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.

  2. Collect statistics when a folder database is new, for example ANALYZE (or PRAGMA 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

  1. added theissue type on Oct 29, 2026
  2. added
    bug
    needs-triage
    on Oct 29, 2026
  3. sspilleman commented on Oct 29, 2026

    @sspilleman
    Author

    A refinement on the fresh-install case, after re-reading periodic() in db_service.go:

    • The first maintenance runs ~1 min after first start. Per folder, it skips GC and tidy() (so ANALYZE) when the local sequence equals lastSuccessfulGCSeq.
    • On a typical new install, the folder is accepted minutes later, so its database is created after that first run and gets no sqlite_stat1 at all until the next run, 8h later. Restarts don't reset that timer, since it's stored in main.db.
    • At ~4 files/s, an initial sync of ~80k files takes 5–6h, so it can finish before the statistics ever arrive.

    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.

  4. sspilleman commented on Oct 29, 2026

    @sspilleman
    Author

    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

  5. bt90 commented on Oct 29, 2026

    @bt90
    Contributor

    Sounds plausible

  6. moraouf11 commented on Nov 1, 2026

    @moraouf11

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds-triage

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions