๐—ฃ๐—ผ๐˜€๐˜๐—ด๐—ฟ๐—ฒ๐—ฆ๐—ค๐—Ÿ ๐—–๐—ผ๐—ป๐—ป๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป ๐—ฃ๐—ผ๐—ผ๐—น๐—ถ๐—ป๐—ด ๐—˜๐˜…๐—ฝ๐—น๐—ฎ๐—ถ๐—ป๐—ฒ๐—ฑ

Opening a new database connection for every query is slow. It costs your backend too much memory and time.

Why connections cost you:

Without a pool, a request with three queries takes 86ms. With a pool, it takes 6ms. Setup waste is 93 percent of your time.

High connection counts hurt your server:

The solution is connection pooling. A pool is a cache of open connections. Your app borrows a connection, runs a query, and returns it.

PgBouncer is the industry standard. It sits between your app and Postgres. It decouples app requests from database connections.

Choose your pool mode carefully:

Sizing your pool: Do not set your pool too high. Formula: (CPU cores x 2) + disk spindles. Too many connections reduce throughput.

How to monitor: Connect to PgBouncer and run SHOW POOLS. Look at cl_waiting. If this number is above zero, your pool is too small.

Source: https://dev.to/sharafathalivk/postgresql-connection-pooling-explained-how-it-works-and-why-it-matters-31g6