Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Enable WAL for SQLite #13897

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/13897.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enable Write-Ahead Logging for SQLite installs. Contributed by [asymmetric](https://github.com/asymmetric).
4 changes: 4 additions & 0 deletions synapse/storage/engines/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def on_new_connection(self, db_conn: "LoggingDatabaseConnection") -> None:

db_conn.create_function("rank", 1, _rank)
db_conn.execute("PRAGMA foreign_keys = ON;")

# Enable WAL.
# see https://www.sqlite.org/wal.html
db_conn.execute("PRAGMA journal_mode = WAL;")
Comment on lines +91 to +94
Copy link
Member

Choose a reason for hiding this comment

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

is it correct that we do this on every new connection? According to https://www.sqlite.org/wal.html#persistence_of_wal_mode it seems like it is persisted in the database, so shouldn't it just go in a database schema migration file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, it's sufficient to do this once (although doing it every time won't cause issues). Any pointers on how to implement this as a migration?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't look like the pragma will be dumped when we do a full schema dump :

$ sqlite3 test.db
-- Loading resources from /home/dmr/.sqliterc
SQLite version 3.36.0 2021-06-18 18:36:39
Enter ".help" for usage hints.
sqlite> PRAGMA journal_mode;
journal_mode
delete
sqlite> PRAGMA journal_mode = WAL;
journal_mode
wal
sqlite> .schema
sqlite> .dump --data-only --nosys
sqlite> .dump 
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
COMMIT;
sqlite> 

So: if we effect this change via a migration, new installations won't benefit from this the next time we take a schema dump.

db_conn.commit()

def is_deadlock(self, error: Exception) -> bool:
Expand Down