Skip to content

Fail a retried upsert instead of inserting a concurrently inserted key - #4392

Open
Anurag-S1ngh wants to merge 3 commits into
apache:mainfrom
Anurag-S1ngh:fix/4194-upsert-retry-concurrent-insert
Open

Anurag-S1ngh wants to merge 3 commits into
apache:mainfrom
Anurag-S1ngh:fix/4194-upsert-retry-concurrent-insert

Conversation

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor

Summary

A commit retry replays the append planned before the concurrent commit. The fast append producer skips concurrency validation, so a key another writer inserted after the upsert read was inserted a second time.

Changes

  • Register the filter for the rows an upsert inserts on the append producer.
  • On retry, validate the data files added since the starting snapshot against that filter and raise CommitFailedException instead of inserting a duplicate key.
  • Plain appends keep skipping validation.

Testing

  • Added a test with two table handles where one upserts a key after the other has read the table, asserting the second upsert fails and the key is stored once.
  • make test passes (4697 passed, 5 skipped).

Closes #4194

A commit retry replays the append that was planned before the concurrent
commit, and the fast append producer skips concurrency validation, so a key
another writer inserted after the upsert read ends up inserted twice.

Record the filter for the rows an upsert inserts on the append producer and
validate the files added since the starting snapshot against it when the
commit is rebuilt for a retry. The upsert now fails with a
CommitFailedException instead of leaving a duplicate key.

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for picking this up, the approach of validating the added data files against the inserted keys on retry looks right to me. A few comments inline.

Comment thread pyiceberg/table/update/snapshot.py Resolved
Comment thread pyiceberg/table/update/snapshot.py Resolved
Comment thread pyiceberg/table/__init__.py Resolved
Address the review feedback on the upsert retry check:

- Gate the check on write.update.isolation-level, so both halves of an upsert
  follow the same isolation setting.
- Let the ValidationException propagate instead of converting it to a
  CommitFailedException, matching the update half.
- Drop the private Transaction.append arguments and let upsert configure the
  append producer through validate_no_conflicting_data. The filter's case
  sensitivity is stored on the producer instead of overwriting its inherited
  value.
@Fokko
Fokko self-requested a review November 17, 2026 20:46

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @Anurag-S1ngh, nice catch and a clean fix! The second commit addressing the isolation level gating and letting the ValidationException propagate reads well.

I pulled the branch and ran tests/table/test_upsert.py locally, all 29 pass, and CI is green.

A couple of small things, none blocking:

  • pyiceberg/table/update/snapshot.py:695: validate_no_conflicting_data only stores the filter, the actual check runs in _validate_concurrency on retry. Maybe worth a docstring line saying the validation happens at commit/retry time, so callers don't expect an exception from the call itself.
  • pyiceberg/table/update/snapshot.py:693: the class level defaults on _FastAppendFiles mean a producer that never calls validate_no_conflicting_data keeps skipping validation, which is what plain appends want. Good.
  • The snapshot isolation test documenting that the duplicate can still land under write.update.isolation-level=snapshot is a nice touch, that makes the semantics explicit.

LGTM once CI stays green!

Comment thread tests/table/test_upsert.py Resolved

This branch has not been deployed

No deployments
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.

A retried upsert inserts a key that a concurrent writer already inserted

2 participants