Skip to content

Conversation

@anatolykoptev
Copy link

Summary

Add a new PostgreSQL + pgvector backend for the graph database layer. This provides a lightweight alternative to Neo4j that uses standard PostgreSQL with the pgvector extension for vector similarity search.

Key Features

  • No special graph extensions required - Uses standard PostgreSQL with pgvector only
  • Connection pooling with health checks and automatic reconnection
  • JSONB properties storage for flexible metadata
  • Vector similarity search via pgvector cosine distance operator
  • Full BaseGraphDB interface implementation including:
    • Node/edge CRUD operations
    • Graph traversal (neighbors, paths, subgraphs)
    • Metadata filtering and grouped counts
    • Memory management (remove_oldest_memory, get_grouped_counts)
    • Export/import functionality

Files Changed

  • src/memos/graph_dbs/postgres.py - New PostgresGraphDB implementation (~885 lines)
  • src/memos/configs/graph_db.py - Added PostgresGraphDBConfig
  • src/memos/graph_dbs/factory.py - Registered postgres backend

Configuration Example

```yaml
graph_db:
backend: postgres
config:
host: postgres
port: 5432
user: memos
password: secret
db_name: memos
schema_name: memos
user_name: default
embedding_dimension: 768
```

Why PostgreSQL?

  • Widely available - Most hosting providers support PostgreSQL
  • Cost effective - No need for separate graph database license
  • Simple deployment - Single database for both relational and vector data
  • Oracle Free Tier compatible - Runs on ARM64 with minimal resources

Test Plan

  • Unit tested with local PostgreSQL + pgvector
  • Integration tested with MemOS MCP server
  • Verified all BaseGraphDB methods work correctly
  • Tested connection pooling under load

Anatoly Koptev added 5 commits January 24, 2026 06:11
The MOSMCPStdioServer class was calling _setup_tools() which was not defined.
Consolidated into MOSMCPServer which has the proper implementation.
- Create PostgresGraphDB class with full BaseGraphDB implementation
- Add PostgresGraphDBConfig with connection pooling support
- Register postgres backend in GraphStoreFactory
- Update APIConfig with get_postgres_config method
- Support GRAPH_DB_BACKEND env var with neo4j fallback

Replaces Neo4j dependency with native PostgreSQL using:
- JSONB for flexible node properties
- pgvector for embedding similarity search
- Standard SQL for graph traversal
Match krolik schema embedding dimension for compatibility
Add remove_oldest_memory and get_grouped_counts methods required by
MemOS memory management functionality.
The merge/deduplicate logic was converting hit IDs to a set, losing
the score-based ordering from vector search. Now keeps highest score
per ID and returns results sorted by similarity score (descending).

Fixes both _vector_recall and _fulltext_recall methods.
When embeddings aren't available, the reranker was defaulting to 0.5
scores, ignoring the relativity scores set during the recall phase.

Now uses item.metadata.relativity from the recall stage when available.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.

1 participant