RE: configure postgtresql to order NULLS FIRST instead of the default NULLS LAST

From: "Vianello, Daniel A" <Daniel(dot)Vianello(at)charter(dot)com>
To: Jay Riddle <jcriddle4(at)yahoo(dot)com>, Vik Fearing <vik(dot)fearing(at)2ndquadrant(dot)com>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, john snow <ofbizfanster(at)gmail(dot)com>
Cc: pgsql-novice <pgsql-novice(at)postgresql(dot)org>
Subject: RE: configure postgtresql to order NULLS FIRST instead of the default NULLS LAST
Date: 2019-01-04 15:44:41
Message-ID: c40c7dbd4fca48a79a8a53427ef6d330@NCEMEXGP001.CORP.CHARTERCOM.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

According to the documentation on SELECT, this can be done a per query basis using the ORDER BY … NULLS FIRST/LAST clause. This syntax was first added in version 8.3 (rather than using a CASE statement), but it is still per query, and not globally on the server.

https://www.postgresql.org/docs/current/sql-select.html#SQL-ORDERBY

> If NULLS LAST is specified, null values sort after all non-null values; if NULLS FIRST is specified, null values sort before all non-null values. If neither is specified, the default behavior is NULLS LAST when ASC is specified or implied, and NULLS FIRST when DESC is specified (thus, the default is to act as though nulls are larger than non-nulls). When USING is specified, the default nulls ordering depends on whether the operator is a less-than or greater-than operator.

There is also syntax that supports using your own operator. If all of your data that is likely to be ordered is of a user-defined data type then you could try creating your own types, domains, and operators that treat nulls however you want, but that is non-trivial and would only apply automatically to columns of those custom data types, and not standard data types like INTEGER, TEXT, etc.

> Alternatively, a specific ordering operator name can be specified in the USING clause. An ordering operator must be a less-than or greater-than member of some B-tree operator family. ASC is usually equivalent to USING < and DESC is usually equivalent to USING >. (But the creator of a user-defined data type can define exactly what the default sort ordering is, and it might correspond to operators with other names.)

Of course you can work on training users and developers on the use of NULLS FIRST/LAST. Or you could create views to do complex sorting on a table and have users use the view instead of the table, but that is still extra development per table, not your apparently desired global change of behavior.

E-MAIL CONFIDENTIALITY NOTICE:
The contents of this e-mail message and any attachments are intended solely for the addressee(s) and may contain confidential and/or legally privileged information. If you are not the intended recipient of this message or if this message has been addressed to you in error, please immediately alert the sender by reply e-mail and then delete this message and any attachments. If you are not the intended recipient, you are notified that any use, dissemination, distribution, copying, or storage of this message or any attachment is strictly prohibited.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message David G. Johnston 2019-01-04 15:58:13 Re: configure postgtresql to order NULLS FIRST instead of the default NULLS LAST
Previous Message Stephen Froehlich 2019-01-04 13:24:53 WAL on zfs Settings