From a85e39aac18fb436eeb71ca57bf62cd1c99b1729 Mon Sep 17 00:00:00 2001 From: Moustafa Moustafa Date: Thu, 24 Sep 2026 17:54:12 +0000 Subject: [PATCH] Use a direct repository filter for the latest-publication lookup Distribution.get_repository_publication_and_version() resolves the newest publication for a repository-backed distribution with repository_version__in=repository.versions.all(). Django renders that as a subquery joined on pulp_id, which hides repository_id from the planner. It cannot use the (repository_id, number) unique index and instead scans the global index on number, filtering each row by repository. Since number is a per-repository counter and that index is global, the work is set by the total number of repository versions across all repositories rather than by the size of the repository being served. On an installation with a large number of repository versions this scans a substantial portion of core_repositoryversion to return a single row, and every repository degrades as any of them grow. Filtering with repository_version__repository selects the same rows but keeps repository_id visible, so the planner uses the (repository_id, number) index and the lookup becomes a short index scan. The same query also pulled repository_version.content_ids in via select_related. Nothing on this path reads it, and it holds every content unit UUID in the version, so defer it. closes #1995 Assisted-by: Claude Opus 5 (GitHub Copilot) --- CHANGES/1995.bugfix | 2 ++ pulpcore/app/models/publication.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 CHANGES/1995.bugfix diff --git a/CHANGES/1995.bugfix b/CHANGES/1995.bugfix new file mode 100644 index 0000000000..25e4115807 --- /dev/null +++ b/CHANGES/1995.bugfix @@ -0,0 +1,2 @@ +Improved the performance of the latest publication lookup when serving content +from a repository-backed distribution. diff --git a/pulpcore/app/models/publication.py b/pulpcore/app/models/publication.py index dd28ed8de5..6e8f677026 100644 --- a/pulpcore/app/models/publication.py +++ b/pulpcore/app/models/publication.py @@ -792,9 +792,10 @@ def get_repository_publication_and_version(self): try: publication = ( Publication.objects.filter( - repository_version__in=repository.versions.all(), complete=True + repository_version__repository=repository, complete=True ) .select_related("repository_version") + .defer("repository_version__content_ids") .latest("repository_version", "pulp_created") .cast() )