#4832·cognee

[Bug]: GET /datasets/graph-summary materializes entire Neo4j graph and triggers OOM errors

Author: MixtonCreated Aug 30, 2026Updated Sep 30, 2026
Labelsbug

Bug Description

GET /api/v1/datasets/graph-summary exhausts Neo4j transaction memory pool on large graphs by materializing the complete graph instead of performing lightweight count queries.

Root Cause

The endpoint calls:

get_datasets_graph_summary()
  → graph_engine.get_graph_metrics(include_optional=False)
    → Neo4jAdapter.get_model_independent_graph_data()

Which executes memory-intensive Cypher queries:

cypher
MATCH (n:`__Node__`) 
RETURN collect(n) AS nodes

MATCH (n:`__Node__`)-[r]->(m:`__Node__`) 
RETURN collect([n, r, m]) AS elements

The second query materializes the entire graph—all nodes and edges with all their properties—into a single Neo4j aggregation result. On large graphs, this allocates multiple GiB of transaction memory within seconds, eventually failing with:

Neo.TransientError.General.MemoryPoolOutOfMemoryError
dbms.memory.transaction.total.max threshold reached

Retry Amplification Issue

Because Neo4jAdapter.query() is decorated with @deadlock_retry(), and the decorator retries all errors containing Neo.TransientError, this deterministic OOM error is retried 10 times consecutively, each time materializing the entire graph again.

Result: a single graph-summary request can consume 30+ GiB of transaction memory across retries and eventually crash Neo4j.

Caching Ineffective

Since the initial computation deterministically fails, the GraphMetrics cache entry is never written. Every subsequent request to /datasets/graph-summary triggers the same retry storm.

Steps to Reproduce

  1. Deploy Cognee with Neo4j 5.26.29 Community and a moderately-large graph (10k+ nodes and edges)
  2. Call GET /api/v1/datasets/graph-summary
  3. Observe Neo4j transaction memory growth in logs
  4. Transaction fails with MemoryPoolOutOfMemoryError after 3-5 seconds
  5. @deadlock_retry() decorator retries the same query 9 more times
  6. Each retry attempt materializes the complete graph again
  7. Subsequent requests trigger the same retry storm (no cache, first attempt failed)

Expected Behavior

/datasets/graph-summary should obtain node and edge counts using bounded-memory count queries that complete in < 1 second and use O(1) memory regardless of graph size.

For Neo4j, this should be:

cypher
MATCH (n:`__Node__`)
RETURN count(n) AS num_nodes

MATCH (n:`__Node__`)-[r]->(m:`__Node__`)
RETURN count(r) AS num_edges

This pattern already works correctly in Neptune's adapter, proving it is a viable solution.

Additionally, MemoryPoolOutOfMemoryError should not be retried—it is a structural problem with the query, not a transient race condition like actual deadlocks.

Actual Behavior

Observable transaction memory growth (from Neo4j logs on moderately-large graph):

0.12 GiB after 0.06s
1.49 GiB after 0.72s
3.68 GiB after 2.07s
5.71 GiB after 3.37s
[Transaction fails with MemoryPoolOutOfMemoryError]
[deadlock_retry() restarts the same query]

Request behavior:

  • First request: 10 retry attempts × full graph materialization = eventual Neo4j crash or timeout
  • Subsequent requests: same retry storm (GraphMetrics cache never written because first attempt failed)
  • Other concurrent queries: may timeout or fail due to memory contention

Impact: API endpoint becomes a denial-of-service vector for large graphs; even polling for status updates can crash Neo4j.

Environment

  • OS: Linux (Docker Compose deployment)
  • Python version: 3.x (not specified in original report)
  • Cognee version: 1.5.0-local (also reproduced by source inspection in released 1.5.3)
  • Database: Neo4j 5.26.29 Community
  • Storage: PostgreSQL + PGVector
  • Graph size: Moderately-large (10k+ nodes and edges)

Logs/Error Messages

bash
Neo.TransientError.General.MemoryPoolOutOfMemoryError
dbms.memory.transaction.total.max threshold reached


Transaction is retried 10 times via `@deadlock_retry()` decorator, materializing the entire graph on each attempt:

**Relevant code locations:**
- Problematic method: `cognee/infrastructure/databases/graph/neo4j_driver/adapter.py#L1772-L1791` (`get_model_independent_graph_data`)
- Graph metrics endpoint: `cognee/api/v1/datasets/routers/get_datasets_router.py#L509-L646` (`get_datasets_graph_summary`)
- Deadlock retry decorator: `cognee/infrastructure/databases/graph/neo4j_driver/adapter.py` (decorates `query()` method)

Additional Context

Comparative: Neptune Implementation (Correct)

Neptune's adapter already implements this correctly using COUNT queries:

python
query_string = f"""
    MATCH (n :{self._GRAPH_NODE_LABEL})
    WITH count(n) AS nodeCount
    MATCH (a :{self._GRAPH_NODE_LABEL})-[r]->(b :{self._GRAPH_NODE_LABEL})
    RETURN nodeCount AS numVertices, count(r) AS numEdges
"""

See: cognee/infrastructure/databases/graph/neptune_driver/adapter.py#L1475-L1495

This proves the pattern works and should be adopted for Neo4j.

Related Issues

Related to #4233 (full-graph projection through remember → improve → memify), but this report is distinct:

  • Triggered by: GET /api/v1/datasets/graph-summary → get_graph_metrics() → get_model_independent_graph_data()
  • Distinct problem: OOM retry amplification on a lightweight endpoint, not a heavy computation pipeline

Proposed Fixes

Priority 1 (Required):

  1. Add lightweight get_graph_counts() method to GraphDBInterface that returns only (num_nodes, num_edges) using COUNT queries
  2. Implement get_graph_counts() in Neo4jAdapter with native COUNT Cypher (mirroring Neptune's pattern)
  3. Update /datasets/graph-summary endpoint to call get_graph_counts() instead of get_graph_metrics()
  4. Exclude MemoryPoolOutOfMemoryError from deadlock_retry() decorator—immediately re-raise (not a retryable error)

Priority 2 (Recommended): 5. Update get_model_independent_graph_data() to avoid collect() when only counts are needed 6. Add regression test: mock Neo4j adapter to verify /graph-summary endpoint never calls full-graph projection methods

Priority 3 (Follow-up): 7. Audit all callers of get_model_independent_graph_data() and get_graph_metrics() for similar OOM vulnerabilities

Pre-submission Checklist

  • I have searched existing issues to ensure this bug hasn't been reported already
  • I have provided a clear and detailed description of the bug
  • I have included steps to reproduce the issue
  • I have included my environment details