From c52b48b0f5105c6036480248e9e833b37f17fa87 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sun, 17 Apr 2022 22:14:32 +0000 Subject: [PATCH 1/2] sql: update version first in migration transactions In execute_migration transaction first update the version, and only then execute the rest of the migration. This ensures that transaction is immediately recognized as write transaction and there is no need to promote it from read transaction to write transaction later, e.g. in the case of "DROP TABLE IF EXISTS" that is a read only operation if the table does not exist. Promoting a read transaction to write transaction may result in an error if database is locked. --- src/sql/migrations.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sql/migrations.rs b/src/sql/migrations.rs index 92f1c95516..02f13ecf32 100644 --- a/src/sql/migrations.rs +++ b/src/sql/migrations.rs @@ -723,14 +723,14 @@ impl Sql { async fn execute_migration(&self, query: &'static str, version: i32) -> Result<()> { self.transaction(move |transaction| { - transaction.execute_batch(query)?; - // set raw config inside the transaction transaction.execute( "UPDATE config SET value=? WHERE keyname=?;", paramsv![format!("{version}"), VERSION_CFG], )?; + transaction.execute_batch(query)?; + Ok(()) }) .await From 7d2cca8633dc37f479900e96b03b108b8c143957 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sun, 17 Apr 2022 22:16:16 +0000 Subject: [PATCH 2/2] sql: enable auto_vacuum on all connections --- CHANGELOG.md | 1 + src/sql.rs | 31 +++++++++++-------------------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49d5e3c35f..f3781b5d3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Fixes - deltachat-rpc-server: do not block stdin while processing the request. #4041 deltachat-rpc-server now reads the next request as soon as previous request handler is spawned. +- enable `auto_vacuum` on all SQL connections #2955 ### API-Changes diff --git a/src/sql.rs b/src/sql.rs index c01322c8b0..4fcb90ee93 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -213,6 +213,17 @@ impl Sql { Duration::from_secs(10).as_millis() ))?; c.pragma_update(None, "key", passphrase.clone())?; + // Try to enable auto_vacuum. This will only be + // applied if the database is new or after successful + // VACUUM, which usually happens before backup export. + // When auto_vacuum is INCREMENTAL, it is possible to + // use PRAGMA incremental_vacuum to return unused + // database pages to the filesystem. + c.pragma_update(None, "auto_vacuum", "INCREMENTAL".to_string())?; + + c.pragma_update(None, "journal_mode", "WAL".to_string())?; + // Default synchronous=FULL is much slower. NORMAL is sufficient for WAL mode. + c.pragma_update(None, "synchronous", "NORMAL".to_string())?; Ok(()) }); @@ -228,26 +239,6 @@ impl Sql { async fn try_open(&self, context: &Context, dbfile: &Path, passphrase: String) -> Result<()> { *self.pool.write().await = Some(Self::new_pool(dbfile, passphrase.to_string())?); - { - let conn = self.get_conn().await?; - tokio::task::block_in_place(move || -> Result<()> { - // Try to enable auto_vacuum. This will only be - // applied if the database is new or after successful - // VACUUM, which usually happens before backup export. - // When auto_vacuum is INCREMENTAL, it is possible to - // use PRAGMA incremental_vacuum to return unused - // database pages to the filesystem. - conn.pragma_update(None, "auto_vacuum", "INCREMENTAL".to_string())?; - - // journal_mode is persisted, it is sufficient to change it only for one handle. - conn.pragma_update(None, "journal_mode", "WAL".to_string())?; - - // Default synchronous=FULL is much slower. NORMAL is sufficient for WAL mode. - conn.pragma_update(None, "synchronous", "NORMAL".to_string())?; - Ok(()) - })?; - } - self.run_migrations(context).await?; Ok(())