electric-db-collection: support for snapshots in awaitTxid#648
electric-db-collection: support for snapshots in awaitTxid#648
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
|
| 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.
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.
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.
iiuc this subscription is racing against the seenTxidssubscription. The first one to find the txId will resolve the promise.
Summary
This PR enhances the
awaitTxIdutility in@tanstack/electric-db-collectionto resolve transaction IDs based on PostgreSQL snapshot metadata fromsnapshot-endmessages, 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-endcontrol message with PostgreSQL snapshot metadata (xmin, xmax, xip_list). Previously,awaitTxIdcould 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):seenSnapshotsstore to track PostgreSQL snapshots alongsideseenTxidsisSnapshotEndMessageandparseSnapshotMessagehelper functionsawaitTxIdto check transaction visibility against stored snapshots usingisVisibleInSnapshotfrom@electric-sql/clientsnapshot-endcontrol messagesTests (
electric.test.ts):Dependencies
@electric-sql/clientfrom^1.0.10to^1.0.14to access theisVisibleInSnapshotutility andPostgresSnapshottypeFuture 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).