Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 137F6B5DC1B for ; Wed, 21 Sep 2011 17:29:32 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 89487-04 for ; Wed, 21 Sep 2011 20:29:25 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from smtp-out-1.01.com (smtp.01.com [216.115.88.169]) by mail.postgresql.org (Postfix) with ESMTP id 080B1B5DC1A for ; Wed, 21 Sep 2011 17:29:24 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id 3B6619D0D1C for ; Wed, 21 Sep 2011 15:29:24 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-1.01.com Received: from smtp-out-1.01.com ([127.0.0.1]) by localhost (smtp-out-1.01.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 95fAcov7SjPl for ; Wed, 21 Sep 2011 15:29:24 -0500 (CDT) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id 199C49D0D14 for ; Wed, 21 Sep 2011 15:29:24 -0500 (CDT) Received: from Sidney-Stratton.local (70-36-143-92.dsl.dynamic.sonic.net [70.36.143.92]) by smtp-out-1.01.com (Postfix) with ESMTPSA id C686C9D0D23 for ; Wed, 21 Sep 2011 15:29:23 -0500 (CDT) Message-ID: <4E7A4923.1020900@agliodbs.com> Date: Wed, 21 Sep 2011 13:29:23 -0700 From: Josh Berkus Organization: PostgreSQL Experts Inc. User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: pgsql-bugs@postgresql.org Subject: Broken selectivity with special inet operators X-Enigmail-Version: 1.1.1 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-5 required=5 tests=BAYES_00=-1.9, TO_NO_BRKTS_PCNT=0.001 X-Spam-Level: X-Archive-Number: 201109/142 X-Sequence-Number: 31159 Summary: special inet operators ( << >> <<= =>> ) are up to 1000000X off in estimating rowcounts Type: performance Severity: normal Tested on: 9.1.0 Description: We've been noticing that row estimates for queries which use the =>> and <<= operators for inet data were way, way off. We finally narrowed the problem down to a simple test: =========== USING <<= : =========== explain analyze SELECT count(*) FROM partition1 lh WHERE lh.ip <<= '1.2.3'::cidr; QUERY PLAN ..... -> Index Scan using partition1_ip on partition1 lh (cost=0.00..10.21 rows=6956732 width=0) (actual time=0.016..0.016 rows=0 loops=1) Index Cond: ((ip >= '1.2.3.0/24'::inet) AND (ip <= '1.2.3.255'::inet)) Filter: (ip <<= '1.2.3.0/24'::inet) ..... explain analyze SELECT count(*) FROM partition2 WHERE 1=1 AND ip <<= '87.178.193.0/24'::inet; QUERY PLAN Aggregate (cost=18296.78..18296.79 rows=1 width=0) (actual time=0.037..0.038 rows=1 loops=1) -> Index Scan using partition2_ip on partition2 (cost=0.00..38.36 rows=7303365 width=0) (actual ti me=0.022..0.031 rows=5 loops=1) Index Cond: ((ip >= '87.178.193.0/24'::inet) AND (ip <= '87.178.193.255'::inet)) Filter: (ip <<= '87.178.193.0/24'::inet) Total runtime: 0.107 ms ============ USING < > : ============ explain analyze SELECT count(*) FROM partition1 lh WHERE lh.ip >= '1.2.3.0/24'::inet and lh.ip <= '1.2.3.255'::inet; QUERY PLAN .... -> Index Scan using partition1_ip on partition1 lh (cost=0.00..10.22 rows=1 width=0) (actual time=0.016..0.016 rows=0 loops=1) Index Cond: ((ip >= '1.2.3.0/24'::inet) AND (ip <= '1.2.3.255'::inet)) .... explain analyze SELECT count(*) FROM partition2 WHERE 1=1 AND ip > '87.178.193.0'::inet and ip <= '87.178.193.255'::inet; QUERY PLAN Aggregate (cost=26.34..26.35 rows=1 width=0) (actual time=0.033..0.033 rows=1 loops=1) -> Index Scan using partition2_ip on partition2 (cost=0.00..26.33 rows=5 width=0) (actual time=0.0 19..0.029 rows=5 loops=1) Index Cond: ((ip > '87.178.193.0'::inet) AND (ip <= '87.178.193.255'::inet)) Total runtime: 0.097 ms ==== Note that the mis-estimate of rows returned in each case is almost exactly 50% of the total rows in the table. That would suggest that match_special_index_operator is failing, and not recognizing the <<= operator for estimation purposes and just going with a default estimate of 0.5. I've tried to locate the cause of this problem, but the code involved is rather convoluted and crusty, and I can't follow the logic. Help? -- Josh Berkus PostgreSQL Experts Inc. http://pgexperts.com