Sort order in sub-select

From: "Craig A(dot) James" <cjames(at)modgraph-usa(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Sort order in sub-select
Date: 2006-06-30 04:55:10
Message-ID: 44A4AEAE.8070809@modgraph-usa.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Here is a question about SQL. I have a one-to-many pair of tables (call them "P" and "C" for parent and child). For each row of P, there are many rows in C with data, and I want to sort P on the min(c.data). The basic query is simple:

select p_id, min(data) as m from c group by p_id order by m;

Now the problem: I also want to store this, in sorted order, as a "hitlist", so I have a table like this:

create table hitlist(p_id integer, sortorder integer);

and a sequence to go with it. The first thing I tried doesn't work:

insert into hitlist(p_id, sortorder)
(select p_id, nextval('hitlist_seq') from
(select p_id, min(data) as m from c group by p_id order by m);

Apparently, the sort order returned by the innermost select is NOT maintained as you go through the next select statement -- the rows seem to come out in random order. This surprised me. But in thinking about the definition of SQL itself, I guess there's no guarantee that sort order is maintained across sub-selects. I was caught by this because in Oracle, this same query works "correctly" (i.e. the hitlist ends up in sorted order), but I suspect that was just the luck of their implementation.

Can anyone confirm this, that the sort order is NOT guaranteed to be maintained through layers of SELECT statements?

The apparent solution is to make the hitlist.sortorder column have nextval() as its default and eliminate the first sub-select. But I thought the two would be equivalent.

Thanks,
Craig

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2006-06-30 06:04:49 Re: explain analyze reports 20x more time than actual
Previous Message Craig A. James 2006-06-30 04:52:42 explain analyze reports 20x more time than actual