Sub topic 11
Tuning Memory and Storage
Prerequisites and the working model
This chapter assumes that you can use Linux commands, edit service configuration, and understand basic SQL. It also assumes that you know the difference between system memory, disk storage, and a database connection.
A database uses memory to cache frequently read data, sort query results, and manage connections. PostgreSQL uses shared buffers plus memory for each connection and operation. MySQL commonly uses the InnoDB buffer pool as its main data and index cache. More cache can reduce disk reads, but reserving too much memory can make Linux swap or terminate processes.
Storage affects both capacity and speed. Database files, transaction logs, temporary files, and backups may all use different amounts of space. Measure before changing settings.
Inspect the host before tuning
Use these commands to establish the current state:
free -h
swapon --show
df -hT
df -ih
lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINTThe free command shows available memory and swap. The df command shows used filesystem space and inode usage. A filesystem can have free bytes but no free inodes, which also prevents new files from being created.
Check the database’s data directory before placing it on another disk. PostgreSQL commonly uses a directory under /var/lib/postgresql; MySQL commonly uses /var/lib/mysql. Confirm the real path from the active configuration and package version rather than assuming it.
Set cache and connection-related memory
For PostgreSQL, shared_buffers is the main shared cache. A common starting point for a dedicated database host is about 25% of RAM, followed by measurement. PostgreSQL also allocates memory per operation through settings such as work_mem, so multiplying that value by every connection can produce unexpected usage.
For MySQL, innodb_buffer_pool_size is the main cache for InnoDB data and indexes. On a dedicated host, it often receives most available memory, while leaving room for the operating system, connections, temporary tables, and monitoring tools.
# PostgreSQL configuration
shared_buffers = 2GB
work_mem = 16MB
maintenance_work_mem = 256MB# MySQL configuration
[mysqld]
innodb_buffer_pool_size = 2G
tmp_table_size = 64M
max_heap_table_size = 64MChange one setting at a time, restart or reload as required by that setting, and watch memory pressure, query latency, and swap activity.
Plan storage and verify the result
Keep database data and transaction logs on reliable storage with enough free space for temporary growth. Fast storage improves random reads and write latency, but it does not replace indexes or efficient queries. Monitor filesystem usage and database growth before the disk reaches capacity.
A practical change record includes the old value, new value, reason, timestamp, and observed result. Revert a change if memory pressure or query performance worsens.
| Symptom | Likely area to inspect |
|---|---|
| Swapping under load | Total memory, per-connection settings, and cache size |
| Slow random reads | Storage latency, cache hit rate, and indexes |
| Disk fills unexpectedly | Database growth, logs, temporary files, and backups |
Exercise
A host has 8 GB of RAM and starts swapping after increasing the PostgreSQL cache. What should you check first?
Show answer
Check total memory usage, per-connection settings such as work_mem, concurrent connections, and other processes. Reduce the cache or per-operation settings before adding more memory.