Skip to content

Spike: Content Drive folder-listing query has planner-driven latency on very large folders #37183

Closed

Description

@ihoffmann-dot

Research Question

Why does the Content Drive/Site Browser folder-listing candidate-scan query take 564ms on a ~21,000-child folder but stay under 20ms on folders up to ~4,000 children — and can that be fixed without regressing the common (small/medium folder) case?

Timebox

4h

Context

Root cause identified as PostgreSQL planner instability tied to LIMIT: below ~500 rows the planner walks an index on the sort column hoping to satisfy the limit early; above it, it switches to gather+sort, which is ~10x faster on large folders. The production chunk size (400) lands at the worst point of the slow plan.

A candidate rewrite (enter via folder instead of sort column) is 4.3x faster on the largest folder but 3x SLOWER on a mid-size one — rejected as a blanket fix. Raising the chunk size was also tested and rejected (regressed a mid-size folder by 42%).

Latency on the largest folder is also bimodal (some requests ~165ms, ~27% spike to ~587ms) independent of the above, cause unidentified.

What we need to learn

  • Which of three candidate directions (schema/denormalization change, plan-stabilization without rewriting the query, or a distinct large-folder code path) is viable, and at what cost/risk
  • What is causing the latency bimodality on large folders

Acceptance Criteria

  • Findings and a recommended direction (or explicit "needs a follow-up prototype/design work") documented as comments on this issue
  • A recommended direction is agreed with the team before any implementation work starts
  • Coordination constraint with the field-filter chunking fix (Content Drive: field filter re-scans the folder candidate query up to 4x per request #37184) is preserved: whichever direction is chosen must not break the single-scan-per-request fast path that fix depends on

Note on process

This is a spike — per team convention, spikes don't carry a formal Spec-Kit spec/PR. Investigation and findings are tracked as comments on this issue directly.

Parent Epic

#36814

Activity

  1. added
    Type : Spike
    on Aug 24, 2026
  2. github-actions commented on Aug 24, 2026

    @github-actions
    Contributor
  3. ihoffmann-dot commented on Aug 25, 2026

    @ihoffmann-dot
    MemberAuthor

    Findings (spike investigation, live instance)

    Reproduced the reference dataset in Docker and tested directly against Postgres — this is real measurement, not code-reading inference.

    The three candidate directions, resolved

    A usable index already exists — identifier_parent_path_asset_name_host_inode_key (unique index on parent_path, asset_name, host_inode). This closes direction (a)'s open question: no schema change is needed. A bitmap scan on it materializes the folder's full candidate set in ~9ms even for the 21,383-child folder.

    The fix is a query-shape change: wrap the folder lookup in WITH folder_ids AS MATERIALIZED (...) before joining out to contentlet_version_info/contentlet/structure. Forcing materialization (not just writing a CTE — Postgres inlines a plain CTE and can still pick the bad plan) is what makes it stick.

    Folder (children) Baseline Materialized CTE
    /outreach/ (21,383) ~470-490ms ~120-136ms
    /nci/ (4,252) 227ms ~59-61ms
    /chans/ (6,061) 39ms 63-68ms
    tiny (5 children) 0.2-1.6ms 0.2-0.3ms (no meaningful change)

    This reframes the problem: it's not folder size, it's match density in mod_date order

    /nci/ and /chans/ are near-identical in size (4,252 vs 6,061) but baseline behaves oppositely — /nci/ hits the pathological plan (227ms), /chans/ doesn't (39ms, already fine). Size-based routing (the original direction c) would misroute both: it can't tell a slow "medium" folder from a fast one at the same size. The actual driver is where a folder's content falls in the ascending mod_date scan and how densely it's packed there — not observable cheaply without something like the index-driven approach above.

    Practical conclusion: the materialized-CTE approach trades a small, bounded cost on folders the baseline already handled well (+25-30ms on /chans/) for eliminating the unbounded tail on pathological ones (470ms → 120ms). For a user-facing browse feature, bounding worst-case latency is normally worth a modest, predictable tax — the earlier-reported "3x regression on mid-size folders" doesn't reproduce here at this magnitude; it may have been a different-shaped rewrite or a differently-distributed test folder.

    At production scale in this dataset: 106 folders exceed 500 children — the population this actually matters for.

    Latency bimodality — probable mechanism identified, not fully confirmed

    Reproduced directly: running the same query as a PREPARE/EXECUTE sequence on /outreach/, executions 1-5 (Postgres's custom plan, parameter-aware) took 416-447ms; execution 6+ (generic plan, once Postgres decides re-planning isn't worth it) dropped to 100-152ms. That's a ~4x swing, landing squarely in the previously observed bimodal range (~165ms vs ~587ms).

    This is consistent with the earlier hypothesis (plan-cache generic/custom flip), but with a wrinkle: DotConnect.java has CACHE_PREPARED_STATEMENTS defaulting to false (not overridden in shipped config), meaning the application doesn't reuse PreparedStatement Java objects across calls. Whether pgjdbc's own connection-level statement cache still accumulates toward the same threshold independently of that (it can, depending on driver version/config) is not confirmed — no prepareThreshold override was found in the JDBC config either way. Next step to close this: check pg_prepared_statements / pg_stat_activity against a live dotCMS instance under repeated real requests, not just a raw psql session.

    Caveats

    • Tested a representative simplified version of the query (same join skeleton: parent_path, host_inode, deleted, lang, structure join, ORDER BY mod_date) — not the full dynamically-built query with structure exclusions, variant_id, and other optional predicates BrowserAPIImpl adds.
    • Did not re-test permission filtering, or combinations with tag/workflow/field filters end-to-end.
    • Coordination constraint with Content Drive: field filter re-scans the folder candidate query up to 4x per request #37184 still applies: whatever lands here must keep working when invoked at most once per request.

    Recommended next step

    Prototype the materialized-CTE approach as an actual BrowserAPIImpl code change (not raw SQL), re-run the full acceptance-scenario matrix against it (all folder sizes, both roles, deep pagination), and separately confirm the prepared-statement hypothesis against a live instance before deciding whether it needs its own fix.

  4. ihoffmann-dot commented on Aug 26, 2026

    @ihoffmann-dot
    MemberAuthor

    Closing — the spike served its purpose. Findings are documented in the comments on this issue; the validated approach (folder-first materialized CTE) is being implemented in #37229.

  5. removed their assignment
    on Sep 22, 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

    Labels

    Team : ScoutType : Spike

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions