zlacker

[parent] [thread] 5 comments
1. postgr+(OP)[view] [source] 2021-06-12 18:51:47
> LISTEN ties up one connection completely

I've seen this twice in this thread, but I don't know what that means. Can you explain a bit?

replies(1): >>sa46+P9
2. sa46+P9[view] [source] 2021-06-12 20:33:52
>>postgr+(OP)
When a client connects to Postgres, Postgres creates a new process just for that client. Separate processes are great for isolation but it means Postgres connections are a bit expensive (this is an active area of improvement). Postgres really starts to struggle once you a have a few thousand connections, even if those connections aren’t doing anything.

The common workaround is to use a connection pooler like PGBouncer so that clients reuse connections. This approach doesn’t work for LISTEN because typically a client will listen for its entire lifecycle so you can’t share connections in a pool.

replies(2): >>taffer+og >>postgr+r41
◧◩
3. taffer+og[view] [source] [discussion] 2021-06-12 21:41:51
>>sa46+P9
You need one connection per worker thread, and realistically you would only have one worker per cpu core. So how many LISTENing connections do you really need?
replies(1): >>sa46+Ym
◧◩◪
4. sa46+Ym[view] [source] [discussion] 2021-06-12 22:49:10
>>taffer+og
You generally have more than one database connection per thread. As an example, consider Node.js which acts like a single threaded process. You’d probably want to be able to handle more than 1 database query concurrently since network latency tends to dominate OLTP requests.

How you setup LISTEN and NOTIFY is app dependent. In a multi-tenant database, you could have 1 NOTIFY channel per tenant.

As you scale, you probably do something that listens in a smarter way, maybe 1 Go channel per client with a single LISTEN instead of 1 database connection per client. The downside is that now the app code is responsible for tenant isolation instead of the database.

replies(1): >>taffer+7k1
◧◩
5. postgr+r41[view] [source] [discussion] 2021-06-13 08:09:34
>>sa46+P9
> This approach doesn’t work for LISTEN because typically a client will listen for its entire lifecycle so you can’t share connections in a pool.

But you only need one connection for LISTEN per database, total. So I'm confused why this is made out to be a big issue.

◧◩◪◨
6. taffer+7k1[view] [source] [discussion] 2021-06-13 11:43:27
>>sa46+Ym
> [...] since network latency tends to dominate OLTP requests.

You are absolutely right about that. However, in this case we are talking about background workers. I would argue that background workers, such as an image resizing worker, are typically CPU-bound and do not perform high-volume OLTP queries. Therefore, a background worker does not require multiple database connections per thread as a web server would.

[go to top]