Repository navigation
Add delete_key store host function for exact-key deletes - #1085
Merged
maoueh merged 6 commits intoDec 3, 2026
Merged
Conversation
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
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>
This was referenced Dec 3, 2026
Merged
This was referenced Dec 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A module can delete from its output store only with
delete_prefix(substreams-rsStoreDelete::delete_prefix). The engine queues it asOperation_DELETE_PREFIX(storage/store/value_delete.go:14) and applies it withkvImpl.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"). ItsScan(kv_impl_memory.go:55) checks every key in the map (line 58) and sorts the matches (line 63), so eachdelete_prefixcosts the whole store, even when the prefix matches one key. The bbolt backend'sScanseeks to the prefix (kv_impl_mmap.go:254) and is cheap. TherecentlyDeletedPrefixescache (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 todelete_prefixalso 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 thestatemodule, on the wazero and wasmtime runtimes, registered besidedelete_prefixwith the same(i64, i32, i32)signature.Call.DoDeleteKeyhands it to the store. It'sdelete_keyrather thandelete, which is a reserved word in JS/TS/AssemblyScript and C++, so other SDKs can import it without an alias, and it pairs withdelete_prefix.The store's
Deleterinterface gainsDeleteKey(ord, key), named so becauseFullKValready has aDelete(ctx, file). It queues a newOperation_DELETE_KEY(value 22 in the internaldeltas.proto).Flushapplies it with onekvImpl.Getand, when the key exists, anApplyDeltaof the sameStoreDelta_DELETE, withOldValue, that a prefix delete emits for that key. An absent key emits nothing. The old value needs no copy: unlikeScan's,Get's value stays valid after the call (mmapKVImpl.Getcopies it out of bbolt).PartialKV.DeleteKeyrecords each key once inDeletedKeys. The partial file'sStoreDatagetsrepeated string delete_keys = 3(storage/store/marshaller/pb/store.proto), written byMarshalStreamSnapshotand read byUnmarshalIterinto the load trailer, asdelete_prefixesis.Mergedeletes 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.DoDeleteKeycounts as a store write in the module stats.total_store_deleteprefix_countstays 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.mdlistsdelete_keybesidedelete_prefix, andEmptyPartialKVs's doc counts deleted keys.recentlyDeletedPrefixesneeds 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
delete_keyfails to instantiate on an engine without it.delete_keysload as before. Nothing is written to the field when no key was deleted, so those files are byte-identical to today's.delete_keys(UnmarshalIterskips unknown fields) and keep those keys in the full store. Deploy tier1 no later than tier2.Marshal,Unmarshal,MarshalStream,UnmarshalStream,UnmarshalStreamFast,MarshalStreamUnsorted, and theProtoandProtoingFastmarshallers), which only tests and benchmarks call, don't carrydelete_keys; they skip it like any unknown field. Full stores never hold deleted keys.deleteverb 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 ./...andgo veton those packages are clean.delete_keyyet. That needs the substreams-rs change (AddStoreDelete::delete_keyfor exact-key deletes substreams-rs#41).Generated code:
pb/sf/substreams/intern/v2/deltas.pb.goregenerated withbuf generate --path proto/sf/substreams/intern/v2/deltas.protoand protoc-gen-go v1.36.10, the committed version;deltas_vtproto.pb.gois unchanged, since an enum value adds no vtproto code.storage/store/marshaller/pb/store.pb.goandstore_vtproto.pb.goregenerated with the protoc command in that directory'sgenerate.shand 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