Sorting with materialized paths - Mailing list pgsql-general

From Ovid
Subject Sorting with materialized paths
Date
Msg-id 405187.35062.qm@web65711.mail.ac4.yahoo.com
Whole thread Raw
Responses Re: Sorting with materialized paths  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: Sorting with materialized paths  (Peter Hunsberger <peter.hunsberger@gmail.com>)
Re: Sorting with materialized paths  (Thomas Kellerer <spam_eater@gmx.net>)
List pgsql-general
My apologies. This isn't PG-specific, but since this is running on PostgreSQL 8.4, maybe there are specific features
whichmight help. 

I have a tree structure in a table and it uses materialized paths to allow me to find children quickly. However, I also
needto sort the results depth-first, as one would expect with threaded forum replies. 

 id | parent_id | matpath |          created
----+-----------+---------+----------------------------
  2 |         1 | 1       | 2010-05-08 15:18:37.987544
  3 |         1 | 1       | 2010-05-08 17:38:14.125377
  4 |         1 | 1       | 2010-05-08 17:38:57.26743
  5 |         1 | 1       | 2010-05-08 17:43:28.211708
  7 |         1 | 1       | 2010-05-08 18:18:11.849735
  6 |         2 | 1.2     | 2010-05-08 17:50:43.288759
  9 |         5 | 1.5     | 2010-05-09 14:02:43.818646
  8 |         6 | 1.2.6   | 2010-05-09 14:01:17.632695

So the final results should actually be sorted like this:

 id | parent_id | matpath |          created
----+-----------+---------+----------------------------
  2 |         1 | 1       | 2010-05-08 15:18:37.987544
  6 |         2 | 1.2     | 2010-05-08 17:50:43.288759
  8 |         6 | 1.2.6   | 2010-05-09 14:01:17.632695
  3 |         1 | 1       | 2010-05-08 17:38:14.125377
  4 |         1 | 1       | 2010-05-08 17:38:57.26743
  5 |         1 | 1       | 2010-05-08 17:43:28.211708
  9 |         5 | 1.5     | 2010-05-09 14:02:43.818646
  7 |         1 | 1       | 2010-05-08 18:18:11.849735

Rationale:  this is for a threaded forum and id 6 is a reply to id 2, so it needs to show up after that one.  Here's
therough structure of what the output would look like (imagine an HTML forum): 

* id 1 (root post)
    * id 2
        * id 6
            * id 8
    * id 3
    * id 4
    * id 5
        * id 9
    * id 7

How would I work that out? Can I do that in straight SQL or should additional information be added to this table?

Cheers,
Ovid
--
Buy the book         - http://www.oreilly.com/catalog/perlhks/
Tech blog            - http://blogs.perl.org/users/ovid/
Twitter              - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6


pgsql-general by date:

Previous
From: John Gage
Date:
Subject: Re: Documentation availability as a single page of text
Next
From: "Abraham, Danny"
Date:
Subject: PG 8.3.7. Windows 7. select inet_server_addr() returns ::1 - the loopback adrs.