Skip to content
Source
Sign inSign up
TanStack/db

query-db on-demand refetch leaves a gap between served ordered pages #2136

Send to my agent
ClosedKyleAMathews(GitHub profile, opens in a new tab) opened this issue 1 comment

Problem

An on-demand queryCollectionOptions collection can show a wrong ordered window after refetch(). A row that moves between two served pages never loads.

The ordered loader pages a window: a first page (orderBy, limit) and later cursor pages (cursor.whereFrom, limit). Each loadSubset call gets its own query observer, keyed by its options, including the cursor (packages/query-db-collection/src/query.ts:2015). refetchQueryResults (query.ts:3653) refetches each observer on its own. After the backend changes, the first page can end at a different order value. The cursor page still starts after the old boundary. A row between the two pages is in no query result.

The DB side gets no signal: the provider deletes no loaded row and changes no row inside the loaded range. So the window stays wrong until a different change starts a load.

Reproduction

Order rank desc, limit 2. A LEFT-joined marker filters the rows, so the loader needs a cursor page.

  1. Backend rows: rank 10, 4, 1, 0, each with a marker. The window is [10, 4].
  2. Delete the marker of rank 10. The loader requests a cursor page (rank < 4, limit 1). The window is [4, 1].
  3. The backend adds rank 5 (with a marker) and rank 6 (without a marker).
  4. Call collection.utils.refetch().
QueryResult after refetch
first page (limit 2)ranks 10, 6
cursor page (rank < 4, limit 1)rank 1
tie requests (rank = 4, rank = 1)rank 4, rank 1

Expected window: [5, 4]. Actual window: [4, 1]. The row with rank 5 is in no result.

Contract

The LoadSubset contract must state this rule: a provider keeps each served ordered range whole. After a refetch, the provider must deliver every row that now sorts within a served range. A served range starts at the start of the order and ends at the last order value that a limited page returned. A provider can also delete the later cursor pages, so that the loader requests them again.

Evidence

  • The ordered-work oracle in #2132 has a refetching provider that obeys this rule. A provider that refetches each page on its own fails the named replay shows a row that moved between two served pages after a refetch. Random seeds found the trace first.
  • A receiving witness against the real query-db collection fails in the same way. It will go into the fix PR.

Work

  • Fix the query-db on-demand refetch so that it obeys the rule, and add the witness.
  • State the rule in the LoadSubsetOptions and SyncConfig.loadSubset documentation.

Sign in to join this conversation.