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 1fEaor-00071g-Ne for pgsql-general@arkaria.postgresql.org; Fri, 04 May 2018 13:32:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fEaoq-000762-FI for pgsql-general@arkaria.postgresql.org; Fri, 04 May 2018 13:32:16 +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 1fEaoq-00075s-75 for pgsql-general@lists.postgresql.org; Fri, 04 May 2018 13:32:16 +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.89) (envelope-from ) id 1fEaon-0002PG-0h for pgsql-general@postgresql.org; Fri, 04 May 2018 13:32:15 +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 w44DW885025194; Fri, 4 May 2018 09:32:09 -0400 From: Tom Lane To: Markus cc: pgsql-general@postgresql.org Subject: Re: Query planner riddle (array-related?) In-reply-to: <20180504080913.33yx46gkx3jfaflf@victor> References: <20180504080913.33yx46gkx3jfaflf@victor> Comments: In-reply-to Markus message dated "Fri, 04 May 2018 10:09:13 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <25192.1525440728.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 04 May 2018 09:32:08 -0400 Message-ID: <25193.1525440728@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Markus writes: > I'm running Postgresql 9.6.7 on Debian stretch -- and I'm trying to > understand a query plan, with any hint where to gain further insight > welcome. Well, you say > select count(*) from gaia.dr2light where parallax>50; > gives 5400 rows in no time. but the planner thinks there are 12991627 such rows: > -> Bitmap Heap Scan on dr2light (cost=3D243173.69..25288015.7= 4 rows=3D12991627 width=3D132) > Recheck Cond: (parallax > '50'::double precision) > -> Bitmap Index Scan on dr2light_parallax (cost=3D0.00.= .239925.78 rows=3D12991627 width=3D0) > Index Cond: (parallax > '50'::double precision) So my first instinct would be to try to get that estimate more in line with reality. Maybe you need to increase the statistics target for that column. Also, this sort of thing is usually much easier to diagnose from EXPLAIN ANALYZE output. All we can see from these queries is that the planner picked what it thought was the lowest-cost plan. Without actual rowcounts it's very hard to guess why the estimates were wrong. You happened to provide one actual-rowcount number that maybe was enough to diagnose the issue; but if the above doesn't do the trick, we're going to need to see EXPLAIN ANALYZE to guess what else is up. regards, tom lane > -> Hash (cost=3D118285.38..118285.38 rows=3D551038 width=3D1523) > -> Seq Scan on dr2epochflux (cost=3D0.00..118285.38 rows=3D55= 1038 width=3D1523) > And here's the bad plan (for query 1): > ------------------------------------------------------------------------= -------------- > Nested Loop (cost=3D0.58..4801856.96 rows=3D4229 width=3D1647) > -> Seq Scan on dr2epochflux (cost=3D0.00..118285.38 rows=3D551038 w= idth=3D1523) > -> Index Scan using dr2light_pkey on dr2light (cost=3D0.58..8.49 ro= ws=3D1 width=3D132) > Index Cond: (source_id =3D dr2epochflux.source_id) > Filter: (parallax > '50'::double precision) > If I enable_seqscan=3D0, it comes up with this for query 1: > ------------------------------------------------------------------------= --------------------------------- > Nested Loop (cost=3D1.00..4810726.18 rows=3D4229 width=3D1647) > -> Index Scan using dr2epochflux_pkey on dr2epochflux (cost=3D0.42.= .127154.60 rows=3D551038 width=3D1523) > -> Index Scan using dr2light_pkey on dr2light (cost=3D0.58..8.49 ro= ws=3D1 width=3D132) > Index Cond: (source_id =3D dr2epochflux.source_id) > Filter: (parallax > '50'::double precision) > -- which in reality appears to be a good deal faster than the "bad" > plan, though still much, much slower than the "good plan". > Both tables are ANALYZE-d, and they should be reasonably VACUUMED. > Is there anything I can do to make it easier for the planner to see the > light? > -- Markus