Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wt5b5-0008Q9-PK for pgsql-performance@arkaria.postgresql.org; Sat, 07 Jun 2014 01:39:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Wt5b4-00084w-KI for pgsql-performance@arkaria.postgresql.org; Sat, 07 Jun 2014 01:39:02 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Wt5b2-00084l-FU for pgsql-performance@postgresql.org; Sat, 07 Jun 2014 01:39:00 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Wt5ay-0007BY-46 for pgsql-performance@postgresql.org; Sat, 07 Jun 2014 01:38:58 +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 s571csWO004583; Fri, 6 Jun 2014 21:38:54 -0400 From: Tom Lane To: Scott Marlowe cc: "pgsql-performance@postgresql.org" Subject: Re: In-reply-to: References: Comments: In-reply-to Scott Marlowe message dated "Fri, 06 Jun 2014 16:15:53 -0600" Date: Fri, 06 Jun 2014 21:38:54 -0400 Message-ID: <4582.1402105134@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 Scott Marlowe writes: > Well it's me again, with another performance regression. We have this query: > SELECT * > FROM users u > WHERE (u.user_group_id IN > (SELECT ug.id > FROM user_groups ug, pro_partners p > WHERE ug.pro_partner_id = p.id > AND p.tree_sortkey BETWEEN > E'0000000000010101000001000101000110000000000000000000000101101010' > AND > tree_right(E'0000000000010101000001000101000110000000000000000000000101101010') > OFFSET 0) > AND u.deleted_time IS NULL) > ORDER BY u.id LIMIT 1000; > OK so on 8.4.2 it runs fast. If I take out the offset 0 it runs slow. > If I run this on 8.4.15. 8.4.19 or 8.4.21 it also runs slow. This seems to be about misestimation of the number of rows out of a semijoin, so I'm thinking that the reason for the behavior change is commit 899d7b00e9 or 46f775144e. It's unfortunate that your example ends up on the wrong side of that change, but the original 8.4.x behavior was definitely pretty bogus; I think it's only accidental that 8.4.2 manages to choose a better plan. (The fact that you need the crutch of the "OFFSET 0" to get it to do so is evidence that it doesn't really know what its doing ;-).) One thing you might try is back-patching commit 4c2777d0b733, as I suspect that you're partially getting burnt by that in this scenario. I was afraid to back-patch that because of the API change possibly breaking third-party code, but in a private build that's unlikely to be an issue. 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