Skip to content

Commit 2984727

Browse files
committed
perf: optimize SQLite PRAGMAs for WAL mode
1 parent 53caff7 commit 2984727

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

  • backend/open_webui/internal

backend/open_webui/internal/db.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,17 @@ def create_sqlcipher_connection():
9595
import sqlcipher3
9696

9797
conn = sqlcipher3.connect(db_path, check_same_thread=False)
98+
# PRAGMA key must be set before any other statements
9899
conn.execute(f"PRAGMA key = '{database_password}'")
100+
if DATABASE_ENABLE_SQLITE_WAL:
101+
conn.execute('PRAGMA journal_mode=WAL')
102+
conn.execute('PRAGMA synchronous=NORMAL')
103+
else:
104+
conn.execute('PRAGMA journal_mode=DELETE')
105+
# leave synchronous=FULL (default) for durability without WAL
106+
conn.execute('PRAGMA cache_size=-32000')
107+
conn.execute('PRAGMA temp_store=MEMORY')
108+
conn.execute('PRAGMA busy_timeout=5000')
99109
return conn
100110

101111
# The dummy "sqlite://" URL would cause SQLAlchemy to auto-select
@@ -132,8 +142,13 @@ def on_connect(dbapi_connection, connection_record):
132142
cursor = dbapi_connection.cursor()
133143
if DATABASE_ENABLE_SQLITE_WAL:
134144
cursor.execute('PRAGMA journal_mode=WAL')
145+
cursor.execute('PRAGMA synchronous=NORMAL')
135146
else:
136147
cursor.execute('PRAGMA journal_mode=DELETE')
148+
# leave synchronous=FULL (default) for durability without WAL
149+
cursor.execute('PRAGMA cache_size=-32000')
150+
cursor.execute('PRAGMA temp_store=MEMORY')
151+
cursor.execute('PRAGMA busy_timeout=5000')
137152
cursor.close()
138153

139154
event.listen(engine, 'connect', on_connect)

0 commit comments

Comments
 (0)