Skip to content

The typed source cleanup builds the set of sources that still hold once, so a large base's cleanup no longer rescans the binding match per source - #1490

Merged
WaylandYang merged 2 commits into
deeplethe:devfrom
re-cinq:perf/typed-source-cleanup-holding-set
Nov 10, 2026

Conversation

@gedaiu

@gedaiu gedaiu commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1483

The first step of the typed-source cleanup in materialize was a correlated NOT EXISTS over the binding match. On a base with about 690k typed sources the planner estimated one row for that match and rescanned it for every source, so the delete ran for roughly twelve hours. This PR collects the sources that still hold once, in a WITH holding AS MATERIALIZED CTE, and deletes against it with an anti-join. The conditions are the same, including the mood check through no_mood_spoken(), so the same rows are deleted. On that base the new shape, run as a read-only count, finishes in 2 min 23 s and selects 44,892 sources. The statement now lives in a small source_cleanup_sql() function so a test can plan it.

The test is a plan test next to the existing one in materialize_plan_tests.rs. It runs EXPLAIN on the statement and checks that the plan builds CTE holding and the delete scans it, which fails on the old statement. The behaviour tests in tests/store/ are unchanged and green.

This follows the proposal in the issue, and we are glad to reshape it however you prefer. Thank you for looking!

…ce, so a large base's cleanup no longer rescans the binding match per source

Signed-off-by: Bogdan Szabo <bogdan@re-cinq.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, the rewrite is equivalent to the old query. Merging. Not blocking: when you next have the big base, could you post the DELETE plan itself (EXPLAIN, expecting a hash anti join)? The test proves the CTE exists, not that the plan is fast.

@WaylandYang
WaylandYang merged commit 15a2df1 into deeplethe:dev Nov 10, 2026
6 checks passed
gedaiu added a commit to re-cinq/utopia that referenced this pull request Nov 10, 2026
@gedaiu

gedaiu commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Here is the plan you asked for, from our largest base (922k rows in typed_fact_sources for that base, 1.7M in the table). It is a plain EXPLAIN, so it shows the planner's choice and no timings. The delete is a hash anti join over one scan of holding.

One honest caveat. This base has not run migration 0121 yet, so the mood check inside holding is the older single-row form. The rest of the statement is the merged one.

EXPLAIN
Delete on typed_fact_sources src  (cost=221203.39..606580.04 rows=0 width=0)
  CTE holding
    ->  Nested Loop Anti Join  (cost=11.96..155043.28 rows=1 width=32)
          ->  Nested Loop Left Join  (cost=11.53..155042.78 rows=1 width=48)
                Filter: ((s.object_id IS NULL) OR (NOT (b.object_type_id IS DISTINCT FROM oe.type_id)))
                ->  Nested Loop  (cost=11.11..155042.28 rows=1 width=80)
                      Join Filter: ((b.relation_type_id = t_1.predicate_id) AND (((b.direction = 'forward'::text) AND (t_1.subject_id = s.subject_id)) OR ((b.direction = 'reverse'::text) AND (t_1.subject_id = s.object_id))))
                      ->  Nested Loop  (cost=10.68..155041.17 rows=1 width=120)
                            ->  Nested Loop  (cost=10.25..155040.67 rows=1 width=88)
                                  Join Filter: ((b.object_is_value = (s.object_id IS NULL)) AND (b.phrase = lower(btrim(regexp_replace(s.phrase, '\s+'::text, ' '::text, 'g'::text)))))
                                  ->  Nested Loop  (cost=9.82..144600.41 rows=3525 width=87)
                                        Join Filter: (NOT (b.subject_type_id IS DISTINCT FROM se.type_id))
                                        ->  Seq Scan on entities se  (cost=0.00..30370.20 rows=626220 width=32)
                                        ->  Materialize  (cost=9.82..1510.64 rows=12 width=87)
                                              ->  Bitmap Heap Scan on phrase_bindings b  (cost=9.82..1510.58 rows=12 width=87)
                                                    Recheck Cond: ((kb_id = '01a14133-f48c-7183-a3e2-48296b6752d6'::uuid) AND (status = 'bound'::text))
                                                    Filter: ((direction = 'forward'::text) OR (direction = 'reverse'::text))
                                                    ->  Bitmap Index Scan on phrase_bindings_kb_status_idx  (cost=0.00..9.81 rows=552 width=0)
                                                          Index Cond: ((kb_id = '01a14133-f48c-7183-a3e2-48296b6752d6'::uuid) AND (status = 'bound'::text))
                                  ->  Index Scan using facts_live_subject_idx on facts s  (cost=0.43..2.40 rows=25 width=72)
                                        Index Cond: ((kb_id = '01a14133-f48c-7183-a3e2-48296b6752d6'::uuid) AND (subject_id = se.id))
                            ->  Index Scan using typed_fact_sources_statement_idx on typed_fact_sources src_1  (cost=0.43..0.50 rows=1 width=32)
                                  Index Cond: (statement_id = s.id)
                      ->  Index Scan using facts_pkey on facts t_1  (cost=0.43..1.08 rows=1 width=48)
                            Index Cond: (id = src_1.fact_id)
                            Filter: ((invalidated_at IS NULL) AND (kb_id = '01a14133-f48c-7183-a3e2-48296b6752d6'::uuid))
                ->  Index Scan using entities_pkey on entities oe  (cost=0.42..0.49 rows=1 width=32)
                      Index Cond: (id = s.object_id)
          ->  Index Only Scan using statement_qualifiers_pkey on statement_qualifiers q  (cost=0.43..0.46 rows=1 width=16)
                Index Cond: ((fact_id = s.id) AND (role = 'mood'::text))
  ->  Hash Anti Join  (cost=66160.11..451536.76 rows=846864 width=68)
        Hash Cond: ((src.fact_id = h.fact_id) AND (src.statement_id = h.statement_id))
        ->  Hash Join  (cost=66160.07..438622.04 rows=846864 width=44)
              Hash Cond: (t.id = src.fact_id)
              ->  Seq Scan on facts t  (cost=0.00..296658.01 rows=3231464 width=22)
                    Filter: (kb_id = '01a14133-f48c-7183-a3e2-48296b6752d6'::uuid)
              ->  Hash  (cost=31559.81..31559.81 rows=1703381 width=38)
                    ->  Seq Scan on typed_fact_sources src  (cost=0.00..31559.81 rows=1703381 width=38)
        ->  Hash  (cost=0.02..0.02 rows=1 width=88)
              ->  CTE Scan on holding h  (cost=0.00..0.02 rows=1 width=88)
JIT:
  Functions: 60
  Options: Inlining true, Optimization true, Expressions true, Deforming true

Thank you again for the quick merge.

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