From: Tom Lane <tgl@sss.pgh.pa.us>
To: seandknutson@gmail.com
Cc: pgsql-bugs@postgresql.org, Kevin Grittner <kgrittn@gmail.com>
Subject: Re: BUG #14136: select distinct from a materialized view does not preserve result order
Date: Fri, 13 May 2016 18:17:42 -0400
Message-ID: <21571.1463177862@sss.pgh.pa.us> (raw)
In-Reply-To: <20160513214356.2842.6899@wrigleys.postgresql.org>
References: <20160513214356.2842.6899@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, seandknutson@gmail.com, kgrittn@gmail.com
Subject: Re: BUG #14136: select distinct from a materialized view does not preserve result order
In-Reply-To: <21571.1463177862@sss.pgh.pa.us>
References: <20160513214356.2842.6899@wrigleys.postgresql.org> <21571.1463177862@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox