Repository navigation
feat(mem_wal): move a live writer onto a schema change instead of reopening it - #9712
Merged
Merged
Conversation
xuanyu-z
force-pushed
the
mem-wal-evolve-schema
branch
from
October 5, 2026 19:30
505ff2f to
90933a7
Compare
A schema change on the base table (a column added, dropped, renamed, or relaxed to nullable) leaves a live ShardWriter holding the schema it opened with. Every write shaped to the new schema is rejected, and the only way on was to reopen the writer: replay the WAL tail and rebuild every in-memory index. ShardWriter::evolve_schema seals the active memtable under the schema it was written with and starts the next one under the new schema. The sealed memtable flushes as a generation of its own schema, with the index configs it was built with, which the flush trigger now carries instead of the writer holding one set. ShardWriter::evolve_to derives the schema and maintained index set from a Dataset the way mem_wal_writer does. Reads resolve each in-memory memtable by field id the way they already resolve sealed generations: scan, point lookup, vector search and full-text search each route a memtable whose schema no longer matches the table through GenerationRead, and keep today's path for one that does. The point-lookup probe fast paths are skipped while any memtable predates the table's schema. Writes are now validated and conformed under the writer lock, against the schema of the memtable they land in, so a write racing a schema change cannot land under the wrong one. MemTableScanner::use_index(false) now also forces brute force for vector search, which a memtable with a deferred predicate needs to rank every row. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
A schema change and an index-set change are the same operation on a live writer: build the next `WriterSchema`, then seal so the following memtable carries it. `evolve_schema` and `replace_index_configs` now share that half and build their replacement under the `state` write lock, against the schema the writer actually holds, so neither can flatten the half it does not own. `evolve_to` read `maintained_indexes` straight off the index details. A table maintaining every index carries no names there -- they come from the table -- so the first schema change installed an empty set and the writer stopped maintaining every index it had until something else refreshed it. All three paths that hand a set to a writer now derive it through one helper, which is what let this one drift. Folds `rebind_active_indexes` into the shared seal path, drops the second index-set comparison in `WriterSchema::is_equivalent`, and returns `Option<SealFence>` from `evolve_schema` so a caller can wait on what it sealed. `evolve_schema` now checks the fence, which `force_seal_active` and `replace_index_configs` already did.
`rust/AGENTS.md` puts imports at the top of the file, not in function bodies. `WriterSchema`'s doc named only `evolve_schema` as replacing the writer's current one, which is now half the answer, and `evolve_to` did not say what it returns when the writer already holds the schema and index set.
…column Refusing every primary-key rename was too broad: an unsharded MemWAL has one shard that owns every row, so no name decides anything there and the rename is as ordinary as any other. Three end-to-end tests exist to prove that, two of them on unsharded tables. The refusal now reads the spec and fires only for a column the MemWAL actually shards by, which is the column whose name decides where a row goes.
`stored_name` and its full-text twin scanned their map by value on every lookup, once per column per read. The maps are built once per generation, so they are inverted there instead and the lookups are a hash hit. The two sites that ask whether a stored column is the table's at all take a set of the stored names, which is what they were using the map's keys for.
Restricting only the column a sharded table hashes was not enough. The key is how a writer identifies a row at all: a writer that has not yet seen a rename aligns a batch through the names it still holds, so an upsert naming the new key lands its values under the wrong field ids. On a sharded table it also hashes the wrong column and places the row in the wrong shard. Nothing downstream separates the two schemas — swapping two names leaves every name and type known to both — and the refresh sweep does not heal what is already stored. Refusing the rename removes the case, beside the retype and non-null tightenings already refused here. Every other column renames freely.
Contributor
There was a problem hiding this comment.
Primary-key renames require dropping the attached MemWAL first.
Consumers must stay in one mode or checkpoint, unset, and reinstall before switching, as clarified by the author. The API does not enforce that transition requirement; switching the same shard with an uncheckpointed tail can leave acknowledged writes unrecoverable.
Please mark this PR with the breaking-change label.
hamersaw
approved these changes
Oct 7, 2026
hamersaw
left a comment
Contributor
There was a problem hiding this comment.
A few nits and questions, but overall looks great! Thanks.
Every comment this change added now says why in one or two plain sentences: restatements of the code and step-by-step test narration are gone, and the invariants stay, shorter. The doc on `GenerationRead::names` described the map in the direction it had before this change; it now matches the code.
`memtable_cannot_take` was inserted between `fill_reached_flush_threshold` and its doc comment and lint allowance, so both applied to the wrong function. `flush_memtable`'s doc also continued after its `#[instrument]` attribute.
`write.rs` is past 13,000 lines, mostly tests. The tests this change adds, for moving a writer onto a new schema and for replaying the generation boundaries that leaves behind, now live in `write/tests/evolve_schema.rs`. Two helpers they share with the rest of the writer tests become `pub(super)`.
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.
A schema change on a MemWAL-backed table — a column added, dropped, renamed at
any depth, or relaxed to nullable — lands on the base table only. A live
ShardWriterkeeps the schema it was opened with, so it rejects every writeshaped to the new one, and the only way forward is to reopen it: re-read the
WAL tail since the last flush, re-flush what fills during replay, and rebuild
every in-memory index. The shard takes no writes while that happens.
ShardWriter::evolve_schemamoves the writer instead. It seals the activememtable under the schema it was written with and starts the next one under the
new schema, so the sealed one flushes as a generation of its own schema with the
index configs it was built with. An empty memtable is replaced rather than
sealed. Nothing is replayed and no index is rebuilt.
ShardWriter::evolve_to(&Dataset)derives both halves from a dataset.The writer side of this change is small. Most of the diff is the read side: the memtable sealed at a schema change stays in memory until it flushes, still holding its rows under the old column names, so every read path now has to resolve such a memtable.
Reading across the change
A sealed generation already resolves to the table's schema by field id. An
in-memory memtable did not — pairing one with a schema it was not created from
was documented as outside the contract. Now a memtable whose schema no longer
matches the table resolves the same way, on every read path:
A memtable that matches the table — every memtable in the steady state — keeps
exactly today's path, behind a positional check.
Writing safely across it
A put is validated and conformed under the writer lock, against the schema of
the memtable it lands in, so a write racing a schema change cannot be checked
against one schema and inserted under another. The logical, storage and blob
schemas, the primary key and the index configs move together as one
WriterSchema.Recovery
A WAL entry now records the generation its rows belong to, beside the writer
epoch it already carried. Replay reads the boundary instead of measuring it,
which is what lets a generation end at a schema change rather than at a row
count. A WAL-only writer holds no memtable and records none; replay still sizes
those entries itself. Where a recorded generation will not fit the memtable
replaying it, replay refuses before publishing anything, so the shard is
untouched and reopens under the configuration it was written with.
This is a format addition — an older reader ignores the key and falls back to
measuring — and
docs/src/format/table/mem_wal.mddocuments it.Two bugs surfaced on the way and are fixed here: replay indexed only its final
memtable, so any generation it sealed mid-replay was published with no
primary-key sidecar and its rows could not be looked up by key; and the writer
did not own the entry's reserved metadata keys, so a caller's schema metadata
could name a generation the entry did not have.
Testing
mem_wal: 809 passing,fmtandclippy -D warningsclean. New coverage foreach read arm across a rename, an add and a drop; for a nested path, which only
resolves because the field-id map pairs full paths rather than top-level names;
for the recorded generation, including a boundary nothing else can measure; and
for the capacity refusal, asserting the manifest is unchanged by it.