From c818be362aef9244bb644a2c50a620ac1ef077b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Grze=C5=9Bkiewicz?= Date: Fri, 12 Apr 2024 15:13:21 +0200 Subject: [PATCH] feat(en): add manual vacuum step in db pruning (#1652) Signed-off-by: tomg10 --- ...dd70e04bd6e714c8b763efdfe0259a5dd4128.json | 12 ++++++++++ core/lib/dal/src/pruning_dal.rs | 23 ++++++++++++++++++- core/lib/zksync_core/src/db_pruner/mod.rs | 6 +++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 core/lib/dal/.sqlx/query-b66045a20fd41f44627e5f1360cdd70e04bd6e714c8b763efdfe0259a5dd4128.json diff --git a/core/lib/dal/.sqlx/query-b66045a20fd41f44627e5f1360cdd70e04bd6e714c8b763efdfe0259a5dd4128.json b/core/lib/dal/.sqlx/query-b66045a20fd41f44627e5f1360cdd70e04bd6e714c8b763efdfe0259a5dd4128.json new file mode 100644 index 00000000000..93f485f255f --- /dev/null +++ b/core/lib/dal/.sqlx/query-b66045a20fd41f44627e5f1360cdd70e04bd6e714c8b763efdfe0259a5dd4128.json @@ -0,0 +1,12 @@ +{ + "db_name": "PostgreSQL", + "query": "\n VACUUM l1_batches,\n miniblocks,\n storage_logs,\n events,\n call_traces,\n l2_to_l1_logs,\n transactions\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [] + }, + "nullable": [] + }, + "hash": "b66045a20fd41f44627e5f1360cdd70e04bd6e714c8b763efdfe0259a5dd4128" +} diff --git a/core/lib/dal/src/pruning_dal.rs b/core/lib/dal/src/pruning_dal.rs index e52e856c862..7f0c54f2901 100644 --- a/core/lib/dal/src/pruning_dal.rs +++ b/core/lib/dal/src/pruning_dal.rs @@ -404,7 +404,7 @@ impl PruningDal<'_, '_> { i64::from(last_miniblock_to_prune.0), PruneType::Hard as PruneType ) - .instrument("soft_prune_batches_range#insert_pruning_log") + .instrument("hard_prune_batches_range#insert_pruning_log") .with_arg("last_l1_batch_to_prune", &last_l1_batch_to_prune) .with_arg("last_miniblock_to_prune", &last_miniblock_to_prune) .with_arg("prune_type", &PruneType::Hard) @@ -414,4 +414,25 @@ impl PruningDal<'_, '_> { Ok(()) } + + // This method must be separate as VACUUM is not supported inside a transaction + pub async fn run_vacuum_after_hard_pruning(&mut self) -> DalResult<()> { + sqlx::query!( + r#" + VACUUM l1_batches, + miniblocks, + storage_logs, + events, + call_traces, + l2_to_l1_logs, + transactions + "#, + ) + .instrument("hard_prune_batches_range#vacuum") + .report_latency() + .execute(self.storage) + .await?; + + Ok(()) + } } diff --git a/core/lib/zksync_core/src/db_pruner/mod.rs b/core/lib/zksync_core/src/db_pruner/mod.rs index e54fc11ae8e..17057ced881 100644 --- a/core/lib/zksync_core/src/db_pruner/mod.rs +++ b/core/lib/zksync_core/src/db_pruner/mod.rs @@ -141,6 +141,12 @@ impl DbPruner { transaction.commit().await?; + let mut storage = pool.connection_tagged("db_pruner").await?; + storage + .pruning_dal() + .run_vacuum_after_hard_pruning() + .await?; + let latency = latency.observe(); tracing::info!( "Hard pruned db l1_batches up to {} and miniblocks up to {}, operation took {:?}",