Skip to content

Stream the zip file listing with a plan for its whole result - #1662

Merged
danlamanna merged 1 commit into
masterfrom
zip-listing-cursor-plan
Nov 20, 2026
Merged

danlamanna merged 1 commit into
masterfrom
zip-listing-cursor-plan

Conversation

@danlamanna

@danlamanna danlamanna commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Django reads the listing through a server side cursor, and postgres plans a cursor for a cheap first row, assuming only cursor_tuple_fraction (default 0.1) of the rows will be fetched. The listing always fetches every row, and the first-row plan took several times longer. The transaction the view opened ended when the view returned, before the response streamed, so it never covered the listing. It now opens in the writer, around the filename check and the listing, and sets cursor_tuple_fraction to 1.0.

The count query that only decided the filename and the separate licenses query are gone: two ids answer the filename, and the licenses are collected while streaming. Rows are fetched 20,000 at a time, and the per-row work is trimmed.

Minimum of the benchmark rounds on one machine. The fixed dataset has 100,000 images:

search images before after speedup
medium collection 10,000 88.9 ms 34.6 ms 2.6x
malignant 4,991 73.2 ms 22.4 ms 3.3x
all public 46,001 172.5 ms 111.2 ms 1.6x
everything, staff 100,000 367.3 ms 222.6 ms 1.6x

With BENCHMARK_EXTRA_IMAGES=1000000 the dataset has 1,100,000 images, and the same searches match more of them, except the collection:

search images before after speedup
medium collection 10,000 767 ms 117 ms 6.6x
malignant 55,868 3,932 ms 611 ms 6.4x
all public 506,854 5,217 ms 1,528 ms 3.4x
everything, staff 1,100,000 10,766 ms 2,890 ms 3.7x

EXPLAIN ANALYZE DECLARE CURSOR of the staff listing at 1,100,000 images takes 7.0 s with the default cursor_tuple_fraction and 1.0 s with 1.0.

Summary by CodeRabbit

  • Improvements
    • Image download listings are now streamed in batches, supporting more efficient handling of large downloads.
    • Listings include the applicable copyright license files, with duplicates removed and files presented in a consistent order.
    • Download filenames are based on the image IDs included in the listing.

Django reads the listing through a server side cursor, and postgres plans
a cursor for a cheap first row, assuming only cursor_tuple_fraction
(default 0.1) of the rows will be fetched. The listing always fetches
every row, and the first-row plan took several times longer. The
transaction the view opened ended when the view returned, before the
response streamed, so it never covered the listing. It now opens in the
writer, around the filename check and the listing, and sets
cursor_tuple_fraction to 1.0.

The count query that only decided the filename and the separate licenses
query are gone: two ids answer the filename, and the licenses are
collected while streaming. Rows are fetched 20,000 at a time, and the
per-row work is trimmed.

Minimum of the benchmark rounds on one machine. The fixed dataset has
100,000 images:

  search               images    before     after      speedup
  medium collection    10,000    88.9 ms    34.6 ms    2.6x
  malignant            4,991     73.2 ms    22.4 ms    3.3x
  all public           46,001    172.5 ms   111.2 ms   1.6x
  everything, staff    100,000   367.3 ms   222.6 ms   1.6x

With BENCHMARK_EXTRA_IMAGES=1000000 the dataset has 1,100,000 images, and
the same searches match more of them, except the collection:

  search               images     before      after      speedup
  medium collection    10,000     767 ms      117 ms     6.6x
  malignant            55,868     3,932 ms    611 ms     6.4x
  all public           506,854    5,217 ms    1,528 ms   3.4x
  everything, staff    1,100,000  10,766 ms   2,890 ms   3.7x

EXPLAIN ANALYZE DECLARE CURSOR of the staff listing at 1,100,000 images
takes 7.0 s with the default cursor_tuple_fraction and 1.0 s with 1.0.
@danlamanna
danlamanna enabled auto-merge November 20, 2026 07:46
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Walkthrough

The ZIP listing now generates entries during response streaming. The writer manages the transaction and suggested filename, while the generator gathers image and license data during queryset iteration.

Changes: ZIP listing generation

Layer / File(s) Summary
Generate listing entries
isic/zip_download/api.py
The generator reads image fields in 20,000-row chunks, uses configured storage for private images and sponsored storage for public images, and collects image counts and licenses during iteration. It emits sorted license entries.
Orchestrate the streamed response
isic/zip_download/api.py
The view passes the queryset and authentication data to the writer. The writer opens a repeatable-read transaction, sets PostgreSQL’s cursor tuple fraction, derives the suggested filename, and streams generated entries.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant ListingView
  participant Writer as _write_file_listing
  participant PostgreSQL
  participant Generator as _zip_file_listing_generator
  participant Storage
  participant Client
  ListingView->>Writer: Pass queryset and authentication data
  Writer->>PostgreSQL: Open repeatable-read transaction and set cursor tuple fraction
  Writer->>Generator: Generate listing entries
  Generator->>PostgreSQL: Iterate image rows in chunks
  Generator->>Storage: Resolve image files using configured storage
  Generator-->>Writer: Yield listing entries
  Writer-->>Client: Stream entries
Loading

Possibly related PRs

Suggested reviewers: brianhelba

Merge Risk: Minimal · up to 8687f

This change speeds up streamed ZIP listings by planning the query for its whole result and moving the transaction into the writer. No concrete defect was found in the supplied context. The remaining uncertainty is limited to the lack of reported test results and of a test for the suggested filename.

- [ ] 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 27eefd1 into master Nov 20, 2026
2 of 3 checks passed
@danlamanna
danlamanna deleted the zip-listing-cursor-plan branch November 20, 2026 07:50
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