Re: Do Layered Views/Relations Preserve Sort Order ?

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Charles Sheridan <cesheri(at)swbell(dot)net>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Do Layered Views/Relations Preserve Sort Order ?
Date: 2015-09-10 00:44:59
Message-ID: CAKFQuwZzCxvXxtk-gpW7nx46X758y=wbc=Sa2MuaSAcEHhXaTg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Sep 9, 2015 at 7:53 PM, Charles Sheridan <cesheri(at)swbell(dot)net> wrote:

> Hi All,
>
> When there are several views defined on top of each other, are SELECTs on
> views that do not specify a SORT order guaranteed to preserve the
> cumulative sort order of the lower-level views ?
>
> Is the answer true for any arbitrarily large set of layered views?
>
> Is the answer the same if the layers of relations are a mix of views and
> tables ?
>

​The answer to any question as broad and non-specific as yours is likely to
be answered wit​h a no.

The better question is how expensive is it to sort already sorted data. If
its cheap, and it likely is, then placing explicit sorting where you care
is the best solution regardless of your level of confidence that lower
level sorting is being maintained.

Since tables are never sorted I don't get why you think they enter into the
equation.

If ones operates under the guideline that only top-layer queries should
contain ORDER BY then your whole structure is unsound. Presumably those
queries you rely upon are or were themselves considered top-level queries
at one point and now you are adding a dependent to them that they likely
were never intended to consider. Simplification queries should not use
ORDER BY unless it is necessary to implement their logic. A query whose
logic depends on order really should declare that fact.

David J.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Fetter 2015-09-10 00:47:19 Re: [PATCH] SQL function to report log message
Previous Message Charles Sheridan 2015-09-09 23:53:31 Do Layered Views/Relations Preserve Sort Order ?