pgbrain is a PostgreSQL extension designed to provide intelligent, biologically-inspired memory management for AI agents. Traditional vector stores treat all memories as equally important, leading to information overload and difficulty distinguishing between critical facts and ephemeral observations.
pgbrain addresses this by implementing two fundamental cognitive mechanisms:
- Semantic Decay: Memories naturally lose importance over time, ensuring that recent, relevant information takes precedence while preserving genuinely critical long-term facts
- Memory Consolidation: Similar memories are automatically merged into summarized "core" facts, reducing redundancy and improving retrieval efficiency
This approach allows AI agents to maintain large-scale memory systems without overwhelming context windows, automatically prioritizing information based on recency, relevance, and importance.
Status: 🚧 Work in Progress
This extension is under active development. Some features are experimental. Production use is not yet recommended.
Modern AI systems struggle with memory management:
- Information Overload: Unlimited memory growth leads to poor retrieval performance
- Flat Storage: All memories are treated equally, regardless of importance
- No Forgetting: Older, less relevant information clutters retrieval results
- Redundancy: Similar memories duplicate storage without synthesis
pgbrain solves these problems by combining vector similarity with biologically-inspired memory dynamics, creating a memory layer that automatically optimizes itself over time.
- Semantic Decay: Configurable half-life (default: 30 days) with exponential decay formula
- Memory Consolidation: Automatic merging of similar memories above configurable threshold (default: 0.85)
- Intelligent Weighting: Combined scoring of recency, relevance, and importance
- Vector Embedding Support: Optional integration with pgvector for semantic similarity
- Metadata Queries: JSONB metadata for flexible categorization and filtering
# Clone and build
git clone <repository-url>
cd pgbrain
make clean && make
sudo make install
# Enable in PostgreSQL
psql -c "CREATE EXTENSION pgbrain;"-- Create memories with importance scores
SELECT pg_brain_create_memory(
'User prefers dark mode interfaces for reduced eye strain',
0.7,
'{"source": "conversation", "category": "preferences"}'::jsonb
);
SELECT pg_brain_create_memory(
'User is a PostgreSQL developer building AI applications',
0.95,
'{"source": "profile", "verified": true}'::jsonb
);
-- Search memories with automatic weighted ranking
SELECT id, content, importance, normalized_weight
FROM pg_brain_search_memories('PostgreSQL development', 10);
-- Boost importance when a memory is referenced
SELECT pg_brain_boost_importance(1, 0.15);-- Adjust decay parameters
SET pg_brain.decay_half_life_days = 60;
SET pg_brain.decay_min_importance = 0.05;
-- Configure consolidation thresholds
SET pg_brain.consolidation_similarity = 0.90;
SET pg_brain.enable_auto_consolidation = false;
-- Manually trigger consolidation
SELECT pg_brain_consolidate_memories();The MEMORY index combines three factors in a biologically-inspired weighting:
Weight = (recency × relevance × importance)^(1/3)
Where:
- recency = 2^(-age_days / half_life_days)
- relevance = 1 - (vector_distance / 2) or text similarity
- importance = stored importance score (0-1)
This ensures that:
- Recent memories are prioritized over older ones
- Semantically relevant memories surface in searches
- High-importance facts persist through decay
- Redundant memories are consolidated over time
pgbrain follows OWASP ASVS Level 2+ security standards:
- ✅ Input validation with parameterized queries
- ✅ No hardcoded credentials or secrets
- ✅ SQL injection mitigation through SPI
- ✅ Principle of least privilege by default
- ✅ OWASP Top 10 compliance
pgbrain is released under the PostgreSQL License, same as PostgreSQL itself.
Created by Chris Bunting cbuntingde@gmail.com