Re: BUG #18536: Using WITH inside WITH RECURSIVE triggers a "shouldn't happen" error
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: exclusion@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Date: 2024-07-14T15:09:37Z
Lists: pgsql-bugs
PG Bug reporting form <noreply@postgresql.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
Commits
-
Avoid unhelpful internal error for incorrect recursive-WITH queries.
- f96c2c72788c 18.0 landed
- e7f9f44e3bd9 15.8 landed
- cf588e10f664 17.0 landed
- b020a866a22b 13.16 landed
- 8fc4876147fb 16.4 landed
- 236b225ed452 12.20 landed
- 02b4f5e1f26b 14.13 landed