Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lMd4w-0000ph-ID for pgsql-performance@arkaria.postgresql.org; Wed, 17 Mar 2021 20:47:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lMd4u-0003RG-IK for pgsql-performance@arkaria.postgresql.org; Wed, 17 Mar 2021 20:47:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lMd4u-0003R8-9t for pgsql-performance@lists.postgresql.org; Wed, 17 Mar 2021 20:47:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lMd4s-0008D7-4z for pgsql-performance@postgresql.org; Wed, 17 Mar 2021 20:47:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 12HKlZAn3610143; Wed, 17 Mar 2021 16:47:35 -0400 From: Tom Lane To: Marcin Gozdalik cc: pgsql-performance@postgresql.org Subject: Re: Extremely inefficient merge-join In-reply-to: References: Comments: In-reply-to Marcin Gozdalik message dated "Wed, 17 Mar 2021 16:10:14 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3610141.1616014055.1@sss.pgh.pa.us> Date: Wed, 17 Mar 2021 16:47:35 -0400 Message-ID: <3610142.1616014055@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Marcin Gozdalik writes: > Sometimes Postgres will choose very inefficient plan, which involves > looping many times over same rows, producing hundreds of millions or > billions of rows: Yeah, this can happen if the outer side of the join has a lot of duplicate rows. The query planner is aware of that effect and will charge an increased cost when it applies, so I wonder if your statistics for the tables being joined are up-to-date. regards, tom lane