Skip to content

Conversation

@drahnr
Copy link
Contributor

@drahnr drahnr commented Oct 15, 2025

Ref #1185 step 5.

  • cleanup account table
  • cleanup account vault
  • cleanup storage table

#[derive(Clone)]
pub struct Db {
pool: deadpool_diesel::Pool<ConnectionManager, deadpool::managed::Object<ConnectionManager>>,
notify_cleanup_task: tokio::sync::mpsc::Sender<BlockNumber>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on what we do with the validator database (whether we re-use this or not), it would be good to make this task optional/disable-able

Comment on lines +950 to +958
// Strategy: For each vault_key, keep the latest entry (is_latest_update=true) and the
// most recent MAX_HISTORICAL_ENTRIES_PER_ACCOUNT historical entries, deleting the rest.
//
// Unfortunately, Diesel doesn't support window functions (ROW_NUMBER OVER) in its type-safe
// API, so we must use raw SQL for this complex deletion pattern. This is necessary because:
// 1. We need to partition by vault_key and order by block_num
// 2. We need to delete based on row ranking within each partition
// 3. The table is created WITHOUT ROWID, so we use the primary key (account_id, block_num,
// vault_key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need to keep MAX_HISTORICAL_ENTRIES_PER_ACCOUNT - we just need to make sure we have the data for the last $n$ blocks. So, query could be as simple as:

DELETE FROM account_vault_assets
WHERE
    block_num < chain_tip - n
    AND is_latest_update = false

To make this query efficient, we may need to add indexes on block_num and is_latest_update fields.

This methodology should also apply to the account_storage_map_values table (and in the future to the accounts table).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants