Skip to content

Copy advisory children in two queries instead of one per advisory - #4680

Open
pablomh wants to merge 1 commit into
pulp:mainfrom
pablomh:batched-child-detection
Open

pablomh wants to merge 1 commit into
pulp:mainfrom
pablomh:batched-child-detection

Conversation

@pablomh

@pablomh pablomh commented Dec 1, 2026

Copy link
Copy Markdown

Problem

Copying a selection that contains advisories finds the packages and modules they
reference by looping over the advisories. Each iteration runs get_pkglist() and
get_module_list(), then a NEVRA/NSVCA match query, so child detection costs
O(advisories) round trips. This is the path Katello incremental updates take by
default, since resolve_dependencies defaults to false.

Change

Resolve the selection with one query per content type:

  • packages: an Exists between the version's packages and the
    UpdateCollectionPackage rows reached through UpdateCollection.update_record;
  • modules: the same through the collection's module JSON, extracting the NSVCA
    fields with KeyTextTransform.

The two queries evaluate inside a scoped _hash_join_only() context that disables
nested-loop plans and restores the previous setting in finally. PostgreSQL
estimates the advisory subquery at a handful of rows, which allowed a quadratic
nested-loop plan for a broader queryset shape than the caller builds. With nested
loops off the planner must use a hash or merge semi-join, both linear in their
inputs and spilled to disk beyond work_mem.

Measurements (Alma 9 AppStream repository version)

Case Result
2,059-advisory copy, 7,656-package version 90% less child-detection time
destination content identical (2,600 packages / 2,059 errata)
468k collection rows (21× data) hash semi-join
small work_mem 1,024 batches, disk spill, correct result

Equivalence: old and new implementations return identical child sets for every
advisory across three repository versions (4,431 single-advisory cases and 84
multi-advisory selections, packages and modules). All 144 module collections
carry the five expected NSVCA keys; the 22,288 collection-package rows contain no
duplicate tuples.

Tests

  • pytest --pyargs pulp_rpm.tests.unit: 144 passed, 1 skipped
  • copy performance tests: 22 passed, including the advisory family after removing
    main's get_pkglist|get_module_list N+1 exemption
  • functional test_advisory_copy_child_detection: passed
  • ruff check / ruff format --check: clean

No API, schema or migration changes.

Behavior note

A malformed UpdateCollection.module missing one of the five NSVCA keys used to
raise KeyError; it now matches nothing. No such row exists in the audited data,
and validation belongs at advisory ingestion.

🤖 Generated with Claude Code

Copying a selection that includes advisories looped over them to find
the packages and modules they reference: get_pkglist() and
get_module_list() issued their own queries per advisory, followed by a
NEVRA or NSVCA match query, so child detection cost grew with the
number of advisories.

Resolve the whole selection with one set-based query per content type:
match the version's packages against the selected advisories' collection
packages through the existing relations, and the modules against their
collection NSVCAs. The queries are evaluated with nested loops disabled
so PostgreSQL must use a hash or merge semi-join; the cardinality
estimate for the version-content subquery can otherwise make a quadratic
nested-loop plan look cheap. Both plans are linear in the inputs and
spill to disk beyond work_mem.

On a 2,059-advisory selection over a 7,656-package version, child
detection takes 90% less time with identical destination content. At
468k collection-package rows the join remains a hash semi-join; with a
small work_mem it spills across 1,024 batches and still resolves
correctly. The old and new implementations return the same child sets
for every advisory across three repository versions (4,431
single-advisory cases and 84 multi-advisory selections).

Co-Authored-By: Claude Code <noreply@anthropic.com>
@pablomh
pablomh force-pushed the batched-child-detection branch from 9ffdfb4 to e320566 Compare December 1, 2026 19:30

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant