Sub topic 3
Connection limits and connection pooling
This chapter assumes you have a running PostgreSQL or MySQL server, can use sudo, can edit config files, and can run basic SQL.
Connection limits
Each client connection uses server memory and a server process or thread. When the connection count reaches the configured limit, the server refuses new clients.
Check the current limit on PostgreSQL:
sudo -u postgres psql -c 'SHOW max_connections;'Check it on MySQL:
mysql -e "SHOW VARIABLES LIKE 'max_connections';"Set the limit in postgresql.conf (max_connections = 100) or in the [mysqld] section of the MySQL config (max_connections = 151). PostgreSQL needs a restart after this change.
Connection pooling
A connection pool keeps a small set of server connections open and lends them to many clients. PgBouncer is a common pooler for PostgreSQL. ProxySQL is a common pooler for MySQL. Applications connect to the pooler's port instead of the database port.
Logging and monitoring
Log slow queries to find performance problems. On PostgreSQL, set log_min_duration_statement = 500 to log statements that take longer than 500 ms. On MySQL, set slow_query_log = 1 and long_query_time = 0.5.
See live connections:
-- PostgreSQL
SELECT pid, usename, state, query FROM pg_stat_activity;
-- MySQL
SHOW PROCESSLIST;Backing up the database
A logical backup stores the data as SQL or a custom archive. Create one with each engine's standard tool:
# PostgreSQL
pg_dump -Fc -f appdb.dump appdb
# MySQL
mysqldump --single-transaction --routines --events appdb > appdb.sqlTesting restores
A backup is useful only if it restores. Load it into a scratch database and check the row counts:
createdb appdb_restore_test
pg_restore --dbname=appdb_restore_test appdb.dumpRecord how long the restore takes. That time is your recovery time objective (RTO), the longest outage you can accept. The age of the newest backup is your recovery point objective (RPO), the most data you can lose.
Replication and read replicas
Replication sends changes from a primary server to one or more replicas. PostgreSQL streaming replication ships write-ahead log records to a standby. MySQL replication ships binary log events. A replica can serve reads or take over during a failure.
A replica is not a backup. A mistaken DELETE replicates to every copy within seconds.
Maintenance and upgrades
Take a backup and test a restore before any upgrade. Apply minor updates with the package manager:
sudo apt update && sudo apt upgrade postgresqlMajor version upgrades usually need a dump and restore, or a tool such as pg_upgrade. Test the upgrade on a copy first.
Troubleshooting common failures
Start with the service state, the recent logs, and the listening ports:
sudo systemctl status postgresql
sudo journalctl -u postgresql --since '15 minutes ago'
ss -ltnp | grep -E ':(5432|3306)'"FATAL: sorry, too many clients already" (PostgreSQL) and "ERROR 1040: Too many connections" (MySQL) both mean the connection limit is reached. Add a pooler or find the clients that hold connections open.
Exercise
A PostgreSQL server returns "sorry, too many clients already." Name two changes you could make, and state which one does not add capacity.
Show answer
Add a connection pooler such as PgBouncer, or raise max_connections after checking host memory. Raising the limit alone does not add capacity, because each connection uses memory.