Re: BUG #18536: Using WITH inside WITH RECURSIVE triggers a "shouldn't happen" error

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: exclusion(at)gmail(dot)com
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18536: Using WITH inside WITH RECURSIVE triggers a "shouldn't happen" error
Date: 2024-07-14 15:09:37
Message-ID: 2781444.1720969777@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PG Bug reporting form <noreply(at)postgresql(dot)org> 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-recursive 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 UNION [ALL] recursive-term

Will look closer later. Thanks for the report.

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Vasilii Smirnov 2024-07-14 15:39:22 libpq: unexpected return code from PQexecParams with a DO INSTEAD rule present
Previous Message PG Bug reporting form 2024-07-13 09:00:00 BUG #18536: Using WITH inside WITH RECURSIVE triggers a "shouldn't happen" error