Closed sdorazio closed 1 year ago
Unfortunately, connection pooling doesn't currently work on Cloudflare Workers and similar platforms.
But we'll be making new Client connections significantly quicker soon, and we'll also be looking at possibilities for pooling going forward.
@jawj, I found an official blog post https://neon.tech/docs/serverless/serverless-driver which create a pool and calls 1 query, only to end it immediately. That's not very good use of a pool?
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
const pool = new Pool({ connectionString: env.DATABASE_URL });
const { rows: [{ now }] } = await pool.query('SELECT now()');
ctx.waitUntil(pool.end()); // this doesn’t hold up the response
return new Response(`The time is ${now}`);
}
}
Could you explain why that's done?
Ahh, I think that's just a trivial example designed to be extended. A pool would be useful if you wanted to make parallel queries. I tried to write some more on https://stackoverflow.com/questions/76211890/when-to-use-pg-client-instead-of-pool-max-1-connection-on-serverless/76336202#76336202
And also, that connection pooling does work on Cloudflare now, since the official blog post uses it?
Creating a new client / calling client.connect() for every request / cron trigger in a Cloudflare worker (or any codebase) is not ideal. I suggest updating the demo to use a connection pool instead. Alternatively, give a second demo with connection pools.