database-connection-management
Skill1 starsChanged 2 months agoMITView on GitHub
Size, configure and debug database connection pools - pool exhaustion, connection storms on restart, prepared-statement caching, pgbouncer and proxy session vs transaction pooling, and the per-request-connection anti-pattern. Use when a user sees "too many connections", pool timeouts, connection refused under load, a deploy that knocks over the database, or is picking pool sizes and timeouts for an ORM or a proxy. Triggers on "connection pool", "too many connections", "max_connections", "pool exhausted", "pgbouncer", "connection storm", "pool_size", "FATAL: sorry, too many clients".
In this repository126 other files it gives its agents.
- eu-data-backend-job-hunterSkill
- job-application-matcherSkill
- llm-agents-and-tool-useSkill
- llm-application-architectureSkill
- llm-cost-and-latency-optimizationSkill
- llm-evaluationSkill
- llm-fine-tuning-and-adaptersSkill
- llm-privacy-and-safetySkill
- llm-prompt-engineeringSkill
- llm-rag-systemsSkill
- structured-output-and-jsonSkill
- graphql-api-designSkill
- pagination-and-filteringSkill
- rest-api-designSkill
- webhooks-and-integrationsSkill
- caching-architecture-multi-layerSkill
- resilience-patternsSkill
- service-boundaries-and-monolithsSkill
- system-design-interviewSkill
- go-concurrencySkill
- go-error-handlingSkill
- java-spring-patternsSkill
- nodejs-server-performanceSkill
- python-async-patternsSkill
- python-data-modelingSkill
- python-packaging-and-toolingSkill
- python-performanceSkill
- rest-api-implementationSkill
- rust-error-handlingSkill
- rust-ownership-patternsSkill
- typescript-type-safetySkill
- ci-caching-and-speedupSkill
- ci-pipeline-designSkill
- deployment-strategiesSkill
- github-actions-hardeningSkill
- release-versioning-and-changelogsSkill
- reproducible-buildsSkill
- aws-iam-and-securitySkill
- aws-serverless-architectureSkill
- azure-fundamentalsSkill
- cloud-cost-optimizationSkill
- cloud-networking-and-vpcSkill
- gcp-cloud-architectureSkill
- code-review-skillsSkill
- design-review-and-refactoring-architectureSkill
- refactoring-techniqueSkill
- understanding-unknown-codebaseSkill
- buildkit-and-cacheSkill
- container-image-debuggingSkill
- container-runtime-securitySkill
- docker-compose-workflowsSkill
- dockerfile-authoringSkill
- docker-image-optimizationSkill
- database-transactionsSkill
- distributed-databasesSkill
- nosql-data-modelingSkill
- postgres-query-tuningSkill
- redis-and-cachingSkill
- schema-migrationsSkill
- data-modeling-for-analyticsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 99
When a process starts it does not open pool_size connections at once — it opens them as demand arrives.
- Very specific: line 136
Prepared plans go stale: when the underlying statistics change.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Database Connection Management
A connection is expensive (memory, a backend process, a file descriptor, a
buffer allocation) and scarce (a hard max_connections limit that will reject
you). Pools are the only way to keep that cost bounded.
The per-request connection anti-pattern
Opening a connection per HTTP request (or per query) is the most common way a
service takes down a database, because the arrival of requests and the
acquisition of connections are unbounded and uncoordinated. Symptoms: the
database looks fine until a traffic spike, then
FATAL: sorry, too many clients already, and recovery requires killing
clients.
- One pool per process, created at startup, sized for the process count.
A pool per request is just as bad as no pool: it churns backend processes,
defeats server-side prepared statements, and exhausts
max_connectionsduring a rolling deploy. - Never hold a connection across a network call to another system. A checkout that spans an HTTP request to a slow third party is a pool capacity bug waiting to happen. Check out, do the work, check in.
- The pool is a queue with a timeout, not a blocking line. A checkout that waits forever converts a slow database into a hung application. Every pool has a checkout/connection timeout, and it is short (a few seconds).
Sizing: the CPU/IO trade-off
Two hard limits, and the pool must fit under both.
- Postgres
max_connections(default 100, often raised to200-500for app servers) andsuperuser_reserved_connections(3) are kept for you by admin and monitoring. A connection is a process with a private memory allocation; 500 idle backends on a small box is RSS you cannot use forshared_buffers. - MySQL
max_connections(default 151) plus themax_connectionsthe internal threads reserve.
Rules of thumb:
- Start at
pool_size = 4–10per process, not 50. If a single query needs more, the query is slow, not the pool small. - Sum across all application instances and the database's own headroom.
instances × workers_per_instance × pool_size + admin + migrations < max_connections × 0.7. Twenty instances × 8 workers × 10 = 1600 — far past a single Postgres's comfortable range. That is the signal to add pgbouncer, not to raisemax_connections. - The pool should be smaller than the number of CPU cores times a small factor for OLTP, because each connection that is actually running a query wants a core; more runnable queries than cores just adds context switching and I/O contention. For batch/analytics, many connections can help hide I/O latency, but the right answer there is usually a separate pool (or a separate database) with a lower, explicit limit.
- A pool larger than the database can serve is not a buffer, it is a queue at the wrong end. Requests then wait on the database instead of in your process, where you could at least time them out and shed load.
Debug the actual cause with the database's view, not the app's:
-- Postgres: what is connected and doing what
SELECT state, count(*), max(now() - state_change) AS oldest
FROM pg_stat_activity GROUP BY state;
SELECT application_name, count(*) FROM pg_stat_activity GROUP BY 1;
SHOW max_connections;High idle connections are a pool that is too large or leaked; high active
with high wait events is a database that is too slow for the workload (tune
the query — see postgres-query-tuning).
Pool exhaustion, by symptom
| Symptom | Likely cause | Fix |
Checkout timeout, QueuePool limit ... overflow ... / connection pool exhausted | All connections busy, pool too small or connections leaked | Find the leak (below); size the pool to the core count; bound the work |
FATAL: sorry, too many clients already | Sum of pools exceeds max_connections | Add pgbouncer, or reduce total pool size |
FATAL: the database system is starting up | The server restarted and old clients are reconnecting | Connection storm — see below |
could not connect to server: ... too many clients intermittently, under deploy | Connection storm on restart | Stagger starts; add pgbouncer; pre-warm slowly |
FATAL: terminating connection due to administrator command | idle_in_transaction_session_timeout or a pg_terminate_backend sweep | Find the long transaction (see database-transactions) |
| Pool grows to its max over time and never shrinks | Leaked connections (checkout without checkin) | Find the code path; the pool is the last line of defence, not a fix |
Leaks are the most common cause of "the pool is too small". The signature
is a pool whose in-use count only goes up. Look for: a connection held across
an await/HTTP call, an exception between checkout and checkin (use
with/try…finally/defer), a generator function that yields while holding
a connection, or a streaming cursor never closed.
Connection storms on restart
When a process starts it does not open pool_size connections at once — it
opens them as demand arrives. After a restart, the entire fleet does this
simultaneously, and the database receives a thundering herd of thousands of
connection requests in a few seconds. Symptoms:
FATAL: the database system is starting up, connection timeouts across all
services, and a database that is briefly unable to serve anything.
- Stagger and rate-limit startup. Warm the pool gradually, with jitter, rather than at full speed. Do not open the pool synchronously in an application health probe.
- Keep connections across deploys where you can — blue/green or a proxy in front (see pgbouncer) means old connections drain while new ones connect.
- Frontend/backend pooling (pgbouncer) is the real fix because the database only ever sees the proxy's small server-side pool, no matter how many clients connect.
- Postgres has a
superuser_reserved_connectionsbacklog precisely so an admin can get in to fix a storm; do not plan on it.
Prepared statement caching
Prepared statements are parsed once and reused, which saves planning time and lets the server cache the plan. The pool and statements interact badly:
- Server-side prepared statements are tied to a connection. A statement prepared on connection A is unknown to connection B; if your pool is larger than 1 and the driver caches the prepared handle, statements leak or get re-prepared per checkout.
- Postgres:
max_prepared_transactionsapplies to two-phase commit, not to prepared statements; ordinary named prepared statements are unlimited but consume memory in the per-backend plan cache.pg_prepared_statementsshows them;DEALLOCATE ALLfrees them. - Drivers: enable client-side reuse (SQLAlchemy
pool_pre_ping, psycopg's statement cache,go-sql-driverwithPreferSimpleProtocol=falsefor implicit prepare, JDBCprepStmtCacheSize/prepStmtCacheSqlLimitfor the server-side cache). Prefer relying on the driver's cache and not on explicitPREPAREin application SQL. - Prepared plans go stale when the underlying statistics change; Postgres
re-plans on execution (generic vs custom plan), so a cached plan is not a
correctness risk, but it can be a performance risk on skewed parameters —
see
references/plan-nodes.mdin thepostgres-query-tuningskill. pool_pre_ping(SQLAlchemy),tcp_keepalives, and a server-sideidle_session_timeouthandle connections the network dropped; a pool with no health check hands a dead connection to a request and you get a one-off error.
Proxy pooling: session vs transaction
A pooler (pgbouncer, ProxySQL, RDS Proxy, Pgpool-II) multiplexes many clients onto few server connections. The mode determines what is legal in SQL.
| Mode | Connection is held for | Constraint | Use when |
session | the whole client session | Full SQL, including SET, temp tables, LISTEN, PREPARE, advisory locks | You need session state; you want full compatibility |
transaction | one transaction | No SET/temp tables/LISTEN between transactions; SET must be inside the transaction or use server_reset_query | The default for web apps with plain SQL |
statement | one statement | Autocommit only, no transactions | Autocommit read-only replicas |
Practical consequences of transaction mode:
- Any code that does
SET timezone = ...then issues queries outside a transaction will lose the setting when the connection is returned. Useserver_reset_query(DISCARD ALL) and set the value via the connection string (?options=-c%20timezone%3DUTC) so it is re-applied on every server connection. LISTEN/NOTIFYand advisory locks used as application locks do not survive transaction pooling. Use a real external lock (Redis, etcd) or a table lock inside a transaction.WITH HOLDcursors and server-sidePREPARE/EXECUTEacross transactions break in transaction mode.- Pooler capacity is a hard limit on the database's real concurrency: size
default_pool_size(server connections per database/user pair) to the database'smax_connectionsbudget, and size client pools to the client count (hundreds is fine, the pooler multiplexes).
MySQL's analogue is ProxySQL in mysql_native vs the transaction-aware
protocols (MySQL 8's CLIENT_COMPRESS, and notably MySQL doesn't have a
true transaction pooler the way pgbouncer does for Postgres — MySQL
Community's mysql-connector and Oracle MySQL Proxy behave differently).
For MySQL, RDS Proxy and ProxySQL are the usual answers; do not assume
transaction pooling semantics identical to pgbouncer's.
Gotchas
- A statement cache and a pooler only coexist from pgbouncer 1.21, and only
with
max_prepared_statementsabove 0 (it defaults to 0 where it was introduced). Below that, protocol-level named statements sent by node-postgres or PgJDBC fail in transaction mode withprepared statement "s1" does not existorprepared statement "s1" already exists, because the name is re-prepared on a different server connection each time. idle_in_transaction_session_timeoutdefaults to 0 — disabled. A leaked transaction keeps its snapshot open, holds the globalxminhorizon back so autovacuum cannot remove dead tuples anywhere in the database, and keeps any row locks it took. Set it per role, and watchpg_stat_activity WHERE state = 'idle in transaction'.- Behind a transaction pooler, a client-side "idle" session is invisible to
the server as idle — the server only sees an open transaction, so
pg_stat_activityshowsactive/idle in transactionfor a client that is doing nothing. Client-side timeouts are the only control you have there. - libpq connects to a standby unless you say otherwise. With several
Arecords behind one hostname it will happily land on a replica and every write fails withcannot execute INSERT in a read-only transaction. Passtarget_session_attrs=read-writein the connection string. - A checkout timeout bounds queueing, not execution. A query that hangs for
ten minutes keeps its connection for ten minutes, and the pool drains with no
error anywhere. Bound execution server-side with
ALTER ROLE app SET statement_timeout = '30s'andlock_timeout; client timeouts cannot substitute. - A fixed-size pool reconnects in lockstep. HikariCP's
minimumIdledefaults tomaximumPoolSize, so an idle pool expires fully and the whole fleet reconnects at once — setminimumIdlelower and giveidleTimeoutsomewhere to shrink to.connectionTimeoutbelow 250ms is rejected outright. pool_pre_pingcosts a round trip per checkout and does not fix "too many clients". The server refusing the connection is a capacity problem; the ping just fails faster. Use keepalives (tcp_keepalives,keepalives_idle) where you only need dead-socket detection.- MySQL closes idle connections long before you expect. Past
wait_timeout(default 28800s) or an intermediate proxy/LB idle timeout you getMySQL server has gone away(2006) orLost connection to MySQL server during query(2013). Setpool_recyclebelow the smallest hop's timeout and keeppool_pre_pingon. - One async connection is not concurrency-safe. Two tasks sharing a
database/sql*sql.DBare fine; two tasks sharing onesql.Txor one asyncpgConnectioninterleave on the same wire and you getInterfaceError: cannot perform operation: another operation is in progress. Transaction per task, connection per transaction. - The pooler's
default_pool_sizeis per database and user pair, and its client side has its own ceiling.default_pool_size(default 20) × pairs is the real server-connection count, and exceedingmax_client_connsurfaces asno more connections allowed (max_client_conn)on the proxy, not on Postgres. - A long transaction in transaction mode consumes the proxy's whole server connection for its duration. Transaction pooling multiplexes only across transactions, so one 30s transaction holds one backend and blocks every client multiplexed onto it.
The per-instance checklist
- One pool per process, created at startup, never per request.
pool_size× instances + workers × ... is under ~70% ofmax_connections.- A short checkout timeout, and the app sheds or retries rather than blocking forever.
- Connections are never held across an external network call.
pool_pre_ping/health check on, so a dropped connection is detected.- Statement caching left to the driver; no hand-rolled
PREPARE. - Startup is staggered and rate-limited; no synchronous full warm-up in a health check.
- If behind a pooler: mode chosen deliberately,
server_reset_queryset, and no session-state assumptions in the application. application_name(or equivalent) is set on every pooled connection, sopg_stat_activitytells you which service is holding what.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

