Skill 2.2.6
Implement connection optimization to improve throughput and minimize latency
Key Concepts
Client-side pooling, such as psycopg_pool.ConnectionPool, keeps a set of open connections that requests borrow and return. PgBouncer is built into flexible server and, when enabled, listens on port 6432; it multiplexes many client connections onto fewer server connections, which suits serverless functions and horizontally scaled containers. In transaction pooling mode, session state such as prepared statements and SET commands does not persist between transactions.
Latency also depends on placement and round trips. The application should run in the same region, ideally over private networking; queries should fetch only needed columns and use batching (executemany or COPY) for bulk inserts. max_connections depends on server memory, and monitoring active connections reveals leaks.