Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cTr4N-0001cX-HT for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Jan 2017 14:18:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cTr4N-0006YK-4X for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Jan 2017 14:18:35 +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 1cTr4L-0006W4-U2 for pgsql-bugs@postgresql.org; Wed, 18 Jan 2017 14:18:34 +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 1cTr4J-0007l7-Cj for pgsql-bugs@postgresql.org; Wed, 18 Jan 2017 14:18:32 +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 v0IEITwb013997; Wed, 18 Jan 2017 09:18:29 -0500 From: Tom Lane To: =?UTF-8?B?0JLQsNC00LjQvCDQkNC60LHQsNGI0LXQsg==?= cc: Pg Bugs Subject: Re: Strange influence of default_statistics_target In-reply-to: References: Comments: In-reply-to =?UTF-8?B?0JLQsNC00LjQvCDQkNC60LHQsNGI0LXQsg==?= message dated "Wed, 18 Jan 2017 12:54:43 +0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <13995.1484749109.1@sss.pgh.pa.us> Date: Wed, 18 Jan 2017 09:18:29 -0500 Message-ID: <13996.1484749109@sss.pgh.pa.us> X-Pg-Spam-Score: -5.1 (-----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org =?UTF-8?B?0JLQsNC00LjQvCDQkNC60LHQsNGI0LXQsg==?= writes: > I have encountered a problem with querry plan building: > I'd set default_statistics_target=700, run analyze. Postgres optimize had > chosen plan with hash_join and it took ~1 min for qerry to complete. > Then i set default_statistics_target=500 and the plan was significantly > changed and was using merge_join instead, complition time reduced in > hundreds times, cost reduced drastically. > Now i can't understand why more precise statistics leads to less optimized > plan and what is the right way to use default_statistics_target parameter? > I attach both good and bad querry plans and the querry itself Are those really the same query? Plan 2 is enforcing a "number_value IS NOT NULL" condition on "attribute_value av1" that I don't see in plan 1. And neither plan seems to have much to do with the query, since the query has UNIONs that aren't in the plans. But the short answer seems to be that in both cases, the only reason that the plan doesn't take forever to run is that one sub-join chances to yield precisely zero rows, and the PG executor happens to be more efficient about that corner case in the one plan shape than the other. The planner doesn't take the possibility of that short-circuit happening into account, since it generally cannot be sure that a sub-join wouldn't yield any rows. So it's just luck that one plan is noticeably faster in this case. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs