notes

Log | Files | Refs | README

connection_pool.md (1920B)


      1 # Connection Pool
      2 
      3 A connection pool is a component (in your app or as a proxy like
      4 PgBouncer/Pgpool-II) that keeps a limited number of database connections open
      5 and reuses them across many client requests.
      6 
      7 ## Key ideas:
      8 
      9 - Opening a [PostgreSQL connection](/operating_systems/postgres/postgresql_connection.md) is
     10   relatively expensive and each connection consumes memory; creating hundreds or
     11   thousands on demand is wasteful.
     12 
     13 - The pool maintains, say, 20–50 actual DB connections and lets many more
     14   logical clients "borrow" them for short periods.
     15 
     16 - When a request finishes, the connection is returned to the pool instead of
     17   being closed, so the next request can reuse it without the startup cost.
     18 
     19 - The pool enforces an upper bound on concurrent PostgreSQL connections, which
     20   indirectly bounds per-connection memory usage.
     21 
     22 ### Question: what if the pool is full?
     23 
     24 #### General behavior when pool is full:
     25 
     26 - The pool has a max number of physical PostgreSQL connections it will open.
     27 
     28 - When all of them are checked out and a new request comes in, most pool
     29   implementations put the request in a queue and wait for a connection to be
     30   returned. If no connection becomes free before a timeout, they raise an
     31   error/exception (often something like "pool exhausted" or "timeout waiting for
     32   connection").
     33 
     34 - This is separate from PostgreSQL's own max_connections; the pool will usually
     35   hit its own limit first and throttle clients, which is the goal.
     36 
     37 ### Question: What if the DB itself is at max connections?
     38 
     39 - PostgreSQL has a server-side max_connections limit; when that is reached, any
     40   new physical connection attempt fails with "too many connections".
     41 
     42 - If your pool tries to open more physical connections beyond what the server
     43   allows (e.g. under load or after restart), those attempts will fail and bubble
     44   up as errors to the application (connection failure rather than "queued").