Skip to content

fix(storage): lock storage use rows in id order - #12919

Open
akeldamas wants to merge 1 commit into
IQSS:developfrom
akeldamas:storage-use-lock-order
Open

akeldamas wants to merge 1 commit into
IQSS:developfrom
akeldamas:storage-use-lock-order

Conversation

@akeldamas

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

Locks the storage use rows of a dataset and its parent collections in order of id before updating them. Below about 25,000 datasets and collections, PostgreSQL ran the update as a scan of the whole table and locked the rows in the order in which it found them. Concurrent uploads into one collection could take the locks in opposite order, deadlock and fail.

Which issue(s) this PR closes:

Special notes for your reviewer:

The change is one statement in StorageUseServiceBean.incrementStorageSizeRecursively. A MATERIALIZED CTE selects the rows with ORDER BY storageuse.id FOR UPDATE OF storageuse, and the update joins on it. EXPLAIN shows LockRows above Sort, so the locks are taken in order of id whichever join the planner picks.

On a large table, where the old statement already locked child before parent, the extra lookup costs some throughput of the statement itself: 3,415 against 4,302 updates a second in one pgbench run with 64 clients, about 0.06 ms per update.

Suggestions on how to test this:

  • FilesIT#testCollectionStorageQuotas and FilesIT#testDatasetStorageQuotas check the recorded storage use and pass.
  • With the pgbench reproduction from the issue, on a tree of 2,000 datasets, 30% to 43% of the updates of the old statement fail on a deadlock. The new statement has no deadlock failures.
  • Load test of 6.12 on one machine, three minutes a level, all users starting at once:
Build Users Deposits done Failed Deadlocks Files a second
6.12 32 644 3 3 38.4
6.12 64 97 277 276 6.7
6.12 with this change 32 611 0 0 35.6
6.12 with this change 64 701 0 0 39.3

Does this PR introduce a user interface change? If mockups are available, please link/include them here:

No.

Is there a release notes update needed for this change?:

Yes, doc/release-notes/storage-use-lock-order.md.

Additional documentation:

None.

Below about 25,000 datasets and collections, PostgreSQL ran the storage
use update as a scan of the whole table and locked the rows in table
order. Concurrent uploads into one collection could take the locks in
opposite order and deadlock.
@pdurbin pdurbin moved this to Ready for Triage in IQSS Dataverse Project Nov 16, 2026
@pdurbin

pdurbin commented Nov 24, 2026

Copy link
Copy Markdown
Member

@akeldamas thanks for the pull request! We've been too busy to look at it closely but we will!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for Triage

Development

Successfully merging this pull request may close these issues.

Concurrent uploads deadlock on the storage use update

2 participants