Files
silo-server/internal/userstore/profile_libraries.go
f637404c46 fix(userstore): batch allowed-library and profile lookups when listing (#380)
* fix(userstore): batch allowed-library and profile lookups when listing

Listing profiles cost 1 + P queries: one allowed-libraries lookup fired
per profile row while the cursor was still open, which on Postgres also
checks out a second pooled connection mid-scan. The admin sessions
dashboard makes it worse, calling ListProfiles once per streaming user
just to resolve names. The SQLite store had the same pattern for both
profiles and collections, even though the Postgres collections path was
already written with array_agg to avoid exactly this.

Collect the rows first, then fetch the child lists in one batched query
and stitch them together in Go. No behaviour change, just fewer round
trips.

* fix(userstore): avoid sqlite batch variable limits

---------

Co-authored-by: Quick104 <31828688+Quick104@users.noreply.github.com>
2026-07-20 11:25:24 -04:00

23 lines
709 B
Go

package userstore
// ProfileAllowedLibrary associates one profile with one allowed library.
type ProfileAllowedLibrary struct {
ProfileID string
LibraryID int
}
// AttachAllowedLibraries replaces each profile's AllowedLibraryIDs with the
// matching associations, preserving their input order.
func AttachAllowedLibraries(profiles []Profile, allowedLibraries []ProfileAllowedLibrary) {
byProfile := make(map[string][]int, len(profiles))
for _, allowedLibrary := range allowedLibraries {
byProfile[allowedLibrary.ProfileID] = append(
byProfile[allowedLibrary.ProfileID],
allowedLibrary.LibraryID,
)
}
for i := range profiles {
profiles[i].AllowedLibraryIDs = byProfile[profiles[i].ID]
}
}