Repository navigation
perf: keep the cost of a sync flat as the local files and the WAL grow - #57
Merged
Merged
Conversation
… WAL frames a sync reads Two instances on the same database could write the same TXID: each keeps its position in memory, and a rename wrote over the file of the other. A file is now published with a hard link, which fails when the name is taken, and the sync reads the local files again. A sync which resumes allocated a buffer of the size of the WAL to read a few frames of it. It now holds its header and the frames it reads, which the reader takes by their offsets.
…te each sync to a file of its own Two instances on the same database each kept their position: once the retention removed a file, one of them could write its TXID again. The position is now the one of the local files, shared by the instances of a process. Two syncs which wrote the same TXID at once wrote the same temporary file, so the one published first could be changed by the other: each attempt has its own. Without hard links a file is renamed, as before: looking for the name first only looked safe.
…t outside changes, and no rename over a file The position of the local files was shared by the path as it was given: two spellings of one path had two positions. It is kept by the canonical path of the local cache. A thread or a process which does not share it leaves a level-0 directory which changed: the local files are then read again. On a file system without hard links two syncs could both rename to the same TXID. A sync fails there, and says why: nothing else publishes a file without writing over another one.
A sync picks its TXID and publishes it some awaits later. Another instance could publish that TXID and the next one meanwhile, and the retention remove the first: the name was free again, and the sync published a second file under it, behind the newest one. The syncs and the snapshots of the instances of a process on the same database now wait for each other, from the choice of a TXID to its publication.
mcollina
requested changes
Dec 1, 2026
| const run = (syncQueues.get(key) ?? Promise.resolve()).then(fn); | ||
| const done = run.then( | ||
| () => {}, | ||
| () => {}, |
| syncQueues.set(key, done); | ||
| void done.then(() => { | ||
| if (syncQueues.get(key) === done) syncQueues.delete(key); | ||
| }); |
Member
There was a problem hiding this comment.
can't you move this into the previous .then()?
| const syncStates = new Map<string, SyncState>(); | ||
|
|
||
| /** The last sync or snapshot of each local cache which runs or waits, see {@link exclusive}. */ | ||
| const syncQueues = new Map<string, Promise<void>>(); |
| let range: { minTXID: bigint; maxTXID: bigint }; | ||
| try { | ||
| range = parseFilename(entry.name); | ||
| } catch { |
Member
There was a problem hiding this comment.
genrically we should try to swallow errors
| let fh: FileHandle; | ||
| try { | ||
| fh = await open(this.walPath(), 'r'); | ||
| } catch { |
Member
There was a problem hiding this comment.
please document while swallowing this error is ok
| } | ||
| // The WAL may have grown since its size was read, never shrunk without being reset: what was read is its length. | ||
| return new PartialWAL(start + read < size ? start + read : size, header, start, tail.subarray(0, read)); | ||
| } catch { |
Member
There was a problem hiding this comment.
please document while swallowing this error is ok
…nd fail on a WAL which cannot be read
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 sync after every small write got slower with each write, until the next snapshot cleared the local files.
To find its position, a sync listed the local LTX files of every level and called
staton each, three times. It now reads their names only, once, and keeps what it published in memory for the next sync; anything else which changes the local files (a snapshot, a compaction, the retention, a restore) makes the next sync read them again. A sync which resumes also read the whole WAL: it now reads its header, the frame before the point it resumes from and the frames after it, and reads it whole whenever it cannot resume. It holds those bytes only, not a buffer of the size of the WAL.Since a sync no longer reads its position from the disk each time, two instances on the same database could write the same TXID. The position is the one of the local files, kept by the canonical path of the local cache and shared by the instances of a process, so one which syncs after another starts from its file, also once the retention removed the files in between. Their syncs and snapshots run one at a time, from the choice of a TXID to its publication, so that none publishes a TXID which another one wrote and the retention removed meanwhile. A change of the level-0 directory by anything else, another thread or process, makes the next sync read the local files again. Each attempt of a sync writes a temporary file of its own, and publishes it with a hard link, which fails when the name is taken where a rename wrote over it: the sync then reads the local files again.
One change of behavior: on a file system without hard links a sync now fails, with an error which says so. Nothing there publishes a file without writing over another one, and a rename let two syncs write the same TXID.
pnpm bench:small-writesis new: it writes one row, syncs, and repeats, and reports the time of the local step of a sync block after block, as the local files and the WAL grow. Nothing is uploaded. Onmainand on this branch, on one machine:main, ms/syncThe last block against the first is 5.9 times on
mainand 0.9 times here. With the WAL checkpointed every 250 syncs (CHECKPOINT=250) it stays at about 1.1 ms. In an application which syncs after every write, to a Postgres replica, with 16 writes in flight, the writes went from 332 to 639 a second and their median from 47 to 21 ms; one run each.New tests cover the state kept against the one read from the disk, a new instance, two instances which write in turn, at the same time, across a file the retention removed and through two spellings of one path, a file added by something else, a directory or a link named as an LTX file, a snapshot, a restore, a WAL reset between two syncs, and what a resuming sync allocates for a WAL of several megabytes.