diff -u b/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php --- b/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php @@ -53,6 +53,9 @@ */ public function orderBy($field, $direction = 'ASC') { // Only allow ASC and DESC, default to ASC. + // Emulate MySQL default behavior to sort NULL values first for ascending, + // and last for descending. + // @see http://www.postgresql.org/docs/9.3/static/queries-order.html $direction = strtoupper($direction) == 'DESC' ? 'DESC NULLS LAST' : 'ASC NULLS FIRST'; $this->order[$field] = $direction; diff -u b/core/modules/system/src/Tests/Database/SelectComplexTest.php b/core/modules/system/src/Tests/Database/SelectComplexTest.php --- b/core/modules/system/src/Tests/Database/SelectComplexTest.php +++ b/core/modules/system/src/Tests/Database/SelectComplexTest.php @@ -221,7 +221,8 @@ // Check that the ordering clause is handled properly. $orderby = $query->getOrderBy(); - // The orderby string is different for postgreSQL. + // The orderby string is different for PostgreSQL. + // @see Drupal\Core\Database\Driver\pgsql\Select::orderBy() $db_type = Database::getConnection()->databaseType(); $this->assertEqual($orderby['name'], ($db_type == 'pgsql' ? 'ASC NULLS FIRST' : 'ASC'), 'Query correctly sets ordering clause.'); $orderby = $count->getOrderBy();