Defer compactSourceSql JSONB projection until after LIMIT/OFFSET in listMixedGalleryPage

Aperta
#19,065 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Refactoring
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
javascript, postgresql
Ambito
backend, database

Direzione di ricerca

Start in server/services/mediaAssetIndex/db.js, reading assetSource and listMixedGalleryPage, plus compactSourceSql where the JSONB projection is defined. Check how gallery_assets is materialized and how gallery_page applies LIMIT and OFFSET. Done means compactSourceSql and the full data fetch run only on the bounded page rows, with identical results, verified by db.test.js and db.db.test.js.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area:media effort:medium model:medium performance performance-efficiency plan planner:gemini-4-neon source:performance-efficiency

Audit slice: server/services/mediaAssetIndex (re-verification of 2026-11-24 performance-efficiency slice) — Date: 2026-11-26
Scope: small

Problem

In server/services/mediaAssetIndex/db.js:249-264 (assetSource) and :319-357 (listMixedGalleryPage), the gallery_assets CTE is declared AS MATERIALIZED and evaluates CASE WHEN kind = 'image' THEN ${compactSourceSql('data')} ELSE data END AS data for every matching image row in media_assets before gallery_page applies ORDER BY ... LIMIT $limit OFFSET $offset:

  • compactSourceSql() (db.js:189-196) runs a correlated SELECT jsonb_object_agg(...) || jsonb_strip_nulls(...) FROM jsonb_each(data) with regex matching (~) over the row's full JSONB payload.
  • Although #18631 moved search and facet predicates onto stored generated columns (search_text, is_hidden, universe_id, entry_category, entry_kind), placing compactSourceSql('data') inside the MATERIALIZED gallery_assets CTE forces PostgreSQL to read/decompress media_assets.data (including TOASTed JSONB) and run jsonb_each/jsonb_object_agg/regex for every matching image in the entire gallery, even though the four COUNT(*) FILTER (...) aggregates never read data and only limit rows (default 60) ever enter gallery_page.

Impact

Every mixed Media Gallery page load (listMixedGalleryPage, invoked by listGalleryPage in server/services/mediaAssetIndex/gallery.js:155) pays O(total_images) JSONB decompression and per-key aggregation work in PostgreSQL instead of O(limit) (60 rows), causing latency and CPU load to scale linearly with total library size even on paginated reads.

Fix

Update server/services/mediaAssetIndex/db.js (and verify with server/services/mediaAssetIndex/db.test.js and server/services/mediaAssetIndex/db.db.test.js):

  1. Keep filename and cleanedFrom predicates at the source level in listMixedGalleryPage (they are identical across totalFilter, hiddenFilter, facetFilter, and pageFilter), and compute has_cover (kind = 'image' OR NULLIF(video.data->>'thumbnail', '') IS NOT NULL) as a boolean column in gallery_assets so gallery_assets never needs media_assets.data.
  2. Materialize only (media_key, kind, ref, created_at, is_hidden, has_cover, video_data) in gallery_assets (with NULL::jsonb for image rows' video_data, since videos are already compact in the bound parameter).
  3. In the final SELECT over gallery_page (at most limit rows), LEFT JOIN media_assets ma ON ma.media_key = gallery_page.media_key AND gallery_page.kind = 'image' and project CASE WHEN gallery_page.kind = 'image' THEN ${compact ? compactSourceSql('ma.data') : 'ma.data'} ELSE gallery_page.video_data END only for the bounded page rows.

Acceptance Criteria

  • listMixedGalleryPage no longer evaluates compactSourceSql('data') or materializes media_assets.data inside the gallery_assets CTE.
  • compactSourceSql (and full ma.data fetch for images) executes only on the bounded gallery_page (LIMIT/OFFSET) rows via PK join on media_assets.media_key.
  • All filters (q, hidden, kind, filename, cleanedFrom, mediaKeys, excludeKeys, cover, universeId, entryCategory, entryKind, orderedKeys, countMediaKeys, compact) return identical items, totals, hiddenTotal, and counts.
  • Unit and DB tests (server/services/mediaAssetIndex/db.test.js and server/services/mediaAssetIndex/db.db.test.js) pass, including an assertion that compactSourceSql is applied after LIMIT/OFFSET rather than inside gallery_assets.
Lingua principale
JavaScript
Stelle
38
Fork
32
Merge medio
14m
PR unite (30g)
995

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di atomantic/PortOS

Tutte le issue di atomantic/PortOS

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.