Senior
How many connections were there to PostgreSQL? What to do if connections are insufficient at 10,000 RPS?
sobes.tech AI
Answer from AI
The number of active connections to PostgreSQL can be checked using the following query on the system view:
SELECT count(*) FROM pg_stat_activity;
If there are not enough connections at a load of 10,000 RPS, then:
- Increase the
max_connectionsparameter in the PostgreSQL configuration, but this is not always scalable. - Use a connection pool, such as PgBouncer or the built-in pool in your application, to reuse connections.
- Optimize queries and transactions to take less time, reducing the duration of connection holding.
- Consider horizontal scaling or sharding if the load is too high.
In Go, connection pools are usually managed through database/sql, which controls the number of open connections:
db.SetMaxOpenConns(100) // limit the maximum number of open connections
Thus, proper pool management and query optimization are key to handling a large number of RPS.