MySQL and PostgreSQL in Production: A Basic Tuning Guide
Whether you choose MySQL or PostgreSQL, the core of production deployment is the same: make the default configuration match your hardware and workload. Both ship with "compatibility-first" rather than "performance-first" defaults, and memory parameters in particular tend to be conservative, so tuning after deployment is a must.
1. Connections and Concurrency
PostgreSQL: max_connections defaults to 100. The official wiki notes that PostgreSQL on good hardware can support a few hundred connections, but for thousands you should introduce a connection pooler (such as PgBouncer) to reduce overhead.
# postgresql.conf
max_connections = 200
MySQL: max_connections defaults to 151. Each MySQL connection carries memory overhead; pair it with thread_cache_size to reuse threads:
# my.cnf
max_connections = 300
thread_cache_size = 64
Bigger is not better. Each MySQL connection occupies several MB of RAM and each PostgreSQL backend process eats memory too, so blindly raising max_connections to thousands can exhaust the machine before the workload ever does. The common pattern is for apps to connect to a pool that reuses a small number of real connections. On the PostgreSQL side the standard answer is PgBouncer; with pool_mode set to transaction, 50 to 200 backend connections can carry thousands of application connections:
# pgbouncer.ini
[databases]
appdb = host=127.0.0.1 port=5432 dbname=appdb
[pgbouncer]
pool_mode = transaction
max_client_conn = 2000
default_pool_size = 50
2. Memory Tuning
PostgreSQL memory parameters (classic guidance from the official wiki):
| Parameter | Suggested starting value | Notes |
|---|---|---|
shared_buffers |
25% of physical memory | Shared buffer cache; over 40% rarely helps |
effective_cache_size |
50% to 75% of physical memory | Estimate for the planner only, not a real allocation |
work_mem |
4MB to 64MB | Memory for sorts/hashes; scales with concurrent connections |
maintenance_work_mem |
64MB to 1GB | Used by VACUUM, index builds and other maintenance |
# Example for a dedicated database server with 16GB RAM
shared_buffers = 4GB
effective_cache_size = 12GB
work_mem = 64MB
maintenance_work_mem = 1GB
work_mem is allocated per operation: 50MB x 30 concurrent users quickly consumes 1.5GB, so multiply by concurrency when sizing it.
MySQL InnoDB memory parameters:
# Example for 16GB RAM
innodb_buffer_pool_size = 8G # ~50% to 75% of physical memory
innodb_log_file_size = 1G
innodb_buffer_pool_instances = 8
innodb_buffer_pool_size is the most critical parameter; it caches table and index data. innodb_log_file_size controls redo log size, and if too small, write-heavy workloads suffer frequent checkpoints. On MySQL 8.0.30+ you can manage redo logs centrally with innodb_redo_log_capacity.
3. WAL and Durability
PostgreSQL's synchronous_commit (default on) guarantees every commit is flushed to disk and is the foundation of ACID; do not disable it in production. wal_buffers follows shared_buffers automatically and rarely needs manual adjustment.
MySQL's innodb_flush_log_at_trx_commit:
1(default): flush on every commit, safest2: flush once per second, better performance but may lose up to 1 second of data on crash0: determined by the system, fastest but riskiest
For almost all production scenarios, keep 1 and combine it with sync_binlog=1 for replication safety.
4. Automatic Maintenance
Never turn off autovacuum in PostgreSQL. The official wiki is explicit: "the answer to almost all vacuuming problems is to vacuum more often, not less". It removes dead rows and prevents table bloat:
autovacuum = on
autovacuum_max_workers = 3
MySQL redo log and purge threads: keeping the defaults is usually fine; the priority is monitoring status values such as Innodb_buffer_pool_wait_free to spot bottlenecks.
5. Slow Query Logs
Both databases support slow query logs, the first step in locating performance problems.
PostgreSQL:
log_min_duration_statement = 1000 # log queries taking over 1 second
log_line_prefix = '%t:%r:%u@%d:[%p]: '
MySQL:
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /var/log/mysql/slow.log
The slow query log is only the first step; once you have entries, learn to read execution plans. PostgreSQL uses EXPLAIN ANALYZE; MySQL's EXPLAIN does not actually execute by default, so use EXPLAIN ANALYZE (8.0.18+) when you want real costs. When reading a plan, watch three things: full table scans (seq scan / type=ALL), extra sorting (Sort / filesort), and indexes that should be used but are not (estimated rows far above actual matches). Most slow queries disappear with one well-chosen index.
6. Deployment Shapes and Operations
- Containerized: databases can also run in Docker Compose, with the data directory persisted via named volumes; see also the Compose LAMP environment.
- Backups: essential in production; see the backup and restore runbook and cloud server backup strategy.
- Monitoring: track connections, slow queries and buffer-pool hit ratios with Prometheus and Grafana, and route alerts into monitoring and alerting.
- LNMP integration: for PHP sites, see the LNMP stack setup.
A Real Tuning Story
An article-heavy site hit 100% database CPU at the evening peak, with pages taking 3 seconds to render. The first move was enabling the slow query log; the next day it surfaced an aggregation query counting articles by month that ran for tens of seconds. Reading the plan confirmed it was skipping the published_at index; after adding one, the query dropped from 8 seconds to 0.3. Then shared_buffers went from the default 128MB to 4GB, the hit ratio climbed, and CPU usage fell by half. Three parameters changed in total, each validated with a load-test script — database tuning is often just "logs + index + memory".
16IDC Note
The biggest tuning payoff comes up front: matching memory parameters to hardware, enabling slow query logs and keeping automatic maintenance on solves most performance problems without diving into indexes and query plans. The key principle is to change one parameter at a time, validate with real load, and record every change, rather than blindly copying online configs. For small and mid-sized projects, getting these basics right matters more than chasing advanced features.
Reference: PostgreSQL tuning wiki https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server ; PG resource parameters https://www.postgresql.org/docs/current/runtime-config-resource.html ; MySQL InnoDB parameters https://dev.mysql.com/doc/refman/8.0/en/innodb-parameters.html ; PgBouncer config https://www.pgbouncer.org/config.html
Source: https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server , https://www.postgresql.org/docs/current/runtime-config-resource.html , https://dev.mysql.com/doc/refman/8.0/en/innodb-parameters.html
Operation & Verification Steps
Example 1: key parameters in postgresql.conf
# postgresql.conf — a 16 GB dedicated database server
# Change one item at a time, validate with real load, then move on
listen_addresses = '127.0.0.1' # listen on localhost or the private network only
max_connections = 200 # put a connection pooler in front as well
shared_buffers = 4GB # about 25% of physical memory
effective_cache_size = 12GB # planner estimate only, nothing is allocated
work_mem = 16MB # multiply by concurrent sorts when sizing
maintenance_work_mem = 1GB # VACUUM and index builds
wal_buffers = 16MB # keep it proportional to shared_buffers
synchronous_commit = on # do not disable it for order or payment data
log_min_duration_statement = 1000 # log statements slower than one second
log_line_prefix = '%t:%r:%u@%d:[%p]: '
autovacuum = on # never turn this off
autovacuum_max_workers = 3
Note: memory settings must be derived from the machine's real RAM, and work_mem has to be multiplied by concurrent operations — otherwise sorts and hashes at peak hours consume far more memory than expected.
Example 2: locating a slow query with EXPLAIN (ANALYZE, BUFFERS)
-- Read the plan and the real timings first, then decide on an index
EXPLAIN (ANALYZE, BUFFERS, VERBOSE)
SELECT a.id, a.title, count(c.id) AS comments
FROM articles a
LEFT JOIN comments c ON c.article_id = a.id
WHERE a.published_at >= now() - interval '30 days'
AND a.status = 'published'
GROUP BY a.id, a.title
ORDER BY comments DESC
LIMIT 20;
Note: watch three things in the output — whether Seq Scan appears on a large table, whether Sort Method spilled to disk, and whether Buffers: shared read dwarfs hit; lots of reads with few hits usually means a missing index or undersized shared_buffers.
Limitations
- Figures reflect public information at publication; terms, prices and quotas follow the provider's current published terms.
- This article reflects public information as of 2026-08-06; terms, prices and quotas follow the provider's current published terms.
- Third-party capability and limit statements follow their official documentation; no further commitment is implied here.
FAQ
With 16GB of RAM, how large should shared_buffers be?
Start at 25%, so 4GB. The PostgreSQL wiki notes that going above 40% usually buys nothing, so pair it with effective_cache_size at 50-75% of RAM, 12GB in the worked example, and start work_mem at 4MB-64MB, multiplied by concurrent operations to estimate the total. Change one parameter at a time and verify with real load before the next.
A query takes 3 seconds: how do you troubleshoot it?
Turn on the slow query log, then read the plan. Set log_min_duration_statement to 1000 to capture statements over a second, and use EXPLAIN (ANALYZE, BUFFERS) to check three things: Seq Scan on a large table, a Sort spilling to disk, and shared reads far above hits. In the case study an index took one query from 8 seconds to 0.3.
Is it better to turn synchronous_commit off for orders?
No. With it off a crash can lose recently committed transactions, which order and payment data cannot accept, so keep the default on; read-heavy smaller sites may trade it for write latency after confirming they tolerate losing up to one second. On MySQL the matching setting is innodb_flush_log_at_trx_commit staying at 1.
Can a connection pool replace raising max_connections?
Yes, and it is the better move. Every PostgreSQL backend consumes memory, so pushing max_connections into the thousands exhausts the machine through connections alone; PgBouncer with pool_mode = transaction and default_pool_size = 50 lets 50-200 backends serve thousands of application connections. Investigate connection leaks before adding a pool.
Related Providers
- AWS EC2: use it to spin up an instance you can resize at will while baselining shared_buffers and innodb_buffer_pool_size against the memory size.
- Grafana: after changing connection and cache settings, chart connections, hit ratio and slow queries there instead of reading a single top snapshot.
- pgAdmin: for PostgreSQL execution plans and backup-restore checks, connect through it and verify beyond the shell.
Community & Resources
-
Environment Deployment: other coverage in this category.
Official docs and communities -
PostgreSQL: Resource Consumption — Official definitions and defaults for
shared_buffers,work_mem, and friends. -
PostgreSQL: EXPLAIN — Authoritative reference for every field printed by
EXPLAIN (ANALYZE, BUFFERS). -
MySQL 8.4 InnoDB Parameters — Reference for
innodb_buffer_pool_sizeand redo log settings. -
PgBouncer Configuration — Official documentation for
pool_modeanddefault_pool_size.
Learning resources and tools
- MariaDB Knowledge Base — A MySQL-compatible documentation base that makes parameter cross-checking easy.
- Stack Overflow: postgresql — Real-world troubleshooting on connections and execution plans.
- Stack Overflow: mysql — Frequent Q&A on InnoDB tuning and slow query analysis.
Original source: PostgreSQL Wiki ↗