pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christopher Browne <cbbrowne@acm.org>
To: pgsql-general@postgresql.org
Subject: Re: Recursive queries
Date: Wed, 26 Jan 2005 08:52:37 -0500
Message-ID: <m3651ks3fe.fsf@knuth.knuth.cbbrowne.com> (raw)
References: <pan.2005.01.24.16.27.44.945370@amossen.dk>
	<20050125185833.GK29308@svana.org>

In an attempt to throw the authorities off his trail, kleptog@svana.org (Martijn van Oosterhout) transmitted:
> On Mon, Jan 24, 2005 at 05:27:46PM +0100, tmp wrote:
>> Are there any plans on implementing support for recursive queries in
>> postgresql in the near future? If so: When?
>> 
>> I can see there has been some discussion on the subject in the
>> developer-group for quite some time ago, but aparently all thoughts of
>> recursive queries has been stalled. :-(
>
> What do you mean by resursive queries? A query can have a subquery
> which calls a function which executes another query. That counts as
> recursion in my book. What type of recursion are you thinking of?

By recursive queries, we mean the form defined in SQL3/SQL.1999.

IBM DB2 uses a syntax like the following; I'd have to rummage around
for extra books to verify standards conformance, but hopefully this
gives the idea...

 WITH tmp_rel (object, subobject, quantity) AS
   (SELECT part, child_part, quantity FROM
    partlist root 
    WHERE root.part in ('ASSEMBLY 1', 'ASSEMBLY 2', 'ASSEMBLY 3')
    UNION ALL
    SELECT child.part, child.child_part, child.quantity
    FROM partlist child, tmp_rel parent
    WHERE parent.subobject = child.part)
 SELECT DISTINCT object, subobject, quantity
 FROM tmp_rel;

What you add in is the "WITH" clause that lets you define a (possibly
self-referencing) query which you then reference below.

This is more or less equivalent to the "let" clause offered in
languages like Lisp and ML

let disc = (x*x-y*y) in
   calculate_with_squares (disc)
   calculate_again_with_squares (disc);;

Or

(let ((disc (+ (* x x) (* y y))))
   (calculate_with_squares disc)
   (calculate_again_with_squares disc))

In Lisp, the thing that allows recursing is, strictly speaking, called
"letrec"...

Nonetheless, the similarity is still quite evident.  SQL "WITH" allows
building self-referencing queries, as well as allowing you to better
organize bits that are likely to get reused.  If I have some complex
subquery that occurs several times in a query, I might want to use
WITH in a not-so-recursive way to factor out that subquery so it only
needs to be expressed once.
-- 
output = reverse("gro.gultn" "@" "enworbbc")
http://www.ntlug.org/~cbbrowne/linux.html
"Life.  Don't talk to me about life."
-- Marvin the Paranoid Android



view thread (27+ messages)  latest in thread

Message-ID: <m3651ks3fe.fsf@knuth.knuth.cbbrowne.com>
Permalink:  ../m3651ks3fe.fsf@knuth.knuth.cbbrowne.com/
Also on:    postgresql.org/message-id/m3651ks3fe.fsf@knuth.knuth.cbbrowne.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: cbbrowne@acm.org
  Subject: Re: Recursive queries
  In-Reply-To: <m3651ks3fe.fsf@knuth.knuth.cbbrowne.com>
  References: <pan.2005.01.24.16.27.44.945370@amossen.dk> <20050125185833.GK29308@svana.org> <m3651ks3fe.fsf@knuth.knuth.cbbrowne.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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