Skip to content

Commit 1a2c50d

Browse files
dkrizanclaude
andauthored
fix: restrict DELETE scope in removeDuplicityDescribingEntities (#3478)
## Summary When a batch job completes, `BatchJobActivityFinalizer` merges activity revisions from individual chunks into a single revision. As part of this, `removeDuplicityDescribingEntities` deletes duplicate `(entity_class, entity_id)` rows to avoid PK violations before reassigning all rows to the merge target. The DELETE query was missing an `activity_revision_id` filter on its outer `WHERE` clause: ```sql -- before delete from activity_describing_entity where (entity_class, entity_id) in (...) -- no revision scope and (activity_revision_id, entity_class, entity_id) not in (...) ``` This caused two problems: **1. Performance** — without `activity_revision_id` as the leading predicate, PostgreSQL cannot use the existing `(activity_revision_id, entity_class, entity_id)` index and falls back to a full sequential scan of the entire table on every batch job completion. **2. Data correctness** — the unscoped DELETE could match and delete rows from old, unrelated activity revisions that happen to reference the same `(entity_class, entity_id)` as entities in the batch being merged, silently corrupting historical activity data. ## Fix Add `(activity_revision_id in (:revisionIds) or activity_revision_id = :activityRevisionIdToMergeInto)` as the leading predicate on the outer DELETE. This restricts the operation strictly to the chunk revisions being merged, enables index usage, and eliminates the risk of touching unrelated historical rows. ## Test plan - [ ] Run a batch job with multiple chunks (e.g. machine translate ≥ 6 keys) and verify it completes successfully - [ ] Verify activity history for affected keys is intact after the job completes - [ ] Run `EXPLAIN ANALYZE` on the DELETE query and confirm it uses an Index Scan rather than a Seq Scan 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Batch job cleanup operations now process an expanded set of entities during merge scenarios, ensuring more thorough data consolidation. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 03cfcf9 commit 1a2c50d

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

backend/data/src/main/kotlin/io/tolgee/batch/BatchJobActivityFinalizer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ class BatchJobActivityFinalizer(
171171
.createNativeQuery(
172172
"""
173173
delete from activity_describing_entity
174-
where (entity_class, entity_id) in
174+
where (activity_revision_id in (:revisionIds) or activity_revision_id = :activityRevisionIdToMergeInto)
175+
and (entity_class, entity_id) in
175176
(select entity_class, entity_id
176177
from activity_describing_entity
177178
where activity_revision_id in (:revisionIds)

0 commit comments

Comments
 (0)