Fix concurrent same-key PUT and PATCH upserts - #3787
Open
aaronburtle wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a TOCTOU race in relational upsert (PUT/PATCH) flows where concurrent requests for the same initially-missing key could both choose the INSERT path, leading to duplicate-key failures (SQL Server/PostgreSQL) or duplicate logical rows (DWSQL). It adds provider-specific serialization around the “row exists?” decision and introduces regression tests to validate same-key concurrency behavior and PostgreSQL result-set handling.
Changes:
- SQL Server & DWSQL: add locking table hints on the existence check for insert-capable upserts (
UPDLOCK,HOLDLOCKfor MSSQL;TABLOCKX,HOLDLOCKfor DWSQL), without affecting update-only fallback behavior. - PostgreSQL: add a transaction-scoped advisory lock derived from source + full PK before the existence check, and update the executor to skip the lock statement’s result set.
- Add unit + integration regression coverage for concurrent PUT/PATCH upserts, composite PK ordering, and PostgreSQL multi-result-set handling.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Core/Resolvers/MsSqlQueryBuilder.cs | Adds locking hints to serialize insert-capable upsert existence checks. |
| src/Core/Resolvers/DWSqlQueryBuilder.cs | Adds exclusive table locking on DWSQL existence checks for insert-capable upserts. |
| src/Core/Resolvers/PostgresQueryBuilder.cs | Emits an advisory-lock statement before the existence check for insert-capable upserts. |
| src/Core/Resolvers/PostgreSqlExecutor.cs | Skips the advisory-lock result set before interpreting existing upsert result sets. |
| src/Service.Tests/UnitTests/RelationalUpsertConcurrencyQueryBuilderTests.cs | Unit tests asserting provider-specific serialization primitives are emitted (and not emitted for update-only fallback). |
| src/Service.Tests/UnitTests/PostgreSqlQueryExecutorUnitTests.cs | Unit test verifying the executor correctly skips the advisory-lock result set. |
| src/Service.Tests/SqlTests/RestApiTests/UpsertConcurrencyTestBase.cs | Shared integration test logic for concurrent same-key PUT/PATCH upserts against missing composite keys. |
| src/Service.Tests/SqlTests/RestApiTests/MsSqlUpsertConcurrencyTests.cs | SQL Server integration coverage for concurrent same-key PUT/PATCH upserts. |
| src/Service.Tests/SqlTests/RestApiTests/PostgreSqlUpsertConcurrencyTests.cs | PostgreSQL integration coverage for concurrent same-key PUT/PATCH upserts. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Why make this change?
Closes #3743
What is this change?
UPDLOCKandHOLDLOCKon the existence check.smallint,integer,bigint, and UUID primary keys use a key-scoped lock derived from the source and complete ordered primary key. Upserts for different keys can therefore proceed concurrently.hashtextextendedand does not introduce a PostgreSQL 14 dependency.TABLOCKXandHOLDLOCKbecause configured logical keys are not necessarily enforced by unique constraints.How was this tested?
character(n)keys that compare equal produce only one row.git diff --checkpassed.Sample Request(s)
201 Created; the remaining requests update that row and return200 OK. Only one row remains for the key.