-
Notifications
You must be signed in to change notification settings - Fork 180
upsert_entity takes 1.8s avg for single entity writes on cloud #709
Copy link
Copy link
Closed
Labels
Milestone
Description
Problem
entity_service.edit.upsert_entity averages 1.8s (P50: 1.89s, P95: 2.84s) for a single entity upsert. In the worst case (trace 019d49a39cffc28f742bca0fd6a8793f), it took 2.6s. This is the single largest span in the write path.
For create, upsert_entity averages 1.13s (P50: 0.78s, P95: 1.97s).
This is likely multiple sequential DB queries to Neon PostgreSQL: upserting the entity row, then diffing and upserting observations, then diffing and upserting relations — each paying ~200-300ms network latency.
Evidence (last 12h)
| Span | Avg | P50 | P95 | Calls |
|---|---|---|---|---|
| entity_service.edit.upsert_entity | 1.79s | 1.89s | 2.84s | 81 |
| entity_service.create.upsert_entity | 1.13s | 0.78s | 1.97s | 49 |
| entity_service.update.upsert_entity | 1.07s | 1.08s | 1.24s | 3 |
| entity_service.edit.update_checksum | 0.26s | 0.24s | 0.38s | 81 |
| entity_service.create.update_checksum | 0.26s | 0.25s | 0.33s | 49 |
Full edit_note write path breakdown (worst case trace):
write_entity 4.201s
├── resolve_entity 0.330s (DB lookup)
├── read_file 0.406s (S3 get)
├── apply_operation 0.002s (in-memory edit)
├── write_file 0.428s (S3 put)
├── parse_markdown 0.084s (CPU)
├── upsert_entity 2.612s ← biggest span
└── update_checksum 0.334s (DB update)
Investigation Areas
- Profile the queries inside upsert_entity — add telemetry sub-spans for entity upsert, observation diff/upsert, relation diff/upsert to identify which DB operations are slowest
- Batch DB writes — if upsert_entity does multiple sequential INSERT/UPDATE statements, consider combining them into fewer round-trips (e.g., bulk upsert observations in one query)
- update_checksum as separate query — the 0.26s checksum update could potentially be folded into the upsert transaction to eliminate a round-trip
Impact
upsert_entity + update_checksum account for ~2s of the ~3.3s avg write_entity duration. Reducing DB round-trips here would directly improve edit_note (P50: 6.7s) and write_note (P50: 6.2s) — the two slowest MCP tools.
Reactions are currently unavailable