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 1nbrUu-0006lj-Mz for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Apr 2022 22:18:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nbrUs-0002Vr-Ha for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Apr 2022 22:17:58 +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 1nbrUq-0002Vc-Pi for pgsql-hackers@lists.postgresql.org; Tue, 05 Apr 2022 22:17:58 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nbrUn-0004gC-L0 for pgsql-hackers@lists.postgresql.org; Tue, 05 Apr 2022 22:17:55 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 382D65C0079 for ; Tue, 5 Apr 2022 18:17:51 -0400 (EDT) Received: from imap46 ([10.202.2.96]) by compute1.internal (MEProxy); Tue, 05 Apr 2022 18:17:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lateraljoin.com; h=cc:content-type:date:date:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to; s=fm3; bh=I 5zxOIXMuTUaKHsWIOoWcV3J5RkKsVL+kHS3iG58GGA=; b=j+r34WFbjcKLJ78Gq ++yEcJ3dAqVi2O6wQRFyru9+kvoGRigVToUM6zZnYaK+TE66ExSKApjw18SV4eqw NAgZ2FJqmP9FfQgID7gE8UBsZnbGZ1n7KciIwxZuraTtWuNxI55Aln+qyXYaYb22 dqAQXKQOkM5JQhSk156PzIXUipc58Wbh/fcwLOUGtQZmF2et80jVGrMiiIz98VlD egJavFaiSBEll5GhYfL7Rzp0+GvSkOH7rPyIsuFxASYHNtkIqNd5RU4iN7KviZf4 pEKmgDQ/R25lM58nAHAiMAKZlwLU6XvQLUR2cJiMOCx3mwxGEGXH36sSgs+U17+W COeEA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:date:from:from :in-reply-to:message-id:mime-version:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=I5zxOIXMuTUaKHsWIOoWcV3J5RkKsVL+kHS3iG58G GA=; b=ikWkzlpNok5Klbxqt1vi7sJ41OZqKhuovYv9QRhNJeD9/R3XaL62HfQGg TA//NF3dR2e86TyKNfhgrw1TSGvQuAQeKWpGtnIvVUju0nCzr/th+qgiQ7MF0547 yDop7pYGDDchyZOgAwPaCH7o9Utl3YeCq/e7KhkEaqN3txl28BJx+Ps1+mCY3/M/ dYo8JPyKtIpxmEEtRTkxncAtYqpTSs5wCuQz28Frcsw5Ue0kWNMnIkMNqF6yDX9+ eAgZP0NA1PFFTSXwQMcbOBdHpx/3SNqQT20QrMygrHgU76sBTjD88NyrtW7nMegx mz951L9Cp958FqcryPPSi1/NP7E8w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrudejhedgtdejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfffhffvufgtsehttdertd erredtnecuhfhrohhmpedflfhovgcuhghilhguihhshhdfuceojhhovgeslhgrthgvrhgr lhhjohhinhdrtghomheqnecuggftrfgrthhtvghrnhepfffhjeefvdeghfekveejueekvd ffteegvdehgfegteetffetleegteelhefhgeelnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomhepjhhovgeslhgrthgvrhgrlhhjohhinhdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 9FD111EE007B; Tue, 5 Apr 2022 18:17:50 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.7.0-alpha0-385-g3a17909f9e-fm-20220404.001-g3a17909f Mime-Version: 1.0 Message-Id: Date: Tue, 05 Apr 2022 23:17:30 +0100 From: "Joe Wildish" To: pgsql-hackers@lists.postgresql.org Subject: MERGE bug report Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello Hackers, Reporting a bug with the new MERGE statement. Tested against 75edb919613ee835e7680e40137e494c7856bcf9. psql output as follows: ... psql:merge.sql:33: ERROR: variable not found in subplan target lists ROLLBACK [local] joe@joe=# \errverbose ERROR: XX000: variable not found in subplan target lists LOCATION: fix_join_expr_mutator, setrefs.c:2800 Stack trace: fix_join_expr_mutator setrefs.c:2800 expression_tree_mutator nodeFuncs.c:3348 fix_join_expr_mutator setrefs.c:2853 expression_tree_mutator nodeFuncs.c:2992 fix_join_expr_mutator setrefs.c:2853 expression_tree_mutator nodeFuncs.c:3348 fix_join_expr_mutator setrefs.c:2853 fix_join_expr setrefs.c:2753 set_plan_refs setrefs.c:1085 set_plan_references setrefs.c:315 standard_planner planner.c:498 planner planner.c:277 pg_plan_query postgres.c:883 pg_plan_queries postgres.c:975 exec_simple_query postgres.c:1169 PostgresMain postgres.c:4520 BackendRun postmaster.c:4593 BackendStartup postmaster.c:4321 ServerLoop postmaster.c:1801 PostmasterMain postmaster.c:1473 main main.c:202 __libc_start_main 0x00007fc4ccc0b1e2 _start 0x000000000048804e Reproducer script: BEGIN; DROP TABLE IF EXISTS item, incoming, source CASCADE; CREATE TABLE item (order_id INTEGER NOT NULL, item_id INTEGER NOT NULL, quantity INTEGER NOT NULL, price NUMERIC NOT NULL, CONSTRAINT pk_item PRIMARY KEY (order_id, item_id)); INSERT INTO item VALUES (100, 1, 4, 100.00), (100, 2, 9, 199.00); CREATE TABLE incoming (order_id, item_id, quantity, price) AS (VALUES (100, 1, 4, 100.00), (100, 3, 1, 200.00)); CREATE TABLE source (order_id, item_id, quantity, price) AS (SELECT order_id, item_id, incoming.quantity, incoming.price FROM item LEFT JOIN incoming USING (order_id, item_id)); MERGE INTO item a USING source b ON (a.order_id, a.item_id) = (b.order_id, b.item_id) WHEN NOT MATCHED THEN INSERT (order_id, item_id, quantity, price) VALUES (order_id, item_id, quantity, price) WHEN MATCHED AND a.* IS DISTINCT FROM b.* THEN UPDATE SET (quantity, price) = (b.quantity, b.price) WHEN MATCHED AND (b.quantity IS NULL AND b.price IS NULL) THEN DELETE; COMMIT; It seems related to the use of a.* and b.* Sorry I can't be more specific. Error manifests when planning occurs and that is well outside of my code base knowledge. Hope this helps. Cheers, -Joe