Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 7C04D1337B9C for ; Sat, 16 Apr 2011 20:54:42 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 00369-02 for ; Sat, 16 Apr 2011 23:54:36 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.catalyst.net.nz (bertrand.catalyst.net.nz [202.78.240.40]) by mail.postgresql.org (Postfix) with ESMTP id C94DE1336FA7 for ; Sat, 16 Apr 2011 20:54:34 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mail.catalyst.net.nz (Postfix) with ESMTP id E872C32014; Sun, 17 Apr 2011 11:54:32 +1200 (NZST) X-Virus-Scanned: Debian amavisd-new at catalyst.net.nz Received: from mail.catalyst.net.nz ([127.0.0.1]) by localhost (bertrand.catalyst.net.nz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id DjBWMEvKPJRG; Sun, 17 Apr 2011 11:54:32 +1200 (NZST) Received: from [192.168.1.11] (122-60-64-230.jetstream.xtra.co.nz [122.60.64.230]) (Authenticated sender: mark.kirkwood) by mail.catalyst.net.nz (Postfix) with ESMTPSA id D348F31F4B; Sun, 17 Apr 2011 11:54:31 +1200 (NZST) Message-ID: <4DAA2C37.1010903@catalyst.net.nz> Date: Sun, 17 Apr 2011 11:54:31 +1200 From: Mark Kirkwood User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8 MIME-Version: 1.0 To: Tom Lane CC: Greg Stark , pgsql-bugs@postgresql.org Subject: Re: Massive memory use for star query References: <4DA7CB25.5040102@catalyst.net.nz> <25522.1302885781@sss.pgh.pa.us> <4DA94378.5030600@catalyst.net.nz> <749.1302965922@sss.pgh.pa.us> In-Reply-To: <749.1302965922@sss.pgh.pa.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/127 X-Sequence-Number: 30046 On 17/04/11 02:58, Tom Lane wrote: > Greg Stark writes: >> The planner uses various heuristics to avoid combinatoric growth >> wherever it can but there's no way to completely avoid it. > Yeah. The collapse_limit variables can be seen as another heuristic to > deal with this type of problem: they artificially limit the number of > combinations considered by forcing the join search to be broken down > into subproblems. The trouble of course is that this breakdown is > pretty stupid and can easily prevent the best join order from ever being > considered. > > If you've got a small number of such query types that you can afford to > spend some manual effort on, here's what I'd do: > > 1. With those three planner variables cranked up to more than the number > of relations in the query (if possible), run an EXPLAIN, or better > EXPLAIN ANALYZE so you can confirm you get a good plan. > > 2. Observe the join order selected in the good plan. > > 3. Rearrange your query so that the tables are explicitly JOINed in that > order. Don't use the FROM-comma-list style. > > 4. Now, in your production app, *reduce* join_collapse_limit to a small > value, maybe even 1, to force the syntactic JOIN order to be followed. > (Obviously, don't keep it there when running queries you haven't > hand-optimized this way.) > > This will force the planner to consider only small subproblems, which > will make it both much faster and much less memory-hungry than when it's > trying to solve a large join problem from scratch. > We've sort of done an equivalent thing as a temporary fix - restricted the page generating these queries to one or two keywords to tame the number of tables joined in. We are only seeing this type of query being generated in a very specific part of the application (keyword search), and I've been encouraging a redesign in that area anyway as I don't believe it is necessary to require so many joins to achieve what they wish to do - so this is really the clincher for a redesign. I will get 'em to reduce the *collapse limits too. Thanks to all of you for your help, regards Mark