Skip to content

Sqlite3Handler: do not sync to disk after each record, add wal argument - #105

Open
woutdenolf wants to merge 1 commit into
mainfrom
sqlite3-events-no-fsync
Open

woutdenolf wants to merge 1 commit into
mainfrom
sqlite3-events-no-fsync

Conversation

@woutdenolf

@woutdenolf woutdenolf commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

PR summary

Closes #104

  • Sqlite3Handler gets a wal argument. Both modes stop a commit from holding the lock during a slow disk sync.
    • wal=False (default): PRAGMA synchronous=OFF. Trade-off: an OS crash or a power loss can corrupt the database. An application crash cannot.
    • wal=True: write-ahead logging with PRAGMA synchronous=NORMAL. Crash-safe, but does not work on network file systems.

AI Disclosure

  • No AI used
  • AI tool(s) Claude Code (Claude Opus 5.5) used for the root-cause analysis, the code change and this text.

🤖 Generated with Claude Code

@woutdenolf woutdenolf self-assigned this Nov 29, 2026
@codecov

codecov Bot commented Nov 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@woutdenolf woutdenolf changed the title Sqlite3Handler: do not sync to disk after each record Sqlite3Handler: do not sync to disk after each record, add wal argument Nov 29, 2026
@woutdenolf
woutdenolf force-pushed the sqlite3-events-no-fsync branch from 7800c9f to 06d9748 Compare November 29, 2026 14:39
@woutdenolf
woutdenolf marked this pull request as ready for review November 29, 2026 14:48
@woutdenolf
woutdenolf requested a review from a team November 29, 2026 14:48
Comment on lines +75 to +82
if self._wal:
# The busy timeout does not apply to a journal mode change.
self._sql_query_with_retries(
"PRAGMA journal_mode=WAL", tuple(), conn, retry_period=0.01
)
conn.execute("PRAGMA synchronous=NORMAL")
else:
conn.execute("PRAGMA synchronous=OFF")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It looks like requesting to switch mode is not necessarily accepted / activate by passing by '_sql_query_with_retries'
    So in this case the WAL mode might not be active and move to 'NORMAL' synchronization. How much is it an issue ? I think in this case you will still met performance issue.

  2. Furthermore the wal mode seems to be store in the file and if you have several accessor you might have conflict.

This is the way AI end up fixing those two points. it which seems faire IMO (but not a sqlite3 expert):

Suggested change
if self._wal:
# The busy timeout does not apply to a journal mode change.
self._sql_query_with_retries(
"PRAGMA journal_mode=WAL", tuple(), conn, retry_period=0.01
)
conn.execute("PRAGMA synchronous=NORMAL")
else:
conn.execute("PRAGMA synchronous=OFF")
if self._wal:
# The busy timeout does not apply to a journal mode change.
self._sql_query_with_retries(
"PRAGMA journal_mode=WAL", tuple(), conn, retry_period=0.01
)
journal_mode = conn.execute("PRAGMA journal_mode").fetchone()[0]
if journal_mode == "wal":
# Protected against corruption; a power loss may only lose the last commits.
conn.execute("PRAGMA synchronous=NORMAL")
else:
# No disk waits; an OS crash or power loss can corrupt the database.
conn.execute("PRAGMA synchronous=OFF")

Note: I think switching to 'WAL' mode can still fail silently when calling _sql_query_with_retries but should be safe.

This branch has not been deployed

No deployments
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.

Sqlite3Handler drops records when the disk is slow

2 participants