Thanks for the feedback. I only had passing familiarity with materialized views and I didn't even realize the order would not be preserved. All the more reason to drop that.
I'm attaching two patches: the first drops the original ORDER BY I e-mailed about, and the second applies Tom's change to the man page note (verbatim, though with "materialized view" since the "matview" shorthand doesn't seem to be used in the docs, and with markup). On Tue, Nov 23, 2021 at 10:11 AM Jonathan S. Katz <jk...@postgresql.org> wrote: > On 11/23/21 12:44 PM, Tom Lane wrote: > > Peter Eisentraut <peter.eisentr...@enterprisedb.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. > > +1 > > > 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. > > +1. I think I got bit by this in the real world years back. The above > comment is pretty clear. > > Thanks, > > Jonathan >
commit 45d7ca562daf91f8028335ade2e730bb59d25ba0 Author: Maciek Sakrejda <m.sakre...@gmail.com> Date: Sun Nov 28 16:37:05 2021 -0800 Drop confusing ORDER BY in materialized view documentation diff --git a/doc/src/sgml/rules.sgml b/doc/src/sgml/rules.sgml index 6065b1c2a3..4aa4e00e01 100644 --- a/doc/src/sgml/rules.sgml +++ b/doc/src/sgml/rules.sgml @@ -961,9 +961,6 @@ CREATE MATERIALIZED VIEW sales_summary AS FROM invoice WHERE invoice_date < CURRENT_DATE GROUP BY - seller_no, - invoice_date - ORDER BY seller_no, invoice_date;
commit bef99e3f6c73fc5a64e46bc5902de5346f298047 Author: Maciek Sakrejda <m.sakre...@gmail.com> Date: Sun Nov 28 16:43:42 2021 -0800 Update note on materialized view ORDER BY diff --git a/doc/src/sgml/ref/refresh_materialized_view.sgml b/doc/src/sgml/ref/refresh_materialized_view.sgml index 3bf8884447..4aa5558636 100644 --- a/doc/src/sgml/ref/refresh_materialized_view.sgml +++ b/doc/src/sgml/ref/refresh_materialized_view.sgml @@ -93,12 +93,10 @@ REFRESH MATERIALIZED VIEW [ CONCURRENTLY ] <replaceable class="parameter">name</ <title>Notes</title> <para> - While the default index for future - <link linkend="sql-cluster"><command>CLUSTER</command></link> - operations is retained, <command>REFRESH MATERIALIZED VIEW</command> does not - order the generated rows based on this property. If you want the data - to be ordered upon generation, you must use an <literal>ORDER BY</literal> - clause in the backing query. + If there is an <literal>ORDER BY</literal> clause in the materialized view's + defining query, the original contents of the materialized view will be ordered + that way; but <command>REFRESH MATERIALIZED VIEW</command> does not guarantee + to preserve that ordering. </para> </refsect1>