Build the RLS workspace member predicates once per request

Merged
CH
4h ago
+400-221

Why

Prod-eu p99 request latency roughly doubled after channel visibility gave each workspace member an individual role (Nov 25-26). Reproduced locally on seed data with a per-request probe (CPU, pg time, query count, event-loop delay) and CDP profiles: the extra time is Node CPU in the permission read path, not the database.

#32029 removed the per-request recomputation of the principal's own permissions. The heaviest scans left are findWorkspaceMemberBoundPredicates and findWorkspaceMemberRelationPredicates: they walk every role × every object of the workspace (42,546 object entries per call in a 1,012-role workspace). Their cost grows with the number of members. They ran twice in PermissionsMetadataMapsService.load, and up to 4 more times per message read through AccessGroupReadService.findPrincipalWorkspaceMember.

What

flowchart LR
  A[roles<br/>permissions] --> B[build once<br/>per request]
  B --> M[metadata<br/>maps]
  M --> C[bound<br/>predicates]
  M --> D[relation<br/>predicates]
  style B fill:#d4f7d4
  • New pure util buildRlsWorkspaceMemberPredicates(rolesPermissions) walks the roles once. It returns { bound, relation }: the predicates bound to a workspace member, and the workspace member relation predicates deduplicated by member field. withRlsWorkspaceMemberPredicates(maps) attaches them to a metadata maps object.
  • PermissionsMetadataMaps now carries rlsWorkspaceMemberPredicates. PermissionsMetadataMapsService.load builds it, once per principals through its existing per-principals cache.
  • findWorkspaceMemberBoundPredicates and findWorkspaceMemberRelationPredicates are removed. load and AccessGroupReadService read the built lists from the metadata maps. The access-group sync listener and the RLS relation value cache build them once where they already load rolesPermissions.
  • isWorkspaceMemberUpdateAffectingAccessGroups and findWorkspaceMemberRelationFieldNamesOfObject take rlsWorkspaceMemberPredicates instead of rolesPermissions.
  • Related record path narrowing (omitPermissionsUnableToReadField) is unchanged and carries rlsWorkspaceMemberPredicates over as is. Nothing reads the predicates from narrowed maps; only load and AccessGroupReadService read them, both from the request's unnarrowed maps.

No cache, no WeakMap, no behaviour change, migration, flag or API change. Unit spec fixtures that build a full PermissionsMetadataMaps go through withRlsWorkspaceMemberPredicates. One integration helper now gets the lists from PermissionsMetadataMapsService.load.

Work per request

Counted on a 1,012-role workspace by instrumenting the scans:

Full role × object scans per requestmainThis PR
Message or calendar read61
Other reads21

Timings from my local runs are not reliable enough to quote: the machine was at a load of ~13 on 10 cores during the last benchmark. On v2.66.3 at 1,012 roles, local CDP profiles put these two scans at about a fifth of busy CPU. At prod sizes (≤123 roles) one scan is about 5k entries.

Not in this PR

  • isObjectRestrictedByRlsRule: one pass over the roles per related record path. It is cheap per call and correct on narrowed maps as is.
  • The per-path clone in omitPermissionsUnableToReadField, which matters at 1,000+ roles
  • The access group compact id lookup that runs on the writer for each read of an access-group object
  • The extra source-record queries #31708 added to timeline activity reads