Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id A162F1337C56 for ; Thu, 7 Apr 2011 11:59:32 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 62541-02-4 for ; Thu, 7 Apr 2011 14:59:25 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 2986E1337ECB for ; Thu, 7 Apr 2011 11:26:00 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p37EPs3b012468; Thu, 7 Apr 2011 10:25:54 -0400 (EDT) To: "French, Martin" cc: pgsql-admin@postgresql.org Subject: Re: Out Of Memory 8.1 In-reply-to: <81976671721DF04B9DCA6ECD87941A40287D35AF@roundway.Cromwell-tools.co.uk> References: <81976671721DF04B9DCA6ECD87941A40287D35AF@roundway.Cromwell-tools.co.uk> Comments: In-reply-to "French, Martin" message dated "Wed, 06 Apr 2011 23:44:07 +0100" Date: Thu, 07 Apr 2011 10:25:54 -0400 Message-ID: <12467.1302186354@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/58 X-Sequence-Number: 37239 "French, Martin" writes: > I am having problems with a query on 8.1 running on > RHEL 5.4 > work_mem = 98394 > The explain (cannot explain analyze, or Postgres runs out of memory > again) > 'HashAggregate (cost=2731947.55..2731947.57 rows=1 width=38)' > ' -> Seq Scan on stkl_rec (cost=0.00..2731947.54 rows=1 width=38)' > ' Filter: (((stkl_comp)::text = 'A'::text) AND > ((stkl_stockno)::text ~ > '^(TK[A-Za-z0-9][0-9]{3}(?:[0-9]{5}(?:[0-9]{3,4})?)?|NSP[0-9]{3}([0-9]{4 > })?|Z[MZ][0-9]{8,9}|LSP[0-9]{7}[A-Za-z0-9]|[A-Z][A-Z&][A-Z][0-9]{7}[A-Z0 > -9])$'::text) AND ((stkl_stockno)::text !~~ 'ZZ%'::text) AND > ((stkl_stockno)::text ~ > '^([A-z&]{2,3})([0-9][0-9][0-9])([0-9][0-9][0-9][0-9][A-z0-9]{1,3})$'::t > ext))' Apparently the number of groups is way more than the planner expects, and so the hash table grows to exceed available memory. Kluge fixes: try reducing work_mem to discourage it from using HashAggregate. Or you could temporarily turn off enable_hashagg. A non-kluge fix would involve getting the planner to realize there are a lot of groups needed. Have you analyzed the table lately? Maybe you need to increase the statistics target for it. regards, tom lane