Skip to content

Look up fact successors within their knowledge base - #1512

Merged
WaylandYang merged 1 commit into
deeplethe:devfrom
Maya-Kid:codex/scope-fact-successor-walks
Nov 18, 2026
Merged

WaylandYang merged 1 commit into
deeplethe:devfrom
Maya-Kid:codex/scope-fact-successor-walks

Conversation

@Maya-Kid

Copy link
Copy Markdown
Contributor

Why

The successor lookups in evidence attachment and merge rollback only supplied supersedes. The existing facts_kb_supersedes_fk_idx starts with kb_id, so these queries scanned a broad slice of the index or hashed successors from every knowledge base.

This follows up on the review of #1501.

What changes

  • Read the fact's knowledge base with the existing FOR SHARE query, then use it to look up the next revision.
  • Pass the merge's knowledge base to both recursive successor queries, including the copied-evidence cleanup. The same-base foreign key from migration 0070 guarantees the scope of successors.

How it was checked

Compared the complete prepared SQL on Linux PostgreSQL 16.15 with 500,012 facts across 100 knowledge bases, using all migrations from 4ed8a59f. Medians use five paired runs after warmup and measure statement execution.

Statement Before With kb_id
Evidence attachment's next revision 2.283 ms 0.058 ms
Branched rewrite lookup 65.930 ms 0.109 ms
Copied-evidence cleanup 83.571 ms 0.282 ms
Rewrite lookup with 1,000 roots 71.376 ms 1.155 ms
Evidence cleanup with 1,000 roots 100.348 ms 20.902 ms

EXPLAIN (ANALYZE, BUFFERS) shows equality on both index keys for selective lookups and a knowledge-base-scoped bitmap scan for the bulk cases. The test also measured generic prepared plans and the existing row lock query.

The next revision matched across live and invalidated siblings, tied ranking keys, and no-successor cases. Recursive ID sets matched for branches, cycles, duplicate and missing roots, and the 1,000-root batch. The unordered bulk CTE emitted those IDs in a different physical order, and its callers use them for membership and explicitly sort timeline lock keys. Both DELETE variants removed the exact same fact/chunk pairs under rollback.

The full utopia-store suite passed on Linux with PostgreSQL required, including concurrent evidence attachment during a rewrite and the two merge rollback regressions. Formatting and utopia-store Clippy also passed.

Before review

  • The commit is signed off.
  • SQL and application behavior checked with PostgreSQL.

Signed-off-by: Maya Kid <BinjunYann@gmail.com>

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

Thanks — this closes the follow-up from #1501, and the same-base foreign key from 0070 guarantees the added kb_id scope never drops a successor. The measurements are appreciated. Merging.

@WaylandYang
WaylandYang merged commit 315c7f8 into deeplethe:dev Nov 18, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants