Re: ORDER BY in materialized view example?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>
Cc: Maciek Sakrejda <m(dot)sakrejda(at)gmail(dot)com>, pgsql-docs(at)postgresql(dot)org
Subject: Re: ORDER BY in materialized view example?
Date: 2021-11-23 17:44:56
Message-ID: 38996.1637689496@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com> writes:
> On 23.11.21 07:18, Maciek Sakrejda wrote:
>> An example in the materialized view documentation [1] includes an ORDER
>> BY clause without a clear reason. Does it help build the index more
>> efficiently? I suppose it's also sort of like a CLUSTER?

> I agree the ORDER BY is not relevant to the example. There might be
> some implementation-dependent advantage to ordering a materialized view,
> but if there is, it isn't explained in the example.

Yeah. It would result in the initial contents of the matview being
ordered, but I'm sure we don't wish to guarantee that REFRESH would
preserve that. I'm on board with just removing the ORDER BY from
that example.

There is also this rather opaque "note" in the REFRESH MATERIALIZED VIEW
man page:

While the default index for future CLUSTER operations is retained,
REFRESH MATERIALIZED VIEW does not order the generated rows based on
this property. If you want the data to be ordered upon generation, you
must use an ORDER BY clause in the backing query.

I'd rather say something like

If there is an ORDER BY clause in the matview's defining query,
the original contents of the matview will be ordered that way;
but REFRESH MATERIALIZED VIEW does not guarantee to preserve
that ordering.

regards, tom lane

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Jonathan S. Katz 2021-11-23 18:11:08 Re: ORDER BY in materialized view example?
Previous Message Peter Eisentraut 2021-11-23 16:06:32 Re: ORDER BY in materialized view example?