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 1oKsOy-0001MY-3C for pgsql-bugs@arkaria.postgresql.org; Mon, 08 Aug 2022 02:21:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oKsOx-0001g0-13 for pgsql-bugs@arkaria.postgresql.org; Mon, 08 Aug 2022 02:21:55 +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 1oKsOw-0001fp-OL for pgsql-bugs@lists.postgresql.org; Mon, 08 Aug 2022 02:21:54 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oKsOq-0003vJ-6A for pgsql-bugs@lists.postgresql.org; Mon, 08 Aug 2022 02:21:53 +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 2782L7IM304362; Sun, 7 Aug 2022 22:21:09 -0400 From: Tom Lane To: borz_off@cs.msu.su cc: pgsql-bugs@lists.postgresql.org, Alvaro Herrera Subject: Re: BUG #17579: 15beta2: strange error when trying to use MERGE statement as a CTE In-reply-to: <17579-82482cd7b267b862@postgresql.org> References: <17579-82482cd7b267b862@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Sun, 07 Aug 2022 21:44:25 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <304360.1659925267.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 07 Aug 2022 22:21:07 -0400 Message-ID: <304361.1659925267@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > create table foo (id int); > with cte_failure as ( > merge into foo as target > using foo as source > on target.id =3D source.id > when matched then do nothing > ) > select 'fail!'; > When executing the above code I get the following error: > ERROR: DO INSTEAD NOTIFY rules are not supported for data-modifying > statements in WITH With asserts on, it fails in the parser: TRAP: FailedAssertion("IsA(cte->ctequery, InsertStmt) || IsA(cte->ctequery= , UpdateStmt) || IsA(cte->ctequery, DeleteStmt)", File: "parse_cte.c", Lin= e: 149, PID: 303950) postgres: postgres regression [local] SELECT(ExceptionalCondition+0x7c)[0x= 98013c] postgres: postgres regression [local] SELECT(transformWithClause+0x66c)[0x= 6275ec] postgres: postgres regression [local] SELECT(transformStmt+0x10f9)[0x60361= 9] > I suspect that MERGE was never intended to work as a CTE, but right now = the > grammar allows any PreparableStmt in a common_table_expr and the above e= rror > is triggered a lot later due to implementation details. It evidently wasn't ever *tested*, but in principle I think it ought to work. I'm not sure how much effort will be involved to make that happen. At this point we might have to disallow it for v15 and come back to the problem later. regards, tom lane