Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ create index if not exists idx_batch_index_server_time
on public.batch_index (server_id, received_at desc);
create index if not exists idx_batch_index_session
on public.batch_index (session_id, received_at desc);
-- Used by the TTL cleanup task (object_store_cleanup.rs) to find expired rows.
create index if not exists idx_batch_index_received_at
on public.batch_index (received_at);

--------------------------------------------------------------------------------
-- FINDINGS: detections/alerts produced by processors
Expand Down
23 changes: 21 additions & 2 deletions src/object_store_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,30 @@ async fn cleanup_batch_index(
return Ok(count.max(0) as u64);
}

let res = sqlx::query("delete from public.batch_index where received_at < $1")
// Delete in chunks so a large backlog (e.g. first enable, or the service
// being down for a while) never turns into one giant transaction that
// locks batch_index and cascades millions of module_dispatches rows.
const CHUNK: i64 = 10_000;
let mut total: u64 = 0;
loop {
let res = sqlx::query(
"delete from public.batch_index where id in (
select id from public.batch_index
where received_at < $1
order by received_at
limit $2)",
)
.bind(cutoff)
.bind(CHUNK)
.execute(&state.db)
.await?;
Ok(res.rows_affected())
let deleted = res.rows_affected();
total += deleted;
if deleted < CHUNK as u64 {
break;
}
}
Ok(total)
}

async fn cleanup_local_store(
Expand Down
Loading