Skip to content

Conversation

KyleAMathews
Copy link
Collaborator

@KyleAMathews KyleAMathews commented Jul 9, 2025

Fix #246

After some more research, this is the best way to get the 32bit internal xid (tx id) that is also sent out over logical replication and then through Electric i.e. what we'll match against inside Electric collections.

async function generateTxId(tx: any): Promise<Txid> {
  // The ::xid cast strips off the epoch, giving you the raw 32-bit value
  // that matches what PostgreSQL sends in logical replication streams
  // (and then exposed through Electric which we'll match against
  // in the client).
  const result = await tx`SELECT pg_current_xact_id()::xid::text as txid`
  const txid = result[0]?.txid

  if (txid === undefined) {
    throw new Error(`Failed to get transaction ID`)
  }

  return parseInt(txid, 10)
}

@changeset-bot
Copy link

changeset-bot bot commented Jul 9, 2025

🦋 Changeset detected

Latest commit: 75a86ed

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@tanstack/db-collections Patch
@tanstack/db-example-react-todo Patch
@tanstack/db Patch
@tanstack/react-db Patch
@tanstack/vue-db Patch

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jul 9, 2025

@tanstack/db-example-react-todo

npm i https://pkg.pr.new/@tanstack/db@245
npm i https://pkg.pr.new/@tanstack/db-collections@245
npm i https://pkg.pr.new/@tanstack/react-db@245
npm i https://pkg.pr.new/@tanstack/vue-db@245

commit: 85850f4

@github-actions
Copy link
Contributor

github-actions bot commented Jul 9, 2025

Size Change: 0 B

Total Size: 31.4 kB

ℹ️ View Unchanged
Filename Size
./packages/db/dist/esm/collection.js 8.23 kB
./packages/db/dist/esm/deferred.js 230 B
./packages/db/dist/esm/errors.js 150 B
./packages/db/dist/esm/index.js 528 B
./packages/db/dist/esm/optimistic-action.js 294 B
./packages/db/dist/esm/proxy.js 3.75 kB
./packages/db/dist/esm/query/builder/functions.js 531 B
./packages/db/dist/esm/query/builder/index.js 3.4 kB
./packages/db/dist/esm/query/builder/ref-proxy.js 842 B
./packages/db/dist/esm/query/compiler/evaluators.js 1.34 kB
./packages/db/dist/esm/query/compiler/group-by.js 2.09 kB
./packages/db/dist/esm/query/compiler/index.js 1.42 kB
./packages/db/dist/esm/query/compiler/joins.js 1.15 kB
./packages/db/dist/esm/query/compiler/order-by.js 933 B
./packages/db/dist/esm/query/compiler/select.js 657 B
./packages/db/dist/esm/query/ir.js 318 B
./packages/db/dist/esm/query/live-query-collection.js 2.06 kB
./packages/db/dist/esm/SortedMap.js 1.24 kB
./packages/db/dist/esm/transactions.js 2.29 kB

compressed-size-action::db-package-size

@github-actions
Copy link
Contributor

github-actions bot commented Jul 9, 2025

Size Change: 0 B

Total Size: 1.05 kB

ℹ️ View Unchanged
Filename Size
./packages/react-db/dist/esm/index.js 152 B
./packages/react-db/dist/esm/useLiveQuery.js 902 B

compressed-size-action::react-db-package-size

@KyleAMathews KyleAMathews changed the title Add debug for logging txid activity in electric collections Use numbers for txid Jul 9, 2025
@KyleAMathews KyleAMathews changed the title Use numbers for txid [Electric]: Use numbers for txid Jul 9, 2025
@KyleAMathews KyleAMathews requested a review from samwillis July 9, 2025 20:06
Copy link
Contributor

@kevin-dp kevin-dp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
Note how much easier this would have been if we had used a type alias type TxId = number. Perhaps this is a good time to introduce it?

}

return String(txid)
return parseInt(txid, 10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps good to check that the parsed int is not NaN? Just to safeguard against that even though it doesn't look very plausible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah not sure pg could return an unparsable number? Though I did some more research and txid_current() is actually deprecated. But with the new function, we can cast it to xid then to text and then parse it in js to get the 32bit internal xid that's also on the logical replication:

async function generateTxId(tx: any): Promise<Txid> {
  // The ::xid cast strips off the epoch, giving you the raw 32-bit value
  // that matches what PostgreSQL sends in logical replication streams
  // (and then exposed through Electric which we'll match against
  // in the client).
  const result = await tx`SELECT pg_current_xact_id()::xid::text as txid`
  const txid = result[0]?.txid

  if (txid === undefined) {
    throw new Error(`Failed to get transaction ID`)
  }

  return parseInt(txid, 10)
}

@KyleAMathews KyleAMathews merged commit 9f0b0c2 into main Jul 10, 2025
4 checks passed
@KyleAMathews KyleAMathews deleted the debug branch July 10, 2025 14:44
@github-actions github-actions bot mentioned this pull request Jul 10, 2025
cursor bot pushed a commit that referenced this pull request Jul 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[electric]: txid is a 32 bit number so we don't need to stringify it

2 participants