Commit 241139ae authored by Kevin Grittner's avatar Kevin Grittner

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.
parent 1a091002
...@@ -8,12 +8,12 @@ INSERT INTO t VALUES ...@@ -8,12 +8,12 @@ INSERT INTO t VALUES
(5, 'z', 11); (5, 'z', 11);
-- we want a view based on the table, too, since views present additional challenges -- we want a view based on the table, too, since views present additional challenges
CREATE VIEW tv AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type; CREATE VIEW tv AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type;
SELECT * FROM tv; SELECT * FROM tv ORDER BY type;
type | totamt type | totamt
------+-------- ------+--------
x | 5
y | 12 y | 12
z | 11 z | 11
x | 5
(3 rows) (3 rows)
-- create a materialized view with no data, and confirm correct behavior -- create a materialized view with no data, and confirm correct behavior
...@@ -53,20 +53,22 @@ SELECT * FROM tm; ...@@ -53,20 +53,22 @@ SELECT * FROM tm;
-- create various views -- create various views
EXPLAIN (costs off) EXPLAIN (costs off)
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv; CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
QUERY PLAN QUERY PLAN
--------------------- ---------------------------
HashAggregate Sort
-> Seq Scan on t Sort Key: t.type
(2 rows) -> HashAggregate
-> Seq Scan on t
(4 rows)
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv; CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
SELECT * FROM tvm; SELECT * FROM tvm;
type | totamt type | totamt
------+-------- ------+--------
x | 5
y | 12 y | 12
z | 11 z | 11
x | 5
(3 rows) (3 rows)
CREATE MATERIALIZED VIEW tmm AS SELECT sum(totamt) AS grandtot FROM tm; CREATE MATERIALIZED VIEW tmm AS SELECT sum(totamt) AS grandtot FROM tm;
...@@ -95,7 +97,8 @@ CREATE INDEX aa ON bb (grandtot); ...@@ -95,7 +97,8 @@ CREATE INDEX aa ON bb (grandtot);
View definition: View definition:
SELECT tv.type, SELECT tv.type,
tv.totamt tv.totamt
FROM tv; FROM tv
ORDER BY tv.type;
\d+ tvm \d+ tvm
Materialized view "public.tvm" Materialized view "public.tvm"
...@@ -106,7 +109,8 @@ View definition: ...@@ -106,7 +109,8 @@ View definition:
View definition: View definition:
SELECT tv.type, SELECT tv.type,
tv.totamt tv.totamt
FROM tv; FROM tv
ORDER BY tv.type;
\d+ tvvm \d+ tvvm
Materialized view "public.tvvm" Materialized view "public.tvvm"
...@@ -151,7 +155,8 @@ SET search_path = mvschema, public; ...@@ -151,7 +155,8 @@ SET search_path = mvschema, public;
View definition: View definition:
SELECT tv.type, SELECT tv.type,
tv.totamt tv.totamt
FROM tv; FROM tv
ORDER BY tv.type;
-- modify the underlying table data -- modify the underlying table data
INSERT INTO t VALUES (6, 'z', 13); INSERT INTO t VALUES (6, 'z', 13);
...@@ -328,12 +333,12 @@ SELECT * FROM tum; ...@@ -328,12 +333,12 @@ SELECT * FROM tum;
(3 rows) (3 rows)
-- test join of mv and view -- test join of mv and view
SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type); SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type) ORDER BY type;
type | mtot | vtot type | mtot | vtot
------+------+------ ------+------+------
x | 5 | 5
y | 12 | 12 y | 12 | 12
z | 24 | 24 z | 24 | 24
x | 5 | 5
(3 rows) (3 rows)
-- test diemv when the mv does exist -- test diemv when the mv does exist
......
...@@ -9,7 +9,7 @@ INSERT INTO t VALUES ...@@ -9,7 +9,7 @@ INSERT INTO t VALUES
-- we want a view based on the table, too, since views present additional challenges -- we want a view based on the table, too, since views present additional challenges
CREATE VIEW tv AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type; CREATE VIEW tv AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type;
SELECT * FROM tv; SELECT * FROM tv ORDER BY type;
-- create a materialized view with no data, and confirm correct behavior -- create a materialized view with no data, and confirm correct behavior
EXPLAIN (costs off) EXPLAIN (costs off)
...@@ -24,8 +24,8 @@ SELECT * FROM tm; ...@@ -24,8 +24,8 @@ SELECT * FROM tm;
-- create various views -- create various views
EXPLAIN (costs off) EXPLAIN (costs off)
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv; CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv; CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
SELECT * FROM tvm; SELECT * FROM tvm;
CREATE MATERIALIZED VIEW tmm AS SELECT sum(totamt) AS grandtot FROM tm; CREATE MATERIALIZED VIEW tmm AS SELECT sum(totamt) AS grandtot FROM tm;
CREATE MATERIALIZED VIEW tvmm AS SELECT sum(totamt) AS grandtot FROM tvm; CREATE MATERIALIZED VIEW tvmm AS SELECT sum(totamt) AS grandtot FROM tvm;
...@@ -104,7 +104,7 @@ SELECT pg_relation_is_scannable('tum'::regclass); ...@@ -104,7 +104,7 @@ SELECT pg_relation_is_scannable('tum'::regclass);
SELECT * FROM tum; SELECT * FROM tum;
-- test join of mv and view -- test join of mv and view
SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type); SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type) ORDER BY type;
-- test diemv when the mv does exist -- test diemv when the mv does exist
DROP MATERIALIZED VIEW IF EXISTS tum; DROP MATERIALIZED VIEW IF EXISTS tum;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment