Repository navigation
fix(duckdb): checkpoint right after WAL replay so indexes keep replayed rows - #468
Merged
HananKavitzAmat merged 2 commits intoNov 2, 2026
Merged
Conversation
…ed rows On DuckDB 1.4-1.5, the first implicit checkpoint after a WAL replay (a close or DETACH) persists secondary ART indexes without the replayed rows (duckdb/duckdb#26106; no released fix). A later delete of those rows then fails "FATAL: Failed to delete all rows from index", the damage survives restarts, and the scan stops. _preemptive_wal_cleanup did exactly that sequence on every open after an unclean exit: ATTACH (replay), SELECT 1, DETACH. It now runs an explicit CHECKPOINT straight after the ATTACH, which persists the indexes correctly (the same thing _handle_wal_corruption already did). Seen three times on one index (2026-10-28, -10-29 on a second index, 2026-11-01); reproduced on a copy of that index with DuckDB 1.5.4. A file already damaged is repaired by DROP + CREATE of its secondary indexes (0.3-1.4 s on 16k chunks); this change prevents new damage only. Test: a subprocess writes rows and dies without a checkpoint, the validation runs, the delete succeeds. Fails with the original error when the CHECKPOINT line is removed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
|
Looks good to me |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
_preemptive_wal_cleanupnow runsCHECKPOINTstraight after theATTACHthat replays the WAL, before theSELECT 1/DETACH. That is one line plus a regression test.Why
On DuckDB 1.4–1.5 (we pin
duckdb==1.5.4), the first implicit checkpoint after a WAL replay persists the secondary ART indexes without the replayed rows. That checkpoint can come from a close or aDETACH. The DuckDB issue is duckdb/duckdb#26106, and no release fixes it yet. A laterDELETEof those rows then fails:The database is then invalidated for the rest of the process. The damage is in the file, so it survives a restart.
ChunkHound's WAL validation runs this exact sequence on every open after an unclean exit (ATTACH replays the WAL, then
SELECT 1, thenDETACH), so any killed daemon or CLI leaves the next open able to corrupt the indexes. An explicitCHECKPOINTright after replay persists the indexes correctly._handle_wal_corruptionalready does this. It was only missing from the validation path.This matches the symptom in #222 ("Failed to delete all rows from index" during orphan cleanup, a delete of rows written before an unclean exit). It is likely its root cause, though I have only reproduced it on our indexes, not on the reporter's.
Evidence
chunksrow each time; the HNSW index is not involved.CHECKPOINT, the same delete succeeds.tests/unit/test_duckdb_wal_replay_index.pyreproduces it without a real index. A subprocess inserts 300 rows withwal_autocheckpointraised so nothing is checkpointed, then exits viaos._exit(0), leaving a WAL. The test runs_preemptive_wal_cleanup()and deletes a replayed row.CHECKPOINTline is removed.This prevents new damage only. A file that is already damaged stays damaged. It can be repaired with the server stopped:
DROP INDEXand re-CREATEevery non-HNSW index fromduckdb_indexes().sql, thenCHECKPOINT(0.3–1.4 s on 16k chunks). I left that out of this PR to keep it minimal. I can add it as a repair step if wanted.Related
Checks
ruff check/ruff format --checkon the changed files: no new findings. The 9ruffand 5mypyfindings inconnection_manager.pyare already onmain.pytest tests/unit/test_duckdb_wal_replay_index.py tests/test_smoke.pyrun serially: 15 passed (the new test plus 14 smoke tests). Under-n auto,test_cli_help_commandsfails 7 times on this Windows machine. It fails the same way on unmodifiedmain.🤖 Generated with Claude Code