1 -- create a table to use as a basis for views and materialized views in various combinations
2 CREATE TABLE t (id int NOT NULL PRIMARY KEY, type text NOT NULL, amt numeric NOT NULL);
9 -- we want a view based on the table, too, since views present additional challenges
10 CREATE VIEW tv AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type;
11 SELECT * FROM tv ORDER BY type;
19 -- create a materialized view with no data, and confirm correct behavior
21 CREATE MATERIALIZED VIEW tm AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type WITH NO DATA;
28 CREATE MATERIALIZED VIEW tm AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type WITH NO DATA;
29 SELECT pg_relation_is_scannable('tm'::regclass);
30 pg_relation_is_scannable
31 --------------------------
36 ERROR: materialized view "tm" has not been populated
37 HINT: Use the REFRESH MATERIALIZED VIEW command.
38 REFRESH MATERIALIZED VIEW tm;
39 SELECT pg_relation_is_scannable('tm'::regclass);
40 pg_relation_is_scannable
41 --------------------------
45 CREATE UNIQUE INDEX tm_type ON tm (type);
54 -- create various views
56 CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
58 ---------------------------
65 CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
74 CREATE MATERIALIZED VIEW tmm AS SELECT sum(totamt) AS grandtot FROM tm;
75 CREATE MATERIALIZED VIEW tvmm AS SELECT sum(totamt) AS grandtot FROM tvm;
76 CREATE VIEW tvv AS SELECT sum(totamt) AS grandtot FROM tv;
78 CREATE MATERIALIZED VIEW tvvm AS SELECT * FROM tvv;
80 ---------------------------
86 CREATE MATERIALIZED VIEW tvvm AS SELECT * FROM tvv;
87 CREATE VIEW tvvmv AS SELECT * FROM tvvm;
88 CREATE MATERIALIZED VIEW bb AS SELECT * FROM tvvmv;
89 CREATE INDEX aa ON bb (grandtot);
90 -- check that plans seem reasonable
92 Materialized view "public.tvm"
93 Column | Type | Modifiers | Storage | Stats target | Description
94 --------+---------+-----------+----------+--------------+-------------
95 type | text | | extended | |
96 totamt | numeric | | main | |
104 Materialized view "public.tvm"
105 Column | Type | Modifiers | Storage | Stats target | Description
106 --------+---------+-----------+----------+--------------+-------------
107 type | text | | extended | |
108 totamt | numeric | | main | |
116 Materialized view "public.tvvm"
117 Column | Type | Modifiers | Storage | Stats target | Description
118 ----------+---------+-----------+---------+--------------+-------------
119 grandtot | numeric | | main | |
125 Materialized view "public.bb"
126 Column | Type | Modifiers | Storage | Stats target | Description
127 ----------+---------+-----------+---------+--------------+-------------
128 grandtot | numeric | | main | |
130 "aa" btree (grandtot)
132 SELECT tvvmv.grandtot
135 -- test schema behavior
136 CREATE SCHEMA mvschema;
137 ALTER MATERIALIZED VIEW tvm SET SCHEMA mvschema;
140 Materialized view "public.tvmm"
141 Column | Type | Modifiers | Storage | Stats target | Description
142 ----------+---------+-----------+---------+--------------+-------------
143 grandtot | numeric | | main | |
145 SELECT sum(tvm.totamt) AS grandtot
148 SET search_path = mvschema, public;
150 Materialized view "mvschema.tvm"
151 Column | Type | Modifiers | Storage | Stats target | Description
152 --------+---------+-----------+----------+--------------+-------------
153 type | text | | extended | |
154 totamt | numeric | | main | |
161 -- modify the underlying table data
162 INSERT INTO t VALUES (6, 'z', 13);
163 -- confirm pre- and post-refresh contents of fairly simple materialized views
164 SELECT * FROM tm ORDER BY type;
172 SELECT * FROM tvm ORDER BY type;
180 REFRESH MATERIALIZED VIEW tm;
181 REFRESH MATERIALIZED VIEW tvm;
182 SELECT * FROM tm ORDER BY type;
190 SELECT * FROM tvm ORDER BY type;
199 -- confirm pre- and post-refresh contents of nested materialized views
239 REFRESH MATERIALIZED VIEW tmm;
240 REFRESH MATERIALIZED VIEW tvmm;
241 REFRESH MATERIALIZED VIEW tvvm;
281 -- test diemv when the mv does not exist
282 DROP MATERIALIZED VIEW IF EXISTS tum;
283 NOTICE: materialized view "tum" does not exist, skipping
284 -- make sure that an unlogged materialized view works (in the absence of a crash)
285 CREATE UNLOGGED MATERIALIZED VIEW tum AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type WITH NO DATA;
286 SELECT pg_relation_is_scannable('tum'::regclass);
287 pg_relation_is_scannable
288 --------------------------
293 ERROR: materialized view "tum" has not been populated
294 HINT: Use the REFRESH MATERIALIZED VIEW command.
295 REFRESH MATERIALIZED VIEW tum;
296 SELECT pg_relation_is_scannable('tum'::regclass);
297 pg_relation_is_scannable
298 --------------------------
310 REFRESH MATERIALIZED VIEW tum WITH NO DATA;
311 SELECT pg_relation_is_scannable('tum'::regclass);
312 pg_relation_is_scannable
313 --------------------------
318 ERROR: materialized view "tum" has not been populated
319 HINT: Use the REFRESH MATERIALIZED VIEW command.
320 REFRESH MATERIALIZED VIEW tum WITH DATA;
321 SELECT pg_relation_is_scannable('tum'::regclass);
322 pg_relation_is_scannable
323 --------------------------
335 -- test join of mv and view
336 SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type) ORDER BY type;
344 -- test diemv when the mv does exist
345 DROP MATERIALIZED VIEW IF EXISTS tum;
346 -- make sure that dependencies are reported properly when they block the drop
348 ERROR: cannot drop table t because other objects depend on it
349 DETAIL: view tv depends on table t
350 view tvv depends on view tv
351 materialized view tvvm depends on view tvv
352 view tvvmv depends on materialized view tvvm
353 materialized view bb depends on view tvvmv
354 materialized view mvschema.tvm depends on view tv
355 materialized view tvmm depends on materialized view mvschema.tvm
356 materialized view tm depends on table t
357 materialized view tmm depends on materialized view tm
358 HINT: Use DROP ... CASCADE to drop the dependent objects too.
359 -- make sure dependencies are dropped and reported
360 -- and make sure that transactional behavior is correct on rollback
361 -- incidentally leaving some interesting materialized views for pg_dump testing
363 DROP TABLE t CASCADE;
364 NOTICE: drop cascades to 9 other objects
365 DETAIL: drop cascades to view tv
366 drop cascades to view tvv
367 drop cascades to materialized view tvvm
368 drop cascades to view tvvmv
369 drop cascades to materialized view bb
370 drop cascades to materialized view mvschema.tvm
371 drop cascades to materialized view tvmm
372 drop cascades to materialized view tm
373 drop cascades to materialized view tmm
375 -- some additional tests not using base tables
376 CREATE VIEW v_test1 AS SELECT 1 moo;
377 CREATE VIEW v_test2 AS SELECT moo, 2*moo FROM v_test1 UNION ALL SELECT moo, 3*moo FROM v_test1;
379 View "public.v_test2"
380 Column | Type | Modifiers | Storage | Description
381 ----------+---------+-----------+---------+-------------
382 moo | integer | | plain |
383 ?column? | integer | | plain |
393 CREATE MATERIALIZED VIEW mv_test2 AS SELECT moo, 2*moo FROM v_test2 UNION ALL SELECT moo, 3*moo FROM v_test2;
395 Materialized view "public.mv_test2"
396 Column | Type | Modifiers | Storage | Stats target | Description
397 ----------+---------+-----------+---------+--------------+-------------
398 moo | integer | | plain | |
399 ?column? | integer | | plain | |
409 CREATE MATERIALIZED VIEW mv_test3 AS SELECT * FROM mv_test2 WHERE moo = 12345;
410 SELECT pg_relation_is_scannable('mv_test3'::regclass);
411 pg_relation_is_scannable
412 --------------------------
416 DROP VIEW v_test1 CASCADE;
417 NOTICE: drop cascades to 3 other objects
418 DETAIL: drop cascades to view v_test2
419 drop cascades to materialized view mv_test2
420 drop cascades to materialized view mv_test3
421 -- test that vacuum does not make empty matview look unpopulated
422 CREATE TABLE hoge (i int);
423 INSERT INTO hoge VALUES (generate_series(1,100000));
424 CREATE MATERIALIZED VIEW hogeview AS SELECT * FROM hoge WHERE i % 2 = 0;
425 CREATE INDEX hogeviewidx ON hogeview (i);
427 REFRESH MATERIALIZED VIEW hogeview;
428 SELECT * FROM hogeview WHERE i < 10;
434 SELECT * FROM hogeview WHERE i < 10;
439 DROP TABLE hoge CASCADE;
440 NOTICE: drop cascades to materialized view hogeview