Skip to content

perf(execution): a warm call is admitted and released in 7 statements, not 16 - #778

Merged
allisonmahmood merged 4 commits into
mainfrom
perf/748-warm-binding
Oct 18, 2026
Merged

allisonmahmood merged 4 commits into
mainfrom
perf/748-warm-binding

Conversation

@allisonmahmood

@allisonmahmood allisonmahmood commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

A warm tier 2 call spent 16 platform statements, in two explicit transactions, getting admitted to its company's binding and releasing it (#748's counts):

  • a breaker upsert and a read of the same row;
  • a pool-wait limit read;
  • a claim transaction and an admissibility check for a binding that was already active;
  • a six-statement release.

Now it's 7 statements and no transactions.

What's in, and why authority holds

Warm read: 6 statements become 1. That's the claim transaction, isAdmissibleTask and the execution.pool.wait read. ensure first reads the company's active binding whose task is bound to a deployment that isn't retired. Only on a miss does it read the pool wait and run today's claim loop.

  • protect is unchanged and still decides admission: the epoch, active, a bound task and a deployment that isn't retired. The warm read, protect and adopt now share one SQL fragment for the task check.

  • The claim lock serializes creating a binding, and it was released before protect. It gave an existing active binding nothing that protect relied on.

  • Every case the claim loop handled differently still goes there:

    • no binding;
    • a claiming binding, after adoption, replacement or a lost bind acknowledgement;
    • a task that can't take admissions. The claim loop fences it with cause deployment, as before.
  • If something changes between the read and protect, protect refuses and acquire's existing loop reads again:

    • adoption advances the epoch;
    • a fence, drain or replacement moves the binding to stopping;
    • a retirement fails the task check.

    The next read misses unless the binding is active and admissible, so it falls through to the claim loop.

  • binding_epoch is unique and a binding's company never changes, so protect's epoch check also pins the company.

  • connect, ensureBinding and the limit-override rebind get the answer they got before: an active binding whose task could take admissions when it was read.

Release: 6 statements become 2. The touch and the four-statement transaction become one statement: UPDATE … WHERE (the company's admission binding) OR binding_id = <the call's binding>. A CASE clears that binding's protection only when pg_try_advisory_xact_lock succeeds.

  • The statement's implicit transaction holds the exclusive try-lock until commit, as the old transaction did:
    • a new holder's shared lock waits, then protect sets protection again;
    • a holder already in place makes the try fail, and protection stays.
  • A draining binding's protection is still cleared, so its drain finishes. The company's current binding is still touched.
  • The plan is a BitmapOr over the admission partial index and the primary key, checked with 50k rows of binding history.
  • The unlock stays on the activity session, because a try-lock there would succeed against that session's own shared locks.

Dropped after review

The one-statement breaker. I had used WITH synced AS (upsert … RETURNING) SELECT COALESCE(synced, the row). When the upsert waits on a row lock and then writes nothing, the fallback read sees the row as it stood when the statement started, not after that wait. Two races follow:

  • an admission that waited on a breaker trip was admitted;
  • an admission that waited on a publish clearing the pause was refused.

The read is its own statement again, with a comment saying why. Two new lock-wait tests cover both races, and both fail against the one-statement version.

Releasing after the reply. Forking the release from the owner's finalizer let it escape a closing host scope. The activity session and pool could then be disposed under it, which leaves protection in place through the crash window. Release timing is exactly as on main: quiet queries release after the reply, everything else before.

Main's existing quiet-query fork has a narrow version of the same gap, which I didn't touch here. If a quiet query's owner finishes its run exactly while the host scope is closing, the settlement fiber is forked into the closing scope and never starts. Its release is then skipped, with the same conservative result. An Effect probe confirms the ordering. It's a separate issue.

Not done: 3b, Fleet.route reusing the binding protect returned, is a follow-up. route still reads execution_bindings before both bind and invoke, so it would save 2 more statements per call.

Numbers

Local brightline with tier 2 on the local task provider, which runs the same fleet controller as ECS. Each call is Allison on New Business.

Per warm call main this branch
Query deals.board 29 platform + 5 company 20 + 5
Mutation deals.addNote 35 + 20 26 + 20
Re-run of deals.board 36 + 6 27 + 6
A write with the board open (mutation + its re-run) 71 + 26 53 + 26
Admission and release 16, in 2 transactions 7, in none

Platform statements before the reply, by statement order: the query went from 21 to 16 and the mutation from 35 to 26.

Every main window was identical: 5/5 query, 5/5 mutation, 6/6 re-run. On this branch:

  • query 5/5, mutation 5/5 and write with the board open 3/3;
  • re-run 7 of 8, with the eighth also holding a keepalive touch (+2).

The keepalive touch itself is unchanged.

The board renders and notes save. The board re-runs after every write and rename, and pnpm dev shot reports no errors.

Tests

Fleet.test.ts, over real Postgres:

  • Statement count. A warm acquire and release sends exactly the pause upsert and read, the warm read, protect and the release, none of them in a transaction. On main it's 10 statements in 2 transactions.
  • Two holders and a drain. The first release keeps protection. After a drain while the call is held, the last release clears protection on the stopping binding and touches the company's fresh one, and housekeeping then stops the old task.
  • Adoption between the warm read and the hold. The call is admitted on the new epoch, never the old.
  • A retired deployment. The warm read skips the binding, the claim loop fences it with cause deployment, and the call lands on a fresh task.
  • A breaker trip during an admission's wait. The admission is refused patch_paused, and the call already admitted keeps running.
  • A publish clearing the pause during an admission's wait. The admission is admitted.

The adoption and both breaker cases hold a real transaction open and wait until the call is blocked on its lock (pg_stat_activity) before committing. Every case except the statement count also passes on main. The breaker test now uses a shared helper to publish versions.

pnpm verify passes: format, lint, typecheck, synth, test, postgres-concurrency, cli-smoke, tier2-smoke and browser.

Part of #748

Made by Claude Opus 5.5 via Claude Code; reviewed with GPT-6.1-Sol.

…ents

A warm tier 2 call spent 16 platform statements on the fleet: a breaker
upsert and read, a pool-wait limit read, a claim transaction, an admissibility
check, the hold and protect, and a six-statement release, all before a
mutation's reply. It now spends 6.

- The breaker upsert returns the pause in the same statement.
- ensure reads the company's active, admissible binding without the claim
  lock; protect still decides admission, and any miss takes the claim loop.
- The pool-wait limit is read only when ensure has to claim or wait.
- The release touches the binding and clears protection in one statement
  that holds the exclusive try-lock until it commits.
- An admission's release is forked after the reply for every call kind.

Part of #748

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added area:server Touches the server area size:m 100-500 changed lines labels Oct 18, 2026
allisonmahmood and others added 2 commits October 18, 2026 23:42
…e before the reply

Review found two regressions in the first commit.

- The one-statement breaker read the row as it stood before the upsert
  waited on it, so an admission could miss a trip committed during that
  wait, or be refused after a publish cleared the pause. The read is its own
  statement again, and two lock-wait tests race a trip and a publish reset
  against an admission.
- A release forked from an owner's finalizer during shutdown escaped the
  closing scope, which disposed the activity session under it. Release
  timing is back to main's.

Part of #748

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@allisonmahmood allisonmahmood changed the title perf(execution): a warm call is admitted and released in 6 statements, not 16 perf(execution): a warm call is admitted and released in 7 statements, not 16 Oct 18, 2026
@allisonmahmood
allisonmahmood merged commit 40730dd into main Oct 18, 2026
13 checks passed
@allisonmahmood
allisonmahmood deleted the perf/748-warm-binding branch October 18, 2026 22:10
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:server Touches the server area size:m 100-500 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant