Since PostgreSQL 12, a plain, non-recursive, side-effect-free WITH CTE referenced once is inlined into the surrounding query by default — the planner treats it the same as an equivalent subquery, filters can push down into it, and it produces an identical plan. Before 12, a CTE was always an optimization fence: computed once, in isolation, before the outer query touched it. That fence still exists — it just requires writing AS MATERIALIZED to ask for it. Nothing else on this page matters as much as that one behavioral change.
Table of Contents
Is a CTE faster than a subquery?
That depends entirely on whether the specific CTE gets inlined or materialized, which is exactly the mechanism the rest of this page demonstrates.
Same query, three ways, real EXPLAIN output
200,000-row table, indexed on customer_id, filtering one customer then a range on amount:
-- 1. Plain subquery
EXPLAIN SELECT * FROM (
SELECT customer_id, amount FROM hw_w18_cte_orders WHERE customer_id = 42
) sub WHERE amount > 100;
Bitmap Heap Scan on hw_w18_cte_orders (cost=5.83..537.32 rows=159 width=10)
Recheck Cond: (customer_id = 42)
Filter: (amount > '100'::numeric)
-> Bitmap Index Scan on hw_w18_cte_orders_cust_idx (cost=0.00..5.79 rows=199 width=0)
Index Cond: (customer_id = 42)
-- 2. Same query as a CTE, no MATERIALIZED (PostgreSQL 12+ default: inlined)
EXPLAIN WITH cte AS (
SELECT customer_id, amount FROM hw_w18_cte_orders WHERE customer_id = 42
) SELECT * FROM cte WHERE amount > 100;
Bitmap Heap Scan on hw_w18_cte_orders (cost=5.83..537.32 rows=159 width=10)
Recheck Cond: (customer_id = 42)
Filter: (amount > '100'::numeric)
-> Bitmap Index Scan on hw_w18_cte_orders_cust_idx (cost=0.00..5.79 rows=199 width=0)
Index Cond: (customer_id = 42)
Identical, cost for cost — the amount > 100 filter got pushed down and combined with the index scan in both cases. The CTE bought nothing and cost nothing; it’s the same plan as the subquery.
-- 3. Same query, CTE forced with AS MATERIALIZED
EXPLAIN WITH cte AS MATERIALIZED (
SELECT customer_id, amount FROM hw_w18_cte_orders WHERE customer_id = 42
) SELECT * FROM cte WHERE amount > 100;
CTE Scan on cte (cost=536.83..541.31 rows=159 width=36)
Filter: (amount > '100'::numeric)
CTE cte
-> Bitmap Heap Scan on hw_w18_cte_orders (cost=5.84..536.83 rows=199 width=10)
Recheck Cond: (customer_id = 42)
-> Bitmap Index Scan on hw_w18_cte_orders_cust_idx (cost=0.00..5.79 rows=199 width=0)
Index Cond: (customer_id = 42)
Different shape: the CTE materializes into a separate node first (every row where customer_id = 42, before the amount filter runs), then a CTE Scan applies amount > 100 afterward. This is the fence — the outer filter can no longer be combined with the index scan. The plan is still cheap here because 200 rows is nothing to materialize, but the mechanism is exactly what makes AS MATERIALIZED expensive on a CTE that produces a large intermediate result and only a subquery/plain-CTE version would let the planner push a selective filter down into the scan. cte-window-functions-vs-subqueries.sh regenerates all three plans on a 200,000-row table it adds to the server that PGHOST, PGPORT and PGUSER name (hw_w18_verify_cte_orders, not removed afterwards), and fails if the plain CTE ever shows a CTE Scan node or the MATERIALIZED one does not.
When to reach for MATERIALIZED on purpose
I write AS MATERIALIZED on purpose in one case: a CTE that PostgreSQL would otherwise inline, where EXPLAIN shows the inlined plan is the worse one. By default PostgreSQL inlines a CTE only when it is non-recursive, side-effect-free (a plain SELECT with no volatile functions) and referenced once, as the PostgreSQL docs on CTE materialization spell out.
AS NOT MATERIALIZED is the mirror image: on a non-recursive, side-effect-free CTE referenced more than once, it forces the inlining, so the outer query’s conditions can be applied directly to the table scans. The price is that the CTE’s query can then run once per reference, so it pays when each reference needs only a small part of the result.
Everywhere else MATERIALIZED changes nothing, because PostgreSQL already calculates those CTEs separately: for example a CTE referenced more than once, a CTE that calls a volatile function, or a data-modifying CTE (INSERT, UPDATE or DELETE with RETURNING). A data-modifying CTE is executed exactly once, to completion, and as PostgreSQL’s SELECT reference notes, NOT MATERIALIZED is ignored on a CTE that is not side-effect-free. Neither keyword is a performance default, so I check EXPLAIN before writing one. And note that the keyword is not a relative of a materialized view, which stores its result on disk until you refresh it; AS MATERIALIZED lasts exactly as long as the statement around it.
Recursive CTEs are unaffected
WITH RECURSIVE was not changed by the PostgreSQL 12 inlining change — it still executes as its own node by necessity, since the query has to reference its own prior output. The distinction on this page is specifically about non-recursive CTEs referenced once.
For the query-planning tool used to produce every plan on this page, see how PostgreSQL decides between a custom and a generic plan for a prepared statement — the same EXPLAIN-first approach, on the same server.