Re: Unexpected sort order.

From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Ron Mayer <rm_pg(at)cheapcomplexdevices(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Unexpected sort order.
Date: 2006-11-28 00:46:12
Message-ID: 1164674772.7773.48.camel@dogma.v10.wvs
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-general

On Mon, 2006-11-27 at 17:05 -0500, Tom Lane wrote:
> Jeff Davis <pgsql(at)j-davis(dot)com> writes:
> > On Mon, 2006-11-27 at 12:44 -0800, Ron Mayer wrote:
> >> Shouldn't the results of this query shown here been sorted by "b" rather than by "a"?
>
> >> li=# select * from (select (random()*10)::int as a, (random()*10)::int as b from generate_series(1,10) order by a) as x order by b;
>
> > It looks like a planner bug.
>
> It looks to me like the planner thinks that order by a and order by b
> are equivalent because the expressions are equal(); hence it discards
> what it thinks is a redundant second sort step.
>
> I suppose we could add a check for whether the sort expression contains
> volatile functions before believing this, but I'm having a hard time
> believing that there are any real-world cases where the check wouldn't
> be a waste of cycles. What's the use-case for sorting by a volatile
> expression in the first place?

The only use case that I can think of is avoiding surprise during
testing. random() and generate_series() are probably used rarely in real
applications, as with any other volatile function aside from the
sequence functions. However, they're frequently used when developing and
testing applications.

The only reason I mention this is because it might not always be so
obvious when it's a sorting problem. If you do a GROUP BY, and it's
grouping by the wrong column, I could see how that could be very
confusing[1].

Granted, this is all for hypothetical, contrived testing scenarios. So
it's not very compelling if it requires significant work to implement.

Regards,
Jeff Davis

[1] This result certainly doesn't make much sense, although I suppose
the query doesn't either:

=> select sum(a) as aa,b from (select distinct (random()*10)::int as a,
(random()*10)::int as b from generate_series(1,10)) x group by b;
aa | b
----+----
1 | 3
1 | 5
2 | 4
7 | 8
7 | 10
8 | 6
8 | 10
9 | 1
9 | 6
9 | 8
(10 rows)

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Jeff Davis 2006-11-28 02:22:41 Re: [BUGS] Out of memory error causes Abort, Abort tries
Previous Message Lucian Capdefier 2006-11-28 00:21:27 BUG #2789: problem with delete statement

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2006-11-28 00:50:25 Re: NULLs ;-)
Previous Message Michael Glaesemann 2006-11-28 00:45:42 Re: NULLs ;-)