Skip to content

Commit 241139a

Browse files
committed
Use ORDER BY on matview definitions were needed for stable plans.
Per report from Hadi Moshayedi of matview regression test failure with optimization of aggregates. A few ORDER BY clauses improve code coverage for matviews while solving that problem.
1 parent 1a09100 commit 241139a

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/test/regress/expected/matview.out

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ INSERT INTO t VALUES
88
(5, 'z', 11);
99
-- we want a view based on the table, too, since views present additional challenges
1010
CREATE VIEW tv AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type;
11-
SELECT * FROM tv;
11+
SELECT * FROM tv ORDER BY type;
1212
type | totamt
1313
------+--------
14+
x | 5
1415
y | 12
1516
z | 11
16-
x | 5
1717
(3 rows)
1818

1919
-- create a materialized view with no data, and confirm correct behavior
@@ -53,20 +53,22 @@ SELECT * FROM tm;
5353

5454
-- create various views
5555
EXPLAIN (costs off)
56-
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv;
57-
QUERY PLAN
58-
---------------------
59-
HashAggregate
60-
-> Seq Scan on t
61-
(2 rows)
56+
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
57+
QUERY PLAN
58+
---------------------------
59+
Sort
60+
Sort Key: t.type
61+
-> HashAggregate
62+
-> Seq Scan on t
63+
(4 rows)
6264

63-
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv;
65+
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
6466
SELECT * FROM tvm;
6567
type | totamt
6668
------+--------
69+
x | 5
6770
y | 12
6871
z | 11
69-
x | 5
7072
(3 rows)
7173

7274
CREATE MATERIALIZED VIEW tmm AS SELECT sum(totamt) AS grandtot FROM tm;
@@ -95,7 +97,8 @@ CREATE INDEX aa ON bb (grandtot);
9597
View definition:
9698
SELECT tv.type,
9799
tv.totamt
98-
FROM tv;
100+
FROM tv
101+
ORDER BY tv.type;
99102

100103
\d+ tvm
101104
Materialized view "public.tvm"
@@ -106,7 +109,8 @@ View definition:
106109
View definition:
107110
SELECT tv.type,
108111
tv.totamt
109-
FROM tv;
112+
FROM tv
113+
ORDER BY tv.type;
110114

111115
\d+ tvvm
112116
Materialized view "public.tvvm"
@@ -151,7 +155,8 @@ SET search_path = mvschema, public;
151155
View definition:
152156
SELECT tv.type,
153157
tv.totamt
154-
FROM tv;
158+
FROM tv
159+
ORDER BY tv.type;
155160

156161
-- modify the underlying table data
157162
INSERT INTO t VALUES (6, 'z', 13);
@@ -328,12 +333,12 @@ SELECT * FROM tum;
328333
(3 rows)
329334

330335
-- test join of mv and view
331-
SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type);
336+
SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type) ORDER BY type;
332337
type | mtot | vtot
333338
------+------+------
339+
x | 5 | 5
334340
y | 12 | 12
335341
z | 24 | 24
336-
x | 5 | 5
337342
(3 rows)
338343

339344
-- test diemv when the mv does exist

src/test/regress/sql/matview.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ INSERT INTO t VALUES
99

1010
-- we want a view based on the table, too, since views present additional challenges
1111
CREATE VIEW tv AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type;
12-
SELECT * FROM tv;
12+
SELECT * FROM tv ORDER BY type;
1313

1414
-- create a materialized view with no data, and confirm correct behavior
1515
EXPLAIN (costs off)
@@ -24,8 +24,8 @@ SELECT * FROM tm;
2424

2525
-- create various views
2626
EXPLAIN (costs off)
27-
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv;
28-
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv;
27+
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
28+
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
2929
SELECT * FROM tvm;
3030
CREATE MATERIALIZED VIEW tmm AS SELECT sum(totamt) AS grandtot FROM tm;
3131
CREATE MATERIALIZED VIEW tvmm AS SELECT sum(totamt) AS grandtot FROM tvm;
@@ -104,7 +104,7 @@ SELECT pg_relation_is_scannable('tum'::regclass);
104104
SELECT * FROM tum;
105105

106106
-- test join of mv and view
107-
SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type);
107+
SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type) ORDER BY type;
108108

109109
-- test diemv when the mv does exist
110110
DROP MATERIALIZED VIEW IF EXISTS tum;

0 commit comments

Comments
 (0)