oliver-zehentleitner / maity
performance, hub: members, project and sessions routes — statement counts, N+1 removal, indexes
tsk-6srccd2r1h62 Done An agent
Item of the roadmap on tsk-a5pxwgkgr73e. Oliver (96592, 2026-11-09): focus on performance and implement the next measures.
Measured on prod, 17fce84f, hub access log 07:36–07:39Z (q8q9 96574):
projects/{o}/{s}/members: median 150 ms, p90 195 ms. It runs on every page, and twice on settings.projects/{o}/{s}: median 80 ms.auth/sessions: median 42 ms.projects/{o}/{s}/tasks: median 135 ms.
Step 1 (a note, no code): locally on PostgreSQL, for each of these four routes give:
- the statement count per request and how it grows with N (members, cards);
- the slowest statements, with EXPLAIN.
Name every N+1 and every missing index.
Step 2: one PR per route family. It removes the N+1, adds a statement-count test that fails on the old code, and adds the index migration where needed. A migration slot comes from vuur. The answer shape stays the same: no contract change. If one turns out to be needed, it goes to me first.
Boundary:
- hub only;
- no change to who may read what;
- run locally against PostgreSQL via
hub/scripts/with_postgres.sh, at most two heavy runs, never the full suite on the shared machine; - no prod writes.
Server-Timing (tsk-239r9cwxje6w, eyq5) will confirm the gain on prod.
Notes
An agent · Nov 9, 2026, 07:53 UTC
Step 1, part 1 of 2: measurement (zwhq, local PostgreSQL via
with_postgres.sh, main 17fce84f; owner's cookie session, warm, best of 5)| route | statements | at 1 agent/2 cards | at 9 agents/40 cards | wall local | DB local |
|---|---|---|---|---|---|
|GET …/members| 44 | 44 | 44 | 22→29 ms | 6→8 ms |
|GET /projects/{o}/{s}| 29 | 29 | 29 | 15 ms | 4 ms |
|GET …/tasks| 24 | 24 | 24 | 13→18 ms | 3–4 ms |
|GET /auth/sessions| 17 | 17 | 17 | 9 ms | 2 ms |No N+1 in any of the four: the count is flat in members and cards. Every list is already batched (
member_views,task_views). The cost is a fixed set of statements per request, two transactions each.Model, not reproduced on prod: prod is 150 ms vs 29 ms here for members. Spread over 44 statements, that gap is about 2.7 ms per statement, which fits one network round trip per statement plus slower CPU. Server-Timing (tsk-239r9cwxje6w) will show the db/total split. Until then, the lever is fewer statements per request, not indexes.
Indexes: each statement of the four routes was run through EXPLAIN with
enable_seqscan=off. Every one has an index path except two:instance_settingread in full (a small table, read 2–3× per request, see part 2);- the
human_sessionsweep inGET /auth/sessions:auth/sessions.forget_expired_addressesruns an UPDATE over every human's sessions on each list read, with nohuman_idfilter. It is a Seq Scan that grows with all sessions on the instance and takes the account lock on a GET. This is the one cost that grows with data.
Repeated statements, by source (call sites from a stack per statement):
- Auth path, on every cookie call, statements 0–10 (11 of them):
- session row read twice (caller.py:201 and second_factor.py:235
_session); instance_settingread twice (second_factorpolicy→ sign_in_settingsresolve, twice);humanread twice (deleted_atcaller.py:204,account_statusdeps.py:141);- three UPDATEs per request:
human_session.last_seen_at/ip,human.last_seen_at,membership.last_seen_atfor all memberships.
An agent · Nov 9, 2026, 07:53 UTC
Step 1, part 2 of 2: where the repeats are, and the proposed PRs
GET /projects/{o}/{s}(29):namespace.resolve+access.membership(handle → project → membership, 3 statements) run 3×, fromget_project,_stored(projects.py:676) and_serve_restricted(projects.py:713).owner_ofruns 3× too, andinstance_settinga third time (limits.of). Doing each once gives about 20.GET …/members(44), duplicates insidemember_views:- project row read again 2× (instances.py:321, views.py:621);
- the owner's human row read twice (views.py:588, 882);
- id → handle read 3× (views.py:174, briefings.py:53 ×2);
long_pollread 2× (listening.which, from views and from instances);credentialread 2× (instances.py:325, views.py:893);agent_sessionread 2× (views.py:469, instances.py:449);avatar_imageread 2×.
Shared reads give about 33, still flat in N.
GET …/tasks(24): batched and lean; only the auth path applies.GET /auth/sessions(17): see the sweep in part 1.
Proposed PRs (step 2), one per family. Each comes with a statement-count test that fails on the old code. The answer shapes stay unchanged.
- Auth path, every request (biggest win: about −5 statements on every API call):
- pass the session row and the human row through, instead of reading them again;
- resolve the sign-in policy once per request.
Open question for you: throttle the three
last_seenUPDATEs (for example, write only when the stored value is older than 60 s) and spare most GETs a write and a commit. That changes how freshlast_seen_atand the session'sipare (§2.6.5), so it is a contract point and goes to you first.- Project read: resolve the project, membership and owner once per request (about −9).
- Members: share the duplicate reads in
member_views/instances.views(about −11). - Sessions: limit the sweep to the caller's sessions (index
human_id) and leave the instance-wide sweep to sign-in or a worker. Open question: this changes when another person's expired session loses its address, so it goes to you first. The alternative is a partial index with no behaviour change.
No migration is needed for 1–3. Item 4 needs one only for the partial-index variant.
Not in this card: the dashboard calls members twice on settings (web).