BLACK FRIDAY 2026 LIVE: RackNerd 4GB VPS $47.98/yr (Lifetime) · ElevenLabs 55% Off · Hostinger 73% Off · Amazon Local AI Hardware View Verified Deals →
Lab Essay #05 · Database Internals9 min readReal Lab Measurements

PostgreSQL 16 on a 4 GB VPS: The Exact 6 Settings That Stop Disk Bloat, Autovacuum Stalls & Connection Exhaustion

TL;DR

Stock PostgreSQL 16 Docker images ship tuned for a 1998 512 MB workstation (`shared_buffers=128MB`, `work_mem=4MB`) while n8n writes heavy JSONB execution payloads continuously. On a 4 GB VPS, apply 6 targeted overrides (`shared_buffers=512MB`, `effective_cache_size=1536MB`, `work_mem=8MB`, `checkpoint_completion_target=0.9`, `max_connections=60`, `autovacuum_vacuum_scale_factor=0.05`) alongside `EXECUTIONS_DATA_PRUNE=true` so database size stays flat under 400 MB.

Chart showing 500-op 4K fsync latency distribution P50 0.659ms and P99 1.289ms alongside SQLite DELETE 21.3% and WAL 10.7% write failure rates vs PostgreSQL 16 0% failures
Figure 1 · Why Postgres 16 Beats SQLite on the Same 4K fsync Disk: Even with 7.69 ms noisy-neighbor disk tail spikes on a shared KVM host, PostgreSQL 16 commits concurrent webhook writes cleanly because WAL group-commit amortizes fsync calls across transactions.

1. Why Untuned Postgres Still Gets Sluggish at Week 6

Moving n8n off SQLite onto PostgreSQL 16 eliminates `SQLITE_BUSY` immediately, which lulls many builders into ignoring Postgres forever. Six weeks later, opening the n8n "Executions" tab takes 8 seconds, and `df -h` shows `/var/lib/docker/volumes` eating 28 GB of a 40 GB VPS disk.

The culprit is how PostgreSQL MVCC handles `EXECUTIONS_DATA_PRUNE=true`. Deleting 20,000 old execution rows does NOT shrink files on disk immediately—it marks tuples as dead until `autovacuum` reclaims them for reuse. With Postgres’s default `autovacuum_vacuum_scale_factor = 0.2` (20% of table rows!), a huge `execution_data` table accumulates gigabytes of dead JSONB TOAST bloat before autovacuum even wakes up.

2. The 6 Exact PostgreSQL 16 Overrides for a 4 GB KVM Node

You do NOT want to give Postgres 2 GB of `shared_buffers` on a 4 GB machine running n8n/OpenClaw—that starves Node.js and invites Exit Code 137. Instead, size Postgres deliberately to sit in a tight ~300–600 MB envelope while letting Linux page cache accelerate reads:

Verified PostgreSQL 16 Tuning Profile for 4 GB RAM VPS Running n8n + Caddy
ParameterStock Default4 GB VPS TunedWhy It Matters
shared_buffers128 MB512 MBHolds hot workflow definitions & active indexes in RAM
effective_cache_size4 GB1536 MBHonest planner hint accounting for Node.js RAM share
work_mem4 MB8 MBSpeeds up Execution-history ORDER BY sorts without OOM
max_connections10060Caps per-connection RAM overhead under webhook storms
checkpoint_completion_target0.90.9Spreads WAL fsync writes smoothly across shared KVM I/O
autovacuum_vacuum_scale_factor0.20 (20%)0.05 (5%)Reclaims pruned n8n JSONB dead tuples 4× sooner

3. Drop-In Docker Compose Command Flags (Zero Custom Config File Needed)

You do not even need to mount a custom `postgresql.conf`. Pass all six flags cleanly via `command:` inside your `postgres:16-alpine` Compose service, and verify with `SHOW shared_buffers;`:

  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    command:
      - "postgres"
      - "-c"
      - "shared_buffers=512MB"
      - "-c"
      - "effective_cache_size=1536MB"
      - "-c"
      - "work_mem=8MB"
      - "-c"
      - "max_connections=60"
      - "-c"
      - "checkpoint_completion_target=0.9"
      - "-c"
      - "autovacuum_vacuum_scale_factor=0.05"
    mem_limit: 900m

Related Engineering Deep-Dives