Repository navigation
perf(auth): resolving the viewer takes one database round trip, not three - #772
Merged
Merged
Conversation
…hree Users.refreshClaims answers the user and their company in one statement: a data-modifying CTE writes the claims only when they changed, and the SELECT joins the company. resolveViewer no longer reads Companies. Part of #748. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The single-statement refresh answered every column but email and name from its snapshot. A resolve with changed claims that waited on an admin's uncommitted deactivation, demotion or company move then answered the authority from before that change; the old UPDATE ... RETURNING and its fresh fallback read answered the row as the change left it. refreshClaims now reads the user and company in one lock-free statement and returns when the claims already match. Otherwise it runs the old UPDATE ... RETURNING unchanged, falls back to a fresh read when a concurrent refresh already wrote the claims, and reads the company again only when it moved. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… claims never wait A changed-claims resolve reused the company from its first read when the company id didn't move, so a company renamed while the write waited came back with its old name. It now reads the company after every write, as main did. Tests pin the rename during a held demotion, and that a refresh with unchanged claims answers while an admin's change still holds the row. Comments say one read for unchanged claims rather than one statement for every resolve. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…h waiter wins When a held deactivation commits, Postgres wakes both queued refreshes and either may take the row first. The queued case held the first refresh open and then waited for the second to queue behind it; when the second won instead, nothing waited again and the test timed out (CI, Node 22). Both refreshes now run on their own and the case asserts what holds either way: one writes the claims, the other writes nothing, and both answer the deactivation. Every wait is bounded at three seconds and fails naming the step, or as soon as a transaction it waits on ends first, instead of hanging until vitest's timeout; held transactions roll back with the case. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… inferred type Co-Authored-By: Claude Opus 5.5 <[email protected]>
allisonmahmood
added a commit
that referenced
this pull request
Oct 19, 2026
…ng Reconnecting (#783) Release 14's What's new entry, written from \`7694bab\`. It covers everything merged since release 13's \`through\` (\`c9b8fc4\`), and sets the \`patchy\` release to \`0.14.0\` (\`packages/patchy/package.json\` and \`CURRENT_RELEASE\`). | Change | PRs | | --- | --- | | Improved: Tools with server code answer faster | #756, #764, #768, #769, #771, #772, #773, #775, #778 | | Improved: Your company's tools and pages open sooner | #759, #761, #766, #770 | | Fixed: Tools stop flashing Reconnecting while they load | #755, #776, #780 | | Improved: Publishing is quicker | #729 | | Improved: New tools start with a tidy layout | #763 | | Improved: Your usual is easier to scan | #721 | | Fixed: A page that isn't there says what to do | #762 | | Fixed: A logo Patchy can't read is refused, not a crash | #728 | | Behind the scenes: richer logs for live data | #758 | #720 is release 13's own entry, so it's left out. Made by Claude Opus 5.5 via Claude Code. Co-authored-by: Claude Opus 5.5 <[email protected]>
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.
Every viewer resolve cost 3 platform round trips:
UPDATE users … RETURNING, which usually changed nothing;SELECT usersit then fell back to;Companies.findById.A tier 2 call resolves the viewer at admission, on every directory
reauthorize, in every document check, on stream open and on every subscription POST. That's 2 resolves per query, 1 per mutation and 3 per re-run.Now a resolve whose claims haven't changed (almost every resolve) is one statement. Every resolve stays where it was and still reads current state; nothing is cached.
The fix
Users.refreshClaimsreturns the user and their company, and reads before it writes:displayNamefallback are unchanged.UPDATE users … WHERE … IS DISTINCT FROM … RETURNING, verbatim. It waits on any lock holder and returns the row as that holder left it. The company is then read after it, as main did, even when its id didn't change: a company renamed during the wait comes back renamed.null, andresolveViewerdies withUser company is missing.resolveViewerno longer needsCompanies. The/joinredirect, the deactivation 403 page and theViewerit returns are unchanged.Doorstops capturingCompanies, and the runtime'sEffect.Services<typeof resolveViewer>follows on its own.Why it's equivalent
UPDATE's condition was false on its snapshot, so Postgres skipped the row without locking it. Its fallbackSELECTran one round trip later than the new read does. That difference is a request arriving a moment earlier, not a lost guarantee.SELECTread the snapshot from before its ownUPDATE's lock wait. A resolve with changed claims that waited on an admin's deactivation, demotion or move then returned the authority from before that change. Doored POST handlers act on that viewer without resolving again. The fresh fallback has no single-statement equivalent either: when queued behind an identical refresh, aUNION ALLfallback still reads the old snapshot and misses a company created by the lock holder.UPDATEtakes one, and only when the claims changed. The first read takes none, so unchanged claims never wait behind an admin's transaction; a test pins that.Tests
On a real Postgres, against the migrated test template:
RequireSession.test.ts(new). It counts statements withStatement.CurrentTransformer.session_replication_role = replica, inside a rolled-back transaction.Users.test.ts:The two lock-wait tests fail on round 1's CTE, and on the
RETURNING/UNION ALLvariant: there the queued refresh misses the deactivation, and the moved-to company comes back null. The rename case fails on round 2, which reused the first read's company.Numbers
Platform statements per call on
pnpm dev up brightline, as the admin on the New Business board. The baseline is main at #771, after its echo fix. Counts come from Postgres's statement log (log_min_duration_statement = 0), sliced between marker statements. Queries and mutations ran 5 times; streams ran 3 opens × 3 writes. Every repeat matched.deals.board(one-shot)deals.addNoteThat's −2 per resolve: query −4, mutation −2, re-run −6.
When the claims changed (the stored name edited, so the next resolve writes), that resolve costs 3 statements here and 2 on main, and a query in that state costs 12 here and 13 on main. The extra statement buys a fresh company after the write, and the path only runs when someone's name or email changes in Clerk. If a concurrent refresh already wrote the claims, the changed path costs 3 on both: the read, the empty write and the fresh read here; the write, the fallback read and the company on main.
Company statements are unchanged. These totals come from
pnpm dev's default executor, which runs tier 2 without the fleet, so they leave out the fleet's admission and release statements that #748's counts include. The resolve statements are the same on both executors.On the same instance:
principal_changed, and the new one'smeanswers;/join;access_deniedon a runtime call; reactivating restores access.Part of #748.
Made by Claude Opus 5.5 via Claude Code; reviewed with GPT-6.1-Sol.