Unused CTE affects result set

From: Gabriel Sánchez <gabrielesanchez(at)gmail(dot)com>
To: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Unused CTE affects result set
Date: 2023-11-17 18:30:34
Message-ID: CANHuRqHUR+a0TEF7WXLNN1+_=6CjET-jtu=QFuEPJ875oMHC7A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Dear Postgres community,

I'm seeing unintuitive behavior with a query structured as follows:

WITH used_cte (SELECT a, b, c FROM t1 ...)
, unused_cte (SELECT d, e, f FROM t2 ...)
SELECT * FROM used_cte ...
-- returns no results

WITH used_cte (SELECT a, b, c FROM t1 ...)
-- , unused_cte (SELECT d, e, f FROM t2 ...)
SELECT * FROM used_cte ...
-- returns results

I don't understand why the presence of a CTE that ends up not being
included in the final query affects the result set. Could this be a bug,
or is there something I don't understand?

PostgreSQL 14.9 (Ubuntu 14.9-0ubuntu0.22.04.1) on x86_64-pc-linux-gnu,
compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, 64-bit

Thanks,
Gabriel

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2023-11-17 18:35:22 Re: Unused CTE affects result set
Previous Message Adrian Klaver 2023-11-17 16:14:43 Re: PostgreSQL statistics