Skip to content

SQLite: cloudsync_commit_alter is quadratic in the table's rows (orphan-metadata GC cannot use an index) #104

Done

Description

@waspeer

cloudsync_commit_alter takes time quadratic in the table's row count, and it holds the write lock for all of it. A single ADD COLUMN on 4,000 rows takes 10–20 s, depending on the column count. When the rows double, the time goes up about 4×:

rows × columns commit_alter
1,000 × 18 1.9 s
2,000 × 18 5.1 s
4,000 × 18 20.2 s
4,000 × 4 3.4 s

These are 1.2.3 numbers on linux-arm64. 1.2.2 gives the same. darwin-arm64 shows the same shape, about 4× faster. begin_alter and the ALTER itself take 0–1 ms.

Cause

cloudsync_finalize_alter runs SQL_CLOUDSYNC_GC_DELETE_ORPHANED_PK (src/sqlite/sql_sqlite.c):

DELETE FROM "t_cloudsync"
WHERE ("col_name" != '__[RIP]__' OR ("col_name" = '__[RIP]__' AND col_version % 2 != 0))
  AND NOT EXISTS (SELECT 1 FROM "t" WHERE "t_cloudsync".pk = cloudsync_pk_encode("t"."id") LIMIT 1);

The function call on the base-table side means no index can be used:

SCAN t_cloudsync
CORRELATED SCALAR SUBQUERY 1
SCAN t

The base table is therefore scanned once per metadata row, which means (rows × columns) × rows work.

Reproduction

CREATE TABLE t (id TEXT PRIMARY KEY NOT NULL, c1 TEXT, c2 TEXT, c3 TEXT, c4 TEXT, c5 TEXT, c6 TEXT, c7 TEXT, c8 TEXT);
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 4000)
  INSERT INTO t SELECT 'r' || i, 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v' FROM n;
SELECT cloudsync_init('t');

SELECT cloudsync_begin_alter('t');
ALTER TABLE t ADD COLUMN extra TEXT;
SELECT cloudsync_commit_alter('t');   -- 10.5 s here

Fix

Build the set of live keys once instead of probing per row:

DELETE FROM "t_cloudsync"
WHERE ("col_name" != '__[RIP]__' OR ("col_name" = '__[RIP]__' AND col_version % 2 != 0))
  AND pk NOT IN (SELECT cloudsync_pk_encode("t"."id") FROM "t");

SQLite evaluates the list once, with a bloom filter. On the 4,000 × 18 case (10% of rows deleted) the delete took 25 ms instead of 16.0 s, deleting the same rows. NOT IN is safe here because PK values are never NULL, so cloudsync_pk_encode never returns NULL.

Interim workaround that needs no new build: create a temporary expression index CREATE INDEX tmp_pk ON t (cloudsync_pk_encode(id)) (PK columns in PK order) before cloudsync_begin_alter, and drop it after cloudsync_commit_alter, in the same transaction. On the 4,000 × 18 case this took the migration from 19.7 s to 0.16 s. The schema hash was the same, and the metadata was byte-identical to a migration without the index, including on a table with deleted rows and orphaned metadata.

Activity

  1. andinux commented on Nov 24, 2026

    @andinux
    Collaborator

    Thanks for the analysis and the timings. Fixed in 1.2.4 with the NOT IN form you proposed, plus a filter that skips NULL keys. On 4,000 rows × 8 columns, commit_alter drops from about 8 s to about 50 ms here, and it removes exactly the same metadata rows, including on tables with deleted rows and composite keys. Until you upgrade, the temporary expression index remains a valid workaround.

  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