Patch placement
Status: spike done 2026-11-12; the recommendation below, with its names and type tags, was
accepted the same day. It answers where a patch's bytes live (ADR-0008). Upstream here means
langgraph-checkpoint-postgres 3.2.0, langgraph-checkpoint 4.3.1 and langgraph 1.2.20 on
PostgreSQL 16.15.
Note on timings: These placement spike runs were collected on a dev host under heavy IO pressure (PSI io "full avg10" 36.58 %), so the timings below are inferred, not a valid performance benchmark; byte counts are unaffected. Acceptance benchmarks were run separately on uncontended hosts (core-set-results.md).
Answer: option 1, with one change to the first sketch. Patch bytes go in upstream's
checkpoint_blobs under our own type tag, and a side table holds the base pointer. The foreign key
to the base row must be DEFERRABLE INITIALLY DEFERRED. With the plain (immediate) key that the
plan implies, upstream's adelete_thread and aprune(strategy="delete") fail on every thread
that holds a patch. Option 2 fails the comparison: upstream returns partial state, and no choice of
ON DELETE gives both a working delete and a safe prune.
How it was tested
- Option 1:
tests/integration/test_patch_placement.py, 16 tests, kept as stage 3 regression tests. Rows are written by our stage 2 saver, then turned into patches with SQL: the type tag is rewritten and a side row is added. Upstream fails on the tag before it reads any bytes, so the bytes do not have to be a real patch. Run:uv run pytest tests/integration/test_patch_placement.py -m integration -vgave16 passed. - Option 2, and option 1 with an immediate key: a scratch file that records each outcome
(exception and row counts per table) instead of asserting. It is not kept because it would not
be a regression test for the design we chose. Its results below are from one run. They are not kept as a record, so they count as
inferred. - Bytes and the cost of the foreign key:
benchmarks/s3_placement.py(removed before publication), in a run before public release.
Claim 1 — upstream reading our thread raises (option 1: proved)
| Upstream call | Test | Result |
|---|---|---|
aget_tuple, patch tag and saver-compressed full-copy tag |
test_upstream_get_tuple_raises_on_our_tag[...] |
raises |
alist |
test_upstream_list_raises_on_a_patch |
raises |
aget_delta_channel_history, seed is a patched snapshot |
test_upstream_delta_history_raises_on_a_patched_snapshot |
raises |
A graph compiled on upstream: aget_state, then the next ainvoke |
test_a_graph_on_upstream_raises_reading_our_thread |
both raise |
The error, from langgraph/checkpoint/serde/jsonplus.py:312 (langgraph-checkpoint 4.3.1):
NotImplementedError: Unknown serialization type: opc:zstd-patch:msgpack
Upstream raises on the first stored value of ours it decodes. A value we stored in plain upstream form (values below the minimum patch size) is read correctly. That is correct state, not partial state.
Delta history seeds from the target's ancestors, not from the target's own row. A first version of the test patched the target's snapshot, and upstream did not raise because it never read that row. The test now patches the parent's snapshot.
Not covered:
- An
EncryptedSerializeron upstream's side splits a type at its first+. With an encrypted inner type (opc:zstd-patch:msgpack+aes) it would try to decrypt the patch, and the error would be a decryption failure instead of "Unknown serialization type". This isinferredfromserde/encrypted.py:26-36and NOT run (pycryptodomeis not installed). - Upstream writing to a thread of ours. That succeeds silently, and the next read raises
(
inferred). NOT run.
Claim 2 — upstream's delete and prune never half-delete (option 1: proved)
Row counts were checked in all four tables (checkpoints, checkpoint_blobs,
checkpoint_writes, opc_blob_patch) after each call.
| Upstream call, thread state | Test | Result |
|---|---|---|
adelete_thread, three patches on one base |
test_upstream_delete_thread_removes_our_rows_with_its_own |
every row of the thread gone; other thread untouched |
aprune(strategy="delete") |
test_upstream_prune_delete_removes_our_rows_with_its_own |
every row gone |
aprune("keep_latest"), the kept value is a patch whose base only a dropped checkpoint names |
test_upstream_prune_keep_latest_fails_whole_when_it_would_drop_a_kept_base |
ForeignKeyViolation; every count unchanged |
| The same, upstream in pipeline mode | test_upstream_in_pipeline_mode_fails_the_same_prune_whole_and_stays_usable |
ForeignKeyViolation, counts unchanged; the next call raises the tag error (no hang) |
aprune("keep_latest"), kept value a full copy; dropped values are a base and its patches |
test_upstream_prune_keep_latest_drops_unkept_patches_with_their_base |
succeeds; 1 checkpoint, 1 blob, 1 write, 0 side rows left |
aprune("keep_latest") on a DeltaChannel thread of ours |
test_upstream_prune_keep_latest_refuses_our_delta_thread_untouched |
upstream's own NotImplementedError; counts unchanged |
Why the base key is deferred. Within one DELETE that removes a base and its patches, the
immediate key's check on the base row runs before the cascade has removed the patches' side rows.
The scratch run showed this: with the immediate key, adelete_thread and
aprune(strategy="delete") both raised ForeignKeyViolation on a thread with a patch. Nothing was
deleted (counts 4/4/4/3 before and after). Upstream's three pipelined DELETEs run as one implicit
transaction. A deferred key is checked at commit, after the cascade, and still refuses what rule 4
refuses (claim 3).
A hazard of deferred keys that option 1 avoids. In the scratch run, a deferred key that fails
inside upstream's pipelined adelete_thread left upstream's connection waiting forever. The
transaction stayed open, the call never returned, and the test timed out after 15 s. This is a
psycopg pipeline behaviour (inferred; seen in one scratch run with option 2). Under option 1,
adelete_thread cannot fail on our key: the key includes thread_id, so deleting all of a
thread's blobs also deletes every side row that references them. aprune runs in an explicit
transaction, and the pipeline-mode test above shows that its failure at commit does not hang.
Claim 3 — the database refuses a patch without its base (option 1: proved)
| Case | Test | Result |
|---|---|---|
Patch row and side row written in one statement, as put will write them |
test_a_patch_and_its_side_row_in_one_statement_are_accepted |
accepted: the keys see the row the sibling CTE inserted |
| Base row does not exist | test_a_patch_whose_base_does_not_exist_is_refused |
ForeignKeyViolation; the patch blob row is rolled back with it |
| Same version string, but in another thread, namespace or channel | test_a_base_with_the_same_version_elsewhere_does_not_satisfy_the_key[other-thread\|other-namespace\|other-channel] |
ForeignKeyViolation (rule 5) |
Claim 4 — option 2 under the same tests (scratch run; inferred)
Option 2 puts the patch bytes in our own table with a foreign key to the base row. The patch's
version has no row in checkpoint_blobs.
Reads: partial state, no error. Upstream's aget_tuple returned the checkpoint with the
channel absent. alist returned 4 checkpoints, and 3 of them were missing the channel. On a graph
thread, aget_state still returned all 6 messages. The reason is that LangGraph 1.2.20 rebuilds a
reducer channel that has a version but no stored value by replaying writes through
aget_delta_channel_history (langgraph/pregel/_checkpoint.py:410-430). That gives the right
answer only while every write since the nearest stored value still exists. After a prune, it gives
a wrong answer silently. A plain LastValue channel gets no rebuild and is simply missing
(inferred from the same code).
Delete and prune, for each ON DELETE choice on the base key:
| Upstream call | NO ACTION |
DEFERRABLE INITIALLY DEFERRED |
ON DELETE CASCADE |
|---|---|---|---|
adelete_thread |
ForeignKeyViolation, nothing deleted |
hangs (timed out at 15 s) | works |
aprune(strategy="delete") |
ForeignKeyViolation, nothing deleted |
ForeignKeyViolation, nothing deleted |
works |
keep_latest, the kept value's base would be dropped |
ForeignKeyViolation, unchanged |
ForeignKeyViolation, unchanged |
succeeds and silently deletes the kept checkpoint's patch |
keep_latest, the kept value is a full copy |
ForeignKeyViolation: the dropped checkpoints' patches still reference the base |
same | works |
Option 2 cannot satisfy both requirements. Upstream sees partial state, which is the loud-failure
problem. Its delete and prune either fail on every thread with a patch, or (with CASCADE) lose
data silently.
Measurements
Bytes — 400-turn plain graph (measured)
All figures in this table are measured before public release, from that run's cells (clean
commit; byte cells are
not affected by host load). One thread was written by the prototype PatchSaver (a prototype saver, removed before publication) and copied into
each layout: 775 patches and 425 full copies, the same rows in each. Every table was rewritten by
VACUUM FULL before measuring, so the conversion's dead rows cost nothing.
| Layout | Total | Indexes | checkpoint_blobs |
opc_blob_patch |
Against prototype |
|---|---|---|---|---|---|
| Prototype (base named in the type tag, no side table) | 9.26 MB | 0.47 MB | 7.03 MB | — | 1.000× |
| Option 1 | 9.55 MB | 0.58 MB | 7.05 MB | 0.28 MB | 1.032× |
| Option 2 | 9.34 MB | 0.51 MB | 1.00 MB | 6.11 MB | 1.009× |
Option 1's side table costs about 280 KB for 775 patches, roughly 360 B per patch (measured
total, inferred per-row division). Most of that is two copies of the 49-character version string
in the heap and in two indexes. Against upstream's 66.1 MB on the same workload (measured),
option 1 is about 0.144× (inferred: the two figures come from different ledgers and different
VACUUM settings). That is still far inside the acceptance bar of at least 3× smaller.
Cost of the foreign key per patch write (inferred: contended run)
A micro-benchmark timed 3,000 single-patch statements per arm on one autocommit
connection, interleaving four arms: options 1 and 2, each with and without their keys. The run was
contended: load per core peaked at 0.856 against the ledger's threshold of 0.7, and IO pressure
was about 49 % when checked before the run. By the benchmark rule these timings are not a
performance measurement, so they are inferred:
| Arm | p50 with keys | p50 without | p50 difference |
|---|---|---|---|
| Option 1 (blob row + side row, two keys) | 1.22 ms | 1.15 ms | +64 µs |
| Option 2 (one row, one key) | 1.18 ms | 1.12 ms | +58 µs |
The difference is small next to upstream's put p50 of 14.5 ms on this graph (measured),
so the key should not threaten the 1.25× bar (inferred). The extra cost of the
key on deletes (known limit 5) was not measured here.
The design to build (option 1)
Type tags
| Stored value | checkpoint_blobs.type |
|---|---|
| Patch | opc:zstd-patch: + the serde's own type, e.g. opc:zstd-patch:msgpack |
| Full copy compressed by the saver | opc:zstd: + the serde's own type, e.g. opc:zstd:msgpack |
Anything else (small values, empty) |
the serde's type, unchanged, so upstream reads it |
- The
opc:prefix cannot collide with upstream's types (null,bytes,bytearray,json,msgpack,pickle,empty, and<type>+<cipher>from the encrypting serde). - The tag names the algorithm instead of carrying a version number. A future format gets a new tag, and rows in the old tag stay readable.
- The tag does not repeat the base version. The prototype's tag did, but the side table is the one place the base pointer lives.
Side table
CREATE TABLE opc_blob_patch (
thread_id text NOT NULL,
checkpoint_ns text NOT NULL,
channel text NOT NULL,
version text NOT NULL,
base_version text NOT NULL CHECK (base_version <> version),
PRIMARY KEY (thread_id, checkpoint_ns, channel, version),
-- the patch's own row: deleting it (upstream's delete or prune) drops this row with it
FOREIGN KEY (thread_id, checkpoint_ns, channel, version)
REFERENCES checkpoint_blobs ON DELETE CASCADE,
-- the base row: no delete of a base that a patch still needs (rules 4 and 5); checked
-- at commit so one DELETE can take a base together with its patches
FOREIGN KEY (thread_id, checkpoint_ns, channel, base_version)
REFERENCES checkpoint_blobs (thread_id, checkpoint_ns, channel, version)
DEFERRABLE INITIALLY DEFERRED
);
-- serves the base key's check on every delete from checkpoint_blobs
CREATE INDEX opc_blob_patch_base
ON opc_blob_patch (thread_id, checkpoint_ns, channel, base_version);
setup() creates it from our own migration ledger, opc_migrations
(src/open_postgres_checkpointer/_migrations.py); the tests use that. The spike harness keeps its
own copies, because it builds variants with and without each key.
What it means for the rest of stage 3
putwrites the patch's blob row and its side row in one statement (tested above). A retriedputneedsON CONFLICT DO NOTHINGon both.- Reads join the side table to find each patch's base.
rehydraterewrites each row of ours as a plain upstream row and deletes its side row in the same statement. It must not delete a base before the patches that depend on it are rewritten.- Our own lifecycle code (stage 4) must rewrite a base's dependants as full copies before deleting
the base. The key is the guard that catches it if it does not.
Note (2026-11-30): stage 4 instead keeps every kept patch's base, in SQL only (
_sql.RETAIN_CHECKPOINTS_SQL,RETAIN_CHAINS_SQL; architecture.md §7); no base is rewritten.
Decisions
- Accept option 1 with the deferred base key (recommended).
- Accept the tag strings
opc:zstd-patch:andopc:zstd:, and the side-table nameopc_blob_patch. - Quote the cost of the foreign key only after a re-run on a quiet host.