Repository navigation
perf(memories): page the memory scan by keyset, not offset (#7116) - #7183
Merged
Merged
Conversation
A full scan walked offset pages, so every page re-sorted offset+limit rows: quadratic, and deep pages spilled to temp files (51 GB for two Observations tab opens on a 320k-observation bank). A row deleted behind the cursor also shifted the next page so it skipped a live row. The token is now the last row's (created_at, id), and the next page seeks past it. The comparison is spelled out instead of a row comparison because Oracle runs the same statement.
…the test seeds and deletes with raw SQL
nicoloboschi
force-pushed
the
triage/7116
branch
from
December 2, 2026 16:17
83d0802 to
e72ff34
Compare
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.
Fixes #7116.
Problem
Reading a whole bank's memories (exports, the Observations tree, several delete and cleanup paths) walked offset pages of 1000. Each page made Postgres re-sort
offset + limitrows, so a full walk was quadratic, and deep pages spilled to temp files. The reporter saw 51 GB of temp writes from two Observations-tab opens on a 320k-observation bank.It also lost rows: deleting a memory the walk had already passed shifted the next page, so the walk skipped a live row.
Fix
The page token is now the last row's
(created_at, id), and the next page starts right after it. The comparison is written ascreated_at > $a OR (created_at = $a AND id > $b)rather than the row form(created_at, id) > (...)from the issue's patch, because Oracle runs the same statement and doesn't support row comparisons. No index is added.What changes
<created_at iso>|<id>instead of a number. Only the engine reads it, and nothing reaches it over the API. An old numeric token now means "start from the beginning".Evidence
tests/test_scan_memories_keyset.py. On the old code, the delete-mid-walk test fails because a row goes missing. With the fix it passes.Oracle has not been run locally; the Oracle CI job covers it.
#7112 fixed the tree build and caching on top of this walk. The same offset problem in the document list is filed separately.