1

i spent almost a day on it now and it seems like i am doing something wrong. ok , here is the relation: document_urls( doc_id , url_id)

what i want to do is to build a sorte of graph that will show all the children that has been generated from a document through on of his urls. example select * from document_urls where doc_id=1

doc_id url_id
1 2
1 3

if i select all the document with url_id=3 or 2 i will find select * from document_urls where url_id=2 or url_id=3

doc_id url_id
1 2
1 3
2 3

now i do the same exercise with document 2 since we covered all links of document 1 and so forth.

here is my recursive query now

WITH  RECURSIVE generate_links(document_id,url_id) as(  
    select document_id,url_id from document_urls where document_id=1 
UNION ALL
    select du.document_id,du.url_id from generate_links gl,document_urls du
    where gl.url_id=du.url_id 
)

SELECT * FROM generate_links GROUP BY url_id,document_id limit 10;

1 Answer 1

2

I take it you want to move your where document_id=1 into the lower part of the query.

Be wary about doing so, however, because a recursive query does not inject the constraint into the with statement. In other words, it'll actually seq scan your whole table, recursively build every possibility and filter out those you need.

You'll be better off with an sql function in practice, i.e. something like this:

create or replace function gen_links(int) returns table (doc_id int, doc_url text) as $$
WITH  RECURSIVE generate_links(document_id,url_id) as(  
    select document_id,url_id from document_urls where document_id=$1
UNION ALL
    select du.document_id,du.url_id from generate_links gl,document_urls du
    where gl.url_id=du.url_id 
)

SELECT * FROM generate_links GROUP BY url_id,document_id;
$$ language sql stable;
4
  • ok , so basically it filters the table after doing the recursion.
    – fenec
    Commented May 12, 2011 at 16:56
  • it is taking forever, i am suspecting a limitation in how deep could the recursion could go in Postgres. any idea?
    – fenec
    Commented May 12, 2011 at 16:57
  • 1
    your first comment gets it right: it processes the whole table, no matter how deep. i suspect you may have an error in your join clause, though, i.e. are you sure about l.url_id=du.url_id? Commented May 12, 2011 at 17:02
  • am not sure about the join, but this is how i want to link the documents ,by their common URL
    – fenec
    Commented May 12, 2011 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.