LangGraph checkpointer that uses a Postgres instance as the backing store. Uses the node-postgres package internally to connect to a Postgres instance.

import { ChatOpenAI } from "@langchain/openai";
import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres";
import { createReactAgent } from "@langchain/langgraph/prebuilt";

const checkpointer = PostgresSaver.fromConnString(
"postgresql://user:password@localhost:5432/db",
// optional configuration object
{
schema: "custom_schema" // defaults to "public"
}
);

// NOTE: you need to call .setup() the first time you're using your checkpointer
await checkpointer.setup();

const graph = createReactAgent({
tools: [getWeather],
llm: new ChatOpenAI({
model: "gpt-4o-mini",
}),
checkpointSaver: checkpointer,
});
const config = { configurable: { thread_id: "1" } };

await graph.invoke({
messages: [{
role: "user",
content: "what's the weather in sf"
}],
}, config);

Hierarchy (View Summary)

Constructors

Properties

isSetup: boolean

Methods

  • Parameters

    • threadId: string
    • checkpointNs: string
    • values: Record<string, unknown>
    • versions: ChannelVersions

    Returns [string, string, string, string, string, undefined | Uint8Array][]

  • Parameters

    Returns Record<string, unknown>

  • Parameters

    • threadId: string
    • checkpointNs: string
    • checkpointId: string
    • taskId: string
    • writes: [string, unknown][]

    Returns [string, string, string, string, number, string, string, Uint8Array][]

  • Parameters

    • blobValues: [Uint8Array, Uint8Array, Uint8Array][]

    Returns Promise<Record<string, unknown>>

  • Parameters

    • checkpoint: Omit<Checkpoint<string, string>, "channel_values" | "pending_sends">
    • channelValues: [Uint8Array, Uint8Array, Uint8Array][]
    • pendingSends: [Uint8Array, Uint8Array][]

    Returns Promise<Checkpoint<string, string>>

  • Parameters

    • metadata: Record<string, unknown>

    Returns Promise<any>

  • Parameters

    • writes: [Uint8Array, Uint8Array, Uint8Array, Uint8Array][]

    Returns Promise<[string, string, unknown][]>

  • Return WHERE clause predicates for a given list() config, filter, cursor.

    This method returns a tuple of a string and a tuple of values. The string is the parameterized WHERE clause predicate (including the WHERE keyword): "WHERE column1 = $1 AND column2 IS $2". The list of values contains the values for each of the corresponding parameters.

    Parameters

    • Optionalconfig: RunnableConfig<Record<string, any>>
    • Optionalfilter: Record<string, unknown>
    • Optionalbefore: RunnableConfig<Record<string, any>>

    Returns [string, unknown[]]

  • Returns Promise<void>

  • Parameters

    • config: RunnableConfig

    Returns Promise<undefined | Checkpoint<string, string>>

  • Generate the next version ID for a channel.

    Default is to use integer versions, incrementing by 1. If you override, you can use str/int/float versions, as long as they are monotonically increasing.

    Parameters

    Returns number

  • Get a checkpoint tuple from the database. This method retrieves a checkpoint tuple from the Postgres database based on the provided config. If the config's configurable field contains a "checkpoint_id" key, the checkpoint with the matching thread_id and namespace is retrieved. Otherwise, the latest checkpoint for the given thread_id is retrieved.

    Parameters

    • config: RunnableConfig

      The config to use for retrieving the checkpoint.

    Returns Promise<undefined | CheckpointTuple>

    The retrieved checkpoint tuple, or undefined.

  • List checkpoints from the database.

    This method retrieves a list of checkpoint tuples from the Postgres database based on the provided config. The checkpoints are ordered by checkpoint ID in descending order (newest first).

    Parameters

    Returns AsyncGenerator<CheckpointTuple>

  • Save a checkpoint to the database.

    This method saves a checkpoint to the Postgres database. The checkpoint is associated with the provided config and its parent config (if any).

    Parameters

    Returns Promise<RunnableConfig<Record<string, any>>>

  • Store intermediate writes linked to a checkpoint.

    This method saves intermediate writes associated with a checkpoint to the Postgres database.

    Parameters

    • config: RunnableConfig

      Configuration of the related checkpoint.

    • writes: PendingWrite<string>[]

      List of writes to store.

    • taskId: string

      Identifier for the task creating the writes.

    Returns Promise<void>

  • Set up the checkpoint database asynchronously.

    This method creates the necessary tables in the Postgres database if they don't already exist and runs database migrations. It MUST be called directly by the user the first time checkpointer is used.

    Returns Promise<void>

  • Creates a new instance of PostgresSaver from a connection string.

    Parameters

    • connString: string

      The connection string to connect to the Postgres database.

    • Optionaloptions: Partial<PostgresSaverOptions>

      Optional configuration object.

    Returns PostgresSaver

    A new instance of PostgresSaver.

    const connString = "postgresql://user:password@localhost:5432/db";
    const checkpointer = PostgresSaver.fromConnString(connString, {
    schema: "custom_schema" // defaults to "public"
    });
    await checkpointer.setup();