X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5B7BE3A5BC0 for ; Wed, 26 Jan 2005 14:08:25 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 22321-02 for ; Wed, 26 Jan 2005 14:08:14 +0000 (GMT) Received: from floppy.pyrenet.fr (floppy.pyrenet.fr [194.250.190.2]) by svr1.postgresql.org (Postfix) with ESMTP id 4FD973A5921 for ; Wed, 26 Jan 2005 14:08:17 +0000 (GMT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id D46BC31D93; Wed, 26 Jan 2005 15:08:17 +0100 (MET) From: Christopher Browne X-Newsgroups: pgsql.general Subject: Re: Recursive queries Date: Wed, 26 Jan 2005 08:52:37 -0500 Organization: cbbrowne Computing Inc Lines: 60 Message-ID: References: <20050125185833.GK29308@svana.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@news.hub.org X-message-flag: Outlook is rather hackable, isn't it? X-Home-Page: http://www.cbbrowne.com/info/ X-Affero: http://svcs.affero.net/rm.php?r=cbbrowne User-Agent: Gnus/5.1007 (Gnus v5.10.7) XEmacs/21.4 (Corporate Culture, linux) Cancel-Lock: sha1:qY4TaWLNqX+o0tdTPILb+npp6tU= To: pgsql-general@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.079 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200501/1487 X-Sequence-Number: 72436 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