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 1qleB7-003lDm-2o for pgsql-docs@arkaria.postgresql.org; Wed, 27 Sep 2023 23:42:49 +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 1qleB5-006X6W-83 for pgsql-docs@arkaria.postgresql.org; Wed, 27 Sep 2023 23:42:47 +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 1qleB4-006X6F-Vr for pgsql-docs@lists.postgresql.org; Wed, 27 Sep 2023 23:42:47 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qleB2-007yzc-8N for pgsql-docs@lists.postgresql.org; Wed, 27 Sep 2023 23:42:46 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=momjian.us; s=2023062407; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-ID:Content-Description; bh=cfH613C+cZMcZN0Ae66tf4r2wDvMvRYq+JkPMdibUHw=; b=hmmstcHBxOjjnv3b4Ji478Qlun ml5ptTSeJ5HpCRI27KK3K56+G0iopormkA8HtgjsdC253yzkskwT1C5qu70xJgxfvcmoMrsVIP666 HQXygeFDFJngIE+cyoXD4NMupoOq2O4ba2HiNOPbEK9Dco0tihP7ULEcS3AQJESd+k17wXey7MEHR HRUIWHBNdqW9tuMluBL7phUiDgcVYlfQRImBK6s0UYRyYBIYI3R7SjR7ySQpUZkLy4BiDLnUY2pmr 42F83XvZz2EW3q2TjppGebUv2uz23m+A8nvgYInM+3h/cT62dITT9R2JbAuBC/ItCt+KQpWYqSMlF vZTRMmMw==; Received: from bruce by momjian.us with local (Exim 4.96) (envelope-from ) id 1qleB0-0077cU-0O; Wed, 27 Sep 2023 19:42:42 -0400 Date: Wed, 27 Sep 2023 19:42:42 -0400 From: Bruce Momjian To: "David G. Johnston" Cc: dwayne.towell@gmail.com, pgsql-docs@lists.postgresql.org Subject: Re: MERGE examples not clear Message-ID: References: <167699245721.1902146.6479762301617101634@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="xHvpDtqq0WSJtx0g" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --xHvpDtqq0WSJtx0g Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Tue, Feb 21, 2023 at 08:56:50AM -0700, David G. Johnston wrote: > On Tue, Feb 21, 2023 at 8:35 AM PG Doc comments form > wrote: > > The following documentation comment has been logged on the website: > > Page: https://www.postgresql.org/docs/15/sql-merge.html > Description: > > On this page: https://www.postgresql.org/docs/15/sql-merge.html > the first and second examples seems to be contrasted (by "this would be > exactly equivalent to the following statement"), however the difference > does > not seem to related to the stated reason ("the MATCHED result does not > change"). It seems like the difference should involve the order of WHEN > clauses? > Of course, it might be that I don't understand the point, in which case > maybe the point could be stated more clearly? > > > Yeah, that is a pretty poor pair of examples.  Given that a given customer can > reasonably be assumed to have more than one recent transaction the MERGE has a > good chance of failing. > > The only difference between the two is the second one uses an explicit subquery > as the source while the first simply names a table.  If the subquery had a > GROUP BY customer_id that would be a good change explaining that the second > query is different because it is resilient in the face of duplicate customer > recent transactions. > > While here...source_alias (...completely hides...the fact that a query was > issued).  What?  Probably it should read (not verified) that it is actually > required when the source is a query (maybe tweaking the syntax to match). The attached patch removes the second example, which doesn't seem to add much. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Only you can decide what is important to you. --xHvpDtqq0WSJtx0g Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="merge.diff" diff --git a/doc/src/sgml/ref/merge.sgml b/doc/src/sgml/ref/merge.sgml index 0995fe0c04..4544ce92b3 100644 --- a/doc/src/sgml/ref/merge.sgml +++ b/doc/src/sgml/ref/merge.sgml @@ -582,23 +582,6 @@ WHEN NOT MATCHED THEN - - Notice that this would be exactly equivalent to the following - statement because the MATCHED result does not change - during execution. - - -MERGE INTO customer_account ca -USING (SELECT customer_id, transaction_value FROM recent_transactions) AS t -ON t.customer_id = ca.customer_id -WHEN MATCHED THEN - UPDATE SET balance = balance + transaction_value -WHEN NOT MATCHED THEN - INSERT (customer_id, balance) - VALUES (t.customer_id, t.transaction_value); - - - Attempt to insert a new stock item along with the quantity of stock. If the item already exists, instead update the stock count of the existing --xHvpDtqq0WSJtx0g--