Appearance
๐ณ Docker & PostgreSQL Memory Cheat Sheet โ
A quick-reference guide for Docker container runtimes, Docker Compose multi-service networks, pgvector SQL queries, HNSW index creation, and relational-vector memory decay logic.
๐ณ Docker CLI Core Operations โ
๐ฆ Container Lifecycles & Audits โ
bash
### List all actively running containers
docker ps
### List all containers (including exited and stopped containers)
docker ps -a
### Run a resource-constrained background container (gVisor sandboxed)
docker run -d --name agent-sandbox --runtime=runsc -m 128m --cpus=0.5 python:3.11-slim python -c "print('Sandboxed!')"
### Stream real-time stdout logs from a container
docker logs -f agent-sandbox
### Inspect detailed JSON configuration metadata of a container
docker inspect agent-sandbox
### Forcefully terminate a running container
docker kill agent-sandbox
### Completely delete a stopped container
docker rm agent-sandbox๐งน System Prunes & Housekeeping โ
bash
### Delete all stopped containers, unused networks, and dangling build caches
docker system prune -f
### Completely purge all unused docker volumes (Warning: Deletes database storage!)
docker volume prune -f
### List disk space consumed by container layers, volumes, and images
docker system df๐ Docker Compose Orchestration โ
Use a single command block to manage your networked databases, gateways, and webhooks:
bash
### Spin up your n8n, pgvector PostgreSQL, and Redis stack in the background
docker compose up -d
### Spin up services and force container builds from updated Dockerfiles
docker compose up -d --build
### View active service statuses and mapped port allocations
docker compose ps
### Stream consolidated real-time logs across all services
docker compose logs -f
### Stream logs for a single target service
docker compose logs -f db
### Stop and completely delete all containers, networks, and volume mounts
docker compose down -v๐๏ธ PostgreSQL & pgvector Cheat Sheet โ
Connect to your database container from the host command line:
bash
docker exec -it pg_database psql -U postgres -d agent_memory๐ ๏ธ 1. Enabling Extension & Tables โ
sql
-- 1. Initialize the vector storage engine
CREATE EXTENSION IF NOT EXISTS vector;
-- 2. Create agent memory table with relational data + 1536-dimensional embeddings
CREATE TABLE IF NOT EXISTS agent_memories (
id SERIAL PRIMARY KEY,
user_id VARCHAR(50) NOT NULL,
content TEXT NOT NULL,
metadata JSONB,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
embedding vector(1536) -- dimension size matching Gemini/OpenAI
);๐งญ 2. Cosine, L2 & Inner Product Queries โ
Select the correct similarity operator based on your model embeddings:
| Operator | Metric | Best Use Case |
|---|---|---|
<=> | Cosine Distance | Text similarity (evaluates direction/angle; ignores length) |
<-> | L2 Euclidean Distance | Image search / normalized dense coordinates |
<#> | Negative Inner Product | Multi-class projection / dot-product comparisons |
sql
-- Query top 5 most semantically similar memories using Cosine Distance
SELECT id, content, (1 - (embedding <=> '[0.015, -0.024, ..., 0.089]')) AS similarity_score
FROM agent_memories
WHERE user_id = 'user_123'
ORDER BY embedding <=> '[0.015, -0.024, ..., 0.089]' ASC
LIMIT 5;๐ 3. Creating HNSW Graph Proximity Indices โ
Scanning millions of rows sequentially takes linear time ($O(N)$). Accelerate searches to logarithmic time ($O(\log N)$) using HNSW graphs:
sql
-- Construct an HNSW index on the embedding vector column
CREATE INDEX IF NOT EXISTS memories_hnsw_idx
ON agent_memories
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);m = 16: Number of bidirectional links per node in the proximity graph (higher = more accurate but consumes more RAM).ef_construction = 64: Size of dynamic candidate lists checked during index building (higher = more accurate index but slows indexing writes).
โณ 4. SQL Relational-Vector Memory Decay Math โ
Combine cosine semantic distance with an exponential decay formula ($e^{-\lambda t}$) to prioritize recent memories over older records:
sql
-- Recall memories using Cosine Similarity scaled by an exponential age decay
-- Half-life Lambda (0.0001) scales degradation over time differences
SELECT
id,
content,
(1 - (embedding <=> '[0.015, -0.024, ..., 0.089]')) *
EXP(-0.0001 * EXTRACT(EPOCH FROM (NOW() - created_at)) / 3600) AS decayed_memory_score
FROM agent_memories
ORDER BY decayed_memory_score DESC
LIMIT 5;