/database-operations-skills:database-connection-pooling-strategiesfrom database-operations-skills
Guides connection pool sizing, pooling mode selection (transaction/session/statement), and tool setup (PgBouncer, ProxySQL) for PostgreSQL and MySQL/MariaDB. Diagnoses prepared statement and session-variable issues under pooling.
Ships in database-operations-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Every relational database has a real, finite cost per open connection — memory, a backend process/thread, and context-switch overhead — which means "just let every application instance open its own connections" stops scaling long before the database's actual query-processing capacity is exhausted. A connection pooler sits between application and database specifically to decouple the number of client-facing connections (which can be very large — thousands of application threads) from the number of backend connections the database actually has to serve (which should stay well under the database's practical capacity). This skill covers the pooling-mode decision that generalizes across engines (transaction vs. session vs. statement pooling), and the two dominant tools that implement it for the two most common relational engines — PgBouncer for PostgreSQL and ProxySQL for MySQL/ MariaDB — plus the failover-aware routing patterns that make a pooler also useful as an application-transparent HA layer, not just a connection multiplexer.
SET variables, or
advisory locks behaving unexpectedly under a pooled connection.max_connections ceiling before performance degrades);
MySQL/MariaDB use a thread per connection (generally cheaper per
connection, higher realistic ceiling, but still finite).pg_stat_activity for PostgreSQL,
SHOW STATUS LIKE 'Threads_connected' for MySQL/MariaDB) to size
against real demand rather than a guess.SET variables, prepared statements,
advisory locks, temp tables) behaves exactly as it would without a
pooler — but gives the least connection-multiplexing benefit, since
one idle client still holds one backend connection.SET, LISTEN/NOTIFY, advisory locks held across
transactions, and (depending on client library) server-side prepared
statements.Pick per workload, not fleet-wide by default: a typical stateless web application backend is the canonical transaction-pooling case; a migration runner or a session relying on temp tables/advisory locks needs session pooling even if the rest of the fleet uses transaction mode.
# pgbouncer.ini
[databases]
appdb = host=<PRIMARY_HOST> port=5432 dbname=appdb
[pgbouncer]
listen_port = 6432
pool_mode = transaction
max_client_conn = 2000
default_pool_size = 25
reserve_pool_size = 5
reserve_pool_timeout = 3
default_pool_size — the actual backend connections PgBouncer holds
open per database/user pair — is what determines load on PostgreSQL's
max_connections, not max_client_conn. Size it against
(core_count * 2) + effective_spindle_count as a starting heuristic for
CPU-bound OLTP workloads, well under max_connections, then validate
against measured PostgreSQL-side connection counts and query latency
rather than trusting the heuristic blindly. reserve_pool_size gives a
small burst allowance above default_pool_size for transient spikes,
activated only after reserve_pool_timeout of queueing — a safety
valve, not a substitute for correct baseline sizing.
-- ProxySQL admin interface (port 6032) — configure backend servers
INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (10, '<PRIMARY_HOST>', 3306);
INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (20, '<REPLICA_HOST>', 3306);
-- Query rule: route SELECTs to the replica hostgroup, everything else to primary
INSERT INTO mysql_query_rules (rule_id, active, match_pattern, destination_hostgroup, apply)
VALUES (100, 1, '^SELECT.*FOR UPDATE$', 10, 1),
(200, 1, '^SELECT', 20, 1);
LOAD MYSQL SERVERS TO RUNTIME;
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
SAVE MYSQL QUERY RULES TO DISK;
ProxySQL is meaningfully more than a connection pool — it's a
query-aware proxy that can route based on query pattern (the SELECT ... FOR UPDATE rule above deliberately routes locking reads to the primary
hostgroup even though it matches ^SELECT, since a replica's data may
be stale for a read that's about to inform a write). Configure
per-hostgroup connection pool sizing similarly to PgBouncer's
default_pool_size:
UPDATE mysql_servers SET max_connections = 50 WHERE hostgroup_id = 10;
LOAD MYSQL SERVERS TO RUNTIME;
Every configuration change must be explicitly LOADed to runtime and
SAVEd to disk — a change left only in the in-memory admin tables
without both steps is lost on ProxySQL restart, a common source of
"the config change didn't survive a restart" confusion.
Both poolers can track which backend is currently the writable primary and update routing without the application knowing a failover happened:
# PgBouncer: paired with a healthcheck/failover manager (e.g. Patroni)
# that rewrites the [databases] host entry via a reload, or via a
# virtual IP/DNS layer PgBouncer connects through instead of a static host
-- ProxySQL: mysql_replication_hostgroups lets ProxySQL detect the
-- current primary automatically via read_only status, not a static config entry
INSERT INTO mysql_replication_hostgroups (writer_hostgroup, reader_hostgroup, check_type)
VALUES (10, 20, 'read_only');
LOAD MYSQL SERVERS TO RUNTIME;
With mysql_replication_hostgroups configured, ProxySQL periodically
checks each backend's read_only variable and automatically moves a
newly-promoted primary into the writer hostgroup — this is what makes a
MySQL/MariaDB failover (whether via Orchestrator, Galera's own
membership, or manual promotion) transparent to the application, which
only ever talks to ProxySQL's stable endpoint.
-- PgBouncer admin console
SHOW POOLS;
SHOW STATS;
-- ProxySQL stats schema
SELECT * FROM stats_mysql_connection_pool;
Watch for a pool consistently at its default_pool_size/
max_connections ceiling with client connections queueing
(cl_waiting in PgBouncer's SHOW POOLS, or growing queue depth in
ProxySQL's connection pool stats) — this is the actual signal that a
pool needs resizing, not application-reported "connection timeout"
errors alone, which can also be caused by a genuinely overloaded
database backend that more pooled connections would only make worse.
LOAD ... TO RUNTIME with SAVE ... TO DISK for every configuration change — a change that only reaches
runtime is lost on the next restart.Symptom: Under transaction pooling, prepared statements
intermittently fail, or SET search_path/SET NAMES appears to
randomly stop taking effect.
Fix: In transaction pooling mode, a client's backend connection
can change between transactions, so anything scoped to a session
(server-side prepared statements via some drivers, a SET issued
outside a transaction, advisory locks held across transactions) is
unsafe. Either switch that specific workload's pool to session mode,
or configure the client driver to avoid server-side prepared
statements against a transaction-pooled connection (many modern
drivers support a client-side/"simple query" prepared-statement mode
specifically for this).
Symptom: ProxySQL's query routing rules appear correct in the
admin tables but have no effect on actual traffic.
Fix: The rules were inserted into the admin schema but never
promoted to runtime. Always run LOAD MYSQL QUERY RULES TO RUNTIME;
(and the equivalent for servers/variables) after any change, and
SAVE ... TO DISK so the change survives a ProxySQL restart.
Symptom: Application connection errors ("too many clients
already" or ProxySQL connection timeouts) occur even though the pool
size looks generously configured.
Fix: Check SHOW POOLS/stats_mysql_connection_pool for actual
queueing before assuming the pool is undersized — a pool that's
correctly sized can still queue if the database itself is the
bottleneck (slow queries holding connections open longer than
expected), in which case adding more pooled connections just pushes
more concurrent load onto an already-overloaded backend rather than
fixing the underlying slowness.
Symptom: After a database failover, the application keeps sending
writes to the old (now-demoted) primary for a period, causing errors
or, worse, split-brain-style writes to a node that should be
read-only.
Fix: The pooler's routing configuration wasn't wired to detect
the failover automatically (a static [databases] host entry in
PgBouncer with no reload triggered by the failover manager, or
ProxySQL without mysql_replication_hostgroups configured to detect
read_only status changes). Wire the pooler's routing to the actual
HA mechanism's failover signal (a Patroni/etcd-triggered PgBouncer
reload, or ProxySQL's automatic replication-hostgroup detection)
rather than a static configuration that requires manual updating.
Symptom: Someone reconfigures a production pooler's pool size or routing rules directly via the admin interface during an incident, without saving to disk, "just to test something quickly." Fix: This is a risky, easy-to-forget action — the change works until the next restart, then silently reverts, which can reintroduce the original problem at an unpredictable time and confuse whoever is debugging it later.
Warning — avoid untracked production config drift. Treat pooler configuration (pool sizes, routing rules, hostgroup membership) as infrastructure-as-code reviewed the same way as database configuration itself — see postgresql-configuration-validation and mysql-mariadb-configuration-validation — rather than ad hoc admin-console changes made directly against production without a corresponding tracked, reviewed change.
Scenario: A checkout service on MySQL/MariaDB with a primary and
two read replicas currently has every application instance connecting
directly to the primary for all reads and writes, and is hitting
max_connections during traffic spikes. The team introduces ProxySQL
for pooling and read/write splitting.
INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (10, '<PRIMARY_HOST>', 3306);
INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (20, '<REPLICA1_HOST>', 3306);
INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (20, '<REPLICA2_HOST>', 3306);
INSERT INTO mysql_replication_hostgroups (writer_hostgroup, reader_hostgroup, check_type)
VALUES (10, 20, 'read_only');
LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK;
SELECTs to the reader
hostgroup, everything else (writes, SELECT ... FOR UPDATE) to the
writer hostgroup, LOADed and SAVEd.max_connections = 60 for the writer hostgroup (well under
the primary's max_connections = 300, leaving headroom for
replication threads and monitoring), max_connections = 40 per
replica for the reader hostgroup.read_only flips to OFF) and verify ProxySQL's automatic
replication-hostgroup detection moves it into the writer hostgroup
within the configured check interval, with zero application
connection-string changes required.stats_mysql_connection_pool for queueing over the following
week's peak traffic; connection-exhaustion errors on the primary
stop recurring, and read load is now measurably distributed across
both replicas instead of concentrated on the primary.mysql_replication_hostgroups automatic routing tracks.max_connections/pooler-sizing math this skill's pool configurations depend on before rollout.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
connection-pooling, pgbouncer, proxysql, database-proxy, pool-sizing
npx claudepluginhub selvarajmurugesan90/ops-engineering-skills --plugin database-operations-skills23 skills in this plugin
/arangodb-multi-model-database-operationsCovers operating ArangoDB, a multi-model database combining document, graph, and key-value collections in a single engine with a unified query language (AQL): collection type selection, graph traversal via AQL, and cluster configuration (coordinators, DB-servers, agency). Use when the user asks to "write an AQL query," "should I use a document or edge collection in ArangoDB," "set up an ArangoDB cluster," "why is this AQL graph traversal slow," or "choose between ArangoDB and a single-purpose database for a mixed graph/document workload."
/cassandra-wide-column-database-operationsCovers Apache Cassandra ring topology and token/partition design, tunable consistency levels (ONE, QUORUM, ALL, LOCAL_QUORUM), compaction strategy selection (SizeTiered, Leveled, TimeWindow), and tombstone/GC-grace management. Use when the user asks to "design a Cassandra partition key," "choose a Cassandra consistency level," "why is this Cassandra query slow," "pick a compaction strategy," "Cassandra tombstone warnings in logs," or "add/remove a node from a Cassandra ring."
/clickhouse-analytical-database-operationsCovers operating ClickHouse, the leading open-source OLAP columnar database: MergeTree engine family selection (MergeTree, ReplacingMergeTree, AggregatingMergeTree, SummingMergeTree), sharding and replication topology via ReplicatedMergeTree and Distributed tables, and materialized views for streaming aggregation. Use when the user asks to "set up a ClickHouse cluster," "pick a MergeTree engine," "why are my ClickHouse queries slow," "shard and replicate ClickHouse tables," "deduplicate rows in ClickHouse," or "build a ClickHouse materialized view for rollups."
/cloud-data-warehouse-operations-snowflake-bigquery-redshiftComparative operations guide for the three dominant managed cloud data warehouses: Snowflake virtual warehouse sizing and multi-cluster scaling, BigQuery slot allocation and on-demand vs. capacity pricing, and Redshift cluster/RA3 node sizing and workload management (WLM) — plus cost-control and query-performance practices common to all three. Use when the user asks to "size a Snowflake warehouse," "control BigQuery query costs," "tune Redshift WLM queues," "why is this warehouse query slow," "reduce our data warehouse bill," or "choose between Snowflake, BigQuery, and Redshift."
/database-backup-and-restore-strategiesCross-database backup tooling and restore-testing discipline: pg_dump/pg_basebackup for PostgreSQL, mysqldump/Percona XtraBackup for MySQL/MariaDB, and mongodump/mongorestore for MongoDB — logical vs. physical backup trade-offs, point-in-time recovery, and why an untested restore is not a real backup. Use when the user asks to "set up database backups," "restore from a pg_basebackup," "why did our mysqldump restore fail," "test our backup/restore process," "set up point-in-time recovery," or "design a backup retention policy."
/database-schema-migration-with-liquibase-and-flywayCovers version-controlled schema migration tooling (Liquibase changesets/changelogs, Flyway versioned/repeatable migrations), forward-only vs. reversible migration design, safe patterns for backward-compatible rolling deploys, and testing migrations in CI before they reach production. Use when the user asks to "set up Flyway/Liquibase for this project," "write a rollback for this migration," "why did this migration fail halfway through," "how do I test a schema migration in CI," or "design a zero-downtime column rename/backfill."
Other plugins' skills for the same job