Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Yl1Wy-0000ff-Jy for pgsql-performance@arkaria.postgresql.org; Wed, 22 Apr 2015 20:46:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Yl1Wx-0004nI-M7 for pgsql-performance@arkaria.postgresql.org; Wed, 22 Apr 2015 20:45:59 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YjAx9-0006dh-Vx for pgsql-performance@postgresql.org; Fri, 17 Apr 2015 18:25:24 +0000 Received: from mail-qg0-x22a.google.com ([2607:f8b0:400d:c04::22a]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YjAx6-0002Qh-EK for pgsql-performance@postgresql.org; Fri, 17 Apr 2015 18:25:22 +0000 Received: by qgdy78 with SMTP id y78so25664182qgd.0 for ; Fri, 17 Apr 2015 11:25:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:content-type:content-transfer-encoding:subject:message-id:date :to:mime-version; bh=6ZT/sU6wBQ2r+/Sm+aVShf//uaxjpQOgzPm+P2xmMtE=; b=NH5NZ5JKe42NjsEbVDlpaxYpOcs7pvMpI0yWnCTRATu9/V3tUBIkUonxj8bmF08AJW KWzypDRJogAywghFitKhsSp9v4JhLWgSOigA1yr2zUyjlRwgrh+U1OlqnB4RBLVNhQPL eugO47+9pC5kmpaQDdwU70VXn286vDgZJLWSb3Oc19+Oy1Rba79zi0h6NIiH+N/GnlpN OCQW0DPi2HaywGSUKo8ZMlekUt4eRKIvX19F7oWgVWk3+vcoWZfSSVxmyg8x7mDVW11v 47wMTgQSdb2lGNARCWcTGWKO6keY++0dHewRX6ZuWFElytxXe5OuKwSnyS9XU7R2w9sE TsHg== X-Received: by 10.140.150.209 with SMTP id 200mr3504012qhw.9.1429295119468; Fri, 17 Apr 2015 11:25:19 -0700 (PDT) Received: from [10.0.1.16] (cpe-104-162-204-234.nyc.res.rr.com. [104.162.204.234]) by mx.google.com with ESMTPSA id f5sm8615943qki.1.2015.04.17.11.25.17 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 17 Apr 2015 11:25:18 -0700 (PDT) From: Christian Gough Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: Postgresql Host Swapping Hard With Abundant Free Memory Message-Id: Date: Fri, 17 Apr 2015 14:25:15 -0400 To: pgsql-performance@postgresql.org Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2070.6\)) X-Mailer: Apple Mail (2.2070.6) X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Greetings, We have been having some pretty painful site outages related to heavy swapp= ing, even though the system may appear to have as much as 10GB of free memo= ry. It seems that as soon as the system hits ~22GB (of 32GB) of memory usage it= starts to swap. As soon as we go below ~22GB, swap is released.=20=20 During the worst outages we see: heavy swapping (10-15GB) heavy disk IO (up to 600ms) heavy CPU load: 4.0 (load over 100+ with 26 cores) available memory: (6-8GB) Here's the host information, it's a dedicated physical host, not a VM. ubuntu-14.04.1 LTS Linux db004 3.13.0-34-generic #60-Ubuntu SMP Wed Aug 13 15:45:27 UTC 2014 x= 86_64 x86_64 x86_64 GNU/Linux 24 cores, 32GB RAM, 32GB swapfile RAID 10 with SSDs Postgres version: postgresql-9.3.5 postgis-2.1.4 extension Here's our /etc/sysctl.conf: kernel.shmmax =3D 16850395136 kernel.shmall =3D 4113866 vm.overcommit_memory =3D 2 vm.overcommit_ratio =3D 50 vm.swappiness =3D 0=20 vm.dirty_ratio =3D 10 ## maximum 3.2GB dirty cache size=20 vm.dirty_background_ratio =3D 5 ## ratio at which disk begins to flush cach= e (1.6GB) We are setting our shared_buffers, work_mem and maintenance_work_mem very h= igh which I suspected is reason for swapping. The huge mystery to me is that during heavy swap there appears to be ~8-10G= B free memory in cache. We wondered whether that unused memory in cache was being consumed by our m= emory settings in postgresql.conf: shared_buffers =3D 8GB ## 25% of system memory (32GB) maintenance_work_mem =3D 8GB ## autovacuuming on, 3 workers (defaults) work_mem =3D 256MB ## we have as many as 170 connections We have average around 170 connections and have been moving everything to p= gbouncer to reduce this count. Rough estimate of cost with work_mem set to 256MB: 170 x 256MB =3D 43510MB = (43GB) Is it possible that the high work_mem setting is causing the connections to= hold on to the extra available memory? This is our assumption so we plan on dialing down work_mem and maintenance_= work_mem to sane values. We have also ordered more RAM (increase from 32GB to 96GB) but I would like= to understand what is happening. Here are the values we are going to change: maintenance_work_mem =3D 1GB work_mem =3D 64MB One last thing, we disabled THP (tranparent huge pages) because we were see= ing compaction errors: [db003.prod:~] root% egrep 'compact_(fail|stall)' /proc/vmstat compact_stall 34682729 compact_fail 32915396 Here are our competing theories of why we are swapping as much as 10-15GB w= ith 6GB-10GB of free memory: 1.) maintenance_work_mem and work_mem are set too high causing postgres to = allocate too much memory 2.) The 6GB - 10GB of free memory observed during swapping is postgresql's = shared buffer=20 3.) Linux filesystem caching is tuned incorrectly, maybe SSD related? 4.) This is a NUMA related issue similar to the Mysql Swap Insanity issue: http://blog.jcole.us/2010/09/28/mysql-swap-insanity-and-the-numa-architectu= re/ http://frosty-postgres.blogspot.com/2012/08/postgresql-numa-and-zone-reclai= m-mode.html http://www.postgresql.org/message-id/CAGTBQpacrSDcN10rTwRbH+AGm2_y0Qao6CJDo= yvEp504iFbdrw@mail.gmail.com Note, we zone_reclaim_mode appears disabled on our database host. root% cat /proc/sys/vm/zone_reclaim_mode 0 Hoping someone here can help us sort this out because it's a huge mystery f= or us. We are going to add more RAM but I'm trying to understand what is happening. Thank you kindly. Christian Gough --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance