Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VEl6G-0000Ku-Hc for pgsql-performance@arkaria.postgresql.org; Wed, 28 Aug 2013 19:08:16 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VEl6G-0008Gs-1d for pgsql-performance@arkaria.postgresql.org; Wed, 28 Aug 2013 19:08:16 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VEl6E-0008Gl-QM for pgsql-performance@postgresql.org; Wed, 28 Aug 2013 19:08:14 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VEl6C-0004SH-0B for pgsql-performance@postgresql.org; Wed, 28 Aug 2013 19:08:14 +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 r7SJ8Aip017620; Wed, 28 Aug 2013 15:08:11 -0400 From: Tom Lane To: Rafael Martinez cc: pgsql-performance@postgresql.org Subject: Re: SQL statement over 500% slower with 9.2 compared with 9.1 In-reply-to: <521B4B2A.5010800@usit.uio.no> References: <521B4B2A.5010800@usit.uio.no> Comments: In-reply-to Rafael Martinez message dated "Mon, 26 Aug 2013 14:33:46 +0200" Date: Wed, 28 Aug 2013 15:08:10 -0400 Message-ID: <17619.1377716890@sss.pgh.pa.us> X-Pg-Spam-Score: -0.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 Rafael Martinez writes: > We have a SQL statement that with 9.1 takes ca 4000ms to finnish and > with 9.2 over 22000ms. > ... > We can see that the query plan is very different between versions and > that 9.2 is really wrong with the number of rows involved. Why is 9.2 > taking so wrong about the number of rows involved in some parts of the > plan? 9.1's no better. The reason you don't get a similar plan out of 9.1 is that it doesn't flatten the nested EXISTS sub-selects, so that a parameterized nestloop plan is the best it can do no matter what. 9.2 is able to consider more types of plan for this query, and it's finding one that it thinks is cheaper. Unfortunately, parameterized nestloop really is the best thing in this specific case. I think the rowcount estimation error that's actually serious is the one for the "19 < (Subplan 1)" condition, where it's expecting 161252 rows but reality is only 179. If that were even just one order of magnitude closer to reality, the other plan style would look cheaper. Unfortunately, I can't offhand think of anything you can do to improve the estimation of that condition as-is. Maybe there's some other way to phrase the query? The current coding of the query looks rather like it's been tuned for the one case that pre-9.2 releases can manage to do well. If you don't want to do any major rewriting, you could probably stick an OFFSET 0 into the outer EXISTS sub-select (and/or the inner one) to get something similar to the 9.1 plan. For some context see commit 0816fad6eebddb8f1f0e21635e46625815d690b9 and the previous commits it references. 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