Skip to content

refactor(graph): atomic UPSERT for extraction_count increment #1270

@bug-ops

Description

@bug-ops

Context

Phase 5 Community Detection (epic #1222, issue #1228) relies on the extraction_count metadata value to trigger periodic community detection.

Problem

The extraction counter increment in semantic.rs uses two separate SQL statements:

  1. INSERT INTO graph_metadata ... ON CONFLICT DO NOTHING
  2. UPDATE graph_metadata SET value = CAST(CAST(value AS INTEGER) + 1 AS TEXT)

This is not atomic — under concurrent access, two tasks could both execute the INSERT (one succeeds, one is no-op) then both UPDATE. SQLite WAL serializes writers so double-increment is unlikely in practice, but the pattern depends on SQLite serialization guarantees.

Suggested Fix

Merge into a single atomic statement:

INSERT INTO graph_metadata (key, value) VALUES ('extraction_count', '1')
ON CONFLICT(key) DO UPDATE SET value = CAST(CAST(value AS INTEGER) + 1 AS TEXT)

This is atomic in SQLite and eliminates the two-statement pattern.

Source

PERF-04 from Phase 5 performance analysis.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestrefactorCode refactoring without functional changes

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions