Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b1L84-0000YL-Q8 for pgsql-bugs@arkaria.postgresql.org; Fri, 13 May 2016 22:00:16 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b1L84-0007iP-D5 for pgsql-bugs@arkaria.postgresql.org; Fri, 13 May 2016 22:00:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b1KsP-0002O8-Oj for pgsql-bugs@postgresql.org; Fri, 13 May 2016 21:44:05 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b1KsN-0000sP-9G for pgsql-bugs@postgresql.org; Fri, 13 May 2016 21:44:05 +0000 MIME-Version: 1.0 Subject: BUG #14136: select distinct from a materialized view does not preserve result order To: pgsql-bugs@postgresql.org From: seandknutson@gmail.com Date: Fri, 13 May 2016 21:43:56 +0000 Message-ID: <20160513214356.2842.6899@wrigleys.postgresql.org> X-Pg-Spam-Score: -0.3 (/) Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" 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: 14136 Logged by: Sean Email address: seandknutson@gmail.com PostgreSQL version: 9.5.0 Operating system: Arch Linux Description: It seems that ordering is lost when doing a "select distinct" from a materialized view. Say I have a table called "test" that contains id | name ----+------- 1 | steve 2 | adam 3 | jim 4 | steve 5 | adam 6 | sean and a materialized view defined as create materialized view view_test as (select * from test order by name); If I run select distinct name from view_test; I get name ------- steve adam sean jim instead of name ------- adam jim sean steve like I'd expect. If I have a non-materialized view with the same definition, the query returns name ------- adam jim sean steve as expected. For simple cases, the workaround is relatively simple: just reapply the same "order by" in the "select distinct.." query. E.g. select distinct name from view_test order by name; However, if the ordering defined in the mat view is complex, it may be difficult to replicate, or even impossible (say if the order is based on a column from a table that isn't included in the "select" part of the view definition). -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs