PostgresSaver is a checkpoint saver that stores checkpoints in a PostgreSQL database. It provides a robust, production-ready persistence solution for LangGraph agents with support for high concurrency and advanced features.
Overview
PostgresSaver is designed for:
- Production workloads
- High concurrency applications
- Multi-threaded environments
- Distributed systems
- Applications requiring advanced querying capabilities
Class Definition
Source: langgraph.checkpoint.postgres.__init__:32
Installation
Install the PostgreSQL checkpoint package:
This package requires psycopg (version 3+) and psycopg-pool for connection pooling.
Constructor
Parameters
conn (Connection | ConnectionPool): PostgreSQL connection or connection pool
pipe (Pipeline | None): Optional psycopg Pipeline for batching operations
serde (SerializerProtocol | None): Serializer for encoding/decoding checkpoints. Defaults to JsonPlusSerializer
Pipeline should only be used with a single Connection, not ConnectionPool.
Source: langgraph.checkpoint.postgres.__init__:37
Usage
Basic Setup
Using with Pipeline
Pipeline mode enables batching of database operations for better performance:
Source: langgraph.checkpoint.postgres.__init__:54
Using Connection Pool
Class Methods
from_conn_string
Create a new PostgresSaver instance from a connection string.
Parameters:
conn_string (str): PostgreSQL connection string (e.g., postgres://user:pass@host:port/db)
pipeline (bool): Whether to use Pipeline for batching operations (default: False)
Returns:
Iterator[PostgresSaver]: A context manager yielding a PostgresSaver instance
Example:
Source: langgraph.checkpoint.postgres.__init__:54
Instance Methods
setup
Set up the checkpoint database. Creates necessary tables and runs migrations.
Important: This method MUST be called directly by the user the first time the checkpointer is used.
Example:
Source: langgraph.checkpoint.postgres.__init__:77
get_tuple
Get a checkpoint tuple from the database.
Parameters:
config (RunnableConfig): Configuration containing thread_id and optionally checkpoint_id
Returns:
CheckpointTuple | None: The checkpoint tuple, or None if not found
Example:
Source: langgraph.checkpoint.postgres.__init__:184
list
List checkpoints from the database.
Parameters:
config (RunnableConfig | None): Base configuration for filtering
filter (dict[str, Any] | None): Additional metadata filtering criteria
before (RunnableConfig | None): Only return checkpoints before this checkpoint ID
limit (int | None): Maximum number of checkpoints to return
Returns:
Iterator[CheckpointTuple]: Iterator of checkpoint tuples, ordered by checkpoint ID (newest first)
Example:
Source: langgraph.checkpoint.postgres.__init__:104
put
Save a checkpoint to the database.
Parameters:
config (RunnableConfig): Configuration for the checkpoint
checkpoint (Checkpoint): The checkpoint to save
metadata (CheckpointMetadata): Additional metadata
new_versions (ChannelVersions): New channel versions
Returns:
RunnableConfig: Updated configuration with the new checkpoint ID
Example:
Source: langgraph.checkpoint.postgres.__init__:255
put_writes
Store intermediate writes linked to a checkpoint.
Parameters:
config (RunnableConfig): Configuration of the related checkpoint
writes (Sequence[tuple[str, Any]]): List of (channel, value) pairs to store
task_id (str): Identifier for the task creating the writes
task_path (str): Path of the task (default: "")
Source: langgraph.checkpoint.postgres.__init__:336
delete_thread
Delete all checkpoints and writes associated with a thread ID.
Parameters:
thread_id (str): The thread ID to delete
Example:
Source: langgraph.checkpoint.postgres.__init__:370
Database Schema
PostgresSaver creates three tables:
checkpoints table
checkpoint_blobs table
checkpoint_writes table
Indices are automatically created on thread_id columns for performance.
AsyncPostgresSaver
For async applications, use AsyncPostgresSaver:
Source: langgraph.checkpoint.postgres.aio:32
Advanced Features
Connection Pooling
Use ConnectionPool for better resource management:
Pipeline Mode
Pipeline mode batches database operations for improved performance:
JSONB Storage
PostgresSaver stores checkpoints as JSONB, enabling:
- Efficient querying of checkpoint data
- Native JSON operators in SQL queries
- Indexing on specific JSON fields
- Smaller storage footprint for structured data
Blob Storage
Large channel values are stored separately in checkpoint_blobs table for:
- Optimized storage of binary data
- Reduced checkpoint table size
- Better query performance
- Use connection pooling for multi-threaded applications
- Enable pipeline mode when available for batching operations
- Set appropriate pool sizes based on your concurrency requirements
- Use indices on frequently queried metadata fields
- Regular VACUUM operations to maintain performance
ShallowPostgresSaver
For specialized use cases requiring minimal checkpoint storage:
ShallowPostgresSaver stores only essential checkpoint data, reducing storage requirements.
Migrations
The checkpointer automatically runs database migrations on setup(). The migration system:
- Tracks applied migrations in
checkpoint_migrations table
- Applies new migrations incrementally
- Uses
CONCURRENTLY for index creation to avoid locking
- Supports version-based migration ordering
See Also