title: How to reliably memory constrain a postgres database
body: <p>I run postgres 10.4 on a very small machine with strict memory constraints (e.g. 200MB) on Debian. System swap space must be disabled in my case but SSD Disk space is plenty available (e.g. > 500GB). I am using a waterfall approach to distribute all available memory to the different uses in postgres following this logic: </p>
  
  <pre><code>The available memory is 200MB
  ---
  max_connections = 10
  max_worker_processes = 2
  shared_buffers = 50MB
  work_mem = (200MB - shared_buffers) * 0.8 / max_connections
  maintenance_work_mem = (200MB - shared_buffers) * 0.1 / max_worker_processes
  temp_buffers = (200MB - shared_buffers) * 0.05
  
  wal_buffers = (200MB - shared_buffers) * 0.05
  temp_file_limit = -1 (i.e. unlimited)
  effective_cache_size = 200MB / 2
  </code></pre>
  
  <p>It is crucial for me, that sessions or even the postmaster are never canceled due to memory restrictions to ensure stable operation of postgres. In low memory situations postgres should work with temp files instead of memory.</p>
  
  <p>I still get out of memory errors in some situations. (e.g. when I have a large insert into a table.)</p>
  
  <p>How do I need to set all parameters to guarantee that postgres will not try to get more memory than there is available.</p>