Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wesyu-000224-E6 for pgsql-bugs@arkaria.postgresql.org; Mon, 28 Apr 2014 21:20:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Wesys-00056Y-Rd for pgsql-bugs@arkaria.postgresql.org; Mon, 28 Apr 2014 21:20:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wer9q-0006TO-4F for pgsql-bugs@postgresql.org; Mon, 28 Apr 2014 19:24:06 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wer9n-0002MN-C0 for pgsql-bugs@postgresql.org; Mon, 28 Apr 2014 19:24:05 +0000 MIME-Version: 1.0 Subject: BUG #10164: Inconsistent Order When Limit is Applied To: pgsql-bugs@postgresql.org From: sluggy.fan@gmail.com Date: Mon, 28 Apr 2014 19:23:20 +0000 Message-ID: <20140428192320.2661.1832@wrigleys.postgresql.org> X-Pg-Spam-Score: 1.1 (+) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 10164 Logged by: Brad Lindsay Email address: sluggy.fan@gmail.com PostgreSQL version: 9.3.4 Operating system: OS X Description: I have a query where the order is inconsistent when different LIMITs are specified. Here's the setup: CREATE TABLE "storage_tests" ( "id" SERIAL, "name" TEXT DEFAULT NULL, "field1" TEXT DEFAULT NULL, PRIMARY KEY (id) ); INSERT INTO storage_tests (id, name, field1) VALUES (1, 'name', 'z'), (2, 'name', 'a'), (3, 'named', 't'), (4, 'names', 'b'), (5, 'blank', 'c'), (6, 'Harry', 'e'), (7, 'Rose', 'w'), (8, 'Andrew', 'O'), (9, 'George', 'L'), (10, 'Rachel', 'M'), (11, 'John', 'm'), (12, 'Anne', 'a'), (13, 'Susan', 'i'), (14, 'Justin', 'M'), (15, 'Virginia', 'a'), (16, 'Cynthia', 'J'), (17, 'John', 't'), (18, 'Cynthia', 'R'), (19, 'Robert', 'P'), (20, 'Victor', 'B'), (21, 'Henry', 'i'), (22, 'Mark', 'e'), (23, 'Albert', 'J'), (24, 'Lynn', 'a'), (25, 'Tanya', 'ob'), (26, 'Michael', 'C'), (27, 'Tony', 'e'), (28, 'Dave', 't'), (29, 'Robbin', 'C'), (30, 'Robert', 'B'), (31, 'Ryan', NULL); Issuing this SQL: SELECT * FROM "storage_tests" ORDER BY LOWER("field1") DESC NULLS LAST Results in the first four IDs being in this order: 1, 7, 3, 17 When I add a LIMIT of 3, everything still looks good: SELECT * FROM "storage_tests" ORDER BY LOWER("field1") DESC NULLS LAST OFFSET 0 LIMIT 3 Results in the ids in this order (as expected): 1, 7, 3 However, when I LIMIT 4, the id of 3 moves to the fourth position: SELECT * FROM "storage_tests" ORDER BY LOWER("field1") DESC NULLS LAST OFFSET 0 LIMIT 4 Results in the following (unexpected) order: 1, 7, 17, 3 If I increase to 5, it drops to the fifth position: 1, 7, 17, 28, 3 If I increase to 6 (the point where there's another row with a different value in field1) it jumps back to the third position: 1, 7, 3, 17, 28, 18 The documentation states that when the ordered fields all have the same value, that the order is implementation-dependent, but shouldn't that implementation be consistent no matter what the LIMIT is? -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs