Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 4F5B91E0AD71 for ; Wed, 11 Jan 2012 20:55:53 -0400 (AST) Received: from mail.anarazel.de ([217.115.131.40]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rl8xI-00076Y-6C for pgsql-bugs@postgresql.org; Thu, 12 Jan 2012 00:55:53 +0000 Received: from intern.anarazel.de (brln-d9ba669a.pool.mediaWays.net [217.186.102.154]) (Authenticated sender: andres@anarazel.de) by mail.anarazel.de (Postfix) with ESMTPA id 2FFBB448004; Thu, 12 Jan 2012 01:53:37 +0100 (CET) From: Andres Freund To: pgsql-bugs@postgresql.org Subject: Re: Botched estimation in eqjoinsel_semi for cases without reliable ndistinct Date: Thu, 12 Jan 2012 01:53:02 +0100 User-Agent: KMail/1.13.7 (Linux/3.1.0-1-amd64; KDE/4.6.5; x86_64; ; ) Cc: Tom Lane , casey.shobe@messagesystems.com References: <201201112110.40403.andres@anarazel.de> <29570.1326326461@sss.pgh.pa.us> In-Reply-To: <29570.1326326461@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: Text/Plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <201201120153.02587.andres@anarazel.de> X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201201/96 X-Sequence-Number: 32163 On Thursday, January 12, 2012 01:01:01 AM Tom Lane wrote: > (In cases where we know it's unique, something like this > could be reasonable, but I believe get_variable_numdistinct already > accounts for such cases.) One of those case which looks relatively easy is that CTEs currently work as a kind of 'statistics barrier' here. Specifically I wonder why: test_raptelan=# EXPLAIN WITH foo AS (SELECT * FROM b WHERE id < 5000) SELECT * FROM a WHERE a.id IN (SELECT id FROM foo); QUERY PLAN ------------------------------------------------------------------------------ Nested Loop (cost=302.02..1876.30 rows=2550000 width=11) CTE foo -> Index Scan using b_pkey on b (cost=0.00..184.06 rows=5243 width=10) Index Cond: (id < 5000) -> HashAggregate (cost=117.97..119.97 rows=200 width=4) -> CTE Scan on foo (cost=0.00..104.86 rows=5243 width=4) -> Index Scan using a_pkey on a (cost=0.00..7.85 rows=1 width=11) Index Cond: (id = foo.id) plans differently than test_raptelan=# EXPLAIN SELECT * FROM a WHERE a.id IN (SELECT id FROM b WHERE id < 5000 OFFSET 0); QUERY PLAN -------------------------------------------------------------------------------------------- Merge Semi Join (cost=560.41..17426.03 rows=5243 width=11) Merge Cond: (a.id = b.id) -> Index Scan using a_pkey on a (cost=0.00..160013.81 rows=5100000 width=11) -> Sort (cost=560.40..573.51 rows=5243 width=4) Sort Key: b.id -> Limit (cost=0.00..184.06 rows=5243 width=4) -> Index Only Scan using b_pkey on b (cost=0.00..184.06 rows=5243 width=4) Index Cond: (id < 5000) Couldn't the CTE pass a vardata from inside to the outside? Andres