query

CTE MATERIALIZED

PostgreSQL can materialize a CTE separately to prevent folding into the parent query. This may avoid repeated computation but can restrict predicate pushdown.

Formal Syntax & Structure

sql syntaxsql
WITH t AS MATERIALIZED (SELECT ...)

Working Example

Example Snippetsql
CREATE TABLE a(id int primary key,k int); CREATE TABLE b(k int); INSERT INTO a VALUES (1,10),(2,20),(3,NULL); INSERT INTO b VALUES (10),(10),(NULL);WITH t AS MATERIALIZED (SELECT k FROM a) SELECT count(*) FROM t WHERE k=10;
SQL Reference