BUILD · 4 MIN READ

Use PostgreSQL and pgvector for agent memory

Keep explicit facts, metadata, and vector retrieval close to application data.

Separate the records

Store messages, checkpoints, user facts, and knowledge chunks in tables with explicit ownership and retention fields. PostgreSQL provides the relational store; pgvector adds vector types and similarity operators. The application still decides what becomes a memory.

A tiny SQL example

This example uses three-dimensional toy vectors to illustrate the query shape. A real embedding column must match the dimensions of your embedding model. The pgvector extension must already be installed on your PostgreSQL server.

SQL · demonstration schema
CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE agent_memories (
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    tenant_id text NOT NULL,
    content text NOT NULL,
    embedding vector(3) NOT NULL,
    created_at timestamptz NOT NULL DEFAULT now()
);

INSERT INTO agent_memories (tenant_id, content, embedding)
VALUES ('demo', 'Prefers concise answers', '[0.2,0.8,0.1]');

SELECT content, embedding <=> '[0.1,0.9,0.2]' AS distance
FROM agent_memories
WHERE tenant_id = 'demo'
ORDER BY distance
LIMIT 3;

Educational example. Uses your configured environment and credentials; no paid model call is made by this website.

Enforce the real boundary

The sample filter illustrates scoping. In an application, use parameterized queries and derive the tenant identifier from authenticated context. Apply database access controls or row-level security with properly configured roles. Evaluate retrieval recall with your actual filtering and index settings.

Keep exploring

A layered architecture for agent memoryAdd a dedicated memory layer with Mem0