Another look into PostgreSQL CTE materialization and non-idempotent subqueries

A few days ago, I wrote about a surprising planner behavior with CTEs, DELETE, and LIMIT in PostgreSQL, a piece I hastily put together on a bus ride. That post clearly only scratched the surface of a deeper issue that I&rsquo;ve since spent way too many hours exploring. So here are some more formed thoughts and findings. The core issue revisited Let&rsquo;s quickly recap: when using a query like…