Memory
Use Linux v7.0+ kernel, and enable zswap to compress memory.
For example, on Ubuntu you can configure /etc/default/grub:
echo 'GRUB_CMDLINE_LINUX_DEFAULT="zswap.enabled=1 zswap.shrinker_enabled=1 zswap.max_pool_percent=30"' | sudo tee /etc/default/grub.d/99-zswap
sudo update-grubPostgreSQL
postgresql.conf parameter tuning
shared_buffersandeffective_cache_sizeshould be tuned according to how much RAM you have. Pay special care when tuningwork_mem. It’s quota for each operation, which could run in parallel in a SQL query.- Keep
max_connectionssmall and use a connection pooler like PgDog. - Increasing
checkpoint_timeoutalong withmax_wal_sizereduces average CPU and I/O usage, and also counterintuitively reduces the total amount of WAL to backup due to FPI (full-page image writes) after each checkpoint.min_wal_sizeshould be set to theestimatefromlog_checkpointslogs plus some overhead, andmax_wal_sizecould be set to 4 times ofmin_wal_size. wal_compressioncan be enabled to save disk space. Note that standbys must be built with support for the compression algorithm used.- Set
statement_timeoutandidle_in_transaction_session_timeoutto ensure that idle sessions do not hold locks for an unreasonable amount of time. An open transaction prevents vacuuming so remaining idle for a long time can contribute to table bloat. - Increase
autovacuumthresholds so that tables aren’t auto analyze/vacuum-ed too frequently. You can keep the defaultautovacuumscale factors if you don’t have very large tables. - Optimize your queries and indexes!