Re: Stable sort?

From: "Dann Corbit" <DCorbit(at)connx(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Stable sort?
Date: 2006-11-08 19:53:37
Message-ID: D425483C2C5C9F49B5B7A41F8944154757DD44@postal.corporate.connx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> -----Original Message-----
> From: pgsql-general-owner(at)postgresql(dot)org [mailto:pgsql-general-
> owner(at)postgresql(dot)org] On Behalf Of Tom Lane
> Sent: Wednesday, November 08, 2006 7:05 AM
> To: redhog
> Cc: pgsql-general(at)postgresql(dot)org
> Subject: Re: [GENERAL] Stable sort?
>
> "redhog" <redhog(at)redhog(dot)org> writes:
> > My question was if the order of two
> > elements whose internal order is not affected by the current
ordering
> > clause, still may change places due to technicalities.
>
> Postgres usually sorts using qsort(), which (on most platforms) is not
> stable in that sense.

This is (of course) what we would expect from any SQL database.

From ISO/IEC ISO/IEC 9075-2:1999 (E) Section 4.29 Cursors on page 71, we
have this:
"A cursor in the open state identifies a table, an ordering of the rows
of that table, and a position relative to that ordering. If the <declare
cursor> does not contain an <order by clause>, or contains an <order by
clause> that does not specify the order of the rows completely, then the
rows of the table have an order that is defined only to the extent that
the <order by clause> specifies an order and is otherwise
implementation-dependent.
When the ordering of a cursor is not defined by an <order by clause>,
the relative position of two rows is implementation-dependent. When the
ordering of a cursor is partially determined by an <order by clause>,
then the relative positions of two rows are determined only by the
<order by clause>; if the two rows have equal values for the purpose of
evaluating the <order by clause>, then their relative positions are
implementation-dependent."

This is talking about cursors specifically, but the same ordering
principles would obviously hold throughout.

The standard does not prevent the use of a stable sort, but it also does
not require it.

Obviously, if you order by all the result columns, then the orderings
will always be identical (though this would in general be a bad idea).

In response to

Browse pgsql-general by date

  From Date Subject
Next Message novnov 2006-11-08 19:54:43 Re: Table and Field namestyle best practices?
Previous Message Tom Lane 2006-11-08 19:42:53 Re: "Broken" Plan? Nested Loop IN Join vs. Nested Loop/Hash Aggregate