Re: Abitity to identify the current iteration in a recursive SELECT (feature request)

From: shammat(at)gmx(dot)net
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Re: Abitity to identify the current iteration in a recursive SELECT (feature request)
Date: 2024-12-20 15:25:39
Message-ID: 3926d507-2ef4-4b94-ba84-2dafd2e1110a@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Am 16.12.24 um 11:21 schrieb sulfinu(at)gmail(dot)com:
> the algorithm present here <https://www.postgresql.org/docs/current/
> queries-with.html#QUERIES-WITH-RECURSIVE> for the resolution of WITH
> RECURSIVE queries is, broadly speaking, iterative, with the
> iteration sequence number indicated in the first row item of the
> field generated by the SEARCH BREADTH FIRST clause,
> i.e. /0/, /1/, /2/ etc.
>
> Is there a way to obtain directly this iteration sequence number
> within the SELECT statement following the UNION keyword in a
> recursive construction? I know it can by obtained by maintaining its
> value in a working table column, but that's suboptimal - I need it
> as a "magic" variable, akin, for example, the excluded variable
> available inside the ON CONFLICT DO UPDATE clause of an INSERT
> statement.
>

I assume you don't want to do something like the following when you
refer to "working table column"?

with recursive cte as (
select ..., 1 as level
from ...
union all
select ...., parent.level + 1
from ...
join cte as parent on ...
)

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tomasz Szypowski 2025-01-02 20:02:05 View performance with implicit cast
Previous Message sulfinu 2024-12-19 09:16:47 Re: Abitity to identify the current iteration in a recursive SELECT (feature request)