Skip to content

fix: load only the vulnerability records of a scan report - #24509

Open
secmff wants to merge 1 commit into
goharbor:mainfrom
secmff:fix-job-service-memory
Open

secmff wants to merge 1 commit into
goharbor:mainfrom
secmff:fix-job-service-memory

Conversation

@secmff

@secmff secmff commented Dec 1, 2026

Copy link
Copy Markdown

fix: load only the vulnerability records of a scan report

Related: #22008, #22059 (earlier attempts #22009 and #22127 were closed as stale)

Problem

After every vulnerability scan, jobservice stores the report in the relational schema (toSchema in src/pkg/scan/postprocessors/report_converters.go). To find the records that already exist, it runs:

c.dao.List(ctx, q.New(q.KeyWords{"cve_id": q.NewOrList(cveIDs), "registration_uuid": registrationUUID}))

This returns every record of every CVE in the report, for all packages and versions that were ever scanned, with all columns, including description, urls and vendor_attributes. Only the records that match the report on cve_id, package and package_version are used; the rest is thrown away.

Common CVEs (glibc, openssl, zlib, ...) have thousands of records, so on a large registry a single report pulls in hundreds of thousands of rows. With an IN list that long, PostgreSQL also gives up on the cve_id index and scans the whole table.

What we see in production:

  • vulnerability_record: about 1.2M rows, 21,444 sequential scans that read 25.5 billion rows in total, so the whole table on every scan.
  • Jobservice uses about 3GB per pod with 3 workers, and is OOM killed during scan-all.
  • PostgreSQL (4 vCores) runs at 60–90% CPU during scan-all, and adding jobservice or Trivy pods doesn't make scanning faster.

Change

  • New DAO method ListByKeys(ctx, registrationUUID, keys): looks records up by (cve_id, package, package_version) for one scanner, which matches the existing unique constraint on vulnerability_record. It queries in batches of 500 keys and only loads id, the key columns, severity and status, which is all toSchema needs to update severity and status.
  • toSchema builds the keys from the de-duplicated vulnerabilities of the report and uses ListByKeys. The update, create and link logic is unchanged.
  • The report JSON is parsed once in ToRelationalSchema and passed to toSchema, instead of being parsed again there.

The memory a job needs now grows with the size of the report, not with the vulnerability history of the whole registry.

Query plan on the production database, with 500 keys:

Nested Loop
  ->  HashAggregate (the 500 keys)
  ->  Index Scan using vulnerability_record_cve_id_registration_uuid_package_packa_key on vulnerability_record  (cost=0.55..4.56 rows=1)
        Index Cond: ((cve_id = ...) AND (registration_uuid = ...) AND (package = ...) AND (package_version = ...))

Tests

  • TestListVulnerabilityRecordByKeys:
    • only exact key matches are returned, not another version of the same CVE and package;
    • records of another scanner are not returned;
    • an empty key list works;
    • large columns are not loaded.
  • TestListVulnerabilityRecordByKeysBatches: more keys than fit in one batch.
  • The existing tests in pkg/scan/dao/scan, pkg/scan/postprocessors, pkg/scan/report, pkg/scan/vulnerability and pkg/scan/export pass.

@secmff
secmff requested a review from a team as a code owner December 1, 2026 08:47
@Vad1mo
Vad1mo requested a balanced review from Copilot December 1, 2026 14:52

Copilot AI 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.

🟢 Approval recommended

No active review issues remain for the targeted vulnerability-lookup changes.

No open issues 🎉

What changed in this PR

This PR reduces memory use and database work during scan-report conversion by fetching only vulnerability records matching the report, addressing the scan-all resource pressure described in #22008 and #22059.

Changes:

  • Adds scanner-scoped lookups by CVE, package, and version, querying 500 keys per batch and loading only required columns.
  • Reuses the parsed report instead of decoding its JSON twice.
  • Adds tests for exact matching, scanner isolation, empty input, limited field loading, and batching.
File Description
src/​pkg/​scan/​postprocessors/​report_converters.go Builds deduplicated lookup keys and reuses the parsed report.
src/​pkg/​scan/​dao/​scan/​vulnerability.go Adds batched exact-key queries with a limited column selection.
src/​pkg/​scan/​dao/​scan/​vulnerability_test.go Tests lookup filtering, field selection, empty input, and batching.
src/​pkg/​scan/​dao/​scan/​model.go Defines the vulnerability-record lookup key type.

Review effort: Balanced

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.26%. Comparing base (3cc8b5c) to head (83d84aa).

Files with missing lines Patch % Lines
src/pkg/scan/dao/scan/vulnerability.go 90.90% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #24509      +/-   ##
==========================================
- Coverage   74.27%   74.26%   -0.01%     
==========================================
  Files        1096     1096              
  Lines      103611   103630      +19     
  Branches     3079     3079              
==========================================
+ Hits        76953    76964      +11     
- Misses      26290    26297       +7     
- Partials      368      369       +1     
Flag Coverage Δ
unittests 74.26% <92.85%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/pkg/scan/dao/scan/model.go 38.46% <ø> (+7.69%) ⬆️
src/pkg/scan/postprocessors/report_converters.go 74.61% <100.00%> (+0.11%) ⬆️
src/pkg/scan/dao/scan/vulnerability.go 78.15% <90.90%> (+2.89%) ⬆️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

After a scan, the report converter listed the existing vulnerability
records with cve_id IN (all CVEs of the report). That returns the records
of every package and version that has one of those CVEs, with all
columns, and only the exact cve_id/package/version matches were used.
On a large registry this loads hundreds of thousands of rows per scan
job into jobservice and makes PostgreSQL scan the whole
vulnerability_record table.

Look the records up by cve_id, package and package_version instead,
which uses the unique index on these columns, and only load the columns
needed to update severity and status. The report JSON is now parsed once
in ToRelationalSchema and passed to toSchema.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Mathijs Möhlmann <mathijs.mohlmann@ns.nl>
@secmff
secmff force-pushed the fix-job-service-memory branch from 83d84aa to ef2976a Compare December 1, 2026 16:26

This branch has not been deployed

No deployments
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.

5 participants