Skip to content

A chunked record's head chunk ratchets down to the smallest size it has ever had, and never recovers #6163

Closed

Description

@lvca

Found while building the page-sealing fixture for #6149 (PR #6153): a fixture that sealed a page by growing its last record past the page size, then shrank that record back, silently un-sealed the page. Chasing why turned up a permanent, one-way ratchet.

The head chunk's declared size only ever goes DOWN

LocalBucket.updateMultiPageRecord sizes the head chunk from what the page already says, and can only cap it downwards:

int chunkSize = currentPage.readInt(newPosition);   // what the head chunk declares TODAY
int bufferSize = buffer.size();

// WRITE THE 1ST CHUNK
if (bufferSize < chunkSize)
  // 1ST AND LAST CHUNK
  chunkSize = bufferSize;
...
currentPage.writeInt(newPosition, chunkSize);       // written back, permanently

There is no path that ever raises it again: the next update reads the already-shrunk value and can only cap it further. So a chunked record's head chunk ratchets down to the smallest size that record has ever had, and stays there for the rest of its life.

The room is not reclaimed by the record, either. The physical gap between the shrunk end and where the head chunk used to end stays inside the record's slot region, unusable by the record (it cannot grow its declared size back) and, unless the head chunk is the page's last record, unusable by anyone else.

Why it matters

A record that oscillates in size degrades permanently. Take a 200 KB chunked record whose head chunk holds ~9 KB. One update drops it to 100 bytes; the head chunk is now 100 bytes forever. Every later update back up to 200 KB puts ~9 KB less on the head page and pushes it into continuation chunks on other pages - more chunk headers, more pages touched per read and per write, for the whole remaining life of the record. Nothing recovers it short of deleting and re-creating the record.

It also makes a page's free tail move in a direction nothing else does. getPageOccupiedInBytes derives a chunk record's footprint from the declared chunk size, so when the head chunk is the page's LAST record, shrinking it hands the difference back as free tail that the allocator can give to other records. That is the mechanism that un-sealed the fixture, and it is the only way a page's occupied byte count goes down without a delete.

Not a correctness bug, and specifically not one for the slot merge

I checked the disjoint-slot merge (#6129) against this before filing:

  • SLOT_KIND_FIRST_CHUNK replay refuses when body.length > committedChunk.length, and a shrink makes body shorter, so the replay writes strictly inside the committed footprint. No overlap.
  • A concurrent transaction cannot allocate into the tail a shrink is about to release, because it sizes the page from the committed image, which still has the larger head chunk. The release is only visible after the shrink commits.

So this is a space/performance issue, not a race.

Worth fixing, and the constraint on how

The natural fix - let the head chunk grow back into the room it already owns - has to know where its region ends, which the page does not record: the head chunk declares its content length, not its allocated footprint. The end is only recoverable by looking at where the next record starts (or at the page's content end when it is the last record), which is exactly what getPageOccupiedInBytes/getLastRecordPositionInPage already compute for other purposes.

Two shapes, both worth weighing:

  1. Let updateMultiPageRecord grow the head chunk back up to the next record's offset (or the page's content end when last). Recovers the ratchet for free on the next update, and keeps the "head chunk keeps its slot" property the merge relies on. Needs care: growing the head chunk past what the merge's pre-image described is the one thing rebaseRecordOnPage currently refuses outright (a chunk head never grows), so that refusal would have to become a re-derivation rather than a flat no.
  2. Do not shrink the declared size at all - keep the head chunk's footprint truly fixed and let the unused bytes sit as slack inside it. Simpler, matches what engine/CLAUDE.md already claims, and removes the odd "page occupancy went down without a delete" behaviour. Costs the space permanently rather than temporarily.

(1) is better if the merge change is done properly; (2) is the honest minimum if it is not.

Documentation

engine/CLAUDE.md currently states "The head chunk's footprint on the page is fixed when the record spills and is never grown again". The "never grown" half is right; the sentence reads as though the footprint is immutable, which is what made this take a while to spot. It should say the declared size can SHRINK, one-way, and that the shrink is what releases page tail.

Activity

  1. added this to the 26.9.1 milestone on Aug 14, 2026
  2. self-assigned this
    on Aug 14, 2026
  3. added theissue type on Aug 14, 2026
  4. added 3 commits that reference this issue on Aug 14, 2026
  5. added a commit that references this issue on Aug 15, 2026
  6. added
    engine
    storage
    on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugenginestorage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions