Files
ScadaBridge/docs/plans/sql/AddNotificationsListSortIndex.sql
T
Joseph Doherty 1cea01d924 fix(notifications): list sort index + CAS operator Retry/Discard (P5-3, P5-4)
P5-3: add IX_Notifications_Created_NotificationId (CreatedAt DESC,
NotificationId DESC) — the Central UI Notification Outbox list page sorts on
exactly that pair, but no existing index leads unconditionally on CreatedAt,
so an unfiltered/lightly filtered page fell back to a full clustered-index
scan + sort on every COUNT + page request. Mirrors the Medium wave's
IX_SiteCalls_Created_TrackedOperationId migration + SQL twin pattern
faithfully, including the handwritten docs/plans/sql script.

P5-4: operator Retry/Discard was read-check-write keyed on NotificationId
only — a detached PipeTo task interleaving with the dispatcher's own write
(or a second concurrent operator action) could land between the read and
the write and get silently clobbered by the unconditional UpdateAsync,
stamping a Delivered row Discarded with stale fields. Adds
INotificationOutboxRepository.TryUpdateFromParkedAsync, a single-statement
compare-and-swap (WHERE Status == Parked) mirroring
SecuredWriteRepository.TryMarkApprovedAsync's CAS shape. NotificationOutboxActor's
RetryAsync/DiscardAsync now use the CAS write and, on a lost race, re-read
to report "notification already progressed to <Status>" instead of silently
overwriting whatever is there now.

Proof of failure: reverting only the actor's CAS usage while keeping the
repository addition reproduces the silent-overwrite bug — 7 tests fail,
including two new deterministic (non-sleep) race-proof tests that mock the
exact GetByIdAsync -> lost-CAS -> re-read sequence the finding describes.
P5-3 is perf-only; verified via migration/SQL-twin parity instead (no
runtime proof-of-failure is meaningful for an index).
2026-08-19 16:41:04 -04:00

52 lines
2.3 KiB
Transact-SQL

-- arch-review 2026-08-19 P5-3 (register row-35 class): the Notifications list
-- page's sort key gets its own index.
--
-- NotificationOutboxRepository.QueryAsync orders (CreatedAt DESC, NotificationId
-- DESC) (NotificationId breaks CreatedAt ties for deterministic OFFSET paging).
-- None of the existing Notifications indexes lead unconditionally on CreatedAt:
-- - IX_Notifications_Status_NextAttemptAt requires an equality on Status first.
-- - IX_Notifications_SourceSiteId_CreatedAt requires an equality on SourceSiteId first.
-- - IX_Notifications_Delivered is filtered to Status = 'Delivered' only.
-- So an unfiltered (or lightly filtered) page fell back to a full clustered-index
-- scan + sort. IX_Notifications_Created_NotificationId (CreatedAt DESC,
-- NotificationId DESC) makes a forward scan of the index deliver rows in the
-- requested order directly, so a page reads a Top-N off the index instead of
-- sorting the whole table on every COUNT + page request.
--
-- Notifications is small enough today that a plain (offline) index build is
-- fine. On an Enterprise edition instance where the table has grown large and
-- the Central UI must stay responsive, add WITH (ONLINE = ON) to the CREATE
-- INDEX below.
--
-- Run with QUOTED_IDENTIFIER ON. Several tables in this database carry filtered
-- indexes and persisted computed columns, and SQL Server refuses DDL/DML against
-- them with error 1934 when the session has QUOTED_IDENTIFIER OFF -- which is
-- exactly what `sqlcmd` gives you by default (pass -I, or keep this header). The
-- SETs sit in their own batch so they are in force when the statements after the
-- GO parse.
SET QUOTED_IDENTIFIER ON;
SET ANSI_NULLS ON;
GO
BEGIN TRANSACTION;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20260819201210_AddNotificationsListSortIndex'
)
BEGIN
EXEC(N'CREATE INDEX [IX_Notifications_Created_NotificationId]
ON [Notifications] ([CreatedAt] DESC, [NotificationId] DESC)');
END;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20260819201210_AddNotificationsListSortIndex'
)
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20260819201210_AddNotificationsListSortIndex', N'10.0.7');
END;
COMMIT;
GO