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 1tcLjx-00H2Uo-Mx for pgsql-performance@arkaria.postgresql.org; Mon, 27 Jan 2025 09:49:10 +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 1tcLjw-00B4kj-Pi for pgsql-performance@arkaria.postgresql.org; Mon, 27 Jan 2025 09:49:08 +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 1tcLjw-00B4ka-FG for pgsql-performance@lists.postgresql.org; Mon, 27 Jan 2025 09:49:08 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tcLjt-001lJs-14 for pgsql-performance@postgresql.org; Mon, 27 Jan 2025 09:49:07 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-2166f1e589cso107636655ad.3 for ; Mon, 27 Jan 2025 01:49:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737971344; x=1738576144; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=dP4hzsSoECu2ZFxooVLQABLz3hg1idAcI5d6aa+nOsY=; b=QvX5Z0sTwlQkjLgh+nIK3aXgO4kHskj7pZ6iqif/8s68XQNbJgj9whIfWbl0zftvcX DXRBwskj3MsPNwiFEgJ85voFtUkRdkL16Elxmqb5jPpjOT4Uz81jD6HcTNAGwBBVEZ9c pumdAS/5G0e0T0mfAgzbdxywCfbAqogZz/Ca1ja+6NRaTUcFVCsmZKkUEC89EGOX6MBO ev+nZbAczpLuO7MHNBNHrf3l2O2T4G0d2QQQsdVLQlOtP33vC2F8MMB60qJTH2CKiHQC EfNs+6KbzouTgQM4dQZTrW/O9OSAcmKmecdDXsWjw/gK9qGP3VbdnhD769L99xADpteK f6ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737971344; x=1738576144; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dP4hzsSoECu2ZFxooVLQABLz3hg1idAcI5d6aa+nOsY=; b=CC1C0NpRtXrqo2H3QCiZunUV453t23tmGfiP0rq/EGSmYDItL7LTlEDXBVmBm/n/VT oJPP9onea81uRAntp7Gmsmyt5L7f7+yPJFG60eistC4GlqXEg+mF5SGuOzmb2NJRfi3f W4pKdnSRGLprmFHHllORjBSmPPSMMiWW7xKkE/m5l5YUYSkf/ys5LqVibZczAT99tks9 lex6ElFY9Nn34zwZLlp6/fUlSfcsSQYThp0X2q2NPWPj9evFQgtB1pKTWG90hwQI50j0 boHlg2NbYIvrOTV8tmLfCaYVL2ggUj8djiFUId9Zup4MRsX7QEBiLpP/9aqF8yObHr7I g7sQ== X-Forwarded-Encrypted: i=1; AJvYcCXfOXa6OHMYP48+QyqM6CvyZM5pp+buQxinCFxQdVlpHUv8MDdrXTRqVYjj7N+Vd31tqHMSpoDQf03QFg8CSeJX/g==@postgresql.org X-Gm-Message-State: AOJu0Yzb7AmMp7wxxAY2U+DFtd0xUd8s+GQAorGO2/8es/t8Uu7vyEGY OQHY5iYipJHI6KCaWVFvoo50EJJO2eX3Qny6tX2ozYPHQhuEe3ndOhY9dlAgq4GZKyUdsZ37kzG kLgOGB9E66ALLYJEFKcgDg5MYiqbqU7bN X-Gm-Gg: ASbGncvHhCXwKvqy/1M8V6yW7jkjtQkES+8uUtHvfjnDIMaTpyv23qa13MYgORknvgY X+XrDq2nRHnpLbBVNAGKAZfiz3IcjJjhMNNt+VdX8F0MZQzlA0hMCuP32cjPZ X-Google-Smtp-Source: AGHT+IGQpn6w7+b4RWCEKY2IZ3T/KeRxsauVKjVlgg4eGTSe03CvtlwE+BVMXhiuS507vKlnGUj42t9ltTz873wmjS4= X-Received: by 2002:a17:902:e546:b0:216:281f:820d with SMTP id d9443c01a7336-21c3553b6famr642023455ad.11.1737971343782; Mon, 27 Jan 2025 01:49:03 -0800 (PST) MIME-Version: 1.0 References: <1903318.1736451593@sss.pgh.pa.us> In-Reply-To: <1903318.1736451593@sss.pgh.pa.us> From: Feike Steenbergen Date: Mon, 27 Jan 2025 10:48:52 +0100 X-Gm-Features: AWEUYZnaRYyWPaik3SmD0FDgpDOyHZjr3DVIiR8WoG9EC2q9vrtZmNXJvhu_W8E Message-ID: Subject: Re: Unexpected Seq Scan's when using MERGE WHEN NOT MATCHED BY SOURCE To: Tom Lane Cc: David Mullineux , pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary="00000000000093791e062cacfc92" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000093791e062cacfc92 Content-Type: text/plain; charset="UTF-8" On Thu, 9 Jan 2025 at 20:39, Tom Lane wrote: > 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. > Not sure if this pattern is common enough to be worth trying to implement such > an optimization. I'm not going to pursue this much further, however for those reading along/ future reference, what we're trying to do is: - for a given target table - merge a subset of the data using a source table (only update any significant changes). The subset filter `WHERE device_id=$1` is applied to both the JOIN as well as the NOT MATCHED BY SOURCE part of the merge. - when using MERGE ... NOT MATCHED BY SOURCE THEN DELETE currently (pg17) reads the whole of the target table before applying the subset filter. As we plan to merge only very small subsets (1/10,000 or so), this means that for now, this isn't usable for that use case. For example, for a monitoring system, we have 10's of thousands of remote systems, for which we want to merge a snapshot of their current state inside a central target table. That use case is currently not well supported with MERGE. (The workaround is a DELETE inside a CTE). I would expect this use case to be quite common, however, as this feature is only available in PG17, it may not be used yet with MERGE, so I don't expect any others to voice the same concern. --00000000000093791e062cacfc92 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Thu, 9 Jan 2025 at 20:39, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> = Yeah. That prevents pushing down the join condition, since in a FULL join a= ll
> rows of both sides will contribute to the result; none can be re= moved ahead of
> the join.

> Not sure if this pattern is co= mmon enough to be worth trying to implement such
> an optimization.
I'm not going to pursue this much further, however for those read= ing along/
future reference, what we're trying to do is:

- fo= r a given target table
- merge a subset of the data using a source table= (only update any significant
changes). The subset filter `WHERE device_= id=3D$1` is applied to both the JOIN
as well as the NOT MATCHED BY SOURC= E part of the merge.
- when using MERGE ... NOT MATCHED BY SOURCE THEN D= ELETE currently (pg17) reads
the whole of the target table before applyi= ng the subset filter. As we plan to
merge only very small subsets (1/10,= 000 or so), this means that for now, this
isn't usable for that use = case.

For example, for a monitoring system, we have 10's of thou= sands of remote
systems, for which we want to merge a snapshot of their = current state inside a
central target table. That use case is currently = not well supported with MERGE.
(The workaround is a DELETE inside a CTE)= .

I would expect this use case to be quite common, however, as this = feature is
only available in PG17, it may not be used yet with MERGE, so= I don't expect
any others to voice the same concern.

--00000000000093791e062cacfc92--