Skip to content

The typed source cleanup can rescan its binding match once per source #1483

Done

Description

@gedaiu

Thank you for the note above step 1 of materialize_in_tx about generic plans (#1298): it pointed us straight at the right statement. This is a follow-up observation on the same step, with a shape we tried, and we would like your view before sending anything.

What we see

On one base with 690k rows in typed_fact_sources (2.1M facts, 600k entities), the source cleanup of step 1 ran for twelve hours, with the custom plan. EXPLAIN shows why: the planner estimates one row for the NOT EXISTS body (the binding match on lower(btrim(regexp_replace(s.phrase, …))) is opaque to it), builds that body once without the s.id = src.statement_id correlation, materializes it, and then evaluates the correlation as a join filter against the materialized set for every one of ~600k sources. The real set is far larger than one row, so it is a quadratic scan.

While it runs it holds its transaction open, which in our case also kept a migration's ALTER TABLE and a REINDEX CONCURRENTLY waiting behind it.

What we tried

The same conditions, moved into a set of the sources that still hold, built once, and the delete anti-joined against it:

WITH holding AS MATERIALIZED (
    SELECT src.fact_id, src.statement_id
      FROM typed_fact_sources src
      JOIN facts t ON t.id = src.fact_id AND t.kb_id = $1 AND t.invalidated_at IS NULL
      JOIN facts s ON s.id = src.statement_id AND s.kb_id = $1 AND s.invalidated_at IS NULL
      JOIN entities se ON se.id = s.subject_id
 LEFT JOIN entities oe ON oe.id = s.object_id
      JOIN phrase_bindings b ON {MATCH}
     WHERE b.status = 'bound' AND b.relation_type_id = t.predicate_id
       AND ((b.direction = 'forward' AND t.subject_id = s.subject_id)
         OR (b.direction = 'reverse' AND t.subject_id = s.object_id))
       AND NOT EXISTS (SELECT 1 FROM statement_qualifiers q
                        WHERE q.fact_id = s.id AND q.role = 'mood'))
DELETE FROM typed_fact_sources src
 USING facts t
 WHERE src.fact_id = t.id AND t.kb_id = $1
   AND NOT EXISTS (SELECT 1 FROM holding h
                    WHERE h.fact_id = src.fact_id AND h.statement_id = src.statement_id)

As a read-only count on the same base it finishes in 2 minutes 23 seconds and selects 44,892 sources. The store suite passes unchanged. We believe it deletes exactly the rows the current statement does, since every condition is the same and only its place moved, but you know this step far better than we do.

What we would like to ask

Would you take this shape, or would you prefer another, for instance an index or an expression the planner can estimate? We will keep it on our fork meanwhile, and send a PR here if and how you would like it.

Activity

  1. WaylandYang commented on Nov 25, 2026

    @WaylandYang
    Contributor

    Fixed by #1490 (merged into dev): the typed source cleanup builds the set of sources that still hold once. Closing by hand, since closing keywords don't fire on dev.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions