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 219763A5C33 for ; Wed, 26 Jan 2005 15:50:55 +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 34409-05 for ; Wed, 26 Jan 2005 15:50:46 +0000 (GMT) Received: from svana.org (svana.org [203.20.62.76]) by svr1.postgresql.org (Postfix) with ESMTP id 7A98C3A5C22 for ; Wed, 26 Jan 2005 15:50:49 +0000 (GMT) Received: from kleptog by svana.org with local (Exim 3.35 #1 (Debian)) id 1CtpQd-0000Hh-00; Thu, 27 Jan 2005 02:49:59 +1100 Date: Wed, 26 Jan 2005 16:49:58 +0100 From: Martijn van Oosterhout To: Christopher Browne Cc: pgsql-general@postgresql.org Subject: Re: Recursive queries Message-ID: <20050126154958.GH23796@svana.org> Reply-To: Martijn van Oosterhout Mail-Followup-To: Christopher Browne , pgsql-general@postgresql.org References: <20050125185833.GK29308@svana.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="fd5uyaI9j6xoeUBo" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.3.28i X-PGP-Key-ID: Length=1024; ID=0x0DC67BE6 X-PGP-Key-Fingerprint: 295F A899 A81A 156D B522 48A7 6394 F08A 0DC6 7BE6 X-PGP-Key-URL: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.015 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200501/1499 X-Sequence-Number: 72448 --fd5uyaI9j6xoeUBo Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jan 26, 2005 at 08:52:37AM -0500, Christopher Browne wrote: > By recursive queries, we mean the form defined in SQL3/SQL.1999. >=20 > 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... >=20 > WITH tmp_rel (object, subobject, quantity) AS > (SELECT part, child_part, quantity FROM > partlist root=20 > 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 =3D child.part) > SELECT DISTINCT object, subobject, quantity > FROM tmp_rel; >=20 > What you add in is the "WITH" clause that lets you define a (possibly > self-referencing) query which you then reference below. OMG! While I can understand what you say query does, I simply can't visualise it at all. Using WITH for named subqueries, that I can understand, it would even be useful. But self-referencing, I can't even think of how an executor would even calculate the resultset of the above query. There must be some additional constraints, because what happens if I write a query like: WITH tmp_rel (num) AS (SELECT num+1 FROM tmp_rel) SELECT * FROM tmp_rel; If I'm understanding you correctly, this query should never complete. I guess you need to build a query processor smart enough to detect this. As a base recursive functions should have a seed and an iterator and the syntax should reflect that. I don't think the WITH syntax is doing that. Incidently, for the case people are pointing to recursive queries here, the contrib/tablefunc module provides an answer with the connectby(). Given the table name, the parent and the child field names it will return a set resulting from a walk down the tree: http://developer.postgresql.org/cvsweb.cgi/~checkout~/pgsql/contrib/tablefu= nc/README.tablefunc?rev=3D1.11;content-type=3Dtext%2Fplain Anyway, thanks for the info about recurive queries. Have a nice day, --=20 Martijn van Oosterhout http://svana.org/kleptog/ > Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a > tool for doing 5% of the work and then sitting around waiting for someone > else to do the other 95% so you can sue them. --fd5uyaI9j6xoeUBo Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQFB97wmY5Twig3Ge+YRAo+cAJ4w1aUBzO4KqruP5lCie+v2cUZVUQCgwqeL ab1elpBY85hV2lfLxfY/4OI= =FH65 -----END PGP SIGNATURE----- --fd5uyaI9j6xoeUBo--