Repository navigation
Add safe checkpoint polling across SQL dialects (#72) - #75
AGiorgetti wants to merge 14 commits into
Conversation
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>
Why this PR uses shared writer locks and a short polling fenceThe aim for #21 and the SQL Server part of #72 is to preserve the existing
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 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). |
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>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
Exercise 8.0, 8.4, and 9.7 against real InnoDB. Keep MySQL 5.7 excluded because its allocator can reuse a deleted checkpoint after a server restart. Co-authored-by: Codex <codex@openai.com>
Use interleaved buckets and a one-commit page with both core pollers on all five SQL engines. Without bucket filtering the SQLite cases resume at the other bucket checkpoint. Co-authored-by: Codex <codex@openai.com>
There was a problem hiding this comment.
🟡 Changes recommended
The new CI job uses mutable action tags, allowing executable workflow code to change without a repository update; checkout and setup-dotnet must be pinned to verified full commit SHAs.
1 open issue
What changed in this PR
Adds opt-in safe checkpoint polling across all five SQL providers for #72, building on existing keyset paging to prevent skipped commits when transactions complete out of order. Legacy polling remains unchanged; Azure SQL verification stays deferred.
Changes:
- Adds dialect-specific checkpoint fences and configuration checks.
- Adds concurrency, rollback, cancellation, and poller-resume regressions.
- Documents deployment requirements and expands MySQL CI coverage.
| File | Description |
|---|---|
| src/NEventStore.Persistence.Sqlite.Tests/SafeCheckpointPollingTests.cs | Tests SQLite visibility, resume, and configuration rejection. |
| src/NEventStore.Persistence.Sqlite.Tests/NEventStore.Persistence.Sqlite.Core.Tests.csproj | Updates test dependencies and adds shared helpers. |
| src/NEventStore.Persistence.Sql/SqlPersistenceEngine.SafePolling.cs | Implements independent, bounded safe polling. |
| src/NEventStore.Persistence.Sql/SqlPersistenceEngine.cs | Exposes the contract and independent synchronous queries. |
| src/NEventStore.Persistence.Sql/SqlPersistenceEngine.Async.cs | Supports independent asynchronous queries and cancellation. |
| src/NEventStore.Persistence.Sql/SqlDialects/SqliteDialect.cs | Adds opt-in schema and read-isolation checks. |
| src/NEventStore.Persistence.Sql/SqlDialects/PostgreSqlDialect.cs | Enables safe polling for both PostgreSQL dialects. |
| src/NEventStore.Persistence.Sql/SqlDialects/PostgreSqlCheckpointProtocol.cs | Adds advisory locks and sequence qualification. |
| src/NEventStore.Persistence.Sql/SqlDialects/OracleNativeDialect.cs | Integrates Oracle protocol initialization and writes. |
| src/NEventStore.Persistence.Sql/SqlDialects/OracleDbStatement.cs | Binds 64-bit checkpoint outputs from PL/SQL. |
| src/NEventStore.Persistence.Sql/SqlDialects/OracleCheckpointProtocol.cs | Adds Oracle gates and allocator checks. |
| src/NEventStore.Persistence.Sql/SqlDialects/MySqlDialect.cs | Integrates MySQL gate setup and writes. |
| src/NEventStore.Persistence.Sql/SqlDialects/MySqlCheckpointProtocol.cs | Adds InnoDB locking and allocator qualification. |
| src/NEventStore.Persistence.Sql/SqlDialects/MsSqlDialect.cs | Adds shared writer locks and exclusive reader fences. |
| src/NEventStore.Persistence.Sql/SqlDialects/CommonSqlDialect.cs | Checks inherited protocol compatibility. |
| src/NEventStore.Persistence.Sql.Tests/NEventStore.Persistence.Sql.Core.Tests.csproj | References core polling clients. |
| src/NEventStore.Persistence.Sql.Tests/BucketPollingAssertions.cs | Shares bucket-scoped stop-and-resume assertions. |
| src/NEventStore.Persistence.PostgreSql.Tests/SafeCheckpointPollingTests.cs | Tests PostgreSQL ordering, fencing, and sequence caching. |
| src/NEventStore.Persistence.PostgreSql.Tests/NEventStore.Persistence.PostgreSql.Core.Tests.csproj | Updates test dependencies. |
| src/NEventStore.Persistence.Oracle.Tests/SafeCheckpointPollingTests.cs | Tests Oracle fencing, upgrades, and 64-bit checkpoints. |
| src/NEventStore.Persistence.Oracle.Tests/NEventStore.Persistence.Oracle.Core.Tests.csproj | Updates test dependencies. |
| src/NEventStore.Persistence.MySql.Tests/SafeCheckpointPollingTests.cs | Tests MySQL fencing, gate validation, and resume behavior. |
| src/NEventStore.Persistence.MySql.Tests/NEventStore.Persistence.MySql.Core.Tests.csproj | Updates test dependencies. |
| src/NEventStore.Persistence.MsSql.Tests/SafeCheckpointPollingTests.cs | Tests overlap, rollback, cancellation, and dialect compatibility. |
| src/NEventStore.Persistence.MsSql.Tests/NEventStore.Persistence.MsSql.Core.Tests.csproj | Updates dependencies and references polling clients. |
| docs/SqlServer-Safe-Checkpoint-Polling.md | Explains SQL Server rollout, guarantees, and performance. |
| docs/Sqlite-Safe-Checkpoint-Polling.md | Explains SQLite qualification and single-writer guarantees. |
| docs/PostgreSql-Safe-Checkpoint-Polling.md | Documents PostgreSQL locking and sequence requirements. |
| docs/Oracle-Safe-Checkpoint-Polling.md | Documents Oracle gates and sequence migration. |
| docs/MySql-Safe-Checkpoint-Polling.md | Documents MySQL requirements, rollout, and costs. |
| Changelog.md | Announces safe polling for all five providers. |
| .github/workflows/ci.yml | Uses MySQL 8.4 and adds 8.0/9.7 integration jobs. |
Review effort: Balanced

Scope
SQL provider implementation for #72 and NEventStore/NEventStore#425. This draft carries one commit per additional dialect after the SQL Server slice. Azure SQL verification remains deferred to #14.
SQL Server
IDENTITYinsert. 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.SQLite
Uses SQLite’s single writer and the provider-created
AUTOINCREMENTschema. No writer-side change or gate. The safe poll checks the schema and dirty-read setting and captures the committed ceiling in one scalar query before the bounded seek. See the SQLite guide for proof, scope, and measured SQL timings.PostgreSQL
Shared transaction-owned advisory locks register writers before
BIGSERIALallocation. The independent reader fences pending writers and qualifies the primary, table, andCACHE 1sequence before bounding the seek. See the PostgreSQL guide for the all-writer cutover, the sequence cache regression, and prepared versus unprepared SQL timings.MySQL/InnoDB
Opt-in initialization installs an InnoDB gate row. Writers use a shared lock in the existing insert command. A safe read takes an exclusive lock in a short independent READ COMMITTED transaction, releases it before paging, and validates the gate and
AUTO_INCREMENTcolumn. See the MySQL guide for all-writer cutover and measured overhead.Oracle
The existing insert trigger allocates the checkpoint from an ordered sequence. An empty gate table provides shared writer registration and a short exclusive read fence. Both PL/SQL blocks return their checkpoint via a 64-bit output binding. See the Oracle guide for existing-store sequence migration, privileges, and transaction costs.
The guarded Oracle ceiling plus commit took 1.19–1.39 ms per call across three local server-only timing runs on 100,000 rows. Insert comparisons varied across trials and do not support a reliable overhead figure. This is not a network or end-to-end poll measurement.
SQL Server 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
IDENTITYschema: 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.
20 focused tests passed on SQL Server 2022 / .NET 10, including compatible derived dialects and rejection of overrides that bypass the protocol. All five CI jobs passed on the SQL Server slice.
171 SQLite tests passed locally on .NET 10, including 14 focused regressions across both clients, WAL and rollback journals. The .NET Framework 4.7.2 SQLite test project compiles.
154 PostgreSQL tests passed on PostgreSQL 15.18 / .NET 10, including both built-in dialect paths, derived dialect, commit/rollback, two-session sequence caching, and allocation ordering. The .NET Framework 4.7.2 PostgreSQL test project compiles.
153 MySQL tests passed on MySQL 8.0.46 / .NET 10, including six focused cases for ambient commit/rollback, waiting before allocation, an existing database upgrade, invalid gates, and a 64-bit checkpoint. The .NET Framework 4.7.2 MySQL test project compiles.
156 Oracle tests passed per runtime against Oracle XE, including six regressions for ambient completion, waiting before allocation, a 64-bit checkpoint, and existing-store qualification. Native XE checks also verified the writer overlap and the blocked fence.
The CI run before the terminology-only cleanup passed all five jobs: Linux and Windows builds and 927 tests each on .NET 8, 9 and 10, no failures or skips.
Release follow-up: Alessandro will handle the later core #555 repin. 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).