Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQTxM-0006IA-Fx for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Jun 2018 08:38:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fQTxK-0005V3-Fl for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Jun 2018 08:38: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.89) (envelope-from ) id 1fQTxK-0005Ut-9P for pgsql-bugs@lists.postgresql.org; Wed, 06 Jun 2018 08:38:10 +0000 Received: from gauss.credativ.com ([93.94.130.89]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQTxC-0001K7-Tp for pgsql-bugs@postgresql.org; Wed, 06 Jun 2018 08:38:09 +0000 Received: from gauss.credativ.com (localhost [127.0.0.1]) by gauss.credativ.com (Postfix) with ESMTP id 97F5A1E1DAB; Wed, 6 Jun 2018 10:38:01 +0200 (CEST) Received: from snipe (fw-front.credativ.com [62.154.226.94]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: ase@gauss.credativ.com) by gauss.credativ.com (Postfix) with ESMTPSA id 832F01E1AAF; Wed, 6 Jun 2018 10:38:01 +0200 (CEST) From: Andreas Seltenreich To: pgsql-bugs@postgresql.org Subject: Unnecessarily imprecise stats for very small tables leading to bad plans X-PGP-Key: 4096R/803D1FFF 2015-02-04 Key fingerprint = 4758 81DC 8539 494D 29B4 692D F587 0601 803D 1FFF X-Now-Playing: Triple X Girlfriend / Pulse / Front 242 Date: Wed, 06 Jun 2018 10:38:01 +0200 Message-ID: <87o9gos42e.fsf@credativ.de> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, one of our customers suffers from excessively bad plans when joining a big table (100G) against a temp table with only a single digit number of rows. I found a way to reproduce it, and it seems to me like the optimizer cannot do better since the statistics says it can early-out a merge-join because the maximum value is missing from the stats. Below is the data, statistics an plan for the bad merge join as well as the expected nested loop. Intuitively, I'd say pg_statistic can represent such a small dataset perfectly. But instead, most_common_values only contains one of the two distinct values. This seems like a bug to me. I looked at the code for why this is the case, but couldn't find an answer right-away. Any input? The customer uses latest 9.6, the testcase was against 10.0. regards Andreas =3D# create table big(c int primary key); =3D# insert into big select generate_series(1,1000000); =3D# analyze big; =3D# create table small(c int); =3D# insert into small values (10), (10), (800000); =3D# analyze verbose small; =3D# explain analyze select * from big b join small s on (b.c=3Ds.c); QUERY PLAN ---------------------------------------------------------------------------= ----------------------------------------------------------------- Merge Join (cost=3D1.49..1.82 rows=3D3 width=3D8) (actual time=3D0.110..1= 51.939 rows=3D3 loops=3D1) Merge Cond: (b.c =3D s.c) -> Index Only Scan using big_pkey on big b (cost=3D0.42..30408.42 rows= =3D1000000 width=3D4) (actual time=3D0.067..122.192 rows=3D800001 loops=3D1) Heap Fetches: 800001 -> Sort (cost=3D1.05..1.06 rows=3D3 width=3D4) (actual time=3D0.027..0= .028 rows=3D3 loops=3D1) Sort Key: s.c Sort Method: quicksort Memory: 25kB -> Seq Scan on small s (cost=3D0.00..1.03 rows=3D3 width=3D4) (a= ctual time=3D0.009..0.011 rows=3D3 loops=3D1) Planning time: 0.514 ms Execution time: 151.989 ms (10 rows) =3D# select * from pg_stats where tablename=3D'small' \gx -[ RECORD 1 ]----------+----------- schemaname | public tablename | small attname | c inherited | f null_frac | 0 avg_width | 4 n_distinct | -0.666667 most_common_vals | {10} -- here, I expected to see 800000 as well most_common_freqs | {0.666667} histogram_bounds | =E2=8A=A5 correlation | 1 most_common_elems | =E2=8A=A5 most_common_elem_freqs | =E2=8A=A5 elem_count_histogram | =E2=8A=A5 =3D# insert into small values (800000); =3D# analyze verbose small; =3D# explain analyze select * from big b join small s on (b.c=3Ds.c); QUERY PLAN ---------------------------------------------------------------------------= ------------------------------------------------ Nested Loop (cost=3D0.42..34.81 rows=3D4 width=3D8) (actual time=3D0.037.= .0.068 rows=3D4 loops=3D1) -> Seq Scan on small s (cost=3D0.00..1.04 rows=3D4 width=3D4) (actual = time=3D0.012..0.015 rows=3D4 loops=3D1) -> Index Only Scan using big_pkey on big b (cost=3D0.42..8.44 rows=3D1= width=3D4) (actual time=3D0.010..0.010 rows=3D1 loops=3D4) Index Cond: (c =3D s.c) Heap Fetches: 4 Planning time: 0.478 ms Execution time: 0.133 ms (7 rows) =3D# select * from pg_stats where tablename=3D'small' \gx -[ RECORD 1 ]----------+------------ schemaname | public tablename | small attname | c inherited | f null_frac | 0 avg_width | 4 n_distinct | -0.5 most_common_vals | {10,800000} most_common_freqs | {0.5,0.5} histogram_bounds | =E2=8A=A5 correlation | 1 most_common_elems | =E2=8A=A5 most_common_elem_freqs | =E2=8A=A5 elem_count_histogram | =E2=8A=A5