Arezgitfield notes / engineering
Engineering practiceUPDATED OCT 15, 2026

Budget PostgreSQL Connections Across Application Pools

Size PostgreSQL connection pools across replicas, workers, and deployment overlap, with bounded waits and evidence for diagnosing saturation.

Arezgit Editorial5 min read
AREZGIT / FIELD NOTEENGINEERING PRACTICE
A pool configured for ten connections can become a hundred database sessions when an application runs ten processes. A deployment can temporarily add another set before old instances finish
READ / VERIFY / APPLYTECHNICALLY REVIEWED

A pool configured for ten connections can become a hundred database sessions when an application runs ten processes. A deployment can temporarily add another set before old instances finish draining. Size connection pools against the complete topology that can coexist, then decide how excess work waits or fails.

This guide covers application-side pool budgets for PostgreSQL, using node-postgres documentation for specific client behavior. Its arithmetic is a synthetic capacity example rather than a benchmark or recommended default. A pooling proxy introduces another boundary that needs separate measurement.

Inventory every process that can open a pool

List web processes, job workers, scheduled tasks, migration runners, reporting services, and operational tools. Record how many pools each process creates and the maximum size of each pool. A replica count alone is insufficient if each replica starts several independent worker processes.

Use maximum simultaneous process counts, including rollout overlap and autoscaling limits. If the deployment can run six new instances alongside three draining instances, the connection budget must account for nine until the older instances release their sessions. A rollback may create a similar overlap.

The node-postgres pool sizing guide explicitly calls for considering pool limits across services and instances. Apply that reasoning to each database destination. Read replicas and independent databases have their own limits; they should not be combined into one fictional capacity figure.

Make pool ownership explicit in the application. A request handler that creates a new pool changes the topology with traffic volume. Prefer a bounded process-level lifecycle where that matches the framework, and verify that hot reload or background task initialization does not create extra retained pools.

Allocate headroom before dividing capacity

PostgreSQL's connection settings include the overall connection maximum and reserved slots. Increasing the maximum also increases resources allocated according to that setting. Treat it as a server capacity decision rather than a quick response to an application queue.

For a synthetic server with a maximum of 160 connections, suppose the operator sets aside 20 for reserved access and operating headroom, then allocates 30 to workers and scheduled tasks. That leaves an application allocation of 110 for web processes. If ten web processes can coexist during deployment, a per-process maximum of 11 fits that allocation.

This calculation checks a configured upper bound. It does not establish that 110 simultaneous queries would perform well, nor that all pools will open every possible session. Confirm that the reservations match the server's actual role privileges and that operational access remains possible during saturation.

If the calculated pool is too small for useful throughput, inspect connection hold time and workload concurrency before increasing it. A long transaction waiting on an external service can occupy capacity without making database progress. More sessions may simply permit more work to wait on the same constrained resource.

With a proxy, budget client connections and database sessions separately. Review the proxy's pooling mode and compatibility requirements for transactions and session state. A large client allowance should not be interpreted as an equivalent number of available backend sessions.

Bound waiting work and distinguish timeout scopes

Choose how much work the service may admit while all clients are occupied. A pool's maximum connection count does not by itself define an acceptable number of waiting HTTP requests or queued jobs. Add admission limits at the layer that owns those operations.

The node-postgres Pool API documents connection acquisition behavior, connectionTimeoutMillis, and the pool's total, idle, and waiting counts. Use those controls and signals according to the installed version. A deadline for obtaining a client is a different concern from a deadline for executing a query after acquisition.

Give the complete operation a deadline that accommodates acquisition, query work, and response delivery. When a request expires, ensure it does not later acquire a client and start abandoned work. Verify the framework and driver's cancellation behavior rather than assuming a closed HTTP connection cancels database execution.

Return a stable public error when admission or acquisition fails. Keep database addresses, query text, and internal exceptions in access-controlled diagnostics where justified. Clients need documented retry behavior, not the pool's raw exception. Coordinate that contract with bounded retry design.

Keep transaction ownership and release paths explicit

A checked-out client needs one clear owner until its work is complete. The node-postgres pooling guide warns that clients must be returned after use, including error paths. A missing release can exhaust a pool even when the database has spare capacity.

For a transaction, retain the same client through its required statements and final commit or rollback. Do not spread transaction statements across independent pool queries that may use different sessions. Keep cleanup structured so an early return, validation failure, or downstream exception cannot bypass it.

Review what happens after a connection becomes unusable. The release path must follow the driver's documented handling for discarding a broken client, and a failed commit may require reconciliation before a business operation is retried. Connection recovery and business idempotency are separate responsibilities.

During shutdown, stop admission before draining work and closing the pool. Closing shared resources while accepted requests still depend on them creates avoidable failures. The shutdown lifecycle guide covers the ordering and deadlines for that transition.

Diagnose saturation before changing the maximum

Observe acquisition delay, waiting requests, checked-out clients, transaction duration, query latency, database utilization, and rejected work together. A waiting queue with few active database sessions suggests a different failure from a fully occupied database doing slow work.

Reproduce saturation in an isolated environment. Include a missing release, a slow query, a transaction waiting on a lock, a deployment overlap, and a database restart. Verify that waiting stays bounded, requests fail predictably, and the process recovers after the fault ends.

Exercise the autoscaling maximum as a capacity scenario even if ordinary traffic rarely reaches it. Check that scaling the application does not consume the operational reserve. Preserve a path to inspect the database when application pools are under pressure.

Accept a pool change when its measured effect matches the intended outcome under the supported topology. Keep the process inventory and budget with deployment configuration so a later worker or replica increase triggers a fresh calculation before it reaches the shared database.

READER SIGNAL

Was this guide useful?

Your rating helps us prioritize clearer, more practical technical content.

AREZGIT / DESKTOP WORKSPACEFROM FIELD NOTE TO RELEASE

Review diffs, run checks, and prepare the release in Arezgit.

Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.

Explore Arezgit