Skip to content

fix(db): add missing index on dataflow.call_edge_id (#1948) - #2203

Merged
carlos-alm merged 1 commit into
mainfrom
fix/dataflow-call-edge-id-index-1948
Aug 2, 2026
Merged

carlos-alm merged 1 commit into
mainfrom
fix/dataflow-call-edge-id-index-1948

Conversation

@carlos-alm

Copy link
Copy Markdown
Contributor

Summary

Root-causes and fixes the WASM engine's detectMs regression reported in #1948.

dataflow.call_edge_id (added in migration v18, REFERENCES edges(id)) was never given its own index. Every DELETE FROM edges forces SQLite to scan the entire dataflow table to enforce the foreign-key constraint, and the explicit dataflowByCallEdge cleanup query in build-stmts.ts scans it too. Confirmed via EXPLAIN QUERY PLAN: without the index both queries show SCAN dataflow; with it, both become an indexed SEARCH.

Why WASM-only: better-sqlite3 defaults PRAGMA foreign_keys = ON. The native/rusqlite connection (crates/codegraph-core/src/db/connection.rs) never sets this pragma at all, so it stays at SQLite's true default of OFF — native never pays the FK-check-scan cost. This exactly matches #1948's own report that native's detectMs stayed flat (2-3ms) while WASM regressed.

Investigation note

A previous /fixer pass's comment on #1948 hypothesized the cause was uncached db.prepare() calls inside clearDeletedExportAdvisories/recordDeletedExportAdvisories. I measured that path directly (microbenchmark against both an in-memory DB and a real WAL-mode file-backed DB) and found it costs ~0.02-0.3ms per call — nowhere near the ~57ms reported gap. That hypothesis doesn't hold up quantitatively. Instrumenting the real detectChanges pipeline end-to-end on this repo's own build pointed to purgeStaleReverseDeps → purgeFilesFromGraph → the FK-check scan as the actual dominant cost (~81ms of ~91ms total on one profiling run).

Verification

  • EXPLAIN QUERY PLAN before/after: DELETE FROM edges ... and the dataflowByCallEdge delete both flip from SCAN dataflow to SEARCH dataflow USING COVERING INDEX idx_dataflow_call_edge
  • Direct before/after benchmark of purgeFilesData against this repo's own built graph (~19.8K nodes, ~41.8K edges): 62.01ms → 1.47ms (97.6% reduction) for a 1-file purge
  • End-to-end via the real CLI (codegraph build --engine wasm, 1-file touch + incremental rebuild): purgeStaleReverseDeps dropped from 62.02ms → 1.79ms, total handle*Build from 63.26ms → 2.93ms — combined with getChangedFiles (~9ms), overall detectMs for a 1-file rebuild now lands well under the pre-regression 3.15.0 baseline of 56ms
  • cargo test --release (crates/codegraph-core): 669 passed
  • npm test: 260 files, 4197 passed, 30 skipped, 2 todo, 0 failed
  • npm run lint: clean
  • Migration is mirrored in both engines (src/db/migrations.ts v23, crates/codegraph-core/src/db/connection.rs v23) and is a plain CREATE INDEX IF NOT EXISTS, safe on existing databases

Refs #1948

dataflow.call_edge_id (added in migration v18, `REFERENCES edges(id)`)
was never given its own index. Every DELETE FROM edges forces SQLite
to scan the entire dataflow table to enforce the FK constraint, and
the explicit dataflowByCallEdge cleanup query in build-stmts.ts scans
it too — both confirmed via EXPLAIN QUERY PLAN to flip from `SCAN
dataflow` to an indexed SEARCH once this index exists.

better-sqlite3 defaults `PRAGMA foreign_keys = ON`; the native/rusqlite
connection never sets this pragma, so only the WASM engine ever paid
this cost — matching #1948's report that WASM-only detectMs regressed
while native stayed flat. Measured on this repo's own graph (~19.8K
nodes, ~41.8K edges): a 1-file incremental purge dropped from 62ms to
1.47ms, and confirmed end-to-end via the real build CLI that overall
detectMs for a 1-file rebuild drops well below the pre-regression
3.15.0 baseline (56ms).

The prior /fixer pass's hypothesis (blaming clearDeletedExportAdvisories/
recordDeletedExportAdvisories for uncached db.prepare() calls) measured
at only ~0.3ms per call — not the actual cause. This fix targets the
verified root cause instead.

No README/CLAUDE.md/ROADMAP.md changes needed — neither enumerates
individual migration versions (docs check acknowledged).

Refs #1948
@greptile-apps

greptile-apps Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds a mirrored SQLite migration for the TypeScript and native database engines to index dataflow.call_edge_id, avoiding full-table scans during edge cleanup.

  • Adds schema migration v23 to both migration arrays.
  • Uses identical, idempotent CREATE INDEX IF NOT EXISTS statements across engines.
  • Preserves migration ordering after the migration that introduced call_edge_id.

Confidence Score: 5/5

The PR appears safe to merge; the migration is compatible with fresh and existing databases and remains synchronized across both database engines.

Both migration paths create dataflow and add call_edge_id before applying the identical, idempotent v23 index statement, with no conflicting index definition present.

Important Files Changed

Filename Overview
src/db/migrations.ts Adds a correctly ordered v23 index migration after dataflow.call_edge_id was introduced in v18.
crates/codegraph-core/src/db/connection.rs Mirrors the TypeScript v23 migration with identical SQLite DDL for native database compatibility.

Reviews (1): Last reviewed commit: "fix(db): add missing index on dataflow.c..." | Re-trigger Greptile

@carlos-alm
carlos-alm merged commit cf17bb9 into main Aug 2, 2026
33 checks passed
@carlos-alm
carlos-alm deleted the fix/dataflow-call-edge-id-index-1948 branch August 2, 2026 05:03
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant