RFP: Recursive query in 8.4
RFP: Recursive query in 8.4
От:
Tatsuo Ishii <ishii@postgresql.org>
Дата:
Hi, As I promised before we would like to propose implementing the recursive query as defined in the standard for PostgreSQL 8.4. The work is supported by Sumitomo Electric Information Systems Co., Ltd. (http://www.sei-info.co.jp/) and SRA OSS, Inc. Japan (http://www.sraoss.co.jp). 1. Overview We propose to implement the recursive query (WITH RECURSIVE clause) defined in SQL:1999 and later. With the recursive query, one can easily inquire the data expressed as tree and graph structures. The actual syntax we prefer is the one defined in SQL:2008 (it's not published yet, but I have a closest draft). We do not propose the WITH clause without RECURSIVE key word here since someone else has already made a proposal for this. (http://archives.postgresql.org/pgsql-patches/2008-01/msg00105.php) 2. Example For those who are not familiar with the recursive query, I include an example: CREATE TABLE department ( id INT PRIMARY KEY, parent_department INT REFERENCES department, name TEXT ); INSERT INTO department VALUES (0, NULL, 'ROOT'); INSERT INTO department VALUES (1, 0, 'A'); INSERT INTO department VALUES (2, 1, 'B'); INSERT INTO department VALUES (3, 2, 'C'); INSERT INTO department VALUES (4, 2, 'D'); INSERT INTO department VALUES (5, 0, 'E'); INSERT INTO department VALUES (6, 4, 'F'); INSERT INTO department VALUES (7, 4, 'G'); This will represent a tree structure of an organization: ROOT ---> A ---> B ---> C ---> F | | | +----> D | +-----> E ---> G If you want to extract all departments "under" A, you could use a recursive query: WITH RECURSIVE subdepartment AS ( -- SELECT * FROM department WHERE id = 'A' UNION ALL -- recursive term referring to "subdepartment" SELECT d.* FROM department AS d, subdepartment AS sd WHERE d.id = sd.parent_department ) SELECT * FROM subdepartment; This will return A, B, C, D and F. 2. The syntax As described above, we refers to the SQL:2008's WITH RECURSIVE clause syntax. WITH RECURSIVE clause ::= WITH RECURSIVE AS (
Re: RFP: Recursive query in 8.4
От:
ITAGAKI Takahiro <itagaki.takahiro@oss.ntt.co.jp>
Дата:
Tatsuo Ishii wrote: > 5. Limitation with PostgreSQL > > 1) we do not implement SEARCH clause and CYCLE clause. This is because > we need array of rows to implement them. Note that there's no > support for array of rows in PostgreSQL. What is difference between "array of rows" and Arrays of composite types, that is new feature in 8.3 ? =# CREATE TABLE foo (i integer); CREATE TABLE =# CREATE TABLE bar (foos foo[]); -- *here* CREATE TABLE Regards, --- ITAGAKI Takahiro NTT Open Source Software Center
Re: RFP: Recursive query in 8.4
От:
Tatsuo Ishii <ishii@sraoss.co.jp>
Дата:
> Tatsuo Ishii wrote: > > > 5. Limitation with PostgreSQL > > > > 1) we do not implement SEARCH clause and CYCLE clause. This is because > > we need array of rows to implement them. Note that there's no > > support for array of rows in PostgreSQL. > > What is difference between "array of rows" and > Arrays of composite types, that is new feature in 8.3 ? > > =# CREATE TABLE foo (i integer); > CREATE TABLE > =# CREATE TABLE bar (foos foo[]); -- *here* > CREATE TABLE Will check. Thanks for pointing it out. -- Tatsuo Ishii SRA OSS, Inc. Japan
Re: RFP: Recursive query in 8.4
От:
"Merlin Moncure" <mmoncure@gmail.com>
Дата:
On Tue, Feb 19, 2008 at 3:36 AM, Tatsuo Ishii wrote: > We propose to implement the recursive query (WITH RECURSIVE clause) > defined in SQL:1999 and later. With the recursive query, one can > easily inquire the data expressed as tree and graph structures. The > actual syntax we prefer is the one defined in SQL:2008 (it's not > published yet, but I have a closest draft). I am sure you are aware of various ad hoc approaches that are currently possible. The recursive clause seems to generalize these approaches. Do you expect that your proposed solution will have performance advantages over solutions like using recursive functions and (for tree organized data) arrays? merlin
Re: RFP: Recursive query in 8.4
От:
Tatsuo Ishii <ishii@postgresql.org>
Дата:
> On Tue, Feb 19, 2008 at 3:36 AM, Tatsuo Ishii wrote: > > We propose to implement the recursive query (WITH RECURSIVE clause) > > defined in SQL:1999 and later. With the recursive query, one can > > easily inquire the data expressed as tree and graph structures. The > > actual syntax we prefer is the one defined in SQL:2008 (it's not > > published yet, but I have a closest draft). > > I am sure you are aware of various ad hoc approaches that are > currently possible. The recursive clause seems to generalize these > approaches. > > Do you expect that your proposed solution will have performance > advantages over solutions like using recursive functions and (for tree > organized data) arrays? I hope so. But the first thing I would like to do is, to implement the right thing (i.e. following the standard). I don't see any reason that the proposal gets less performance than existing functions. Moreover the proposal could better cooperate with the optimizer since it can feed more info to it. Any ideas to enhance the performance are welcome. -- Tatsuo Ishii SRA OSS, Inc. Japan
Re: RFP: Recursive query in 8.4
От:
Gregory Stark <gsstark@mit.edu>
Дата:
[This message is mostly for the benefit of the list -- he and I already talked a bit about this here at FOSDEM. Ishii-san, if you have a chance we should sit down and talk about this in more detail before we leave!] Tatsuo Ishii wrote: >> On Tue, Feb 19, 2008 at 3:36 AM, Tatsuo Ishii wrote: >> > I hope so. But the first thing I would like to do is, to implement the > right thing (i.e. following the standard). > > I don't see any reason that the proposal gets less performance than > existing functions. Moreover the proposal could better cooperate with > the optimizer since it can feed more info to it. Any ideas to enhance > the performance are welcome. I agree about following the standard but I think it's true that the standard creates some challenges for the optimizer. The standard recursive query syntax is quite general. It can represent arbitrary non-linear recursive queries including possibly mutually recursive queries, for example. The challenge is that there are no extra hints when you have the more usual case of a simple linear recursion. You really do want to discover such linear recursive structures because you can use simpler algorithms and recover memory sooner if you know you have a linear recursive query. You can also support the SEARCH and CYCLE clauses to do depth-first searches which you can't do for arbitrary recursive queries. I also don't have much hope for good optimizer estimates for general recursive queries but for linear recursive queries we can probably do better. But I think (surprisingly) it's actually easier to implement the general case than the special nodes to handle the linear case more efficiently. To handle the general case we need the memoize node to handle recursive loops in the plan and then we can use otherwise normal plan nodes. My plan was to implement the general case first, then look for ways to add intelligence in the planner to discover linearity and add new paths to take advantage of it. -- greg
Re: RFP: Recursive query in 8.4
От:
"Greg Stark" <stark@enterprisedb.com>
Дата:
Tatsuo Ishii wrote: >> On Tue, Feb 19, 2008 at 3:36 AM, Tatsuo Ishii wrote: >> > I hope so. But the first thing I would like to do is, to implement the > right thing (i.e. following the standard). > > I don't see any reason that the proposal gets less performance than > existing functions. Moreover the proposal could better cooperate with > the optimizer since it can feed more info to it. Any ideas to enhance > the performance are welcome. > I agree about following the standard but I think it's true that the standard creates some challenges for the optimizer. The standard recursive query syntax is quite general. It can represent arbitrary non-linear recursive queries including possibly mutually recursive queries, for example. The challenge is that there are no extra hints when you have the more usual case of a simple linear recursion. You really do want to discover such linear recursive structures because you can use simpler algorithms and recover memory sooner if you know you have a linear recursive query. You can also support the SEARCH and CYCLE clauses to do depth-first searches which you can't do for arbitrary recursive queries. I also don't have much hope for good optimizer estimates for general recursive queries but for linear recursive queries we can probably do better. But I think it's actually easier to implement the general case than the special nodes to handle the linear case more efficiently. To handle the general case we need the memoize node to handle recursive loops in the plan and then we can use otherwise normal plan nodes. My plan was to implement the general case first, then look for ways to add intelligence in the planner to discover linearity and add new paths to take advantage of it.
Re: RFP: Recursive query in 8.4
От:
Tatsuo Ishii <ishii@postgresql.org>
Дата:
> Tatsuo Ishii wrote: > >> On Tue, Feb 19, 2008 at 3:36 AM, Tatsuo Ishii wrote: > >> > > I hope so. But the first thing I would like to do is, to implement the > > right thing (i.e. following the standard). > > > > I don't see any reason that the proposal gets less performance than > > existing functions. Moreover the proposal could better cooperate with > > the optimizer since it can feed more info to it. Any ideas to enhance > > the performance are welcome. > > > > I agree about following the standard but I think it's true that the > standard creates some challenges for the optimizer. > > The standard recursive query syntax is quite general. It can represent > arbitrary non-linear recursive queries including possibly mutually > recursive queries, for example. The challenge is that there are no extra > hints when you have the more usual case of a simple linear recursion. I seems the standard does not allow non-linear recursive queries. In the SQL:2008 draft pp.380: 7.13 Syntax Rules 2) g) iv) "If WLE_i is recursive, then WLE_i shall be linearly recursive." So now the problem is, how to detect the non-linear recursive queries and reject them. > You really do want to discover such linear recursive structures because > you can use simpler algorithms and recover memory sooner if you know you > have a linear recursive query. You can also support the SEARCH and CYCLE > clauses to do depth-first searches which you can't do for arbitrary > recursive queries. I also don't have much hope for good optimizer > estimates for general recursive queries but for linear recursive queries > we can probably do better. > > But I think it's actually easier to implement the general case than the > special nodes to handle the linear case more efficiently. To handle the > general case we need the memoize node to handle recursive loops in the > plan and then we can use otherwise normal plan nodes. > > My plan was to implement the general case first, then look for ways to > add intelligence in the planner to discover linearity and add new paths > to take advantage of it. >
Re: RFP: Recursive query in 8.4
От:
Tatsuo Ishii <ishii@postgresql.org>
Дата:
> > Tatsuo Ishii wrote: > > > > > 5. Limitation with PostgreSQL > > > > > > 1) we do not implement SEARCH clause and CYCLE clause. This is because > > > we need array of rows to implement them. Note that there's no > > > support for array of rows in PostgreSQL. > > > > What is difference between "array of rows" and > > Arrays of composite types, that is new feature in 8.3 ? > > > > =# CREATE TABLE foo (i integer); > > CREATE TABLE > > =# CREATE TABLE bar (foos foo[]); -- *here* > > CREATE TABLE > > Will check. Thanks for pointing it out. Yes, I agree with that the 8.3's new feature might be used to implement SEARCH clause and CYCLE clause. Problem is, it requres that the named composite type (the standard's term is "row type") is previously defined. Maybe we need "anonymous" row type? -- Tatsuo Ishii SRA OSS, Inc. Japan