pgvectorscale_memory_integration.md · 13.0 KB

pgvectorscale Memory Integration

Overview

pgvectorscale is the semantic memory backbone of the mindX autonomous intelligence platform, providing vector similarity search capabilities for context-aware reasoning and self-improvement.

Status: ✅ DEPLOYED & OPERATIONAL Integration: PostgreSQL 15+ with pgvector extension Architecture: Dual-write mode (PostgreSQL + JSON files) Performance: <100ms semantic search queries with 1M+ vectors


🏗️ Architecture

Core Components

📊 PostgreSQL with pgvector

🧠 Embedding Engine

🔄 Memory Agent Integration

🎯 RAGE System Integration


📋 Database Schema

Core Tables

memories - Main Memory Storage

CREATE TABLE memories (
    memory_id VARCHAR(64) PRIMARY KEY,
    agent_id VARCHAR(255) NOT NULL,
    memory_type VARCHAR(50) NOT NULL,
    importance INTEGER NOT NULL,
    timestamp TIMESTAMPTZ NOT NULL,
    content JSONB NOT NULL,
    context JSONB,
    tags TEXT[],
    parent_memory_id VARCHAR(64),
    created_at TIMESTAMPTZ DEFAULT NOW()
);

memory_embeddings - Vector Storage

CREATE TABLE memory_embeddings (
    memory_id VARCHAR(64) PRIMARY KEY REFERENCES memories(memory_id) ON DELETE CASCADE,
    embedding vector(384),
    text_content TEXT NOT NULL,
    created_at TIMESTAMPTZ DEFAULT NOW()
);

resource_metrics - Performance Monitoring

CREATE TABLE resource_metrics (
    metric_id SERIAL PRIMARY KEY,
    agent_id VARCHAR(255),
    timestamp TIMESTAMPTZ NOT NULL,
    cpu_percent FLOAT,
    memory_percent FLOAT,
    disk_percent FLOAT,
    network_bytes_sent BIGINT,
    network_bytes_recv BIGINT,
    process_count INTEGER,
    metrics JSONB,
    created_at TIMESTAMPTZ DEFAULT NOW()
);

memory_relationships - Context Threading

CREATE TABLE memory_relationships (
    relationship_id SERIAL PRIMARY KEY,
    parent_memory_id VARCHAR(64) REFERENCES memories(memory_id),
    child_memory_id VARCHAR(64) REFERENCES memories(memory_id),
    relationship_type VARCHAR(50),
    created_at TIMESTAMPTZ DEFAULT NOW()
);

Indexes & Performance

Vector Search Optimization

-- IVFFlat index for cosine similarity search
CREATE INDEX idx_embeddings_vector ON memory_embeddings USING ivfflat (embedding vector_cosine_ops);

-- Metadata indexes for filtering CREATE INDEX idx_memories_agent_timestamp ON memories(agent_id, timestamp DESC); CREATE INDEX idx_memories_type ON memories(memory_type); CREATE INDEX idx_memories_tags ON memories USING GIN(tags); CREATE INDEX idx_resource_metrics_agent_time ON resource_metrics(agent_id, timestamp DESC);


🚀 Installation & Setup

Auto-Installer (Linux Mint/Ubuntu)

# Run the auto-installer
sudo ./scripts/install_pgvectorscale.sh

Setup Python configuration

python scripts/setup_memory_db.py

Install Python dependencies

pip install psycopg2-binary pgvector sentence-transformers asyncpg

Manual Installation

1. Install PostgreSQL

# Ubuntu 22.04+
sudo apt-get install postgresql-14 postgresql-contrib-14

Linux Mint (Ubuntu-based)

sudo apt-get install postgresql postgresql-contrib

2. Install pgvector

# Download and compile pgvector
cd /tmp
git clone --branch v0.7.0 https://github.com/pgvector/pgvector.git
cd pgvector
make
sudo make install

Enable extension in database

sudo -u postgres psql -d mindx_memory -c "CREATE EXTENSION IF NOT EXISTS vector;"

3. Configure Database

# Create database and user
sudo -u postgres psql -c "CREATE USER mindx WITH PASSWORD 'mindx_password_2024_secure';"
sudo -u postgres psql -c "CREATE DATABASE mindx_memory OWNER mindx;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE mindx_memory TO mindx;"

🔧 Configuration

Environment Variables

# Database connection
export MINDX_DB_PASSWORD="mindx_password_2024_secure"

Optional: Custom database settings

export MINDX_DB_HOST="localhost" export MINDX_DB_PORT="5432" export MINDX_DB_NAME="mindx_memory" export MINDX_DB_USER="mindx"

mindx_config.json

{
  "memory": {
    "storage_backend": "postgresql",
    "postgresql": {
      "host": "localhost",
      "port": 5432,
      "database": "mindx_memory",
      "user": "mindx",
      "password_env": "MINDX_DB_PASSWORD",
      "pool_size": 10,
      "max_overflow": 20
    },
    "embeddings": {
      "model": "all-MiniLM-L6-v2",
      "dimensions": 384,
      "device": "cpu"
    },
    "vector_search": {
      "default_limit": 10,
      "min_similarity": 0.5,
      "index_type": "ivfflat"
    },
    "resource_integration": {
      "auto_store_metrics": true,
      "store_interval_seconds": 60,
      "correlation_window_days": 7
    }
  }
}

📊 API Integration

Memory Retrieval (RAGE System)

Semantic Search

POST /api/rage/memory/retrieve
Content-Type: application/json

{ "query": "agent performance optimization", "agent_id": "mindxagent", "top_k": 10, "min_similarity": 0.5 }

Response Format

{
  "success": true,
  "query": "agent performance optimization",
  "contexts": [
    {
      "content": "Performance optimization strategies...",
      "metadata": {
        "agent_id": "mindxagent",
        "memory_type": "learning",
        "similarity": 0.87
      },
      "doc_id": "memory_123"
    }
  ],
  "count": 5
}

Memory Storage

Store Memory

POST /api/rage/memory/store
Content-Type: application/json

{ "agent_id": "mindxagent", "memory_type": "interaction", "content": { "action": "code_optimization", "result": "success", "metrics": {"improvement": "25%"} }, "context": {"session_id": "abc123"}, "tags": ["optimization", "performance"] }


🧠 mindXagent Integration

Semantic Context Retrieval

# Get context from semantic memory
context_memories = await mindxagent._query_rage_memories(
    query="performance optimization strategies",
    top_k=10
)

Store memory with automatic embedding

memory_id = await mindxagent.store_memory_via_rage( memory_type="learning", content={"strategy": "parallel_processing", "improvement": "40%"}, tags=["optimization", "performance"] )

Self-Improvement Feedback

# Get memory feedback for context
memory_context = await mindxagent.get_memory_feedback(
    context="resource optimization patterns"
)

Learn from resource patterns

improvements = await mindxagent.learn_from_resource_patterns()

📈 Performance & Scaling

Benchmark Results

Query Performance

Embedding Generation

Optimization Strategies

Index Tuning

-- Adjust IVFFlat index parameters
ALTER TABLE memory_embeddings SET (ivfflat.probes = 10);

-- Rebuild index for better performance REINDEX INDEX idx_embeddings_vector;

Connection Pooling

# Async connection pool configuration
pool = psycopg2.pool.SimpleConnectionPool(
    minconn=5,
    maxconn=50,
    host="localhost",
    database="mindx_memory"
)

Embedding Caching

# Cache frequently used embeddings
embedding_cache = {}
if text not in embedding_cache:
    embedding_cache[text] = model.encode(text)

🔄 Migration Strategy

From File-Based Storage

Dry Run Migration

# Test migration without making changes
python scripts/migrate_memories_to_postgres.py --dry-run --verbose

Full Migration

# Perform actual migration
python scripts/migrate_memories_to_postgres.py

Dual-Write Mode

# Enable dual-write during transition
config.set("memory.dual_write_mode", True)

Gradually migrate components

1. New memories go to both systems

2. Validate PostgreSQL performance

3. Switch read operations to PostgreSQL

4. Disable file-based storage

Data Validation

Migration Verification

-- Count memories in both systems
SELECT COUNT() FROM memories;
SELECT COUNT() FROM memory_embeddings;

-- Validate embedding quality SELECT memory_id, vector_dims(embedding) as dimensions FROM memory_embeddings LIMIT 10;


🔍 Monitoring & Maintenance

Health Checks

Database Connectivity

async def check_database_health():
    try:
        conn = await pool.getconn()
        await conn.execute("SELECT 1")
        pool.putconn(conn)
        return True
    except Exception as e:
        logger.error(f"Database health check failed: {e}")
        return False

Vector Search Performance

-- Monitor query performance
EXPLAIN ANALYZE
SELECT memory_id, 1 - (embedding <=> '[0.1,0.2,...]') as similarity
FROM memory_embeddings
ORDER BY embedding <=> '[0.1,0.2,...]'
LIMIT 10;

Maintenance Tasks

Index Optimization

-- Regular VACUUM and ANALYZE
VACUUM ANALYZE memory_embeddings;

-- Rebuild IVFFlat index periodically REINDEX INDEX CONCURRENTLY idx_embeddings_vector;

Embedding Quality Monitoring

# Monitor embedding distribution
def analyze_embedding_quality():
    embeddings = get_all_embeddings()
    norms = [np.linalg.norm(emb) for emb in embeddings]

# Check for zero vectors or outliers zero_count = sum(1 for n in norms if n < 1e-6) avg_norm = np.mean(norms)

return { "zero_vectors": zero_count, "average_norm": avg_norm, "quality_score": "good" if zero_count == 0 else "needs_attention" }


🚨 Troubleshooting

Common Issues

Connection Pool Exhaustion

# Increase pool size
pool_config = {
    "minconn": 10,
    "maxconn": 50,
    "max_overflow": 30
}

Slow Query Performance

-- Check index usage
EXPLAIN SELECT * FROM memory_embeddings
ORDER BY embedding <=> '[0.1,0.2,...]' LIMIT 10;

-- Adjust ivfflat.probes ALTER TABLE memory_embeddings SET (ivfflat.probes = 20);

Embedding Generation Bottlenecks

# Use GPU acceleration
model = SentenceTransformer('all-MiniLM-L6-v2', device='cuda')

Implement batch processing

embeddings = model.encode(texts, batch_size=64, show_progress_bar=True)

🎯 Use Cases & Applications

Autonomous Self-Improvement

Enterprise Platform Intelligence

Economic Intelligence


📚 API Reference

MemoryAgent Methods

query_memories_semantic(query, kwargs)

Query memories using semantic similarity search.

save_memory_with_embedding(memory_data)

Save memory with automatic embedding generation.

get_resource_correlated_memories(pattern, kwargs)

Find memories correlated with specific resource conditions.

RAGE System Methods

retrieve_context(query, kwargs)

Retrieve context using semantic search.

store_memory(memory_data)

Store memory for semantic retrieval.

get_retrieval_stats()

Get system performance statistics.


🔗 Related Documentation


pgvectorscale Memory Integration enables context-aware autonomous intelligence with enterprise-grade performance and semantic understanding.


Referenced in this document
MINDXmindXagent

All DocumentsDocument IndexThe Book of mindXImprovement JournalAPI Reference