pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
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
Date: Sun, 14 Jul 2024 11:09:37 -0400
Message-ID: <2781444.1720969777@sss.pgh.pa.us> (raw)
In-Reply-To: <18536-0a342ec07901203e@postgresql.org>
References: <18536-0a342ec07901203e@postgresql.org>

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





view thread (6+ messages)  latest in thread

Message-ID: <2781444.1720969777@sss.pgh.pa.us>
Permalink:  ../2781444.1720969777@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2781444.1720969777@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, exclusion@gmail.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #18536: Using WITH inside WITH RECURSIVE triggers a "shouldn't happen" error
  In-Reply-To: <2781444.1720969777@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox