Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b1LP5-0001Iw-Sc for pgsql-bugs@arkaria.postgresql.org; Fri, 13 May 2016 22:17:51 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b1LP5-0000Q9-FO for pgsql-bugs@arkaria.postgresql.org; Fri, 13 May 2016 22:17:51 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b1LP4-0000OJ-Px for pgsql-bugs@postgresql.org; Fri, 13 May 2016 22:17:50 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b1LOy-0008Lg-BN for pgsql-bugs@postgresql.org; Fri, 13 May 2016 22:17:49 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u4DMHg0M021572; Fri, 13 May 2016 18:17:42 -0400 From: Tom Lane To: seandknutson@gmail.com cc: pgsql-bugs@postgresql.org, Kevin Grittner Subject: Re: BUG #14136: select distinct from a materialized view does not preserve result order In-reply-to: <20160513214356.2842.6899@wrigleys.postgresql.org> References: <20160513214356.2842.6899@wrigleys.postgresql.org> Comments: In-reply-to seandknutson@gmail.com message dated "Fri, 13 May 2016 21:43:56 -0000" Date: Fri, 13 May 2016 18:17:42 -0400 Message-ID: <21571.1463177862@sss.pgh.pa.us> X-Pg-Spam-Score: -3.3 (---) 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 seandknutson@gmail.com writes: > It seems that ordering is lost when doing a "select distinct" from a > materialized view. SELECT DISTINCT doesn't promise to preserve order in any context, matview or otherwise. If you want a particular output ordering you need to say ORDER BY explicitly in the query. Otherwise the planner is free to do the DISTINCT via hashing, as it evidently did here. (Actually, it's free to do it by hashing anyhow; but with ORDER BY it'd then have to re-sort.) > and a materialized view defined as > create materialized view view_test as (select * from test order by name); AFAIK, a matview is just a table, and even if you define it like that there's no guarantee that the rows in the matview will be physically ordered by name. Certainly, if you haven't created an index on the matview, the planner will assume that it must either sort-and-unique or hash in order to do the DISTINCT correctly ... and unless the table is too large for the hashtable to fit in memory, it's likely to think the hash approach is preferable. This seems like a documentation shortcoming, though, as the documentation doesn't really say that a matview won't preserve row order (or at least I see nothing about it in the CREATE MATERIALIZED VIEW ref page --- maybe it's mentioned somewhere else?) regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs