Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dHFed-0000Dd-Id for pgsql-bugs@arkaria.postgresql.org; Sat, 03 Jun 2017 20:28:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dHFec-0007U2-Uz for pgsql-bugs@arkaria.postgresql.org; Sat, 03 Jun 2017 20:28:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dHFdw-0006Gp-B0 for pgsql-bugs@postgresql.org; Sat, 03 Jun 2017 20:27:28 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dHFds-0001hM-NO for pgsql-bugs@postgresql.org; Sat, 03 Jun 2017 20:27:27 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v53KRGdW023724; Sat, 3 Jun 2017 16:27:17 -0400 From: Tom Lane To: marko@joh.to cc: pgsql-bugs@postgresql.org Subject: Re: BUG #14664: Nonsensical join selectivity estimation despite n_distinct In-reply-to: <20170522132017.29944.48391@wrigleys.postgresql.org> References: <20170522132017.29944.48391@wrigleys.postgresql.org> Comments: In-reply-to marko@joh.to message dated "Mon, 22 May 2017 13:20:17 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <23722.1496521636.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 03 Jun 2017 16:27:16 -0400 Message-ID: <23723.1496521636@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) 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 marko@joh.to writes: > Despite the fact that postgres knows that the lookup on q2 will always > produce exactly 10 rows, it still estimates it at 58. It doesn't know that. You've told it there are N/10 distinct values, but the conclusion that any given outer row matches exactly 10 inner rows depends on an additional assumption that the distribution is exactly uniform. It's not going to think that, because of ANALYZE's not-exactly-uniform sampling. Repeatedly running ANALYZE on your example table with the usual default_statistics_target of 100, I get results similar to regression=3D# select f, count(*) from (select unnest(most_common_freqs) f = from pg_stats where tablename =3D 'qqq' and attname =3D 'other') ss group b= y 1 order by 1; f | count=20 -------------+------- 6.66667e-05 | 98 0.0001 | 2 (2 rows) which corresponds to a maximum of 3 occurrences of a "most common value", and a minimum of 2 occurrences, out of the default 30000-row sample. (Of course, ANALYZE will reject single-occurrence values as not being MCVs, so we could never see frequencies less than 2/30000 =3D 6.67e-5.) If I increase the stats target to 1000, I get results like regression=3D# select f, count(*) from (select unnest(most_common_freqs) f = from pg_stats where tablename =3D 'qqq' and attname =3D 'other') ss group b= y 1 order by 1; f | count=20 -------------+------- 6.66667e-06 | 935 1e-05 | 65 (2 rows) regression=3D# select f, count(*) from (select unnest(most_common_freqs) f = from pg_stats where tablename =3D 'qqq' and attname =3D 'other') ss group b= y 1 order by 1; f | count=20 -------------+------- 6.66667e-06 | 936 1e-05 | 63 1.33333e-05 | 1 (3 rows) (the counts move around a bit, but not very much). That corresponds to a minimum of 2 and a maximum of either 3 or 4 duplicates in a 300000-row sample. Given that we know that the duplicate values of "other" are adjacent, what this is looking like to me is that there's something fishy about ANALYZE's choice of sample rows. It seems like it should not be picking 3 or 4 rows from the same block very often, and for all of those to come from the same 10-row span (out of 225 rows per page, more or less, in this table) seems like it ought to be a really low-probability event. But maybe my math is off. Another way we could look at this is that if ANALYZE can't find any entries occurring more than 3 or 4 times in 300000 samples, it ought to reject them as not being MCVs after all. The code's existing credulity about what counts as an MCV dates from back when typical sample sizes were 3000 rows, not 10 or 100 times that. Maybe we'd better rethink that, though I'm not sure what a suitable filtering rule would be. Anyway, this is not the fault of the selectivity estimator, it's the fault of a nonuniform ANALYZE sample that reports some values as MCVs, with overblown frequency estimates, even though they're not really any more common than anything else. Pending some decision about whether and how to change ANALYZE, it seems like raising the stats target to 1000 mostly solves the problem for your example table: regression=3D# explain select * from qqq q1 join qqq q2 using (other) where= q1.pk =3D 10; QUERY PLAN=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20 ---------------------------------------------------------------------------= -------- Nested Loop (cost=3D1.14..17.43 rows=3D15 width=3D12) -> Index Scan using qqq_pkey on qqq q1 (cost=3D0.57..8.59 rows=3D1 wid= th=3D8) Index Cond: (pk =3D 10) -> Index Scan using qqq_other_idx on qqq q2 (cost=3D0.57..8.74 rows=3D= 10 width=3D8) Index Cond: (other =3D q1.other) (5 rows) We're still overestimating a bit from thinking that the "MCVs" really occur more often than other values, but the error is fairly small at this level. regards, tom lane --=20 Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs