Postgres pool, review round 1: nested transactions, account mail after commit

P-1: a transaction begun inside another one (a hook calling the Local API
without req) no longer queues behind its caller. An AsyncLocalStorage mark set
when a transaction begins lets the inner one through at once, without a
place, but never into the last connection (a second level may take that
one). If the mark is lost, a queue that has not moved for a second admits
its newest waiter. connectionReserve: false now admits everything, as
before. Measured on Postgres 17: 9, 12 and 30 such submissions finish
(before: stuck from 9).

P-2: Payload's "forgot password" and "verify your e-mail" through
mailAdapter() go out right after their transaction commits
(mail.accountMailAfterCommit, default on). With the relay hanging,
30 resets + 30 submissions finish in about 11 s instead of 43.5 s.

P-3: Postgres tests for the "not confirmed" file result after commit, a
caller's outer transaction rolled back or committed, the relay hanging,
and the nested rows; deployment.md has the measured table.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 01:19:27 +01:00
co-authored by Claude Opus 5.5
parent 32bb542e8e
commit 4efadb21d5
18 changed files with 534 additions and 107 deletions
+26 -9
View File
@@ -366,29 +366,46 @@ Since 1.17.0 the plugin keeps a few connections free of transactions (`connectio
- at most `pool size − reserve` transactions are open at once; the next one waits for a place — its request is
slower, nothing stops. Default reserve: a fifth of the pool, at least 2 (1 in a pool of fewer than 4): with the
default pool of 10, eight transactions at once and two connections for everything else;
- a transaction that has waited a second (`RESERVE_ESCAPE_MS`) may use the reserve too, all but one connection — so a
transaction begun inside another one (a hook without `req`) does not wait for ever on its caller, and one connection
is never held by a transaction;
- a transaction begun **inside** another one — a hook that calls the Local API without `req` writes in a
transaction of its own while its caller's is open — does not queue: it takes no place, because its caller cannot
finish without it. It may use the reserve but the last connection (a second level inside it that one too), so one
connection stays free for the queries outside transactions. "Inside" is told by a mark the plugin sets when a
transaction begins (`AsyncLocalStorage`); in case the mark is lost on the way, a queue that has not moved for a
second (`RESERVE_ESCAPE_MS`) lets its newest waiter use the reserve, all but the last connection;
- form e-mails go out **after** the submission's transaction commits ([forms.md](./forms.md) "When e-mails go out"),
so a slow mail server holds no connection; Payload's own account mail ("forgot password", "verify your e-mail") is
still sent inside its transaction — with a mail server that hangs, those requests hold a place until the SMTP
time-out ([email.md](./email.md) "SMTP": 10 s to connect, 30 s for the greeting by default).
and so do Payload's own "forgot password" and "verify your e-mail" sent through `mailAdapter()`
(`mail.accountMailAfterCommit`, default on): a slow or hanging mail server holds no connection and no place; the
request still ends when the mail is out. A rolled-back operation sends nothing.
Measured on Postgres 17, default pool of 10 (`test/transactionPool*.test.ts`):
| At the same moment | 1.16.8 / `connectionReserve: false` | 1.17.0 |
|---|---|---|
| 12 or 30 form submissions, or 12 or 30 "forgot password" | site stops for good, pages too | all finish, a page answers in milliseconds |
| 9 / 12 / 30 submissions whose hook writes without `req` | 9 finish (363 ms); 12 and 30 stop the site | all finish (under 2 s) |
| 30 "forgot password" + 30 submissions, the mail service hanging (10 s budget a send) | site stops for good | all finish in about 11 s (one budget); with the reset mail inside its transaction (`accountMailAfterCommit: false`) the last waited 43.5 s |
| the same with an SMTP server that never answers (2 s time-outs in the test) | site stops for good | all finish in a few seconds |
```ts
iapl({ connectionReserve: 3 }) // keep three connections free
iapl({ connectionReserve: false }) // no limit, as before 1.17.0 (not recommended)
iapl({ mail: { accountMailAfterCommit: false } }) // "forgot password" mail inside its transaction, as before
```
MongoDB, SQLite and other adapters without a pool: no effect. A larger pool (`postgresAdapter({ pool: { max: 20 } })`)
moves the reserve with it; the Postgres server's own `max_connections` must allow every instance's pool.
Without `iapl()` (building blocks only): `onInit: (payload) => { reserveConnections(payload) }`.
Building blocks: `reserveConnections`, `watchTransactions`, `afterCommit` (run work right after a transaction
commits), `inWatchedTransaction`, `transactionsBusy`, `transactionWatchState` (for a health or debug route),
`poolSize`, `defaultConnectionReserve`, `RESERVE_ESCAPE_MS`.
commits), `inWatchedTransaction`, `transactionsBusy`, `transactionWatchState` (for a health or debug route:
`{ active, nested, waiting, reserve }`), `poolSize`, `defaultConnectionReserve`, `RESERVE_ESCAPE_MS`; for account mail
`withAccountMailAfterCommit(collection)` (the hook the plugin puts on every auth collection),
`sendAccountMailAfterCommit(message, send)` (what an e-mail adapter of your own calls first), `holdAccountMailFor`,
`releaseAccountMailFor`. Still true: a hook that writes without `req` is a transaction of its own — it is not rolled
back with its caller's. Pass `req`.
**Check** (staging, Postgres): send 30 submissions of a form at once (or 30 "forgot password" requests for different
accounts) while loading a page — the page answers at once, every request finishes. `transactionWatchState(payload)`
shows `{ active, waiting, reserve }`.
shows `{ active, nested, waiting, reserve }`.
### 4.4 Health check and rolling updates