Recursive CTE trees + Sorting by votes - Mailing list pgsql-general

From Gregory Taylor
Subject Recursive CTE trees + Sorting by votes
Date
Msg-id CAA0B==T4boSEy06apYPqz5cyXsa8KzB173pOVutcSB5yJyOKdQ@mail.gmail.com
Whole thread Raw
Responses Re: Recursive CTE trees + Sorting by votes  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
We are working on a threaded comment system, and found this post by Disqus to be super helpful:


The CTE works wonderfully, and we're really happy with the results. The last obstacle is figuring out how to sort by a "votes" field, meanwhile preserving the tree structure.

If we "ORDER BY path, votes" (assuming we have the same structure as in the article), we never need tie-breaking on "path", so the "votes" part of this doesn't even come into the equation.

I suspect we need to do some path manipulation, but I'm not too sure of where to begin with this. I attempted incorporating "votes" into the path, but I failed pretty badly with this. It's probably way off, but here's my last (failed) attempt:


Any ideas would be greatly appreciated! If we can retain the path structure and also sort by votes, we'll be able to paginate freely without issues.

--

pgsql-general by date:

Previous
From: David Johnston
Date:
Subject: Re: Re: understanding why two nearly identical queries take two different planner routes, one 5s and one 2hr
Next
From: Martijn van Oosterhout
Date:
Subject: Re: Recursive CTE trees + Sorting by votes