Repository navigation
Description
Description
Spike #37183 validated a concrete direction for the folder-listing candidate-scan query's planner instability on large folders (564ms on a ~21,000-child folder vs <20ms on folders up to ~4,000).
Validated on a live instance (raw SQL, not yet as an application-level change):
- A usable index already exists (
identifier_parent_path_asset_name_host_inode_key, unique onparent_path, asset_name, host_inode) — no schema change needed. - Wrapping the folder lookup in
WITH folder_ids AS MATERIALIZED (...)before joining out tocontentlet_version_info/contentlet/structureforces a stable, fast access path. Plain (non-materialized) CTEs get inlined by Postgres and can still hit the bad plan — materialization is what matters. - Measured: 21,383-child folder 470-490ms → 120-136ms. A folder that was already fast (39ms, 6,061 children) picks up a small, bounded cost (~63-68ms) — not the 3x regression an earlier, differently-shaped rewrite showed.
- Root cause is not folder size — two folders of near-identical size (4,252 vs 6,061 children) showed opposite baseline behavior (227ms vs 39ms) depending on how their content is distributed relative to
mod_dateorder, not their child count. A size-based special case would misroute both. Full detail: Spike: Content Drive folder-listing query has planner-driven latency on very large folders #37183. - At production scale in the reference dataset, 106 folders exceed 500 children — the population this affects.
Scope
- Prototype the materialized-CTE approach as an actual
BrowserAPIImplchange (not the simplified raw SQL tested in the spike) — the real query has additional dynamic predicates (structure exclusions,variant_id, language, etc.) not present in the spike's test query. - Preserve permission filtering exactly as today — untested in the spike.
- Verify against every existing filter combination this query serves (tag, workflow, field filters), not just the unfiltered case.
- Re-run the full size matrix from the original investigation (empty, small, medium, large, extreme folders) plus deep pagination, both roles (admin and permission-limited).
Explicitly out of scope
- The prepared-statement plan-cache mechanism flagged in Spike: Content Drive folder-listing query has planner-driven latency on very large folders #37183 as the probable cause of latency bimodality (~4x swing reproduced directly via
PREPARE/EXECUTE, but not confirmed reachable in production givenCACHE_PREPARED_STATEMENTSdefaults to false). Documented as an open question in Spike: Content Drive folder-listing query has planner-driven latency on very large folders #37183; may warrant its own follow-up if the team wants to pursue it, but does not block this fix — the CTE fix's benefit stands independently. - Content Drive: field filter re-scans the folder candidate query up to 4x per request #37184's field-filter chunk multiplier (separate issue, shares this same underlying query — coordinate before either merges).
Acceptance Criteria
- The materialized-CTE approach is implemented in
BrowserAPIImpland produces identical result sets to the current query, for empty/small/medium/large/extreme folders and at least one deep-pagination case - The largest tested folder drops from the current ~470-490ms baseline to within the same order of magnitude as the spike's ~120-136ms finding
- No folder in the test matrix regresses by more than a small, bounded amount versus its current baseline (spike observed ~25-30ms on an already-fast folder; a large regression anywhere is a signal something differs from the spike's simplified query)
- Permission filtering is verified unchanged for both an administrator and a permission-limited user
- Behavior is verified unchanged when combined with tag, workflow, and field filters
- Tests written and confirmed failing first (Constitution Principle V) before the implementation change
- Coordination with Content Drive: field filter re-scans the folder candidate query up to 4x per request #37184 confirmed: the single-scan-per-request assumption that fix depends on still holds
Related
- Spike: Spike: Content Drive folder-listing query has planner-driven latency on very large folders #37183 (full investigation, measurements, and the folder-size-vs-match-density finding)
- Coordination: Content Drive: field filter re-scans the folder candidate query up to 4x per request #37184 (shares this query)
- Parent epic: [EPIC] Content Drive: Search & Performance at Scale #36814
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone
Activity
github-actions commentedon Aug 25, 2026
PRs linked to this issue
fix(content-drive): fix real bugs found running BrowserAPITest permis…
chore: trigger CI re-run (retry Semgrep scan for #37229)
docs(content-drive): confirm SC-001, correct SC-002's overhead number…
fix(content-drive): scope the id.id ORDER BY tiebreaker to folder-sco…
chore: trigger CI re-run (retry Semgrep scan for #37229, after /fp tr…
chore: trigger CI re-run (retry Semgrep scan for #37229, FP marked in…
fabrizzio-dotCMS commentedon Sep 21, 2026
QA: verified, with one important caveat
Ran the permission-scoped case that the original validation did not cover. The
fix passes — details below — but the exercise surfaced a much larger
optimization opportunity, which I am opening as a separate issue.
Test conditions
Local Docker instance built from
main(ba7ab6f0a2), single binary with alocal kill switch so both query shapes run against identical data and hardware.
Both shapes confirmed in
pg_stat_statements, not assumed.shared_buffers512MB,CACHE_PREPARED_STATEMENTS=false,chunking knobs pinned at their defaults (
BROWSER_DB_CHUNK_FACTOR=10,BROWSER_DB_CHUNK_MIN_SIZE=200,BROWSER_DB_MAX_SCAN_ROWS=50000).identifiers total, folders at 0 / 500 / 4,252 / 6,061 / 7,154 (all file
assets) / 21,383 children, plus a nested tree (11 items with 5,000- and
4,000-child subfolders) and an 11,000-item folder where the test user can read
only 151.
user who can read none of it.
VACUUM ANALYZEbefore each run.Results
Correctness: 60 of 60 cases return the same set of items in both query
shapes, across all three users, unfiltered and with text, tag and workflow
filters, at first page and deep pagination. Some come back reordered, which is
the new
id.idtiebreaker doing its job on rows sharing amod_date. Full pagewalks following the cursor to the end show no duplicated or skipped pages.
As an administrator the fix is neutral to positive, which is what it was
built for. Tag-filtered browsing improves substantially: on the 5,000-child
folder the candidate scan drops from 731,010 to 233,720 buffers.
With a permission-limited user, listing degrades sharply — expected, given
how the algorithm works. Permission filtering happens in Java after the SQL, one
chunk at a time, so a user who can see little forces the loop to fetch chunk
after chunk. An admin needs 1; a user reading 1.4% needs 8; a user reading none
of the 21,383-child folder needs 54. The per-chunk cost the fix added is paid
every time:
Nothing hangs, nothing times out. The worst case in the matrix is a listing
that takes about 5 seconds and then renders correctly. Note also that the two
worst rows were already slow before this change — 3.5 seconds — so this is a
pre-existing cost that the fix makes more expensive, not one it introduced.
Caveat on scale: this database is 59k identifiers against the 418k where the
fix was validated. There,
contentletis large enough that sorting it bymod_dateis genuinely expensive and the CTE earned its 272→114 ms. Thepermission-limited case has never been measured at that scale, so the absolute
numbers above should not be read as production figures. The mechanism —
fixed per-chunk cost multiplied by the chunk count a restricted user forces —
does not depend on dataset size.
The chunk size is a constant 400, and each chunk re-runs the whole candidate
query with a larger
OFFSET. PostgreSQL cannot jump to row N — it produces anddiscards the N preceding rows — so 54 chunks at growing offsets re-read the
prefix 54 times. The cost is quadratic in chunk count, and this predates the CTE
fix entirely.
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. Prototyped and measured on the same rig:
Blocks read per request fall 9x (5,876,712 → 652,968); chunks fall from 54 to 6.
Admin is untouched. Full page walks return identical items in identical order.
This puts the worst case at 1.3 s — below the 3.5 s it cost before this issue
existed. It is ~40 lines in
BrowserAPIImpl#getContentByChunks, behind a flag,and it needs unit and integration tests before it is a PR.
I also prototyped keyset pagination to attack the quadratic directly and
measured it as a dead end here: 6% slower than
OFFSET, because the sortcomes from an explicit sort over a four-table join rather than an index, so the
predicate trims rows only after the work is already done. Recording it so nobody
spends the time twice.
Filed as #37665 (sub-issue of #36814), with the prototype, the measurements and
the keyset dead end recorded there.