Very slow query performance when using CTE - Mailing list pgsql-performance

From Chris Joysn
Subject Very slow query performance when using CTE
Date
Msg-id CAKRYhW36jEbPPSPoiPkNcPWdrEXOtdugw9Hyg1poHju7LECHJg@mail.gmail.com
Whole thread Raw
Responses Re: Very slow query performance when using CTE
List pgsql-performance
Hello,
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. In the CTE there are 2600 rows, compared to results of the other CTEs its a fraction of the data.
When replacing this CTE and use the original table instead in the jions, the query performs nicely.
However, it is not always like this. Running the same query on a almost same set of data, quantity wise, may give indeed good performance when using that CTE.
This is the slow performing query using CTE: 
And this is the fast performing query without that CTE: 
The query runs on the very same environment and data.
What can be the issue here and how can I address it?

pgsql-performance by date:

Previous
From: Dimitrios Apostolou
Date:
Subject: Re: parallel pg_restore blocks on heavy random read I/O on all children processes
Next
From: Tom Lane
Date:
Subject: Re: Very slow query performance when using CTE