-
Notifications
You must be signed in to change notification settings - Fork 96
feat/db: cleanup old account state data db entries #1645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
drahnr
wants to merge
1
commit into
next
Choose a base branch
from
miden-node-bernhard-cleanup-old-db-entries-main
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
crates/store/src/db/migrations/2026020600000_cleanup_indices/down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| -- Reverse the cleanup indices migration | ||
|
|
||
| DROP INDEX IF EXISTS idx_vault_cleanup; | ||
| DROP INDEX IF EXISTS idx_storage_cleanup; |
9 changes: 9 additions & 0 deletions
9
crates/store/src/db/migrations/2026020600000_cleanup_indices/up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| -- Add indices to optimize cleanup queries that delete old non-latest entries. | ||
| -- | ||
| -- These partial indices only include rows where is_latest = 0, making them: | ||
| -- - Smaller (only index rows that will eventually be deleted) | ||
| -- - Faster for cleanup operations (direct lookup of old entries) | ||
| -- - No overhead for is_latest = 1 rows (which are never deleted) | ||
|
|
||
| CREATE INDEX idx_vault_cleanup ON account_vault_assets(block_num) WHERE is_latest = 0; | ||
| CREATE INDEX idx_storage_cleanup ON account_storage_map_values(block_num) WHERE is_latest = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1252,3 +1252,46 @@ pub(crate) struct AccountStorageMapRowInsert { | |
| pub(crate) value: Vec<u8>, | ||
| pub(crate) is_latest: bool, | ||
| } | ||
|
|
||
| // CLEANUP FUNCTIONS | ||
| // ================================================================================================ | ||
|
|
||
| /// Number of historical blocks to retain for vault assets and storage map values. | ||
| /// Entries older than `chain_tip - HISTORICAL_BLOCK_RETENTION` will be deleted, | ||
| /// except for entries marked with `is_latest=true` which are always retained. | ||
| pub const HISTORICAL_BLOCK_RETENTION: u32 = 50; | ||
drahnr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Clean up old entries for all accounts, deleting entries older than the retention window. | ||
| /// | ||
| /// Deletes rows where `block_num < chain_tip - HISTORICAL_BLOCK_RETENTION` and `is_latest = false`. | ||
| /// This is a simple and efficient approach that doesn't require window functions. | ||
| /// | ||
| /// # Returns | ||
| /// A tuple of `(vault_assets_deleted, storage_map_values_deleted)` | ||
| pub fn cleanup_all_accounts( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Organizationally, should we not have: fn prune_history(..) {
let cutoff_block = i64::from(chain_tip.as_u32().saturating_sub(HISTORICAL_BLOCK_RETENTION));
prune_account_vault_assets()?;
prune_account_storage_maps()?;
// Probably more that we will need.
}
fn prune_account_vault_assets(...) {}
fn prune_account_storage_map(...) {}and ideally we should also instrument each of these so we know how long each took. |
||
| conn: &mut SqliteConnection, | ||
| chain_tip: BlockNumber, | ||
| ) -> Result<(usize, usize), DatabaseError> { | ||
| let cutoff_block = i64::from(chain_tip.as_u32().saturating_sub(HISTORICAL_BLOCK_RETENTION)); | ||
| let vault_deleted = diesel::delete( | ||
| schema::account_vault_assets::table.filter( | ||
| schema::account_vault_assets::block_num | ||
| .lt(cutoff_block) | ||
| .and(schema::account_vault_assets::is_latest.eq(false)), | ||
| ), | ||
| ) | ||
| .execute(conn) | ||
| .map_err(DatabaseError::Diesel)?; | ||
|
|
||
| let storage_deleted = diesel::delete( | ||
| schema::account_storage_map_values::table.filter( | ||
| schema::account_storage_map_values::block_num | ||
| .lt(cutoff_block) | ||
| .and(schema::account_storage_map_values::is_latest.eq(false)), | ||
| ), | ||
| ) | ||
| .execute(conn) | ||
| .map_err(DatabaseError::Diesel)?; | ||
|
|
||
| Ok((vault_deleted, storage_deleted)) | ||
| } | ||
drahnr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.