PG Bug reporting form <noreply(at)postgresql(dot)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