Skip to content

Comments

Fix: Use merge when updating auto restatements#5016

Merged
vchan merged 2 commits intomainfrom
vchan/auto-restatement-merge
Jul 25, 2025
Merged

Fix: Use merge when updating auto restatements#5016
vchan merged 2 commits intomainfrom
vchan/auto-restatement-merge

Conversation

@vchan
Copy link
Contributor

@vchan vchan commented Jul 24, 2025

This PR prevents a race condition that can occur when two runs update auto restatements at the same time. Although the delete + insert happens in a transaction, certain databases and transaction isolation levels can result in duplicate rows being inserted and violate the primary key constraint on the auto restatements table.

For example, the default transaction isolation level for Postgres is READ COMMITTED, and at that level, if the first updater commits, the second updater will ignore the row if the first updater deleted it. In this case, the second updater will be blocked when deleting auto restatements until the first updater commits, but then will not delete any rows even though the first updater has re-inserted matching snapshot name versions.

To get prevent it, auto restatements are updated with a MERGE.

@vchan vchan requested a review from Copilot July 24, 2025 20:51
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the data update strategy for auto restatements by replacing insert_append with merge to prevent duplicate entries when updating existing records.

  • Changes from insert_append to merge operation for auto restatements updates
  • Adds a unique key constraint based on snapshot name and version to ensure proper merging

@vchan vchan marked this pull request as ready for review July 24, 2025 21:24
@vchan vchan changed the title Fix: Use merge instead over insert append when updating auto restatem… Fix: Use merge when updating auto restatements Jul 24, 2025
@vchan vchan merged commit 089ba46 into main Jul 25, 2025
27 checks passed
@vchan vchan deleted the vchan/auto-restatement-merge branch July 25, 2025 17:08
vchan added a commit that referenced this pull request Aug 7, 2025
vchan added a commit that referenced this pull request Aug 8, 2025
vchan added a commit that referenced this pull request Aug 12, 2025
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.

2 participants