Skip to content

Content Drive: size the folder-listing DB chunk from the observed permission-visible ratio #37665

Description

@fabrizzio-dotCMS

Description

BrowserAPIImpl#getContentByChunks reads the folder listing in fixed 400-row
chunks, permission-filters each chunk in Java, and repeats until it has a full
page of visible items. The chunk size never changes, and every chunk re-runs the
whole candidate query with a larger OFFSET.

PostgreSQL cannot jump to row N — it produces and discards the N preceding rows
— so N chunks at growing offsets re-read the prefix N times. The cost is
quadratic in chunk count
, and the chunk count is driven by how much the
permission filter discards, not by folder size:

user visible in a 21,383-child folder chunks
administrator everything 1
reads 1.4% 151 of 11,000 8
reads none 0 54

The loop already knows the visible/read ratio after its first chunk, so it can
size the next fetch to what is actually needed instead of asking for 400 rows 54
times. Total rows the request must read barely changes; what collapses is how
many times the prefix is re-read.

This predates #37229 — the materialized CTE raised the per-chunk constant and
made it visible, but the quadratic was already there.

Measured

Prototype (~40 lines, behind BROWSER_DB_CHUNK_ADAPTIVE) measured on a local
instance built from main @ ba7ab6f0a2, 59k identifiers, PostgreSQL 18,
7 repetitions per case, warmup discarded, VACUUM ANALYZE before each run:

case today adaptive
21,383-child folder, user reads none 5,026 ms 1,258 ms -75%
21,383-child folder, user reads 151 5,009 ms 1,344 ms -73%
11,000-child folder, user reads none 1,619 ms 669 ms -59%
11,000-child folder, user reads 151 510 ms 242 ms -52%
any folder, administrator 192 ms 202 ms unchanged

Blocks read per request fall 9x (5,876,712 → 652,968). Chunks fall from 54 to 6.
Full page walks following the cursor to the end return identical items in
identical order, no duplicated or skipped pages.

The worst case lands at 1.3 s — below the 3.5 s it cost before #37229 existed,
so this does not merely cancel that issue's regression, it improves on the
behaviour that preceded it.

Approach

Two regimes, both in the chunk loop — no change to the SQL or to the permission
filter:

  • Some rows were visible. The ratio is measurable: request what is still
    needed at that ratio, times a safety factor, since the ratio is a sample and
    the next stretch of the folder may be sparser.
  • No rows were visible. There is no ratio to extrapolate from, so double
    instead. Geometric growth bounds the overshoot at one chunk's worth of extra
    rows while turning a linear walk into a logarithmic one.

Never below the caller's current chunk size, never above a configured ceiling or
what is left of BROWSER_DB_MAX_SCAN_ROWS.

Scope to the permission-only path. The Elasticsearch-narrowed path is bounded by
elapsed time and a separate hard cap (#37211, #37184); growing its chunks would
change how those bounds behave.

Ruled out

Keyset pagination was prototyped and measured as a dead end for this query:
6% slower than OFFSET, blocks read essentially unchanged (5,767,884 vs
5,876,712). It converts a skip into a seek only when an index supplies the order;
here the order comes from an explicit sort over a four-table join, and no index
can provide it because mod_date lives in contentlet while parent_path lives
in identifier. The predicate trims rows only after the work is already done.
Recorded so nobody spends the time twice.

Acceptance Criteria

  • Tests written, dev-approved, and confirmed failing before any implementation
    code (Constitution Principle V)
  • The chunk size grows from the observed permission-visible ratio, and falls
    back to geometric growth when no rows were visible
  • Chunk size never drops below the caller's configured size and never exceeds
    either the configured ceiling or the remaining scan budget
  • A listing returns the same items in the same order with the feature on and
    off, verified by walking every page of a folder to the end — no duplicated
    or skipped pages, on an administrator and on a permission-limited user
  • The worst case in the test matrix (a large folder browsed by a user who can
    read none of it) improves by at least half against the current behaviour
  • An administrator's request is unchanged: still one chunk, no measurable
    latency difference
  • The Elasticsearch-narrowed path is untouched, verified against the cases
    Content Drive: adding a content-type filter to the global search hides matches the search alone found #37211 and Content Drive: field filter re-scans the folder candidate query up to 4x per request #37184 added
  • Behaviour verified unchanged when combined with tag, workflow and field
    filters
  • The feature is behind a configuration flag that restores today's fixed
    chunk size

Priority

Medium

Additional Context

Found while running the permission-scoped QA for #37229 — see the verification
comment on that issue for the full test conditions and the caveat about dataset
scale (this was measured on 59k identifiers, not the 418k reference dataset from
#37183).

Coordinate with #37184, which tunes the chunk multiplier for field filters and
shares this loop.

Activity

  1. moved this from New to Next Sprint in dotCMS - Product Planningon Sep 23, 2026
  2. moved this from Next Sprint to Current Sprint Backlog in dotCMS - Product Planningon Sep 23, 2026
  3. removed their assignment
    on Oct 5, 2026
  4. dario-daza commented on Oct 9, 2026

    @dario-daza
    Member

    ✅ QA: passed

    Tested on image 26.10.08-01 (includes #37838), 6 GB heap, toggling BROWSER_DB_CHUNK_ADAPTIVE on and off on the same instance.

    Results:

    • Faster where it matters: a user who can read nothing in a large folder now lists it 73–76% faster on a customer data copy (45k items: 8.5 s vs 35 s) and 57–61% faster on synthetic data, above the 50% the issue asks for. With 10 concurrent requests it was 73% faster with no errors.
    • Same results: full page walks with the feature on and off returned identical items in the same order, with no duplicates or gaps, for admin and limited users, including Tag, workflow and text filters.
    • Nothing else changed: administrators still read one chunk at the same latency. The text and indexed-field search paths keep their fixed chunk sizes. Setting the flag to false restores the old behaviour immediately, with no restart.

    Note: on a 1 GB heap that was already full, the feature was slower than off under concurrent load. It loads up to 2,000 items per chunk, so it needs some memory headroom.

  5. removed their assignment
    on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions