Upstream 16527 - Eliminate LEFT OUTER JOINs in unified job RBAC query#586
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Ports an upstream RBAC query optimization to the legacy RBAC backend by rewriting several access-control filtered_queryset() implementations to use pk__in subqueries, reducing LEFT OUTER JOIN pressure and improving query planner/index usage for large unified job tables.
Changes:
- Refactors
JobEventAccess.filtered_queryset()to use*_id__insubqueries instead of relationship traversal. - Refactors
UnifiedJobTemplateAccess/UnifiedJobAccessRBAC branches to usepk__insubqueries (notably forInventoryUpdateandAdHocCommandbranches) to avoidLEFT OUTER JOINs in the unified job query. - Refactors
LabelAccess.filtered_queryset()to use the M2M through-table subquery instead of a join +distinct().
(This won't do much for performance though)
TheWitness
approved these changes
Jul 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Upstream uses the new RBAC backend, but we can port the Upstream PR to fix the same issue on the older RBAC.
Upstream Summary
UnifiedJobAccess.filtered_queryset()withpk__insubqueries, eliminating 3 LEFT OUTER JOINs that Django generates for theinventoryupdateandadhoccommandRBAC branchesProblem
Django translates
Q(inventoryupdate__inventory_source__inventory__id__in=...)into LEFT OUTER JOINs, which are evaluated unconditionally for every row inmain_unifiedjobbefore filtering. At scale, 99.84% of rows are scanned and discarded. The 4-way OR prevents PostgreSQL from using any column-specific index.Under production concurrency (21 controller replicas),
pg_stat_statementsshows this query averages 3,959ms per call across 29K+ calls, consuming 32.5 hours of DB time — the single largest database workload.Fix
Note:
Exists()withOuterRefcannot be used here — django-polymorphic raisesAttributeErroronExistsnodes. Thepk__inpattern is the polymorphic-compatible equivalent.Test plan
awx/main/tests/functional/rbac/)str(qs.query)in dev containeraap26-next