Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tVyNs-00D7I9-HS for pgsql-performance@arkaria.postgresql.org; Thu, 09 Jan 2025 19:40:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tVyNr-0076ue-Ny for pgsql-performance@arkaria.postgresql.org; Thu, 09 Jan 2025 19:39:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tVyNr-0076t8-DU for pgsql-performance@lists.postgresql.org; Thu, 09 Jan 2025 19:39:59 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tVyNn-000mn7-2D for pgsql-performance@postgresql.org; Thu, 09 Jan 2025 19:39:58 +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 509JdriL1903319; Thu, 9 Jan 2025 14:39:53 -0500 From: Tom Lane To: David Mullineux cc: Feike Steenbergen , pgsql-performance@postgresql.org Subject: Re: Unexpected Seq Scan's when using MERGE WHEN NOT MATCHED BY SOURCE In-reply-to: References: Comments: In-reply-to David Mullineux message dated "Thu, 09 Jan 2025 15:52:45 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1903317.1736451593.1@sss.pgh.pa.us> Date: Thu, 09 Jan 2025 14:39:53 -0500 Message-ID: <1903318.1736451593@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Mullineux writes: > On Thu, 9 Jan 2025, 12:26 Feike Steenbergen, > wrote: >> If we run this however, there is always a Seq Scan against merge_target, >> whereas the filter of `AND t.device_id = $1` uses a (Bitmap) Index scan >> in other types of queries. > I note ,in the documentation, that a Warning box got added which says > this... >> If both WHEN NOT MATCHED BY SOURCE and WHEN NOT MATCHED [BY TARGET] clauses >> are specified, the MERGE command will perform a FULL join between >> data_source and the target table. Yeah. That prevents pushing down the join condition, since in a FULL join all rows of both sides will contribute to the result; none can be removed ahead of the join. I may not have fully wrapped my head around this example, but I think that the fact that "t.device_id = $1" appears in both the ON condition and the WHEN NOT MATCHED BY SOURCE condition means that only t rows meeting that condition are of interest, so that in principle we could optimize by pushing that down to the scan of t. But as you can see, we don't. Not sure if this pattern is common enough to be worth trying to implement such an optimization. regards, tom lane