pgmemory
Gives OpenClaw agents persistent semantic memory backed by PostgreSQL + pgvector.
Agents wake up fresh every session. pgmemory fixes that — decisions, constraints,
infrastructure facts, and discoveries persist across sessions and surface automatically
when relevant.
Setup
Run once after installing:
CODEBLOCK0
The wizard handles everything: Docker/PostgreSQL, migrations, embedding provider,
AGENTS.md scaffolding, and decay cron.
When to use this skill
Read this SKILL.md when:
- - Setting up pgmemory for the first time
- Adding a new OpenClaw agent and want it to have persistent memory
- Diagnosing memory issues (run
--doctor) - Switching embedding providers
- Understanding how memory decay or archiving works
Core commands
Write a memory
CODEBLOCK1
Categories: decision · constraint · infrastructure · vision · preference · context · INLINECODE7
Importance:
- -
3 = critical — decisions, constraints, infrastructure. Never expires. Always loaded. - INLINECODE9 = important — context, preferences. Expires after 180 days if unused.
- INLINECODE10 = transient — low-value notes. Expires after 30 days.
Search memories
CODEBLOCK2
Maintenance
CODEBLOCK3
What to write to memory
Write immediately when:
- - You make a decision about architecture, tooling, or approach
- You discover a constraint — something that will bite you if forgotten
- You complete infrastructure work (migrations, deployments, config changes)
- You identify a preference or vision that should guide future work
- A sub-agent completes — harvest its important findings
Skip writing for:
- - Casual conversation
- Things already in MEMORY.md or other workspace files
- Anything you'd classify as importance 1 unless it's genuinely useful
Multi-agent setup
Each OpenClaw agent gets its own namespace (= agent ID). Run --sync-agents after
adding a new agent to scaffold pgmemory automatically:
CODEBLOCK4
Or add --sync-agents to HEARTBEAT.md for automatic pickup within 30 minutes.
Harvest from sub-agents
After a sub-agent completes, pull its important findings into the primary namespace:
CODEBLOCK5
Memory decay
Memories decay based on age and category. Frequently accessed memories stay fresh.
Decayed memories move to archive (never deleted). Restored automatically if matched
in a future search.
Decay runs daily via cron (configured during setup). Run manually anytime:
CODEBLOCK6
Switching embedding providers
Changing providers after setup requires re-embedding all memories — you cannot mix
dimensions in the same database. Run --doctor first to check for mismatches.
⚠️ Provider migration (--re-embed) is planned for v1.1. For now: set up a fresh
database if you need to switch providers.
Config reference
Minimal (all that's required):
CODEBLOCK7
Default config is at ~/.openclaw/pgmemory.json. Override with --config <path>.
Full config reference: see references/schema.sql and CHANGELOG.md.
Requirements
- - Python 3.9+
- PostgreSQL 14+ with pgvector 0.5+
- INLINECODE19 ,
numpy — install via INLINECODE21 - Embedding provider API key (or Ollama for local)
pgmemory
为OpenClaw智能体提供由PostgreSQL + pgvector支持的持久化语义记忆。
智能体每次会话都会重新启动。pgmemory解决了这个问题——决策、约束、基础设施事实和发现会在会话间持久化,并在相关时自动呈现。
设置
安装后运行一次:
bash
python3 ~/.openclaw/skills/pgmemory/scripts/setup.py
向导会处理所有事项:Docker/PostgreSQL、迁移、嵌入提供者、AGENTS.md脚手架以及衰减定时任务。
何时使用此技能
在以下情况下阅读此SKILL.md:
- - 首次设置pgmemory时
- 添加新的OpenClaw智能体并希望其拥有持久化记忆时
- 诊断记忆问题时(运行--doctor)
- 切换嵌入提供者时
- 了解记忆衰减或归档机制时
核心命令
写入记忆
bash
python3 ~/.openclaw/skills/pgmemory/scripts/write_memory.py \
--key unique.descriptive.key \
--content 要记住的内容 \
--category decision \
--importance 3
类别: decision · constraint · infrastructure · vision · preference · context · task
重要性:
- - 3 = 关键——决策、约束、基础设施。永不过期。始终加载。
- 2 = 重要——上下文、偏好。180天未使用则过期。
- 1 = 临时——低价值笔记。30天后过期。
搜索记忆
bash
语义搜索
python3 ~/.openclaw/skills/pgmemory/scripts/query_memory.py 数据库连接
加载所有关键记忆(重要性3)
python3 ~/.openclaw/skills/pgmemory/scripts/query_memory.py --importance 3 --limit 20
统计信息
python3 ~/.openclaw/skills/pgmemory/scripts/query_memory.py --stats
列出所有键
python3 ~/.openclaw/skills/pgmemory/scripts/query_memory.py --list
维护
bash
全面健康检查
python3 ~/.openclaw/skills/pgmemory/scripts/setup.py --doctor
验证配置
python3 ~/.openclaw/skills/pgmemory/scripts/setup.py --validate
运行待处理的迁移
python3 ~/.openclaw/skills/pgmemory/scripts/setup.py --migrate
将pgmemory同步到所有OpenClaw智能体工作区
python3 ~/.openclaw/skills/pgmemory/scripts/setup.py --sync-agents
手动运行衰减周期
python3 ~/.openclaw/skills/pgmemory/scripts/setup.py --decay
写入记忆的内容
在以下情况立即写入:
- - 你对架构、工具或方法做出决策时
- 你发现一个约束——忘记它就会带来麻烦的事情
- 你完成基础设施工作(迁移、部署、配置变更)
- 你确定应指导未来工作的偏好或愿景
- 子智能体完成任务——收集其重要发现
跳过写入:
- - 随意对话
- 已在MEMORY.md或其他工作区文件中的内容
- 任何你归类为重要性1的内容,除非确实有用
多智能体设置
每个OpenClaw智能体拥有自己的命名空间(= 智能体ID)。添加新智能体后运行--sync-agents以自动搭建pgmemory:
bash
openclaw agents add code-writer
python3 ~/.openclaw/skills/pgmemory/scripts/setup.py --sync-agents
或者将--sync-agents添加到HEARTBEAT.md中,以便在30分钟内自动获取。
从子智能体收集信息
子智能体完成任务后,将其重要发现拉取到主命名空间:
bash
python3 ~/.openclaw/skills/pgmemory/scripts/query_memory.py \
--harvest shopwalk:subagent:task-label
记忆衰减
记忆根据年龄和类别衰减。频繁访问的记忆保持新鲜。衰减的记忆移至归档(从不删除)。如果在未来搜索中匹配,会自动恢复。
衰减通过定时任务每天运行(在设置时配置)。随时手动运行:
bash
python3 ~/.openclaw/skills/pgmemory/scripts/setup.py --decay
切换嵌入提供者
设置后更改提供者需要重新嵌入所有记忆——你不能在同一数据库中混合不同维度。先运行--doctor检查不匹配情况。
⚠️ 提供者迁移(--re-embed)计划在v1.1版本中实现。目前:如果需要切换提供者,请设置一个新的数据库。
配置参考
最小配置(所有必需项):
json
{
db: { uri: postgresql://openclaw@localhost:5432/openclaw },
embeddings: { provider: voyage, apikeyenv: VOYAGEAPIKEY },
agent: { name: main }
}
默认配置位于~/.openclaw/pgmemory.json。使用--config <路径>覆盖。
完整配置参考:参见references/schema.sql和CHANGELOG.md。
要求
- - Python 3.9+
- PostgreSQL 14+ 带 pgvector 0.5+
- psycopg2-binary、numpy——通过pip install -r requirements.txt安装
- 嵌入提供者API密钥(或本地使用Ollama)