Skip Orderby Execution for Materialized Views

Started by Zhang Minglialmost 3 years ago4 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

appliestests failedCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t48533
psql -h localhost -U postgres

Built from patchset v1 (message #1), September 20, 2026 at 05:03 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t48533_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t48533_1 && git checkout t48533_1

Patchset v1 (message #1) is on t48533_1

Jump to latest
#1Zhang Mingli
zmlpostgres@gmail.com

Hi, all

When create  or refresh a Materialized View, if the view’s query has order by, we may sort and insert the sorted data into view.

Create Materialized View mv1 as select c1, c2 from t1 order by c2;
Refresh Materialized View mv1;

And it appears that we could get ordered data  when select from Materialized View;

Select * from mv1;

But it’s not true if we use other access methods, or we choose a parallel seqscan plan.
A non-parallel seqscan on heap table appears ordered as we always create new rel file and swap them, in my opinion, it’s more like a free lunch.

So, conclusion1:  We couldn’t rely on the `ordered-data` even the mv’s sql has order by clause, is it right?

And if it’s true, shall we skip the order by clause for Materialized View  when executing create/refresh statement?

Materialized View’s order by clause could be skipped if

1. Order by clause is on the top query level
2. There is no real limit clause

The benefit is the query may be speeded up without sort nodes each time creating/refreshing Materialized View.

A simple results:

create table t1 as select i as c1 , i/2 as c2 , i/5 as c3 from generate_series(1, 100000) i;
create materialized view mvt1_order as select c1, c2, c3 from t1 order by c2, c3, c1 asc

Without this patch:
zml=# refresh materialized view mvt1_order;
REFRESH MATERIALIZED VIEW
Time: 228.548 ms
zml=# refresh materialized view mvt1_order;
REFRESH MATERIALIZED VIEW
Time: 230.374 ms
zml=# refresh materialized view mvt1_order;
REFRESH MATERIALIZED VIEW
Time: 217.079 ms

With this patch:

zml=# refresh materialized view mvt1_order;
REFRESH MATERIALIZED VIEW
Time: 192.409 ms
zml=# refresh materialized view mvt1_order;
REFRESH MATERIALIZED VIEW
Time: 204.398 ms
zml=# refresh materialized view mvt1_order;
REFRESH MATERIALIZED VIEW
Time: 197.510 ms

Zhang Mingli
www.hashdata.xyz

Attachments:

t48533_1
v1-01-Skip-Orderby-clause-execution-for-Materialized-Views.patchapplication/octet-streamDownload+9-1
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Zhang Mingli (#1)
Re: Skip Orderby Execution for Materialized Views

Zhang Mingli <zmlpostgres@gmail.com> writes:

When create  or refresh a Materialized View, if the view’s query has order by, we may sort and insert the sorted data into view.

Indeed.

And if it’s true, shall we skip the order by clause for Materialized View  when executing create/refresh statement?

No. The intent of a materialized view is to execute the query
as presented. If the user doesn't understand the consequences
of that, it's not our job to think we are smarter than they are.

I think this patch should be rejected.

BTW, I'm pretty certain that this patch breaks some valid cases
even if we take your point of view as correct. For one example,
you can't just remove the sort clause if the query uses DISTINCT ON.

regards, tom lane

#3Zhang Mingli
zmlpostgres@gmail.com
In reply to: Tom Lane (#2)
Re: Skip Orderby Execution for Materialized Views

HI,

On Oct 1, 2023, at 22:54, Tom Lane <tgl@sss.pgh.pa.us> wrote:

For one example,
you can't just remove the sort clause if the query uses DISTINCT ON

Hi, Tom, got it, thanks,

Zhang Mingli
HashData https://www.hashdata.xyz

#4David G. Johnston
david.g.johnston@gmail.com
In reply to: Zhang Mingli (#1)
Re: Skip Orderby Execution for Materialized Views

On Sun, Oct 1, 2023 at 8:57 AM Zhang Mingli <zmlpostgres@gmail.com> wrote:

And if it’s true, shall we skip the order by clause for Materialized
View when executing create/refresh statement?

We tend to do precisely what the user writes into their query. If they
don't want an order by they can remove it. I don't see any particular
reason we should be second-guessing them here. And what makes the trade-off
worse is the reasonable expectation that we'd provide a way to force an
ordering of the inserts should the user actually want it after we defaulted
to ignoring that part of their query.

But yes, you are correct that adding an order by to a materialized view is
typically pointless. To the extent it is detrimental varies since even
partially ordered results can save on processing time.

David J.