Sub topic 1
Why PostgreSQL Connections Are Expensive
A PostgreSQL connection is more than a lightweight network socket. PostgreSQL usually creates a dedicated server process for each client connection. That process authenticates the client, allocates memory, and maintains connection-specific state until the client disconnects.
This chapter assumes you know basic SQL, the psql client, PostgreSQL roles, and the max_connections setting. You should also be comfortable reading Linux configuration files and using TCP host and port settings.
Prerequisites
You need:
- A PostgreSQL server you can inspect.
- Access to
psqlwith permission to read connection statistics. - Basic knowledge of databases, roles, and client applications.
- A way to run SQL statements in the target database.
What happens when a client connects
A typical connection follows several steps:
- The client opens a TCP connection to PostgreSQL.
- PostgreSQL accepts the connection and creates a server process.
- The server authenticates the role, often using a password or certificate.
- PostgreSQL selects the requested database.
- The client and server negotiate settings and begin exchanging messages.
- The server process remains available until the client disconnects.
The work in these steps happens even when the client immediately runs a small query. Creating and destroying many connections can therefore consume more resources than running the queries themselves.
SELECT
current_setting('max_connections') AS max_connections,
count(*) AS current_connections
FROM pg_stat_activity;max_connections limits how many client connections PostgreSQL can accept. pg_stat_activity shows connections known to the server, including active sessions and sessions that are currently idle.
Why idle connections still cost resources
An idle connection is waiting for the next command, but its PostgreSQL server process still exists. It consumes process-table capacity, memory, authentication state, and a slot counted against max_connections.
The exact memory cost varies by PostgreSQL version, extensions, workload, and configuration. A connection can also allocate more memory while executing a query. As the number of simultaneous connections rises, the operating system and PostgreSQL spend more time scheduling processes and managing memory.
| Connection pattern | Main cost |
|---|---|
| Short-lived connections | Repeated startup, authentication, and teardown |
| Long-lived idle connections | Memory and a slot under `max_connections` |
| Many active connections | Process scheduling, memory pressure, and query contention |
Connection storms
Applications often create too many connections during predictable events:
- A web service starts many workers at once.
- A deployment replaces several application instances.
- A traffic spike causes autoscaling.
- A database restart makes every client reconnect together.
- A serverless function creates a new connection for each invocation.
This pattern is called a connection storm. PostgreSQL may spend its available capacity accepting and authenticating clients instead of processing useful queries. Some clients then wait, fail, or retry, which creates even more connection attempts.
Why a pooler helps
A connection pooler keeps a controlled number of PostgreSQL connections open and lets many client connections share them over time. Clients can connect to the pooler quickly, while the pooler reuses database connections instead of creating one PostgreSQL process per client.
This separates two limits:
- The number of client connections an application can hold.
- The smaller number of server connections PostgreSQL must manage.
A pooler does not make expensive queries faster, and it cannot remove database locking or CPU limits. Its main benefit is reducing connection setup and keeping PostgreSQL from being overwhelmed by idle or short-lived clients. The next chapters use PgBouncer to apply this model.
Exercise
An application has 800 mostly idle client connections, but only 20 queries run at the same time. Why might a pooler help?
Show answer
The pooler can accept many client connections while reusing a smaller number of PostgreSQL server connections, reducing idle process and connection-slot usage.