Skip to content

perf(memories): page the memory scan by keyset, not offset (#7116) - #7183

Merged
nicoloboschi merged 2 commits into
mainfrom
triage/7116
Dec 2, 2026
Merged

nicoloboschi merged 2 commits into
mainfrom
triage/7116

Conversation

@nicoloboschi

Copy link
Copy Markdown
Collaborator

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 + limit rows, 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 as created_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

  • The token is <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".
  • Nothing else.

Evidence

  • New 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.
  • Nearby tests pass: 617 (memories extension, document transfer/export, observation cleanup and scopes).
  • Full walk over 150k observations, 1000 per page, three runs:
Time Temp spill
Before 53 s / 33 s / 16 s ~2.5 GB each run
After 11 s / 8 s / 6 s 0

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.

@nicoloboschi nicoloboschi added the core Core Hindsight service (API, engine, DB, clients, CLI, UI, docs) label Dec 2, 2026
@nicoloboschi nicoloboschi added the oracle-tests CI: run Oracle 23ai tests on this PR label Dec 2, 2026
@nicoloboschi nicoloboschi reopened this Dec 2, 2026
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.
@nicoloboschi
nicoloboschi merged commit 368d402 into main Dec 2, 2026
226 of 228 checks passed
@nicoloboschi
nicoloboschi deleted the triage/7116 branch December 2, 2026 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core Hindsight service (API, engine, DB, clients, CLI, UI, docs) oracle-tests CI: run Oracle 23ai tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

scan_memories offset cursor makes full scans quadratic and spills to temp files (follow-up to #7029)

1 participant