Database field guide · 24
Database Connections
Pools, sessions, admission, and failure diagnosis
Treat database connections as bounded stateful resources, from listener and authentication handshakes to pooling, serverless bursts, and session cleanup.
Franck Pachot7 chaptersconnections · pooling · sessions · admission
Build the mental model before choosing the mechanism.
This minibook was AI-generated from Franck Pachot's archived blog posts. Links to the original articles are included for source context and verification.
01
A connection is a protocol lifecycle
Opening a database connection traverses naming, networking, transport security, authentication, and session initialization. An error at each layer needs different evidence.
- Resolve the endpoint before debugging credentials.
- Distinguish connect timeout from query timeout.
- Retain server and client error details without exposing secrets.
02
A session consumes server state
A connected client may own a process, memory, transaction context, prepared statements, temporary objects, and observability identity. Idle does not mean free.
- Capacity planning starts with concurrent active work.
- Idle-in-transaction sessions retain locks and versions.
- Session initialization must be repeatable.
03
Pools trade setup cost for state hygiene
A pool reuses established connections and queues demand above its size. It improves admission control only when checkout deadlines, validation, reset behavior, and transaction boundaries are correct.
- Size pools from database capacity across all application instances.
- Always return connections after rollback or commit.
- Reset tenant, role, schema, and tracing state before reuse.
04
Multiplexing changes session assumptions
Transaction pooling and database-resident connection managers can route successive transactions through different server processes. Features tied to one backend may no longer persist across calls.
- Prepared statement support depends on pool mode.
- Temporary tables and session variables can pin sessions.
- Observe logical clients separately from physical backends.
05
Serverless bursts need admission control
Functions can scale clients faster than a database can create sessions. A bounded intermediary, concurrency limit, and backpressure protect the database better than unlimited connection retries.
- Reuse clients across warm invocations.
- Add jitter to bounded retries.
- Budget connections across versions, regions, and deployment overlap.
06
Diagnose from both endpoints
Client exceptions show the attempted endpoint and phase; server listeners, authentication logs, and session views show what arrived. Correlating timestamps and request identity separates refusal, timeout, and post-connect failure.
- Test from the same network path as the application.
- Count connection rate as well as concurrent sessions.
- Do not diagnose intermittent exhaustion with one successful login.
07
Field manual
Concrete mechanics, diagnostic evidence, and executable patterns to carry into a real system.
01
Budget the global pool
A per-instance pool limit multiplies by replicas, workers, regions, and overlapping deployments. Reserve capacity for administration and background jobs before dividing the remainder.
usable_connections = database_limit - admin_reserve - job_reserve
per_instance_pool = usable_connections // maximum_app_instances
# Include blue/green overlap and autoscaling maximums.
02
Set bounded pool behavior
Checkout timeout limits queueing, connection timeout limits establishment, and statement timeout limits execution. They protect different phases and should produce distinguishable telemetry.
pool.max_size = 20
pool.checkout_timeout = '2s'
connect_timeout = '5s'
statement_timeout = '10s'
# Retry only operations whose outcome is known or idempotent.
03
Find PostgreSQL connection pressure
Group sessions by application, state, and transaction age. A pool with many idle backends may be oversized; idle-in-transaction sessions are correctness and cleanup defects.
SELECT application_name, state, count(*),
max(now()-xact_start) AS oldest_transaction
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY application_name, state
ORDER BY count(*) DESC;
04
Classify a failed connection
Capture endpoint, DNS result, route, TLS phase, authentication result, and server log timestamp in order. This prevents a generic connection error from collapsing unrelated failures.
1. Resolve host and selected address.
2. Test TCP reachability from the application network.
3. Verify TLS name, trust, and wallet/certificate expiry.
4. Correlate listener and authentication logs.
5. Check session limits and pool saturation.
08
Source articles
Optional deep dives with the complete experiments and product-version context behind this guide.