1

I have a (postgresql) query, that goes like

with 
recursive account_tree as (<recursive function depending on path>), -- 1
path as (<some other query>)                                        -- 2
select * from account_tree;

This works perfectly fine.

But when I reorder the with queries to that

with 
path as (<some other query>),                                      -- 2
recursive account_tree as (<recursive function depending on path>) -- 1
select * from account_tree;

it suddenly shows a syntax error. This behaviour doesn't occur, when I have standard non-recursive queries. With non-recursive queries, I can order them, as they please me.

Why does it do that?

0

2 Answers 2

4

The recursive keyword always goes right behind WITH regardless which of the CTE is actually the recursive one:

with recursive path as (
  <some other query>
), account_tree as (
  <recursive function depending on path>
)
select * 
from account_tree;
Sign up to request clarification or add additional context in comments.

Comments

2

recursive refers to the entire with clause. So just use:

with recursive path as (<some other query>),                                      -- 2

What recursive really means is that Postgres will search for "table" names first as CTEs and then as tables/views in the recursive part of the CTE. The issue is really resolving identifiers. It has no effect on other CTEs.

2 Comments

So, the recursive makes only queries recursive, which are designed to be recursive. It has no effect on standard queries, right?
@MaestroGlanz . . . It should have no effect on "standard" CTEs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.