I have a simple question. Somehow I was unable to find a definitive answer.
How much is WITH RECURSIVE syntax optimized in PostgreSQL? By that I mean: is it merely a syntactic sugar for a series of non recursive queries, OR is it more of a single statement that despite its complicated semantics has been optimized as a whole. A follow-up question – just about how much is it possible to optimize this kind of syntax? Of course some concrete data on the matter is most welcome.
My experience is that it is indeed very well optimized.
Check out the execution plan for your query generated by EXPLAIN ANALYZE and you’ll see how “costly” it really is (and then compare that e.g. to a self written recursive function)