Skip to content

SQLite: after cloudsync_commit_alter, writes from a new connection reuse an already-sent db_version and are never sent #103

Done

Description

@waspeer

After a wrapped schema change (cloudsync_begin_alter / cloudsync_commit_alter), every new connection restarts its db_version counter at the pre-alter value. Local writes then get a db_version at or below what a peer has already sent, so db_version > checkpoint never selects them and they are never sent. There is no error. The connection that ran the migration is fine, which is why it looks healthy right after migrating.

Seen in 1.2.2 and 1.2.3 (main is the same), linux-arm64 and darwin-arm64.

Cause

cloudsync_finalize_alter stores pre_alter_dbversion in cloudsync_settings, whose value column is TEXT, and nothing removes it afterwards. At open, SQL_DBVERSION_BUILD_QUERY (src/sqlite/sql_sqlite.c) takes max(version) over each meta table's max(db_version) and that setting:

... UNION SELECT value as version FROM cloudsync_settings WHERE key = 'pre_alter_dbversion'

In SQLite a TEXT value sorts above every INTEGER, so max() returns the stale text value however far the meta tables have moved on.

The PostgreSQL query already casts the value (SELECT value::bigint FROM cloudsync_settings ..., from #78).

Reproduction

CREATE TABLE t (id TEXT PRIMARY KEY NOT NULL, v TEXT);
SELECT cloudsync_init('t');
INSERT INTO t VALUES ('a', 'x');
INSERT INTO t VALUES ('b', 'x');

SELECT cloudsync_begin_alter('t');
ALTER TABLE t ADD COLUMN w TEXT;
SELECT cloudsync_commit_alter('t');

SELECT max(db_version) FROM cloudsync_changes;                                  -- 3: a sender's checkpoint after sending everything
SELECT value, typeof(value) FROM cloudsync_settings WHERE key = 'pre_alter_dbversion';  -- '2', text

-- close and reopen the database (any new connection, e.g. the next process), load the extension
INSERT INTO t (id, v) VALUES ('c', 'y');
SELECT DISTINCT db_version FROM cloudsync_changes WHERE pk = cloudsync_pk_encode('c');  -- 3, expected 4
SELECT count(*) FROM cloudsync_payload_chunks WHERE since_db_version = 3;       -- 0: the new row is never sent

Impact

This hits every SQLite client that follows the documented migration procedure and sends from a checkpoint. That includes cloudsync_payload_get (db_version > send_dbversion) and cloudsync_payload_chunks with a stored or explicit since_db_version. The writes stay local until the counter happens to climb past the checkpoint again, or the client resets its sync state.

Fix

Cast in the SQLite query, as the PostgreSQL one does:

... UNION SELECT CAST(value AS INTEGER) as version FROM cloudsync_settings WHERE key = 'pre_alter_dbversion'

Interim workaround we use: delete pre_alter_dbversion right after cloudsync_commit_alter, in the same transaction. We checked that a new connection's counter then stays at or past everything already sent. We covered three cases: an ADD COLUMN, a PK-changing rebuild of a table with rows, and an alter on a table whose rows had all been deleted.

Activity

  1. andinux commented on Nov 24, 2026

    @andinux
    Collaborator

    Thanks for the detailed report and the fix. It's confirmed and fixed in 1.2.4: the SQLite query now casts pre_alter_dbversion to INTEGER, as PostgreSQL does, so a new connection's db_version counter continues past every existing change. Until you upgrade, your workaround (deleting pre_alter_dbversion right after cloudsync_commit_alter, in the same transaction) remains valid. If a database already wrote from a new connection after an alter, the fix won't resend those changes on its own: call cloudsync_network_reset_sync_version() once after upgrading.

  2. added a commit that references this issue on Nov 25, 2026
    e1aa40e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions