You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make MsSqlDialect compatible with AzureSql and READ COMMITTED SNAPSHOT #31
NEventStore's SqlPersistenceEngine relies on database-level locks when reading commits. When one enables SQL Server's READ COMMITTED SNAPSHOT option at the DB level, this stops working and there's a high possibility of skipping commits. On Azure SQL this option is always on, so one will easily experience lost commits on Azure SQL. This was reported previously, e.g., in #14 or #21 (comment).
To cope with that, the MsSqlDialect should use READCOMMITTEDLOCK table hints when reading commits to enforce locking.
It should suffice to just replace all the FROM Commits strings in the SELECT statements for reading commits with FROM Commits WITH (READCOMMITTEDLOCK).
changed the title [-]Add READCOMMITTEDLOCK table hint to MsSqlDialect for AzureSql and READ COMMITTED SNAPSHOT compatibility[/-][+]Make MsSqlDialect compatible with AzureSql and READ COMMITTED SNAPSHOT compatibility[/+]on Aug 9, 2019
changed the title [-]Make MsSqlDialect compatible with AzureSql and READ COMMITTED SNAPSHOT compatibility[/-][+]Make MsSqlDialect compatible with AzureSql and READ COMMITTED SNAPSHOT[/+]on Aug 9, 2019
Scope: useful SQL Server mitigation, not the safe-polling fix
READCOMMITTEDLOCK is a SQL Server/Azure SQL table hint. It requests lock-based READ COMMITTED for the read even when the database uses READ_COMMITTED_SNAPSHOT. That can prevent the immediate RCSI miss if the read encounters the lower row while its insert is uncommitted. The trade-off is that this read can block. It does not order checkpoint allocation, does not order transaction commits, and has no portable equivalent across all supported dialects.
Writer A receives checkpoint 101, but its row is not yet available for the reader to encounter and lock against.
Writer B receives 102 and commits. A poll can see 102 without encountering a conflicting lock at 101.
The projection saves 102. A then commits 101; the next poll uses CheckpointNumber > 102 and cannot see 101.
This is distinct from the easier RCSI reproduction, where the lower row has already been inserted but is uncommitted. A locking read may block on that row; a row-versioned read can pass it.
SQL #21 contains a production report of continued misses with the hint. That report does not prove which plan or timing caused each individual miss, but it is consistent with the distinction above. PR #39 is useful as a legacy mitigation and test reference; it does not provide the safe checkpoint contract. Keep this issue separate from SQL #72, which addresses writer publication order for core #425, and from SQL #63, which fixes offset paging. SQL #14 captures the original Azure SQL symptoms. Do not require #31 to land before #72.
Co-authored by Alessandro Giorgetti and Codex (OpenAI).
SQL Server and Azure SQL: opt-in locking for commit reads
We’re implementing #31 as an opt-in mitigation for commit readers under READ_COMMITTED_SNAPSHOT (RCSI). If transaction A has inserted checkpoint 101 but hasn’t committed, and transaction B commits 102, an ordinary versioned read can return 102 without waiting. A projection that saves 102 will never ask for 101 again. With READCOMMITTEDLOCK, a poll that encounters A’s conflicting lock waits for A to finish. If A commits, the poll can read 101 before advancing; if A rolls back, the poll can move past that gap. This is the Azure SQL scenario described in #14.
Implementation.PR #73 covers built-in pollers and custom consumers, regardless of which public commit-read query they use. Opt in with WithDialect(new MsSqlDialect(useReadCommittedLockForCommitReads: true)). The AddReadCommittedLockToFromCommits helper applies the hint to all seven statements: global and bucket-scoped open-ended checkpoint reads, both bounded checkpoint reads, both date-based reads, and stream revision reads. Sync and async callers share those statements where both APIs exist. This reuses the opt-in idea from PR #39. The default, paging behavior, writes, snapshots, schema, and other dialects are unchanged. Opted-in reads can wait longer or time out under contention.
Verification. The expanded SQL-shape test failed before the change and passed afterward, including checks that the default and snapshot/write queries stay unchanged. Ten SQL Server integration cases use independent connections in an isolated RCSI database to check a real lock wait and the result after commit or rollback. They cover checkpoint, date, and stream consumers, with sync/async where available. CI for the expanded revision is the runtime check; Azure SQL is not separately runtime-tested.
What this does not fix. The hint cannot wait on a conflicting row lock that does not exist yet. If 101 has been allocated but the reader can already pass its position, it can still save 102 and miss 101 later. That publication-order race is tracked in #21; #72 remains the SQL Server implementation of the stronger core #425 contract. #63 remains separate: it addresses offset paging, not publication order. This change is intended to resolve #31, mitigate the RCSI symptom in #14 and #21, and leave the broader issues open.
Co-authored by Alessandro Giorgetti and Codex (OpenAI).
Hi @AGiorgetti , cool that this has been implemented at last! Did you consider making "true" the default? I believe that it would be a much safer default, I cannot think of any scenario where somebody would want the "false" behavior on a database with RCSI...
Activity
fschmied commentedon Aug 9, 2019
It should suffice to just replace all the
FROM Commitsstrings in the SELECT statements for reading commits withFROM Commits WITH (READCOMMITTEDLOCK).[-]Add READCOMMITTEDLOCK table hint to MsSqlDialect for AzureSql and READ COMMITTED SNAPSHOT compatibility[/-][+]Make MsSqlDialect compatible with AzureSql and READ COMMITTED SNAPSHOT compatibility[/+][-]Make MsSqlDialect compatible with AzureSql and READ COMMITTED SNAPSHOT compatibility[/-][+]Make MsSqlDialect compatible with AzureSql and READ COMMITTED SNAPSHOT[/+]Refs: #31 added support for READCOMMITTEDLOCK to MsSqlDialect, need to
AGiorgetti commentedon Dec 3, 2026
Scope: useful SQL Server mitigation, not the safe-polling fix
READCOMMITTEDLOCKis a SQL Server/Azure SQL table hint. It requests lock-based READ COMMITTED for the read even when the database usesREAD_COMMITTED_SNAPSHOT. That can prevent the immediate RCSI miss if the read encounters the lower row while its insert is uncommitted. The trade-off is that this read can block. It does not order checkpoint allocation, does not order transaction commits, and has no portable equivalent across all supported dialects.CheckpointNumber > 102and cannot see 101.This is distinct from the easier RCSI reproduction, where the lower row has already been inserted but is uncommitted. A locking read may block on that row; a row-versioned read can pass it.
SQL #21 contains a production report of continued misses with the hint. That report does not prove which plan or timing caused each individual miss, but it is consistent with the distinction above. PR #39 is useful as a legacy mitigation and test reference; it does not provide the safe checkpoint contract. Keep this issue separate from SQL #72, which addresses writer publication order for core #425, and from SQL #63, which fixes offset paging. SQL #14 captures the original Azure SQL symptoms. Do not require #31 to land before #72.
Co-authored by Alessandro Giorgetti and Codex (OpenAI).
AGiorgetti commentedon Dec 3, 2026
SQL Server and Azure SQL: opt-in locking for commit reads
We’re implementing #31 as an opt-in mitigation for commit readers under
READ_COMMITTED_SNAPSHOT(RCSI). If transaction A has inserted checkpoint 101 but hasn’t committed, and transaction B commits 102, an ordinary versioned read can return 102 without waiting. A projection that saves 102 will never ask for 101 again. WithREADCOMMITTEDLOCK, a poll that encounters A’s conflicting lock waits for A to finish. If A commits, the poll can read 101 before advancing; if A rolls back, the poll can move past that gap. This is the Azure SQL scenario described in #14.Implementation. PR #73 covers built-in pollers and custom consumers, regardless of which public commit-read query they use. Opt in with
WithDialect(new MsSqlDialect(useReadCommittedLockForCommitReads: true)). TheAddReadCommittedLockToFromCommitshelper applies the hint to all seven statements: global and bucket-scoped open-ended checkpoint reads, both bounded checkpoint reads, both date-based reads, and stream revision reads. Sync and async callers share those statements where both APIs exist. This reuses the opt-in idea from PR #39. The default, paging behavior, writes, snapshots, schema, and other dialects are unchanged. Opted-in reads can wait longer or time out under contention.Verification. The expanded SQL-shape test failed before the change and passed afterward, including checks that the default and snapshot/write queries stay unchanged. Ten SQL Server integration cases use independent connections in an isolated RCSI database to check a real lock wait and the result after commit or rollback. They cover checkpoint, date, and stream consumers, with sync/async where available. CI for the expanded revision is the runtime check; Azure SQL is not separately runtime-tested.
What this does not fix. The hint cannot wait on a conflicting row lock that does not exist yet. If 101 has been allocated but the reader can already pass its position, it can still save 102 and miss 101 later. That publication-order race is tracked in #21; #72 remains the SQL Server implementation of the stronger core #425 contract. #63 remains separate: it addresses offset paging, not publication order. This change is intended to resolve #31, mitigate the RCSI symptom in #14 and #21, and leave the broader issues open.
Co-authored by Alessandro Giorgetti and Codex (OpenAI).
Add opt-in locking for SQL Server legacy checkpoint polls (#31)
fschmied commentedon Dec 3, 2026
Hi @AGiorgetti , cool that this has been implemented at last! Did you consider making "true" the default? I believe that it would be a much safer default, I cannot think of any scenario where somebody would want the "false" behavior on a database with RCSI...