Skip to content

perf(postgresql): replace correlated NOT EXISTS with set-based EXCEPT in prune_stale_cooccurrences (#2660)#2797

Open
handnewb wants to merge 1 commit into
vectorize-io:mainfrom
handnewb:fix/pass3-anti-join-set-based
Open

perf(postgresql): replace correlated NOT EXISTS with set-based EXCEPT in prune_stale_cooccurrences (#2660)#2797
handnewb wants to merge 1 commit into
vectorize-io:mainfrom
handnewb:fix/pass3-anti-join-set-based

Conversation

@handnewb

Copy link
Copy Markdown
Contributor

Summary

The original Pass-3 prune_stale_cooccurrences uses a correlated NOT EXISTS self-join on unit_entities that evaluates per-row. On large graphs:

  • 50K+ entities
  • 1M+ unit_entities rows
  • 660K+ entity_cooccurrences rows

This hits a 300s statement_timeout with 0 rows pruned because Postgres can't complete the per-row evaluation in time.

Changes

Replaced the correlated NOT EXISTS with a set-based EXCEPT:

  1. Materialize the stale cooccurrence target set: cooccurrences EXCEPT valid_witness_pairs
  2. DELETE joining against the materialized stale set

This produces the identical result set (verified via bidirectional EXCEPT and ordered digest match) while completing in ~3.4s vs >300s for the correlated form — ~90x faster.

Verification

  • Same target set: OLD EXCEPT NEW = 0, NEW EXCEPT OLD = 0
  • Ordered digest matches
  • Only targets cooccurrences belonging to the bank (via entity → bank_id join)
  • Preserves duplicate/cooccurrence semantics

Closes #2660

… in prune_stale_cooccurrences

The original Pass-3 correlated NOT EXISTS self-join evaluates per-row
against unit_entities, hitting a 300s statement_timeout on large graphs
(50K+ entities, 1M+ unit_entities). The set-based EXCEPT rewrite
materializes the stale cooccurrence set once and deletes against it —
same result, ~90x faster (~3.4s vs 300s for 5.7K stale rows).

Closes vectorize-io#2660
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.

Pass-3 correlated anti-join is excessively slow; equivalent set-based DELETE is substantially faster

1 participant