Re: BUG #17721: A completely unused CTE negatively affect Query Plan - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #17721: A completely unused CTE negatively affect Query Plan
Date
Msg-id 2784036.1671042328@sss.pgh.pa.us
Whole thread Raw
In response to BUG #17721: A completely unused CTE negatively affect Query Plan  (PG Bug reporting form <noreply@postgresql.org>)
Responses Re: BUG #17721: A completely unused CTE negatively affect Query Plan
List pgsql-bugs
PG Bug reporting form <noreply@postgresql.org> writes:
> I've boiled down an issue we have in production to a simple query that can
> demonstrate it.  I've run this on 13,14 and 15 locally in a docker container
> with the same results.  If a CTE that is completely unexecuted exists in a
> subquery (or view in our production case) it affects the query plan VERY
> negatively.  The first explain shows a sequential scan, where the second
> explain shows an index scan, just by the existence of the (obviously)
> unexecuted CTE.

The presence of the CTE prevents flattening of the subquery.
If you don't like it, don't attach the CTE right there.

            regards, tom lane



pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #17721: A completely unused CTE negatively affect Query Plan
Next
From: Mats Kindahl
Date:
Subject: Re: Crash during backend start when low on memory