TOOL WIKI · MEMORY & STATE

PostgreSQL

Stores application records, explicit memories, and durable state in relational tables.

Download example files Code + setup + checks in a ZIP
Jump to a section 10 sections

EXAMPLE USE CASE

Store an owned, versioned preference with expiry

A returning user’s preference or an unfinished run needs an explicit owner, storage lifetime and recall rule.

Input
Tenant a, user u1, key format, value bullets and a retention deadline.
Expected output
A scoped preference row whose version increments on an update.
  • Success check: The preference is bullets; the other-tenant count is zero. Rerunning increments the record version.
  • Try a failure: An expired row or other-tenant query must not become an active preference.

Example scope: Actual relational SQL; checkpoint and preference lifecycles remain separate.

Official reference · PostgreSQL

WHAT IT ISRelational database
WHERE IT FITSMemory & state
SEE HOW THE SYSTEM FITS TOGETHERSELECT A COMPONENT
candidateretainqueryrecordscontextmaintainInteractionSubject + evidenceWrite policySelect and verifyPostgreSQLRelational databaseRecall requestAuthenticated scopeContext builderRelevant selected factsLifecycle controlsCorrect, expire, delete
  1. Separate working conversation state from candidate long-term facts.

  2. Retain only permitted information and preserve where it came from.

  3. Owning transactional records and access-scoped memory in one database. The application must define what is retained and how it is retrieved.

  4. Use verified tenant and subject identifiers when retrieving memory.

  5. Choose useful records within the current context budget.

  6. Apply corrections and deletion to records, derived indexes, and caches.

PostgreSQL

Owning transactional records and access-scoped memory in one database. The application must define what is retained and how it is retrieved.

Follow the arrows, then select a component to inspect its responsibility. On mobile, downward arrows connect the next step; bent arrows identify branches and return paths. Select a destination to jump to that component.

When to consider it

  • Owning transactional records and access-scoped memory in one database.

A design decision to make

  • The application must define what is retained and how it is retrieved.

Prerequisites and setup

  • Docker and a new local database. This lab stores explicit preference records, without an embedding model.
Shell
docker run --name asg-pg-state -d -p 127.0.0.1:5433:5432 \
  -e POSTGRES_PASSWORD=local-demo-only postgres:17

Try a small integration

state.sql

SQL · state.sql
CREATE TABLE IF NOT EXISTS agent_preference (
  tenant text NOT NULL, user_id text NOT NULL, key text NOT NULL,
  value jsonb NOT NULL, version integer NOT NULL DEFAULT 1,
  expires_at timestamptz NOT NULL,
  PRIMARY KEY (tenant,user_id,key)
);
INSERT INTO agent_preference VALUES ('a','u1','format','"bullets"',1,now()+interval '1 day')
ON CONFLICT (tenant,user_id,key) DO UPDATE
SET value=excluded.value, version=agent_preference.version+1, expires_at=excluded.expires_at;
SELECT key,value,version FROM agent_preference
WHERE tenant='a' AND user_id='u1' AND expires_at>now();
SELECT count(*) AS other_tenant_count FROM agent_preference WHERE tenant='b' AND user_id='u1';

Run and check the result

Shell
docker exec -i asg-pg-state psql -U postgres -v ON_ERROR_STOP=1 < state.sql
  • The preference is bullets; the other-tenant count is zero. Rerunning increments the record version.
  • Checkpoint and memory tables should be separate even when they share one database.

Common failures

  • Do not connect a real application as the database owner. The demo uses postgres only to keep the local schema exercise short.
  • Lost updates need optimistic version checks or row locking, not repeated blind upserts.

Production decisions

  • Define allowed memory types, evidence/provenance, correction, expiry and user deletion before accepting automatic writes.
  • Derive user and tenant scope from authenticated application state. Test cross-user recall, expired facts and deletion from every derived index.
  • Use parameterized queries, restricted roles and tested row-level policies. Back up authoritative records and verify restore procedures.
Read the PostgreSQL walkthrough

Keep exploring

A layered architecture for agent memoryA context pipeline for a long-running agent

Explore the same layer

Mem0LettaZepGraphiti

Compare candidates on your own workload. This entry describes a product’s role; it is not a claim that products in the category are interchangeable.