Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1X77IX-0003jI-TK for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Jul 2014 18:17:54 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1X77IX-0000Fh-Dm for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Jul 2014 18:17:53 +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 1X77IW-0000FZ-My for pgsql-bugs@postgresql.org; Tue, 15 Jul 2014 18:17:52 +0000 Received: from mail-qa0-f47.google.com ([209.85.216.47]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1X77IT-0007Y2-Cb for pgsql-bugs@postgresql.org; Tue, 15 Jul 2014 18:17:50 +0000 Received: by mail-qa0-f47.google.com with SMTP id i13so4806280qae.20 for ; Tue, 15 Jul 2014 11:17:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:user-agent :mime-version:content-type; bh=+xcvBtlG9L7WS0WZjpeL2HbUZAj9cvIgZBM0NBo31Yg=; b=i/tyZHmQk995RPMWACgqzFm9ge2oPNZU1UBKhMKTkWYqHA1x09ELFnZRm/RxL5zg5O E67oMJP6omWQSHMpf8vk8/zcGvst9Hd48yBEPE7KNZL8KY86WsKKug/sg+92UnSLlTkx mkY1WHHPcCko4hkDFChlgHUUTxG+uyRse9HNnSzDOmkS5Y7hbleAP4oBqHaImfdLYAz4 S79omA9ym57WACmWZpXNzF14Hm2tgpZVjelijRiJXPbfPpwLNo8kl1dE9KZjS3lIdDQV XQWrqrRTVrldMX7wlF1Fkf6eroGXqmicvJ1Ew92uo4sp1LmIwLaAIDmbO0/RefMdttCN tisA== X-Gm-Message-State: ALoCoQktGaVMWae9/z5PSUCDuxCuoXOi3yrW0eXe1+Bv35a1DAESP013hLi/SpHDnw81MtEHoPyC X-Received: by 10.140.109.164 with SMTP id l33mr37191331qgf.72.1405448268143; Tue, 15 Jul 2014 11:17:48 -0700 (PDT) Received: from rain.nss.udel.edu (rain.nss.udel.edu. [128.175.2.33]) by mx.google.com with ESMTPSA id z9sm14948208qge.4.2014.07.15.11.17.47 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 15 Jul 2014 11:17:47 -0700 (PDT) Date: Tue, 15 Jul 2014 14:17:45 -0400 (EDT) From: Mike Porter X-X-Sender: mike@rain.local To: pgsql-bugs@postgresql.org Subject: INET datatype and '<<' operator results in bad row estimate Message-ID: User-Agent: Alpine 2.00 (OSX 1167 2008-08-23) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Postgres version: 9.3.4 Note: This behavior appears to have been fixed in 9.4-beta1. I am reporting it in case it can be fixed in 9.3.x. Perhaps it should be mentioned in the 9.4.x release notes (or did I miss it?) A simple query of the form: mike=# explain select * from swmon_internal.arpmon a join swmon_internal.macaddr_ip mi on( a.macaddr_ip = mi.id ) where ip << '128.175.10.0/25'; QUERY PLAN ------------------------------------------------------------------------------------------------------------------- Hash Join (cost=5215.15..112839.99 rows=1591378 width=53) Hash Cond: (a.macaddr_ip = mi.id) -> Seq Scan on arpmon a (cost=0.00..59883.53 rows=3182753 width=32) -> Hash (cost=8.45..8.45 rows=416536 width=21) -> Index Scan using macaddr_ip_ip_macaddr_index on macaddr_ip mi (cost=0.42..8.45 rows=416536 width=21) Index Cond: ((ip > '128.175.10.0/25'::inet) AND (ip <= '128.175.10.127'::inet)) Filter: (ip << '128.175.10.0/25'::inet) Results in a bad row guess (416536) and hence a slower plan is chosen since postgres is expecting far too many rows. The number chosen is usually 1/2 the number of rows in the macaddr_ip table, by the way. Notice that if I manually add the index condition to the query: mike=# explain select * from swmon_internal.arpmon a join swmon_internal.macaddr_ip mi on( a.macaddr_ip = mi.id ) where ip << '128.175.10.0/25' and ((ip > '128.175.10.0/25'::inet) AND (ip <= '128.175.10.127'::inet)); QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=0.85..97.81 rows=4 width=53) -> Index Scan using macaddr_ip_ip_macaddr_index on macaddr_ip mi (cost=0.42..8.45 rows=1 width=21) Index Cond: ((ip > '128.175.10.0/25'::inet) AND (ip <= '128.175.10.127'::inet) AND (ip > '128.175.10.0/25'::inet) AND (ip <= '128.175.10.127'::inet)) Filter: (ip << '128.175.10.0/25'::inet) -> Index Scan using arpmon_macaddr_ip_index on arpmon a (cost=0.43..89.13 rows=22 width=32) Index Cond: (macaddr_ip = mi.id) The number of estimated rows from macaddr_ip is about right (22) and a much better plan is used. vacuum analyze has been done and the histogram for the table is reasonable. mike=# \d swmon_internal.macaddr_ip Table "swmon_internal.macaddr_ip" Column | Type | Modifiers ---------+---------+--------------------------------------------------------------------- id | bigint | not null default nextval('swmon_internal.macaddr_ip_seq'::regclass) macaddr | macaddr | not null ip | inet | not null Indexes: "macaddr_ip_id_key" UNIQUE CONSTRAINT, btree (id) "macaddr_ip_ip_macaddr_index" UNIQUE, btree (ip, macaddr) "macaddr_ip_host_ip_index" btree (host(ip)) "macaddr_ip_macaddr_index" btree (macaddr) Referenced by: TABLE "swmon_internal.arpmon" CONSTRAINT "arpmon_macaddr_ip_fkey" FOREIGN KEY (macaddr_ip) REFERENCES swmon_internal.macaddr_ip(id) ON UPDATE CASCADE mike=# \d swmon_internal.arpmon Table "swmon_internal.arpmon" Column | Type | Modifiers ------------+--------------------------+----------------------------------------------------------------- id | bigint | not null default nextval('swmon_internal.arpmon_seq'::regclass) macaddr_ip | bigint | not null start | timestamp with time zone | not null default now() finish | timestamp with time zone | not null default now() Indexes: "arpmon_id_key" UNIQUE CONSTRAINT, btree (id) "arpmon_finish_index" btree (finish) "arpmon_macaddr_ip_index" btree (macaddr_ip) "arpmon_start_index" btree (start) Foreign-key constraints: "arpmon_macaddr_ip_fkey" FOREIGN KEY (macaddr_ip) REFERENCES swmon_internal.macaddr_ip(id) ON UPDATE CASCADE Referenced by: TABLE "swmon_internal.link_swmon_arpmon" CONSTRAINT "link_swmon_arpmon_arpmon_id_fkey" FOREIGN KEY (arpmon_id) REFERENCES swmon_internal.arpmon(id) ON UPDATE CASCADE Again, this works correctly in 9.4-beta1. Thanks, Mike - Mike Porter PGP Fingerprint: F4 AE E1 9F 67 F7 DA EA 2F D2 37 F3 99 ED D1 C2 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs