Re: any way for ORDER BY x to imply NULLS FIRST in 8.3? - Mailing list pgsql-general

From Tom Lane
Subject Re: any way for ORDER BY x to imply NULLS FIRST in 8.3?
Date
Msg-id 19134.1194360517@sss.pgh.pa.us
Whole thread Raw
In response to any way for ORDER BY x to imply NULLS FIRST in 8.3?  (rihad <rihad@mail.ru>)
Responses Re: any way for ORDER BY x to imply NULLS FIRST in 8.3?  (Simon Riggs <simon@2ndquadrant.com>)
List pgsql-general
rihad <rihad@mail.ru> writes:
> Is there any way to "hardcode" the NULL handling in an index (as  per
> http://www.postgresql.org/docs/8.3/static/indexes-ordering.html) so that
> SELECT * FROM t ORDER BY foo automatically implies NULLS FIRST (and,
> similarly so that SELECT * FROM t ORDER BY foo DESC automatically
> implies NULLS LAST)?

No.  This is not a question of how indexes behave, it is a question of
which semantics the parser ascribes to an undecorated ORDER BY request.

> Thing is, I'm using PHP Symfony/Propel to generate
> their SQL and have no easy way to influence how they do so.

SQL99 section 14.1 <declare cursor> saith:

              ... Whether
              a sort key value that is null is considered greater or less
              than a non-null value is implementation-defined, but all sort
              key values that are null shall either be considered greater
              than all non-null values or be considered less than all non-
              null values. ...

Essentially the same text appears in SQL2003.  Any application that
depends on one particular choice here is therefore broken, or at least
has chosen to work with only about half of the DBMSes in the world.

            regards, tom lane

pgsql-general by date:

Previous
From: SHARMILA JOTHIRAJAH
Date:
Subject: Re: Postgresql simple query performance question
Next
From: "Scott Marlowe"
Date:
Subject: Re: Selecting all but a few fields in a query