Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

Database Connections and Pooling

Direct answer

Database connections are expensive server resources; pooling (application pools, PgBouncer, ProxySQL) reuses a smaller number of connections for many clients.

When to use this

Use it when connection counts hit limits or new connections are slow.

First 30 seconds

1. Current vs max: SELECT count(*) FROM pg_stat_activity; / SHOW STATUS LIKE 'Threads_connected';.

2. Idle connections per app.

3. Pool size × instances ≤ limit.

4. Pooler stats (SHOW POOLS; in PgBouncer).

5. Errors "too many connections".

Operational verification

app → pool → limited server connections → queries

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it when connection counts hit limits or new connections are slow.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 How to verify Verify boundary

Watch connections during peak load. Look for idle in transaction sessions that hold connections and locks. Size the pool from database CPU and I/O capacity.

Step 2 Common failures Narrow the fault

Raising max_connections without memory planning can make the server swap. Many application instances each with a large pool exceed the server limit. Leaked connections (never returned) exhaust the pool.

Key points

Size pools so all app instances together stay under max_connections. PgBouncer transaction mode breaks session features (prepared statements, temp tables) unless supported.

Key checks
  • SELECT count(*) FROM pg_stat_activity; — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand

How it works

A pool keeps connections open and hands one to each request for its duration. Transaction-mode pooling in PgBouncer returns the connection after each transaction, allowing far more clients than server connections, but session state such as prepared statements may not carry over.

ReferenceImportant terms & boundaries3 sections · depth on demand

Pooling and limits

DatabaseLimitPooler
PostgreSQLmax_connections (each is a process)PgBouncer (transaction pooling), application pools
MySQL/MariaDBmax_connections (threads)ProxySQL, application pools
SQL ServerEffectively highADO.NET/JDBC pools

Check current use: SELECT count(*) FROM pg_stat_activity; or SHOW STATUS LIKE 'Threads_connected';. "Too many connections" usually means leaked connections or no pooling, not a need to raise the limit.

In depth

Why pooling matters

Opening a connection costs a TCP and TLS handshake plus authentication; in PostgreSQL each connection is also a separate backend process with its own memory. Many short requests that each open a connection spend more time connecting than querying, and thousands of idle connections use memory and slow snapshot handling.

PgBouncer pool modes

ModeServer connection returned to poolBreaks
sessionWhen the client disconnectsNothing; least saving
transactionAt COMMIT/ROLLBACKSession state: SET, advisory locks, LISTEN, temp tables; prepared statements unless supported by the PgBouncer version and driver
statementAfter each statementMulti-statement transactions

Sizing

Total connections = application instances × pool size per instance. Ten pods with a pool of 20 already make 200 connections. A useful starting point for the database side is a pool of a few connections per CPU core of the database server; more connections than the server can run in parallel only adds queueing and lock contention inside the database.

Worked check — who holds the connections?

SELECT usename, application_name, state, count(*)
FROM pg_stat_activity GROUP BY 1,2,3 ORDER BY 4 DESC;
-- many 'idle in transaction' → application keeps transactions open
SHOW STATUS LIKE 'Threads_connected';  SHOW VARIABLES LIKE 'max_connections';   -- MySQL

Set idle_in_transaction_session_timeout (PostgreSQL) or wait_timeout (MySQL) so leaked connections are closed, and pool timeouts on the client so callers fail fast instead of queuing forever.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • No connection pooling, exhausting max connections.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
database connections and poolingconnection pooling pgbouncermax_connections
Confirmation

Confirm action

Continue?