mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-26 12:34:22 +01:00
## What The following sequence of actions is possible: ```sql -- TRUNCATE checkpoint fails during WAL restart, -- but OngoingCheckpoint.state is still left at Done for conn 0 Connection 0(op=23): PRAGMA wal_checkpoint(TRUNCATE) Connection 0(op=23) Checkpoint TRUNCATE: OK: false, wal_page_count: NULL, checkpointed_count: NULL -- TRUNCATE checkpoint succeeds for conn 1 Connection 1(op=26): PRAGMA wal_checkpoint(TRUNCATE) Connection 1(op=26) Checkpoint TRUNCATE: OK: true, wal_page_count: 0, checkpointed_count: 0 -- Conn 0 now does a PASSIVE checkpoint, and immediately thinks -- it's in the Done state, and thinks it checkpointed 17 frames. -- since mode is PASSIVE, it now thinks both the WAL and the DB have those 17 frames -- so the first 17 frames of the WAL can be ignored from now on. Connection 0(op=27): PRAGMA wal_checkpoint(PASSIVE) Connection 0(op=27) Checkpoint PASSIVE: OK: true, wal_page_count: 0, checkpointed_count: 17 -- Connection 0 starts a txn with min=18 (ignore first 17 frames in WAL), -- and deletes rowid=690, which becomes WAL frame number 1 Connection 0(op=28): DELETE FROM test_table WHERE id = 690 begin_read_tx(min=18, max=0, slot=1, max_frame_in_wal=0) -- Connection 1 starts a txn with min=18 (ignore first 17 frames in WAL), -- and inserts rowid=1128, which becomes WAL frame number 2 Connection 1(op=28): INSERT INTO test_table (id, text) VALUES (1128, text_560) begin_read_tx(min=18, max=1, slot=1, max_frame_in_wal=1) -- Connection 0 again starts tx with min=18, and performs a read, and two wrong things happen: -- 1. it doesn't see row 690 as deleted, because it's in WAL frame 1, which it ignores -- 2. it doesn't see the new row 1128, because it's in WAL frame 2, which it ignores Connection 0(op=29): SELECT * FROM test_table begin_read_tx(min=18, max=2, slot=1, max_frame_in_wal=2) ``` ## Fix Reset `ongoing_checkpoint.state` to `Start` when checkpoint fails. Issue found in #2364 . Reviewed-by: bit-aloo (@Shourya742) Closes #2380