From 06c7f62ee14b7ad39163d87cdd6105a3127618c8 Mon Sep 17 00:00:00 2001 From: Abdul Basit <45506001+imabdulbasit@users.noreply.github.com> Date: Fri, 28 Feb 2025 04:34:58 +0500 Subject: [PATCH] vacuum sqlite after pruning data (#2697) * vaccum sqlite database * use pruned height table for opening sqlite write txn * move vacuum after each batch delete --- .../src/data_source/storage/sql.rs | 14 ++++++++++++++ .../src/data_source/storage/sql/transaction.rs | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/hotshot-query-service/src/data_source/storage/sql.rs b/hotshot-query-service/src/data_source/storage/sql.rs index 01149a62d1..3bb20aa98a 100644 --- a/hotshot-query-service/src/data_source/storage/sql.rs +++ b/hotshot-query-service/src/data_source/storage/sql.rs @@ -731,6 +731,13 @@ impl PruneStorage for SqlStorage { } } + #[cfg(feature = "embedded-db")] + { + let mut conn = self.pool().acquire().await?; + query("VACUUM").execute(conn.as_mut()).await?; + conn.close().await?; + } + // If threshold is set, prune data exceeding minimum retention in batches // This parameter is needed for SQL storage as there is no direct way to get free space. if let Some(threshold) = cfg.pruning_threshold() { @@ -765,6 +772,13 @@ impl PruneStorage for SqlStorage { message: format!("failed to commit {e}"), })?; + #[cfg(feature = "embedded-db")] + { + let mut conn = self.pool().acquire().await?; + query("VACUUM").execute(conn.as_mut()).await?; + conn.close().await?; + } + pruner.pruned_height = Some(height); return Ok(Some(height)); diff --git a/hotshot-query-service/src/data_source/storage/sql/transaction.rs b/hotshot-query-service/src/data_source/storage/sql/transaction.rs index 5eb3c731b8..efeea128e6 100644 --- a/hotshot-query-service/src/data_source/storage/sql/transaction.rs +++ b/hotshot-query-service/src/data_source/storage/sql/transaction.rs @@ -124,7 +124,7 @@ impl TransactionMode for Write { // statement that has no actual effect on the database is suitable for this purpose, hence // the `WHERE false`. #[cfg(feature = "embedded-db")] - conn.execute("UPDATE header SET height = height WHERE false") + conn.execute("UPDATE pruned_height SET id = id WHERE false") .await?; // With Postgres things are much more straightforward: just tell Postgres we want a write