Repository navigation
Understanding postgres.js connection management in Trigger.dev v3 #2218
Replies: 2 comments
|
@leedia-tech each task run gets its own child process via 1. task isolation by default, every run forks a fresh process. after the task completes, the process gets SIGTERM then SIGKILL. with 2. connection lifecycle without with your pattern matches the official drizzle guide which does the same (db instance at module level), so you're good. 3. cleanup you don't need to manually call one edge case: if you use waitpoints ( for explicit control, you can use the middleware pattern: // init.ts (auto-loaded by trigger.dev)
import { tasks } from "@trigger.dev/sdk/v3";
tasks.middleware("db", async ({ next }) => {
await next();
});
tasks.onWait("db", async () => {
await sql.end({ timeout: 5 }); // free connections during checkpoint
});
tasks.onResume("db", async () => {
// postgres.js reconnects lazily, nothing needed here
});but honestly for postgres.js this is optional since it handles reconnection automatically. ref: how it works | tasks overview (lifecycle hooks) | processKeepAlive changelog | postgres.js lazy connections |
|
One practical caveat to add to the existing explanation: the module-level client is scoped to a worker process, not to the whole Trigger.dev project. With processKeepAlive enabled, that client can be reused by sequential runs handled by that process, so the effective database connection count is roughly the pool size multiplied by the number of warm workers. For that reason, I would:
This keeps the connection lifecycle per-process and avoids accidentally creating a new pool for every execution while still releasing connections before a waitpoint checkpoint. Useful references: task execution model, processKeepAlive, Drizzle guide, and postgres.js connections. |
Uh oh!
There was an error while loading. Please reload this page.
I'm using postgres.js and noticed your Drizzle example creates the db instance outside the task.
I'm wondering:
Task isolation: Do tasks run in separate workers/processes, or share the same Node process?
Connection lifecycle: If I create a postgres instance outside the task:
Is this instance shared across all executions, or does each worker get its own?
Connection cleanup: postgres.js requires
sql.end()to properly close connections. Should I handle this cleanup or does the worker lifecycle manage it?All reactions