# PostgreSQL checkpoint operations Spring AI Alibaba `2.1.0.1` introduces the checkpoint bridge and key-level storage options. See the [upgrade guide by saver](https://java2ai.com/docs/frameworks/graph-core/core/checkpoint-rolling-upgrade) for rollout, cutover, and rollback. Configure a `PostgresSaver` and a `PostgresCheckpointBlobStore` with the same `DataSource`. Keep the namespace stable across restarts and use `CompileConfig.Builder.externalizeCheckpointKeys(...)` to select top-level state keys. Nodes continue to use the normal state APIs. Reading checkpoint metadata, enumerating keys, and resuming without accessing an external key leave its body in PostgreSQL. When an external key is removed from the writer's selection, unchanged saved references remain lazy. Keep the blob store and its namespace available while any retained checkpoint references its bodies. ## Rolling upgrade 1. Initialize the checkpoint schema with the upgraded saver, before starting upgraded application writers. If you provision schemas separately, run the upgrade as a role with DDL privileges and use `CREATE_NONE` for application instances after provisioning. The upgrade assigns sequences in the published `saved_at` order while holding a checkpoint table lock. 2. An initialized schema from the released `2.1.0.0` stays in compatibility mode. Upgraded instances write the published bare-state format during the rollout. Defer offloading and runtime metadata features such as task checkpoints until cutover, since checkpoints that require these fields fail in legacy mode. Ordinary compiled subgraphs can run in this phase. Their checkpoint payloads stay in the published bare-state format. 3. Upgrade or drain every old reader and writer that uses the schema. Call `saver.activateCheckpointV1Writes()` once. The database drains in-flight writes, switches all upgraded instances to `checkpoint-v1`, and rejects subsequent writes from legacy writers. Complete reader rollout before activation. Upgraded readers can restore earlier bare checkpoints. 4. Enable offloading and other features that require `checkpoint-v1`. For a new installation running only the upgraded application, the default writer already emits `checkpoint-v1`. Calling activation in that installation adds the database fence against legacy writers. Upgraded PostgreSQL graph instances share a per-thread execution lease even when their checkpoint feature flags differ. An overlapping execution on the same thread fails before it loads state or runs nodes. Retry it after the active execution completes. During the legacy-write phase, route each thread to one writer generation because published binaries do not acquire this lease. ## Collection and retention Start `blobStore.scheduleGarbageCollection(interval, grace, batchSize)` from the application's lifecycle and dispose the returned handle on shutdown. Each job waits for its current pass to finish before scheduling the next one. Multiple instances using the same namespace can run jobs safely. A failed pass retries at the next interval. For externally managed schedules, call `blobStore.collectGarbage(grace, batchSize)` directly. Collection starts in observation mode. Observe candidate count, candidate bytes, and oldest candidate age through the `spring.ai.alibaba.checkpoint.blob.gc.*` metrics. The `spring.ai.alibaba.checkpoint.blob.gc` timer tags successful and failed passes by result. After observing collection, enable deletion with `blobStore.setDeletionEnabled(true)`. Call the same method with `false` to disable further physical deletion. Checkpoint references and active read leases protect bodies through the grace period. Lazy snapshots are backed by finite read leases, so refresh an old snapshot if its lease has expired. Configure active history limits with `RunnableConfig.Builder.checkpointsNumRetained(...)` if your application has a retention limit. Archives are kept by default. If the schema has an explicit archive policy, invoke `saver.pruneArchivedThreads(retention, batchSize)` on a schedule controlled by that policy. Each call removes up to the batch size of archived threads with a recorded archive timestamp older than the retention period, across the schema. Existing archives with an unknown archive time are retained. Blob collection separately handles bodies made eligible by pruning, and live read leases still protect any snapshot already returned to a reader. The `Tag` returned by `PostgresSaver.release(...)` lists the checkpoints archived by that transaction. Paused subgraphs written in `checkpoint-v1` record the exact child checkpoint. For PostgreSQL parent and child savers sharing a `DataSource`, a retained parent protects that child checkpoint through history pruning and archive retention. A PostgreSQL fork of paused subgraphs requires the same `DataSource` for the parent and its children. Their new checkpoints, links, and blob references commit in one transaction. Use `CompiledGraph.lastStateOf(...)` for the active head. For PostgreSQL history, use `CompiledGraph.getStateHistoryPage(config, null, pageSize)` to start and keep passing `nextBeforeSequence` with the same thread until the cursor is null. ## Append delta pilot For selected external keys using `AppendStrategy`, enable `CompileConfig.Builder.checkpointAppendDeltaKeys(...)` to try storing appended string-list elements as separate bodies. A checkpoint registers its base and all tail bodies inside the existing `checkpoint-v1` envelope. The pilot writes a full snapshot when the next append would exceed eight tail segments or when a value ceases to extend its saved prefix. Readers see a list through the normal state APIs, with the merge behavior defined by `AppendStrategy`. Upgraded applications can read pilot checkpoints with the option disabled. ## Reducer deltas With a PostgreSQL saver and blob store, opt in for individual external keys. For example, a graph whose `messages` key uses `MessageIdStrategy` can use `externalizeCheckpointKeys("messages").checkpointDeltaKeys("messages")` on its `CompileConfig.Builder`. Nodes keep their existing state API and can append, replace by message ID, or return a `RemoveMessage` update. Use stable message IDs for edits and removals. A key can select either the append pilot or reducer deltas. Reducer deltas support `AppendStrategy`, `MessageIdStrategy`, and custom `CheckpointDeltaReducer` implementations. A custom reducer uses a stable ID and version for its merge behavior. It must produce equivalent results when replaying the original ordered inputs after serialization, with deterministic behavior and no external side effects. Register previous custom versions with `registerCheckpointDeltaReader(...)` when replacing the graph's strategy. Each `checkpoint-v1` record directly references its base and ordered update segments. The writer compacts a key when another segment would exceed eight segments or their total would exceed 1 MiB. A whole-value replacement or an in-place change outside reducer inputs also creates a new base. Reading metadata or resuming without accessing or merging the key leaves its bodies unopened. The existing PostgreSQL references and read leases protect the base and every segment through history pruning, branching, and archive retention. Delta writes are disabled by default. Removing a key from `checkpointDeltaKeys(...)` returns subsequent changes to regular checkpoint storage. This version can still read existing delta checkpoints on demand, including registered custom reducer versions. Upgrade all checkpoint readers and writers for the schema before enabling delta writes. ## Runtime-only state keys Use `CompileConfig.Builder.untrackedCheckpointKeys("retrievalCache")` for top-level values that can be recomputed or supplied on resume. This policy is off by default. Nodes read and write the key through the normal state API during a run, and runtime snapshots keep the value by reference. Choose either runtime-only state or external checkpoint storage for a given key. The key is omitted from new checkpoint state and task recovery records, including interruption snapshots. Resuming a checkpoint starts with the key absent unless it is supplied as input. Completed parallel tasks are reused with their durable outputs, including tasks that produced only runtime-only values. When a paused branch resumes, it can receive runtime-only values from the current parent state. A mapped subgraph uses its own configured key names. A multi-node parallel branch can resume from its pause checkpoint with task checkpointing disabled. Use a durable reference for a `Send` argument that must be checkpointed. The runtime rejects persisted dispatch arguments carrying an untracked key. Graph history and resume views also omit the key from older checkpoints. Existing records remain subject to their configured retention and blob collection policies. Streaming and application logs follow their own output policies.