What is a CTE scan, and what are its implications for performance?

A “CTE scan” is a sequential scan of the materialized results of a CTE term (a named section like “blah” in a CTE like WITH blah AS (SELECT ...).

Materialized means that PostgreSQL has calculated the results and turned them into a temporary store of rows, it isn’t just using the CTE like a view.

The main implication is that selecting a small subset from a CTE term and discarding the rest can do a lot of wasted work, because the parts you discard must still be fully calculated.

For details see a recent blog post I wrote on the topic.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)