pg.ddx.io  pgsql-docs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
WITH RECURSIVE (Documentation section 7.8.1) Note
2+ messages / 2 participants
[nested] [flat]

* WITH RECURSIVE (Documentation section 7.8.1) Note
@ 2019-10-02 13:41  PG Doc comments form <noreply@postgresql.org>
  0 siblings, 1 reply; 2+ messages in thread

From: PG Doc comments form @ 2019-10-02 13:41 UTC (permalink / raw)
  To: pgsql-docs@lists.postgresql.org; +Cc: ceharris@vt.edu

The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/9.4/queries-with.html
Description:

In section 7.8.1, the author comments "Note: Strictly speaking, this process
is iteration not recursion, but RECURSIVE is the terminology chosen by the
SQL standards committee."

This comment is not helpful (at best) and misleading (at worst). 

The "process" which is the subject of the comment is based on the
implementation of WITH RECURSIVE in PostgreSQL -- the implementation happens
to execute a sequence of queries and a temporary table, and one could
arguably describe this as iteration.

However, the use of the term RECURSIVE in the syntax, refers not to any
particular implementation, but to an observation that WITH RECURSIVE is
effectively an example of a recursively defined function in set-theoretic
terms. Consider the first of the UNIONed statements to produce a row set
R_0. Consider the second of the UNIONed statements to be function F that
produces a row set R_k  = F(R_k-1). (The underscore is used here to denote a
subscript).

Example: R_2 = F(F(R_0))
Example: R_5 = F(F(F(F(F(R_0))))

This is textbook recursion, not just some folly of the ANSI standards
committee as suggested by the note in the text.


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: WITH RECURSIVE (Documentation section 7.8.1) Note
@ 2019-10-02 16:41  Tom Lane <tgl@sss.pgh.pa.us>
  parent: PG Doc comments form <noreply@postgresql.org>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2019-10-02 16:41 UTC (permalink / raw)
  To: ceharris@vt.edu; +Cc: pgsql-docs@lists.postgresql.org

PG Doc comments form <noreply@postgresql.org> writes:
> In section 7.8.1, the author comments "Note: Strictly speaking, this process
> is iteration not recursion, but RECURSIVE is the terminology chosen by the
> SQL standards committee."

I think I'm to blame for that wording.

> Example: R_2 = F(F(R_0))
> Example: R_5 = F(F(F(F(F(R_0))))
> This is textbook recursion, not just some folly of the ANSI standards
> committee as suggested by the note in the text.

No, it isn't.  F() is being applied repeatedly (iteratively).  If F()
called itself internally, that would be recursion.

Another way to look at this is that control of whether the repetition
is finished is external to the function/query.  If the stop condition
were part of the query logic, it'd be more sensible to think of it as
recursion, IMO.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2019-10-02 16:41 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-10-02 13:41 WITH RECURSIVE (Documentation section 7.8.1) Note PG Doc comments form <noreply@postgresql.org>
2019-10-02 16:41 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox