← 输入框|github.com|刷新
Skip to content

Add opt-in safe checkpoint polling for SQL Server (#72) - #75

Draft
AGiorgetti wants to merge 5 commits into
developfrom
fix/72-sql-server-safe-checkpoint-polling
Draft

AGiorgetti wants to merge 5 commits into
developfrom
fix/72-sql-server-safe-checkpoint-polling

Conversation

@AGiorgetti

@AGiorgetti AGiorgetti commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Scope

SQL Server slice of #72, addressing #21 and the SQL provider part of NEventStore/NEventStore#425. The other SQL dialects still need independent qualification. Azure SQL verification remains deferred to #14.

  • Writers acquire a shared, transaction-owned application lock before the existing IDENTITY insert. Independent writers can allocate and commit concurrently, and the shared lock stays held through actual commit or rollback, including ambient transactions. Registration and insert still share one batch; no new transaction, schema, checkpoint or writer-side round trip.
  • A safe poll uses an exclusive checkpoint fence to wait for in-flight writers, capture a committed ceiling, and release the lock before paging or running handlers. Then it uses [Order 06][P2] Replace offset checkpoint paging with keyset paging #63's bounded keyset reads. Make MsSqlDialect compatible with AzureSql and READ COMMITTED SNAPSHOT #31's hint is not required. Requested cancellation while waiting is propagated without delivering a checkpoint.
  • Enable the protocol across all writers and safe readers of a store. Neither a legacy writer that bypasses it nor a reader from the earlier exclusive-writer draft is supported in a mixed deployment. See the SQL Server guide.

Performance

Eight writers with a simulated 12 ms hold inside each ambient transaction and one active poller: 77 → 473 commits/s and 115 → 24 ms p95 append, comparing this protocol against the earlier exclusive-writer draft. p95 poll duration increased 4 → 18 ms. Without a poller, the same writer workload improved from 75 → 540 commits/s. Local SQL Server 2022/.NET 10, pooled loopback, three trials; not a production estimate. Frequent or waiting poll fences can still delay subsequent writes.

There is no additional transaction. An optimistic explicit-key allocator like MongoDB #118 is not a drop-in replacement for the existing SQL Server IDENTITY schema: it needs a durable floor against ID reuse, and same-key contenders would still wait on an uncommitted ambient writer.

Verification

  • Eight overlap regressions failed under the old exclusive-writer protocol and pass here. Tests prove that the higher writer can commit first and the safe poll does not expose it before the lower writer commits or rolls back. They cover both READ COMMITTED modes, sync and async, global and bucket polling, and fresh-reader resume through core PR #555's pollers.
  • 14 focused tests pass on real SQL Server 2022 / .NET 10, including cancellation of a waiting poll, failed lock acquisition, and writes during bounded paging. The .NET Framework 4.7.2 test project compiles. The broader local run still hit existing serializable tests that expect deadlocks; GitHub Actions must verify the latest head. All five jobs passed on the preceding exclusive-writer revision.

Draft dependency: Repin the source submodule to the merged core #555 revision and rerun CI before merging. Before release, set the SQL NuGet dependency floor to a published core containing the contract and verify a package-only consumer.

Co-authored by Alessandro Giorgetti and Codex (OpenAI).

AGiorgetti and others added 5 commits December 3, 2026 16:40
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>

Copy link
Copy Markdown
Member Author

Why this PR uses shared writer locks and a short polling fence

The aim for #21 and the SQL Server part of #72 is to preserve the existing long checkpoint and prevent a safe poller from saving 102 while a lower matching commit at 101 can still become visible. Rollback and deletion gaps are fine; we do not wait for missing numbers to appear.

  • Writes stay concurrent. Each writer takes a shared, transaction-owned application lock for the database before the existing IDENTITY insert. The lock lasts through actual commit or rollback, including an enlisted outer transaction. Shared locks are compatible, so writers do not serialize behind one another. Registration and insert are in the same SQL batch and reuse the existing transaction: no new write round trip, transaction, schema, or cursor format.
  • The safe reader establishes the boundary. On an independent, non-ambient connection, it takes the exclusive lock, waits for in-flight writers to resolve, reads the committed maximum, and releases the lock before paging or invoking handlers. It then uses [Order 06][P2] Replace offset checkpoint paging with keyset paging #63's keyset reads over (after, ceiling]. This works without Make MsSqlDialect compatible with AzureSql and READ COMMITTED SNAPSHOT #31's READCOMMITTEDLOCK hint and does not rely on the query plan encountering a lower row lock first. A canceled or failed fence supplies no new checkpoint. The trade-off is that a long ambient write can delay the poll, and a queued poll can also delay later writers.
  • Keep the existing indexes. Actual SQL Server 2022 plans on 80,000 commits showed the ceiling reading the end of clustered PK_Commits (CheckpointNumber): one row, three logical reads. Global pages used an ordered clustered seek. Bucket-scoped pages of 128 used IX_Commits_Bucket_Checkpoint (BucketId, CheckpointNumber), then looked up the full commits. This does not justify adding a wide covering index for headers and payload. On upgraded stores, check that the bucket index actually exists: initialization skips schema creation if Commits already exists.

Fabian Schmied's insert-only lock proposal highlighted the cost of our earlier exclusive lock held through each writer's whole transaction. The separate prototype and benchmark passed 32 controlled cases with the locking hint on the stock SQL Server schema. The unhinted RCSI control exposed the higher checkpoint early. With eight writers, a 12 ms hold and an active poller, #75 measured 457 commits/s, the insert-only prototype 428, and the bounded version of that prototype 421. With no hold and no poller, the comparison was 2,160 vs. 1,002 commits/s. These are local results, not a production estimate or a proof for all query plans. The fence gives a correctness argument independent of the row access path and had higher write throughput in those runs.

A MongoDB-style optimistic allocator is not a drop-in replacement here: SQL Server IDENTITY gives competing writers different values. Switching to explicit keys would need an allocator and a durable floor against checkpoint reuse, and same-key contenders could still wait on an open ambient transaction.

Safe mode stays opt-in. Drain incompatible in-flight writes before enabling it, and switch all writers and safe readers using the same store. Legacy polling is unchanged and does not acquire this guarantee automatically. The implementation is scoped to the built-in SQL Server path; other engines remain tracked in #72 and Azure SQL verification is deferred to #14. See the SQL Server guide.

Co-authored by Alessandro Giorgetti and Codex (OpenAI).

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.

1 participant