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 1fb49B-0007vK-UO for pgsql-general@arkaria.postgresql.org; Thu, 05 Jul 2018 13:18:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fb499-0002wC-U9 for pgsql-general@arkaria.postgresql.org; Thu, 05 Jul 2018 13:18:07 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fb499-0002w5-It for pgsql-general@lists.postgresql.org; Thu, 05 Jul 2018 13:18:07 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fb48w-0005IB-Qo for pgsql-general@postgresql.org; Thu, 05 Jul 2018 13:18:05 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 5E4041C53; Thu, 5 Jul 2018 15:17:51 +0200 (CEST) Date: Thu, 5 Jul 2018 15:17:51 +0200 From: "Peter J. Holzer" To: pgsql-general@postgresql.org Subject: Inefficient plan on 10.4 Message-ID: <20180705131751.so2x3xxemmtr24nj@hjp.at> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="5plbsesg7ejth7xw" Content-Disposition: inline User-Agent: NeoMutt/20170113 (1.7.2) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --5plbsesg7ejth7xw Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I have this table: Table "public.relation" Column | Type | Collation | Nullable | = Default =20 -------------------+-----------------------------+-----------+----------+--= ------------------------------------ parent | integer | | not null |= =20 child | integer | | not null |= =20 type | character varying | | |= =20 sortorder | integer | | |= =20 valid_from | timestamp without time zone | | not null | n= ow() from_job_queue_id | integer | | |= =20 id | integer | | not null | n= extval('relation_id_seq'::regclass) Indexes: "relation_pkey" PRIMARY KEY, btree (id) "relation_child_idx" btree (child) "relation_parent_idx" btree (parent) Foreign-key constraints: "relation_child_fkey" FOREIGN KEY (child) REFERENCES concept(id) DEFERR= ABLE "relation_parent_fkey" FOREIGN KEY (parent) REFERENCES concept(id) DEFE= RRABLE which has about 150 million rows: wdsah=3D> select count(*) from relation; count =20 ----------- 147810590 (1 row) I'm trying to get to get the siblings of a node (including itself): select r2.parent, r2.type, r2.child =66rom relation r1, relation r2 where r1.child=3D643541 and r2.parent=3Dr1.parent order by r2.type This worked fine on 9.5, but on 10.4 it takes several seconds: QU= ERY PLAN = =20 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ------- Gather Merge (cost=3D12032221.68..25195781.12 rows=3D112822632 width=3D15= ) (actual time=3D4086.255..4086.257 rows=3D2 loops=3D1) Workers Planned: 2 Workers Launched: 2 -> Sort (cost=3D12031221.66..12172249.95 rows=3D56411316 width=3D15) (= actual time=3D4080.862..4080.862 rows=3D1 loops=3D3) Sort Key: r2.type Sort Method: quicksort Memory: 25kB -> Nested Loop (cost=3D0.57..1876228.04 rows=3D56411316 width=3D= 15) (actual time=3D3036.547..4080.826 rows=3D1 loops=3D3) -> Parallel Seq Scan on relation r1 (cost=3D0.00..1856722.= 83 rows=3D1 width=3D4) (actual time=3D3036.525..4080.802 rows=3D0 loops=3D3) Filter: (child =3D 643541) Rows Removed by Filter: 49270196 -> Index Scan using relation_parent_idx on relation r2 (co= st=3D0.57..17041.69 rows=3D246351 width=3D15) (actual time=3D0.053..0.055 r= ows=3D2 loops=3D1) Index Cond: (parent =3D r1.parent) Planning time: 0.418 ms Execution time: 4090.442 ms If I disable sequential scans, it works fine again: QUERY= PLAN = =20 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------- Sort (cost=3D25246497.92..25584965.81 rows=3D135387158 width=3D15) (actua= l time=3D0.119..0.119 rows=3D2 loops=3D1) Sort Key: r2.type Sort Method: quicksort Memory: 25kB -> Nested Loop (cost=3D1.14..19513.79 rows=3D135387158 width=3D15) (ac= tual time=3D0.101..0.104 rows=3D2 loops=3D1) -> Index Scan using relation_child_idx on relation r1 (cost=3D0.= 57..8.59 rows=3D1 width=3D4) (actual time=3D0.079..0.080 rows=3D1 loops=3D1) Index Cond: (child =3D 643541) -> Index Scan using relation_parent_idx on relation r2 (cost=3D0= =2E57..17041.69 rows=3D246351 width=3D15) (actual time=3D0.018..0.020 rows= =3D2 loops=3D1) Index Cond: (parent =3D r1.parent) Planning time: 0.446 ms Execution time: 0.165 ms There are several points about these plans that I don't understand: * Why does the nested loop expect 56E6 or even 135E6 rows? It expects 1 row for the outer table and then 246351 rows in the inner table for each of them. 1 * 246351 =3D=3D 246351. So it should expect 246351 rows. (246351 itself is way too high, but the table has a very skewed distribution, and I've already set the statistics target to the maximum of 10000, so there's not much I can do about that) * Why does the Parallel Seq Scan report actual rows=3D0? It did return 1 row (or is that the average per worker? That would be < 1, and probably rounded down to 0) hp PS: The obvious workaround is to remove "order by r2.type". I can easily get the required partial order in the application. But I'd like to understand what the optimizer is doing here. --=20 _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp@hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson --5plbsesg7ejth7xw Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAls+GnkACgkQ8g5IURL+ KF1CAg//TTgVTXCdFoBSelDblrjuu0M+l4eHX2NeXIOLaIn2NCDKjCaw66GPeA3O 9UDXDqOxk+Cgjd+bnRrN8n9yJezriCEtUHCFGrTsja5HPlJTwWGlGAnVJWqcj40S ZJNyV5mgPZpSuFEJVXDkToOEmvnwHZSqc6/ROi1rd1R8zQmTdh0Eeggk8lgWpns+ PVSA+YLhkfg2dTxDFUXw+7w03A2ZDjji26FBhi+KFo6GG0zzbABwgiHE94LVfNXT fIaoeniPmNg7EdH+/vUkjBjDVjDGsXYmgukWMOqnJo5qytFDw/Czh5VwT0fD9EFf VV6dUp/kN5m0JKyN6OjOOsnyHPheskShn65DPyKZXNG83qshcgdl/i3lic2oVl+a cwOn5KM/25/H9WArULXHD0YLa/2kJUdCN/l+EGt6Z/0aFeQ5ENdPlL0O/JobggXK Q70Sjs158NXHIPhOibTcNv6/db6QlshyH2ONtOXXcECeG9OQ5x5hTwhXLy/5BfGP KA+4aQjQVBoOLEpaImRQIivowAfggmPKIUVy3VA3xY+jjPeQJ+7/HFK6C8yPmTgT 4iBee4MYCPzseG+9NdXiTwz70XnV3xQ6OoaFRk7z6FTkQY6/B77o9dlfXegHTRto otxZ/QPxeHWMLWNZ2g4FpJ+8ZJOHmKgkSjGL1fXruxsKx7/O6sI= =eFik -----END PGP SIGNATURE----- --5plbsesg7ejth7xw--