Load SBOM advisory matches in one query instead of each_batch

What does this MR do and why?

When a dependency scanning job sends its SBOM, we look up advisories for the components 50 at a time. That lookup joins pm_affected_packages against the 50 component names and was iterated with each_batch. Before each_batch reads anything it first asks for the smallest matching id:

WITH occurrences_cte(purl_type, name) AS (VALUES (5, 'org.slf4j/slf4j-api'), ... 50 rows ...)
SELECT DISTINCT id FROM pm_affected_packages
INNER JOIN occurrences_cte ON ... purl_type ... AND ... package_name ...
ORDER BY id ASC LIMIT 1

Because of the ORDER BY id LIMIT 1, Postgres sometimes decides it is cheaper to walk the whole table in id order and stop at the first match than to look up each of the 50 names in the index. On a customer's database that walk took 28 seconds on a 3.9M row table, well past the 15 second statement timeout, and every dependency scanning job failed with HTTP 410 because the scan never finished. The same query with 22 names took 0.25 ms. The shorter list is not the fix, it just happened to sit on the right side of the planner's cost estimate.

The change drops each_batch for this lookup and loads the matches in one query:

WITH occurrences_cte(...) AS (VALUES ...)
SELECT DISTINCT pm_affected_packages.* FROM pm_affected_packages INNER JOIN occurrences_cte ON ...

With no ORDER BY and no LIMIT there is nothing for the planner to discount, so it uses the (purl_type, package_name) index. The result is already bounded by the 50 component slice, which is why each_batch was not buying anything here. The same change is applied to Sbom::CreateVulnerabilitiesService, which ran the identical loop, and the malware advisory path shares the builder's iteration so it is covered too. A regression spec checks that no query combines the component join with ORDER BY ... LIMIT.

Reproduction

Reproduced on a GDK Postgres 17 with 3.8M real pm_affected_packages rows and the customer's exact 50 names. GDK runs with random_page_cost = 1, which keeps the index plan at any list size, so the flip needs the planner to price index reads closer to what it does on RDS. With SET random_page_cost = 8 the 50-name probe picks the same plan tree as the customer and runs 12.9 seconds reading every row, while the 22-name probe stays on the index at 0.1 ms:

 Limit  (cost=1000.45..8538.56 rows=1 width=8) (actual time=12889.079..12898.191 rows=1 loops=1)
   Buffers: shared hit=1015427 read=83079 dirtied=3556
   ->  Unique  (cost=1000.45..3106698.48 rows=412 width=8)
         ->  Gather Merge  (cost=1000.45..3106697.45 rows=412 width=8)
               Workers Planned: 2  Workers Launched: 2
               ->  Nested Loop  (cost=0.43..3105649.87 rows=172 width=8) (actual time=60.172..8610.085 rows=22 loops=3)
                     Join Filter: ((pm_affected_packages.purl_type = "*VALUES*".column1) AND (pm_affected_packages.package_name = "*VALUES*".column2))
                     Rows Removed by Join Filter: 63631563
                     ->  Parallel Index Scan using pm_affected_packages_pkey on pm_affected_packages  (cost=0.43..911412.50 rows=1595809 width=25) (actual rows=1272632 loops=3)
                     ->  Values Scan on "*VALUES*"  (cost=0.00..0.62 rows=50 width=36) (loops=3817896)
 Planning Time: 0.641 ms
 Execution Time: 12898.231 ms

The estimate for the join never changes with the number of names (412 rows here, 618 on the customer), because it comes from reltuples / n_distinct(package_name), and n_distinct is sampled at 9,292 against an actual 54,791. The LIMIT 1 then prices the primary key walk as 1/412 of a full scan while the index path grows with each name, so past a certain list size the walk wins. The walk only takes seconds because Gather Merge has to get a first row from every worker. The single query this MR uses instead stays on the index plan at every cost setting tried, 0.36 ms for the same 50 names.

Relates to

https://gitlab.com/gitlab-com/request-for-help/-/work_items/5619

Edited by Ahmad Zaydan

Merge request reports

Loading
Loading