Re: Very slow query performance when using CTE

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Chris Joysn <joysn71@gmail.com>
Cc: pgsql-performance@lists.postgresql.org
Date: 2025-04-01T14:28:58Z
Lists: pgsql-performance
Chris Joysn <joysn71@gmail.com> writes:
> I have an issue when using CTEs. A query, which consists of multiple CTEs,
> runs usually rather fast (~5s on my environment). But it turned out that
> using one CTE can lead to execution times of up to one minute.
> That CTE is used two times within the query.

Try labeling that CTE as NOT MATERIALIZED.

			regards, tom lane