Skip to main content

Postgres Adapter

For team sharing and production deployments. Uses PostgreSQL with database-level triggers for outcome propagation, LISTEN/NOTIFY for real-time watch, native full-text search via tsvector/GIN, and vector similarity search via pgvector.

Installation

Requires PostgreSQL 14+ with psycopg3. For vector search, install the pgvector extension (included in the pgvector/pgvector Docker image).

Configuration

YAML

Environment Variable

Programmatic


Schema

The adapter auto-creates three tables and associated triggers:

amfs_memory_entries

amfs_outcomes

amfs_knowledge_graph

A unique constraint on (namespace, branch, source_entity, relation, target_entity) ensures idempotent upserts — repeated writes increment evidence_count and update last_seen rather than creating duplicates. The knowledge graph is populated automatically by the SDK’s materializers: write() with pattern_refs, commit_outcome(), and read_from() all create edges without extra code. See API Reference for the full model.

Graph Methods


How It Works

Write

  1. SELECT ... FOR UPDATE locks the current version row
  2. UPDATE sets superseded_at on the old row
  3. INSERT creates the new version

Outcome Propagation

An AFTER INSERT trigger on amfs_outcomes automatically:
  1. Reads the causal entry keys
  2. Supersedes the current version of each
  3. Inserts a new version with updated confidence (old × multiplier)

Watch (LISTEN/NOTIFY)

An AFTER INSERT trigger on amfs_memory_entries calls pg_notify('amfs_write', ...). The adapter listens on this channel and dispatches to your callbacks.

Docker Quick Start


The adapter automatically maintains a search_tsv column (GIN-indexed) that combines the key, entity_path, and value fields. When SearchQuery.query is set, the adapter uses plainto_tsquery with the @@ operator for efficient in-database filtering. When sort_by is "confidence" and a query is present, results are ordered by ts_rank(search_tsv, ...) first, then by confidence — so textually relevant results float to the top.

Vector Similarity Search (pgvector)

When an embedder is configured, the adapter stores vector embeddings in a VECTOR(384) column with an HNSW index. The semantic_search() method uses cosine similarity directly in SQL via pgvector’s <=> operator. To use pgvector, install the extension in your database:
The pgvector/pgvector:pg16 Docker image ships with pgvector pre-installed. The docker-compose.yml in the repo uses this image.

Tier Indexes

Two partial indexes accelerate progressive retrieval queries:
  • idx_entries_hotWHERE tier = 1 AND superseded_at IS NULL
  • idx_entries_warmWHERE tier <= 2 AND superseded_at IS NULL
When depth=1, the query uses the hot index; depth=2 uses the warm index.

Connection Pooling

The adapter uses psycopg_pool.ConnectionPool for efficient connection management. The pool size is configurable and defaults to sensible limits for most deployments.

When to Use

  • Team environments (multiple developers/agents sharing memory)
  • Production deployments with full-text and vector search
  • When you need database-level consistency guarantees
  • When you want memory to survive machine restarts
  • When you need efficient search across large memory stores

Next Steps