-
Notifications
You must be signed in to change notification settings - Fork 130
Fix Date Precision Mismatch in Cursor-Based Pagination #913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: b2766e1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 11 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Size Change: +65 B (+0.08%) Total Size: 86.5 kB
ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 3.34 kB ℹ️ View Unchanged
|
| createOrderByBugTests(`eager`) | ||
| }) | ||
|
|
||
| describe(`OrderBy with Date values and precision differences`, () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test checks that we push down the range predicates rather than the eq - I considered making it test the effect, but that ends up testing the mock backend not the implementation.
Arguably we could add a e2e for electric on this.
KyleAMathews
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very nice! Makes me wonder how many other apps out there have this bug and would never figure out a nice clever optimization like this. Big pro of using DB vs. naive filters.
|
🎉 This PR has been released! Thank you for your contribution! |
Failed test run before the fix: https://github.com/TanStack/db/actions/runs/19701959124/job/56440153416?pr=913
Summary
This PR fixes a bug where cursor-based pagination could stall or skip rows when ordering by Date columns, due to precision differences between JavaScript (milliseconds) and backends like PostgreSQL (microseconds).
The Problem
JavaScript
Dateobjects have millisecond precision, but databases like PostgreSQL store timestamps with microsecond precision. This creates a mismatch:When paginating, TanStack DB loads "all rows with the same cursor value" to avoid skipping duplicates. The previous code used exact equality (
eq), which only matched rows with exactly123.000000μs, missing rows at other microsecond offsets.The Fix
For
Datevalues, we now query for the 1ms range that the Date represents:Changes
packages/db/src/collection/subscription.ts: Added special handling forDatevalues to use range queries instead of exact equalitypackages/db/tests/query/order-by.test.ts: Added test that verifies the correct query structure is generated