Re: select random order by random

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: select random order by random
Date: 2007-11-01 18:07:12
Message-ID: 20071101180712.GE1955@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Nov 01, 2007 at 04:49:16PM +0000, Richard Huxton wrote:
> Gregory Stark wrote:
> >This does strike me as wrong. random() is marked volatile and the planner
> >ought not collapse multiple calls into one.
>
> I think I agree with the earlier poster. Surely these two queries should
> be equivalent?
>
> SELECT random() FROM generate_series(1, 10) ORDER BY random();
> SELECT random() AS foo FROM generate_series(1, 10) ORDER BY foo;

If they were pure (in the functional programming sense) then this would
be a correct optimisation. However, if they're marked as volatile then
they should be called independently---they're not pure anymore and
you're calling the code for its side-effects and optimising out the
either call changes the semantics. Try playing around with monads in
Haskell or uniqueness types in Clean, they help to clarify what's going
on when you call a "function" in an impure language.

Sam

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-11-01 18:22:58 Re: select random order by random
Previous Message Bruce Momjian 2007-11-01 17:25:21 Re: [GENERAL] Abbreviation list