连接池在多个客户端请求中重复使用少量数据库连接,而不是为每个请求打开一个新连接。PostgreSQL 连接很昂贵(每个连接都是一个占用大量内存的独立进程),因此池化对于负载下的性能和可扩展性至关重要。PgBouncer 是标准的连接池。
问题:PostgreSQL 连接很昂贵
Each PostgreSQL connection = a separate OS PROCESS, using ~5-10MB of memory.
✗ Opening a connection has overhead (process creation, auth, setup)
✗ Many connections (e.g. 1000s from a busy app) → huge memory use, contention
✗ Postgres has a max_connections limit (often ~100); exceeding it = errors
→ Opening/closing a connection per request, or holding thousands open, doesn't scale.
