TOOL WIKI · MEMORY & STATE
PostgreSQL
Stores application records, explicit memories, and durable state in relational tables.
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
Separate working conversation state from candidate long-term facts.
candidateTo 02 · Write policyRetain only permitted information and preserve where it came from.
retainTo 03 · PostgreSQLOwning transactional records and access-scoped memory in one database. The application must define what is retained and how it is retrieved.
recordsTo 05 · Context builderUse verified tenant and subject identifiers when retrieving memory.
Choose useful records within the current context budget.
contextBack to 01 · InteractionApply corrections and deletion to records, derived indexes, and caches.
maintainBack to 03 · PostgreSQL
Owning transactional records and access-scoped memory in one database. The application must define what is retained and how it is retrieved.
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.
docker run --name asg-pg-state -d -p 127.0.0.1:5433:5432 \
-e POSTGRES_PASSWORD=local-demo-only postgres:17Try a small integration
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
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.
Keep exploring
Explore the same layer
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.