Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4687E1337B80 for ; Fri, 29 Apr 2011 05:07:15 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 80303-10 for ; Fri, 29 Apr 2011 08:07:04 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by mail.postgresql.org (Postfix) with ESMTP id 6D04C1337B4E for ; Fri, 29 Apr 2011 05:07:02 -0300 (ADT) Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by wwwmaster.postgresql.org (8.14.3/8.14.3) with ESMTP id p3T87245079124 for ; Fri, 29 Apr 2011 08:07:02 GMT (envelope-from www@wwwmaster.postgresql.org) Received: (from www@localhost) by wwwmaster.postgresql.org (8.14.3/8.14.3/Submit) id p3T872Gp079123; Fri, 29 Apr 2011 08:07:02 GMT (envelope-from www) Date: Fri, 29 Apr 2011 08:07:02 GMT Message-Id: <201104290807.p3T872Gp079123@wwwmaster.postgresql.org> To: pgsql-bugs@postgresql.org Subject: BUG #5997: [queryplan] PostgreSQL is sorting the query results when the result can only be 1 item. From: "Michiel" Content-Type: text/plain; charset=utf-8 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 201104/214 X-Sequence-Number: 30133 The following bug has been logged online: Bug reference: 5997 Logged by: Michiel Email address: michiel@ict4schools.nl PostgreSQL version: 8.4.7 Operating system: Linux Description: [queryplan] PostgreSQL is sorting the query results when the result can only be 1 item. Details: PostgreSQL is sorting the query results when the result can only be 1 item. Table scheme: CREATE TABLE dashboards ( id SERIAL NOT NULL PRIMARY KEY, uuid uuid NOT NULL, name varchar(50) NOT NULL, title varchar(100) NOT NULL, description text, approved boolean DEFAULT FALSE, ispublic boolean DEFAULT FALSE, readonly boolean DEFAULT FALSE, creationtime TIMESTAMP NOT NULL DEFAULT NOW(), modificationtime TIMESTAMP NOT NULL DEFAULT NOW(), UNIQUE(uuid), UNIQUE(name) ); Query: SELECT "dashboards".* FROM dashboards WHERE "dashboards"."id" = 1 ORDER BY "dashboards"."title" ASC LIMIT 1 OFFSET 0 Explain analyze: Limit (cost=8.28..8.28 rows=1 width=407) (actual time=0.084..0.084 rows=1 loops=1) -> Sort (cost=8.28..8.28 rows=1 width=407) (actual time=0.082..0.082 rows=1 loops=1) Sort Key: title Sort Method: quicksort Memory: 25kB -> Index Scan using dashboards_pkey on dashboards (cost=0.00..8.27 rows=1 width=407) (actual time=0.042..0.043 rows=1 loops=1) Index Cond: (id = 1) Explain analyze without ORDER BY: Limit (cost=0.00..8.27 rows=1 width=407) (actual time=0.045..0.045 rows=1 loops=1) -> Index Scan using dashboards_pkey on dashboards (cost=0.00..8.27 rows=1 width=407) (actual time=0.041..0.041 rows=1 loops=1) Index Cond: (id = 1) What i see and expect: I see that the query is sorted. But the result is max 1 item. Because id is a primary key, which is unique. Which always results in 1 row. Then i whould say that the query optimiser could leave the sorting out of it. so for the query optimiser it should be: if (rows > 1) sort(); this is also for selecting on unique columns. Because sorting 1 row is nonsence. ps. The query is rendered by an ORM, so not custom made. Else I would leave the ORDER BY out of it. Can someone also test this on version 9.0.3?