Postgres: many requests at once no longer stop the site

Each Payload transaction holds a pool connection; code inside it that runs
without its req needs a second one (Payload's own forgotPassword, the mail
adapter's settings read and status write, payload.kv). With the default pool
of 10, ten form submissions or ten "forgot password" requests at once held
every connection and waited for good.

- connectionReserve (default on): at most pool size − reserve transactions
  open at once; a waiter older than a second may use all but the last
  connection. Building blocks in modules/database/transactions.ts.
- Form e-mails go out right after the submission's transaction commits, in
  the same request (forms.emailsInTransaction: true = the old way).
- recordIntegrationStatus merges identical writes and does not hold its
  caller while the pool is busy.
- SMTP time-outs: 10 s connect, 30 s greeting, 60 s silence (smtp.timeouts).

Tests on Postgres 17: 30 submissions, 30 forgot-password, and both against
an SMTP server that never answers, finish while a page answers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 00:33:07 +01:00
co-authored by Claude Opus 5.5
parent acfcf72c03
commit 32bb542e8e
25 changed files with 916 additions and 23 deletions
+39
View File
@@ -351,6 +351,45 @@ 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 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;
- 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).
```ts
iapl({ connectionReserve: 3 }) // keep three connections free
iapl({ connectionReserve: false }) // no limit, as before 1.17.0 (not recommended)
```
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`.
**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 }`.
### 4.4 Health check and rolling updates
Coolify → the application → **Health Checks**: HTTP, path `/api/health`, port 3000, a start period long enough