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 1iyizF-000438-25 for pgsql-performance@arkaria.postgresql.org; Mon, 03 Feb 2020 21:10:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iyizD-0005DC-KH for pgsql-performance@arkaria.postgresql.org; Mon, 03 Feb 2020 21:10:27 +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 1iyizD-0005D0-BG for pgsql-performance@lists.postgresql.org; Mon, 03 Feb 2020 21:10:27 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iyizA-00044p-3M for pgsql-performance@lists.postgresql.org; Mon, 03 Feb 2020 21:10:26 +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 013LAKGW028149; Mon, 3 Feb 2020 16:10:20 -0500 From: Tom Lane To: Benny Kramek cc: pgsql-performance@lists.postgresql.org Subject: Re: Slow performance with trivial self-joins In-reply-to: References: Comments: In-reply-to Benny Kramek message dated "Mon, 03 Feb 2020 15:38:15 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <28147.1580764220.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 03 Feb 2020 16:10:20 -0500 Message-ID: <28148.1580764220@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Benny Kramek writes: > I expect the query plan to be identical for both of the below queries (a= nd I > expect the performance to also be identical). [ shrug... ] Your expectation is mistaken. There is no code in Postgres to eliminate useless self-joins. People have been fooling around with a patch to do so [1], but I'm unsure whether it'll ever get committed, or whether we even want the feature. It seems not unlikely that the time wasted trying to identify useless self-joins (in queries where the optimization doesn't actually apply) would outweigh the win when it does apply. So there's a limit to how much effort the server should spend trying to clean up after poorly-written queries. regards, tom lane [1] https://www.postgresql.org/message-id/flat/64486b0b-0404-e39e-322d-080= 1154901f3@postgrespro.ru