pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Order of "WITH RECURSIVE" output
3+ messages / 2 participants
[nested] [flat]

* Order of "WITH RECURSIVE" output
@ 2009-07-07 01:44 Andreas Joseph Krogh <andreak@officenet.no>
  2009-07-07 01:55 ` Re: Order of "WITH RECURSIVE" output Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Andreas Joseph Krogh @ 2009-07-07 01:44 UTC (permalink / raw)
  To: pgsql-sql

Hi all.
Take this example from src/test/regress/sql/with.sql:

WITH RECURSIVE subdepartment(level, id, parent_department, name) AS
(
	-- non recursive term
	SELECT 1, * FROM department WHERE name = 'A'

	UNION ALL

	-- recursive term
	SELECT sd.level + 1, d.* FROM department AS d, subdepartment AS sd
		WHERE d.parent_department = sd.id
)
SELECT * FROM subdepartment ORDER BY name;

 level | id | parent_department | name
-------+----+-------------------+------
     1 |  1 |                 0 | A
     2 |  2 |                 1 | B
     3 |  3 |                 2 | C
     3 |  4 |                 2 | D
     4 |  6 |                 4 | F
(5 rows)

If I omit "ORDER BY", is the output *guaranteed* (according to some standard) to be ordered in "hierarchy"-order, meaning each parent-department is always output before its respective child, or do I have to calculate a "level" column (like in the example above) and specify "ORDER BY LEVEL" to be 100%?

I'm using WITH RECURSIVE as sub-queries to build up arrays of parents in each output-row and would like as little overhead as possible and hence avoid unnecessary sorting.

-- 
Andreas Joseph Krogh <andreak@officenet.no>
Senior Software Developer / CTO
------------------------+---------------------------------------------+
OfficeNet AS            | The most difficult thing in the world is to |
Rosenholmveien 25       | know how to do a thing and to watch         |
1414 TrollÄsen          | somebody else doing it wrong, without       |
NORWAY                  | comment.                                    |
                        |                                             |
Tlf:    +47 24 15 38 90 |                                             |
Fax:    +47 24 15 38 91 |                                             |
Mobile: +47 909  56 963 |                                             |
------------------------+---------------------------------------------+



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Order of "WITH RECURSIVE" output
  2009-07-07 01:44 Order of "WITH RECURSIVE" output Andreas Joseph Krogh <andreak@officenet.no>
@ 2009-07-07 01:55 ` Tom Lane <tgl@sss.pgh.pa.us>
  2009-07-07 02:06   ` Re: Order of "WITH RECURSIVE" output Andreas Joseph Krogh <andreak@officenet.no>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2009-07-07 01:55 UTC (permalink / raw)
  To: Andreas Joseph Krogh <andreak@officenet.no>; +Cc: pgsql-sql

Andreas Joseph Krogh <andreak@officenet.no> writes:
> If I omit "ORDER BY", is the output *guaranteed* (according to some
> standard) to be ordered

No.  It's not necessary to read any other details to answer that ...

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Order of "WITH RECURSIVE" output
  2009-07-07 01:44 Order of "WITH RECURSIVE" output Andreas Joseph Krogh <andreak@officenet.no>
  2009-07-07 01:55 ` Re: Order of "WITH RECURSIVE" output Tom Lane <tgl@sss.pgh.pa.us>
@ 2009-07-07 02:06   ` Andreas Joseph Krogh <andreak@officenet.no>
  0 siblings, 0 replies; 3+ messages in thread

From: Andreas Joseph Krogh @ 2009-07-07 02:06 UTC (permalink / raw)
  To: pgsql-sql

On Tuesday 07 July 2009 03:55:35 am Tom Lane wrote:
> Andreas Joseph Krogh <andreak@officenet.no> writes:
> > If I omit "ORDER BY", is the output *guaranteed* (according to some
> > standard) to be ordered
> 
> No.  It's not necessary to read any other details to answer that ...
> 
> 			regards, tom lane

Ok, thanks for prompt reply.

-- 
Andreas Joseph Krogh <andreak@officenet.no>
Senior Software Developer / CTO
------------------------+---------------------------------------------+
OfficeNet AS            | The most difficult thing in the world is to |
Rosenholmveien 25       | know how to do a thing and to watch         |
1414 TrollÄsen          | somebody else doing it wrong, without       |
NORWAY                  | comment.                                    |
                        |                                             |
Tlf:    +47 24 15 38 90 |                                             |
Fax:    +47 24 15 38 91 |                                             |
Mobile: +47 909  56 963 |                                             |
------------------------+---------------------------------------------+



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2009-07-07 02:06 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-07-07 01:44 Order of "WITH RECURSIVE" output Andreas Joseph Krogh <andreak@officenet.no>
2009-07-07 01:55 ` Tom Lane <tgl@sss.pgh.pa.us>
2009-07-07 02:06   ` Andreas Joseph Krogh <andreak@officenet.no>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox