Managing Conversation History in a Database
In-memory state vanishes when servers reboot. In this lesson, we connect LangGraph to PostgreSQL and Redis checkpointers: implementing thread-partitioned sessions, immutable audit logs, crash recovery, and time-travel debugging.
1. Core Architecture of Database Checkpointers
Select a database mechanism to inspectPostgres Checkpointer Inspector
# 1. POSTGRES CHECKPOINTER SETUP
from psycopg_pool import ConnectionPool
from langgraph.checkpoint.postgres import PostgresSaver
from langgraph.graph import StateGraph
# Initialize enterprise connection pool
DB_URI = "postgresql://agent_admin:secret@pg-cluster.internal:5432/agents_db"
pool = ConnectionPool(conninfo=DB_URI, max_size=20)
# Instantiate checkpointer and migrate tables
checkpointer = PostgresSaver(pool)
checkpointer.setup() # Automatically creates 'checkpoints' and 'checkpoint_blobs'
# Compile agent with database persistence
app = graph_builder.compile(checkpointer=checkpointer)2. Interactive Database Checkpointer Studio
Multi-Tenant Thread InspectorDatabase Checkpointer & Thread Isolation Studio
Inspect how PostgresSaver chains immutable checkpoints and isolates multi-tenant threads
| Step | Checkpoint ID | Parent ID | Timestamp | Saved State Snapshot |
|---|---|---|---|---|
| #1 | chk_001 | NULL (Root) | 10:14:02 | User: Hi, I am Alice. |
| #2 | chk_002 | chk_001 | 10:14:04 | Assistant: Hello Alice! How can I assist you today? |
Common Engineering Traps
Creating a new database connection inside every agent invocation will quickly exhaust PostgreSQL's max_connections under traffic. Always instantiate a single shared ConnectionPool at application startup and pass it to the checkpointer.
Placing raw 20MB PDF byte arrays or images directly into the State dictionary means every node execution writes a new 20MB record into Postgres, swelling the database to hundreds of gigabytes within days. Save blobs to S3 and store only the S3 URL in State.
Key Architectural Takeaways
- 1.Thread Partitioning: Use thread_id to isolate multi-tenant user conversations cleanly in PostgreSQL or Redis.
- 2.Immutable Audit Trail: Checkpointers append snapshots rather than overwriting, giving you full compliance history and rollback capabilities.
- 3.Crash Resilience: Severed network connections or pod restarts can pick up right where they left off by querying the latest thread checkpoint.
Implementing Semantic Memory with Vector Stores
Short-term memory tracks the current chat, but long-term memory tracks user preferences across weeks and months. Learn how to build persistent episodic and semantic memory.