Skip to content

fast-stats over-count: novelty asserts not reconciled against the base index #1391

Open

Description

@bplatz

Summary

assemble_fast_stats applies a blind ±1 per novelty flake on top of the indexed base counts, with no reconciliation against whether the asserted fact already exists in the base index. An idempotent re-assert of a fact that has already been reindexed into the base therefore transiently inflates fast-stats counts until the next reindex collapses the duplicate.

Detail

  • fluree-db-novelty/src/runtime_stats.rs (assemble_fast_stats_inner): delta = if flake.op { 1 } else { -1 }, added to indexed.flakes and the per-graph / class / property counts, with no base-presence check.
  • Within-novelty dedup (NoveltyFactState) prevents re-asserting a fact already asserted in the novelty window, but it is novelty-window-scoped and cannot see the base index. Once a fact is reindexed into base (and dropped from novelty), a subsequent assert of the same (s, p, o, dt, m) is kept as a novelty flake and counted again.
  • Net effect: counts (total flakes, class instance counts, property counts) can over-report by the number of such idempotent re-asserts, until the next reindex merges novelty into base under set semantics and the count self-corrects.

Scope / severity

  • Pre-existing; affects the normal reindex path. Not introduced by the segmented-novelty rework (perf: append-only segmented novelty — O(batch) write path #1302) — surfaced during its review.
  • Transient (self-corrects at the next reindex), and query results are unaffected — set semantics make the duplicate assert idempotent at read time. Only the stat counts drift.

Note on whether to fix

These stats are used primarily for query optimization / planning, not as an authoritative user-facing count. Reconciling every novelty assert against base presence (e.g. a base-index existence probe per asserted fact) could add non-trivial cost to the stats-assembly path. If making the counts 100% accurate carries a significant performance implication, it may not be worth doing — the planner tolerates approximate counts. Recording here for future consideration / measurement rather than as an urgent fix.

Activity

  1. added
    P2
    area:query
    on Aug 25, 2026
  2. aaj3f commented on Aug 26, 2026

    @aaj3f
    Contributor

    Scoping note so this issue stays truthful once #1699 lands: that PR is deliberately marked Partially addresses, not Fixes, and this issue should stay open with the remainder below.

    What #1699 does close: the blind +1/-1 novelty delta on the user-facing count surfaces — fluree info (both the full and fast ledger_info arms), apoc.meta.data, and the db.* catalog shims — now resolve against base-index presence rather than charging a re-asserted already-indexed fact a second +1. It also closes the mirror case nobody had noticed, where a DELETE matching nothing charged -1 against a count that never included it.

    What remains, and why the issue stays open:

    1. Above the MAX_RECONCILED_NOVELTY_FLAKES cap (50k), the fixed surfaces still drift. The probe declines and both lanes fall back to the estimate. This one alone makes "counts no longer drift" untrue, which is why the PR doesn't claim to fix this issue.
    2. The planner (stats_cache) and policy_builder lanes keep the blind merge by design — per-identity probes there would be quadratic in the window for the planner, and uncached per-query for policy. Right call, but it means those lanes still carry the drift.
    3. stats.size still charges duplicate bytes — only counts were reconciled.
    4. LedgerState::current_stats() is structurally unable to reconcile (no probe available at that layer).
    5. class_property.rs retains the same blind ±1 shape, seeded from a prior. It's currently unreachable — compute_class_property_stats_parallel has no call sites — so it isn't a live bug, but anyone wiring that module up would inherit it.

    An adversarial review of #1699 also disproved, by execution, the invariant its first draft rested on: novelty's set semantics are one-sided (lib.rs:887 gates on flake.op &&, so redundant asserts are suppressed but retractions are always accepted), and a same-t assert+retract resolves retract-wins while cmp_post sorts op ascending — so kept flakes do not alternate. The shipped resolver folds presence rather than assuming alternation, which handles arbitrary arrival order including two same-t pairs back to back.

    Measurement, since this issue asked for it: ~4.3µs per flake — a 10k-flake window goes 1.5ms → 41.7ms, 48k goes 7.6ms → 210ms, and a 50,008-flake window declines at the cap with both lanes converging.

  3. aaj3f commented on Aug 27, 2026

    @aaj3f
    Contributor

    Correcting a claim in this issue, since it's now been disproved by execution.

    The body says of the blind ±1 novelty lane that "query results are unaffected — set semantics make the duplicate assert idempotent at read time. Only the stat counts drift."

    That holds for the assert direction it analyzes. It does not hold for the retract direction, because one consumer of the merged stats view isn't an estimate at all: StatsView::property_ref_only feeds filter_fold's node-only soundness guard, and merge_property_datatypes drops any datatype whose merged count reaches zero. A spurious -1 — from a delete that matches nothing, or a replayed delete — can drop a predicate's last literal datatype tag, flip the flag, and license an unsound query rewrite.

    Demonstrated end to end: three triples under a mixed ref/literal predicate, a DELETE DATA of triples that aren't in the graph (a spec no-op), and a FILTER(?x = ?y) goes from 3 rows to 5 — the two extra rows asserting "abc"^^xsd:string = "abc"@en. The data is provably unchanged across the delete. It heals at the next index publish, because the indexer's on_record_with_base_presence already reconciles retractions against base presence, so the corruption window is bad-delete → next publish.

    Tracked as #1721, with a second and unconditional defect in the same fold as #1723. Neither is a reason to reconcile the planner lane — that's still quadratic in the novelty window and still the right call to leave on Estimate; the repair belongs on the consumer side.

    Flagging it here because this issue's severity note is the thing a reader would rely on to decide the blind lane is harmless, and in the retract direction it isn't.

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

    P2area:query

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions