Skip to content

fix(transact): a delete of an overflow-IRI object retracts the stored fact - #2142

Merged
bplatz merged 5 commits into
mainfrom
fix/overflow-iri-delete-index
Oct 23, 2026
Merged

bplatz merged 5 commits into
mainfrom
fix/overflow-iri-delete-index

Conversation

@bplatz

@bplatz bplatz commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2138
Follow-up: #1842

Problem

Once a ledger's namespace table is full, a new IRI is stored whole under the OVERFLOW code. Deleting a fact whose object is such an IRI wrote a retraction that didn't match the stored fact:

  • DELETE DATA: the retraction was dropped, because it named no stored fact.
  • DELETE WHERE: the retraction carried the object as Sid(EMPTY, iri).

Indexing the DELETE WHERE commit left the fact in place. It also gave the IRI a second, empty subject under EMPTY, and lookups by IRI check EMPTY first. After that, the IRI's own facts and every reference to it disappeared from bound-IRI queries. On BSBM this affected 37 of 38 deleted offers in one update window. Q10 lost correct rows.

Cause

Staging builds every retraction from the stored fact. Two decodes of a stored subject id produced the wrong identity. Each one resolved the id to its IRI, then re-encoded that IRI through the namespace table, which gives an uncoded prefix the EMPTY code:

  1. The store's object-reference decode (decode_value_v3, IriRef). Staging reads current facts through it.
  2. Staging's materialization of WHERE solutions (materialize_one_binding, EncodedSid).

Fix

Both now decode a subject id the way subject positions already do: resolve_subject_parts plus subject_sid_of_parts, giving the Sid the subject is stored under. In staging that goes through the graph view's resolve_subject_sid, which also covers ids from pending commits.

subject_sid_of_parts now returns an OVERFLOW subject's stored parts directly. It used to look the IRI up in the reverse dictionary, EMPTY first. That cost every overflow object a lookup once objects decoded through it. On a ledger that already holds #2138's empty subject, the lookup also returned that subject, so deletes there still missed.

Ordinary query lanes don't go through these decodes. I ran 15 query shapes over indexed overflow data, covering bound objects, FILTER =, sameTerm, VALUES, IN, joins through the object, GROUP BY and DISTINCT. They returned the same rows with and without this change.

Not in this PR

  • Bound-IRI reads of pending data (Bound-IRI lookups miss overflow-namespace subjects in novelty #1842). A query constant for an overflow IRI still encodes under EMPTY, so it misses pending commits that hold the IRI under OVERFLOW. The regression test reads pending data only through variable joins for this reason.
  • Bound-IRI reads on ledgers that already committed such a delete. New deletes on those ledgers now work, because the decode returns the stored subject rather than the empty one. A query constant for that IRI still encodes as Sid(EMPTY, iri) and names the empty subject, so its bound-IRI reads stay empty until Bound-IRI lookups miss overflow-namespace subjects in novelty #1842 is settled. A rebuild replays the old commits, so it brings the empty subject back.
  • Other resolve-then-re-encode sites, which still give an overflow subject the EMPTY code (Bound-IRI lookups miss overflow-namespace subjects in novelty #1842): DictOverlay::decode_value, decode_overlay_object and term_matches_probe_value in join.rs, the export resolver's object decode, geo_search.rs, shortest_path.rs, fluree-db-shacl/src/sparql.rs, and the indexer's two class lookups in build/incremental.rs. Bound-object queries over indexed overflow data come out right. Export, geo, shortest path, SHACL and the class lookups weren't exercised.

Testing

it_overflow_iri_delete_index (in grp_transact) fills the namespace table, then changes four offers whose vendor and page are overflow IRIs:

  • one deleted with DELETE WHERE;
  • one deleted with DELETE DATA;
  • one deleted with a JSON-LD where/delete;
  • one whose vendor is replaced by upsert with another overflow IRI.

It checks the reads pending, after an incremental index, and after a full rebuild. With the store decode reverted, all three deletes fail, the upserted offer keeps both vendors, and the empty subject appears. With the staging change reverted, the DELETE WHERE offer survives.

overflow_subject_decodes_to_its_own_sid_beside_a_twin builds a store that holds an OVERFLOW subject and a second subject with the same IRI, once under EMPTY and once under a coded prefix. Either way, a lookup by IRI finds the second subject first. The test checks that subject and object decodes of the OVERFLOW id both give the OVERFLOW Sid. Both cases fail without the OVERFLOW early return.

… fact

Once the namespace table is full, a new IRI is stored whole under the
OVERFLOW code. Two decodes of a stored subject id instead resolved its
IRI and re-encoded it through the namespace table, which gives an
uncoded prefix the EMPTY code:

- the store's object-reference decode, through which staging reads the
  current facts every retraction is built from;
- staging's materialization of WHERE solutions.

DELETE DATA's retraction then named no stored fact and was dropped;
DELETE WHERE's carried the object under EMPTY. Indexing that left the
fact in place and gave the IRI a second, empty subject, which reads by
IRI found first: the IRI's own facts and every reference to it
disappeared from bound-IRI queries.

Both now decode a subject id as subject positions already do, to the
Sid it is stored under.
@bplatz bplatz added bug Something isn't working as expected area:storage Storage backends, nameservice, ledger lifecycle, index storage, caches labels Oct 20, 2026
@bplatz
bplatz requested a review from a team October 20, 2026 03:20
The explanation's budget also bounds the materialization it starts with.
At 2 seconds a loaded CI runner capped that materialization before it
derived the fact, so the test failed on its precondition, not on the
replay. The replay costs about thirty times the materialization, and an
8-second budget sits between the two.

@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.

@bplatz nice find, and decoding an object ref the way a subject is decoded, instead of resolving its IRI and re-encoding it, is the right identity. Three headline notes:

(1) The one must below lives in the helper this now routes through. subject_sid_of_parts still looks a full-IRI subject up by its IRI, EMPTY first (binary_index_store.rs:2374-2378), so every overflow object decode now pays a reverse-dictionary lookup, and on a ledger that already holds #2138's empty twin it hands the twin back, so deletes there still don't take. Returning the stored parts for OVERFLOW fixes both (numbers below).

(2) "An IRI's Sid" is still derived per site. Besides the two this PR fixes, DictOverlay::decode_value (dict_overlay.rs:601), decode_overlay_object and term_matches_probe_value in join.rs, export.rs:375, geo_search.rs:285, shortest_path.rs:132, fluree-db-shacl/src/sparql.rs:504 and the indexer's class lookups (incremental.rs:2418, :3773) all resolve an id to its IRI and re-encode it, and query constants still take encode_iri's EMPTY fallback (#1842, #1891). I ran 15 bound-object query shapes on indexed overflow data, with and without novelty, and they all come out right at this head, so not this PR. The structural direction I'd suggest is that all of those go through resolve_subject_sid/subject_sid_of_parts and nothing re-encodes.

(3) For ledgers that already took one of these deletes, the must below makes new deletes work again, but a bound-IRI read of a twinned IRI still finds the empty twin, and changing the fallback lookup order didn't bring those reads back in my run. I think that's the query constant itself (Sid(EMPTY, iri)) naming the twin exactly, which makes it #1842's decision rather than this PR's. It might be worth saying so on #1842, since until it's settled a bound-IRI read of an IRI that already has a twin stays empty on that ledger.

Claude-assisted review below:


✅ Approving to unblock you — with 1 thing that needs to be addressed before this merges.

Must address:

  1. subject_sid_of_parts resolves an OVERFLOW subject by IRI, EMPTY first (fluree-db-binary-index/src/read/binary_index_store.rs:2374-2378; inline at :1585). With the object decode routed through it, a PSOT range read over 40,000 rows whose objects are overflow IRIs (range_with_overlay, debug build, min of 7) takes 298 ms on main and 447 ms at this head. And on a ledger whose dictionary already holds #2138's empty twin (written by main's DELETE WHERE), this head's DELETE WHERE stages the retraction with the twin's Sid(EMPTY, iri) again, so the facts stay. Returning Sid::new(OVERFLOW, suffix) for an OVERFLOW subject fixes both: 99 ms on the same read, the PR's test still passes, and the deletes on that corrupted ledger take.

Otherwise this looks right to me. A few smaller things:

  • 🟡 the decode change also fixes upsert, which the test doesn't cover (inline at it_overflow_iri_delete_index.rs:93).
  • On the "Not in this PR" list: #1842 is genuinely separable, since it needs the encode_iri decision the issue lays out and touches every caller of that fallback. For ledgers that already committed such a delete, the write side is the must above and the read side is note (3), which I think belongs with #1842.
  • The replay-deadline budget bump in it_datalog_explain.rs reads fine to me as its own commit; CI ran it at 8.1 s.

Adherence to repo commitments:

  • Patterns/abstractions: ✔ moves both decodes onto the existing subject-decode helper instead of adding a third rule; ⚠️ about eight other resolve-then-re-encode sites remain (headline note 2).
  • Performance (speed first, memory second): 🔴 the object decode now inherits subject_sid_of_parts' reverse-dictionary lookup for every overflow object: +50% on the range read above, on exactly the ledgers this PR is for (#2135: a BSBM import leaves 95.6% of subjects in overflow). The one-line change in the must makes that read faster than main.
  • Deployment targets: ✔ n/a — host-neutral decode and staging code; it behaves the same in the server, the embedded library and wasm32, and touches no runtime fence.
  • Testing: ✔ it_overflow_iri_delete_index (wired through grp_transact) covers DELETE WHERE, DELETE DATA and a JSON-LD where/delete across pending, incremental and full-rebuild reads, ran in CI by name, and goes red with the two decodes put back to main. ⚠️ No upsert row, and nothing exercises a ledger that already holds the twin.
  • Conventions: ✔ multi-line commit bodies; the CI-budget fix is its own commit with its reasoning.

Verified locally at branch HEAD (000d37c69): deletes_of_overflow_iri_objects_read_the_same_once_indexed passes; with binary_index_store.rs and stage.rs put back to main it fails exactly as the PR body describes (each deleted offer keeps 2 facts, and ?s vendor <V> and <V> ?p ?x read 0 after both builds). Scratch tests next to it: the 15 bound-object shapes (both join orders, a three-pattern star, VALUES, FILTER EXISTS, MINUS, OPTIONAL, FILTER =/sameTerm/IN, COUNT), identical with and without novelty; an upsert replacing an overflow-IRI vendor (wrong on main, right here); the range read above at main, this head and with the one-line change; and a ledger corrupted by main's code, then deleted from under this head and under the change. git merge-tree against #2144, #2154 and #2153 is clean.

Approving now so you can merge without waiting on another pass from me — just be sure the subject_sid_of_parts change is in before you do.

// code, a Sid no stored fact has.
let sid = self
.resolve_subject_parts(o_key)
.and_then(|(ns_code, suffix)| self.subject_sid_of_parts(ns_code, suffix))

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.

🔴 Must address before merge: subject_sid_of_parts looks an OVERFLOW subject up by IRI, which makes this decode slower on overflow-heavy ledgers and gives back #2138's twin on ledgers that already have one.

fluree-db-binary-index/src/read/binary_index_store.rs:2374-2378: for a full-IRI namespace (EMPTY or OVERFLOW) it returns find_subject_sid(&suffix), which goes through find_full_iri_subject_fallback and tries EMPTY before OVERFLOW. 09aea1b kept that lookup on purpose for subject positions; routing the object decode through it adds a reverse-dictionary lookup to every overflow object this decode sees (BinaryGraphView::decode_value, so the range provider, staging's current-fact reads, history and the decode_value_v3 query paths), where before it was a namespace-map miss in encode_iri.

Two things I measured. A PSOT range read (range_with_overlay, p = vendor) over 40,000 rows whose objects are overflow IRIs, debug build, min of 7 runs: 298 ms on main, 447 ms at this head. And I wrote a ledger with main's code (the PR's fixture plus DELETE WHERE { <O1> ?p ?o }, then an incremental build), so its dictionary holds the empty twin of the vendor IRI the way #2138 describes. Then from this head, DELETE WHERE { <O3> ?p ?o } stages its vendor retraction with o = Ref(Sid { namespace_code: 0, name: "http://vendors.example/v/1" }), while page comes out under 65534. After an incremental build, O2, deleted the same way, keeps 1 of its 3 facts, and O1 still has both of its survivors.

The narrow fix I tried is to return the stored parts for OVERFLOW, since an OVERFLOW subject is stored under its full IRI and nothing can register its prefix once the table is full:

if ns_code == namespaces::OVERFLOW {
    return Ok(Sid::new(ns_code, suffix));
}

With that, the same range read takes 99 ms, deletes_of_overflow_iri_objects_read_the_same_once_indexed still passes, the 15 bound-object shapes come out the same, and on the corrupted ledger DELETE WHERE of O1, O2 and O3 leaves all three empty after an incremental build. It also drops the lookup for overflow subjects in subject position, which is most of a BSBM import (#2135). If an OVERFLOW subject can also be stored under a coded prefix in some state I'm not thinking of, that's the case to check, and a test for it would be worth adding either way.

Commenting here because subject_sid_of_parts is not in this diff.

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 3323124

),
)
.await;
let ledger = fluree

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.

🟡 Optional: an upsert row would pin a path this PR fixes but doesn't test.

Upsert's replacement wave reads the current values through the same store decode, so it had the same bug. I ran it as a scratch test with this fixture: upsert O1's vendor from V to another overflow IRI, then an incremental build. On main, O1 ends up with both vendors and V reads empty by IRI; at this head it has one vendor and V reads right. Sync's whole-graph scan goes through the same decode too, though I didn't run that one.

Something like a fourth offer replaced by fluree.upsert(ledger, &json!({"@id": O4, VENDOR: {"@id": V2}})) (with V2 another overflow IRI), and its reads added to checks, would cover it. It's minor and non-blocking, but if you agree it's right I'd rather see it folded in now than lost in the backlog.

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 3323124

… under

Decoding a persisted OVERFLOW subject looked its IRI up in the reverse
dictionary, EMPTY first. Object references now decode through the same
helper, so every overflow object paid that lookup. On a ledger that
already holds an EMPTY subject with the same IRI (left by #2138's
deletes), the lookup returned that subject, and a delete staged from it
still named no stored fact.

An OVERFLOW subject is stored under its full IRI, so its stored parts
are its Sid. They are returned directly, without the lookup.

The overflow-delete test now also replaces one offer's vendor by
upsert. Upsert retracts the current value it reads through the same
decode, and before this branch it kept both vendors.
@bplatz
bplatz merged commit b439089 into main Oct 23, 2026
19 checks passed
@bplatz
bplatz deleted the fix/overflow-iri-delete-index branch October 23, 2026 21:11
bplatz added a commit that referenced this pull request Oct 24, 2026
Brings #2142, #2144, #2154, #2155, #2156, #2158, #2159, #2165, #2166 and
#2167 into next.

Where main's RDF lists (#2159) meet next's RDF 1.2 links:
- The list view walks an EdgeAnnotation's body; next has no
  AnnotationTarget.
- Export marks annotated edges skipping list rows, and writes an
  rdf:reifies row as next's @reifies block through main's JsonLdNode,
  untranslated rows included.
- The JSON-LD and import readers keep next's graph argument and
  triple-term spool records beside main's gap-free list positions and
  list chunking.
- One collection writer: main's shared module, which both JSON-LD and
  Turtle use, replaces next's Turtle-only one, and now also keeps the
  labels of blank nodes inside triple terms, of reifiers, and of blank
  graph names. A reified cell keeps its triples while the well-formed
  tail after it folds, as JSON-LD's list conversion does; next's writer
  test now says so.
- Hiding list members also turns off next's wildcard lane, which reads
  leaflets directly, and next's independent-scan count builds its right
  scan with the same hiding.

Elsewhere:
- List-predicate discovery opens a leaf ranged unless the store cannot
  read ranges, which next fetches whole.
- GC reports main's walk stop reason without next's removed artifact
  cache directory.
- Tests from main await next's async genesis_ledger, reload with
  without_storage_lock, and build stores without a cache directory.
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.

Deleting facts with overflow-namespace IRI objects corrupts the index: retractions survive and the object's IRI stops resolving

2 participants