Skip to content

Express image permissions without joins that multiply rows - #1666

Merged
danlamanna merged 1 commit into
masterfrom
image-perms-without-multiplying-joins
Nov 24, 2026
Merged

danlamanna merged 1 commit into
masterfrom
image-perms-without-multiplying-joins

Conversation

@danlamanna

Copy link
Copy Markdown
Member

The share branch of the image visibility filter was Q(shares=user), a LEFT JOIN on the share table. It produced one row per share row of the image, for any grantee, so every caller wrapped its queryset in distinct(), and the first page of a list sorted and deduplicated the full image, accession and cohort rows (72 columns) before Unique could collapse them.

  • The share branch is now a correlated Exists on ImageShare, and the collections filter of a search an Exists on CollectionImage, so nothing multiplies rows and the distinct() calls go (image API, search serializer, collection detail page and attribution endpoint, zip download, pin endpoint).
  • The contributor branch stays a join: the accession is one-to-one and its cohort many-to-one, so it never multiplied rows, and as a subquery Postgres hashes every accession the user owns before the first row (122 ms instead of 1.5 ms for an owner at 1.1M images).
  • ImagePermissions.visible_image_q is the one predicate; build_elasticsearch_query mirrors it as before. The Elasticsearch-backed counts are unchanged.

Benchmarks, fastest round, master vs this branch (the shares user has 221k shared images):

benchmark 100k images 1.1M images
test_image_detail_api[anonymous] 3.7 → 1.6 ms (2.26x) 3.9 → 1.6 ms (2.41x)
test_image_detail_api[shares] 5.8 → 3.8 ms (1.51x) 6.8 → 4.0 ms (1.69x)
test_image_search[collection-shares] 20.1 → 14.2 ms (1.42x) 20.7 → 14.1 ms (1.47x)
test_image_search[license_in_collection-shares] 22.1 → 16.2 ms (1.36x) 23.5 → 16.5 ms (1.43x)
test_image_search[rcm_case-anonymous] 6.4 → 4.4 ms (1.46x) 6.8 → 4.3 ms (1.58x)
test_image_list[anonymous] 11.4 → 8.3 ms (1.38x) 11.1 → 10.5 ms (1.06x)
test_image_list[shares] 14.8 → 11.6 ms (1.28x) 17.4 → 16.3 ms (1.07x)
test_image_list[staff] 11.1 → 9.1 ms (1.21x) 11.8 → 10.1 ms (1.17x)
test_zip_download[attribution-file-all_public] 52.8 → 36.2 ms (1.46x) 589 → 334 ms (1.76x)
test_zip_download[file-listing-malignant] 21.6 → 23.2 ms (0.93x) 926 → 678 ms (1.37x)

Known trade-off: inside an OR, Postgres rewrites the share EXISTS into id = ANY(hashed subquery) and builds the hash of every share the user has before the first row, LIMIT or not. For a user with 221k shares (production has one with 226k) a DSL-filtered search with no collection is slower than master at 1.1M images:

benchmark 100k images 1.1M images
test_image_search[diagnosis-shares] 19.7 → 18.9 ms (1.04x) 22.2 → 46.5 ms (0.48x)
test_image_search[image_type-shares] 17.8 → 16.9 ms (1.05x) 19.4 → 46.1 ms (0.42x)
test_image_search[nested-shares] 25.6 → 24.7 ms (1.04x) 32.2 → 58.8 ms (0.55x)

A second, non-hashable correlated clause blocks that rewrite and fixes this one user, but it costs every other sharing user 20 to 60% on filtered pages and 3 to 5x on unbounded scans such as zip listings and counts, so it was left out. For the median production user, with about 1,000 shares, the hashed plan is the fastest of the three.

Tests: test_image_shares_do_not_multiply_images fails on master ([3, 3, 4, 4] for two images) and covers the new invariant; the existing share, collection and search tests cover the rest.

The share branch of the image visibility filter was a LEFT JOIN that
multiplied rows, so every caller had to wrap its queryset in distinct()
and the first page of a list sorted and deduplicated the full image,
accession and cohort rows. The share branch is now a correlated EXISTS,
and so is the collections filter of a search, so nothing multiplies rows
and the distinct() calls go. The contributor branch stays a join: the
accession is one-to-one and its cohort many-to-one, and as a subquery
postgres hashes every accession the user owns before the first row.
@coderabbitai

coderabbitai Bot commented Nov 24, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b294460a-fc20-4aee-9177-ddf4b6dc6633
Commits

Reviewing files that changed from the base of the PR and between cb1faa9 and 35dd05b.

Files selected for processing: 8

  • isic/core/api/collection.py
  • isic/core/api/image.py
  • isic/core/models/image.py
  • isic/core/search.py
  • isic/core/serializers.py
  • isic/core/tests/test_image_shares.py
  • isic/core/views/collections.py
  • isic/zip_download/api.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@danlamanna
danlamanna merged commit 580211b into master Nov 24, 2026
3 checks passed
@danlamanna
danlamanna deleted the image-perms-without-multiplying-joins branch November 24, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant