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 1mqhAB-0005mY-PO for pgsql-general@arkaria.postgresql.org; Fri, 26 Nov 2021 19:45:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mqh9f-0007Ak-9H for pgsql-general@arkaria.postgresql.org; Fri, 26 Nov 2021 19:45:07 +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 1mqh9H-000112-TJ for pgsql-general@lists.postgresql.org; Fri, 26 Nov 2021 19:44:44 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mqh9F-0007Vm-7W for pgsql-general@lists.postgresql.org; Fri, 26 Nov 2021 19:44:42 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id E67285C0154; Fri, 26 Nov 2021 14:44:39 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Fri, 26 Nov 2021 14:44:39 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=YFA9THIZ26k+zgshy4i+C/cULYHLE3MJ9VxNO6MFYL0=; b=Tt3UFHUm sRGWF8rjVsU0bmA5uYM+YtNg6ZlGUb4I/auqrqBGIcfTKbtjikp9JZmR1yLLsl9K 3Hizo2V1dro63dE806zM0qc0H4P09yekMKCPVU2wKqOYul65mEPjcaLuZXkEKzje TldnXce2xE1FY+oSj1CGX+gj3gXUZdIgHhJtPcm04trQ3SihTQ+unI/xzxAs6NxG LBHhTy6vTzAT8Get21scdQRUcgl71hV7dTmXZ6eplHM91kxuN1kdQWMHFbPsBJ47 oLV+2TWMCnStrQ2w5jOfKjrts83rP0iJXe4On+Lre6M68cA50xm9GPO9521OPGOU o6955VvT1sfq0A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvuddrhedvgdduvdejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfggtggugfgjsehtkeertddttdejnecuhfhrohhmpeetlhhvrghr ohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgqe enucggtffrrghtthgvrhhnpeejjeevffeukeejieekheehleduudehteeiudegteetkefh veelhfefvdduvdfgfeenucffohhmrghinhepphhoshhtghhrrdgvshdpvghnthgvrhhprh hishgvuggsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghi lhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 26 Nov 2021 14:44:39 -0500 (EST) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 8CF452A07D2; Fri, 26 Nov 2021 16:44:36 -0300 (-03) Date: Fri, 26 Nov 2021 16:44:36 -0300 From: Alvaro Herrera To: Shaozhong SHI Cc: pgsql-general Subject: Re: Merge into does not work Message-ID: <202111261944.siaofq7of32m@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 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 On 2021-Nov-26, Shaozhong SHI wrote: > MERGE INTO Stock USING Buy ON Stock.item_id = Buy.item_id > WHEN MATCHED THEN UPDATE SET balance = balance + Buy.volume > WHEN NOT MATCHED THEN INSERT VALUES (Buy.item_id, Buy.volume); It does work for me: 55479 15devel 680346=# MERGE INTO Stock USING Buy ON Stock.item_id = Buy.item_id WHEN MATCHED THEN UPDATE SET balance = balance + Buy.volume WHEN NOT MATCHED THEN INSERT VALUES (Buy.item_id, Buy.volume); MERGE 2 Duración: 3,879 ms 55479 15devel 680346=# select * from stock; item_id │ balance ─────────┼───────── 20 │ 1900 10 │ 3200 30 │ 300 (3 filas) > I am using Postgres 9.6. I am using the MERGE patch I posted here[1], on top of Postgres 15. https://postgr.es/m/202111152245.byerxxac444d@alvherre.pgsql -- Álvaro Herrera Valdivia, Chile — https://www.EnterpriseDB.com/ "Pido que me den el Nobel por razones humanitarias" (Nicanor Parra)