Skip to content

Conversation

@stevensJourney
Copy link
Collaborator

Overview

When using triggers (e.g., database.triggers.createDiffTrigger()), the updateLocalTarget() method could fail to read the ps_crud sequence value, causing sync to be blocked after performing uploads.

Root Cause

The trigger system creates temporary tables and triggers (CREATE TEMP TABLE and CREATE TEMP TRIGGER) in the temp schema on the connection. When these temporary objects exist, SQLite's schema resolution mechanism prioritizes the temp schema when resolving unqualified table names. This means that queries to sqlite_sequence without an explicit schema qualifier could resolve to temp.sqlite_sequence instead of main.sqlite_sequence, returning no rows even though the sequence was actually advancing correctly.

Solution

Explicitly qualify sqlite_sequence queries with the main. schema prefix in SqliteBucketStorage.updateLocalTarget():

  • Changed SELECT seq FROM sqlite_sequenceSELECT seq FROM main.sqlite_sequence

This ensures we always query the main database's sequence table, regardless of temporary schema context.

closes #778

@changeset-bot
Copy link

changeset-bot bot commented Nov 24, 2025

🦋 Changeset detected

Latest commit: f3ea146

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

This PR includes changesets to release 8 packages
Name Type
@powersync/common Patch
@powersync/adapter-sql-js Patch
@powersync/node Patch
@powersync/op-sqlite Patch
@powersync/react-native Patch
@powersync/tanstack-react-query Patch
@powersync/web Patch
@powersync/diagnostics-app 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

Copy link
Contributor

@rkistner rkistner left a comment

Choose a reason for hiding this comment

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

Great find! I would never have guessed this could be the cause.

Coukd there be any other places in our codebase affected by this type of issue? I.e. any other queries where we should add the main prefix to be safe?

@stevensJourney
Copy link
Collaborator Author

Great find! I would never have guessed this could be the cause.

Coukd there be any other places in our codebase affected by this type of issue? I.e. any other queries where we should add the main prefix to be safe?

This certainly had me scratching my head for a while before I found it.

The only other thing in this codebase I could think of are the queries to sqlite_master here and here - however in testing it seems like querying sqlite_master includes the data for the main database.
There might be some cases in the Rust core - I'm not very familiar with that codebase though, I could not see any glaring issues at a glance.

@stevensJourney stevensJourney marked this pull request as ready for review November 24, 2025 10:06
@stevensJourney stevensJourney merged commit 9b82867 into main Nov 24, 2025
11 of 15 checks passed
@stevensJourney stevensJourney deleted the sqlite-sequence branch November 24, 2025 10:06
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.

SQLite Sequence Error on Update/Delete Mutations: 'SQLite Sequence should not be empty'

3 participants