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 1sT0r9-00CEge-OI for pgsql-bugs@arkaria.postgresql.org; Sun, 14 Jul 2024 15:09:43 +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 1sT0r7-005vjB-Rb for pgsql-bugs@arkaria.postgresql.org; Sun, 14 Jul 2024 15:09:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sT0r7-005vj3-JC for pgsql-bugs@lists.postgresql.org; Sun, 14 Jul 2024 15:09:41 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sT0r5-0021d1-13 for pgsql-bugs@lists.postgresql.org; Sun, 14 Jul 2024 15:09:40 +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 46EF9bBm2781445; Sun, 14 Jul 2024 11:09:37 -0400 From: Tom Lane To: exclusion@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18536: Using WITH inside WITH RECURSIVE triggers a "shouldn't happen" error In-reply-to: <18536-0a342ec07901203e@postgresql.org> References: <18536-0a342ec07901203e@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Sat, 13 Jul 2024 09:00:00 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2781443.1720969777.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 14 Jul 2024 11:09:37 -0400 Message-ID: <2781444.1720969777@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: > The following query: > WITH RECURSIVE t(n) AS ( > WITH t1 AS (SELECT 1 FROM t) SELECT 1 > UNION > SELECT 1 FROM t1) > SELECT * FROM t; That should throw an error, certainly: it's not a valid recursive structure. (Since the inner WITH clause spans the whole "SELECT 1 UNION SELECT 1 FROM t1" structure, we don't have a top- level UNION anymore.) But it shouldn't throw this error: > ERROR: XX000: missing recursive reference > LOCATION: checkWellFormedRecursion, parse_cte.c:896 We do get the right behaviors for WITHs that are down inside one side or the other of the UNION: WITH RECURSIVE t(n) AS ( (WITH t1 AS (SELECT 1 FROM t) SELECT 1 FROM t1) UNION SELECT 1) = SELECT * FROM t; ERROR: recursive reference to query "t" must not appear within its non-re= cursive term LINE 2: (WITH t1 AS (SELECT 1 FROM t) SELECT 1 FROM t1) ^ WITH RECURSIVE t(n) AS ( SELECT 1 UNION (WITH t1 AS (SELECT 1 FROM t) SELECT 1 FROM t1)) SELECT * FROM t; n = --- 1 (1 row) I think the case you show should be throwing ERROR: recursive query "t" does not have the form non-recursive-term UNIO= N [ALL] recursive-term Will look closer later. Thanks for the report. regards, tom lane