Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c9yya-0006UF-Bv for pgsql-performance@arkaria.postgresql.org; Thu, 24 Nov 2016 18:42:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1c9yyZ-0005VF-5L for pgsql-performance@arkaria.postgresql.org; Thu, 24 Nov 2016 18:42:27 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1c9yws-0003eK-TX for pgsql-performance@postgresql.org; Thu, 24 Nov 2016 18:40:43 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1c9ywm-0006MC-Fk for pgsql-performance@postgresql.org; Thu, 24 Nov 2016 18:40:41 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id uAOIeYVX023302; Thu, 24 Nov 2016 13:40:34 -0500 From: Tom Lane To: Carmen Mardiros cc: pgsql-performance@postgresql.org Subject: Re: How to tune Postgres to take advantage of 256GB RAM hardware In-reply-to: References: Comments: In-reply-to Carmen Mardiros message dated "Wed, 23 Nov 2016 22:15:25 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <23300.1480012834.1@sss.pgh.pa.us> Date: Thu, 24 Nov 2016 13:40:34 -0500 Message-ID: <23301.1480012834@sss.pgh.pa.us> X-Pg-Spam-Score: -4.9 (----) 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 Carmen Mardiros writes: > I've then tried different settings for work_mem, not changing anything else. > work_mem = 400MB -> query runs fine but memory usage in the system doesn't > exceed 1.3% > work_mem = 500MB -> usage hits 100% and postgres crashes out of memory. I suspect what may be happening is that when you push work_mem to >= 500MB, the planner decides it can replace the GroupAgg step with a HashAgg, which tries to form all the aggregate results at once in memory. Because of the drastic underestimate of the number of groups (2.7 mil vs 27 mil actual), the hash table is much bigger than the planner is expecting, causing memory consumption to bloat way beyond what it should be. You could confirm this idea by seeing if the EXPLAIN output changes that way depending on work_mem. (Use plain EXPLAIN, not EXPLAIN ANALYZE, so you don't actually run out of memory while experimenting.) If it's true, you might be able to improve the group-count estimate by increasing the statistics target for ANALYZE. However, the group-count underestimate only seems to be a factor of 10, so you'd still expect the memory usage to not be more than 5GB if the planner were getting it right otherwise. So there may be something else wrong, maybe a plain old memory leak. Can you generate a self-contained example that causes similar memory overconsumption? I'm guessing the behavior isn't very sensitive to the exact data you're using as long as the group counts are similar, so maybe you could post a script that generates junk test data that causes this, rather than needing 27M rows of real data. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance