refresh_immv(immv_name, with_data) is a function to refresh IMMV like REFRESH MATERIALIZED VIEW command. It has two argument. immv_name is incrementally maintainable materialized view's name, and with_data is an option that is corresponding to the WITH [NO] DATA option. When with_data is set false, the IMMV gets unpopulated. One of differences between IMMVs unpopulated by this function and normal materialized views unpopulated by REFRESH ... WITH NO DATA is that such IMMVs can be referenced by SELECT but return no rows, while unpopulated materialized views are not scanable. The behaviour may be changed in future to raise an error when unpopulated an IMMV is scanned.
105 lines
1.5 KiB
Text
105 lines
1.5 KiB
Text
CREATE TABLE t (i int PRIMARY KEY);
|
|
INSERT INTO t SELECT generate_series(1, 5);
|
|
SELECT create_immv('mv', 'SELECT * FROM t');
|
|
NOTICE: created index "mv_index" on immv "mv"
|
|
create_immv
|
|
-------------
|
|
5
|
|
(1 row)
|
|
|
|
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1;
|
|
immvrelid | ispopulated
|
|
-----------+-------------
|
|
mv | t
|
|
(1 row)
|
|
|
|
-- refresh immv without changing the ispopulated flag
|
|
SELECT refresh_immv('mv', true);
|
|
refresh_immv
|
|
--------------
|
|
5
|
|
(1 row)
|
|
|
|
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1;
|
|
immvrelid | ispopulated
|
|
-----------+-------------
|
|
mv | t
|
|
(1 row)
|
|
|
|
INSERT INTO t VALUES(6);
|
|
SELECT i FROM mv ORDER BY 1;
|
|
i
|
|
---
|
|
1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
(6 rows)
|
|
|
|
-- change ispopulated to False
|
|
SELECT refresh_immv('mv', false);
|
|
refresh_immv
|
|
--------------
|
|
0
|
|
(1 row)
|
|
|
|
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1;
|
|
immvrelid | ispopulated
|
|
-----------+-------------
|
|
mv | f
|
|
(1 row)
|
|
|
|
SELECT i FROM mv ORDER BY 1;
|
|
i
|
|
---
|
|
(0 rows)
|
|
|
|
-- immv remains empty
|
|
INSERT INTO t VALUES(7);
|
|
SELECT i FROM mv ORDER BY 1;
|
|
i
|
|
---
|
|
(0 rows)
|
|
|
|
-- chaneg ispopulated to True, immv is updated
|
|
SELECT refresh_immv('mv', true);
|
|
refresh_immv
|
|
--------------
|
|
7
|
|
(1 row)
|
|
|
|
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1;
|
|
immvrelid | ispopulated
|
|
-----------+-------------
|
|
mv | t
|
|
(1 row)
|
|
|
|
SELECT i FROM mv ORDER BY 1;
|
|
i
|
|
---
|
|
1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
(7 rows)
|
|
|
|
-- immediate maintenance
|
|
INSERT INTO t VALUES(8);
|
|
SELECT i FROM mv ORDER BY 1;
|
|
i
|
|
---
|
|
1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
(8 rows)
|
|
|