Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WvouW-0000Ah-4o for pgsql-performance@arkaria.postgresql.org; Sat, 14 Jun 2014 14:26:24 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WvouU-0006mp-Vu for pgsql-performance@arkaria.postgresql.org; Sat, 14 Jun 2014 14:26:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WvouU-0006md-55 for pgsql-performance@postgresql.org; Sat, 14 Jun 2014 14:26:22 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WvouQ-0002xQ-GM for pgsql-performance@postgresql.org; Sat, 14 Jun 2014 14:26:21 +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 s5EEQEIh006054; Sat, 14 Jun 2014 10:26:14 -0400 From: Tom Lane To: Timothy Garnett cc: "pgsql-performance@postgresql.org" Subject: Re: Query memory usage greatly in excess of work_mem * query plan steps In-reply-to: References: Comments: In-reply-to Timothy Garnett message dated "Wed, 11 Jun 2014 18:02:55 -0400" Date: Sat, 14 Jun 2014 10:26:14 -0400 Message-ID: <6053.1402755974@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) 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 Timothy Garnett writes: > I have a query that's pulling data for another system using COPY (query) to > STDOUT CSV on a 9.2.4 db (we're in the process of upgrading to 9.3). > ... > We're running into problems with the machine running out of memory with > this single query process consuming over 100GB resident memory before the > machine exhausts swap and the Linux OOM handling eventually kills it. I wonder if you're hitting some sort of memory leak. What I'd suggest doing to help diagnose that is to show us a memory map. Do this: (1) Set a ulimit so that the process will get ENOMEM sometime before the OOM killer awakens (this is good practice anyway, if you've not disabled OOM kills). On Linux systems, ulimit -m or -v generally does the trick. The easiest way to enforce this is to add a ulimit command to the script that launches the postmaster, then restart. (2) Make sure your logging setup will collect anything printed to stderr by a backend. If you use logging_collector you're good to go; if you use syslog you need to check where the postmaster's stderr was redirected, making sure it's not /dev/null. (3) Run the failing query. Collect the memory map it dumps to stderr when it fails, and send it in. What you're looking for is a couple hundred lines looking like this: TopMemoryContext: 69984 total in 10 blocks; 6152 free (16 chunks); 63832 used MessageContext: 8192 total in 1 blocks; 7112 free (1 chunks); 1080 used Operator class cache: 8192 total in 1 blocks; 1640 free (0 chunks); 6552 used smgr relation table: 24576 total in 2 blocks; 13872 free (3 chunks); 10704 used ... lots more in the same vein ... > As to the right join (used for a few of the joins, most are left join or > merge): > -> Hash Right Join (cost=225541299.19..237399743.38 > rows=86681834 width=1108) > Hash Cond: (xxx.xxx = yyy.yyy) > -> Seq Scan on xxx (cost=0.00..6188.18 > rows=9941 width=20) > Filter: (mode = 'live'::text) > -> Hash (cost=212606744.27..212606744.27 > rows=86681834 width=1096) > .... > I'm not sure if I'm reading it right, but it looks like it's hashing the 86 > million row set and scanning over the 10k row set which seems to me like > the opposite of what you'd want to do, but I haven't seen a lot of hash > right joins in plans and I'm not sure if that's how it works. That looks pretty odd to me too, though I guess the planner might think it was sensible if xxx's join column had very low cardinality. Still, it's weird. What have you got work_mem set to exactly? 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