matview.out 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
-- create a table to use as a basis for views and materialized views in various combinations
CREATE TABLE t (id int NOT NULL PRIMARY KEY, type text NOT NULL, amt numeric NOT NULL);
INSERT INTO t VALUES
  (1, 'x', 2),
  (2, 'x', 3),
  (3, 'y', 5),
  (4, 'y', 7),
  (5, 'z', 11);
-- 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;
11
SELECT * FROM tv ORDER BY type;
12 13
 type | totamt 
------+--------
14
 x    |      5
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 y    |     12
 z    |     11
(3 rows)

-- create a materialized view with no data, and confirm correct behavior
EXPLAIN (costs off)
  CREATE MATERIALIZED VIEW tm AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type WITH NO DATA;
     QUERY PLAN      
---------------------
 HashAggregate
   ->  Seq Scan on t
(2 rows)

CREATE MATERIALIZED VIEW tm AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type WITH NO DATA;
SELECT pg_relation_is_scannable('tm'::regclass);
 pg_relation_is_scannable 
--------------------------
 f
(1 row)

SELECT * FROM tm;
ERROR:  materialized view "tm" has not been populated
HINT:  Use the REFRESH MATERIALIZED VIEW command.
REFRESH MATERIALIZED VIEW tm;
SELECT pg_relation_is_scannable('tm'::regclass);
 pg_relation_is_scannable 
--------------------------
 t
(1 row)

CREATE UNIQUE INDEX tm_type ON tm (type);
SELECT * FROM tm;
 type | totamt 
------+--------
 y    |     12
 z    |     11
 x    |      5
(3 rows)

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

65
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
66 67 68
SELECT * FROM tvm;
 type | totamt 
------+--------
69
 x    |      5
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
 y    |     12
 z    |     11
(3 rows)

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 VIEW tvv AS SELECT sum(totamt) AS grandtot FROM tv;
EXPLAIN (costs off)
  CREATE MATERIALIZED VIEW tvvm AS SELECT * FROM tvv;
        QUERY PLAN         
---------------------------
 Aggregate
   ->  HashAggregate
         ->  Seq Scan on t
(3 rows)

CREATE MATERIALIZED VIEW tvvm AS SELECT * FROM tvv;
CREATE VIEW tvvmv AS SELECT * FROM tvvm;
CREATE MATERIALIZED VIEW bb AS SELECT * FROM tvvmv;
CREATE INDEX aa ON bb (grandtot);
-- check that plans seem reasonable
\d+ tvm
                    Materialized view "public.tvm"
 Column |  Type   | Modifiers | Storage  | Stats target | Description 
--------+---------+-----------+----------+--------------+-------------
 type   | text    |           | extended |              | 
 totamt | numeric |           | main     |              | 
View definition:
 SELECT tv.type, 
    tv.totamt
100 101
   FROM tv
  ORDER BY tv.type;
102 103 104 105 106 107 108 109 110 111

\d+ tvm
                    Materialized view "public.tvm"
 Column |  Type   | Modifiers | Storage  | Stats target | Description 
--------+---------+-----------+----------+--------------+-------------
 type   | text    |           | extended |              | 
 totamt | numeric |           | main     |              | 
View definition:
 SELECT tv.type, 
    tv.totamt
112 113
   FROM tv
  ORDER BY tv.type;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

\d+ tvvm
                    Materialized view "public.tvvm"
  Column  |  Type   | Modifiers | Storage | Stats target | Description 
----------+---------+-----------+---------+--------------+-------------
 grandtot | numeric |           | main    |              | 
View definition:
 SELECT tvv.grandtot
   FROM tvv;

\d+ bb
                     Materialized view "public.bb"
  Column  |  Type   | Modifiers | Storage | Stats target | Description 
----------+---------+-----------+---------+--------------+-------------
 grandtot | numeric |           | main    |              | 
Indexes:
    "aa" btree (grandtot)
View definition:
 SELECT tvvmv.grandtot
   FROM tvvmv;

-- test schema behavior
CREATE SCHEMA mvschema;
ALTER MATERIALIZED VIEW tvm SET SCHEMA mvschema;
\d+ tvm
\d+ tvmm
                    Materialized view "public.tvmm"
  Column  |  Type   | Modifiers | Storage | Stats target | Description 
----------+---------+-----------+---------+--------------+-------------
 grandtot | numeric |           | main    |              | 
View definition:
 SELECT sum(tvm.totamt) AS grandtot
   FROM mvschema.tvm;

SET search_path = mvschema, public;
\d+ tvm
                   Materialized view "mvschema.tvm"
 Column |  Type   | Modifiers | Storage  | Stats target | Description 
--------+---------+-----------+----------+--------------+-------------
 type   | text    |           | extended |              | 
 totamt | numeric |           | main     |              | 
View definition:
 SELECT tv.type, 
    tv.totamt
158 159
   FROM tv
  ORDER BY tv.type;
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

-- modify the underlying table data
INSERT INTO t VALUES (6, 'z', 13);
-- confirm pre- and post-refresh contents of fairly simple materialized views
SELECT * FROM tm ORDER BY type;
 type | totamt 
------+--------
 x    |      5
 y    |     12
 z    |     11
(3 rows)

SELECT * FROM tvm ORDER BY type;
 type | totamt 
------+--------
 x    |      5
 y    |     12
 z    |     11
(3 rows)

REFRESH MATERIALIZED VIEW tm;
REFRESH MATERIALIZED VIEW tvm;
SELECT * FROM tm ORDER BY type;
 type | totamt 
------+--------
 x    |      5
 y    |     12
 z    |     24
(3 rows)

SELECT * FROM tvm ORDER BY type;
 type | totamt 
------+--------
 x    |      5
 y    |     12
 z    |     24
(3 rows)

RESET search_path;
-- confirm pre- and post-refresh contents of nested materialized views
EXPLAIN (costs off)
  SELECT * FROM tmm;
   QUERY PLAN    
-----------------
 Seq Scan on tmm
(1 row)

EXPLAIN (costs off)
  SELECT * FROM tvmm;
    QUERY PLAN    
------------------
 Seq Scan on tvmm
(1 row)

EXPLAIN (costs off)
  SELECT * FROM tvvm;
    QUERY PLAN    
------------------
 Seq Scan on tvvm
(1 row)

SELECT * FROM tmm;
 grandtot 
----------
       28
(1 row)

SELECT * FROM tvmm;
 grandtot 
----------
       28
(1 row)

SELECT * FROM tvvm;
 grandtot 
----------
       28
(1 row)

REFRESH MATERIALIZED VIEW tmm;
REFRESH MATERIALIZED VIEW tvmm;
REFRESH MATERIALIZED VIEW tvvm;
EXPLAIN (costs off)
  SELECT * FROM tmm;
   QUERY PLAN    
-----------------
 Seq Scan on tmm
(1 row)

EXPLAIN (costs off)
  SELECT * FROM tvmm;
    QUERY PLAN    
------------------
 Seq Scan on tvmm
(1 row)

EXPLAIN (costs off)
  SELECT * FROM tvvm;
    QUERY PLAN    
------------------
 Seq Scan on tvvm
(1 row)

SELECT * FROM tmm;
 grandtot 
----------
       41
(1 row)

SELECT * FROM tvmm;
 grandtot 
----------
       41
(1 row)

SELECT * FROM tvvm;
 grandtot 
----------
       41
(1 row)

-- test diemv when the mv does not exist
DROP MATERIALIZED VIEW IF EXISTS tum;
NOTICE:  materialized view "tum" does not exist, skipping
-- make sure that an unlogged materialized view works (in the absence of a crash)
CREATE UNLOGGED MATERIALIZED VIEW tum AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type WITH NO DATA;
SELECT pg_relation_is_scannable('tum'::regclass);
 pg_relation_is_scannable 
--------------------------
 f
(1 row)

SELECT * FROM tum;
ERROR:  materialized view "tum" has not been populated
HINT:  Use the REFRESH MATERIALIZED VIEW command.
REFRESH MATERIALIZED VIEW tum;
SELECT pg_relation_is_scannable('tum'::regclass);
 pg_relation_is_scannable 
--------------------------
 t
(1 row)

SELECT * FROM tum;
 type | totamt 
------+--------
 y    |     12
 z    |     24
 x    |      5
(3 rows)

REFRESH MATERIALIZED VIEW tum WITH NO DATA;
SELECT pg_relation_is_scannable('tum'::regclass);
 pg_relation_is_scannable 
--------------------------
 f
(1 row)

SELECT * FROM tum;
ERROR:  materialized view "tum" has not been populated
HINT:  Use the REFRESH MATERIALIZED VIEW command.
REFRESH MATERIALIZED VIEW tum WITH DATA;
SELECT pg_relation_is_scannable('tum'::regclass);
 pg_relation_is_scannable 
--------------------------
 t
(1 row)

SELECT * FROM tum;
 type | totamt 
------+--------
 y    |     12
 z    |     24
 x    |      5
(3 rows)

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;
337 338
 type | mtot | vtot 
------+------+------
339
 x    |    5 |    5
340 341 342 343
 y    |   12 |   12
 z    |   24 |   24
(3 rows)

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
-- test diemv when the mv does exist
DROP MATERIALIZED VIEW IF EXISTS tum;
-- make sure that dependencies are reported properly when they block the drop
DROP TABLE t;
ERROR:  cannot drop table t because other objects depend on it
DETAIL:  view tv depends on table t
view tvv depends on view tv
materialized view tvvm depends on view tvv
view tvvmv depends on materialized view tvvm
materialized view bb depends on view tvvmv
materialized view mvschema.tvm depends on view tv
materialized view tvmm depends on materialized view mvschema.tvm
materialized view tm depends on table t
materialized view tmm depends on materialized view tm
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
-- make sure dependencies are dropped and reported
-- and make sure that transactional behavior is correct on rollback
-- incidentally leaving some interesting materialized views for pg_dump testing
BEGIN;
DROP TABLE t CASCADE;
NOTICE:  drop cascades to 9 other objects
DETAIL:  drop cascades to view tv
drop cascades to view tvv
drop cascades to materialized view tvvm
drop cascades to view tvvmv
drop cascades to materialized view bb
drop cascades to materialized view mvschema.tvm
drop cascades to materialized view tvmm
drop cascades to materialized view tm
drop cascades to materialized view tmm
ROLLBACK;
-- some additional tests not using base tables
CREATE VIEW v_test1 AS SELECT 1 moo;
CREATE VIEW v_test2 AS SELECT moo, 2*moo FROM v_test1 UNION ALL SELECT moo, 3*moo FROM v_test1;
\d+ v_test2
                 View "public.v_test2"
  Column  |  Type   | Modifiers | Storage | Description 
----------+---------+-----------+---------+-------------
 moo      | integer |           | plain   | 
 ?column? | integer |           | plain   | 
View definition:
         SELECT v_test1.moo, 
            2 * v_test1.moo
           FROM v_test1
UNION ALL 
         SELECT v_test1.moo, 
            3 * v_test1.moo
           FROM v_test1;

CREATE MATERIALIZED VIEW mv_test2 AS SELECT moo, 2*moo FROM v_test2 UNION ALL SELECT moo, 3*moo FROM v_test2;
\d+ mv_test2
                  Materialized view "public.mv_test2"
  Column  |  Type   | Modifiers | Storage | Stats target | Description 
----------+---------+-----------+---------+--------------+-------------
 moo      | integer |           | plain   |              | 
 ?column? | integer |           | plain   |              | 
View definition:
         SELECT v_test2.moo, 
            2 * v_test2.moo
           FROM v_test2
UNION ALL 
         SELECT v_test2.moo, 
            3 * v_test2.moo
           FROM v_test2;

CREATE MATERIALIZED VIEW mv_test3 AS SELECT * FROM mv_test2 WHERE moo = 12345;
SELECT pg_relation_is_scannable('mv_test3'::regclass);
 pg_relation_is_scannable 
--------------------------
 t
(1 row)

DROP VIEW v_test1 CASCADE;
NOTICE:  drop cascades to 3 other objects
DETAIL:  drop cascades to view v_test2
drop cascades to materialized view mv_test2
drop cascades to materialized view mv_test3
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
-- test that vacuum does not make empty matview look unpopulated
CREATE TABLE hoge (i int);
INSERT INTO hoge VALUES (generate_series(1,100000));
CREATE MATERIALIZED VIEW hogeview AS SELECT * FROM hoge WHERE i % 2 = 0;
CREATE INDEX hogeviewidx ON hogeview (i);
DELETE FROM hoge;
REFRESH MATERIALIZED VIEW hogeview;
SELECT * FROM hogeview WHERE i < 10;
 i 
---
(0 rows)

VACUUM ANALYZE;
SELECT * FROM hogeview WHERE i < 10;
 i 
---
(0 rows)

DROP TABLE hoge CASCADE;
NOTICE:  drop cascades to materialized view hogeview