fix(data): light statistics lock their sequences before their frames

FrameRepository.UpdateLightStatsAsync updated every frame of a sequence
in batches without locking the sequence row. On PostgreSQL a scan's or
rebuild's sync holds the row and rewrites every frame in its own order,
so a deep analysis writing its statistics at the same time could hold
some frames while waiting for others the sync held, and PostgreSQL
aborted one of the two jobs with a deadlock.

On PostgreSQL the method now locks the rows of its frames' sequences
first, in the same transaction, like every other writer of a sequence's
frames. SQLite's write lock already serialises writers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 22:08:05 +00:00
1 parent bf93434bec
commit 343ef76798
2 files changed
+46 -1

No files matched your search

@@ -80,13 +80,33 @@ public sealed class FrameRepository
_db.ListAsync("SELECT id, idx FROM frames WHERE sequence_id = $1 ORDER BY idx", [sequenceId], r => new FramePosition(r.GetGuid(0), r.GetInt32(1)),
cancellationToken);
/// <summary>Writes analysis results for many frames in one transaction; returns the number of frames updated.</summary>
/// <summary>
/// Writes analysis results for many frames in one transaction; returns the number of frames updated. The rows of their
/// sequences are locked first, as every writer of a sequence's frames does (<see cref="SequenceRepository.GetForUpdateAsync"/>).
/// </summary>
public Task<int> UpdateLightStatsAsync(IReadOnlyList<FrameLightStats> stats, CancellationToken cancellationToken = default) =>
_db.InTransactionAsync(async session =>
{
var updated = 0;
if (_db.Sql.IsPostgres)
{
// The rows before any frame: a scan's sync holds its sequence's row while it rewrites every frame in its own
// order, so frames locked here first would wait for it while it waits for them, and PostgreSQL would abort
// one of the two. SQLite's write lock already serialises writers.
for (var start = 0; start < stats.Count; start += Batch)
{
var count = Math.Min(Batch, stats.Count - start);
var args = new DbParam[count];
var placeholders = new StringBuilder();
for (var i = 0; i < count; i++)
{
args[i] = stats[start + i].FrameId;
placeholders.Append(i > 0 ? ", $" : "$").Append(i + 1);
}
await session.ExecuteAsync(
$"SELECT id FROM sequences WHERE id IN (SELECT sequence_id FROM frames WHERE id IN ({placeholders})) ORDER BY id{_db.Sql.ForUpdate}",
args, cancellationToken).ConfigureAwait(false);
}
// One UPDATE … FROM (VALUES …) per batch instead of a round trip per frame.
for (var start = 0; start < stats.Count; start += Batch)
{