-
Notifications
You must be signed in to change notification settings - Fork 100
electric-db-collection: support for snapshots in awaitTxid #648
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: 2c0a5c2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 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/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: 0 B Total Size: 75.2 kB ℹ️ View Unchanged
|
Size Change: 0 B Total Size: 1.47 kB ℹ️ View Unchanged
|
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.
LGTM, left a few nits.
if (hasTxid) return true | ||
|
||
// Then check if the txid is in any of the seen snapshots | ||
const hasSnapshot = seenSnapshots.state.some((snapshot) => |
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.
How big are we envisioning this seenSnapshots
list could become? Worst case we're looping over all snapshots and for each snapshot we check isVisibleInSnapshot
which in the worst case loop over the snapshot's xip_list
. So i'm wondering here if this could become a performance issue. If it is, we could as well keep a set of all txIds we've seen in snapshots and then we could just do a set lookup (which is in constant time).
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.
Initially it should only be one, the initial snapshot, but with incremental sync it could grow. I intend to revisit both this and the seenTxids in a future PR
} | ||
}) | ||
|
||
const unsubscribeSeenSnapshots = seenSnapshots.subscribe(() => { |
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.
iiuc this subscription is racing against the seenTxids
subscription. The first one to find the txId will resolve the promise.
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.
Yes
Summary
This PR enhances the
awaitTxId
utility in@tanstack/electric-db-collection
to resolve transaction IDs based on PostgreSQL snapshot metadata fromsnapshot-end
messages, enabling transaction matching on the initial snapshot at the start of a new shape.Motivation
When an Electric shape starts, the initial snapshot includes a
snapshot-end
control message with PostgreSQL snapshot metadata (xmin, xmax, xip_list). Previously,awaitTxId
could only match against explicit txid arrays sent with individual change messages. This meant that transactions completed before the snapshot started couldn't be matched, even though they were visible in the snapshot data.By supporting snapshot-based visibility checks, we can now correctly identify which transactions are included in the initial snapshot, improving transaction confirmation reliability.
Changes
Implementation (
electric.ts
):seenSnapshots
store to track PostgreSQL snapshots alongsideseenTxids
isSnapshotEndMessage
andparseSnapshotMessage
helper functionsawaitTxId
to check transaction visibility against stored snapshots usingisVisibleInSnapshot
from@electric-sql/client
snapshot-end
control messagesTests (
electric.test.ts
):Dependencies
@electric-sql/client
from^1.0.10
to^1.0.14
to access theisVisibleInSnapshot
utility andPostgresSnapshot
typeFuture Work
This implementation currently handles snapshots at the beginning of shape logs. Upcoming PRs for incremental sync will extend this to handle multiple snapshots over time and may introduce snapshot retention policies (they are kept unbounded for now).