pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: seandknutson@gmail.com
To: pgsql-bugs@postgresql.org
Subject: BUG #14136: select distinct from a materialized view does not preserve result order
Date: Fri, 13 May 2016 21:43:56 +0000
Message-ID: <20160513214356.2842.6899@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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



view thread (4+ messages)  latest in thread

Message-ID: <20160513214356.2842.6899@wrigleys.postgresql.org>
Permalink:  ../20160513214356.2842.6899@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20160513214356.2842.6899@wrigleys.postgresql.org

 · 

reply

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: seandknutson@gmail.com
  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>

* 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