database-connection-pooling · git:20261120.76a1b83 · 2026-11-20 · sha256 0b163b803ba6e340

database-connection-pooling git:20261120.76a1b83A

Immutable. This exact content never changes and is served at /api/v1/blob/0b163b803ba6e340.

No tool to install first
claude plugin marketplace add https://markdownregistry.com/mp/a/art_ci3vvgldgzajpqu2/0b163b803ba6e340fb51d9d931c43fc423593cc17a4726cc05e77ae84c94007f.json

Then /plugin install database-connection-pooling@markdownregistry-ci3vvgldgzajpqu2 in Claude Code. It installs this exact version, the one graded A here, pinned to the commit it was graded at, and leaves it there.

---
name: database-connection-pooling
description: Sizes and configures database connection pools, and diagnoses exhausted pools, leaked connections and idle-in-transaction sessions.
---

# Database connection pooling

A pool protects the database. Bigger is rarely better.

## Steps

1. Collect facts: database max connections, number of app instances and workers, pool settings per process, and typical query time. Total possible connections = instances x workers x pool max. Compare to the database limit with headroom for admin and migrations.
2. Size it. A database does best with roughly (cores x 2) + disk spindles active connections. For most apps a pool of 10 to 20 per instance is plenty; if you need more, queries are too slow or transactions too long.
3. Set the pool knobs deliberately: max size, min idle, acquire timeout (fail in a few seconds, not minutes), max connection lifetime (shorter than any proxy or firewall idle timeout), and validation or keepalive.
4. Diagnose exhaustion: log pool wait time and active count. Look at the database view (`pg_stat_activity` or `SHOW PROCESSLIST`) for state, `xact_start` age and the query of sessions that are idle in transaction.
5. Find leaks: connections acquired and not released on error paths. Use the language's scoped form (context manager, try-with-resources, `using`, `defer`) everywhere.
6. Shorten transactions: no HTTP calls, file work or user waits between BEGIN and COMMIT. Set `idle_in_transaction_session_timeout` and a statement timeout.
7. For many instances or serverless, put an external pooler (PgBouncer in transaction mode, or a managed proxy) in front, and check that session features such as prepared statements and advisory locks still work.

## Rules

- Never fix exhaustion by only raising max size.
- Report the arithmetic, not just the numbers.

## Report

The current and proposed settings, the evidence for the cause, and the code fixes.