You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SQLite: cloudsync_commit_alter is quadratic in the table's rows (orphan-metadata GC cannot use an index) #104
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.
DELETEFROM"t_cloudsync"WHERE ("col_name"!='__[RIP]__'OR ("col_name"='__[RIP]__'AND col_version % 2!=0))
AND NOT EXISTS (SELECT1FROM"t"WHERE"t_cloudsync".pk = cloudsync_pk_encode("t"."id") LIMIT1);
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
CREATETABLEt (id TEXTPRIMARY KEYNOT NULL, c1 TEXT, c2 TEXT, c3 TEXT, c4 TEXT, c5 TEXT, c6 TEXT, c7 TEXT, c8 TEXT);
WITH RECURSIVE n(i) AS (SELECT1UNION ALLSELECT i +1FROM 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');
ALTERTABLE 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:
DELETEFROM"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.
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.
Activity
andinux commentedon Nov 24, 2026
Thanks for the analysis and the timings. Fixed in 1.2.4 with the
NOT INform you proposed, plus a filter that skips NULL keys. On 4,000 rows × 8 columns,commit_alterdrops 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.fix(sqlite): db_version after commit_alter on reopen, and linear orph…