Skip to content

Instantly share code, notes, and snippets.

@cxxr
Created February 17, 2016 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cxxr/86c1b16acc52f1235d3a to your computer and use it in GitHub Desktop.
Save cxxr/86c1b16acc52f1235d3a to your computer and use it in GitHub Desktop.
PostgreSQL Inconsistent Lexicographic Sort
Run this SQL:
CREATE TEMPORARY TABLE SORTER ( name VARCHAR );
INSERT INTO SORTER (name) VALUES ('A B');
INSERT INTO SORTER (name) VALUES ('AB');
INSERT INTO SORTER (name) VALUES ('A C');
INSERT INTO SORTER (name) VALUES ('AC');
SELECT * FROM SORTER ORDER BY name;
SELECT * FROM SORTER ORDER BY name DESC;
On OSX PostgreSQL 9.4.1:
name
------
A B
A C
AB
AC
(4 rows)
name
------
AC
AB
A C
A B
(4 rows)
On Linux PostgreSQL 9.4.4:
name
------
AB
A B
AC
A C
(4 rows)
name
------
A C
AC
A B
AB
(4 rows)
@cxxr
Copy link
Author

cxxr commented Feb 17, 2016

On Linux PostgreSQL 9.3.11, results are like OS X.

Locale is always en_US.UTF-8.

@decibel
Copy link

decibel commented Feb 17, 2016

Confirmed with 9.5.0 on both and client_encoding=SQL_ASCII

I suspect this is due to an underlying library difference, although the OS level sort command seems to do the same thing on both platform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment