Skip to content

Commit d973359

Browse files
authored
docs: add a database connections guide for tasks (#3881)
## Summary A new guide for connecting a database to your tasks: where to create the client, how to size the connection pool against your provider's limit, when to reach for a pooler, and how to release connections at waits so you don't hit "too many connections" or crash on resume. It covers node-postgres, Prisma, Drizzle, and MongoDB, with researched direct and pooled connection limits for the common Postgres providers (Supabase, Neon, RDS, PlanetScale) and MongoDB Atlas. The page lives under Documentation, Troubleshooting, and is linked from the chat agent docs (overview, lifecycle hooks, chat.local, and the database persistence pattern).
1 parent 774a979 commit d973359

6 files changed

Lines changed: 220 additions & 2 deletions

File tree

docs/ai-chat/chat-local.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,4 @@ onTurnComplete: async ({ chatId }) => {
171171
- [Lifecycle hooks](/ai-chat/lifecycle-hooks)`onBoot` is the canonical init site for `chat.local`.
172172
- [Database persistence pattern](/ai-chat/patterns/database-persistence) — full per-hook breakdown using `chat.local` alongside DB rows.
173173
- [Code execution sandbox pattern](/ai-chat/patterns/code-sandbox) — example of using `chat.local` to hold a sandbox handle across turns.
174+
- [Database connections](/database-connections) — why the database client and its connection pool belong at module scope, not in `chat.local`.

docs/ai-chat/lifecycle-hooks.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Standard [task lifecycle hooks](/tasks/overview) such as `onWait`, `onResume`, `
3737

3838
Fires **once per worker process picking up the chat** — for the initial run, for preloaded runs, AND for reactive continuation runs (post-cancel, crash, `endRun`, `requestUpgrade`, OOM retry). Does NOT fire when the same run resumes from snapshot via the idle-window suspend/resume path — use [`onChatResume`](#onchatsuspend--onchatresume) for that.
3939

40-
This is the right place to initialize anything that lives in the JS process for the lifetime of the run: [`chat.local`](/ai-chat/chat-local) state, DB connections, sandboxes, in-memory caches. It runs before `onPreload`, `onChatStart`, the continuation-wait branch, and any turn — so anything you set up here is available everywhere downstream.
40+
This is the right place to initialize anything that lives in the JS process for the lifetime of the run: [`chat.local`](/ai-chat/chat-local) state, [DB connections](/database-connections), sandboxes, in-memory caches. It runs before `onPreload`, `onChatStart`, the continuation-wait branch, and any turn — so anything you set up here is available everywhere downstream.
4141

4242
<Warning>
4343
If you initialize `chat.local` only in `onChatStart`, your `run()` will crash on continuation runs with `chat.local can only be modified after initialization`. `onChatStart` is once-per-chat by contract; `chat.local` is per-process and needs `onBoot`.

docs/ai-chat/overview.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,7 @@ Three primitives, related but distinct:
8484
<Card title="Patterns" icon="puzzle-piece" href="/ai-chat/patterns/sub-agents">
8585
HITL approvals, branching, sub-agents, OOM/crash recovery.
8686
</Card>
87+
<Card title="Database connections" icon="database" href="/database-connections">
88+
Size and release connection pools so agents don't exhaust your database.
89+
</Card>
8790
</CardGroup>

docs/ai-chat/patterns/database-persistence.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Storing the current **`runId`** is optional — useful for telemetry / dashboard
3434

3535
## Where each hook writes
3636

37-
This pattern covers **durable DB rows** (the conversation and the active session). Per-process in-memory state ([`chat.local`](/ai-chat/chat-local), DB connection pools, sandboxes, etc.) belongs in [`onBoot`](/ai-chat/lifecycle-hooks#onboot) — it fires on every fresh worker including continuation runs, where `onPreload` and `onChatStart` do not.
37+
This pattern covers **durable DB rows** (the conversation and the active session). Per-process in-memory state ([`chat.local`](/ai-chat/chat-local), [DB connection pools](/database-connections), sandboxes, etc.) belongs in [`onBoot`](/ai-chat/lifecycle-hooks#onboot) — it fires on every fresh worker including continuation runs, where `onPreload` and `onChatStart` do not.
3838

3939
### `onPreload` (optional)
4040

docs/database-connections.mdx

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
---
2+
title: "Database connections"
3+
sidebarTitle: "Database connections"
4+
description: "Connect a database to your tasks: where to create the client, how to size the pool for your provider's connection limit, and how to release connections so you don't run out."
5+
---
6+
7+
Tasks connect to your database from their own process. This guide covers the recommended setup for each client, how to size the pool against your provider's connection limit, and how to release connections at waits.
8+
9+
## Create the client once
10+
11+
Create the client at module scope and import it wherever you query. The worker loads the module once per process, so every run on that worker reuses the same pool. Keep the pool small (see [Size the pool](#size-the-pool)) and attach an error handler, since an idle connection can error asynchronously and an unhandled `error` event crashes the worker.
12+
13+
<CodeGroup>
14+
```ts lib/db.ts (node-postgres)
15+
import { Pool } from "pg";
16+
17+
export const pool = new Pool({
18+
connectionString: process.env.DATABASE_URL,
19+
max: 1, // one connection per run; raise only for in-run parallel queries
20+
});
21+
22+
pool.on("error", (err) => console.error("pg pool error", err));
23+
```
24+
25+
```ts lib/db.ts (Prisma)
26+
import { PrismaPg } from "@prisma/adapter-pg";
27+
import { PrismaClient } from "./generated/prisma/client";
28+
29+
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL, max: 1 });
30+
31+
export const prisma = new PrismaClient({ adapter });
32+
```
33+
34+
```ts lib/db.ts (Drizzle)
35+
import { drizzle } from "drizzle-orm/node-postgres";
36+
import { Pool } from "pg";
37+
38+
const pool = new Pool({ connectionString: process.env.DATABASE_URL, max: 1 });
39+
pool.on("error", (err) => console.error("pg pool error", err));
40+
41+
export const db = drizzle({ client: pool });
42+
```
43+
44+
```ts lib/db.ts (MongoDB)
45+
import { MongoClient } from "mongodb";
46+
47+
export const client = new MongoClient(process.env.DATABASE_URL!, { maxPoolSize: 5 });
48+
```
49+
</CodeGroup>
50+
51+
<Note>
52+
Import this one client everywhere. Don't create a client inside `run()` or a lifecycle hook, which opens a new pool on every run, and don't store one in [`chat.local`](/ai-chat/chat-local), which is per-run state that gets serialized into subtasks.
53+
</Note>
54+
55+
## Size the pool
56+
57+
A run uses connections only while it is actively executing. Queued, waiting, and suspended runs use none. So the connections in use at any moment are:
58+
59+
> concurrent executing runs × pool size per run
60+
61+
Set the pool small. A task usually runs its queries in sequence, so one connection per run (`max: 1`) is enough for node-postgres, Prisma, and Drizzle; raise it only when a single run issues queries in parallel. The MongoDB driver shares one pool across all operations, so keep `maxPoolSize` in the low single digits. Each client's out-of-the-box default is far larger:
62+
63+
| Client | Default pool size |
64+
| --- | --- |
65+
| [node-postgres (`pg`)](https://node-postgres.com/guides/pool-sizing) | 10 |
66+
| postgres-js | 10 |
67+
| [Prisma (v7, `pg` adapter)](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections/connection-pool) | 10 (the adapter's `pg` default) |
68+
| Drizzle (node-postgres) | 10 (the underlying `pg` pool) |
69+
| [MongoDB driver](https://www.mongodb.com/docs/drivers/node/current/connect/connection-options/connection-pools/) | 100 (`maxPoolSize`) |
70+
71+
Keep `concurrent runs × pool size` under your provider's connection limit, and cap how many runs execute at once with [concurrency limits](/queue-concurrency) so runs queue instead of overrunning the database. Direct connection limits for common Postgres providers:
72+
73+
| Provider | Direct connection limit |
74+
| --- | --- |
75+
| [PostgreSQL (self-hosted)](https://www.postgresql.org/docs/current/runtime-config-connection.html) | `max_connections`, default `100` |
76+
| [Supabase](https://supabase.com/docs/guides/platform/compute-and-disk) | `60` (Nano/Micro) up to `500` (16XL), by compute size |
77+
| [Neon](https://neon.com/docs/connect/connection-pooling) | `104` (0.25 CU) up to `4000` (capped at 9 CU and above), by compute size |
78+
| [AWS RDS / Aurora](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html) | `LEAST(DBInstanceClassMemory / 9531392, 5000)`, ~5 reserved for superusers |
79+
| [PlanetScale Postgres](https://planetscale.com/docs/postgres/connecting) | set per cluster size (Cluster, then Parameters, then `max_connections`) |
80+
81+
[MongoDB Atlas](https://www.mongodb.com/docs/atlas/reference/atlas-limits/) limits connections per node: `500` on Free and Flex, `1500` on M10, `3000` on M20.
82+
83+
When `concurrent runs × pool size` approaches these numbers, connect through a pooler instead.
84+
85+
## Use a connection pooler
86+
87+
A pooler (PgBouncer, RDS Proxy, Supavisor, Prisma Accelerate) sits between your tasks and the database and multiplexes many client connections onto a few backend connections. Point your connection string at the pooler's endpoint and the ceiling rises without changing your code. Use one when many runs execute concurrently, and for chat agents.
88+
89+
| Provider | Pooled endpoint | Pooled client limit |
90+
| --- | --- | --- |
91+
| [Supabase Supavisor](https://supabase.com/docs/guides/database/connection-management) | port `6543` (transaction mode) | `200` (Nano) up to `12,000` (16XL) |
92+
| [Neon](https://neon.com/docs/connect/connection-pooling) | add `-pooler` to the endpoint host | up to `10,000` |
93+
| [AWS RDS Proxy](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/rds-proxy.html) | the proxy endpoint | managed |
94+
| [PlanetScale Postgres](https://planetscale.com/blog/scaling-postgres-connections-with-pgbouncer) | PgBouncer endpoint | managed |
95+
| Self-hosted | PgBouncer or PgCat | configured |
96+
97+
Use the pooled endpoint for your tasks. Use the direct endpoint for schema migrations (Prisma Migrate, Drizzle Kit), which need a stable session that a transaction pooler does not provide.
98+
99+
Transaction-mode poolers (Supavisor on `6543`, PgBouncer in transaction mode) do not keep server-side prepared statements across queries. With Prisma, add `?pgbouncer=true` to the pooled URL. With node-postgres, don't rely on prepared statements.
100+
101+
## Private databases
102+
103+
If your database lives in a private VPC and isn't reachable over the public internet, connect to it with [private networking](/private-networking/overview), which links your tasks to resources in your own AWS account over AWS PrivateLink. It supports Postgres (RDS, Aurora), MySQL, MongoDB, and any other TCP service behind an internal load balancer.
104+
105+
Once the connection is active, set your connection-string variable (for example `DATABASE_URL`) to the endpoint IP shown in the dashboard, and the client setup above is unchanged. Private networking is a Pro and Enterprise feature, and the endpoint is reachable only from deployed environments, so use a public connection in local development.
106+
107+
## Provider notes
108+
109+
- [Supabase](https://supabase.com/docs/guides/database/connecting-to-postgres): the direct connection (`db.<ref>.supabase.co:5432`) resolves to IPv6 only and is unreachable from many environments, so connect through the Supavisor pooler or add the IPv4 add-on. The pooler presents Supabase's own CA, so prefer passing that CA and keeping verification on (`rejectUnauthorized: true`). Use `rejectUnauthorized: false` only as a temporary troubleshooting step in non-production environments.
110+
- A `DATABASE_URL` with `sslmode=verify-full&sslrootcert=system` uses a libpq feature the `pg` driver (node-postgres, and the Prisma and Drizzle pools built on it) cannot read. Build the pool from discrete fields with `ssl: { rejectUnauthorized: true }` (Node's CA store), or point `sslrootcert` at a real CA file.
111+
112+
## Release connections at a wait
113+
114+
A run holds its connections while it is paused at a wait until the process is torn down, which is not instant. Free them sooner so other runs can reuse them. How you release depends on the client:
115+
116+
- Prisma reconnects lazily, so disconnect it from a global [`tasks.onWait`](/tasks/overview#onwait-and-onresume-functions) handler colocated with the client. One handler covers every task.
117+
- A `pg` Pool (node-postgres and Drizzle) and the MongoDB client can't be reused after a full close, so give them a short idle timeout instead. Idle connections close themselves during the wait while the pool stays usable.
118+
119+
<CodeGroup>
120+
```ts lib/db.ts (node-postgres)
121+
import { Pool } from "pg";
122+
123+
export const pool = new Pool({
124+
connectionString: process.env.DATABASE_URL,
125+
max: 1,
126+
idleTimeoutMillis: 10_000, // idle connections close during a wait; the pool stays usable
127+
});
128+
129+
pool.on("error", (err) => console.error("pg pool error", err));
130+
```
131+
132+
```ts lib/db.ts (Prisma)
133+
import { tasks } from "@trigger.dev/sdk";
134+
import { PrismaPg } from "@prisma/adapter-pg";
135+
import { PrismaClient } from "./generated/prisma/client";
136+
137+
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL, max: 1 });
138+
export const prisma = new PrismaClient({ adapter });
139+
140+
// Disconnect when any run pauses; Prisma reconnects on the next query.
141+
tasks.onWait("db", () => prisma.$disconnect());
142+
```
143+
144+
```ts lib/db.ts (Drizzle)
145+
import { drizzle } from "drizzle-orm/node-postgres";
146+
import { Pool } from "pg";
147+
148+
const pool = new Pool({
149+
connectionString: process.env.DATABASE_URL,
150+
max: 1,
151+
idleTimeoutMillis: 10_000,
152+
});
153+
pool.on("error", (err) => console.error("pg pool error", err));
154+
155+
export const db = drizzle({ client: pool });
156+
```
157+
158+
```ts lib/db.ts (MongoDB)
159+
import { MongoClient } from "mongodb";
160+
161+
export const client = new MongoClient(process.env.DATABASE_URL!, {
162+
maxPoolSize: 5,
163+
maxIdleTimeMS: 10_000, // idle sockets close during a wait; the client stays usable
164+
});
165+
```
166+
</CodeGroup>
167+
168+
Don't hold a client across a slow await, either. `pool.query()` checks a connection out and returns it in one call. If you `pool.connect()` and keep the client across an external HTTP call or a model stream, you pin that connection for the whole operation. Query, release, then do the slow work.
169+
170+
## Chat agents
171+
172+
A chat agent runs one long-lived worker per conversation and suspends between messages, so its connection count tracks the conversations streaming a turn at the same moment. The global `tasks.onWait` handler above covers chat agents too. Two more specifics:
173+
174+
- Don't hold a connection across `streamText()`. A turn spends most of its time waiting on the model, so query and release before the stream starts.
175+
- To release only when a conversation goes idle (rather than on every internal wait within a turn), use [`onChatSuspend`](/ai-chat/lifecycle-hooks#onchatsuspend--onchatresume) instead of the global handler.
176+
177+
```ts /trigger/chat.ts
178+
import { chat } from "@trigger.dev/sdk/ai";
179+
import { streamText } from "ai";
180+
import { openai } from "@ai-sdk/openai";
181+
import { prisma } from "@/lib/db";
182+
183+
export const myChat = chat.agent({
184+
id: "my-chat",
185+
run: async ({ messages, clientData, signal }) => {
186+
const user = await prisma.user.findUnique({ where: { id: clientData.userId } });
187+
// The connection is back in the pool before the model stream starts.
188+
return streamText({
189+
model: openai("gpt-4o"),
190+
system: `Helping ${user?.name ?? "the user"}.`,
191+
messages,
192+
abortSignal: signal,
193+
});
194+
},
195+
});
196+
```
197+
198+
## Troubleshooting
199+
200+
`too many connections` or connection refused: `concurrent runs × pool size` is over your provider's limit. Lower the pool size, cap [concurrency](/queue-concurrency), or connect through a pooler.
201+
202+
The worker crashes right after resuming from a wait: an idle connection that closed during the suspend emitted an unhandled `error` event. Attach `pool.on("error", ...)` on a `pg` pool (node-postgres or Drizzle); Prisma and the MongoDB driver handle this internally.
203+
204+
## How suspend affects connections
205+
206+
When a task waits, the runtime can [checkpoint](/how-it-works#the-checkpoint-resume-system) the run: it snapshots the process and frees the compute, then restores the process when the wait resolves. Process memory comes back, so your pool object survives, but the database closed the idle connections in the meantime. The pool reconnects on the first query after resume. This is why a suspended run holds no connections, and why the pool needs an error handler to absorb the closed connection cleanly.
207+
208+
## See also
209+
210+
- [Wait](/wait) for the primitives that trigger a checkpoint.
211+
- [Concurrency and queues](/queue-concurrency) to cap how many runs execute at once.
212+
- [Lifecycle functions](/tasks/overview#onwait-and-onresume-functions) for global `tasks.onWait` and `tasks.onResume`.
213+
- [Chat agent lifecycle hooks](/ai-chat/lifecycle-hooks) for `onChatSuspend` and `onChatResume`.

docs/docs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@
290290
"group": "Troubleshooting",
291291
"pages": [
292292
"troubleshooting",
293+
"database-connections",
293294
"how-to-reduce-your-spend",
294295
"troubleshooting-debugging-in-vscode",
295296
"upgrading-packages",

0 commit comments

Comments
 (0)