Skip to content

Commit 2406d71

Browse files
Refactor: Adjust bulk delete batch size for EventAttachment
Co-authored-by: tillman.elser <[email protected]>
1 parent 3b761e1 commit 2406d71

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/sentry/db/deletion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _continuous_query(self, query):
7979
cursor.execute(query)
8080
results = cursor.rowcount > 0
8181

82-
def iterator(self, chunk_size=100, batch_size=10000) -> Generator[tuple[int, ...]]:
82+
def iterator(self, chunk_size=100, batch_size=1000) -> Generator[tuple[int, ...]]:
8383
assert self.days is not None
8484
assert self.dtfield is not None
8585

src/sentry/runner/commands/cleanup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ def run_bulk_deletes_in_deletes(
584584
models_attempted: set[str],
585585
) -> None:
586586
from sentry.db.deletion import BulkDeleteQuery
587+
from sentry.models.eventattachment import EventAttachment
587588

588589
debug_output("Running bulk deletes in DELETES")
589590
for model_tp, dtfield, order_by in deletes:
@@ -603,7 +604,9 @@ def run_bulk_deletes_in_deletes(
603604
order_by=order_by,
604605
)
605606

606-
for chunk in q.iterator(chunk_size=100):
607+
# Use smaller batch size for EventAttachment to avoid query timeouts on massive tables
608+
batch_size = 100 if model_tp == EventAttachment else 1000
609+
for chunk in q.iterator(chunk_size=100, batch_size=batch_size):
607610
task_queue.put((imp, chunk))
608611

609612
# Ensure all tasks are completed before exiting

tests/sentry/db/test_deletion.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,26 @@ def test_iteration_descending(self) -> None:
8686
results.update(chunk)
8787

8888
assert results == expected_group_ids
89+
90+
def test_iteration_with_custom_batch_size(self) -> None:
91+
target_project = self.project
92+
expected_group_ids = {self.create_group().id for i in range(2)}
93+
94+
other_project = self.create_project()
95+
self.create_group(other_project)
96+
self.create_group(other_project)
97+
98+
# Test with custom batch_size parameter
99+
iterator = BulkDeleteQuery(
100+
model=Group,
101+
project_id=target_project.id,
102+
dtfield="last_seen",
103+
order_by="last_seen",
104+
days=0,
105+
).iterator(chunk_size=1, batch_size=100)
106+
107+
results: set[int] = set()
108+
for chunk in iterator:
109+
results.update(chunk)
110+
111+
assert results == expected_group_ids

0 commit comments

Comments
 (0)