Plan nodes · Scans
CTE Scan
Reads the stored result of a WITH query.
Reads the rows of a common table expression that was materialized: computed once and kept, so several references to it share the work. Since PostgreSQL 12, a CTE used once and without side effects is inlined instead, unless it says MATERIALIZED.
What to look for
Conditions on a CTE Scan are not pushed into the CTE: a materialized CTE is computed in full first.
Example
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 10) SELECT i FROM n
4 nodes
- Resultrows 1 (estimated 1) cost 0..0.01
Run with EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF) on the sample database, PostgreSQL 19.0. Open it in the EXPLAIN visualizer →
Related
Seq Scan · Index Scan · Index Only Scan · Bitmap Index Scan · Bitmap Heap Scan · BitmapAnd · BitmapOr · Tid Scan · Tid Range Scan · Sample Scan · Subquery Scan · Function Scan · Table Function Scan · Values Scan · WorkTable Scan · Named Tuplestore Scan · Foreign Scan · Custom Scan