Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 9FBA0133655C for ; Tue, 15 Feb 2011 13:18:16 -0400 (AST) 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 63122-01 for ; Tue, 15 Feb 2011 17:18:09 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id CD2DE133616B for ; Tue, 15 Feb 2011 13:18:08 -0400 (AST) 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 p1FHI7Gv023762; Tue, 15 Feb 2011 12:18:07 -0500 (EST) To: Maxim Boguk cc: pgsql-bugs@postgresql.org Subject: Re: BUG #5885: Strange rows estimation for left join In-reply-to: References: <201102150243.p1F2hHge058708@wwwmaster.postgresql.org> <27062.1297740451@sss.pgh.pa.us> Comments: In-reply-to Maxim Boguk message dated "Tue, 15 Feb 2011 23:55:14 +1300" Date: Tue, 15 Feb 2011 12:18:07 -0500 Message-ID: <23761.1297790287@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201102/135 X-Sequence-Number: 29407 Maxim Boguk writes: > Test case look like: > create table "references" ( attr_id integer, reference integer, > object_id integer ); > insert into "references" select *100**(random()), > *100000**(random()^*10*), *1000000**(random()) from > generate_series(*1*,*10000000*); > create index xif01references on "references" ( reference, attr_id ); > create index xif02references on "references" ( object_id, attr_id, reference ); > analyze "references"; > explain select * from "references" rs left join "references" vm on > vm.reference = rs.reference and vm.attr_id = *10* where rs.object_id = > *1000*; I don't believe there's actually anything very wrong here. The large-looking estimate for the join size is not out of line: if you try different values for object_id you will find that some produce more rows than that and some produce less. If we had cross-column stats we could maybe derive a better estimate, but as-is you're getting an estimate that is probably about right on the average, depending on whether the particular object_id matches to more common or less common reference values. The thing that looks funny is that the inner indexscan rowcount estimate is so small, which is because that's being done on the assumption that the passed-in rs.reference value is random. It's not really --- it's more likely to be one of the more common reference values --- which is something that's correctly accounted for in the join size estimate but not in the inner indexscan estimate. regards, tom lane