Skip to content

A typed or implied row follows its statements' role words on every materialize run - #1544

Open
gedaiu wants to merge 2 commits into
deeplethe:devfrom
re-cinq:feat/role-words-follow-materialize
Open

gedaiu wants to merge 2 commits into
deeplethe:devfrom
re-cinq:feat/role-words-follow-materialize

Conversation

@gedaiu

@gedaiu gedaiu commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Part of #1267, PR 3 of 3, after #1428 and #1450. This builds decision 4 of ADR 0070.

materialize copied evidence and role words onto a typed row only while it was making or merging that row. So a statement re-pushed with lines: "58-70" still read 40-52 on its typed row. Now each run copies, for the whole base, the evidence and role-word rows of every live typed and implied row's statements that the row does not have yet. The copy is the same ON CONFLICT DO NOTHING insert as before. It reads typed_fact_sources and implied_fact_sources, and it runs after the implied rows are written. Implied rows now get their statement's role words as well, which they never did. The per-row copies in the loop and in imply_in_tx are gone, since this step covers them.

As the 0121 revision of decision 1 left for this PR, a typed row whose statements disagree on a role now reads both values, the same as two live statements. a_bound_statement_becomes_a_typed_fact changes for that. Its row now reads since 2019 and since 2021. 0070 gets a dated note on decision 1, its status moves to Implemented, and the README index follows.

Tests: a_re_push_that_moves_lines_moves_the_typed_rows_lines_too re-pushes lines and reads only the new value and both chunks' evidence on the typed row, shows a run for another base leaves it alone, and ends with a re-push that adds mood and retires the row. an_implied_row_reads_the_role_words_of_the_statement_that_implied_it reads the statement's lines on an implied row, then follows a re-push. The new behaviour fails on dev. The mood check is from #1450 and only had a test for the drop direction, so that part is a plain characterization test, which I checked by breaking no_mood_spoken. I ran the store, server, core and cli suites against Postgres with UTOPIA_TEST_REQUIRE_DB=1, and fmt and clippy with -D warnings.

The cost of the re-sync on a large base is still unmeasured, as the record says. Happy to add a bench if you'd like one before this goes in.

Thanks for taking a look!

@gedaiu
gedaiu force-pushed the feat/role-words-follow-materialize branch 2 times, most recently from e71808f to 50704ea Compare November 28, 2026 19:36
…s on every materialize run (deeplethe#1267)

Signed-off-by: Bogdan Szabo <bogdan@re-cinq.com>
@gedaiu
gedaiu force-pushed the feat/role-words-follow-materialize branch from 50704ea to 3fc4945 Compare November 28, 2026 20:08
@gedaiu
gedaiu marked this pull request as ready for review November 28, 2026 20:12
@WaylandYang

Copy link
Copy Markdown
Contributor

Thanks, the logic and the tests look right to me, and moving the copy after the implied rows is a nice cleanup. One thing before it merges: this turns two per-row copies into two whole-base INSERT … SELECTs on every materialize run, so I'd like to take you up on the bench. Could you add one, or post EXPLAIN (ANALYZE, BUFFERS) for both statements on a base with a large number of typed rows (say 100k+)? It should show how long a run takes when there is nothing new to copy, since that's the common case.

…ethe#1267)

On a base with nothing new to copy, ON CONFLICT probed every row and the
UNION sorted both source tables. UNION ALL plus a NOT EXISTS filter takes
a 120k-row run from about 1.6 s to 1.26 s for both statements.
@gedaiu

gedaiu commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the review, and for asking for the numbers. Good call: it turned up a cheap improvement, now pushed as d69d6c8.

Setup. A scratch database with the five tables involved (same columns and indexes as on dev). Base A has 100k typed rows, each from one statement, plus 20k implied rows. Every statement carries 2 quotes and 2 role words, all already copied onto their rows, so this is the "nothing new to copy" case. Base B has 200k more rows, so the kb_id filter has something to skip. In total that is 660k facts, 1.32M fact_evidence rows and 1.32M statement_qualifiers rows. Postgres 16, warm cache, best of three.

fact_evidence statement_qualifiers both
as first pushed (UNION, ON CONFLICT only) 837 ms 772 ms ~1.6 s
d69d6c8 (UNION ALL + NOT EXISTS) 658 ms 605 ms ~1.26 s

What changed. The first version spent about 120 ms per statement sorting both source tables across every base for the UNION, and spilled 15 MB to disk doing it. Each statement then spent about 270 ms letting ON CONFLICT reject the 240k rows that were already there. UNION ALL is safe here, because a duplicate pair just meets ON CONFLICT DO NOTHING. A NOT EXISTS on the target filters existing rows out before the insert (a hash anti-join), and the plan now shows Conflicting Tuples: 0.

EXPLAIN (ANALYZE, BUFFERS) for the fact_evidence statement after the change
Insert on fact_evidence  (actual time=647.784..647.789 rows=0 loops=1)
  Conflict Resolution: NOTHING
  Tuples Inserted: 0
  Conflicting Tuples: 0
  ->  Hash Right Anti Join  (actual time=647.782..647.787 rows=0 loops=1)
        Hash Cond: ((h.fact_id = t.id) AND (h.chunk_id = e.chunk_id))
        ->  Seq Scan on fact_evidence h  (rows=1320000)
        ->  Hash  (rows=240000)  Batches: 4
              ->  Nested Loop  (actual time=40.918..351.655 rows=240000 loops=1)
                    ->  Hash Join  (rows=120000)
                          Hash Cond: (typed_fact_sources.fact_id = t.id)
                          ->  Append  (rows=360000)
                                ->  Seq Scan on typed_fact_sources  (rows=300000)
                                ->  Seq Scan on implied_fact_sources  (rows=60000)
                          ->  Hash
                                ->  Bitmap Heap Scan on facts t  (rows=120000)
                                      Recheck Cond: (kb_id = 'a'::uuid)
                    ->  Index Scan using fact_evidence_pkey on fact_evidence e  (rows=2 loops=120000)
Execution Time: 658.507 ms

The statement_qualifiers plan has the same shape.

Where it stays. The cost still grows with the size of the base: about 5 ms per thousand live typed rows for both statements. That is the same order as the other whole-base steps materialize already runs on every pass (steps 0 to 3, the implication scans and the attestation sync), so it doesn't add a new kind of cost. If you would rather it didn't grow with the base at all, the next step would be to limit the copy to statements whose evidence or role words changed since the last run. That needs a change marker we don't have today, so I'd suggest it as a follow-up rather than growing this PR. Happy to do it whichever way you prefer.

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