Skip to content

Commit e81b946

Browse files
committed
Patch Doltlite mergeScan to skip tree entries covered by a mutmap DELETE
prollyBtCursorIndexMoveto can reposition the merge cursor directly at a mutmap INSERT entry via setCursorToMutMapEntryPhys, which jumps mmIdx past any mutmap entries that sort earlier. A subsequent mergeScan iteration that sees `cmp*dir < 0` — the tree entry is ahead of mutmap[mmIdx] — then emits the tree row without noticing that the mutmap holds a DELETE for that tree key at an earlier order index. The tree row is logically deleted but the scan walks over it anyway; SQLite eventually follows its rowid and hits the deleted slot, tripping the "database disk image is malformed" check in sqlite3VdbeFinishMoveto. Reproduces on an INSERT + DELETE + UPDATE sequence inside one transaction: the INSERT fills the mutmap, the DELETE adds a tombstone for an earlier row, the UPDATE's scan emits that earlier row from the tree, then crashes. Patch consults the mutmap before emitting a tree entry; if a DELETE exists for that key, skip and advance the tree cursor. No behavior change in the common case (no matching mutmap entry), constant- factor overhead otherwise. Checked in as a .patch file.
1 parent f98745a commit e81b946

2 files changed

Lines changed: 90 additions & 1 deletion

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Check mutmap for tree-key DELETE when emitting a tree entry in mergeScan
2+
3+
prollyBtCursorIndexMoveto can position the merge cursor directly at a
4+
mutmap INSERT entry via setCursorToMutMapEntryPhys, which jumps mmIdx
5+
past any sort-earlier mutmap entries. The subsequent mergeScan
6+
iteration, on seeing `cmp*dir < 0` (tree entry ahead of mutmap[mmIdx]),
7+
emits the tree entry without noticing that the mutmap contains a
8+
DELETE for that tree key at an earlier order index. The tree key was
9+
logically deleted but stays visible to the scan — SQLite then follows
10+
its rowid and calls TableMoveto to a rowid that mutmap says is
11+
deleted, which hits the "database disk image is malformed" check in
12+
sqlite3VdbeFinishMoveto.
13+
14+
Fix: when about to emit a tree entry, look up the current tree key in
15+
the mutmap; if a DELETE record exists, skip the tree entry and advance
16+
the tree cursor. No behavior change in the common case (no matching
17+
mutmap entry → emit tree as before), constant-factor overhead
18+
otherwise.
19+
20+
Reproduces deterministically on:
21+
22+
CREATE TABLE c (ka TEXT, kb TEXT, kc TEXT, v TEXT,
23+
PRIMARY KEY (ka, kb, kc));
24+
INSERT INTO c VALUES ('db','t','a','');
25+
BEGIN IMMEDIATE;
26+
INSERT INTO c VALUES ('db','t','b','');
27+
DELETE FROM c WHERE ka='db' AND kb='t' AND kc='a';
28+
UPDATE c SET v = 'x' WHERE ka='db' AND kb='t'; -- malformed w/o fix
29+
30+
--- a/src/prolly_btree.c
31+
+++ b/src/prolly_btree.c
32+
@@ -3631,6 +3631,29 @@
33+
}
34+
cmp = mergeCompare(pCur, e);
35+
if( cmp*dir < 0 ){
36+
+ /* Tree entry is ahead of mutmap[mmIdx] in scan direction.
37+
+ ** Check whether the mutmap has a DELETE entry for the tree
38+
+ ** key at an order index the iteration has already walked
39+
+ ** past (e.g. after setCursorToMutMapEntryPhys jumped mmIdx
40+
+ ** directly to a later INSERT). Without this check the scan
41+
+ ** would emit a logically-deleted tree row and SQLite would
42+
+ ** later TableMoveto a rowid that mutmap says is gone. */
43+
+ ProllyMutMapEntry *delE = 0;
44+
+ int delRc;
45+
+ if( pCur->curIntKey ){
46+
+ delRc = prollyMutMapFindRc(pCur->pMutMap, 0, 0,
47+
+ prollyCursorIntKey(&pCur->pCur), &delE);
48+
+ }else{
49+
+ const u8 *pK; int nK;
50+
+ prollyCursorKey(&pCur->pCur, &pK, &nK);
51+
+ delRc = prollyMutMapFindRc(pCur->pMutMap, pK, nK, 0, &delE);
52+
+ }
53+
+ if( delRc!=SQLITE_OK ) return delRc;
54+
+ if( delE && delE->op==PROLLY_EDIT_DELETE ){
55+
+ int advRc = advanceTreeCursor(pCur, dir);
56+
+ if( advRc!=SQLITE_OK ) return advRc;
57+
+ continue;
58+
+ }
59+
pCur->mergeSrc = MERGE_SRC_TREE;
60+
if( pRes ) *pRes = 0;
61+
return SQLITE_OK;

‎.github/workflows/phpunit-tests-doltlite.yml‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ jobs:
5050
# seek_not_found, skipping the actual delete. The patch
5151
# saves eqSeen before findMatchingMutMapEntry and restores
5252
# it when no mutmap entry overrides the tree result.
53+
#
54+
# 3) src/prolly_btree.c — in mergeScan, when emitting a tree
55+
# entry check the mutmap for a DELETE on that tree key; if
56+
# found, skip and advance. Without this, INSERT+DELETE+UPDATE
57+
# in one transaction trips "database disk image is malformed".
5358
run: |
5459
BUILD=doltlite-src/src/build.c
5560
MOVETO=doltlite-src/src/prolly_btree.c
@@ -65,8 +70,12 @@ jobs:
6570
patch -d doltlite-src -p1 --no-backup-if-mismatch \
6671
< .github/doltlite-patches/eqSeen-preservation.patch
6772
73+
# --- Patch 3: check mutmap for tree-key DELETE in mergeScan ---
74+
patch -d doltlite-src -p1 --no-backup-if-mismatch \
75+
< .github/doltlite-patches/mergeScan-check-tree-delete.patch
76+
6877
echo "--- Patched lines ---"
69-
grep -n 'patched off in CI\|savedEqSeen' "$BUILD" "$MOVETO"
78+
grep -n 'patched off in CI\|savedEqSeen\|walked past' "$BUILD" "$MOVETO"
7079
7180
- name: Build Doltlite shared library
7281
run: |
@@ -155,6 +164,25 @@ jobs:
155164
echo "::error::eqSeen-preservation patch did not take effect: expected only 'id', got '${REMAINING}'"
156165
exit 1
157166
fi
167+
# 3) mergeScan patch: UPDATE inside a transaction after an
168+
# INSERT + DELETE no longer trips "database disk image is
169+
# malformed".
170+
VAL=$(php -r '
171+
$db = new PDO("sqlite::memory:");
172+
$db->exec("CREATE TABLE c (ka TEXT, kb TEXT, kc TEXT, v TEXT, PRIMARY KEY (ka, kb, kc))");
173+
$db->exec("INSERT INTO c VALUES (\"db\", \"t\", \"a\", \"\")");
174+
$db->exec("BEGIN IMMEDIATE");
175+
$db->exec("INSERT INTO c VALUES (\"db\", \"t\", \"b\", \"\")");
176+
$db->exec("DELETE FROM c WHERE ka = \"db\" AND kb = \"t\" AND kc = \"a\"");
177+
$db->exec("UPDATE c SET v = \"x\" WHERE ka = \"db\" AND kb = \"t\"");
178+
$db->exec("COMMIT");
179+
echo $db->query("SELECT v FROM c")->fetch()[0];
180+
' 2>&1)
181+
echo "post-UPDATE value = ${VAL}"
182+
if [ "${VAL}" != "x" ]; then
183+
echo "::error::mergeScan patch did not take effect: expected 'x', got '${VAL}'"
184+
exit 1
185+
fi
158186
159187
- name: Install Composer dependencies (root)
160188
uses: ramsey/composer-install@v3

0 commit comments

Comments
 (0)