PostgreSQL does not seem to optimize common table expressions (CTE) very well. At the same time, is seems to optimize properly when views are involved. Any idea why? Is that accidental or is it a technical/theoretical/essential reason? Examples below.
Does not seem optimized.
sap=# explain with j as (select * from suppliers natural join shipments) select * from j where status > 20;