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:
co-authored by
Claude Opus 5.5
parent
32bb542e8e
commit
4efadb21d5
+13
-6
@@ -139,10 +139,17 @@ once there are more than 20.
|
||||
second one; the default pool has 10. Measured on Postgres 17: 12 submissions or 12 resets at once never finished;
|
||||
now 30 of each, also against an SMTP server that never answers, finish while an ordinary request answers at once.
|
||||
- **`connectionReserve`** (new plugin option, default on): a fifth of the pool, at least 2, stays free of
|
||||
transactions; at most `pool size − reserve` transactions are open at once and the next one waits; one that has
|
||||
waited a second may use all but the last connection. `false` = as before. Building blocks `reserveConnections`,
|
||||
`watchTransactions`, `afterCommit`, `inWatchedTransaction`, `transactionsBusy`, `transactionWatchState`,
|
||||
`poolSize`, `defaultConnectionReserve`, `RESERVE_ESCAPE_MS` ([deployment.md](./deployment.md) §4.3a).
|
||||
transactions; at most `pool size − reserve` transactions are open at once and the next one waits. A transaction
|
||||
begun inside another one (a project hook that calls the Local API without `req`) takes no place and goes at once,
|
||||
but never takes the last connection — 9, 12 and 30 such submissions at once finish (before: 12 stopped the site).
|
||||
`false` = as before. Building blocks `reserveConnections`, `watchTransactions`, `afterCommit`,
|
||||
`inWatchedTransaction`, `transactionsBusy`, `transactionWatchState`, `poolSize`, `defaultConnectionReserve`,
|
||||
`RESERVE_ESCAPE_MS` ([deployment.md](./deployment.md) §4.3a, with the measured table).
|
||||
- **Payload's "forgot password" and "verify your e-mail" go out after their transaction commits** when sent through
|
||||
`mailAdapter()` (`mail.accountMailAfterCommit`, default on; same request, nothing sent on a rollback). With the mail
|
||||
service hanging, 30 resets and 30 submissions at once finish in about one send budget (11 s) instead of 43.5 s.
|
||||
Building blocks `withAccountMailAfterCommit`, `sendAccountMailAfterCommit`, `holdAccountMailFor`,
|
||||
`releaseAccountMailFor`.
|
||||
- **Form e-mails go out after the submission's transaction commits** — same request, same answer to the visitor
|
||||
(a file that could not be sent is still reported). A submission rolled back by a later hook now sends nothing; a
|
||||
send that fails after the commit leaves the submission stored (as a failed e-mail always did). Sites without
|
||||
@@ -153,8 +160,8 @@ once there are more than 20.
|
||||
is busy the write is not waited for.
|
||||
- **SMTP time-outs**: 10 s to connect (was nodemailer's 2 min), 30 s for the greeting, 60 s of silence (was 10 min);
|
||||
`mailAdapter({ smtp: { timeouts } })` / `panelSmtpAdapter({ timeouts })`, `SMTP_TIMEOUT_DEFAULTS`, `SmtpTimeouts`.
|
||||
- Not fixed here: Payload's own "forgot password" and "verify your e-mail" still send inside their transaction, so a
|
||||
hanging mail server makes them hold a place until the SMTP time-out (others wait; nothing stops).
|
||||
- Not covered: an e-mail adapter of your own (not `mailAdapter()`) still gets Payload's account mail inside the
|
||||
transaction unless it calls `sendAccountMailAfterCommit` first.
|
||||
|
||||
### Deprecated
|
||||
- **Microsoft Graph as an e-mail transport** — `graphAdapter`, `GraphAdapterArgs`, `mailAdapter({ graph })`,
|
||||
|
||||
+2
-1
@@ -1246,7 +1246,8 @@ add missing hosts with flags / `extra`; (3) change `mode` to `'enforce'` in both
|
||||
| Account e-mails through the mail service: at most 5 to one address and 10 asked for by one visitor an hour | `mail: { relay: { accountMail: false } }` | `accountMail: { perAddress, perVisitor, windowMinutes }` |
|
||||
| Every message says what it is (form notification, form confirmation, account, test, system) | — (other transports ignore the mark) | `withMailKind`, `runWithMailKind`, `expectMailTo`, `mail.relay.kind`, `buildFormMailKindHook` |
|
||||
| Microsoft Graph as transport: deprecated, one warning per start, removed in 2.0 | `mail: { graphDeprecationWarning: false }` (the warning only) | move off it: email.md "Moving off Microsoft Graph" |
|
||||
| Postgres: a fifth of the pool (at least 2) stays free of transactions — at most `pool size − reserve` open at once, the next waits; one waiting a second may use all but the last connection. Without it ten form submissions or "forgot password" requests at once stopped the site | `connectionReserve: false` | `connectionReserve: <number>`; `reserveConnections(payload, n)` in your own `onInit` (without `iapl()`); `watchTransactions`, `afterCommit`, `inWatchedTransaction`, `transactionsBusy`, `transactionWatchState`, `poolSize`, `defaultConnectionReserve`, `RESERVE_ESCAPE_MS` (deployment.md §4.3a) |
|
||||
| Postgres: a fifth of the pool (at least 2) stays free of transactions — at most `pool size − reserve` open at once, the next waits; a transaction begun inside another one (a hook without `req`) takes no place and goes at once, but never the last connection. Without it ten form submissions or "forgot password" requests at once stopped the site | `connectionReserve: false` | `connectionReserve: <number>`; `reserveConnections(payload, n)` in your own `onInit` (without `iapl()`); `watchTransactions`, `afterCommit`, `inWatchedTransaction`, `transactionsBusy`, `transactionWatchState` (`{ active, nested, waiting, reserve }`), `poolSize`, `defaultConnectionReserve`, `RESERVE_ESCAPE_MS` (deployment.md §4.3a) |
|
||||
| Payload's "forgot password" / "verify your e-mail" through `mailAdapter()` go out right after their transaction commits (same request); a rolled-back operation sends nothing | `mail: { accountMailAfterCommit: false }` | your own adapter: `sendAccountMailAfterCommit(message, send)`; `withAccountMailAfterCommit(collection)`, `holdAccountMailFor`, `releaseAccountMailFor` (email.md "Payload system mail") |
|
||||
| Form e-mails go out right after the submission's transaction commits (same request, same answer); a rolled-back submission sends nothing; a send that fails after the commit leaves the submission stored. No transactions (MongoDB without a replica set, SQLite): at once, as before | `forms: { emailsInTransaction: true }` | `sendAfterCommit(prepare, deliver)`, `sendFormEmails(req, emails)`, `afterCommit(req, fn)` (forms.md "When e-mails go out") |
|
||||
| SMTP time-outs: 10 s to connect, 30 s greeting, 60 s silence | — | `mailAdapter({ smtp: { timeouts: { connectionMs, greetingMs, socketMs } } })`, `panelSmtpAdapter({ timeouts })`; `SMTP_TIMEOUT_DEFAULTS`, type `SmtpTimeouts` (email.md "SMTP") |
|
||||
|
||||
|
||||
@@ -121,7 +121,8 @@ through this procedure. Ask only when you are missing something you cannot get y
|
||||
| the site enrols again and again (`reverify` in the log) | requests leave from many changing addresses (serverless, a pool of egress addresses) | Intecion raises the site's limit of server addresses, or SMTP |
|
||||
| start log: "Microsoft Graph as the e-mail transport is deprecated" | `GRAPH_*` is set | email.md "Moving off Microsoft Graph"; silence only: `mail: { graphDeprecationWarning: false }` |
|
||||
| Postgres: under load (many form submissions or "forgot password" requests at once) the whole site stops answering, also pages that only read; `pg_stat_activity` shows the pool's connections "idle in transaction" | each transaction holds a connection and waits for a second one (code inside it without `req`); before 1.17.0, or with `connectionReserve: false` | upgrade / remove `connectionReserve: false`; without `iapl()` call `reserveConnections(payload)` in `onInit`; your own hooks: pass `req` to every Local API call; `transactionWatchState(payload)` shows `{ active, waiting, reserve }` (deployment.md §4.3a) |
|
||||
| Postgres: requests are slow while a mail server hangs (forms answer, logins and saves wait) | Payload's "forgot password" sends inside its transaction and holds a place until the SMTP time-out | fix the mail server; shorter `mailAdapter({ smtp: { timeouts } })`; form e-mails are not affected (sent after the commit unless `forms.emailsInTransaction: true`) |
|
||||
| Postgres: requests are slow while a mail server hangs (logins and saves wait behind "forgot password") | the account mail is sent inside its transaction: `mail: { accountMailAfterCommit: false }`, `forms.emailsInTransaction: true`, or an e-mail adapter of your own that does not call `sendAccountMailAfterCommit` | remove the option / call it in your adapter; fix the mail server; shorter `mailAdapter({ smtp: { timeouts } })` |
|
||||
| Postgres: a project hook writes without `req` and the log row stays when its submission is rolled back | a Local API call without `req` is a transaction of its own (it no longer blocks the pool, but it is not rolled back with its caller) | pass `req` to every Local API call in a hook |
|
||||
| a submission is stored but its e-mail never arrived, nothing in the panel status | the transaction was rolled back after the e-mail was composed (then nothing is stored either), or the process ended right after the commit | server log `[iapl] A step that runs after a transaction commits failed` / form-builder's "Error while sending email"; forms.md "When e-mails go out" |
|
||||
| old email sender | mail client cache | check the raw headers |
|
||||
| form: `unknown_fields` | field outside the definition (honeypot not removed) | `iapl-form` → actions.ts |
|
||||
|
||||
@@ -581,7 +581,9 @@ PROGRAMISTA §6, item 73; email.md; AGENT.md §7.2) — nothing to run; read whi
|
||||
5. Your own account mail (invitations): mark it — `withMailKind(message, 'account')`. Form mail and Payload's
|
||||
"forgot password" are marked by the plugin.
|
||||
6. **Postgres sites** (nothing to run): at most `pool size − reserve` transactions are open at once (default reserve 2 of
|
||||
10; `connectionReserve`), form e-mails go out right after the submission is saved (`forms.emailsInTransaction`),
|
||||
10; `connectionReserve`; a transaction begun inside another one takes no place), form e-mails and Payload's
|
||||
"forgot password" / "verify your e-mail" go out right after their transaction commits (`forms.emailsInTransaction`,
|
||||
`mail.accountMailAfterCommit`),
|
||||
SMTP gives up sooner (10 s to connect, 60 s of silence; `smtp.timeouts`). A site that set its own `pool.max`: the
|
||||
reserve follows it. MongoDB and SQLite sites: no change. deployment.md §4.3a, forms.md "When e-mails go out".
|
||||
**Check:** `pnpm exec iapl-doctor --deep --url <site>` → no `mail-transport` / `mail-relay-state` / `mail-reply-address` /
|
||||
|
||||
+26
-9
@@ -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
|
||||
|
||||
|
||||
+7
-2
@@ -425,7 +425,8 @@ and password switches the site to the client's server.
|
||||
- The adapter reads the config on every send, so changing the mailbox in the panel works without a restart.
|
||||
- **Time-outs** (since 1.17.0): 10 s to connect, 30 s for the server's greeting, 60 s of silence during the
|
||||
dialogue (`SMTP_TIMEOUT_DEFAULTS`; nodemailer's own were 2 min and 10 min). A send inside a database transaction —
|
||||
Payload's "forgot password" — holds a connection that long ([deployment.md](./deployment.md) §4.3a). Change them:
|
||||
Payload's "forgot password" with `mail: { accountMailAfterCommit: false }` — holds a connection that long
|
||||
([deployment.md](./deployment.md) §4.3a; by default that mail goes out after its transaction commits). Change them:
|
||||
`mailAdapter({ smtp: { timeouts: { connectionMs, greetingMs, socketMs } } })` or `panelSmtpAdapter({ timeouts })`
|
||||
(type `SmtpTimeouts`).
|
||||
|
||||
@@ -494,7 +495,11 @@ if (result.sent) {
|
||||
|
||||
Password reset and e-mail verification go through Payload's built-in mechanism (`config.email`). With
|
||||
`mailAdapter()` wired they take the same transport as everything else; with the mail service they are sent as
|
||||
account mail ("What a message is").
|
||||
account mail ("What a message is"). Payload sends them inside the operation's database transaction; `mailAdapter()`
|
||||
sends them right **after** it commits instead (still in the same request; nothing when it is rolled back), so a slow
|
||||
mail server holds no database connection — `mail: { accountMailAfterCommit: false }` keeps them inside
|
||||
([deployment.md](./deployment.md) §4.3a). An e-mail adapter of your own does the same by calling
|
||||
`sendAccountMailAfterCommit(message, () => send(message))` first.
|
||||
|
||||
## Masking secrets in the panel (MaskedField)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user