Commit c489f67
mssql improvements, code clean up and fix issue 428 (#431)
* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT, AND)
- use alias to DELETE statement
- semicolons
* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT, BIGINT)
- remove 'Messages.' from query since it was redundant
- semicolons
* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT)
- remove 'Messages.' from query since it was redundant
- semicolons
* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT)
- remove 'Messages.' from query since it was redundant
- semicolons
* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- formatting: each column on it's own line. Best practice for maintainability of procedures
- use aliases
- semicolons
* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (AND)
- semicolons
* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- remove unused declared variables
- specify all parameters when calling [check_stream] procedure
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (IF, ISNULL, NVARCHAR)
- use alias to UPDATE statement
- remove 'AS' keyword
- semicolons
* feat(mssql): Minor SQL updates
- consistent casing of [Version] column
- explicitly specify NULLable columns
* fix(mssql): Messages.Created NOT NULL
SQL Server schema change so that Messages.Created is NOT NULL. This is safe and accurate to do since append_events will always set this value if a NULL is provided to the procedure.
* feat(mssql): json is not null
- Messages TABLE
- JsonMetaData is NOT NULL
- json columns are checked to ensure they contain json data
- StreamMessage TYPE
- json_metadata is NOT NULL
* feat(mssql): DATETIME2 is actually a DATETIME2(7) so explicitly say that in the scripts
* feat(mssql): ORDER BY StreamPosition per bito-code-review, and and remove redundencies in the CHECK definition
* feat(postgresql): ORDER BY stream_position per bito-code-review
* refactor(mssql): remove square brackets to satisfy 'inconsistent table naming' warning that bito-code-review flagged
* feat(mssql): append_events improve
At first when writing this change I assumed I would be out to fix issue #429. Turns out that is not an issue as the client wraps this procedure call in a transaction. Thus, no transction is needed within the procedure itself. Here are some improvements, nonetheless.
- Changed @stream_name parameter to NVARCHAR(850) for consistency
- Introduced @Inserted table to capture inserted GlobalPosition values atomically
- Normalized optimistic concurrency errors (2627, 2601) to client-detectable messages
- Thrown error messages still StartsWith 'WrongExpectedVersion' but now also includes the full SQL error text for client tracing
- Removed redundant post-commit queries and error-parsing logic
* perf(mssql): enable OPTIMIZE_FOR_SEQUENTIAL_KEY on primary keys for Streams and Messages
- Added WITH (OPTIMIZE_FOR_SEQUENTIAL_KEY = ON) to clustered primary keys on __schema__.Streams (PK_Streams) and __schema__.Messages (PK_Events)
- Improves insert throughput and reduces latch contention on identity-based keys
- No changes to schema shape, constraints, or indexes beyond PK optimization
This improves concurrent insert performance under high write workloads by allowing SQL Server to better handle last-page insert contention on sequential identity keys.
See docs on OPTIMIZE_FOR_SEQUENTIAL_KEY: https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-index-option-transact-sql?view=sql-server-ver17#optimize_for_sequential_key---on--off-
* chore(mssql): enable XACT_ABORT for all stored procedures used by Eventuous
Added SET XACT_ABORT ON to all SQL stored procedures invoked through
GetStoredProcCommand() to ensure atomic rollback behavior when the client
transaction encounters an error. This prevents partial commits and
inconsistent transactional states during event append and stream operations.
Related to issue #429
* fix(mssql): truncate_stream wasn't properly formatting and and sending the error message
* refactor(mssql): use SCOPE_IDENTITY() to get new StreamId rather than querying the table for the record we just inserted
* fix(mssql): correct backwards read bounds check
Replaced invalid guard `@current_version < @from_position + @count` with proper
range validation `@from_position < 0 OR @from_position > @current_version`.
Previous logic incorrectly returned no results when valid messages existed.
New check correctly validates start position independently of page size.
* fix(mssql): moved stream version update inside TRY block
and added `@@ROWCOUNT` check to detect concurrent updates.
If no rows are affected, the procedure now
throws a `WrongExpectedVersion` error, aligning behavior with duplicate
append handling.
- Prevents silent version overwrite on race conditions
- Ensures consistency with optimistic concurrency semantics
- Keeps existing WrongExpectedVersion error format for client handling
---------
Co-authored-by: Neal Mummau <[email protected]>1 parent f4444af commit c489f67
File tree
9 files changed
+282
-133
lines changed- src
- Postgres/src/Eventuous.Postgresql/Scripts
- SqlServer/src/Eventuous.SqlServer/Scripts
9 files changed
+282
-133
lines changedLines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
Lines changed: 12 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
| |||
42 | 44 | | |
43 | 45 | | |
44 | 46 | | |
45 | | - | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
| |||
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
56 | | - | |
57 | | - | |
| 58 | + | |
| 59 | + | |
58 | 60 | | |
59 | 61 | | |
60 | | - | |
| |||
Lines changed: 95 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
9 | 15 | | |
10 | 16 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
| 17 | + | |
| 18 | + | |
15 | 19 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
20 | 24 | | |
21 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
22 | 34 | | |
23 | 35 | | |
24 | | - | |
25 | | - | |
26 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
27 | 77 | | |
28 | 78 | | |
29 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
30 | 86 | | |
31 | | - | |
32 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
33 | 93 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 94 | + | |
| 95 | + | |
38 | 96 | | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
Lines changed: 38 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | | - | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
10 | 16 | | |
11 | | - | |
| 17 | + | |
12 | 18 | | |
13 | | - | |
| 19 | + | |
14 | 20 | | |
15 | 21 | | |
16 | 22 | | |
17 | 23 | | |
18 | 24 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
23 | 35 | | |
24 | 36 | | |
25 | 37 | | |
26 | 38 | | |
27 | 39 | | |
28 | 40 | | |
29 | | - | |
| 41 | + | |
30 | 42 | | |
31 | | - | |
32 | | - | |
33 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
34 | 48 | | |
35 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
36 | 52 | | |
37 | 53 | | |
38 | | - | |
| 54 | + | |
| 55 | + | |
39 | 56 | | |
40 | 57 | | |
41 | 58 | | |
42 | | - | |
43 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
Lines changed: 19 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
4 | | - | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
| 6 | + | |
| 7 | + | |
14 | 8 | | |
15 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
Lines changed: 38 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
9 | 13 | | |
10 | | - | |
11 | | - | |
12 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
13 | 19 | | |
14 | | - | |
15 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
16 | 24 | | |
17 | | - | |
18 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
19 | 30 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
26 | 37 | | |
27 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
0 commit comments