Merge claude/postgres-mail-deadlock: Postgres keeps connections free of transactions; form and account mail go out after the commit

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 01:38:31 +01:00
co-authored by Claude Opus 5.5
30 changed files with 1431 additions and 27 deletions
+57
View File
@@ -356,6 +356,63 @@ with `NODE_ENV=production` — also during `next build` when the build reaches t
schema: add columns/tables first, drop or rename in a later release (`iapl-migrate-fields` skill). Take a
**Backup now** (§4.6) before a large migration.
### 4.3a Postgres: the connection pool
Payload holds one pool connection for the whole of a transaction (a form submission, "forgot password", a save in
the panel). Code inside it that runs **without** the transaction's `req` needs a **second** connection: Payload's own
"forgot password" (it reserves the request interval outside the transaction), an e-mail adapter reading its settings,
`payload.kv`, a project hook that calls the Local API without `req`. With the default pool of 10, **ten such requests
at the same moment held all ten connections and each waited for an eleventh — the whole site stopped, for good**
(measured on 1.16.8 and the 1.17.0 branch: 12 form submissions or 12 "forgot password" requests at once, an ordinary
page got no answer).
Since 1.17.0 the plugin keeps a few connections free of transactions (`connectionReserve`):
- 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 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"),
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:
`{ active, nested, waiting, reserve }`), `poolSize`, `defaultConnectionReserve`, `RESERVE_ESCAPE_MS`; for account mail
`withAccountMailAfterCommit(collection)` (what the plugin does to every auth collection: wraps its
`generateEmailSubject`, which Payload calls with the request and the token right before the send),
`sendAccountMailAfterCommit(message, send)` (what an e-mail adapter of your own calls first), `holdAccountMail`,
`releaseAccountMail`. 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, nested, waiting, reserve }`.
### 4.4 Health check and rolling updates
Coolify → the application → **Health Checks**: HTTP, path `/api/health`, port 3000, a start period long enough