[FlatPostgresCollection] Sort keys before preparedStatement.addBatch() to prevent deadlocks#320
Merged
Merged
Conversation
suddendust
requested review from
avinashkolluru,
kotharironak,
puneet-traceable,
skjindal93 and
suresh-prakash
as code owners
July 21, 2026 09:00
suddendust
commented
Jul 21, 2026
| List<Key> keys = keyGroup.getKeys(); | ||
| List<List<Object>> allKeyParams = keyGroup.getKeyParams(); | ||
|
|
||
| Integer[] order = new Integer[keys.size()]; |
Contributor
Author
There was a problem hiding this comment.
This array contains the order of the keys - We add to the batch according to this order.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #320 +/- ##
============================================
+ Coverage 80.89% 80.90% +0.01%
Complexity 1580 1580
============================================
Files 242 242
Lines 7628 7632 +4
Branches 751 752 +1
============================================
+ Hits 6171 6175 +4
Misses 968 968
Partials 489 489
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
suresh-prakash
approved these changes
Jul 21, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Sort keys to prevent deadlocks in batched writes on FlatPostgresCollection
Summary
Enforces a canonical, string-sorted key order across all JDBC-batched, key-keyed write operations in FlatPostgresCollection. Postgres acquires per-row locks in batch-entry order for
INSERT ... ON CONFLICT DO UPDATEandUPDATE ... WHERE key = ?, so two concurrent batches that touch overlapping key sets in opposite orders can form a wait-for cycle and one side gets aborted with ERROR: deadlock detected (SQLState40P01).Sorting inside the store — the last hop before
addBatch(...)— makes the invariant local to the collection and independent of caller iteration order, so no downstream service has to remember to sort.Symptom this fixes
Callers were seeing production log lines like:
The offending SQL is produced only by the Map-based
bulkUpdateon this class, and the deadlock reproduces trivially with two concurrent callers that iterate overlapping keys in opposite orders. The same class of bug also exists onbulkUpsert/bulkCreateOrReplace, which batchINSERT ... ON CONFLICT DO UPDATE — the DO UPDATEbranch takes a row-exclusive lock and follows the same batch-entry ordering.Testing
Didn't add any additional tests - The existing integration tests should be sufficient.
Checklist: