Skip to content

fix(sqlite): db_version after commit_alter on reopen, and linear orphan cleanup in commit_alter (1.2.4) - #105

Merged
andinux merged 4 commits into
mainfrom
fix/sqlite-alter-dbversion-gc
Nov 24, 2026
Merged

andinux merged 4 commits into
mainfrom
fix/sqlite-alter-dbversion-gc

Conversation

@andinux

@andinux andinux commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Two SQLite fixes in cloudsync_commit_alter, one commit each.

#103: writes from a new connection after an alter were never sent. pre_alter_dbversion is stored as TEXT, and in SQLite TEXT sorts above every INTEGER in max(). So every new connection restarted its db_version counter at the pre-alter value, and its writes reused versions a peer had already sent past.

  • The db_version query now casts the setting to INTEGER, as the PostgreSQL query already does.
  • Changes that already got a reused version are not re-sent automatically. The CHANGELOG tells affected databases to call cloudsync_network_reset_sync_version() once after upgrading.

#104: cloudsync_commit_alter was quadratic in the table's rows. The orphaned-metadata cleanup used a correlated NOT EXISTS on cloudsync_pk_encode(...), which no index can serve, so it scanned the base table once per metadata row.

  • It now uses pk NOT IN (SELECT cloudsync_pk_encode(...) FROM base WHERE ... IS NOT NULL), which builds the list of live keys once. The NULL filter keeps the result identical to the old query in every case.
  • The query is now built per backend (sql_build_gc_delete_orphaned_pk_query). The PostgreSQL query text and its arguments are unchanged.

Timings for commit_alter after ADD COLUMN (darwin-arm64):

rows × columns before after
4,000 × 8 7.8 s 57 ms
4,000 × 18 (with deletes) 16.9 s 96 ms
1,000 × 4, two-column key (with deletes) 245 ms 10 ms

In every case the metadata left after commit_alter is the same as before the change: same row count and same contents (pk, col_name, col_version, db_version, seq).

Tests: make unittest gives 163 OK, with two new tests:

  • Alter DBVersion Reopen: after an alter, a write from a new connection gets a db_version above every existing one. It fails without the cast.
  • Alter GC Orphaned PK: covers a single-column and a two-column key. Metadata of rows deleted during the alter is removed, while tombstones and the metadata of live rows are kept. It also checks that the query plan has no correlated subquery, which fails with the old query.

The PostgreSQL suite was not run locally; CI covers it.

Release 1.2.4: bumps CLOUDSYNC_VERSION and dates the CHANGELOG section.

Fixes #103
Fixes #104

🤖 Generated with Claude Code

andinux and others added 3 commits November 24, 2026 15:19
…reopen

pre_alter_dbversion is stored as TEXT, and TEXT sorts above every INTEGER
in max(), so a new connection after cloudsync_commit_alter restarted its
counter at the pre-alter value and reused versions already sent. Cast it
to INTEGER, as the PostgreSQL query already does.

Fixes #103

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The correlated NOT EXISTS compared each metadata row against
cloudsync_pk_encode() of the base table, which no index can serve, so
the base table was scanned once per metadata row. Use
pk NOT IN (SELECT cloudsync_pk_encode(...) FROM base) instead, built
once. The query is now built per backend; the PostgreSQL query is
unchanged.

Fixes #104

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@andinux andinux changed the title fix(sqlite): db_version after commit_alter on reopen, and linear orphan cleanup in commit_alter fix(sqlite): db_version after commit_alter on reopen, and linear orphan cleanup in commit_alter (1.2.4) Nov 24, 2026
@andinux
andinux merged commit e1aa40e into main Nov 24, 2026
38 checks passed
@andinux
andinux deleted the fix/sqlite-alter-dbversion-gc branch November 24, 2026 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant