Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpwtz-0001te-Pk for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Feb 2019 15:08:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gpwtx-0006V7-Tt for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Feb 2019 15:08:13 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpwtx-0006V0-NU for pgsql-bugs@lists.postgresql.org; Sat, 02 Feb 2019 15:08:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpwtv-0008TV-EV for pgsql-bugs@lists.postgresql.org; Sat, 02 Feb 2019 15:08:13 +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 x12F85pW023838; Sat, 2 Feb 2019 10:08:05 -0500 From: Tom Lane To: Alex cc: pgsql-bugs@lists.postgresql.org, Pavel Stehule Subject: Re: BUG #15618: Duplicating a join clause breaks index use In-reply-to: <378349148.1969803.1549109284822@mail.yahoo.com> References: <15618-5357e448d7cbeb27@postgresql.org> <378349148.1969803.1549109284822@mail.yahoo.com> Comments: In-reply-to Alex message dated "Sat, 02 Feb 2019 12:08:04 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <23836.1549120085.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 02 Feb 2019 10:08:05 -0500 Message-ID: <23837.1549120085@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alex writes: > select > ... > from > =C2=A0=C2=A0=C2=A0 product p > =C2=A0=C2=A0=C2=A0 left outer join product_property pp on p.id=3Dpp.prod= uct_id > =C2=A0=C2=A0=C2=A0 left outer join product_property_value ppv on pp.valu= e_id=3Dppv.id > =C2=A0=C2=A0=C2=A0 left outer join description_element de on pp.descript= ion_source=3Dde.id > =C2=A0=C2=A0=C2=A0 left outer join description_value dev on de.descripti= on_value_id=3Ddev.id > =C2=A0=C2=A0=C2=A0 left outer join description_element dep on de.parent=3D= dep.id > =C2=A0=C2=A0=C2=A0 left outer join description_value depv on dep.descrip= tion_value_id=3Ddepv.id > =C2=A0=C2=A0=C2=A0 left outer join shop_category sc_dup on p.shop_catego= ry=3Dsc_dup.id=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= --duplicate join > =C2=A0=C2=A0=C2=A0 left outer join shop_category sc on p.shop_category=3D= sc.id > =C2=A0=C2=A0=C2=A0 left outer join category c on sc.category=3Dc.id > where > =C2=A0=C2=A0=C2=A0 c.id=3D8511 I suspect the real issue here has little to do with the "duplicate" nature of the extra join, and a lot to do with the fact that your query exceeds join_collapse_limit. That causes the planner to break it up in a semi-arbitrary way to limit planning time. Adding one more join moves where the break happens, and evidently is preventing the planner from discovering a good join order. You want join_collapse_limit to be at least 10 to handle this query well, since 10 tables are mentioned. The default value, 8, is kinda tuned for older and slower machines than most of us use today. I wouldn't make it enormous, because it does prevent exponential planning time growth for many tables, but you could likely get away with 15 or so. You should likely keep from_collapse_limit in sync with that too. regards, tom lane