Re: BUG #18536: Using WITH inside WITH RECURSIVE triggers a "shouldn't happen" error - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #18536: Using WITH inside WITH RECURSIVE triggers a "shouldn't happen" error
Date
Msg-id 2781444.1720969777@sss.pgh.pa.us
Whole thread Raw
In response to BUG #18536: Using WITH inside WITH RECURSIVE triggers a "shouldn't happen" error  (PG Bug reporting form <noreply@postgresql.org>)
Responses Re: BUG #18536: Using WITH inside WITH RECURSIVE triggers a "shouldn't happen" error
List 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



pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #18536: Using WITH inside WITH RECURSIVE triggers a "shouldn't happen" error
Next
From: Vasilii Smirnov
Date:
Subject: libpq: unexpected return code from PQexecParams with a DO INSTEAD rule present