Bottom line: shared_buffers, work_mem, effective_cache_size and max_connections decide whether an 8 GB box runs well or swaps. Four settings, set once from RAM. The hundred-parameter tuning guides mostly do not matter before these.

The four

Setting Value Why
shared_buffers 2 GB (25%) larger starves the OS page cache
effective_cache_size 5.5 GB (~70%) planner hint only, no allocation
work_mem 16 MB start per sort/hash node, not per connection
max_connections 40 each holds work_mem in the worst case
1
2
3
4
5
6
7
8
shared_buffers = 2GB
effective_cache_size = 5500MB
work_mem = 16MB
max_connections = 40
maintenance_work_mem = 512MB
random_page_cost = 1.1
effective_io_concurrency = 200
wal_compression = on

random_page_cost = 1.1 tells the planner SSDs are not seek-bound; leaving the default 4.0 makes it refuse index scans on fast storage.

work_mem is per node, not per connection

A query with three sorts allocates three times. Forty connections with a 64 MB work_mem can demand 7.6 GB and OOM. Start at 16 MB, raise for specific reports with SET LOCAL work_mem rather than globally.

Connections

Every connection is a process with its own memory. Beyond roughly 40 on 8 GB, throughput falls. Put pgbouncer in front in transaction mode and cap the pool at 20.

Monitoring with the right signals

1
2
SELECT sum(blks_read)/sum(blks_hit+blks_read) AS miss_ratio FROM pg_stat_database;
SELECT query, calls, mean_exec_time FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 10;

A cache miss ratio above 2% or a work_mem spill count rising in the log means the settings are wrong, not the query.

What to change first when it is slow

  1. The index — an EXPLAIN (ANALYZE, BUFFERS) showing a seq scan on a filtered column
  2. work_mem for that query only
  3. shared_buffers up if the miss ratio rises with load
  4. Connection pooling if pg_stat_activity shows idle-in-transaction

Autovacuum is the other one: defaults are too lazy for a busy small box. autovacuum_vacuum_scale_factor = 0.05 and autovacuum_naptime = 30s.

Operating notes

Four settings decide an 8 GB box: shared_buffers = 2GB, effective_cache_size = 5500MB, work_mem = 16MB, max_connections = 40. Then random_page_cost = 1.1 so the planner trusts index scans on SSDs.

1
2
3
4
5
shared_buffers = 2GB
effective_cache_size = 5500MB
work_mem = 16MB
max_connections = 40
random_page_cost = 1.1

work_mem is per node

Three sorts in one query allocate three times. Forty connections at 64 MB can demand 7.6 GB. Start at 16 MB and raise per report with SET LOCAL work_mem.

Connections

Beyond about 40 on 8 GB throughput falls. pgbouncer in transaction mode with a pool of 20 in front of it.

What to change first when slow

The index, via EXPLAIN (ANALYZE, BUFFERS); then work_mem for that query only; then shared_buffers if the cache miss ratio rises with load. Autovacuum is the other quiet one — autovacuum_naptime = 30s, autovacuum_vacuum_scale_factor = 0.05.

FAQ

Why is work_mem described as per node rather than per connection?

A single query can allocate work_mem multiple times because each sort or hash node gets its own allocation. A query with three sorts allocates three times the setting. This is why forty connections at a 64 MB work_mem can demand 7.6 GB and OOM the box. Start at 16 MB and raise it for specific reports with SET LOCAL work_mem rather than globally.

What should I change first when PostgreSQL is slow?

Start with the index: run EXPLAIN (ANALYZE, BUFFERS) and look for a seq scan on a filtered column. Then raise work_mem for that query only. If the cache miss ratio rises with load, increase shared_buffers. If pg_stat_activity shows idle-in-transaction, add connection pooling.

Why set random_page_cost to 1.1?

The default of 4.0 assumes storage is seek-bound, which makes the planner refuse index scans on fast storage. Setting random_page_cost = 1.1 tells the planner that SSDs are not seek-bound, so it will trust index scans. The article lists it among the settings to apply after the four core ones.

How many connections should an 8 GB box allow?

Cap max_connections at 40, because beyond roughly that number on 8 GB throughput falls. Every connection is a process with its own memory, and each holds work_mem in the worst case. Put pgbouncer in front in transaction mode and cap the pool at 20.

How do I know if my settings are wrong rather than my queries?

Check two signals: a cache miss ratio above 2%, or a work_mem spill count rising in the log. Either means the settings are wrong, not the query. You can compute the miss ratio from pg_stat_database and find the slowest statements via pg_stat_statements ordered by mean execution time.