[2.x] fix: Batch policy state loading to avoid one pivot query per serialized user - #85
Merged
Conversation
Two paths lazy-loaded each user's accepted policies individually: - The per-user policy state fields on the user resource. These now defer their values and load the accepted policies of every buffered user in one query when the first value is resolved. - The permission group processor, which runs whenever a permission check is made on a user (e.g. another extension's per-user serializer flag) and checked that user's acceptance state. The relevant endpoints now eager load the relation alongside the users they already load. On a discussion list of 20 discussions viewed by an admin, this reduces the extension's queries from one per distinct user on the page (~30) to a constant 3.
This was referenced Jul 26, 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.
Problem
On endpoints that serialize many users (most visibly a discussion list viewed by a privileged actor), fof/terms issued one
fof_terms_policy_userpivot query per distinct user on the page (~30 queries for a 20-discussion list on a busy forum). Two separate paths caused this:fofTermsPoliciesState,fofTermsPoliciesHasUpdate,fofTermsPoliciesMustAccept) each lazy-loaded the user's accepted policies.hasPermission()/can()call on a serialized user (e.g. another extension's per-user serializer flag) computes that user's permission groups, and the processor then checks that user's acceptance state to apply the guest downgrade — one lazy pivot query per user.Fix
PolicyStateBuffer; the first resolved value loads the accepted policies of all buffered users in a single query — mirroring core'sEloquentBufferapproach.fofTermsPoliciesalongside the users they already load, so the permission group processor finds the relation in place. (posts.user.*is deliberately not eager loaded on discussion Show: endpoint eager loads apply to primary models vialoadMissing(), which would load a discussion's entire posts relation. Post stream users are covered by the posts Index endpoint.)Measured
Real forum, admin actor,
/api/discussions?page[limit]=20: extension queries 32 → 3 (constant, regardless of page size), total request queries 149 → 120.Tests
New
PolicyStateQueryCountTestpins the constants: 2 pivot queries for a privileged actor over 5 distinct users, +1 for the actor's own enforcement check, and a test registering an ad-hoc per-userhasPermission()field to simulate the second path.Related: the core half of this investigation was flarum/framework#4839.
Fixes #49.