The admin overview dashboard polls /admin/overview every 30s. Its `manga_stats` aggregate evaluated MANGA_SYNC_STATE_CASE over the WHOLE library (14.5k mangas) with no LIMIT, and that CASE runs a correlated EXISTS over crawler_jobs. With no index on the `sync_chapter_list` manga_id path, Postgres seqscanned all in-flight jobs *per manga* — O(mangas x jobs). The disabled-analysis backlog (9.5k pending `analyze_page` rows that can never match the sync-kind filter) inflated every scan, so each call took 6-7 min. The 30s poll stacked ~10 of them concurrently → load 11, Postgres at ~100%. EXPLAIN ANALYZE on the live DB: the rewrite drops the query from 6-7 min to ~1.5s (per-manga crawler_jobs seqscan → single index-backed pass). Fixes: - Rewrite `manga_stats` to collect the in-flight manga-id set in ONE pass over the sync jobs (index-backed), then classify via a hash semi-join — O(mangas + jobs), immune to the analyze_page backlog size. - Add partial index crawler_jobs_sync_chapter_list_manga_idx (migration 0029) covering the sync_chapter_list manga_id path; mirrors the existing sync_manga index (0020). Also speeds the per-row Mangas tab listing. - statement_timeout backstop (5s) on the aggregate so a pathological plan can never pin a backend for minutes again. - Single-flight + 10s TTL cache on the /admin/overview handler so concurrent pollers coalesce instead of stacking. - Slow the dashboard poll 30s -> 60s (server-cached now anyway). The in_progress rule in the rewrite is kept in lockstep with the first arm of MANGA_SYNC_STATE_CASE (documented in both places). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2.3 KiB
2.3 KiB