Use it when planning a server or when a database runs out of memory or space.
Database Memory and Disk Sizing
Database sizing plans memory (buffer pools, work memory), CPU, disk capacity and IOPS, and growth for data, indexes, logs and backups.
1. Data size: SELECT pg_size_pretty(pg_database_size('db')); / information_schema sizes.
2. Growth trend.
3. Disk free and IOPS headroom.
4. Memory settings vs RAM.
5. Plan capacity 6–12 months ahead.
workload → memory/CPU/IO needs → growth → capacity plan
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Watch cache hit ratio and disk latency under peak load. Alert at a disk free threshold that leaves time to act.
Step 2 Common failures Narrow the fault
Network storage with high latency makes commits slow even at low load. Indexes often use as much space as the data. Backups and WAL/binlog archives on the same disk fill it unexpectedly.
Measure growth per month. Leave space for maintenance (VACUUM FULL, index rebuilds) and WAL bursts. Per-query memory (work_mem, sort buffers) multiplies with connections.
SELECT pg_size_pretty(pg_database_size('db'));— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
Reads served from memory take microseconds; reads from disk take far longer. Commit latency is bounded by log flush time. Running out of disk stops writes entirely.
ReferenceImportant terms & boundaries3 sections · depth on demand
Sizing guidance
| Resource | Guidance |
|---|---|
| Memory | Hot data + indexes fit in cache (buffer pool, shared_buffers + OS cache) |
| Disk latency | < 1 ms on SSD/NVMe for OLTP; separate log/WAL disk on busy systems |
| Disk space | Data + indexes + logs + temp + backups + 30 % headroom |
| CPU | Cores for concurrent queries; parallel query for analytics |
PostgreSQL: shared_buffers ~25 % of RAM, effective_cache_size ~50–75 %. Monitor growth monthly.
In depth
Memory model
| Engine | Shared cache | Per operation / per connection |
|---|---|---|
| PostgreSQL | shared_buffers (often ~25 % of RAM) + the OS page cache for the rest | work_mem per sort/hash node, maintenance_work_mem for VACUUM/CREATE INDEX |
| MySQL InnoDB | innodb_buffer_pool_size (50–75 % of RAM) | sort_buffer_size, join_buffer_size, temp tables per connection |
| SQL Server | max server memory (leave several GB for the OS) | Query memory grants from the same pool |
work_mem arithmetic
work_mem is allowed per sort or hash operation, per parallel worker. A query with four hash joins and a sort, run by 50 connections, can use up to 50 × 5 × work_mem. Setting work_mem = 256MB globally can therefore push the server into swap or the OOM killer under load. Keep the global value modest (for example 16–64 MB) and raise it per session or per role for reports:
ALTER ROLE reporting SET work_mem = '256MB';
Signs it is too small: Sort Method: external merge Disk: … and temp file growth in pg_stat_database.temp_bytes.
Disk
| Measure | Why |
|---|---|
| Write latency of the log device (fsync) | Every commit waits for it |
| Random read IOPS for data | Cache misses |
| Free space with headroom | Space for growth, temp files, index rebuilds, WAL/binlog spikes, local backups |
Separate the transaction log from data files only if they really land on different physical devices; on shared cloud volumes it mostly helps with accounting and with keeping a full data disk from stopping log writes.
Growth planning
Track database size per week (pg_database_size, information_schema.TABLES), extrapolate, and alert on days-until-full rather than a fixed percentage.
Common mistakes
- Sizing memory without considering OS cache.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.