Skip to content

fix: add SQLite indexes to prevent full table scans on history and messages#5009

Open
PHclaw wants to merge 5 commits intomem0ai:mainfrom
PHclaw:fix/sqlite-indexes
Open

fix: add SQLite indexes to prevent full table scans on history and messages#5009
PHclaw wants to merge 5 commits intomem0ai:mainfrom
PHclaw:fix/sqlite-indexes

Conversation

@PHclaw
Copy link
Copy Markdown

@PHclaw PHclaw commented Apr 29, 2026

Summary

The history and messages tables had no indexes, causing full table scans on every query. As the database grows, this leads to O(n) performance degradation.

Changes

Added SQLiteManager._create_indexes() method with:

Index Table Column(s) Used by
idx_history_memory_id history memory_id get_history()
idx_history_created_at history created_at ORDER BY sorting
idx_messages_session_scope messages session_scope session-based queries
idx_messages_created_at messages created_at ORDER BY sorting
idx_messages_scope_created messages (session_scope, created_at) get_last_messages() composite

All indexes use CREATE INDEX IF NOT EXISTS for safe migration.

Closes #4988

PHclaw added 5 commits April 28, 2026 16:04
API keys were previously passed to the telemetry subprocess via
sys.argv, exposing them through ps, /proc/<pid>/cmdline, and
process inspection tools on all platforms.

Changes:
- telemetry.py: read API key from config and pass via MEM0_API_KEY
  environment variable in subprocess.Popen env= parameter
- telemetry_sender.py: read MEM0_API_KEY from os.environ instead of
  ctx dict; updated docstring to document the security model
- test_telemetry_security.py: add regression tests verifying the API
  key never appears in subprocess argv and is correctly passed via
  the environment variable
…move --reload

Three production-hardening fixes for server/Dockerfile:

1. Add libpq5 system library required by psycopg (PostgreSQL adapter).
   Without it, python:3.12-slim fails at runtime with:
   ImportError: libpq.so.5: cannot open shared object file

2. Create and switch to non-root 'app' user.
   Running containers as root is a security anti-pattern.

3. Remove --reload flag from CMD.
   --reload watches files and restarts on changes (development-only).
   The docker-compose.yaml already overrides CMD with --reload for dev.
   Production images should not include it.
…ruption

Concurrent AsyncMemory.add() calls caused 'index N is out of bounds' errors
in Qdrant because multiple upsert() calls to the same collection corrupt
the HNSW index structure during concurrent writes.

Fix:
- Add self._vs_lock = asyncio.Lock() in AsyncMemory.__init__
- Wrap vector store writes in add() with 'async with self._vs_lock'
- This serializes concurrent writes to the same collection

Closes mem0ai#4892
…ssages

The history and messages tables had no indexes, causing full table scans
on every query. This leads to O(n) performance degradation as the database
grows.

Added indexes on:
- history(memory_id) for get_history() lookups
- history(created_at) for ORDER BY sorting
- messages(session_scope) for session-based queries
- messages(created_at) for ORDER BY sorting
- messages(session_scope, created_at) composite for get_last_messages()

Closes mem0ai#4988
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


PHclaw seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: avoid full table scan on every delete/update in _remove_memory_from_entity_store

2 participants