Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id BC3E6CBE9BE for ; Thu, 15 Dec 2011 04:12:01 -0400 (AST) Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rb6Q4-00039Y-3C for pgsql-bugs@postgresql.org; Thu, 15 Dec 2011 08:12:01 +0000 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 pBF8BiEO028904; Thu, 15 Dec 2011 03:11:44 -0500 (EST) To: maxim.boguk@gmail.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #6335: Weird planner decision with exists (a join b) condition In-reply-to: References: Comments: In-reply-to maxim.boguk@gmail.com message dated "Wed, 14 Dec 2011 11:09:38 +0000" Date: Thu, 15 Dec 2011 03:11:44 -0500 Message-ID: <28903.1323936704@sss.pgh.pa.us> From: Tom Lane X-Pg-Spam-Score: -4.2 (----) X-Archive-Number: 201112/106 X-Sequence-Number: 31917 maxim.boguk@gmail.com writes: > EXPLAIN analyze select * > from applicant_adv_subscription aas > where > aas.user_id in (5112699,7995496) > and exists ( > SELECT * from resume > join resume_view_history using (resume_id) > where > resume.user_id = aas.user_id > ); I'm hoping to fix this type of case with the "generalized inner indexscan" work that I've been nattering about for a year or two now. What you need to make this fast, given that resume and resume_view_history are both large, is to push the current value of aas.user_id down into the table scan of resume --- and because the join and semijoin can't be reordered, that's not possible with the planner's current simpleminded idea of what an inner indexscan can be. The other example you show manages to luck out and get a good plan due to transitive propagation of equality conditions, but that's a narrow special case. Any other form of constraint whatsoever on aas is going to end up with the crummy plan where the whole lower join gets computed. regards, tom lane