Skip to content

Delete old related info in bulk as late as possible in UpdateOrInsertItems - #17555

Merged
crobibero merged 3 commits into
jellyfin:masterfrom
IDisposable:fix/reorder-update-items
Aug 8, 2026
Merged

crobibero merged 3 commits into
jellyfin:masterfrom
IDisposable:fix/reorder-update-items

Conversation

@IDisposable

@IDisposable IDisposable commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

In UpdateOrInsertItems, the deletion of the related items (BaseItemProviders, BaseItemImageInfos, BaseItemMetadataFields) was being done item-by-item early in the computation of what will be saved. This means we're doing 3n SQL calls to delete each related table's rows. Additionally this takes a write lock out on all the tables touched very early in the processing.

By moving the related rows deletion to just before the SaveChanges call, we can delete all the rows in the related tables with just 3 calls (in bulk across all the modified items), and will take out the lock at the last possible moment.

NOTE

This stacks on top of PR #17554 because it uses the existingItems hashset.

Changes

Added unit test to ensure the same set of rows is DELETE and INSERT as before.
Use bulk deletion of related items using WhereOneOrMany() against all the IDs instead of Where() on each item
Move the deletion of the related items to just before the SaveChanges to hold the lock for far less time.

Code assistance

Claude Code to build and validate the unit test

Issues

Various places where updating a large number of items at once (for example in Scheduled Task sweeps) results in Database/Table Locked errors.

Makes the test O(log n)
Replace the O(n²)  array .First scan with a dictionary lookup
@IDisposable IDisposable added area:database Issues relating to the server databases and/or database access backend Related to the server backend labels Aug 6, 2026
@jellyfin-bot

Copy link
Copy Markdown
Contributor

Jellyfin API (v 12.0.0)


Result


No differences. Specifications are equivalent

@IDisposable
IDisposable requested review from a team, JPVenson and Shadowghost August 6, 2026 04:36
@IDisposable
IDisposable force-pushed the fix/reorder-update-items branch from d52a518 to 9c229d2 Compare August 6, 2026 05:11
This makes the deletion of BaseItemProviders, BaseItemImageInfos, and BaseItemMetadataFields happen in batch as  a contiguous block so the lock isn't held across items, just before the bulk SaveChanges.
@IDisposable
IDisposable force-pushed the fix/reorder-update-items branch from 9c229d2 to 1c5c95a Compare August 6, 2026 06:00
@sonarqubecloud

sonarqubecloud Bot commented Aug 6, 2026

Copy link
Copy Markdown

@Shadowghost Shadowghost moved this from Todo to Review in Jellyfin v12 Aug 6, 2026
@theguymadmax theguymadmax moved this from Review to Approved in Jellyfin v12 Aug 6, 2026
@crobibero
crobibero merged commit 0ec7cc5 into jellyfin:master Aug 8, 2026
19 checks passed
@github-project-automation github-project-automation Bot moved this from Approved to Done in Jellyfin v12 Aug 8, 2026
@IDisposable
IDisposable deleted the fix/reorder-update-items branch August 9, 2026 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:database Issues relating to the server databases and/or database access backend Related to the server backend

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants