From 0d9b9754a40cb9993f8b98c96d4e8126e7730df3 Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Thu, 7 Jan 2016 11:13:09 -0500 Subject: [PATCH] Use an ORDER BY clause for user-visible queries > If sorting is not chosen, the rows will be returned in an unspecified > order. The actual order in that case will depend on the scan and join > plan types and the order on disk, but it must not be relied on. A > particular output ordering can only be guaranteed if the sort step is > explicitly chosen. http://www.postgresql.org/docs/8.3/static/queries-order.html --- best-practices/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/best-practices/README.md b/best-practices/README.md index 7a6e5fed..f547ff82 100644 --- a/best-practices/README.md +++ b/best-practices/README.md @@ -165,6 +165,9 @@ Postgres * Consider a [partial index] for queries on booleans. * Constrain most columns as [`NOT NULL`]. * [Index foreign keys]. +* Use an `ORDER BY` clause on queries where the results will be displayed to a + user, as queries without one may return results in a changing, arbitrary + order. [`NOT NULL`]: http://www.postgresql.org/docs/9.1/static/ddl-constraints.html#AEN2444 [combines multiple indexes]: http://www.postgresql.org/docs/9.1/static/indexes-bitmap-scans.html