Skip to content

Add delete_key store host function for exact-key deletes - #1085

Merged
maoueh merged 6 commits into
streamingfast:developfrom
abhimanyusinghgaur:abhimanyu/store-delete-key
Dec 3, 2026
Merged

maoueh merged 6 commits into
streamingfast:developfrom
abhimanyusinghgaur:abhimanyu/store-delete-key

Conversation

@abhimanyusinghgaur

@abhimanyusinghgaur abhimanyusinghgaur commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A module can delete from its output store only with delete_prefix (substreams-rs StoreDelete::delete_prefix). The engine queues it as Operation_DELETE_PREFIX (storage/store/value_delete.go:14) and applies it with kvImpl.Scan(prefix, …) (value_delete.go:30).

The default store backend is the in-memory one (storage/store/kv_impl_config.go:73: "mmap is opt-in, memory is the default"). Its Scan (kv_impl_memory.go:55) checks every key in the map (line 58) and sorts the matches (line 63), so each delete_prefix costs the whole store, even when the prefix matches one key. The bbolt backend's Scan seeks to the prefix (kv_impl_mmap.go:254) and is cheap. The recentlyDeletedPrefixes cache (value_delete.go:21) only skips deleting the same prefix again.

Both backends already implement an exact Delete(key) (kv_impl_memory.go:50, kv_impl_mmap.go:224), but no host function reaches it. A full key passed to delete_prefix also deletes every longer key starting with it.

Our case: a Polymarket price store keyed per condition deletes a condition's key when it resolves, about 0.9 deletes a block on average, each one a walk of the whole store on the default backend.

Change

  • A delete_key(ord, key_ptr, key_len) host function in the state module, on the wazero and wasmtime runtimes, registered beside delete_prefix with the same (i64, i32, i32) signature. Call.DoDeleteKey hands it to the store. It's delete_key rather than delete, which is a reserved word in JS/TS/AssemblyScript and C++, so other SDKs can import it without an alias, and it pairs with delete_prefix.

  • The store's Deleter interface gains DeleteKey(ord, key), named so because FullKV already has a Delete(ctx, file). It queues a new Operation_DELETE_KEY (value 22 in the internal deltas.proto).

  • Flush applies it with one kvImpl.Get and, when the key exists, an ApplyDelta of the same StoreDelta_DELETE, with OldValue, that a prefix delete emits for that key. An absent key emits nothing. The old value needs no copy: unlike Scan's, Get's value stays valid after the call (mmapKVImpl.Get copies it out of bbolt).

  • PartialKV.DeleteKey records each key once in DeletedKeys. The partial file's StoreData gets repeated string delete_keys = 3 (storage/store/marshaller/pb/store.proto), written by MarshalStreamSnapshot and read by UnmarshalIter into the load trailer, as delete_prefixes is. Merge deletes those keys from the full store together with the deleted prefixes, before it merges the partial's values, so a key deleted and then set again in the segment ends with the segment's value.

  • DoDeleteKey counts as a store write in the module stats. total_store_deleteprefix_count stays for prefix deletes, which "can be a costly operation on large stores" (proto/sf/substreams/rpc/v2/service.proto:437); a counter of its own would need new fields in the internal and public RPC protos and both TUIs.

  • Docs: the update-policy tip in docs/references/substreams-components/modules/types.md lists delete_key beside delete_prefix, and EmptyPartialKVs's doc counts deleted keys.

recentlyDeletedPrefixes needs no change. An exact delete only removes a key, so a cached prefix is still one under which nothing was set since its delete, and every set already clears the cached prefixes it falls under.

Compatibility

  • A module that imports delete_key fails to instantiate on an engine without it.
  • Partial files without delete_keys load as before. Nothing is written to the field when no key was deleted, so those files are byte-identical to today's.
  • An older tier1 squashing a partial written by a newer tier2 would skip delete_keys (UnmarshalIter skips unknown fields) and keep those keys in the full store. Deploy tier1 no later than tier2.
  • The struct-based codecs (Marshal, Unmarshal, MarshalStream, UnmarshalStream, UnmarshalStreamFast, MarshalStreamUnsorted, and the Proto and ProtoingFast marshallers), which only tests and benchmarks call, don't carry delete_keys; they skip it like any unknown field. Full stores never hold deleted keys.
  • The WASI virtual file system, used by the JavaScript/v8 runtime, is unchanged: its delete verb stays a prefix delete.

Tests

  • TestDeleteKey (new, storage/store/value_delete_test.go), on both backends: only the exact key goes, an absent key emits no delta, set then delete, delete then set, and ordinals deciding over call order. Each case checks the store contents and the deltas.
  • TestStore_Merge_DeletedKeys: on a SET store only the exact keys go and a key set again in the segment keeps its new value; on an ADD store a key deleted in the segment restarts from the segment's sum (5, not 15). Size accounting matches the contents.
  • TestPartialKV_Save_Load_DeletedKeys: keys are recorded once, an absent key included, and survive save and load beside a deleted prefix.
  • TestEmptyPartialKVs: a partial holding only a deleted key is not empty, so the squash doesn't skip it.
  • TestSnapshotStreamRoundTrip: delete keys round trip on both backends.
  • go test ./storage/... ./wasm/... ./pipeline/... ./orchestrator/... ./pb/... passes; go build ./... and go vet on those packages are clean.
  • Not run end to end: no test module calls delete_key yet. That needs the substreams-rs change (Add StoreDelete::delete_key for exact-key deletes substreams-rs#41).

Generated code: pb/sf/substreams/intern/v2/deltas.pb.go regenerated with buf generate --path proto/sf/substreams/intern/v2/deltas.proto and protoc-gen-go v1.36.10, the committed version; deltas_vtproto.pb.go is unchanged, since an enum value adds no vtproto code. storage/store/marshaller/pb/store.pb.go and store_vtproto.pb.go regenerated with the protoc command in that directory's generate.sh and the committed plugin versions (protoc-gen-go v1.27.1, protoc-gen-go-vtproto v0.3.0); besides the new field, only the protoc version in the header changed.

🤖 Generated with Claude Code

A module can only delete store keys through `delete_prefix`, which scans
for every key under the prefix. On the default in-memory backend that
scan checks every key in the store, so deleting one known key costs the
whole store.

`delete_key` deletes one key with a lookup. It is queued as a new
`DELETE_KEY` operation, applied with `kvImpl.Get` and `Delete`, and
emits the same `DELETE` delta a prefix delete emits for that key.
Partial stores record the deleted keys in a new `delete_keys` field of
`StoreData`, and the merge into the full store deletes them before
merging the partial's values, as it does for deleted prefixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	docs/release-notes/change-log.md
`PartialKV.Roll` cleared the KV but kept `DeletedPrefixes` and
`DeletedKeys`, so a partial saved after a roll carried the previous
segment's deletes, and its squash would delete them again from the full
store, including keys the previous segment set back after deleting them.
Tier2 jobs are single-segment today, so no saved partial follows a roll.
`state.delete_keys(ord, keys_ptr, keys_len)` deletes many exact keys in
one host call. The payload is each key as a little-endian u32 length
followed by the key bytes, read to the end; a malformed payload fails the
module deterministically before any key is deleted. Each key goes through
the same `DeleteKey` path as `delete_key`.

The simple_substreams fixture gains `test_store_delete_key` and its
assert module, calling both host functions directly since its substreams
crate predates them; `TestStoreDeleteKey` runs them across a tier2
squash. The fixture's `pb/mod.rs` dropped includes for files it never
had, so it builds again; the spkg was patched in place (new binary, two
modules cloned from the delete_prefix ones) because its manifest no
longer packs.
- Record deletes in a partial store that tier2 rebuilds from cached
  module outputs (`PartialKV.ApplyOps`); `delete_prefix` deletes were
  silently lost on that path before.
- Delete a partial's keys from the full store with a new
  `KVImpl.BatchDelete`, one bbolt transaction per batch, instead of a Get
  and a Delete per key.
- Fail `delete_prefix`, `delete_key` and `delete_keys` deterministically
  when called from a non-store module, instead of a nil-pointer panic.
- Carry `delete_keys` in the struct-based store codecs too.
- Declare `DELETE_KEY` beside `DELETE_PREFIX` in deltas.proto, rename
  `PartialKV.seen` to `seenPrefixes`.
- Test merges with deleted keys for every update policy on both
  backends, `TestStoreDeleteKey` on both WASM runtimes, and the rebuild
  from cached outputs end to end.
@maoueh
maoueh merged commit b2209d9 into streamingfast:develop Dec 3, 2026
7 checks passed
maoueh pushed a commit to streamingfast/substreams-rs that referenced this pull request Dec 3, 2026
Adds the `delete_key` extern to the `state` import module, a
`state::delete_key` wrapper, and a `delete_key` method on `StoreDelete`
beside `delete_prefix`. A module that calls it needs an engine with the
`delete_key` host function (streamingfast/substreams#1085).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@abhimanyusinghgaur
abhimanyusinghgaur deleted the abhimanyu/store-delete-key branch December 4, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants