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

Example

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"
);

// 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 full)

Constructors

Properties

isSetup: boolean
pool: any

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

    Returns any

  • 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 alist() given config, filter, cursor.

    This method returns a tuple of a string and a tuple of values. The string is the parametered 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

    • Optional config: RunnableConfig<Record<string, any>>
    • Optional filter: Record<string, unknown>
    • Optional before: RunnableConfig<Record<string, any>>

    Returns [string, unknown[]]

  • Returns Promise<void>

  • Parameters

    • config: RunnableConfig<Record<string, any>>

    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

    • current: undefined | number
    • _channel: ChannelProtocol<unknown, unknown, unknown>

    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<Record<string, any>>

      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, any, unknown>

  • 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<Record<string, any>>

      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>

  • Parameters

    • connString: string

    Returns PostgresSaver