Skip to content

fix(duckdb): checkpoint right after WAL replay so indexes keep replayed rows - #468

Merged
HananKavitzAmat merged 2 commits into
chunkhound:mainfrom
fstrasz:fix/wal-replay-checkpoint
Nov 2, 2026
Merged

HananKavitzAmat merged 2 commits into
chunkhound:mainfrom
fstrasz:fix/wal-replay-checkpoint

Conversation

@fstrasz

@fstrasz fstrasz commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

What

_preemptive_wal_cleanup now runs CHECKPOINT straight after the ATTACH that replays the WAL, before the SELECT 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 a DETACH. The DuckDB issue is duckdb/duckdb#26106, and no release fixes it yet. A later DELETE of those rows then fails:

FATAL Error: Failed to delete all rows from index. Only deleted 0 out of N rows.

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, then DETACH), so any killed daemon or CLI leaves the next open able to corrupt the indexes. An explicit CHECKPOINT right after replay persists the indexes correctly. _handle_wal_corruption already 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

  • We hit it three times on two DuckDB indexes (2026-10-28, 2026-10-29, 2026-11-01). The failing row is a chunks row each time; the HNSW index is not involved.
  • We reproduced it on a copy of one of those indexes with DuckDB 1.5.4: replay, then implicit checkpoint, then the delete fails. With the explicit CHECKPOINT, the same delete succeeds.
  • The new test tests/unit/test_duckdb_wal_replay_index.py reproduces it without a real index. A subprocess inserts 300 rows with wal_autocheckpoint raised so nothing is checkpointed, then exits via os._exit(0), leaving a WAL. The test runs _preemptive_wal_cleanup() and deletes a replayed row.
    • The test passes with this change.
    • It fails with the original error when the CHECKPOINT line is removed.

This prevents new damage only. A file that is already damaged stays damaged. It can be repaired with the server stopped: DROP INDEX and re-CREATE every non-HNSW index from duckdb_indexes().sql, then CHECKPOINT (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 --check on the changed files: no new findings. The 9 ruff and 5 mypy findings in connection_manager.py are already on main.
  • pytest tests/unit/test_duckdb_wal_replay_index.py tests/test_smoke.py run serially: 15 passed (the new test plus 14 smoke tests). Under -n auto, test_cli_help_commands fails 7 times on this Windows machine. It fails the same way on unmodified main.

🤖 Generated with Claude Code

fstrasz and others added 2 commits November 1, 2026 12:18
…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]>
@HananKavitzAmat

Copy link
Copy Markdown
Collaborator

Looks good to me

@HananKavitzAmat
HananKavitzAmat added this pull request to the merge queue Nov 2, 2026
Merged via the queue into chunkhound:main with commit 0a735a6 Nov 2, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants