Skip to content

indexer: roll up planner-facing property stats after the incremental ndv floor - #1814

Merged
bplatz merged 3 commits into
mainfrom
fix/incremental-stats-flat-ndv
Sep 10, 2026
Merged

bplatz merged 3 commits into
mainfrom
fix/incremental-stats-flat-ndv

Conversation

@bplatz

@bplatz bplatz commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Summary

An incremental build over a base root without HLL sketches (a bulk import, or a ledger's first background build) seeds the properties the write did not touch with empty registers and clamps their finalized ndv to the base root's persisted estimate. The flat IndexStats::properties roll-up, which is the list the planner's StatsView reads, was aggregated from the per-graph entries before that clamp, so every untouched property published ndv_subjects = 0 while the per-graph entries carried the right value.

With ndv 0 the planner estimates a bound-subject probe at its 10-row fallback instead of count / ndv, so a star of three probes compounds to 10k → 100k → 1M rows and the constant-subject patterns are scheduled last. One triple written to a bulk-imported 10M BSBM ledger took Explore Q5 from 6.6 ms to 27.9 ms on 4.2.0 and kept it there. Bulk imports never carry a sketch blob (only a full rebuild uploads one), so this hit every imported ledger on its first write.

The fix aggregates the roll-up from the floored per-graph entries instead.

Test

flat_property_ndv_survives_incremental_over_sketchless_base (fluree-db-api/tests/it_indexing_stats.rs, run through grp_index) bulk-imports 20 subjects, writes one unrelated triple, and asserts the untouched property's count and ndv in the flat roll-up carry through the incremental root. Against the pre-fix code it fails with (0, 0) against (21, 20).

End to end on the 10M BSBM ledger with this branch: after one write and the incremental reindex, Q5 runs in 7.1 ms with the original 583-row driving estimate, instead of 27.9 ms.

…ndv floor

An incremental build over a base root without HLL sketches (a bulk import,
or a ledger's first background build) seeds the untouched properties with
empty registers and clamps their finalized ndv to the base root's persisted
estimate. The flat `IndexStats::properties` roll-up — the list the planner's
StatsView reads — was aggregated from the per-graph entries BEFORE that
clamp, so every property the write did not touch published ndv 0 while the
per-graph entries carried the right value.

With ndv_subjects 0 the planner estimates a bound-subject probe at its
10-row fallback instead of count/ndv, so a star of three probes compounds
to 10k -> 100k -> 1M rows and the constant-subject patterns are scheduled
last. One triple written to a bulk-imported 10M BSBM ledger took Explore
Q5 from 6.6 ms to 27.9 ms on 4.2.0 and kept it there.

Aggregate the roll-up from the floored per-graph entries instead.

Test: flat_property_ndv_survives_incremental_over_sketchless_base indexes
20 subjects, writes one unrelated triple, and asserts the untouched
property's count and ndv in the flat roll-up carry through the incremental
root (fails with ndv 0 before the fix).
@bplatz bplatz added the bug Something isn't working as expected label Sep 8, 2026
@bplatz
bplatz requested review from aaj3f and zonotope September 8, 2026 11:02
@bplatz bplatz added the area:storage Storage backends, nameservice, ledger lifecycle, index storage, caches label Sep 8, 2026
… of racing the indexer

incoming_reference_seek_keeps_base_overlay_lifecycle asserts that the
commit it just made is still trailing novelty, but took its view from
db() after the commit, which races the background indexer: on CI the
overlay commit was already indexed and the fixture assertion failed
(twice on this branch; the test is two days old on main). Commit the
overlay on the indexed ledger state and query the state that comes back,
so the view is an indexed base plus trailing novelty by construction.
@bplatz
bplatz force-pushed the fix/incremental-stats-flat-ndv branch 2 times, most recently from 2457d2d to a1d79e3 Compare September 8, 2026 12:59

@aaj3f aaj3f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really satisfying one-line-of-mechanism fix, @bplatz. So good catch and good fix. The floor had been doing the right thing to graphs[].properties since 6df9cc4 and the planner was just never looking there. I independently confirmed the chain: empty Hll256 registers estimate 0, the flat roll-up was taken before the clamp, StatsView is built from the flat list, and estimate_triple_row_count treats ndv_subjects == 0 as "unknown" and caps a bound-subject probe at 10 rows, so a three-probe star compounds exactly as the body describes. Moving the roll-up under the floor is the right shape (one source of truth for the flat max), nothing else consumed the pre-floor list, the full-rebuild path needs no change, and the regression test reads the flat list — the thing that was wrong — and goes red at (0, 0) vs (21, 20) with the roll-up moved back. The overlay-test commit is a sound race fix and passed 3/3 for me, though I can't reproduce the CI race locally. What I can't verify is the 10M BSBM Q5 timing; the mechanism and the fixture are what I could confirm. Since #1815 is stacked here, merge this first and retarget #1815 to main so it actually gets CI.

Adherence to repo commitments:

  • Patterns/abstractions: ✔ Reuses aggregate_property_entries_from_graphs unchanged; relocates one call; no parallel floor on the flat list.
  • Performance (speed first, memory second): ✔ Indexer-only, per-build (one pass over the predicate list), same input size — neutral; query-side effect is positive (bound-subject probes regain count/ndv). No performance-degradation risk.
  • Testing: ✔ flat_property_ndv_survives_incremental_over_sketchless_base is wired via grp_index (runs under CI's nextest --workspace), passes at HEAD, fails under mutation; indexer unit tests 396/396; overlay test 3/3.
  • Conventions: ✔ Multi-line bodies with mechanism and numbers; clippy -D warnings --all-targets on fluree-db-indexer + fluree-db-api clean; fmt clean.

Verified locally at branch HEAD a1d79e397: cargo test -p fluree-db-api --test grp_index -- flat_property_ndv… (1 passed; pre-floor roll-up mutation → (0, 0) vs (21, 20)), cargo test -p fluree-db-api --test grp_query -- incoming_reference_seek… ×3 green, cargo test -p fluree-db-indexer --lib 396 passed, cargo clippy -p fluree-db-indexer -p fluree-db-api --all-targets -- -D warnings clean, cargo fmt --all -- --check clean.

Approving so you can merge when ready — the only note is the pre-existing sketch-blob caveat above, which is context rather than a change request.

// property (empty registers) published ndv 0 and every
// bound-subject probe on it was estimated at the 10-row fallback —
// one write to a bulk-imported ledger made BSBM Q5 4x slower.
let properties = crate::stats::aggregate_property_entries_from_graphs(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fluree-db-indexer/src/build/incremental.rs:2319 — praise. Deriving the flat entry from the floored per-graph entries (rather than flooring the flat list separately) keeps one source of truth — the flat ndv is the per-p_id max over graphs in aggregate_property_entries_by_sid (stats/mod.rs:128-129), so this is the only ordering under which the two views can't drift. Worth preserving if the class-attribution phase ever moves.

@@ -2285,18 +2285,6 @@ pub async fn incremental_index(
let db_stats = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fluree-db-indexer/src/build/incremental.rs:2248 — optional, pre-existing. Your PR didn't introduce this, but since it's the same mechanism: the sketch blob is uploaded from stats_hook.properties() before the floor, so a seeded property persists with empty registers and its ndv can never grow past the import-time estimate across incrementals — the floor only clamps upward from the base, and the base is itself last time's floored value. 6df9cc4 already named the real fix ("have bulk import produce a sketch_ref"), and that's a genuine scope addition, so I'm not asking for it here; I mention it only so the comment at :2314 doesn't read as if the floor makes ndv accurate rather than non-zero. Maybe one clause in that comment pointing at the import-sketch follow-up? Minor and non-blocking — if you agree, I'd rather see it folded in now than lost.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in f9452fd

let before = indexed_view(&fluree).await;
// Commit the overlay on the INDEXED state and query the state that comes
// back, so the view is "indexed base + trailing novelty" by construction —
// polling `db()` here races the background indexer, which may already have

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fluree-db-api/tests/it_bounded_overlay_translation.rs:940 — praise. Building the overlay on the LedgerState that reindex produced and taking the view from update()'s return value is the right fix for the race: FlureeBuilder::file() does start a background indexer by default (lib.rs:1568, :2931-2993), so polling db() genuinely could see the overlay commit already published. The strengthened assertion (snapshot.t > 0 && t > snapshot.t) also guards the case the old one didn't, where the indexed root hadn't landed yet.

Document that the floor preserves prior estimates without restoring missing HLL registers, and note the bulk-import sketch follow-up.
@bplatz
bplatz merged commit 4e5b9d9 into main Sep 10, 2026
16 checks passed
@bplatz
bplatz deleted the fix/incremental-stats-flat-ndv branch September 10, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:storage Storage backends, nameservice, ledger lifecycle, index storage, caches bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants